#include <iostream>
#include <iomanip>
using namespace std;

int daytable[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 main(){
    int n;
    cin>>n;
    int year,month,day,days;
    int leap;
    for(;n>0;n--){
        cin>>year>>month>>day>>days;
        leap=((year%4==0 && year%100!=0) || year%400==0);
        while(days+day>daytable[leap][month]){
            days-=daytable[leap][month];
            month+=1;
            if(month==13){
                year+=1;
                leap=((year%4==0 && year%100!=0) || year%400==0);
                month=1;
            }
        }
        day=day+days;
        cout<<year<<'-';
        cout<<setw(2)<<setfill('0')<<month;
        cout<<'-';
        cout<<setw(2)<<setfill('0')<<day<<endl;
        
    }
}