  #include <bits/stdc++.h>
  #define ll long long int
  #define ull unsigned long long int
  #define eb emplace_back
  using namespace std;
  void vec(vector<int> & v){

      for(int i = 0;i<v.size() ; i++){
          cout<<v[i]<< " ";
      }
      cout<<endl;
  }
  void vec_ll(vector<long long int> & v){

      for(int i = 0;i<v.size() ; i++){
          cout<<v[i]<< " ";
      }
      cout<<endl;
  }
  void vec_ull(vector<ull> & v){

      for(int i = 0;i<v.size() ; i++){
          cout<<v[i]<< " ";
      }
      cout<<endl;
  }
  int parsestring(string temp){
      int num= 0;
      int digs = temp.size() - 1;
      for(int i = 0 ;i<=digs;i++){
          num += pow(10 , digs -i)*(temp[i] - '0');
      }
      return num;
  }
  void factorize(ll n , map<ll , ll>&m){
    for(ll i = 2;i<=(long long)sqrt(n);i++){
      if(n% i == 0){
        while(n%i == 0){
          m[i]++;
          n = n/i;
        }
      }
    }
    if(n > 1){m[n]++;}
  }
  
  vector<vector<int>>adj , v;
  vector<int>in , out,  h;
  int timer = 1;
void dfs(int root , int depth){
    // cout<<"at "<<root<<endl;
    in[root] = timer;
    h[root] = depth;
    v[depth].eb(in[root]);
    for(int i = 0;i<adj[root].size() ;i++){
        timer++;
        dfs(adj[root][i] ,depth +1 );
    }
    timer++;
    out[root] = timer;
}
    
  int main(){
      ios_base::sync_with_stdio(false);
      cin.tie(NULL);
      cout.tie(NULL);  
      int n;
      cin>>n;
     adj = vector<vector<int>>(n+1);
      for(int i = 2;i<=n;i++){
          int x;
          cin>>x;
          adj[x].eb(i);
      }
      v = vector<vector<int>>(n+1);
      in = out = h = vector<int>(n+1);
      dfs(1 , 0);
    int q;
    cin>>q;
    while(q){
        int u,d;
        cin>>u>>d;
        int temp1 = lower_bound(v[d].begin()  ,v[d].end() , in[u]) - v[d].begin();
        int temp2= lower_bound(v[d].begin() , v[d].end() , out[u]) - v[d].begin();
        // cout<<"temp "<<temp1<< " " <<temp2<<endl;
        if(temp1 < temp2){
            cout<< temp2- temp1<<endl;
        }
        else{cout<<0<<endl;}
        q--;
    }
  }
  
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define sz(x) int(x.size())
#define show(x) {for(auto i: x){cout << i << " ";} cout << endl;}
using namespace std;
using ll = long long;
using P = pair<int, int>;

const int MOD = 1000000007;
struct mint {
    ll x; // typedef long long ll;
    mint(ll x=0):x((x%MOD+MOD)%MOD){}
    mint operator-() const { return mint(-x);}
    mint& operator+=(const mint a) {
        if ((x += a.x) >= MOD) x -= MOD;
        return *this;
    }
    mint& operator-=(const mint a) {
        if ((x += MOD-a.x) >= MOD) x -= MOD;
        return *this;
    }
    mint& operator*=(const mint a) { (x *= a.x) %= MOD; return *this;}
    mint operator+(const mint a) const { return mint(*this) += a;}
    mint operator-(const mint a) const { return mint(*this) -= a;}
    mint operator*(const mint a) const { return mint(*this) *= a;}
    mint pow(ll t) const {
        if (!t) return 1;
        mint a = pow(t>>1);
        a *= a;
        if (t&1) a *= *this;
        return a;
    }

    // for prime MOD
    mint inv() const { return pow(MOD-2);}
    mint& operator/=(const mint a) { return *this *= a.inv();}
    mint operator/(const mint a) const { return mint(*this) /= a;}
};
istream& operator>>(istream& is, mint& a) { return is >> a.x;}
ostream& operator<<(ostream& os, const mint& a) { return os << a.x;}
// combination MOD prime
struct combination {
    vector<mint> fact, ifact;
    combination(int n):fact(n+1),ifact(n+1) {
        assert(n < MOD);
        fact[0] = 1;
        for (int i = 1; i <= n; ++i) fact[i] = fact[i-1]*i;
        ifact[n] = fact[n].inv();
        for (int i = n; i >= 1; --i) ifact[i-1] = ifact[i]*i;
    }
    mint operator()(int n, int k) {
        if (k < 0 || k > n) return 0;
        return fact[n]*ifact[k]*ifact[n-k];
    }
    mint p(int n, int k) {
        if (k < 0 || k > n) return 0;
        return fact[n]*ifact[n-k];
    }
}; // comb(10000007); // ←MOD-1が上限
// comb(5, 2) → 10, comb.p(5, 2) → 20
// comb.fact[4] → 24, 2のN乗 → mint(2).pow(N)

int main() {
    // parallel world (projection)
    int T;
    cin >> T;
    rep(i, T) {
        int N, A, B;
        scanf("%d %d %d", &N, &A, &B);
        if (A + B > N) {
            cout << 0 << '\n';
            continue;
        }
        // 辺で考えて重ならない方法をまず求める、(N-A-B+2)C2
        mint x = mint(N-A-B+2) * (N-A-B+1) / 2 * 2;
        mint y = mint(N-A+1) * (N-B+1) - x;
        mint ans = mint(N-A+1).pow(2) * mint(N-B+1).pow(2);
        ans -= y.pow(2);
        printf("%lld\n", ans.x);
    }
    return 0;
}