#include <cstdio>
#include <cstring>
#include <algorithm>
#define N 100010

using namespace std;

int n;
typedef long long ll;
struct node{
	ll a, c;
}f[2*N];
ll sl[3];
ll dis[2*N][3];
ll ma[3];
ll jl;
ll mi[3], ans;

bool cmp(node x, node y){
	return x.a<y.a;
}

int main(){
//	freopen("funny.in", "r", stdin);
	scanf("%d", &n);
	for (int i=1; i<=2*n; i++){
		char ch;
		scanf("%lld", &f[i].a);
		while ((ch=getchar())!='R' && ch!='B' && ch!='G');
		if (ch=='R') sl[0]++, f[i].c=0;
		else if (ch=='B') sl[1]++, f[i].c=1;
		else if (ch=='G') sl[2]++, f[i].c=2;
	}
	if (sl[0]%2==0 && sl[1]%2==0 && sl[2]%2==0){
		printf("0");
		return 0;
	}
	if (sl[0]%2==0) jl=0;
	else if (sl[1]%2==0) jl=1;
	else jl=2;
	sort(f+1, f+2*n+1, cmp);
	ma[0]=(ll)-2e15;
	ma[1]=(ll)-2e15;
	ma[2]=(ll)-2e15;
	for (int i=1; i<=2*n; i++){
		if (f[i].c==jl) f[i].c=0;
		else if (f[i].c==0) f[i].c=jl;
		ma[f[i].c]=f[i].a;
		for (int j=0; j<=2; j++) dis[i][j]=f[i].a-ma[j];
	}
	ma[0]=(ll)2e15;
	ma[1]=(ll)2e15;
	ma[2]=(ll)2e15;
	ans=(ll)2e15;
	for (int i=2*n; i; i--){
		ma[f[i].c]=f[i].a;
		for (int j=0; j<=2; j++) dis[i][j]=min(dis[i][j], ma[j]-f[i].a);
		if (f[i].c) ans=min(ans, dis[i][3^f[i].c]);
	}
	mi[1]=(ll)2e15;
	mi[2]=(ll)2e15;
	for (int i=1; i<=2*n; i++){
		if (f[i].c==0){
			ans=min(dis[i][1]+mi[2], ans);
			ans=min(dis[i][2]+mi[1], ans);
			mi[1]=min(dis[i][1], mi[1]);
			mi[2]=min(dis[i][2], mi[2]);
		}
	}
	printf("%lld", ans);
	
	return 0;
}
#include <bits/stdc++.h>

#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define FOR(i,n,m) for(int i=(int)(n); i<=(int)(m); i++)
#define RFOR(i,n,m) for(int i=(int)(n); i>=(int)(m); i--)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define RITR(x,c) for(__typeof(c.rbegin()) x=c.rbegin();x!=c.rend();x++)
#define setp(n) fixed << setprecision(n)

template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }

#define ll long long
#define vll vector<ll>
#define vi vector<int>
#define pll pair<ll,ll>
#define pi pair<int,int>

#define all(a) (a.begin()),(a.end())
#define rall(a) (a.rbegin()),(a.rend())
#define fi first
#define se second
#define pb push_back
#define ins insert

#define debug(a) cerr<<(a)<<endl
#define dbrep(a,n) rep(_i,n) cerr<<(a[_i])<<" "; cerr<<endl
#define dbrep2(a,n,m) rep(_i,n){rep(_j,m) cerr<<(a[_i][_j])<<" "; cerr<<endl;}

using namespace std;

template<class A, class B>
ostream &operator<<(ostream &os, const pair<A,B> &p){return os<<"("<<p.fi<<","<<p.se<<")";}
template<class A, class B>
istream &operator>>(istream &is, pair<A,B> &p){return is>>p.fi>>p.se;}

bool alla(const string &S)
{
    for(auto e:S){
        if (e!='a') return false; 
    }
    return true;
}

bool cmp(const string &S, const string &T)
{
    int N = min(S.size(),T.size());
    rep(i,N){
        if (S[i]>T[i]) return true;
        else if(S[i]<T[i]) return false;
    }
    return S.size()>T.size();
}

//-------------------------------------------------

const string str = "atcoder";

int main(void)
{
	cin.tie(0);
	ios::sync_with_stdio(false);
    int t; cin>>t;
    while(t--){
        string S; cin>>S;
        int N = S.size();
        if (alla(S)){
            cout<<"-1\n";
            continue;
        }
        if (cmp(S,str)){
            cout<<"0\n";
            continue;
        }
        int ans=(int)1e9;
        rep(i,2){
            int j=i;
            while(j<N && S[j]<=str[i]) j++;
            if (j<N) chmin(ans,j-i);
        }
        cout<<ans<<endl;
    }
	return 0;
}
