#include using namespace std; typedef long long ll; class HLD { vector > G; vector vid, head, heavy, par, dep; int dfs(int v, int prev) { par[v] = prev; int cnt = 1, ma = 0; for (int to : G[v]) if (to != prev) { dep[to] = dep[v] + 1; int c = dfs(to, v); cnt += c; if (ma < c) { ma = c; heavy[v] = to; } } return cnt; } public: HLD(int n) : G(n), vid(n, -1), head(n), heavy(n, -1), par(n), dep(n) {} void add(int u, int v) { G[u].push_back(v); G[v].push_back(u); } void build() { dfs(0, -1); int k = 0; queue q; q.push(0); while (!q.empty()) { int h = q.front(); q.pop(); for (int i = h; i != -1; i = heavy[i]) { vid[i] = k++; head[i] = h; for (int j : G[i]) if (j != par[i] && j != heavy[i]) q.push(j); } } } int operator[](int v) { return vid[v]; } template // F -> int -> int -> void void for_each(int u, int v, F f) { while (true) { if (vid[u] > vid[v]) swap(u, v); f(max(vid[head[v]], vid[u]), vid[v]); if (head[u] == head[v]) break; v = par[head[v]]; } } template // F -> int -> int -> int -> void void for_each_directed(int u, int v, F f) { while (true) { if (vid[u] > vid[v]) { f(max(vid[head[u]], vid[v]), vid[u], 1); if (head[u] == head[v]) break; u = par[head[u]]; } else { f(max(vid[head[v]], vid[u]), vid[v], 0); if (head[u] == head[v]) break; v = par[head[v]]; } } } template // F -> int -> int -> void void for_each_edge(int u, int v, F f) { while (true) { if (vid[u] > vid[v]) swap(u, v); if (head[u] != head[v]) { f(vid[head[v]], vid[v]); v = par[head[v]]; } else { if (u != v) f(vid[u] + 1, vid[v]); break; } } } //int ancestor(int u, int d) { // while (true) { // if (dep[head[u]] > dep[u] - d) { // d -= dep[u] - dep[head[u]] + 1; // if (head[u] == 0) return 0; // u = par[head[u]]; // } // else { // return inv[vid[u] - d]; // } // } //} int lca(int u, int v) { while (true) { if (vid[u] > vid[v]) swap(u, v); if (head[u] == head[v]) return u; v = par[head[v]]; } } int distance(int u, int v) { return dep[u] + dep[v] - 2 * dep[lca(u, v)]; } }; template class LazySegmentTree { const int n; const T id; vector data, data2; int size(int n) { int res = 1; while (res < n) res <<= 1; return res; } T sub(int l, int r, int node, int lb, int ub) { if (ub <= l || r <= lb) return id; if (l <= lb && ub <= r) { return data[node] + data2[node] * (ub - lb); } return data2[node] * (min(r, ub) - max(l, lb)) + sub(l, r, node * 2, lb, (lb + ub) / 2) + sub(l, r, node * 2 + 1, (lb + ub) / 2, ub); } void suc(int l, int r, int node, int lb, int ub, T val) { if (ub <= l || r <= lb) return; if (l <= lb && ub <= r) { data2[node] += val; return; } data[node] += val * (min(r, ub) - max(l, lb)); suc(l, r, node * 2, lb, (lb + ub) / 2, val); suc(l, r, node * 2 + 1, (lb + ub) / 2, ub, val); } public: LazySegmentTree(int n_) : n(size(n_)), id(0), data(n * 2, id), data2(n * 2, id) {} void add(int l, int r, T val) { suc(l, r + 1, 1, 0, n, val); } T getSum(int l, int r) { return sub(l, r + 1, 1, 0, n); } }; struct Function { // O͓Kł Function(ll& r, LazySegmentTree& seg) : res(r), lst(seg) {} ll& res; LazySegmentTree& lst; void operator()(int l, int r) { res += lst.getSum(l, r); } }; struct Add { LazySegmentTree& lst; Add(LazySegmentTree& seg) : lst(seg) {} void operator()(int l, int r) { lst.add(l, r, 1); } }; int main() { ll N, Q, A, B; cin >> N; HLD hl(N); for (int i = 0, u, v; i < N - 1; i++) { cin >> u >> v; hl.add(u - 1, v - 1); } hl.build(); LazySegmentTree lst(N); cin >> Q; ll res = 0; Function f(res, lst); Add add(lst); while (Q--) { cin >> A >> B; A--; B--; hl.for_each(A, B, f); res += hl.distance(A, B) + 1; hl.for_each(A, B, add); } cout << res << endl; return 0; }