import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines import itertools def EulerTour(graph, root=1): N = len(graph) - 1 parent = [0] * (N + 1) stack = [-root, root] tour = [] ind_L = [0] * (N + 1) ind_R = [0] * (N + 1) i = -1 while stack: v = stack.pop() tour.append(v) i += 1 if v > 0: ind_L[v] = i p = parent[v] for w in graph[v]: if w == p: continue parent[w] = v stack.append(-w) stack.append(w) else: ind_R[-v] = i return tour, ind_L, ind_R, parent class SegTree: """ segment tree with point modification and range product access. """ data_unit = None @classmethod def data_f(cls, x, y): if x is None: return y if y is None: return x if depth[x] > depth[y]: return y else: return x def __init__(self, N): self.N = N self.data = [self.data_unit] * (N + N) def build(self, raw_data): data = self.data f = self.data_f N = self.N data[N:] = raw_data[:] for i in range(N - 1, 0, -1): data[i] = f(data[i << 1], data[i << 1 | 1]) def set_val(self, i, x): data = self.data f = self.data_f i += self.N data[i] = x while i > 1: i >>= 1 data[i] = f(data[i << 1], data[i << 1 | 1]) def fold(self, L, R): """ compute for [L, R) """ vL = vR = self.data_unit data = self.data f = self.data_f L += self.N R += self.N while L < R: if L & 1: vL = f(vL, data[L]) L += 1 if R & 1: R -= 1 vR = f(data[R], vR) L >>= 1 R >>= 1 return f(vL, vR) N = int(readline()) graph = [[] for _ in range(N + 1)] for _ in range(N - 1): u, v = map(int, readline().split()) graph[u].append(v) graph[v].append(u) tour, ind_L, ind_R, parent = EulerTour(graph) depth = tuple(itertools.accumulate(1 if x > 0 else -1 for x in tour)) seg = SegTree(len(depth)) seg.build(range(len(depth))) imos = [0] * (N + 1) Q = int(readline()) m = map(int, read().split()) for a, b in zip(m, m): i, j = ind_L[a], ind_L[b] if i > j: i, j = j, i k = seg.fold(i, j + 1) lca = tour[k] if lca < 0: lca = parent[-lca] imos[a] += 1 imos[b] += 1 imos[lca] -= 1 imos[parent[lca]] -= 1 for v in tour[::-1]: if v > 0: imos[parent[v]] += imos[v] answer = sum(x * (x + 1) for x in imos) // 2 print(answer)