#include <bits/stdc++.h>   using namespace std;   class Graph {     long long n;     vector<long long> f, par, ans, sz, depth, value, size, fst, to, next;     long long l;     void swapUtil(long long &, long long &);     long long getP(long long);     long long dfsUtil(long long);     void setDFS();     void setsz();     void setf();     void setAns(long long);       public:     Graph();     Graph(long long);     void addEdge(long long, long long);     void setSolve(long long);     void solve(long long, long long, long long);     void printAns();   };   void Graph ::swapUtil(long long &u, long long &v) {     long long t = u;     u = v;     v = t;   }   long long Graph ::getP(long long u) {     if (f[u] == u)       return f[u];     else {       f[u] = getP(f[u]);       return f[u];     }   }   long long Graph ::dfsUtil(long long u) {     depth[u] = depth[par[u]] + 1;     for (long long i = fst[u]; i; i = next[i])       if (to[i] != par[u]) {         par[to[i]] = u;         long long t = dfsUtil(to[i]);         value[u] += size[u] * t * 2;         size[u] += t;       }     value[u] += size[u] * (n - size[u] - 1) * 2;     ans[0] += value[u];     size[u]++;     return size[u];   }   void Graph ::setDFS() { dfsUtil(1); }   void Graph ::setsz() {     for (long long i = 1; i < n + 1; i++) sz[i] = 1;   }   void Graph ::setf() {     for (long long i = 1; i < n + 1; i++) f[i] = i;   }   void Graph ::setAns(long long M) { ans.resize(M + 1); }   Graph ::Graph() {}   Graph ::Graph(long long N) {     n = N - 1;     f.resize(N);     par.resize(N);     ans.resize(N);     sz.resize(N);     depth.resize(N);     value.resize(N);     size.resize(N);     fst.resize(N);     to.resize(2 * N);     next.resize(2 * N);     l = 0;   }   void Graph ::addEdge(long long u, long long v) {     to[++l] = v;     next[l] = fst[u];     fst[u] = l;     to[++l] = u;     next[l] = fst[v];     fst[v] = l;   }   void Graph ::setSolve(long long m) {     setAns(m);     setf();     setsz();     setDFS();   }   void Graph ::solve(long long u, long long v, long long i) {     ans[i] = ans[i - 1];     u = getP(u);     v = getP(v);     while (u != v) {       if (depth[u] < depth[v]) swap(u, v);       long long t = getP(par[u]);       ans[i] +=           sz[t] * sz[u] * (size[u] - sz[u]) * 2 + sz[t] * sz[u] * (sz[u] - 1);       ans[i] +=           sz[u] * sz[t] * (n - size[u] - sz[t]) * 2 + sz[u] * sz[t] * (sz[t] - 1);       ans[i] += sz[t] * (value[u] - (n - size[u]) * (size[u] - sz[u]) * 2);       ans[i] += sz[u] * (value[t] - size[u] * (n - size[u] - sz[t]) * 2);       value[t] = value[t] - sz[u] * (n - size[u] - sz[t]) * 2 + value[u] -                  (size[u] - sz[u]) * (n - size[u]) * 2;       sz[t] += sz[u];       f[u] = t;       u = t;     }   }   void Graph ::printAns() {     for (long long i = 0; i < ans.size(); i++) cout << ans[i] << endl;   }   signed main() {     long long n;     cin >> n;     Graph g(n + 1);     for (long long i = 0; i < n - 1; i++) {       long long u, v;       cin >> u >> v;       g.addEdge(u, v);     }     long long m;     cin >> m;     g.setSolve(m);     for (long long i = 0; i < m; i++) {       long long u, v;       cin >> u >> v;       g.solve(u, v, i + 1);     }     g.printAns();     return 0;   }