#include <iostream>
#include<stdio.h>
#include<cmath>
using namespace std;
FILE *f,*g;
int main()
{
int i,j,n,t,r,u,s,t1,k;
float h;
bool ok,ok1;
f=fopen("progresie.in","r");
g=fopen("progresie.out","w");
fscanf(f,"%lld",&t);
for(i=1;i<=t;i++)
{
fscanf(f,"%d %d",&n,&r);
for(k=1;k<=1000000;k++)
{
for(j=k*(k-1)+1;j<=k*k;j++)
{
t1=j;
ok=true;
for(u=1;u<n;u++)
{
ok1=true;
t1=t1+r;
h=sqrt(t1);
if(h==trunc(h)) ok1=false;
if(ok1==true) {if((t1>=(trunc(h)+1)*trunc(h)+1 && j<=(trunc(h)+1)*(trunc(h)+1))) s++; else {ok=false; break;}}
}
if(ok==true) break;
}
if(ok==true) {fprintf(g,"%d\n",j); break;}
}
}
fclose(f);
fclose(g);
return 0;
}