#include<stdio.h>
int Leap(int y) // 返回值为 1:是闰年,0:不是闰年
{
if((y%4==0&&y%100!=0)||(y%400==0)) return 1;
else return 0;
}
int main()
{
int t,s,N,Y;
while(scanf("%d",&t)!=EOF)
{
while(t--){
scanf("%d %d",&Y,&N);
if(Leap(Y)==1) s=1;
else s=0;
while(s<N){
Y++;
if(Leap(Y)==1) s++;
}
printf("%d\n",Y);
}
}
return 0;
}
int Leap(int y) // 返回值为 1:是闰年,0:不是闰年
{
if((y%4==0&&y%100!=0)||(y%400==0)) return 1;
else return 0;
}
int main()
{
int t,s,N,Y;
while(scanf("%d",&t)!=EOF)
{
while(t--){
scanf("%d %d",&Y,&N);
if(Leap(Y)==1) s=1;
else s=0;
while(s<N){
Y++;
if(Leap(Y)==1) s++;
}
printf("%d\n",Y);
}
}
return 0;
}