#include using namespace std; class LCA { public: LCA(int n) : g(n), parent(21, vector(n, -1)), depth(n) {} void add(int u, int v) { g[u].push_back(v); g[v].push_back(u); } void build() { dfs(0, -1); for (int i = 0; i < 20; i++) { for (int j = 0; j < g.size(); j++) { if (parent[i][j] != -1) { parent[i + 1][j] = parent[i][parent[i][j]]; } } } } int query(int u, int v) { if (depth[u] < depth[v]) swap(u, v); for (int i = 20; i >= 0; i--) { if (depth[u] - depth[v] >= 1 << i) { u = parent[i][u]; } } if (u == v) return u; for (int i = 20; i >= 0; i--) { if (parent[i][u] != parent[i][v]) { u = parent[i][u]; v = parent[i][v]; } } return parent[0][u]; } private: vector> g, parent; vector depth; void dfs(int curr, int prev) { parent[0][curr] = prev; for (int next : g[curr]) if (next != prev) { depth[next] = depth[curr] + 1; dfs(next, curr); } } }; vector g[101010]; long long imos[101010]; long long part[101010]; void dfs(int curr, int prev) { for (int next : g[curr]) if (next != prev) { dfs(next, curr); } if (prev != -1) { imos[prev] += imos[curr]; } } int main() { int n; cin >> n; LCA lca(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); lca.add(u, v); } lca.build(); int Q; cin >> Q; while (Q--) { int u, v; scanf("%d %d", &u, &v); u--; v--; int l = lca.query(u, v); imos[u]++; imos[v]++; imos[l] -= 2; part[l]++; } dfs(0, -1); long long ans = 0; for (int i = 0; i < n; i++) { long long v = imos[i] + part[i]; ans += v * (v + 1) / 2; } cout << ans << endl; }