#include <iostream>
using namespace std;
int main()
{
	int a[13] = { 0,31,29,31,30,31,30,31,31,30,31,30,31,};
	int time, year, mon, day, num;
	scanf("%d", &time);
	for (int i = 0; i < time; i++)
	{
		scanf("%d %d %d %d", &year,&mon,&day,&num);
		int temp = num+day;
		int mon1 = mon;
		int year1 = year;
		while (temp > a[mon1])
		{
			if ((year1 % 4 == 0 && year1 % 100 != 0) || year1 % 400 == 0)
			{
				a[2] = 29;
			}
			else a[2] = 28;
			temp -= a[mon1];
			if (mon1 == 12)
			{
				year1++;
				mon1 = 1;
			}
			else
			{
				mon1++;
			}
		}
			printf("%d-%02d-%02d\n", year1,mon1,temp);
		}
	return 0;
	}