#include "bits/stdc++.h" using namespace std; #define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i)) #define rer(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i)) #define reu(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i)) static const int INF = 0x3f3f3f3f; static const long long INFL = 0x3f3f3f3f3f3f3f3fLL; typedef vector vi; typedef pair pii; typedef vector > vpii; typedef long long ll; template static void amin(T &x, U y) { if(y < x) x = y; } template static void amax(T &x, U y) { if(x < y) x = y; } vector t_parent; vi t_ord; void tree_getorder(const vector &g, int root) { int n = g.size(); t_parent.assign(n, -1); t_ord.clear(); vector stk; stk.push_back(root); while(!stk.empty()) { int i = stk.back(); stk.pop_back(); t_ord.push_back(i); for(int j = (int)g[i].size() - 1; j >= 0; j --) { int c = g[i][j]; if(t_parent[c] == -1 && c != root) stk.push_back(c); else t_parent[i] = c; } } } class SchieberVishkinLCA { public: typedef unsigned Word; typedef int Vertex; private: static inline Word lowestOneBit(Word v) { return v & (~v + 1); } static inline Word highestOneBitMask(Word v) { v |= v >> 1; v |= v >> 2; v |= v >> 4; v |= v >> 8; v |= v >> 16; return v >> 1; } std::vector indices; //Vertex -> index std::vector maxHIndices; //Vertex -> index std::vector ancestorHeights; //Vertex -> Word std::vector pathParents; //index-1 -> Vertex public: void build(const std::vector &preorder, const std::vector &parents, Vertex root) { Vertex N = static_cast(preorder.size()); ancestorHeights.resize(N); maxHIndices.resize(N); indices.resize(N); pathParents.resize(N); for(Vertex ix = 0; ix < N; ++ ix) indices[preorder[ix]] = ix + 1; for(Vertex i = 0; i < N; ++ i) maxHIndices[i] = indices[i]; for(Vertex ix = N - 1; ix > 0; -- ix) { Vertex v = preorder[ix], parent = parents[v]; if(lowestOneBit(maxHIndices[parent]) < lowestOneBit(maxHIndices[v])) maxHIndices[parent] = maxHIndices[v]; } ancestorHeights[root] = 0; for(Vertex ix = 1; ix < N; ++ ix) { Vertex v = preorder[ix], parent = parents[v]; ancestorHeights[v] = ancestorHeights[parent] | lowestOneBit(maxHIndices[v]); } pathParents[0] = root; for(Vertex ix = 1; ix < N; ++ ix) { Vertex v = preorder[ix], parent = parents[v]; if(maxHIndices[v] != maxHIndices[parent]) pathParents[indices[v] - 1] = parent; else pathParents[indices[v] - 1] = pathParents[indices[parent] - 1]; } } Vertex query(Vertex v, Vertex u) const { Word Iv = maxHIndices[v], Iu = maxHIndices[u]; Word hIv = lowestOneBit(Iv), hIu = lowestOneBit(Iu); Word hbMask = highestOneBitMask((Iv ^ Iu) | hIv | hIu); Word j = lowestOneBit(ancestorHeights[v] & ancestorHeights[u] & ~hbMask); Vertex x, y; if(j == hIv) x = v; else { Word kMask = highestOneBitMask(ancestorHeights[v] & (j - 1)); x = pathParents[(indices[v] & ~kMask | (kMask + 1)) - 1]; } if(j == hIu) y = u; else { Word kMask = highestOneBitMask(ancestorHeights[u] & (j - 1)); y = pathParents[(indices[u] & ~kMask | (kMask + 1)) - 1]; } return indices[x] < indices[y] ? x : y; } }; int main() { int N; while(~scanf("%d", &N)) { vector > g(N); for(int i = 0; i < N - 1; ++ i) { int u, v; scanf("%d%d", &u, &v), -- u, -- v; g[u].push_back(v); g[v].push_back(u); } tree_getorder(g, 0); SchieberVishkinLCA lca; lca.build(t_ord, t_parent, 0); vector cnt(N); int Q; scanf("%d", &Q); rep(ii, Q) { int A; int B; scanf("%d%d", &A, &B), -- A, -- B; int C = lca.query(A, B); ++ cnt[A]; ++ cnt[B]; if(C != 0) -- cnt[t_parent[C]]; -- cnt[C]; } for(int ix = (int)t_ord.size() - 1; ix > 0; -- ix) { int i = t_ord[ix], p = t_parent[i]; cnt[p] += cnt[i]; } ll ans = 0; rep(i, N) { int n = cnt[i]; ans += (ll)n * (n + 1) / 2; } printf("%lld\n", ans); } return 0; }