#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> 
#include <ext/pb_ds/tree_policy.hpp> 
#include <functional> // for less 
using namespace __gnu_pbds; 
using namespace std;
#define int long long
#define endl "\n"
#define deb(x) cout<<#x<<" "<<x<<endl;
#define sc(ar,n) for(int pen=0;pen<n;pen++){ cin>>ar[pen];}
#define pr(ar,n) for(int pen=0;pen<n;pen++){ cout<<ar[pen]<<" ";} cout<<endl;
#define fr(i,x,n) for(int i=x;i<n;i++)
#define frt(it,vb) for(auto it=vb.begin();it!=vb.end();it++)
#define mem(ar,x) memset(ar,x,sizeof(ar));
#define pb push_back
#define fastio ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define mod 1000000007
#define rt return 0;
#define ct continue;
#define MAX 1000000000000000000
#define MAX1 1000000000
#define CLK clock_t clk = clock();//Start of main
#define OCLK cerr << "Time (in ms): " << (double)(clock() - clk) * 1000.0 / CLOCKS_PER_SEC << '\n';//End of main
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> 

inline int solve()
{
    int n;
    cin>>n;
    int ar[n];
    sc(ar,n);
    int mn = MAX;
    fr(i,0,n) mn = min(mn , ar[i]);
    map <int,vector<int> > mp;
    fr(i,0,n){
        int x = ar[i];
        for(int j=1;j<=sqrt(x);j++){
            if(x%j == 0){
                mp[j].push_back(i);
                if(j != x/j)
                mp[x/j].push_back(i);
            }
        }
    }
    int ans = 0;
    frt(it,mp){
        if(it->first > mn) break;
        int g = 0;
        frt(ut,it->second){
            g = __gcd(g , ar[*ut]);
        }
        if(g == it->first){
            ans++;
        }
    }
    cout << ans << endl;
  rt;
}
signed main()
{
    // fastio;
    int t;
    int test_cases=1;
    if(test_cases==0)
    cin>>t;
    else
    t=1;
    while(t--){
        solve();
    }
}
#include <bits/stdc++.h>
using namespace std;

#define stp(var, init, end) for (auto var = init; var < end; ++var)
#define stpe(var, init, end) for (auto var = init; var <= end; ++var)
#define ll long long

int main(void)
{
    int n, rnum = 0, gnum = 0, bnum = 0;
    cin >> n;

    vector<ll> rdogs;
    vector<ll> gdogs;
    vector<ll> bdogs;
    ll ina;
    char inc;
    stp(i, 0, 2 * n)
    {
        cin >> ina >> inc;
        if (inc == 'R')
        {
            rdogs.emplace_back(ina);
            ++rnum;
        }
        else if (inc == 'G')
        {
            gdogs.emplace_back(ina);
            ++gnum;
        }
        else if (inc == 'B')
        {
            bdogs.emplace_back(ina);
            ++bnum;
        }
    }
    sort(rdogs.begin(), rdogs.end());
    sort(gdogs.begin(), gdogs.end());
    sort(bdogs.begin(), bdogs.end());
    /*
    cout << "R: ";
    stp(i, 0, rnum)
    {
        cout << rdogs[i] << " ";
    }
    cout << "\n";
    cout << "G: ";
    stp(i, 0, gnum)
    {
        cout << gdogs[i] << " ";
    }
    cout << "\n";
    cout << "B: ";
    stp(i, 0, bnum)
    {
        cout << bdogs[i] << " ";
    }
    cout << "\n\n";
    */

    if (rnum % 2 == 0 && gnum % 2 == 0 && bnum % 2 == 0)
    {
        //cout << "even\n";
        cout << 0;
    }
    else
    {
        ll rgmin = 10000000000000000ll, gbmin = 10000000000000000ll, brmin = 10000000000000000ll;
        if (gnum > 0)
        {
            stp(i, 0, rnum)
            {
                auto iter = lower_bound(gdogs.begin(), gdogs.end(), rdogs[i]);
                if (iter == gdogs.begin())
                    rgmin = min(rgmin, abs(rdogs[i] - *iter));
                else if (iter == gdogs.end())
                    rgmin = min(rgmin, abs(rdogs[i] - *(iter - 1)));
                else
                    rgmin = min(rgmin, min(abs(rdogs[i] - *iter), abs(rdogs[i] - *(iter - 1))));
            }
        }
        if (bnum > 0)
        {
            stp(i, 0, gnum)
            {
                auto iter = lower_bound(bdogs.begin(), bdogs.end(), gdogs[i]);
                if (iter == bdogs.begin())
                    gbmin = min(gbmin, abs(gdogs[i] - *iter));
                else if (iter == bdogs.end())
                    gbmin = min(gbmin, abs(gdogs[i] - *(iter - 1)));
                else
                    gbmin = min(gbmin, min(abs(gdogs[i] - *iter), abs(gdogs[i] - *(iter - 1))));
            }
        }
        if (rnum > 0)
        {
            stp(i, 0, bnum)
            {
                auto iter = lower_bound(rdogs.begin(), rdogs.end(), bdogs[i]);
                if (iter == rdogs.begin())
                    brmin = min(brmin, abs(bdogs[i] - *iter));
                else if (iter == rdogs.end())
                    brmin = min(brmin, abs(bdogs[i] - *(iter - 1)));
                else
                    brmin = min(brmin, min(abs(bdogs[i] - *iter), abs(bdogs[i] - *(iter - 1))));
            }
        }

        if (rnum % 2 == 1 && gnum % 2 == 1)
        {
            if (bnum > 0)
                cout << min(rgmin, gbmin + brmin);
            else
                cout << rgmin;
        }
        else if (gnum % 2 == 1 && bnum % 2 == 1)
        {
            if (rnum > 0)
                cout << min(gbmin, brmin + rgmin);
            else
                cout << gbmin;
        }
        else if (bnum % 2 == 1 && rnum % 2 == 1)
        {
            if (gnum > 0)
                cout << min(brmin, rgmin + gbmin);
            else
                cout << brmin;
        }
    }
    return 0;
}