#include #include using namespace std; using namespace atcoder; typedef long long ll; typedef long double ld; #define REP(i, n) for (int i = 0; i < (n); ++i) #define REPR(i, n) for (int i = n - 1; i >= 0; --i) #define FOR(i, m, n) for (int i = m; i < n; ++i) #define FORR(i, m, n) for (int i = m; i >= n; --i) #define ALL(v) (v).begin(),(v).end() #define ALLR(v) (v).rbegin(),(v).rend() #define fi first #define se second #define PB push_back #define EB emplace_back template using PQ = priority_queue; template using QP = priority_queue,greater>; templatevoid debug(const T &v,ll h,ll w){for(ll i=0;ivoid debug(const T &v,ll n){for(ll i=0;ivoid debug(const vector&v){debug(v,v.size());} templatevoid debug(const vector>&v){for(auto &vv:v)debug(vv,vv.size());} templatevoid debug(stack st){while(!st.empty()){cerr<void debug(queue st){while(!st.empty()){cerr<void debug(deque st){while(!st.empty()){cerr<void debug(PQ st){while(!st.empty()){cerr<void debug(QP st){while(!st.empty()){cerr<void debug(const set&v){for(auto z:v)cerr<void debug(const multiset&v){for(auto z:v)cerr<void debug(const array &a){for(auto z:a)cerr<void debug(const map&v){for(auto z:v)cerr<<"["<void foo(Head&& head, Tail&&... tail){cerr << head << " ";foo(move(tail)...);} templatebool chmax(T &a, const T &b) { if (abool chmin(T &a, const T &b) { if (b struct DoublingLowestCommonAncestor { const int LOG; vector< int > dep; const G &g; vector< vector< int > > table; DoublingLowestCommonAncestor(const G &g) : g(g), dep(g.size()), LOG(32 - __builtin_clz(g.size())) { table.assign(LOG, vector< int >(g.size(), -1)); } void dfs(int idx, int par, int d) { table[0][idx] = par; dep[idx] = d; for(auto &to : g[idx]) { if(to != par) dfs(to, idx, d + 1); } } void build() { dfs(0, -1, 0); for(int k = 0; k + 1 < LOG; k++) { for(int i = 0; i < table[k].size(); i++) { if(table[k][i] == -1) table[k + 1][i] = -1; else table[k + 1][i] = table[k][table[k][i]]; } } } int query(int u, int v) { if(dep[u] > dep[v]) swap(u, v); for(int i = LOG - 1; i >= 0; i--) { if(((dep[v] - dep[u]) >> i) & 1) v = table[i][v]; } if(u == v) return u; for(int i = LOG - 1; i >= 0; i--) { if(table[i][u] != table[i][v]) { u = table[i][u]; v = table[i][v]; } } return table[0][u]; } }; int main(){ cin.tie(0); ios::sync_with_stdio(false); int n;cin >> n; vector> e(n); vector h(n); REP(i,n-1){ int u,v;cin >> u >> v; u--,v--; e[u].PB(v); e[v].PB(u); h[u]++,h[v]++; } DoublingLowestCommonAncestor>> lca(e); lca.build(); vector c(n); vector p(n,-1); auto dfs=[&](auto &&dfs,int cur,int pre=-1)->void{ for(auto to:e[cur]){ if(to==pre) continue; p[cur]=pre; dfs(dfs,to,cur); } }; dfs(dfs,0); int q;cin >> q; while(q--){ int a,b;cin >> a >> b; a--,b--; c[a]++,c[b]++; int t=lca.query(a,b); c[t]--; if(p[t]!=-1) c[p[t]]--; } // debug(c); auto dfs2=[&](auto &&dfs2,int cur,int pre=-1)->void{ // foo(cur,pre,e.size()); for(auto to:e[cur]){ if(to==pre) continue; dfs2(dfs2,to,cur); c[cur]+=c[to]; } }; dfs2(dfs2,0); ll ans=0; // debug(c); REP(i,n) ans+=c[i]*(c[i]+1)/2; cout << ans << endl; }