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()
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()
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()

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()

-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 - - +    sys.setrecursionlimit(1000000) -    dist = dijkstra(n, edges, k) +    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)