#include<iostream>
#include<stdio.h>
using namespace std;
int main(){
int m;
cin>>m;
while(m){
int Y,M,N,cal;
int days=0;
cin>>Y>>M>>N>>cal;
bool leapyear=(Y%4==0&&Y%100!=0)||(Y%400==0);
int month[]={31,leapyear?29:28,31,30,31,30,31,31,30,31,30,31};
for(int i=0;i<M-1;i++){
days+=month[i];
}
days=days+N+cal;
while(days>=(leapyear?366:365)){
days=days-(leapyear?366:365);
Y++;
leapyear=(Y%4==0&&Y%100!=0)||(Y%400==0);
}
leapyear=(Y%4==0&&Y%100!=0)||(Y%400==0);
int remonth[]={31,leapyear?29:28,31,30,31,30,31,31,30,31,30,31};
int i=0;
int count=0;
while(i<12){
count=count+remonth[i];
if(count>=days){
M=i+1;
N=days-(count-remonth[i]);
break;
}
else i++;
}
printf("%04d-%02d-%02d\n",Y,M,N);
m--;
}
return 0;
}