#include <iostream>
#include <string>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <queue>
#include <stack>
#include <functional>
#include <bitset>
#include <assert.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef pair<ll,ll> P;
typedef vector<P> vpl;
typedef tuple<ll,ll,ll> tapu;
#define rep(i,n) for(ll i=0; i<(n); i++)
#define REP(i,a,b) for(int i=(a); i<(b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = 1<<30;
const ll linf = 1LL<<62;
const int MAX = 510000;
ll dy[8] = {1,-1,0,0,1,-1,1,-1};
ll dx[8] = {0,0,1,-1,1,-1,-1,1};
const double pi = acos(-1);
const double eps = 1e-7;
template<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){
	if(a>b){
		a = b; return true;
	}
	else return false;
}
template<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){
	if(a<b){
		a = b; return true;
	}
	else return false;
}
template<typename T> inline void print(T &a){
    rep(i,a.size()) cout << a[i] << " ";
    cout << "\n";
}
template<typename T1,typename T2> inline void print2(T1 a, T2 b){cout << a << " " << b << "\n";}
template<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){
	cout << a << " " << b << " " << c << "\n";
}
const int mod = 1e9 + 7;
//const int mod = 998244353;

void solve(){
    ll n; cin >> n;
    vl a(n); rep(i,n) cin >> a[i];
    vl basis;
    string s; cin >> s;
    ll ans = 0;
    for(ll i=n-1; i>=0; i--){
        for(auto b : basis){
            chmin(a[i], a[i]^b);
        }
        if(s[i]=='0'){
            if(a[i]) basis.push_back(a[i]);
        }else{
            if(a[i]) ans = 1;
        }
    }
    cout << ans << "\n";
}

int main(){
    ll t; cin >> t;
    while(t--){
        solve();
    }
}
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> ii;
typedef tuple<ll, ll, ll> iii;
typedef vector<ll> vi;
typedef vector<ii> vii;
typedef vector<iii> viii;
typedef vector<vi> vvi;
typedef vector<vii> vvii;
#define REP(i,n) for (ll i = 0; i < n; ++i)
#define REPR(i,n) for (ll i = n-1; i >= 0; --i)
#define FOR(i,m,n) for (ll i = m; i < n; ++i)
#define FORR(i,m,n) for (ll i = n-1; i >= m; --i)
#define FORE(x,xs) for (const auto& x : xs)
#define FORI(i,v) for (auto i = v.begin(); i != v.end(); i++)
#define ALL(v) v.begin(), v.end()
#define CHMIN(x,y) x = min(x, y)
#define CHMAX(x,y) x = max(x, y)
#define YES(b) cout << ((b) ? "YES" : "NO") << endl
#define Yes(b) cout << ((b) ? "Yes" : "No") << endl

const int MAX = 2e5+10;
const int INF = 1e9+10;

int N;
vii U, D, R, L;

vii holRev(vii v) {
    vii ret;
    FORE (xy, v) {
        int x, y; tie(x, y) = xy;
        ret.push_back(ii(MAX-x, y));
    }
    return ret;
}

vii verRev(vii v) {
    vii ret;
    FORE (xy, v) {
        int x, y; tie(x, y) = xy;
        ret.push_back(ii(x, MAX-y));
    }
    return ret;
}

int sub1(vii r, vii l, bool hol) {
    int ret = INF;
    vector<set<int>> u(MAX);
    FORE (xy, l) {
        int x, y; tie(x, y) = xy; if (!hol) swap(x, y);
        u[y].insert(x);
    }
    FORE (xy, r) {
        int x, y; tie(x, y) = xy; if (!hol) swap(x, y);
        auto it = u[y].upper_bound(x);
        if (it != u[y].end()) {
            CHMIN(ret, (*it - x) * 5);
        }
    }

    return ret;
}

int sub2(vii uu, vii r) {
    int ret = INF;
    vector<set<int>> u(2*MAX+10);
    FORE (xy, uu) {
        int x, y; tie(x, y) = xy;
        u[x+y].insert(x);
    }
    FORE (xy, r) {
        int x, y; tie(x, y) = xy;
        auto it = u[x+y].upper_bound(x);
        if (it != u[x+y].end()) {
            CHMIN(ret, (*it - x) * 10);
        }
    }

    return ret;
}

int solve() {
    int ret = INF;

    // 正面衝突
    CHMIN(ret, sub1(R, L, true));
    CHMIN(ret, sub1(U, D, false));

    // 直行衝突
    CHMIN(ret, sub2(U, R));
    CHMIN(ret, sub2(holRev(U), holRev(L)));
    CHMIN(ret, sub2(verRev(D), verRev(R)));
    CHMIN(ret, sub2(holRev(verRev(D)), holRev(verRev(L))));

    return ret;
}

int main() {
    cout << fixed << setprecision(15);
    cin >> N;
    REP (i, N) {
        int x, y, c; char u; cin >> x >> y >> u;
        if (u == 'U') U.push_back(ii(x, y));
        else if (u == 'R') R.push_back(ii(x, y));
        else if (u == 'D') D.push_back(ii(x, y));
        else L.push_back(ii(x, y));

    }
    int ans = solve();
    if (ans == INF) cout << "SAFE" << endl;
    else cout << ans << endl;
}