#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <iostream>

using namespace std;

bool IsLeapYear(int year) {
    return (year % 400 == 0) || (year % 100 != 0 && year % 4 == 0);
}

int NumberOfYear(int year) {
    if (IsLeapYear(year)) {
        //printf("This is Leap Year %d\n", year);
        return 366;
    } else {
        //printf("This is Normal Year %d\n", year);
        return 365;
    }
}

int main() {
    int year, month, day;
    int number;
    int caseNumber;
    int month_day[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
    scanf("%d", &caseNumber);
    while (caseNumber--) {
        scanf("%d%d%d%d", &year, &month, &day, &number);
        bool leap = IsLeapYear(year);

        if (leap == true) { //注意判等式 = (用双等号 即==)
            month_day[2] = 29;
            //printf("1. This is Leap Year %d\n", year);
        } else {
            month_day[2] = 28;
            //printf("1. This is Normal Year %d\n", year);

        }

        for (int j = 0; j < month; ++j) {
            number += month_day[j];
        }

        number += day;
        while (number > NumberOfYear(year)) {
            //printf("2.\n");
            number -= NumberOfYear(year);
            year++;
        }

        leap = IsLeapYear(year);

        if (leap == true) {
            month_day[2] = 29;
            //printf("3. This is Leap Year %d\n", year);
        } else {
            month_day[2] = 28;
            //printf("3. This is Normal Year %d\n", year);

        }

        month = 0;
        while (number > month_day[month]) {
            number -= month_day[month];
            month++;
        }
        day = number;
        printf("%04d-%02d-%02d\n", year, month, day);
    }
    //system("pause");
    return EXIT_SUCCESS;
}
// 64 位输出请用 printf("%lld")