CodeEff/ECCO huggingface dataset:
# Original solution
import sys
import math
from collections import defaultdict
from bisect import bisect_left, bisect_right

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(eval(input()))
def LI(): return list(map(int, input().split()))
def LIR(row,col):
    if row <= 0:
        return [[] for _ in range(col)]
    elif col == 1:
        return [I() for _ in range(row)]
    else:
        read_all = [LI() for _ in range(row)]
        return list(map(list, list(zip(*read_all))))

#################

from heapq import heappop, heappush
def dijkstra(E,s,n):
    d = [float('inf')]*n
    d[s] = 0
    q = []
    heappush(q,(0,s))
    while q:
        du,u = heappop(q)
        if d[u] < du:
            continue
        for v,weight in list(E[u].items()):
            dnew = du + weight
            if d[v] > dnew:
                d[v] = dnew
                heappush(q,(dnew,v))
    return d

N = I()
a,b,c = LIR(N-1,3)
Q,K = LI()
x,y = LIR(Q,2)

E = [dict() for _ in range(N)]
for i in range(N-1):
    E[a[i]-1][b[i]-1] = c[i]
    E[b[i]-1][a[i]-1] = c[i]

d = dijkstra(E,K-1,N)

for i in range(Q):
    print((d[x[i]-1]+d[y[i]-1]))

# Optimized solution
import sys
import math
from collections import defaultdict
from bisect import bisect_left, bisect_right

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(eval(input()))
def LI(): return list(map(int, input().split()))
def LIR(row,col):
    if row <= 0:
        return [[] for _ in range(col)]
    elif col == 1:
        return [I() for _ in range(row)]
    else:
        read_all = [LI() for _ in range(row)]
        return list(map(list, list(zip(*read_all))))

#################

from heapq import heappop, heappush
def dijkstra(E,s,n,inf=float('inf')):
    d = [inf]*n
    d[s] = 0
    q = []
    heappush(q,(0,s))
    while q:
        du,u = heappop(q)
        if d[u] < du:
            continue
        for v,dist in E[u]:
            dnew = du + dist
            if d[v] > dnew:
                d[v] = dnew
                heappush(q,(dnew,v))
    return d

N = I()
a,b,c = LIR(N-1,3)
Q,K = LI()
x,y = LIR(Q,2)

E = [[] for _ in range(N)]
for i in range(N-1):
    E[a[i]-1].append((b[i]-1,c[i]))
    E[b[i]-1].append((a[i]-1,c[i]))

d = dijkstra(E,K-1,N,inf=10**9*(N-1)+1)

for i in range(Q):
    print((d[x[i]-1]+d[y[i]-1]))
# Original solution
N=int(eval(input()))
edge=[list(map(int,input().split())) for i in range(N-1)]
INF=10**15
c=[[INF]*N for i in range(N)]
for i,j,w in edge:
    c[i-1][j-1]=w
    c[j-1][i-1]=w
Q,K=list(map(int, input().split()))
xy=[list(map(int,input().split())) for i in range(Q)]
from heapq import heappop,heappush
def dij(c,N,start):
    d=[INF]*N
    d[start]=0
    prev=[-1]*N
    q=[]
    heappush(q,(0,start))
    while len(q)>0:
        u=heappop(q)[1]
        for v in range(N):
            if c[u][v]<INF:
                temp=d[u]+c[u][v]
                if d[v]>temp:
                    d[v]=temp
                    prev[v]=u
                    heappush(q,(d[v],v))
    return d
d=dij(c,N,K-1)
for x,y in xy:
    print((d[x-1]+d[y-1]))

# Optimized solution
N=int(eval(input()))
edge=[list(map(int,input().split())) for i in range(N-1)]
inf=10**15
c=[[] for i in range(N)]
for i,j,w in edge:
    c[i-1].append((j-1,w))
    c[j-1].append((i-1,w))
Q,K=list(map(int, input().split()))
d=[inf]*N
d[K-1]=0
v=[0]*N
import sys
sys.setrecursionlimit(10**5+9)
def dfs(p,v,d):
    for n,w in c[p]:
        if v[n]==0:
            d[n]=min(d[n],d[p]+w)
            v[n]=1
            dfs(n,v,d)
dfs(K-1,v,d)
xy=[list(map(int,input().split())) for i in range(Q)]
for x,y in xy:
    print((d[x-1]+d[y-1]))
# Original solution
from collections import defaultdict
 
n = int(eval(input()))
V = defaultdict(list)

for _ in range(n-1):
    a, b,c = list(map(int, input().split()))
    V[a].append([b,c])
    V[b].append([a,c])

def dfs(source):
    dist = [-1]*(n+1)
    dist[source] = 0
    q = [source]
    while q:
        a = q.pop()
        for b in V[a]:
            if dist[b[0]]!=-1:
                continue
            dist[b[0]] = dist[a] + b[1]
            q.append(b[0])
    return dist

q,k = list(map(int, input().split()))

for i in range(q):
    x,y = list(map(int,input().split()))
    print((dfs(x)[k]+dfs(y)[k]))

# Optimized solution
from collections import defaultdict
 
n = int(eval(input()))
V = defaultdict(list)

for _ in range(n-1):
    a, b,c = list(map(int, input().split()))
    V[a].append([b,c])
    V[b].append([a,c])

def dfs(source):
    dist = [-1]*(n+1)
    dist[source] = 0
    q = [source]
    while q:
        a = q.pop()
        for b in V[a]:
            if dist[b[0]]!=-1:
                continue
            dist[b[0]] = dist[a] + b[1]
            q.append(b[0])
    return dist

q,k = list(map(int, input().split()))
dk = dfs(k)

for i in range(q):
    x,y = list(map(int,input().split()))
    print((dk[x]+dk[y]))
# Original solution
## abc070d: Transit Tree Path

n = int(eval(input()))

T = [None]*(n+1)
for _ in range(n-1):
    v1,v2,d = list(map(int, input().split()))
    T[v1] = T[v1] + [[v1,v2,d]] if(T[v1]) else [[v1,v2,d]]
    T[v2] = T[v2] + [[v2,v1,d]] if(T[v2]) else [[v2,v1,d]]

q,k = list(map(int, input().split()))

depth = [0]*(n+1)
visit = [False]*(n+1)
depth[k] = 0
visit[k] = True
stack = T[k]
while stack:
    p,c,d = stack.pop()
    if visit[c]: continue
    visit[c] = True
    depth[c] = depth[p] + d
    for pp,cc,dd in T[c]:
        if not visit[cc]: stack.append([pp,cc,dd])

for _ in range(q):
    v1,v2 = list(map(int, input().split()))
    print((depth[v1]+depth[v2]))

# Optimized solution
## abc070d: Transit Tree Path
 
n = int(eval(input()))
 
T = [None]*(n+1)
for _ in range(n-1):
    v1,v2,d = list(map(int, input().split()))
    if(T[v1] is None): T[v1] = [[v1,v2,d]]
    else: T[v1].append([v1,v2,d])
    if(T[v2] is None): T[v2] = [[v2,v1,d]]
    else: T[v2].append([v2,v1,d])

q,k = list(map(int, input().split()))

depth = [0]*(n+1)
visit = [False]*(n+1)
depth[k] = 0
visit[k] = True
stack = T[k]
while stack:
    p,c,d = stack.pop()
    if visit[c]: continue
    visit[c] = True
    depth[c] = depth[p] + d
    for pp,cc,dd in T[c]:
        if not visit[cc]: stack.append([pp,cc,dd])
 
for _ in range(q):
    v1,v2 = list(map(int, input().split()))
    print((depth[v1]+depth[v2]))
# Original solution
## abc070d: Transit Tree Path

from collections import deque

n = int(eval(input()))

T = [{} for _ in range(n)]
for _ in range(n-1):
    v1,v2,d = list(map(int, input().split()))
    T[v1-1][v2-1] = d
    T[v2-1][v1-1] = d

q,k = list(map(int, input().split()))

visit = [False]*n
depth = [0]*n
que = deque([[-1,k-1,0]])
while que:
    p,c,d = que.popleft()
    if visit[c]: continue
    visit[c] = True
    depth[c] = d
    if p>=0: depth[c] += depth[p]
    for cc in list(T[c].keys()):
        if not visit[cc]: que.append([c,cc,T[c][cc]])

for _ in range(q):
    v1,v2 = list(map(int, input().split()))
    print((depth[v1-1]+depth[v2-1]))

# Optimized solution
## abc070d: Transit Tree Path

from collections import deque

n = int(eval(input()))

T = [{} for _ in range(n)]
for _ in range(n-1):
    v1,v2,d = list(map(int, input().split()))
    T[v1-1][v2-1] = d
    T[v2-1][v1-1] = d

q,k = list(map(int, input().split()))

visit = [False]*n
depth = [0]*n
que = deque()
que.append(k-1)
while que:
    p = que.popleft()
    visit[p] = True
    for c in list(T[p].keys()):
        if visit[c]: continue
        depth[c] = depth[p] + T[p][c]
        que.append(c)

for _ in range(q):
    v1,v2 = list(map(int, input().split()))
    print((depth[v1-1]+depth[v2-1]))
# Original solution
import heapq

INF = 10**15
n = int(eval(input()))
dist_mat = [[INF]*n for _ in range(n)]
edges = [[] for _ in range(n)]
for i in range(n-1):
    a, b, c = list(map(int,input().split()))
    a -= 1
    b -= 1
    dist_mat[a][b] = c
    dist_mat[b][a] = c
    edges[a].append(b)
    edges[b].append(a)
    dist_mat[i][i] = 0

q, k = list(map(int, input().split()))
k -= 1

que = [(0, k)]
heapq.heapify(que)
costs = [INF]*n
costs[k] = 0
while len(que) > 0:
    dist, current = heapq.heappop(que)
    for edge in edges[current]:
        if costs[edge] > costs[current] + dist_mat[current][edge]:
            costs[edge] = costs[current] + dist_mat[current][edge]
            heapq.heappush(que, (costs[edge], edge))

res = []
for _ in range(q):
    x, y = list(map(int, input().split()))
    x -= 1
    y -= 1
    res.append(costs[x]+costs[y])

for ans in res:
    print(ans)


# Optimized solution
import sys
sys.setrecursionlimit(10**9)

n = int(eval(input()))
edges = [[] for _ in range(n)]
costs = [-1]*n
def dfs(current):

    for nex, cost in edges[current]:
        if costs[nex] != -1:
            continue
        costs[nex] = costs[current] + cost
        dfs(nex)

for i in range(n-1):
    a, b, c = list(map(int,input().split()))
    a -= 1
    b -= 1
    edges[a].append((b, c))
    edges[b].append((a, c))

q, k = list(map(int, input().split()))
k -= 1
costs[k] = 0
dfs(k)

res = []
for _ in range(q):
    x, y = list(map(int, input().split()))
    x -= 1
    y -= 1
    res.append(costs[x]+costs[y])

for ans in res:
    print(ans)
# Original solution
from collections import deque
n=int(eval(input()))
tree=[[] for _ in range(n)]
for i in range(n-1):
  a,b,c=list(map(int,input().split()))
  tree[a-1].append([b-1,c])
  tree[b-1].append([a-1,c])
q,k=list(map(int,input().split()))
dis=[-1 for i in range(n)]
dis[k-1]=0
que=deque()
que.append(k-1)
while que:
  x=que.popleft()
  for i,c in tree[x]:
    if dis[i]==-1:
      que.append(i)
      dis[i]=dis[x]+c
for i in range(q):
  x,y=list(map(int,input().split()))
  print((dis[x-1]+dis[y-1]))

# Optimized solution
n=int(eval(input()))
g=[[] for i in range(n)]
for _ in range(n-1):
  u,v,c=list(map(int,input().split()))
  g[u-1].append((v-1,c))
  g[v-1].append((u-1,c))
Q,k=list(map(int,input().split()))
d=[-1]*n
d[k-1]=0
q=[k-1]
while q:
  t=q.pop()
  for x,cost in g[t]:
    if d[x]==-1:
      d[x]=d[t]+cost
      q.append(x)
for _ in range(Q):
  x,y=list(map(int,input().split()))
  print((d[x-1]+d[y-1]))
# Original solution
import queue

n = int(eval(input()))
my_dict = dict()
mtx = [[0] * n for _ in range(n)]

for i in range(n-1):
    a, b, c = list(map(int, input().split()))
    a -= 1
    b -= 1
    if a in my_dict:
        my_dict[a].append(b)
    else:
        my_dict[a] = [b]

    if b in my_dict:
        my_dict[b].append(a)
    else:
        my_dict[b] = [a]
    mtx[a][b] = c
    mtx[b][a] = c

qu, k = list(map(int, input().split()))

ret_dict = dict()
visited = set()

q = queue.Queue()
q.put((k-1, 0))
visited.add(k-1)

while not q.empty():
    ptr, length = q.get()
    if ptr in my_dict:
        for item in my_dict[ptr]:
            if item not in visited:
                q.put((item, length + mtx[ptr][item]))
                ret_dict[item] = length + mtx[ptr][item]
                visited.add(item)

for i in range(qu):
    x, y = list(map(int, input().split()))
    x -= 1
    y -= 1
    print((ret_dict[x] + ret_dict[y]))


# Optimized solution
import queue

n = int(eval(input()))
my_dict = dict()
mtx = dict()

for i in range(n-1):
    a, b, c = list(map(int, input().split()))
    a -= 1
    b -= 1
    if a in my_dict:
        my_dict[a].append(b)
    else:
        my_dict[a] = [b]

    if b in my_dict:
        my_dict[b].append(a)
    else:
        my_dict[b] = [a]
    mtx[(a, b)] = c
    mtx[(b, a)] = c

qu, k = list(map(int, input().split()))

ret_dict = dict()
visited = set()

q = queue.Queue()
q.put((k-1, 0))
visited.add(k-1)

while not q.empty():
    ptr, length = q.get()
    if ptr in my_dict:
        for item in my_dict[ptr]:
            if item not in visited:
                q.put((item, length + mtx[(ptr, item)]))
                ret_dict[item] = length + mtx[(ptr, item)]
                visited.add(item)

for i in range(qu):
    x, y = list(map(int, input().split()))
    x -= 1
    y -= 1
    print((ret_dict[x] + ret_dict[y]))
# Original solution
n = int(eval(input()))
edge = []
for _ in range(n-1):
    a,b,c = list(map(int,input().split()))
    edge.append([a-1,b-1,c])
q, k = list(map(int,input().split()))
querry = []
for _ in range(q):
    x,y = list(map(int,input().split()))
    querry.append([x-1,y-1])
    

class Graph():
    def __init__(self,n,edge):
        self.n = n
        self.graph = [[0]*n for _ in range(n)]
        self.edge = edge
        self.ed_list = [[] for _ in range(n)]
        for a,b,c in edge:
            self.ed_list[a].append([b,c])
            self.ed_list[b].append([a,c])
    
    def dijkstra(self,start):
        INF = float('inf')
        cost = [INF]*self.n
        cost[start] = 0
        from heapq import heappop,heappush
        stack = [[0, start]]
        while stack:
            c, x = heappop(stack)
            for to,j in self.ed_list[x]:
                if cost[to] > j + c:
                    cost[to] = j + c
                    heappush(stack,[j + c, to])
        return cost

gp = Graph(n,edge)
cost = gp.dijkstra(k-1)
for i,j in querry:
    print((cost[i] + cost[j]))



# Optimized solution
n = int(eval(input()))
edge = []
for _ in range(n-1):
    a,b,c = list(map(int,input().split()))
    edge.append([a-1,b-1,c])
q, k = list(map(int,input().split()))
querry = []
for _ in range(q):
    x,y = list(map(int,input().split()))
    querry.append([x-1,y-1])
    

class Graph():
    def __init__(self,n,edge):
        self.n = n
        self.edge = edge
        self.ed_list = [[] for _ in range(n)]
        for a,b,c in edge:
            self.ed_list[a].append([b,c])
            self.ed_list[b].append([a,c])
    
    def dijkstra(self,start):
        INF = float('inf')
        cost = [INF]*self.n
        cost[start] = 0
        from heapq import heappop,heappush
        stack = [[0, start]]
        while stack:
            c, x = heappop(stack)
            for to,j in self.ed_list[x]:
                if cost[to] > j + c:
                    cost[to] = j + c
                    heappush(stack,[j + c, to])
        return cost

gp = Graph(n,edge)
cost = gp.dijkstra(k-1)
for i,j in querry:
    print((cost[i] + cost[j]))
# Original solution
N = int(eval(input()))
edge = [[] for i in range(N)]
for i in range(N-1):
    a, b, c = list(map(int, input().split()))
    edge[a-1].append([c, b-1])
    edge[b-1].append([c, a-1])
Q, K = list(map(int, input().split()))
query = [list(map(int, input().split())) for _ in range(Q)]

import heapq

def dijkstra_heap(s):
    d = [float('inf')] * N
    used = [False] * N
    d[s] = 0
    used[s] = True
    edgelist = []
    for e in edge[s]:
        heapq.heappush(edgelist, e)
    while len(edgelist):
        min_edge = heapq.heappop(edgelist)
        if used[min_edge[1]]:
            continue
        v = min_edge[1]
        d[v] = min_edge[0]
        used[v] = True
        for e in edge[v]:
            heapq.heappush(edgelist, [e[0] + d[v], e[1]])
    return d

for q in query:
    x, y = q[0], q[1]
    x -= 1
    y -= 1
    dist = dijkstra_heap(K-1)
    print((dist[x] + dist[y]))

# Optimized solution
N = int(eval(input()))
edge = [[] for i in range(N)]
for i in range(N-1):
    a, b, c = list(map(int, input().split()))
    edge[a-1].append([c, b-1])
    edge[b-1].append([c, a-1])
Q, K = list(map(int, input().split()))
query = [list(map(int, input().split())) for _ in range(Q)]

import heapq

def dijkstra_heap(s):
    d = [float('inf')] * N
    used = [False] * N
    d[s] = 0
    used[s] = True
    edgelist = []
    for e in edge[s]:
        heapq.heappush(edgelist, e)
    while len(edgelist):
        min_edge = heapq.heappop(edgelist)
        if used[min_edge[1]]:
            continue
        v = min_edge[1]
        d[v] = min_edge[0]
        used[v] = True
        for e in edge[v]:
            heapq.heappush(edgelist, [e[0] + d[v], e[1]])
    return d

dist = dijkstra_heap(K-1)
for q in query:
    x, y = q[0], q[1]
    x -= 1
    y -= 1
    print((dist[x] + dist[y]))
# Original solution
from collections import deque
def dfs(neigh,K):
    q = deque()
    q.append(K)
    visited = [False]*N
    d = [0]*N
    visited[K] = True
    while len(q)>0:
        p = q.pop()
        for i in range(len(neigh[p])):
            if neigh[p][i]==0 or visited[i]:
                continue
            d[i] = d[p]+neigh[p][i]
            visited[i] = True
            q.append(i)
    return d

N = int(eval(input()))
neigh = [[0 for i in range(N)] for i in range(N)]
for i in range(N-1):
    a,b,c = list(map(int,input().split()))
    neigh[a-1][b-1] = c
    neigh[b-1][a-1] = c
Q,K = list(map(int,input().split()))
d = dfs(neigh,K-1)
for i in range(Q):
    x,y = list(map(int,input().split()))
    print((d[x-1]+d[y-1]))


# Optimized solution
from collections import deque
def dfs(neigh,K):
    q = deque([K])
    d = [0]*N
    visited = [0]*N
    visited[K] = True
    while q:
        p = q.pop()
        for di,cost in neigh[p]:
            if visited[di]:
                continue
            d[di] = d[p]+cost
            visited[di] = True
            q.append(di)
    return d

N = int(eval(input()))
neigh = [[] for i in range(N)]
for i in range(N-1):
    a,b,c = list(map(int,input().split()))
    neigh[a-1].append((b-1,c))
    neigh[b-1].append((a-1,c))

Q,K = list(map(int,input().split()))
d = dfs(neigh,K-1)
for i in range(Q):
    x,y = list(map(int,input().split()))
    print((d[x-1]+d[y-1]))
# Original solution
import heapq
def dijkstra(V, edges, s):
    inf=float('inf')

    dist=[inf]*V
    dist[s]=0
    
    que=[[0,s]]
    heapq.heapify(que) #最小値を管理 [dist[i],i]となる要素

    while  que:
        p=heapq.heappop(que) #距離が最小のものを取り出す
        dis=p[0]
        v=p[1]
        if dist[v] <dis:
            continue
        for edge in edges[v]:
            to_v=edge[0]
            cost=edge[1]
        
            if dist[to_v] > dist[v] + cost:
                dist[to_v]=dist[v]+cost
                heapq.heappush(que,[dist[to_v], to_v])
    
    return dist

def main():
    n=int(eval(input()))
    edges=[[] for _ in range(n)]
    for i in range(n-1):
        a,b,c=list(map(int,input().split()))
        a-=1
        b-=1
        edges[a].append([b,c])
        edges[b].append([a,c])
    q,k=list(map(int,input().split()))
    k-=1
   
    dist=dijkstra(n,edges,k)
    for i in range(q):
        x,y=list(map(int,input().split()))
        x-=1
        y-=1
        print((dist[x]+dist[y]))
if __name__=='__main__':
    main()

# Optimized solution
import heapq
def dijkstra(V, edges, s):
    inf=float('inf')

    dist=[inf]*V
    dist[s]=0
    
    que=[[0,s]]
    heapq.heapify(que) #最小値を管理 [dist[i],i]となる要素

    while  que:
        p=heapq.heappop(que) #距離が最小のものを取り出す
        dis=p[0]
        v=p[1]
        if dist[v] <dis:
            continue
        for edge in edges[v]:
            to_v=edge[0]
            cost=edge[1]
        
            if dist[to_v] > dist[v] + cost:
                dist[to_v]=dist[v]+cost
                heapq.heappush(que,[dist[to_v], to_v])
    
    return dist

def main():
    import sys
    input=sys.stdin.readline
    n=int(eval(input()))
    edges=[[] for _ in range(n)]
    for i in range(n-1):
        a,b,c=list(map(int,input().split()))
        a-=1
        b-=1
        edges[a].append([b,c])
        edges[b].append([a,c])
    q,k=list(map(int,input().split()))
    k-=1
   
    dist=dijkstra(n,edges,k)
    for i in range(q):
        x,y=list(map(int,input().split()))
        x-=1
        y-=1
        print((dist[x]+dist[y]))
if __name__=='__main__':
    main()
# Original solution
import heapq
def dijkstra(V, edges, s):
    inf=float('inf')

    dist=[inf]*V
    dist[s]=0
    
    que=[[0,s]]
    heapq.heapify(que) #最小値を管理 [dist[i],i]となる要素

    while  que:
        p=heapq.heappop(que) #距離が最小のものを取り出す
        dis=p[0]
        v=p[1]
        if dist[v] <dis:
            continue
        for edge in edges[v]:
            to_v=edge[0]
            cost=edge[1]
        
            if dist[to_v] > dist[v] + cost:
                dist[to_v]=dist[v]+cost
                heapq.heappush(que,[dist[to_v], to_v])
    
    return dist

def main():
    import sys
    input=sys.stdin.readline
    n=int(eval(input()))
    edges=[[] for _ in range(n)]
    for i in range(n-1):
        a,b,c=list(map(int,input().split()))
        a-=1
        b-=1
        edges[a].append([b,c])
        edges[b].append([a,c])
    q,k=list(map(int,input().split()))
    k-=1
   
    dist=dijkstra(n,edges,k)
    for i in range(q):
        x,y=list(map(int,input().split()))
        x-=1
        y-=1
        print((dist[x]+dist[y]))
if __name__=='__main__':
    main()

# Optimized solution
def main():
    import sys
    input=sys.stdin.readline
    sys.setrecursionlimit(1000000)
    n=int(eval(input()))
    edges=[[] for _ in range(n)]
    for i in range(n-1):
        a,b,c=list(map(int,input().split()))
        a-=1
        b-=1
        edges[a].append([b,c])
        edges[b].append([a,c])
        
    q,k=list(map(int,input().split()))
    k-=1
    
    dist=[10**9+1]*n
    dist[k]=0
    
    def dfs(s,before=-1):
       for edge in edges[s]:
           to=edge[0]
           c=edge[1]
           if to!=before:
               dist[to]=dist[s]+c
               dfs(to,s) 
              
    dfs(k)
    for i in range(q):
        x,y=list(map(int,input().split()))
        x-=1
        y-=1
        print((dist[x]+dist[y]))
        
if __name__=='__main__':
    main()
# Original solution
import queue
N=int(eval(input()))
G=[dict() for i in range(N)]
for i in range(N-1):
    a,b,c=list(map(int,input().split()))
    G[a-1][b-1]=c
    G[b-1][a-1]=c
Q,K=list(map(int,input().split()));K-=1
x=[0 for i in range(Q)]
y=[0 for i in range(Q)]
for i in range(Q):
    x[i],y[i]=list(map(int,input().split()))
    x[i]-=1
    y[i]-=1
q=queue.Queue()
q.put(K)
dist=[-1 for i in range(N)]
dist[K]=0
while(not(q.empty())):
    r=q.get()
    for p in G[r]:
        if dist[p]!=-1:
            continue
        dist[p]=dist[r]+G[p][r]
        q.put(p)
for i in range(Q):
    print((dist[x[i]]+dist[y[i]]))


# Optimized solution
import queue
N=int(eval(input()))
G=[dict() for i in range(N)]
for i in range(N-1):
    a,b,c=list(map(int,input().split()))
    G[a-1][b-1]=c
    G[b-1][a-1]=c
Q,K=list(map(int,input().split()));K-=1
x=[0 for i in range(Q)]
y=[0 for i in range(Q)]
for i in range(Q):
    x[i],y[i]=list(map(int,input().split()))
    x[i]-=1
    y[i]-=1
q=[]
q.append(K)
dist=[-1 for i in range(N)]
dist[K]=0
while(len(q)>0):
    r=q[-1]
    q.pop()
    for p in G[r]:
        if dist[p]!=-1:
            continue
        dist[p]=dist[r]+G[p][r]
        q.append(p)
for i in range(Q):
    print((dist[x[i]]+dist[y[i]]))