#include <bits/stdc++.h>
using namespace std;
string ints(int n){
stringstream ss;
ss << n;
return ss.str();
}
int main(){
cin.sync_with_stdio(0);
cin.tie(0);
int tests; cin >> tests;
int prim[101010] = {0};
prim[2] = 1;
for(int i = 3; i <= 100000; i+=2){
bool g = true;
for(int j = 2; j*j <= i ; j++){
if(i%j == 0){
g = false;
break;
}
}
prim[i] = g;
}
int pal[101010] = {0};
for(int i = 1; i <= 100000; i++){
string is = ints(i);
bool g = true;
for(int j = 0; j < is.length()/2; j++){
if(is[j] != is[is.length()-1-j])
g = false;
}
pal[i] = g;
}
int a[101010];
while(tests--){
int n; cin >> n;
for(int i = 0; i < n; i++){
cin >> a[i];
}
for(int i = 0; i < n; i++){
if(prim[a[i]] && pal[a[i]]){
cout << a[i] << endl;
break;
}
}
}
return 0;
}