#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<iomanip>
#include<math.h>
#include<complex>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<functional>
#include<assert.h>
#include<numeric>
using namespace std;
#define REP(i,m,n) for(int i=(int)(m) ; i < (int) (n) ; ++i )
#define rep(i,n) REP(i,0,n)
using ll = long long;
const int inf=1e9+7;
const ll longinf=1LL<<60 ;


    
int main(){
  int n; cin >> n; ll a[n];
  rep(i, n){
    cin >> a[i];
  }
  if(a[0]!=0){
    cout << -1 << endl; return 0;
  }
  rep(i, n-1){
    if(a[i+1]>a[i]+1){
      cout << -1 << endl; return 0;
    }
  }
  ll ans = 0;
  for(int i=1; i<n; i++){
    if(a[i]==a[i-1]+1) ans++;
    else ans+=a[i];
  }
  cout << ans << endl; 
}
#include "bits/stdc++.h"
#define rep(i,n) for(int i = 0; i < (n); ++i)
using namespace std;
typedef long long int ll;
typedef pair<int, vector<ll>> P;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int t;
    cin >> t;
    rep(ti,t){
        int n;
        cin >> n;
        vector<ll> a(n);
        rep(i,n) cin >> a[i];
        string s;
        cin >> s;
        if(s.back() == 1){
            cout << 1 << endl;
            continue;
        }
        vector<P> v;
        v.push_back(P(0, {}));
        for(int i = n-1; i >= 0; --i){
            if(v.back().first == (s[i] - '0')){
                v.back().second.push_back(a[i]);
            }
            else{
                int nex = 1 - v.back().first;
                v.push_back(P(nex, {a[i]}));
            }
        }
        for(auto &p : v){
            sort(p.second.begin(), p.second.end());
        }
        int sz = v.size();
        vector<ll> buf;
        bool f = true;
        rep(i,sz){
            if(v[i].first == 0){
                for(ll x : v[i].second) buf.push_back(x);
            }
            else{
                auto memo = buf;
                int sz2 = memo.size();
                int rank = 0;
                for(int j = 59; j >= 0; --j){
                    int pivot = -1;
                    for(int k = rank; k < sz2; ++k){
                        if(memo[k] >> j & 1){
                            pivot = k;
                            break;
                        }
                    }
                    if(pivot == -1) continue;
                    swap(memo[pivot], memo[rank]);
                    for(int k = 0; k < sz2; ++k){
                        if(k != rank && (memo[k] >> j & 1)) memo[k] ^= memo[rank];
                    }
                    for(ll &x : v[i].second){
                        if(x >> j & 1) x ^= memo[rank];
                    }
                    ++rank;
                }
                for(ll x : v[i].second){
                    if(x != 0){
                        f = false;
                        break;
                    }
                }
                if(!f) break;
            }
        }
        cout << !f << endl;
    }
    return 0;
}
