#include<iostream>using namespace std;

int ifrun(int year){if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) return 1;else return 0;}

int main(){int n; cin >> n;while (n--){int y, m, d, day;cin >> y >> m >> d >> day;int md[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} };

    int num = d;
    for (int i = 1; i < m; i++) num += md[ifrun(y)][i]; //记录当前日期所在第几天

        y += (num + day) / (ifrun(y) + 365); //确定年份
        day -= md[ifrun(y)][m] - d; //减去当月还剩多少天
        while (day > md[ifrun(y)][((m%12)+1)]) //当下一个月够减时 循环相减 找到day天以后所在月份,当不够减时 即可找出所在月份的所在天数
        {
            m = (m% 12)+1; 
            day -= md[ifrun(y)][m];
        }
        printf("%04d-%02d-%02d\n", y, (m+1)%12, day);
}

}