import sys input = sys.stdin.buffer.readline from collections import deque N = int(input()) E = [[] for _ in range(N)] for _ in range(N-1): a,b,c = map(int, input().split()) E[a-1].append(c*N+b-1) E[b-1].append(c*N+a-1) par = [-1]*N rnk = [-1]*N par[0] = 0 rnk[0] = 0 dist = [0]*N q = deque() q.append((0, 0, 0)) while q: temp = q.popleft() v = temp[0] r = temp[1] d = temp[2] for j in E[v]: if par[j%N] < 0: par[j%N] = v rnk[j%N] = r+1 dist[j%N] = d+j//N q.append((j%N, r+1, d+j//N)) #2^k上の祖先を求める par[0] = 0 LV = (N-1).bit_length() kpar = [par] for k in range(LV): T = [0]*N for i in range(N): if par[i] == 0: continue T[i] = par[par[i]] kpar.append(T) par = T #LCAをO(logN)で求める def lca(u, v): if rnk[u] > rnk[v]: u, v = v, u d = rnk[v]-rnk[u] for i in range(LV+1): if d & 1: v = kpar[i][v] d >>= 1 if u == v: return u for k in range(LV-1, -1, -1): pu = kpar[k][u] pv = kpar[k][v] if pu != pv: u = pu v = pv return kpar[0][u] Q = int(input()) for _ in range(Q): s,t = map(int, input().split()) s -= 1 t -= 1 an = lca(s, t) print(dist[s]+dist[t]-2*dist[an])