#include <iostream>
//设计一个程序,他能够计算一个日期若干天后是什么日期
//输入第一行表示样例个数m,
//第二行是日期年月日+增加天数
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 m;//输入几行数据
    int year,month,day,number;//日期
    scanf("%d",&m);
    int i=0;
        while(i<m){
            scanf("%d %d %d %d",&year,&month,&day,&number);
        int row=(IsLeapYear(year));//判断闰年
        int tag=dayTab[row][month]-day;//计划先将第一个月填平,填成本月最后一天
        number=number-tag;
        day=dayTab[row][month];
        while(number>0){
            if(month<=12){
                    month += 1;
                    number = number - dayTab[row][month];
            }
           else{
               year+=1;
               row=(IsLeapYear(year));
               month=0;
        }
        }
        day=number+dayTab[row][month];
        printf("%04d-%02d-%02d\n",year,month,day);
        i+=1;
    }

    return 0;
}