#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int,int>;
const int N = 1e6+10;
int t,n,m,k,l,r,op,x,y,d,a;
int f[N];
int months[13] = {-1,31,28,31,30,31,30,31,31,30,31,30,31};
bool isleapyear(int y){
return (y%4==0&&y%100!=0)||(y%400==0);
}
int getMd(int y,int m){
if(m==2){
return months[m]+isleapyear(y);
}
return months[m];
}
void solve(){
cin>>t;
while(cin>>y>>m>>d>>a){
d+=a;
while(d>getMd(y,m)){
d-=getMd(y,m);
m++;
if(m>12){
m=1;
y++;
}
}
printf("%04d-%02d-%02d\n",y,m,d);
}
}
int main(){
solve();
return 0;
}