from collections import deque

n,*t=list(map(int,open(0).read().split()))

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

dist_f_k=[0]*n



for a,b,c in zip(t[:3*n-3:3],t[1::3],t[2::3]):

	con[a-1].append((b-1,c))

	con[b-1].append((a-1,c))



k=t[3*n-2]

que=deque([k-1])

while que:

	cur=que.popleft()

	for nxt,dd in con[cur]:

		if dist_f_k[nxt]==0 and nxt!=k-1:

			que.append(nxt)

			dist_f_k[nxt]=dist_f_k[cur]+dd



for x,y in zip(t[3*n-1::2],t[3*n::2]):

	print((dist_f_k[x-1]+dist_f_k[y-1]))
def solve():

	from collections import deque

	n,*t=list(map(int,open(0).read().split()))

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

	dist_f_k=[0]*n

	l1,l2=t[:3*n-3],t[3*n-1:]



	for a,b,c in zip(*[iter(l1)]*3):

		con[a-1].append((b-1,c))

		con[b-1].append((a-1,c))



	k=t[3*n-2]

	que=deque([k-1])

	while que:

		cur=que.popleft()

		for nxt,dd in con[cur]:

			if dist_f_k[nxt]==0 and nxt!=k-1:

				que.append(nxt)

				dist_f_k[nxt]=dist_f_k[cur]+dd



	for x,y in zip(*[iter(l2)]*2):

		print((dist_f_k[x-1]+dist_f_k[y-1]))

		

if __name__=="__main__":

	solve()
from collections import deque

n, *t = list(map(int, open(0).read().split()))
con = [[] for _ in range(n)]
dist_f_k = [0] * n
for a, b, c in zip(t[: 3 * n - 3 : 3], t[1::3], t[2::3]):
    con[a - 1].append((b - 1, c))
    con[b - 1].append((a - 1, c))
k = t[3 * n - 2]
que = deque([k - 1])
while que:
    cur = que.popleft()
    for nxt, dd in con[cur]:
        if dist_f_k[nxt] == 0 and nxt != k - 1:
            que.append(nxt)
            dist_f_k[nxt] = dist_f_k[cur] + dd
for x, y in zip(t[3 * n - 1 :: 2], t[3 * n :: 2]):
    print((dist_f_k[x - 1] + dist_f_k[y - 1]))

def solve():
    from collections import deque

    n, *t = list(map(int, open(0).read().split()))
    con = [[] for _ in range(n)]
    dist_f_k = [0] * n
    l1, l2 = t[: 3 * n - 3], t[3 * n - 1 :]
    for a, b, c in zip(*[iter(l1)] * 3):
        con[a - 1].append((b - 1, c))
        con[b - 1].append((a - 1, c))
    k = t[3 * n - 2]
    que = deque([k - 1])
    while que:
        cur = que.popleft()
        for nxt, dd in con[cur]:
            if dist_f_k[nxt] == 0 and nxt != k - 1:
                que.append(nxt)
                dist_f_k[nxt] = dist_f_k[cur] + dd
    for x, y in zip(*[iter(l2)] * 2):
        print((dist_f_k[x - 1] + dist_f_k[y - 1]))


if __name__ == "__main__":
    solve()

-from collections import deque +def solve(): +    from collections import deque -n, *t = list(map(int, open(0).read().split())) -con = [[] for _ in range(n)] -dist_f_k = [0] * n -for a, b, c in zip(t[: 3 * n - 3 : 3], t[1::3], t[2::3]): -    con[a - 1].append((b - 1, c)) -    con[b - 1].append((a - 1, c)) -k = t[3 * n - 2] -que = deque([k - 1]) -while que: -    cur = que.popleft() -    for nxt, dd in con[cur]: -        if dist_f_k[nxt] == 0 and nxt != k - 1: -            que.append(nxt) -            dist_f_k[nxt] = dist_f_k[cur] + dd -for x, y in zip(t[3 * n - 1 :: 2], t[3 * n :: 2]): -    print((dist_f_k[x - 1] + dist_f_k[y - 1])) +    n, *t = list(map(int, open(0).read().split())) +    con = [[] for _ in range(n)] +    dist_f_k = [0] * n +    l1, l2 = t[: 3 * n - 3], t[3 * n - 1 :] +    for a, b, c in zip(*[iter(l1)] * 3): +        con[a - 1].append((b - 1, c)) +        con[b - 1].append((a - 1, c)) +    k = t[3 * n - 2] +    que = deque([k - 1]) +    while que: +        cur = que.popleft() +        for nxt, dd in con[cur]: +            if dist_f_k[nxt] == 0 and nxt != k - 1: +                que.append(nxt) +                dist_f_k[nxt] = dist_f_k[cur] + dd +    for x, y in zip(*[iter(l2)] * 2): +        print((dist_f_k[x - 1] + dist_f_k[y - 1])) + + +if __name__ == "__main__": +    solve()