#include <bits/stdc++.h>
#define ll long long
ll md = 1000000007;
#define fast                          \
    ios_base::sync_with_stdio(false); \
    cin.tie(0);                       \
    cout.tie(0);
#define pb push_back
#define g(v, i, j) get<i>(v[j])
#define vi vector<int>
#define vll vector<ll>
#define srt(v) sort(v.begin(), v.end())
#define rsrt(v) sort(v.begin(), v.end(), greater<int>())
#define fi(i, a, b) for (int i = a; i < b; i++)
#define fll(i, a, b) for (ll i = a; i < b; i++)
using namespace std;
//using namespace std::chrono;
template <typename T>
T pw(T a, T b)
{
    T c = 1, m = a;
    while (b)
    {
        if (b & 1)
            c = (c * m);
        m = (m * m), b /= 2;
    }
    return c;
}
template <typename T>
T ceel(T a, T b)
{
    if (a % b == 0)
        return a / b;
    else
        return a / b + 1;
}
template <typename T>
T my_log(T n, T b)
{
    T i = 1, ans = 0;
    while (1)
    {
        if (i > n)
        {
            ans--;
            break;
        }
        if (i == n)
            break;
        i *= b, ans++;
    }
    return ans;
}
template <typename T>
T gcd(T a, T b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);
}
template <typename T>
T mysqt(T a)
{
    T ans = 1;
    while (ans * ans <= a)
        ans++;
    ans--;
    return ans;
}
ll pwmd(ll a, ll b)
{
    ll c = 1, m = a;
    while (b)
    {
        if (b & 1)
            c = (c * m) % md;
        m = (m * m) % md;
        b /= 2;
    }
    return c;
}
ll modinv(ll n)
{
    return pwmd(n, md - 2);
}
ll inverse(ll i)
{
    if (i == 1)
        return 1;
    return (md - ((md / i) * inverse(md % i)) % md + md) % md;
}
bool sortbysec(const pair<ll, ll> &a,
               const pair<ll, ll> &b)
{
    return (a.second < b.second);
}
/* auto start = high_resolution_clock::now();
    auto stop = high_resolution_clock::now();
    auto duration = duration_cast<microseconds>(stop - start);
    cerr << "Time taken: "
         << duration.count()<< "ms" <<"\n";*/
vector<char> capl = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
vector<char> sml = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
template <typename T>
string conv(T a)
{
    string b;
    T e = 2;
    T c = my_log(a, e);
    c++, b = '1';
    for (T i = 0; i < c - 1; i++)
    {
        T k = a >> (c - 2 - i);
        if (k & 1)
            b += '1';
        else
            b += '0';
    }
    if (a == 0)
        return "0";
    return b;
}
int main()
{
    fast;
    ll n, m, q, l, r;
    cin >> n >> m >> q;
    ll w[n], val[n], x[m];
    fll(i, 0, n) cin >> w[i] >> val[i];
    fll(i, 0, m) cin >> x[i];
    while (q--){
        vector<pair<int, int>> vp;
        fll(i, 0, n) vp.pb({w[i], val[i]});
        vll vis(n);
        vll xs;
        cin >> l >> r;
        l--, r--;
        fll(i, 0, m){
            if (i >= l && i <= r) continue;
            xs.pb(x[i]);
        }
        srt(xs);
        ll ans = 0;
        fll(i, 0, xs.size()){
            ll ind = -1, mx = 0;
            fll(j, 0, n){
                if (vis[j]) continue;
                if (vp[j].first <= xs[i]){
                   // cout << vp[j].second << "a\n";
                    if (vp[j].second > mx) mx = vp[j].second, ind = j;
                }
            }
            if (ind == -1) continue;
            ans += mx, vis[ind] = 1;
        }
        cout << ans << "\n";
    }
}

#include <bits/stdc++.h>
using namespace std;
typedef long long   ll;
typedef long double ld;
#define MAXN 1000005 
#define INF (ll)1e18
#define mod 1000000007 
//#define mod 998244353
#define endl '\n'
ll cc=0;

void boost()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
}


ll inv(ll a, ll m) 
{ 
    ll m0 = m; 
    ll y = 0, x = 1; 
  
    if (m == 1) 
        return 0; 
  
    while (a > 1) { 
        // q is quotient 
        ll q = a / m; 
        ll t = m; 
  
        // m is remainder now, process same as 
        // Euclid's algo 
        m = a % m, a = t; 
        t = y; 
  
        // Update y and x 
        y = x - q * y; 
        x = t; 
    } 
  
    // Make x positive 
    if (x < 0) 
        x += m0; 
  
    return x; 
} 


int main()
{
    boost();
 
    ll i,j,k,n,t,c=0,l,r,d=0;
    ll ans,h,z,m,u,q,mm,w,mx,x,y;
    ld f;
    
    cin>>t;
    
    while(t--)
    {
        cin>>n>>z>>k;
        y=__gcd(n,z);
        y=__gcd(y,k);
        
        n=n/y;
        z=z/y;
        k=k/y;
        ans=-1;
        
        l=n-z;
        r=__gcd(n,k);
        
        if(r!=1)
        {
            cout<<ans<<endl;
        }
        
        else
        {
            r=inv(k,n);
            ans=r*l;
            ans=ans%n;
            
            cout<<ans<<endl;
        }
   
    }  
return 0;
}
