#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
int read(){
int ans=0,f=1,c=getchar();
while(c<‘0‘||c>‘9‘){if(c==‘0‘) f=-1; c=getchar();}
while(c>=‘0‘&&c<=‘9‘){ans=ans*10+(c-‘0‘); c=getchar();}
return ans*f;
}
int T,n,y,r,ans;
char s[55];
int pd(int x){
if(x%400==0) return 1;
if(x%4==0&&x%100!=0) return 1;
return 0;
}
int main()
{
T=read();
while(T--){
scanf("%d-%d-%d",&n,&y,&r);
int k=0;
if((y<=2&&r<29)||(y<=1)){
for(int i=n+1;;i++){
if(pd(i-1)) k+=2;
else k++;
if(k%7==0){ans=i;break;}
}
}
else if(y>2){
for(int i=n+1;;i++){
if(pd(i)) k+=2;
else k++;
if(k%7==0){ans=i;break;}
}
}
if(y==2&&r==29){
for(int i=n+4;;i+=4){
if(!pd(i)) k+=4;
else k+=5;
if(pd(i)&&k%7==0){ans=i;break;}
}
}
printf("%d\n",ans);
}
return 0;
}