#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<cstdlib>
#include<queue>
#include<set>
#include<cstdio>
#include<map>
#include<cassert>

using namespace std;

#define ll long long
#define reps(i, a, b) for(int i = a; i < b; i++)
#define rreps(i, a, b) for(int i = a-1; i >= b; i--)
#define rep(i, n) reps(i, 0, n)
#define rrep(i, n) rreps(i, n, 0)
#define P pair<int, int>
#define vec vector<int>
#define mat vector<vector<int> >

const ll mod = 1000000007;
const ll INF = 1001001001001001001;

int n;
vec a, t;

ll fi(int i, ll num){
    if(i == n){
        return num;
    }
    if(t[i] == 1) {
        return fi(i+1, num + a[i]);
    }else if(t[i] == 2){
        return fi(i+1, max(num, (ll)a[i]));
    }else{
        return fi(i+1, min(num, (ll)a[i]));
    }
}

int main(){
    cin >> n;
    a = vec(n);
    t = vec(n);
    rep(i, n) {
        cin >> a[i] >> t[i];
    }
    int q;
    cin >> q;
    vec x(q);
    rep(i, q) cin >> x[i];
    ll lval = fi(0, -INF);
    ll rval = fi(0, INF);
    if(lval == rval){
        rep(i, q){
            cout << lval << endl;
        }
    }else{
        ll llim, rlim;
        {
            ll l = -INF, r = INF;
            while(l < r - 1){
                ll mid = (l + r) / 2;
                if(fi(0, mid) == lval) l = mid;
                else r = mid;
            }
            llim = l;
        }
        {
            ll l = -INF, r = INF;
            while(l < r - 1){
                ll mid = (l + r) / 2;
                if(fi(0, mid) == rval) r = mid;
                else l = mid;
            }
            rlim = r;
        }
        ll dif = fi(0, llim) - llim;
        rep(i, q){
            ll ans;
            if(x[i] <= llim) ans = lval;
            else if(x[i] >= rlim) ans = rval;
            else ans = (ll)x[i] + dif;
            cout << ans << endl;
        }
    }


}
/*************************************************************************
	> File Name: 1.cpp
	> Author: Knowledge_llz
	> Mail: 925538513@qq.com 
	> Blog: https://blog.csdn.net/Pig_cfbsl 
	> Created Time: 2020/10/11 14:09:05
 ************************************************************************/
#include<bits/stdc++.h>
#define For(i,a,b) for(register int i=(a);i<=(b);++i)
#define pb push_back
#define pr pair<int,int>
#define fi first
#define se second
#define LL long long
using namespace std;
int read(){
	char x=getchar(); int u=0,fg=0;
	while(!isdigit(x)){ if(x=='-') fg=1; x=getchar(); }
	while(isdigit(x)){ u=(u<<3)+(u<<1)+(x^48); x=getchar(); }
	return fg?-u:u;
}
const int mod=1e9+7;
int main()
{
#ifndef ONLINE_JUDGE
	freopen("input.in", "r", stdin);
	freopen("output.out", "w", stdout);
#endif
	int T=read();
while(T--){
	LL n=read(),a=read(),b=read(),d=n-a-b;
	if(n<a+b){ puts("0"); continue; }
	d=(d+1)*(d+2)%mod;
	a=n-a+1; b=n-b+1;
	n=a*b%mod;
	LL ans=2*d*n%mod-d*d%mod;
	cout<<(ans+mod)%mod<<endl;
}
	return 0;
}
