#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<math.h>
#include<string.h>
const int inf = 0x3f3f3f;
using namespace std;
bool judge(int x)
{
if((x % 4 == 0 && x % 100 != 0) ||(x % 400 == 0))
return true;
return false;
}
int main()
{
int test,y,n,ans;
while(scanf("%d",&test) != EOF)
{
while(test--)
{
ans = 0;
scanf("%d%d",&y,&n);
for(int i = y ; ; i++)
{
if(judge(i))
{
ans++;
}
if(ans == n)
{
printf("%d\n",i);
break;
}
}
}
}
}
#include<stdlib.h>
#include<iostream>
#include<math.h>
#include<string.h>
const int inf = 0x3f3f3f;
using namespace std;
bool judge(int x)
{
if((x % 4 == 0 && x % 100 != 0) ||(x % 400 == 0))
return true;
return false;
}
int main()
{
int test,y,n,ans;
while(scanf("%d",&test) != EOF)
{
while(test--)
{
ans = 0;
scanf("%d%d",&y,&n);
for(int i = y ; ; i++)
{
if(judge(i))
{
ans++;
}
if(ans == n)
{
printf("%d\n",i);
break;
}
}
}
}
}