#include #include typedef long long int ll; using namespace std; // #include // using namespace atcoder; // @@ !! LIM(debug) // ---- inserted function f:<< from util.cc template ostream& operator<< (ostream& os, const pair& p) { os << "(" << p.first << ", " << p.second << ")"; return os; } template ostream& operator<< (ostream& os, const tuple& t) { os << "(" << get<0>(t) << ", " << get<1>(t) << ", " << get<2>(t) << ")"; return os; } template ostream& operator<< (ostream& os, const tuple& t) { os << "(" << get<0>(t) << ", " << get<1>(t) << ", " << get<2>(t) << ", " << get<3>(t) << ")"; return os; } template ostream& operator<< (ostream& os, const vector& v) { os << '['; for (auto it = v.begin(); it != v.end(); it++) { if (it != v.begin()) os << ", "; os << *it; } os << ']'; return os; } template ostream& operator<< (ostream& os, const set& v) { os << '{'; for (auto it = v.begin(); it != v.end(); it++) { if (it != v.begin()) os << ", "; os << *it; } os << '}'; return os; } template ostream& operator<< (ostream& os, const unordered_set& v) { os << '{'; for (auto it = v.begin(); it != v.end(); it++) { if (it != v.begin()) os << ", "; os << *it; } os << '}'; return os; } template ostream& operator<< (ostream& os, const multiset& v) { os << '{'; for (auto it = v.begin(); it != v.end(); it++) { if (it != v.begin()) os << ", "; os << *it; } os << '}'; return os; } template ostream& operator<< (ostream& os, const map& mp) { os << '['; for (auto it = mp.begin(); it != mp.end(); it++) { if (it != mp.begin()) os << ", "; os << it->first << ": " << it->second; } os << ']'; return os; } template ostream& operator<< (ostream& os, const unordered_map& mp) { os << '['; for (auto it = mp.begin(); it != mp.end(); it++) { if (it != mp.begin()) os << ", "; os << it->first << ": " << it->second; } os << ']'; return os; } template ostream& operator<< (ostream& os, const queue& orig) { queue que(orig); bool first = true; os << '['; while (!que.empty()) { T x = que.front(); que.pop(); if (!first) os << ", "; os << x; first = false; } return os << ']'; } template ostream& operator<< (ostream& os, const deque& orig) { deque que(orig); bool first = true; os << '['; while (!que.empty()) { T x = que.front(); que.pop_front(); if (!first) os << ", "; os << x; first = false; } return os << ']'; } template ostream& operator<< (ostream& os, const priority_queue& orig) { priority_queue pq(orig); bool first = true; os << '['; while (!pq.empty()) { T x = pq.top(); pq.pop(); if (!first) os << ", "; os << x; first = false; } return os << ']'; } template ostream& operator<< (ostream& os, const stack& st) { stack tmp(st); os << '['; bool first = true; while (!tmp.empty()) { T& t = tmp.top(); if (first) first = false; else os << ", "; os << t; tmp.pop(); } os << ']'; return os; } #if __cplusplus >= 201703L template ostream& operator<< (ostream& os, const optional& t) { if (t.has_value()) os << "v(" << t.value() << ")"; else os << "nullopt"; return os; } #endif ostream& operator<< (ostream& os, int8_t x) { os << (int32_t)x; return os; } // ---- end f:<< // ---- inserted library file debug.cc template string dbgFormat(const char* fmt, Args... args) { size_t len = snprintf(nullptr, 0, fmt, args...); char buf[len + 1]; snprintf(buf, len + 1, fmt, args...); return string(buf); } template void dbgLog(bool with_nl, Head&& head) { cerr << head; if (with_nl) cerr << endl; } template void dbgLog(bool with_nl, Head&& head, Tail&&... tail) { cerr << head << " "; dbgLog(with_nl, forward(tail)...); } #if DEBUG #define DLOG(...) dbgLog(true, __VA_ARGS__) #define DLOGNNL(...) dbgLog(false, __VA_ARGS__) #define DFMT(...) cerr << dbgFormat(__VA_ARGS__) << endl #define DCALL(func, ...) func(__VA_ARGS__) #else #define DLOG(...) #define DLOGNNL(...) #define DFMT(...) #define DCALL(func, ...) #endif /* #if DEBUG_LIB #define DLOG_LIB(...) dbgLog(true, __VA_ARGS__) #define DLOGNNL_LIB(...) dbgLog(false, __VA_ARGS__) #define DFMT_LIB(...) cerr << dbgFormat(__VA_ARGS__) << endl #define DCALL_LIB(func, ...) func(__VA_ARGS__) #else #define DLOG_LIB(...) #define DFMT_LIB(...) #define DCALL_LIB(func, ...) #endif */ #define DUP1(E1) #E1 "=", E1 #define DUP2(E1,E2) DUP1(E1), DUP1(E2) #define DUP3(E1,...) DUP1(E1), DUP2(__VA_ARGS__) #define DUP4(E1,...) DUP1(E1), DUP3(__VA_ARGS__) #define DUP5(E1,...) DUP1(E1), DUP4(__VA_ARGS__) #define DUP6(E1,...) DUP1(E1), DUP5(__VA_ARGS__) #define DUP7(E1,...) DUP1(E1), DUP6(__VA_ARGS__) #define DUP8(E1,...) DUP1(E1), DUP7(__VA_ARGS__) #define DUP9(E1,...) DUP1(E1), DUP8(__VA_ARGS__) #define DUP10(E1,...) DUP1(E1), DUP9(__VA_ARGS__) #define DUP11(E1,...) DUP1(E1), DUP10(__VA_ARGS__) #define DUP12(E1,...) DUP1(E1), DUP11(__VA_ARGS__) #define GET_MACRO(_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,NAME,...) NAME #define DUP(...) GET_MACRO(__VA_ARGS__, DUP12, DUP11, DUP10, DUP9, DUP8, DUP7, DUP6, DUP5, DUP4, DUP3, DUP2, DUP1)(__VA_ARGS__) #define DLOGK(...) DLOG(DUP(__VA_ARGS__)) #define DLOGKL(lab, ...) DLOG(lab, DUP(__VA_ARGS__)) #if DEBUG_LIB #define DLOG_LIB DLOG #define DLOGK_LIB DLOGK #define DLOGKL_LIB DLOGKL #endif // ---- end debug.cc // @@ !! LIM -- end mark -- int main(/* int argc, char *argv[] */) { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout << setprecision(20); ll N; cin >> N; vector> nbr(N); for (ll i = 0; i < N-1; i++) { ll u, v; cin >> u >> v; u--; v--; nbr[u].push_back(v); nbr[v].push_back(u); } vector parent(N); auto dfs1 = [&](auto rF, ll nd, ll pt) -> ll { parent[nd] = pt; ll ret = 1; ll smax = 0; auto& vec = nbr[nd]; if (nd == 0) vec.push_back(-1); DLOGK(nd, vec); ll sz = vec.size(); for (ll i = 0; i < sz - 1; i++) { ll c = vec[i]; if (c == pt) { swap(vec[i], vec[sz - 1]); c = vec[i]; } ll s = rF(rF, c, nd); ret += s; if (s > smax) { smax = s; swap(vec[i], vec[0]); } } vec.resize(sz - 1); return ret; }; dfs1(dfs1, 0, -1); DLOGK(parent); DLOGK(nbr); vector o2d(N), d2o(N), head(N), lev(N); { ll ser = 0; auto dfs2 = [&](auto rF, ll nd, ll hd, ll lv) -> void { DLOGKL(" dfs2", nd, hd, lv); o2d[nd] = ser; d2o[ser] = nd; head[ser] = hd; lev[ser] = lv; ser++; auto& vec = nbr[nd]; for (ll i = 0; i < (ll)vec.size(); i++) { if (i == 0) rF(rF, vec[i], hd, lv); else rF(rF, vec[i], ser, lv + 1); } }; dfs2(dfs2, 0, 0, 0); } DLOGK(o2d); DLOGK(d2o); DLOGK(head); DLOGK(lev); auto parentD = [&](ll t) -> ll { return t == 0 ? -1 : o2d[parent[d2o[t]]]; }; auto lcaD = [&](ll t1, ll t2) -> ll { auto step = [&](ll& t, ll& s) -> void { t = parentD(s); s = head[t]; }; ll s1 = head[t1]; ll s2 = head[t2]; while (lev[s1] > lev[s2]) step(t1, s1); while (lev[s1] < lev[s2]) step(t2, s2); while (s1 != s2) { step(t1, s1); step(t2, s2); } return min(t1, t2); }; // auto lca = [&](ll x, ll y) -> ll { return d2o[lcaD(o2d[x], o2d[y])]; }; vector diff(N + 1); auto func = [&](ll s, ll t, ll x) -> void { while (true) { ll h = head[s]; if (lev[t] >= lev[h]) break; diff[h] += x; diff[s + 1] -= x; s = parentD(h); } diff[t] += x; diff[s + 1] -= x; }; ll Q; cin >> Q; for (; Q > 0; Q--) { ll a, b; cin >> a >> b; a--; b--; ll ta = o2d[a], tb = o2d[b]; ll tc = lcaD(ta, tb); func(ta, tc, 1); func(tb, tc, 1); func(tc, tc, -1); } ll ans = 0; ll s = 0; for (ll i = 0; i < N; i++) { s += diff[i]; ans += s * (s + 1) / 2; } cout << ans << endl; return 0; }