#include<stdio.h>
int main()
{
int t,y,m,d,c,i;
scanf("%d",&t);
while(t--){
scanf("%d-%d-%d",&y,&m,&d);
c=0;
if(((y%4==0&&y%100!=0)||y%400==0)&&m==2&&d==29){
for(i=y+1;i<=9999;i++){
if((i%4==0&&i%100!=0)||i%400==0) c+=366;
else c+=365;
if(c%7==0&&((i%4==0&&i%100!=0)||i%400==0)){
printf("%d\n",i);
break;
}
}
continue;
}
if(m>2){
for(i=y+1;i<=9999;i++){
if((i%4==0&&i%100!=0)||i%400==0) c+=366;
else c+=365;
if(c%7==0){
printf("%d\n",i);
break;
}
}
continue;
}
if(m<=2){
for(i=y+1;i<=9999;i++){
if(((i-1)%4==0&&(i-1)%100!=0)||(i-1)%400==0) c+=366;
else c+=365;
if(c%7==0){
printf("%d\n",i);
break;
}
}
continue;
}
}
return 0;
}