#include <bits/stdc++.h>
#define REP(i, m, n) for (int(i) = (m); (i) < (n); ++i)
#define rep(i, n) REP(i, 0, n)
#define all(x) (x).begin(), (x).end()
using namespace std;
using Graph = vector<vector<int>>;
template <class T>
inline bool chmax(T &a, T b)
{
    if (a < b)
    {
        a = b;
        return true;
    }
    return false;
}
template <class T>
inline bool chmin(T &a, T b)
{
    if (a > b)
    {
        a = b;
        return true;
    }
    return false;
}
typedef long long ll;
typedef pair<ll, ll> P;
const int INF = 1e9 + 7;
const ll LINF = 1LL << 60;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    int n, m, q;
    cin >> n >> m >> q;
    vector<pair<int, int>> vw(n, make_pair(0, 0));
    vector<pair<int, int>> x(m, make_pair(0, 0));

    rep(i, n)
    {
        int a, b;
        cin >> a >> b;
        vw[i] = make_pair(b, a);
    }
    sort(vw.begin(), vw.end(), greater<pair<int, int>>());

    rep(i, m)
    {
        int a;
        cin >> a;
        x[i] = make_pair(a, i);
    }
    sort(x.begin(), x.end());

    rep(i, q)
    {
        int l, r;
        cin >> l >> r;
        --l, --r;
        int ans = 0;
        vector<int> seen(m, 0);
        rep(i, n)
        {
            int xmin = INF;
            int xmin_idx = -1;
            rep(j, m)
            {
                if (x[j].second >= l && x[j].second <= r)
                    continue;
                if (vw[i].second <= x[j].first && seen[x[j].second] == 0 && x[j].first < xmin)
                {
                    xmin = x[j].first;
                    xmin_idx = x[j].second;
                }
            }
            if (xmin_idx == -1)
                continue;
            seen[xmin_idx] = 1;
            ans += vw[i].first;
        }
        cout << ans << endl;
    }

    return 0;
}
#include"bits/stdc++.h"
#include <inttypes.h>
using namespace std;

#define ll long long
int mod = 1e9+7;
#define int ll

int d, x, y;
void extendedEuclid(int A, int B) {
    if(B == 0) {
        d = A;
        x = 1;
        y = 0;
    }
    else {
        extendedEuclid(B, A%B);
        int temp = x;
        x = y;
        y = temp - (A/B)*y;
    }
}

int modInverse(int A, int M)
{
    extendedEuclid(A,M);
    return (x%M+M)%M;    //x may be negative
}

void test(){
	int n , s , k;
	cin >> n >> s >> k;
	int g = __gcd(n,k);
	if(s%g != 0){
		cout<<"-1\n";
		return;
	}
	n/=g , s/=g , k/=g;
	int ans = ((n-s) * modInverse(k,n))%n;
	cout<<ans<<"\n";
}

int32_t main(){
	#ifndef ONLINE_JUDGE
		freopen("input.txt","r",stdin);
		freopen("output.txt","w",stdout);
	#endif
	int t;
	cin >> t;
	while(t--){
		test();
	}
}