#include <bits/stdc++.h>
using namespace std;
#define int long long
// g++ <filename>.cpp -g -Wall -Wextra -DDEBUG -o <executable>
// copied from: https://codeforces.com/blog/entry/79024
// === Debug macro starts here ===
int recur_depth = 0;
#ifdef DEBUG
#define dbg(x) {++recur_depth; auto x_=x; --recur_depth; cerr<<string(recur_depth, '\t')<<"\e[91m"<<__func__<<":"<<__LINE__<<"\t"<<#x<<" = "<<x_<<"\e[39m"<<endl;}
#else
#define dbg(x)
#endif
template<typename Ostream, typename Cont>
typename enable_if<is_same<Ostream,ostream>::value, Ostream&>::type operator<<(Ostream& os, const Cont& v){
os<<"[";
for(auto& x:v){os<<x<<", ";}
return os<<"]";
}
template<typename Ostream, typename ...Ts>
Ostream& operator<<(Ostream& os, const pair<Ts...>& p){
return os<<"{"<<p.first<<", "<<p.second<<"}";
}
// === Debug macro ends here ===
bool is_prime(int n) {
if (n < 2) return false;
for (int i = 2; i < n; i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
bool is_palindrome (int n) {
string s = to_string(n);
string s_reversed = s;
reverse(s_reversed.begin(), s_reversed.end());
return !s.compare(s_reversed);;
}
signed main() {
int t;
cin >> t;
for (int i = 0; i < t; i++) {
int n;
cin >> n;
for (int j = 0; j < n; j++) {
int number;
cin >> number;
// cout << number << ": Prime: " << is_prime(number) << "\n";
if (is_palindrome(number) && is_prime(number)) {
cout << number << "\n";
}
}
}
return 0;
}