#include "bits/stdc++.h" using namespace std; #define FOR(i,j,k) for(int (i)=(j);(i)<(int)(k);++(i)) #define rep(i,j) FOR(i,0,j) #define each(x,y) for(auto &(x):(y)) #define mp make_pair #define all(x) (x).begin(),(x).end() #define debug(x) cout<<#x<<": "<<(x)< pii; typedef vector vi; typedef vector vll; class LCA{ public: LCA(){ } LCA(const vector> &G, int root = 0):V((int)G.size()), depth(V), parent(V){ rep(i, LG)ancestor[i] = vector(V); dfs(root, 0, 0, G); rep(i, V) ancestor[0][i] = parent[i]; rep(i,LG-1)rep(v,V) ancestor[i + 1][v] = ancestor[i][ancestor[i][v]]; } int get(int u, int v){ if(depth[u] < depth[v]) swap(u, v); int dist = depth[u] - depth[v]; for(int i = 0; i < LG; ++i) if((dist >> i) & 1) u = ancestor[i][u]; if(u == v) return u; for(int i = LG - 1; i >= 0; --i){ if(ancestor[i][u] != ancestor[i][v]){ u = ancestor[i][u]; v = ancestor[i][v]; } } return ancestor[0][u]; } int getDepth(int u){ return depth[u]; } int goUp(int u, int len){ for(int i = 0; i < LG; ++i) if(len >> i & 1)u = ancestor[i][u]; return u; } int distance(int u, int v){ return depth[u] + depth[v] - 2 * depth[get(u, v)]; } int getParent(int u){ return parent[u]; } private: static const int LG = 18; int V; vector depth, parent, ancestor[LG]; void dfs(int v, int p, int d, const vector> &G){ depth[v] = d; parent[v] = p; for(int to : G[v]){ if(to != p){ dfs(to, v, d + 1, G); } } } }; void dfs(int u, int pre, vector &G, vi &imos){ each(to, G[u])if(to != pre){ dfs(to, u, G, imos); imos[u] += imos[to]; } } int N; int main(){ cin >> N; vector G(N); rep(i, N - 1){ int u, v; scanf("%d%d", &u, &v); --u; --v; G[u].push_back(v); G[v].push_back(u); } auto lca = LCA(G, 0); vi imos(N); int Q; cin >> Q; while(Q--){ int a, b; scanf("%d%d", &a, &b); --a; --b; int c = lca.get(a, b); if(c != 0) imos[lca.getParent(c)]--; imos[a]++; imos[b]++; imos[c]--; } dfs(0, -1, G, imos); ll ans = 0; each(x, imos)ans += (ll)x*(x + 1) / 2; cout << ans << endl; }