#include<cstdio>
#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<vector>
#include<utility>
#include<unordered_set>
using namespace std;
unsigned long long int T,n,k,ans,mod;
int main()
{
scanf("%llu",&T);
while(T--){
ans=0,mod=1;
unordered_set<int>s;
scanf("%llu %llu",&n,&k);
//printf("n is %llu.k is %llu",n,k);
for(int i=0;i<n;++i)mod*=10;
while(s.find(k)==s.end()){
//printf("k is %llu\n",k);
s.insert(k);
ans=max(ans,k);
k=k*k;
while(k>=mod)k/=10;
}
printf("%llu\n",ans);
}
return 0;
}
//使用Floyd判圈法
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<vector>
#include<utility>
#include<unordered_set>
using namespace std;
unsigned long long int T,n,k,k1,k2,mod,ans;
unsigned long long int next(unsigned long long int mod,unsigned long long int k){
if(k==0)return k;
k=k*k;
while(k>=mod)k/=10;
return k;
}
int main()
{
scanf("%llu",&T);
while(T--){
scanf("%llu %llu",&n,&k);
mod=1,ans=k,k1=k,k2=k;
for(int i=0;i<n;++i)mod*=10;
do{
k1=next(mod,k1);
ans=max(ans,k1);
k2=next(mod,k2);
ans=max(ans,k2);
k2=next(mod,k2);
ans=max(ans,k2);
}while(k1!=k2);
printf("%llu\n",ans);
}
return 0;
}