#include #define FOR(i,a,b) for (int i=(a);i<(b);i++) #define FORR(i,a,b) for (int i=(a);i>=(b);i--) #define pb push_back #define pcnt __builtin_popcount #define show(x) cout<<#x<<" = "< pii; typedef vector vi; typedef vector vvi; typedef vector vpii; typedef set si; typedef pair pll; typedef vector vl; typedef vector vvl; typedef vector vpll; typedef set sl; templatestring join(vector&v) {stringstream s;FOR(i,0,sz(v))s<<' '<b)swap(a,b);for(;a>0;b%=a,swap(a,b));return b;} int modpow(ll a,ll n,int m){if(a==0)return a;ll p=1;for(;n>0;n/=2,a=a*a%m)if(n&1)p=p*a%m;return(int)p;} void dout(double d){printf("%.12f\n",d);} const int iinf = 1e9; const ll linf = 1e18; const int mod = 1e9+7; const double pi = acos(-1); const double eps = 1e-10; ll n, dp[100001][2], Q, u, v, a, b, c, dd, r, ans; int db[100001][18], dpth[100001]; vi m[100001], m2[100001]; bool y[100001]; void setp(int i, int d){ dpth[i] = d; each(itr, m[i]){ if(y[*itr]) continue; y[*itr] = true; db[*itr][0] = i; m2[i].pb(*itr); setp(*itr, d+1); } } ll calc(int i){ ll s = dp[i][0], ss; each(itr, m2[i]) s += calc(*itr); ss = s + dp[i][1]; ans += ss*(ss+1)/2; return s; } int root(int i, int j){ FORR(k, 17, 0)if(db[i][k] != db[j][k]) return root(db[i][k], db[j][k]); return db[i][0]; } main(){ cin.tie(0); ios::sync_with_stdio(false); cin >> n; FOR(i, 0, n-1){ cin >> u >> v; m[u].pb(v); m[v].pb(u); } m[0].pb(1); db[0][0] = 0; y[0] = true; setp(0, 0); FOR(i, 1, 18)FOR(j, 0, n+1) db[j][i] = db[db[j][i-1]][i-1]; cin >> Q; FOR(i, 0, Q){ cin >> a >> b; if(dpth[a] > dpth[b]) swap(a, b); c = b; dd = dpth[b] - dpth[a]; FOR(j, 0, 18)if((dd & (1< 0) b = db[b][j]; r = (a == b ? a : root(a, b)); dp[a][0]++; dp[c][0]++; dp[r][0] -= 2; dp[r][1]++; } calc(0); cout << ans << endl; return 0; }