#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(), (x).end()
using pii = array<int, 2>;
using vi = vector<int>;
array<pii, 45> mt;
struct Dinic {
struct Edge {
int to, rev;
int c, oc;
int flow() { return max(oc - c, 0); }
};
vi lvl, ptr, q;
vector<vector<Edge>> adj;
Dinic(int n) : lvl(n), ptr(n), q(n), adj(n) {}
void addEdge(int a, int b, int c, int rcap = 0) {
adj[a].emplace_back(b, adj[b].size(), c, c);
adj[b].emplace_back(a, adj[a].size() - 1, rcap, rcap);
}
int dfs(int v, int t, int f) {
if (v == t || !f)return f;
for (int &i = ptr[v]; i < size(adj[v]); ++i) {
Edge &e = adj[v][i];
if (lvl[e.to] == lvl[v] + 1)
if (int p = dfs(e.to, t, min(f, e.c))) {
e.c -= p, adj[e.to][e.rev].c += p;
return p;
}
}
return 0;
}
int calc(int s, int t) {
int flow = 0;
q[0] = s;
int L = 30;
do {
lvl = ptr = vi(size(q));
int qi = 0, qe = lvl[s] = 1;
while (qi < qe && !lvl[t]) {
int v = q[qi++];
for (auto e: adj[v]) {
if (!lvl[e.to] && e.c >> (30 - L))
q[qe++] = e.to, lvl[e.to] = lvl[v] + 1;
}
}
while (int p = dfs(s, t, (int) 2e9 + 13))flow += p;
} while (lvl[t]);
return flow;
}
bool leftOfMinCut(int a) { return lvl[a] != 0; }
};
void solve() {
int n;
string s;
cin >> n >> s;
vi wold(10);
for (int i = 0; i < n; ++i) {
int b = s[i] - '0';
++wold[mt[i][b ^ 1]];
}
for (int i = n; i < 45; ++i) {
auto [x, y] = mt[i];
if (x == 0) {
++wold[x];
}
}
for (int a1 = 1; a1 < 10; ++a1) {
for (int a2 = a1; a2 < 10; ++a2) {
for (int a3 = a2; a3 < 10; ++a3) {
vi w(wold);
vi bst(10);
bst[a1] = 1;
bst[a2] = 1;
bst[a3] = 1;
Dinic sol(100);
int val = w[0];
int nxt = 0;
bool ok = true;
for (int i = n; i < 45; ++i) {
auto [x, y] = mt[i];
if (x == 0 || bst[x] || bst[y]) {
continue;
}
int who = 1 + nxt++;
sol.addEdge(0, who, 1);
sol.addEdge(who, 50 + x, 1);
sol.addEdge(who, 50 + y, 1);
}
for (int i = 1; i < 10; ++i) {
if (bst[i])continue;
if(w[i] <= val)sol.addEdge(50 + i, 90, val-w[i]);
else ok=false;
}
if (sol.calc(0, 90) == nxt && ok) {
cout << "YES\n";
return;
}
}
}
}
cout << "NO\n";
}
int main() {
vi p(10);
iota(all(p), 0);
for (int i = 0; i < 9; ++i) {
for (int j = 0; j < 5; ++j) {
mt[i * 5 + j] = {p[j], p[9 - j]};
}
vi pn(p);
pn[1] = p.back();
for (int j = 2; j < 10; ++j) {
pn[j] = p[j - 1];
}
p = pn;
}
// for (int i = 0; i < 45; ++i) {
// if (i % 5 == 0) {
// cout << endl;
// }
// cout << mt[i][0] + 1 << ' ' << mt[i][1] + 1 << endl;
// }
int t;
cin >> t;
while (t--) {
solve();
}
}