#include<iostream>
using namespace std;
int n;
int months[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
int is_leap(int y){
if((y%4==0 && y%100) || y%400==0) return 1;
return 0;
}
int daysOfMonth(int y,int m){
if(m==2) return months[m]+is_leap(y);
return months[m];
}
int main(){
cin>>n;
while(n--){
int y,m,d,cnt;
cin>>y>>m>>d>>cnt;
d += cnt;
while(d>daysOfMonth(y,m)){
d -= daysOfMonth(y,m);
m++;
if(m==13) m=1,y++;
}
printf("%04d-%02d-%02d\n",y,m,d);
}
}