n=int(eval(input()))

l=[[]for _ in range(n)]

for i in range(n-1):

  a,b,c=list(map(int,input().split()))

  l[a-1]+=[[b-1,c]]

  l[b-1]+=[[a-1,c]]

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

k-=1

Q=[[k,0]]

c=[0]*n;c[k]=-1

while Q:

  p=[]

  for i,j in Q:

    for x,y in l[i]:

      if c[x]==0:

        c[x]=j+y

        p+=[[x,c[x]]]

  Q=p

c[k]=0

for i in range(q):

  x,y=list(map(int,input().split()))

  print((c[x-1]+c[y-1]))
from heapq import heappop,heappush

def dijkstra(s,n,edge):

  inf=10**20

  ans=[inf]*n

  ans[s]=0

  h=[[0,s]]

  while h:

    c,v=heappop(h)

    if ans[v]<c:continue

    for u,t in edge[v]:

      if c+t<ans[u]:

        ans[u]=c+t

        heappush(h,[c+t,u])

  return ans

n=int(eval(input()))

edge=[[]for _ in range(n)]

for i in range(n-1):

  a,b,c=list(map(int,input().split()))

  a-=1;b-=1

  edge[a].append([b,c])

  edge[b].append([a,c])

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

k-=1

xy=[list(map(int,input().split()))for _ in range(q)]

for i in range(q):xy[i][0]-=1;xy[i][1]-=1

edge=dijkstra(k,n,edge)

for x,y in xy:print((edge[x]+edge[y]))
n = int(eval(input()))
l = [[] for _ in range(n)]
for i in range(n - 1):
    a, b, c = list(map(int, input().split()))
    l[a - 1] += [[b - 1, c]]
    l[b - 1] += [[a - 1, c]]
q, k = list(map(int, input().split()))
k -= 1
Q = [[k, 0]]
c = [0] * n
c[k] = -1
while Q:
    p = []
    for i, j in Q:
        for x, y in l[i]:
            if c[x] == 0:
                c[x] = j + y
                p += [[x, c[x]]]
    Q = p
c[k] = 0
for i in range(q):
    x, y = list(map(int, input().split()))
    print((c[x - 1] + c[y - 1]))

from heapq import heappop, heappush


def dijkstra(s, n, edge):
    inf = 10**20
    ans = [inf] * n
    ans[s] = 0
    h = [[0, s]]
    while h:
        c, v = heappop(h)
        if ans[v] < c:
            continue
        for u, t in edge[v]:
            if c + t < ans[u]:
                ans[u] = c + t
                heappush(h, [c + t, u])
    return ans


n = int(eval(input()))
edge = [[] for _ in range(n)]
for i in range(n - 1):
    a, b, c = list(map(int, input().split()))
    a -= 1
    b -= 1
    edge[a].append([b, c])
    edge[b].append([a, c])
q, k = list(map(int, input().split()))
k -= 1
xy = [list(map(int, input().split())) for _ in range(q)]
for i in range(q):
    xy[i][0] -= 1
    xy[i][1] -= 1
edge = dijkstra(k, n, edge)
for x, y in xy:
    print((edge[x] + edge[y]))

+from heapq import heappop, heappush + + +def dijkstra(s, n, edge): +    inf = 10**20 +    ans = [inf] * n +    ans[s] = 0 +    h = [[0, s]] +    while h: +        c, v = heappop(h) +        if ans[v] < c: +            continue +        for u, t in edge[v]: +            if c + t < ans[u]: +                ans[u] = c + t +                heappush(h, [c + t, u]) +    return ans + + -l = [[] for _ in range(n)] +edge = [[] for _ in range(n)] -    l[a - 1] += [[b - 1, c]] -    l[b - 1] += [[a - 1, c]] +    a -= 1 +    b -= 1 +    edge[a].append([b, c]) +    edge[b].append([a, c]) -Q = [[k, 0]] -c = [0] * n -c[k] = -1 -while Q: -    p = [] -    for i, j in Q: -        for x, y in l[i]: -            if c[x] == 0: -                c[x] = j + y -                p += [[x, c[x]]] -    Q = p -c[k] = 0 +xy = [list(map(int, input().split())) for _ in range(q)] -    x, y = list(map(int, input().split())) -    print((c[x - 1] + c[y - 1])) +    xy[i][0] -= 1 +    xy[i][1] -= 1 +edge = dijkstra(k, n, edge) +for x, y in xy: +    print((edge[x] + edge[y]))