#include "cstdio"
#include "string"

using namespace std;

int days[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

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

void add_days(int curr_year, int curr_month, int curr_day, int left_days) {

    while (left_days > 0) {

        if (is_leap_year(curr_year)) {
            days[2] = 29;
        } else {
            days[2] = 28;
        }

        // 1. 日
        if ((curr_day + left_days) > days[curr_month]) {
            int gap = days[curr_month] - curr_day;
            left_days = left_days - gap;
            curr_day += gap;
            if (left_days > 0 && curr_day == days[curr_month]) {
                left_days = left_days - 1;
                curr_day = 1;
                curr_month++;
            }
            if (curr_month > 12) {
                curr_year++;
                curr_month = 1;
            }
        } else {
            curr_day += left_days;
            left_days -= left_days;
        }
    }
    printf("%04d-%02d-%02d", curr_year, curr_month, curr_day);

}



int main() {
    // 接下来m行每行 四个整数 分别表示年月日和累加的天数。
    int n;
    scanf("%d", &n);
    getchar();
    int year, month, day, num;
    for (int i = 0; i < n; ++i) {
        scanf("%d %d %d %d", &year, &month, &day, &num);
        add_days(year, month, day, num);
        printf("\n");
    }

    return 0;
}