#include <cstdio>
#include <iostream>
using namespace std;
typedef long long LL;
int k,k1,k2;
LL next(int n,LL k)
{
int ans = 0 , num = 0;
int a[20] = {0};
k = k * k;
while(k > 0)
{
a[num ++ ] = k%10;
k /= 10;
}
n = min(n,num);
for(int i=1;i<=n;i++)
{
ans = ans * 10 + a[--num];
}
return ans;
}
int main()
{
int n;
int T=0;
scanf("%d",&T);
while(T--)
{
scanf("%d%lld",&n,&k);
int ans = k;
k1 = k2 = k;
do
{
k1 = next(n,k1);
k2 = next(n,k2);
ans = max(ans,k2);
k2 = next(n,k2);
ans = max(ans,k2);
}while(k1 != k2);
printf("%d\n",ans);
}
return 0;
}