#include <iostream>
#include <vector>
#include <cstdio>
#include <string>
#include <algorithm>
bool isprime(int x)
{
for (int i = 2; i < x; ++i) {
if ((x % i) == 0) return false;
}
return true;
}
bool ispalindrome(int x)
{
std::string s = std::to_string(x);
for (int i = 0; i < int(s.size()) / 2; ++i) {
if (s[i] != s[s.size() - i - 1]) return false;
}
return true;
}
int main()
{
std::vector<int> S;
for (int i = 2; i < 1000; ++i) {
if (isprime(i) && ispalindrome(i))
S.push_back(i);
}
int t;
std::cin >> t;
for (int ti = 0; ti < t; ++ti) {
int n;
std::cin >> n;
std::vector<int> vals;
for (int ni = 0; ni < n; ++ni) {
int x;
std::cin >> x;
vals.push_back(x);
}
for (int x : vals) {
if (std::binary_search(S.begin(), S.end(), x)) {
std::cout << x << std::endl;
break;
}
}
}
}