/************
Author :- Profesor(Abhishek)
2020-08-24-15.32.01
**********/
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl "\n"
#define fori(i,a,b) for(ll i=a;i<b;i++)
#define forr(i,a,b) for(ll i=a;i>=b;i--)
#define forit(it,x) for (auto it=(x).begin();it!=(x).end(); it++)
#define all(x) (x).begin(),(x).end()
#define allr(x) (x).rbegin(),(x).rend()
#define eb emplace_back
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define sll set<ll>
#define vll vector<ll>
#define msl map<string,ll>
#define mll map<ll,ll>
const ll MAXN = (ll)1e7;
ll i, j, k;
vll LCM(MAXN+1);
vector<ll> phi(MAXN+1);
vll permuted;
void phi_1_to_n() {
phi[0] = 0;
phi[1] = 1;
for ( i = 2; i <= MAXN; i++)
phi[i] = i;
for (i = 2; i <= MAXN; i++) {
if (phi[i] == i) {
for (j = i; j <= MAXN; j+=i)
phi[j] -= phi[j] / i;
}
}
for(i=21 ; i< MAXN+1 ; i++)
{
string s1, s2;
int check = 1;
s1 = to_string(i);
s2 = to_string(phi[i]);
sort(s1.begin(), s1.end());
sort(s2.begin(), s2.end());
for(j=0;j<s1.length();j++)
{
if(s1[j]!=s2[j])
{
check = 0;
break;
}
}
if(check == 1)
permuted.push_back((ll)i);
}
}
int main()
{
phi_1_to_n();
ll t;
cin >> t;
while(t--)
{
ll n, ans, c=0 ;
double x=INT_MAX;
cin >> n;
for(i=0; i<permuted.size(); i++)
{
if(permuted[i]<=n)
{
double division = ((double)permuted[i]/(double)phi[permuted[i]]);
if(x >= division)
{
ans = permuted[i];
x=division;
c=1;
}
}
}
if(c==1)
cout << ans << endl;
else
cout << "No solution" << endl;
}
}
LyoqKioqKioqKioqKgoKICAgQXV0aG9yIDotIFByb2Zlc29yKEFiaGlzaGVrKQogICAyMDIwLTA4LTI0LTE1LjMyLjAxCioqKioqKioqKiovCiNpbmNsdWRlIDxiaXRzL3N0ZGM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IGkpIHsKICAgICAgICAgICAgZm9yIChqID0gaTsgaiA8PSBNQVhOOyBqKz1pKQogICAgICAgICAgICAgICAgcGhpW2pdIC09IHBoaVtqXSAvIGk7CiAgICAgICAgfQogICAgfQoKICAgIGZvcihpPTIxIDsgaTwgTUFYTisxIDsgaSsrKQogICAgewogICAgICAgIHN0cmluZyBzMSwgczI7CiAgICAgICAgaW50IGNoZWNrID0gMTsKICAgICAgICBzMSA9IHRvX3N0cmluZyhpKTsKICAgICAgICBzMiA9IHRvX3N0cmluZyhwaGlbaV0pOwogICAgICAgIHNvcnQoczEuYmVnaW4oKSwgczEuZW5kKCkpOwogICAgICAgIHNvcnQoczIuYmVnaW4oKSwgczIuZW5kKCkpOwogICAgICAgIGZvcihqPTA7ajxzMS5sZW5ndGgoKTtqKyspCiAgICAgICAgewogICAgICAgICAgICBpZihzMVtqXSE9czJbal0pCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIGNoZWNrID0gMDsKICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIGlmKGNoZWNrID09IDEpCiAgICAgICAgICAgIHBlcm11dGVkLnB1c2hfYmFjaygobGwpaSk7CiAgICB9Cgp9CgoKCmludCBtYWluKCkKewogICAgcGhpXzFfdG9fbigpOwogICAgbGwgdDsKICAgIGNpbiA+PiB0OwogICAgd2hpbGUodC0tKQogICAgewogICAgICAgIGxsIG4sIGFucywgYz0wIDsKICAgICAgICBkb3VibGUgeD1JTlRfTUFYOwogICAgICAgIGNpbiA+PiBuOwogICAgICAgIGZvcihpPTA7IGk8cGVybXV0ZWQuc2l6ZSgpOyBpKyspCiAgICAgICAgewogICAgICAgICAgICBpZihwZXJtdXRlZFtpXTw9bikKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgZG91YmxlIGRpdmlzaW9uID0gKChkb3VibGUpcGVybXV0ZWRbaV0vKGRvdWJsZSlwaGlbcGVybXV0ZWRbaV1dKTsKICAgICAgICAgICAgICAgIGlmKHggPj0gZGl2aXNpb24pCiAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgYW5zID0gcGVybXV0ZWRbaV07CiAgICAgICAgICAgICAgICAgICAgeD1kaXZpc2lvbjsKICAgICAgICAgICAgICAgICAgICBjPTE7CiAgICAgICAgICAgICAgICB9CgogICAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIGlmKGM9PTEpCiAgICAgICAgY291dCA8PCBhbnMgPDwgZW5kbDsKICAgICAgICBlbHNlCiAgICAgICAgICAgIGNvdXQgPDwgIk5vIHNvbHV0aW9uIiA8PCBlbmRsOwoKICAgIH0KCn0KCg==