#include"bits/stdc++.h"
using namespace std; 
#define pb push_back
#define eb emplace_back
#define ins insert
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define repr(i,a,b) for(int i=a;i>=b;i--)
#define lb lower_bound
#define ub upper_bound
#define sz(x) (int)x.size()
#define pii pair<int,int>
#define fir first
#define sec second
#define popcount __builtin_popcount 
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using ll=long long int;
using ld=long double;
#define mp make_pair
#define mt make_tuple
#define bit(x,j) (((ll)x>>j)&1)
#define um unordered_map 
#define mem(a,val) memset(a,val,sizeof(a))
void __print(int x) {cerr << x;} 
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;} 
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x){cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;} 
void __print(double x) {cerr << x;}
void __print(ld x){cerr << x;} 
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x){cerr<<'\"'<<x<<'\"';}
void __print(const string &x) {cerr<<'\"'<<x<<'\"';}
void __print(bool x){cerr<<(x?"true":"false");}
template<typename T, typename V>
void __print(const pair<T, V> &x){cerr<<'{'; __print(x.first);cerr<<','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) 
{ return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")";}
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) 
{ return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")"; } 
void debug_out() { cerr << endl; } 
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) 
{
  cerr << " " << to_string(H);
  debug_out(T...);
}
#ifndef ONLINE_JUDGE 
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
#ifndef ONLINE_JUDGE 
#define debugt(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debugt(...) 42
#endif
constexpr ll inf=2223372036854775807;
constexpr int mod2=(998244353);
constexpr int MAX=(1e4+3); 
constexpr int N=2e5+5; 
ll gcd(ll a, ll b)
{
    if (a < b)
        return gcd(b, a);
    if (b == 0)
        return a;
    return gcd(b, a % b);
}
ll extgcd(ll a, ll b, ll &x, ll &y)
{
    if (b == 0){ x=1; y=0; return a; }
    ll d=extgcd(b,a%b,y,x);
    y-=a/b*x; return d;
}
ll modInv(ll a, ll p)
{
    if (gcd(a, p) != 1)
        return -1;
 
    ll x, y;
    extgcd(a, p, x, y);
 
    x %= p;
    if (x < 0)
        x += p;
    return x;
}
void solve()
{ 
 int n,s,k; cin>>n>>s>>k; int b=n-s,a=k,m=n; 
 int d=__gcd(a,__gcd(b,m)); a/=d; b/=d; m/=d; int t1=__gcd(a,m); 
 if(t1!=1){ cout<<-1<<"\n"; return; } 
 cout<<(modInv(a,m)*b)%m<<"\n"; 
}
int main()
{
  #ifndef ONLINE_JUDGE
  freopen("input.txt","r",stdin);
  freopen("output.txt","w",stdout);
  #endif   
  ios_base::sync_with_stdio(false);
  cin.tie(0); 
  int t=1; int c=1; cin>>t; 
  while(t--)
  { 
    solve(); c++; 
  }  
}
#include <iostream>
#include <iomanip>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <cstdio>
#include <utility>
#include <string>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <numeric>

using namespace std;

typedef uint64_t u64;
typedef int64_t s64;
typedef uint32_t u32;
typedef int32_t s32;
typedef vector<s32> vs32;
typedef vector<u32> vu32;
typedef vector<s64> vs64;
typedef vector<u64> vu64;

const double PI=3.14159265358979323846;

#define MAX(x, y) ((x) < (y) ? (y) : (x))
#define MIN(x, y) ((x) > (y) ? (y) : (x))

#define rep(i, N) for(int i = 0; i < N; ++i)

#define CEIL(x, y) (((x) + (y) - 1) / (y))
#define MOD 1000000007ULL

#define IN(l, r, x) ((l) <= (x) && (x) < (r))

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

    int n;
    cin >> n;
    vs64 a(n);
    vs32 t(n);
    rep (i, n)
    {
        cin >> a[i] >> t[i];
    }

    s64 minf = -1e18;
    s64 Minf = 1e18;
    s64 l = minf, r = Minf;
    s64 add = 0;
    for (int i = n - 1; i >= 0; --i)
    {
        if (t[i] == 1)
        {
            add += a[i];
            if (l != minf) l -= a[i];
            if (r != Minf) r -= a[i];
        }
        else if (t[i] == 2)
        {
            l = max(l, a[i]);
            if (r < l) l = r;
        }
        else
        {
            r = min(r, a[i]);
            if (r < l) r = l;
        }
    }

    int q;
    cin >> q;
    while (q--)
    {
        s64 x;
        cin >> x;
        if (x < l) cout << l + add << "\n";
        else if (x > r) cout << r + add << "\n";
        else cout << x + add << "\n";
    }

    return 0;
}

