#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstring>
#include <iostream>
#include <sstream>
#include <numeric>
#include <map>
#include <set>
#include <queue>
#include <vector>

using namespace std;

int64_t solve(int64_t N, std::vector<int64_t> a, std::vector<std::string> c) {
	int t[128] = {};
	t['R'] = 0;
	t['G'] = 1;
	t['B'] = 2;
	vector<vector<int64_t>> v(3);
	for (int i = 0; i < 2 * N; i++) {
		v[t[c[i][0]]].emplace_back(a[i]);
	}
	if (v[0].size() % 2 == 0) {
		swap(v[0], v[1]);
	}
	if (v[1].size() % 2 == 0) {
		swap(v[1], v[2]);
	}
	if (v[0].size() % 2 == 0) {
		return 0;
	}
	for (int i = 0; i < 3; ++i) {
		sort(v[i].begin(), v[i].end());
	}
	auto get_min = [&](const vector<int64_t>& a, const vector<int64_t>& b) {
		int64_t ans = 1LL << 60;
		int i = 0;
		for (auto x : a) {
			while (i < b.size() - 1 && abs(x - b[i]) >= abs(x - b[i + 1])) {
				++i;
			}
			ans = min(ans, abs(x - b[i]));
		}
		return ans;
	};
	int64_t ans = get_min(v[0], v[1]);
	if (!v[2].empty()) {
		ans = min(ans, get_min(v[0], v[2]) + get_min(v[1], v[2]));
	}
	return ans;
}

int main() {
	int64_t N;
	std::cin >> N;
	std::vector<int64_t> a(2*N);
	std::vector<std::string> c(2*N);
	for (int i = 0; i < 2*N; i++) {
		std::cin >> a[i] >> c[i];
	}
	cout << solve(N, std::move(a), std::move(c)) << endl;
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define sz(x) int(x.size())
const int mod = 1e9 + 7;
const int inf = 2e9 + 5;
const ll linf = 9e18 + 5;
const ll ll0 = 0 * 1ll;


ll n, s, k;

ll gcd (ll a, ll b) {
    if (b == 0) {
        return a;
    }

    return gcd(b, a % b);
}

ll euler (ll n) {
    ll d = 2;

    ll ans = n;

    while (d * d <= n) {
        if (n % d == 0) {
            ans /= d;
            ans *= d - 1;
        }
        while (n % d == 0) {
            n /= d;
        }

        d++;
    }

    if (n > 1) {
        ans /= n;
        ans *= n - 1;
    }

    return ans;
}

ll mull(ll a, ll b) {
    return (a * b) % n;
}

ll binpow (ll a, ll b) {
    if (b == 0) {
        return 1;
    }
    if (b % 2) {
        return mull(a, binpow(a, b - 1));
    }

    ll half = binpow(a, b / 2);
    return mull(half, half);
}

void init () {
}

void input () {
    cin >> n >> s >> k;

    s = n - s;
}

void solve() {
    ll g = gcd(n, k);

    if (s % g) {
        cout << -1 << endl;
        return;
    }

    n /= g;
    s /= g;
    k /= g;

    cout << mull(s, binpow(k, euler(n) - 1)) << endl;
}

void output() {
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    int number_of_testcases = 1;
    cin >> number_of_testcases;
    while (number_of_testcases--) {
        init();

        input();

        solve();

        output();
    }

    return 0;
}
