#include <iostream>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <map>
#include <cmath>
#include <cstring>
#include <stack>
#include <queue>
#include <utility>
#include <set>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
vector<vector<ll>> nm(3, vector<ll>(6));
const int N = 3;
const int M = 1e9 + 7;
vector<ll> num(N);
vector<ll> sum(N);
ll max(ll x, ll y){
return x > y ? x : y;
}
ll min(ll x, ll y){
return x < y ? x : y;
}
bool cmp(int x, int y){
return x > y;
}
ll lowbit(ll x){
return x & -x;
}
ll gcd(ll a, ll b){
ll r;
while (b > 0){
r = a % b;
a = b;
b = r;
}
return a;
}
ll lcm(ll a, ll b){
return a * b / gcd(a, b);
}
void solve(){
ll n;cin>>n;
map<ll,ll> mp;
vector<ll> mm(n+1);
for(ll i=1;i<n;++i){
ll x,y;cin>>x>>y;
mp[max(x,y)]=min(x,y);
mm[min(x,y)]++;
}
for(ll i=1;i<=n;++i){
if(mm[i]==0) mm[i]=1;
}
ll t;cin>>t;
for(ll i=n;i>=1;i--){
mm[mp[i]]+=mm[i]-1;
}
for(ll i=1;i<=t;++i){
ll a,b;cin>>a>>b;
cout<<mm[a]*mm[b]<<'\n';
}
return;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
ll t;
cin >> t;
while (t--)
solve();
return 0;
}