from collections import deque import sys input=sys.stdin.readline J=19 N=int(input()) E={i:[] for i in range(1,N+1)} for _ in range(N-1): u,v,w=map(int,input().split()) E[u].append(w*N+v-1) E[v].append(w*N+u-1) T=[None]*(N+1) A=[[1]*J for _ in range(N+1)] Depth=[None]*(N+1) Q=deque() T[1]=0 Depth[1]=0 Q.append(1) while Q: v=Q.popleft() for x in E[v]: u=x%N+1 w=x//N if T[u]==None: T[u]=T[v]+w Depth[u]=Depth[v]+1 A[u][0]=v Q.append(u) for p in range(J-1): for u in range(1,N+1): v=A[u][p] A[u][p+1]=A[v][p] def upper(u,k): i=0 while k>0: if k%2==1: u=A[u][i] i+=1 k//=2 return u def lca(u,v): x=Depth[u] y=Depth[v] u=upper(u,max(x-y,0)) v=upper(v,max(y-x,0)) if u==v: return u for j in range(J-1,-1,-1): if A[u][j]!=A[v][j]: u=A[u][j] v=A[v][j] return A[u][0] Q=int(input()) X=[0]*Q for i in range(Q): u,v=map(int,input().split()) X[i]=T[u]+T[v]-2*T[lca(u,v)] print(*X,sep="\n")