/*
File Name :UVA11549.cpp
Author : erlingyier
Created Time: 2014年09月05日 星期五 23时26分13秒
*/
#include <iostream>
#include <sstream>
#include <set>
using namespace std;
int next(int n, int k)
{
stringstream ss;
ss << (long long)k*k;
string s = ss.str();
if(s.length() > n)
s = s.substr(0, n);
int ans;
stringstream ss2;
ss2 >> ans;
return ans;
}
int main( int argc, const char* argv[] )
{
int T;
cin>>T;
while(T--)
{
int n, k;
cin>>n>>k;
int ans = k;
set<int> s;
while(!s.count(k))
{
s.insert(k);
if(k > ans)
ans = k;
k = next(n, k);
}
cout<<ans<<endl;
}
return 0;
}
int buf[MAX_N];
int next(int n, int k)
{
long long temp = (long long)k*k;
int j = 0;
while(temp > 0)
{
buf[j++] = temp%10;
temp /= 10;
}
if(n > j)
n = j;
int ans = 0;
for(int i = 0; i < n; i++)
ans = ans*10 + buf[--j];
return ans;
}