#include <iostream>
#include <cstdio>
using namespace std;
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 year, month, day;
    int number, casenumber;
    scanf("%d", &casenumber);
    while (casenumber--) {
        scanf("%d%d%d%d", &year, &month, &day, &number);
        int row = isLeapyear(year);
        for (int i = 0; i < month; ++i) {
            number += daytab[row][i];
        }
        number += day;
        if (number > (isLeapyear(year) + 365)) {
            number -= (isLeapyear(year) + 365);
            year++;//确定年份
        }
        month = 0;
        row = isLeapyear(year);
        while (number > daytab[row][month]) {
            number -= daytab[row][month];
            month++;//确定月份
        }
        day = number;
        printf("%04d-%02d-%02d\n", year, month, day);
    }
    return 0;
}