import os,sys,random,threading #sys.exit() 退出程序 from random import randint,choice,shuffle #randint(a,b)从[a,b]范围随机选择一个数 #choice(seq)seq可以是一个列表,元组或字符串,从seq中随机选取一个元素 #shuffle(x)将一个可变的序列x中的元素打乱 from copy import deepcopy from io import BytesIO,IOBase from types import GeneratorType from functools import lru_cache,reduce #reduce(op,迭代对象) from bisect import bisect_left,bisect_right #bisect_left(x) 大于等于x的第一个下标 #bisect_right(x) 大于x的第一个下标 from collections import Counter,defaultdict,deque from itertools import accumulate,combinations,permutations #accumulate(a)用a序列生成一个累积迭代器,一般list化前面放个[0]做前缀和用 #combinations(a,k)a序列选k个 组合迭代器 #permutations(a,k)a序列选k个 排列迭代器 from heapq import heapify,heappop,heappush #heapify将列表转为堆 from typing import Generic,Iterable,Iterator,TypeVar,Union,List from string import ascii_lowercase,ascii_uppercase,digits #小写字母,大写字母,十进制数字 from math import ceil,floor,sqrt,pi,factorial,gcd,log,log10,log2,inf #ceil向上取整,floor向下取整 ,sqrt开方 ,factorial阶乘 from decimal import Decimal,getcontext #Decimal(s) 实例化Decimal对象,一般使用字符串 #getcontext().prec=100 修改精度 from sys import stdin, stdout, setrecursionlimit input = lambda: sys.stdin.readline().rstrip("\r\n") MI = lambda :map(int,input().split()) li = lambda :list(MI()) ii = lambda :int(input()) mod = int(1e9 + 7) #998244353 inf = int(1e20) py = lambda :print("YES") pn = lambda :print("NO") DIRS = [(0, 1), (1, 0), (0, -1), (-1, 0)] # 右下左上 DIRS8 = [(0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1), (-1, 0),(-1, 1)] # →↘↓↙←↖↑↗ import os,sys,random,threading #sys.exit() 退出程序 #sys.setrecursionlimit(10**6) #调整栈空间 from random import randint,choice,shuffle #randint(a,b)从[a,b]范围随机选择一个数 #choice(seq)seq可以是一个列表,元组或字符串,从seq中随机选取一个元素 #shuffle(x)将一个可变的序列x中的元素打乱 from copy import deepcopy from io import BytesIO,IOBase from types import GeneratorType from functools import lru_cache,reduce #reduce(op,迭代对象) from bisect import bisect_left,bisect_right #bisect_left(x) 大于等于x的第一个下标 #bisect_right(x) 大于x的第一个下标 from collections import Counter,defaultdict,deque from itertools import accumulate,combinations,permutations #accumulate(a)用a序列生成一个累积迭代器,一般list化前面放个[0]做前缀和用 #combinations(a,k)a序列选k个 组合迭代器 #permutations(a,k)a序列选k个 排列迭代器 from heapq import heapify,heappop,heappush #heapify将列表转为堆 from typing import Generic,Iterable,Iterator,TypeVar,Union,List from string import ascii_lowercase,ascii_uppercase,digits #小写字母,大写字母,十进制数字 from math import ceil,floor,sqrt,pi,factorial,gcd,log,log10,log2,inf #ceil向上取整,floor向下取整 ,sqrt开方 ,factorial阶乘 from decimal import Decimal,getcontext #Decimal(s) 实例化Decimal对象,一般使用字符串 #getcontext().prec=100 修改精度 from sys import stdin, stdout, setrecursionlimit input = lambda: sys.stdin.readline().rstrip("\r\n") MI = lambda :map(int,input().split()) li = lambda :list(MI()) ii = lambda :int(input()) mod = int(1e9 + 7) #998244353 inf = 1<<60 py = lambda :print("YES") pn = lambda :print("NO") DIRS = [(0, 1), (1, 0), (0, -1), (-1, 0)] # 右下左上 DIRS8 = [(0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1), (-1, 0),(-1, 1)] # →↘↓↙←↖↑↗ class BIT: """区间更新,区间和查询 """ __slots__ = "size", "_tree1", "_tree2" def __init__(self, n: int): self.size = n self._tree1 = [0]*(n+1) self._tree2 = [0]*(n+1) def add(self, left: int, right: int, delta: int) -> None: """闭区间[left, right]加delta""" self._add(left, delta) self._add(right + 1, -delta) def query(self, left: int, right: int) -> int: """闭区间[left, right]的和""" return self._query(right) - self._query(left - 1) def _add(self, index: int, delta: int) -> None: rawIndex = index while index <= self.size: self._tree1[index] += delta self._tree2[index] += (rawIndex - 1) * delta index += index & -index def _query(self, index: int) -> int: if index > self.size: index = self.size rawIndex = index res = 0 while index > 0: res += rawIndex * self._tree1[index] - self._tree2[index] index -= index & -index return res class HLD: def __init__(self, g, root): #无论是点还是dfn还是dep,都从1开始,默认0是无 n=len(g) self.g=g self.fa=[0]*(n+5) #父节点,0表示无父节点 self.size=[1]*(n+5) #子树大小 self.dep=[0]*(n+5) #深度,根深度为1 self.son=[0]*(n+5) #重儿子,0表示无儿子 self.dfn=[0]*(n+5) #dfs序,子树终点的dfs序是dfn[i]+size[i]-1 self.top=[0]*(n+5) #所在重链起点,起点就是自己 self.rank=[0]*(n+5) #dfs序为i的节点编号 fa=self.fa;size=self.size;dep=self.dep;son=self.son dfn=self.dfn;top=self.top;rank=self.rank stk=[[root,0,0]] #node,flag,fa dep[root]=1 while stk: u,flag,father=stk.pop() if flag: for v in g[u]: if v!=father: size[u]+=size[v] if son[u]==0 or size[v]>size[son[u]]: son[u]=v else: stk.append([u,1,father]) for v in g[u]: if v!=father: stk.append([v,0,u]) fa[v]=u dep[v]=dep[u]+1 stk=[[root,root]] tot=1 while stk: u,tops=stk.pop() dfn[u]=tot rank[tot]=u tot+=1 top[u]=tops if son[u]==0: continue for v in g[u]: if v!=fa[u] and v!=son[u]: stk.append([v,v]) stk.append([son[u],tops]) def lca(self,u,v): #求u和v的最近公共祖先节点 fa=self.fa;size=self.size;dep=self.dep;son=self.son dfn=self.dfn;top=self.top;rank=self.rank while top[u]!=top[v]: if dep[top[u]]>dep[top[v]]: u=fa[top[u]] else: v=fa[top[v]] return v if dep[u]>dep[v] else u def dis(self,u,v): dep=self.dep return dep[u]+dep[v]-2*dep[self.lca(u,v)] def kth_fa(self,root,k): #求root节点的第k个祖先 fa=self.fa;size=self.size;dep=self.dep;son=self.son dfn=self.dfn;top=self.top;rank=self.rank if k>=dep[root]: #无第k个祖先返回-1 return -1 while True: u=top[root] if dfn[root]-k>=dfn[u]: return rank[dfn[root]-k] k-=dfn[root]-dfn[u]+1 root=fa[u] def route_query(self,u,v): #查询u到v简单路径 fa=self.fa;size=self.size;dep=self.dep;son=self.son dfn=self.dfn;top=self.top;rank=self.rank route=[] while top[u]!=top[v]: if dep[top[u]]dep[v]: u,v=v,u route.append((dfn[u],dfn[v])) return route def path(self,start,end): #得到start到end简单路径的所有节点 fa=self.fa;size=self.size;dep=self.dep;son=self.son dfn=self.dfn;top=self.top;rank=self.rank mid=self.lca(start,end) ps=[] while start!=mid: ps.append(start) start=fa[start] pe=[] while end!=mid: pe.append(end) end=fa[end] return ps+[mid]+pe[::-1] n=ii() g=[[] for _ in range(n+1)] for _ in range(n-1): u,v=li() g[u]+=[v] g[v]+=[u] hld=HLD(g,1) bit=BIT(n+5) q=ii() tot=0 for _ in range(q): x,y=li() for l,r in hld.route_query(x,y): bit.add(l,r,1) for l,r in hld.route_query(x,y): tot+=bit.query(l,r) print(tot)