#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
bool palindrome(int x) {
stringstream s;
s << x;
string s1;
s >> s1;
string s2(s1.rbegin(), s1.rend());
return s1 == s2;
}
bool prime(int x) {
if (x == 1) return false;
if (x == 2) return true;
if ((x % 2) == 0) return false;
for (int i = 3; i*i <= x; i += 2) {
if ((x % i) == 0) return false;
}
return true;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
for (int i = 0; i < t; ++i) {
int n;
cin >> n;
int v = -1;
for (int j = 0; j < n; ++j) {
int x;
cin >> x;
if (v == -1) {
if (prime(x) && palindrome(x))
v = x;
}
}
cout << v << endl;
}
}