include<stdio.h>
include<stdbool.h>
int daytab[2][13] =
{
{0,31,28,31,30,31,30,31,31,30,31,30,31},
{0,31,29,31,30,31,30,31,31,30,31,30,31}
};
bool isLeapYear(int year)
{
return (year%4 == 0&&year%100!=0)||(year%400 == 0);
}
int main()
{
int a,year,month,day,number,days;
scanf("%d",&a);
for(int i=0;i<a;i++)
{
while(scanf("%d\n%d%d%d",&year,&month,&day,&number)!=EOF)
{
days=day+number;
int row = isLeapYear(year);
while(days>daytab[row][month])
{
days-=daytab[row][month++];
if(month==13)
{
year++;
row=isLeapYear(year);
month=1;
}
}
printf("%04d-%02d-%02d\n",year,month,days);
}
}
return 0;
}