HDU1076
/*Sample Input
3
2005 25
1855 12
2004 10000
Sample Output
2108
1904
43236*/
#include <iostream>
using namespace std;
bool ifrun(int y)
{
if((y%4==0&&y%100!=0)||y%400==0)
return true;
return false;
}
int main()
{
int t;
cin>>t;
while(t--)
{
int y,n,c=0;
cin>>y>>n;
if(ifrun(y))
{
c++;
while(1)
{
y+=4;
if(ifrun(y))
c++;
if(c==n)
break;
}
}
else
{
while(!ifrun(y))
{
y++;
}
c++;
while(1)
{
y+=4;
if(ifrun(y))
c++;
if(c==n)
break;
}
}
cout<<y<<endl;
}
return 0;
}