#include <iostream>
using namespace std;
int IsLeapY(int y){
    if(y%400==0 || (y%4==0 && y%100!=0)) return 1;
    else return 0;
}
int day[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 dayOfYear(int x){
    if(IsLeapY(x)==1) return 366;
    else return 365;
}
int main() {
    int yr,mh,d,l,m;
    cin>>m;
    while(m>0){
        cin>>yr>>mh>>d>>l;
        m--;
        while(l>dayOfYear(yr)) {
            l-=dayOfYear(yr);
            yr++;
        }
        while(l>day[IsLeapY(yr)][mh]){
            l-=day[IsLeapY(yr)][mh];
            if(mh==12){
            	yr++;
            	mh=1;
			}
            else mh++;
        }
        d+=l;
        if(d>day[IsLeapY(yr)][mh]){
            d-=day[IsLeapY(yr)][mh];
            if(mh==12){
                mh=1;
                yr++;
            }
            else mh++;
        }
        printf("%04d-%02d-%02d",yr,mh,d);
        cout<<endl;
    }
    
}