#include<iostream>
using namespace std;
bool isLeap(int year){
return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
}
int main(){
int year = 0;
int month = 0;
int day = 0;
int add = 0;
int months[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 years[2] = {365,366};
int m = 0;
cin>>m;
while(m--){
// 先进行年判断!
while(cin>>year>>month>>day>>add){
// 把前面的天数算上凑个整
int days = 0;
for(int i = 0; i < month; i++){
days += months[isLeap(year)][i];
}
days += day;
add += days; // 相当于在这一年的开头添加的数!
// 先对年处理!
while(add > years[isLeap(year)]){
add -= years[isLeap(year)];
year++;
}
// 再对月处理!
month = 0;
while(add > months[isLeap(year)][month]){
add -= months[isLeap(year)][month++];
}
day = add;
cout<<year;
cout<<"-";
if(month > 9 ){
cout<<month;
}else {
cout<<0<<month;
}
cout<<"-";
if(day > 9){
cout<<day;
}else {
cout<<0<<day;
}
cout<<endl;
}
}
return 0;
}