Harryxun/sft_time huggingface dataset:
### BUGGY CODE ###

import math
from functools import reduce

def gcd(list):
  return reduce(math.gcd, list)

n = int(input())
a = list(map(int, input().split()))

ans = 0
for i in range(n):
  ans = max(ans, gcd(a[:i]+a[i+1:]))

print(ans)

### FIXED CODE ###

from math import *

n = int(input())
a = list(map(int, input().split()))

l = [a[0]]
for i in range(1, n-1):
  l.append(gcd(a[i], l[i-1]))

ans = l[n-2]
r = a[n-1]
for i in range(n-2,0,-1):
  r = gcd(r, a[i+1])
  ans = max(ans, gcd(l[i-1], r))
ans = max(ans, gcd(r,a[1]))
print(ans)
### BUGGY CODE ###

N=int(input())
A=list(map(int,input().strip().split()))

B=[A.index(n)+1 for n in range(1,N+1)]

print(" ".join(list(map(str,B))))

### FIXED CODE ###

N=int(input())
A=list(map(int,input().strip().split()))

B=[-1 for n in range(N)]
for n in range(len(A)):
    B[A[n]-1]=n+1

print(" ".join(list(map(str,B))))
### BUGGY CODE ###


n = int(input())
a = list(map(int,input().split()))
ans = str(a[0])

if n % 2 == 0:

    for i in range(n-1):

        i+=1

        if i % 2 == 1:

            ans = str(a[i])+" "+ans

        else:

            ans = ans+" "+ str(a[i])

else:

    for i in range(n-1):

        i+=1

        if i % 2 == 0:

            ans = str(a[i])+" "+ans

        else:

            ans = ans+" "+ str(a[i])

print(ans)


### FIXED CODE ###


n = int(input())
a = list(map(int,input().split()))
ans = ""

lis = [a[0]] + [0] * (n-1)
fo = 1
be = -1

if n % 2 == 0:

    for i in range(n-1):

        i+=1

        if i % 2 == 1:

            lis[be] = a[i]
            be -= 1
            #ans = str(a[i])+" "+ans

        else:

            lis[fo] = a[i]
            fo += 1
            #ans = ans+" "+ str(a[i])


else:

    for i in range(n-1):

        i+=1

        if i % 2 == 1:
            
            lis[fo] = a[i]
            fo += 1
            #ans = str(a[i])+" "+ans

        else:
            
            lis[be] = a[i]
            be -= 1
            #ans = ans+" "+ str(a[i])

            
for i in range(n):

        i -= n//2

        ans += str(lis[i]) + " "


#print(lis)
print(ans)
### BUGGY CODE ###

h,w = map(int,input().split(" "))
s = [[0 for i in range(w)] for j in range(h)]
for i in range(h):
    tmp = list(input())
    for j in range(w):
        s[i][j] = tmp[j]
dp1 = [[-1 for i in range(w)] for j in range(h)]
dp2 = [[-1 for i in range(w)] for j in range(h)]
dp3 = [[-1 for i in range(w)] for j in range(h)]
dp4 = [[-1 for i in range(w)] for j in range(h)]



for i in range(h):
    for j in range(w-1,-1,-1):
        if s[i][j] == "#":
            dp2[i][j] = 0
        elif j == w-1:
            dp2[i][j] = 1
        else:
            dp2[i][j] = 1 + dp2[i][j+1]

for i in range(w):
    for j in range(h):
        if s[j][i] == "#":
            dp3[j][i] = 0
        elif j == 0:
            dp3[j][i] = 1
        else:
            dp3[j][i] = 1 + dp3[j-1][i]

for i in range(w):
    for j in range(h-1,-1,-1):
        if s[j][i] == "#":
            dp4[j][i] = 0
        elif j == h-1:
            dp4[j][i] = 1
        else:
            dp4[j][i] = 1 + dp4[j+1][i]

a = 1
for i in range(h):
    for j in range(w):
        if s[i][j] == "#":
            dp1[i][j] = 0
        elif j == 0:
            dp1[i][j] = 1
        else:
            dp1[i][j] = 1 + dp1[i][j-1]
        a = max(a,dp1[i][j]+dp2[i][j]+dp3[i][j]+dp4[i][j]-3)

print(a)

### FIXED CODE ###

h,w = map(int,input().split(" "))
s = [[0 for i in range(w)] for j in range(h)]
for i in range(h):
    tmp = list(input())
    for j in range(w):
        s[i][j] = tmp[j]

dp1= []
dp2= [[-1 for i in range(w)] for j in range(h)]
dp3=[]
dp4=[[-1 for i in range(w)] for j in range(h)]


for i in range(h):
    dp1.append([])
    dp3.append([])
    for j in range(w):
        dp1[i].append(-1)
        dp3[i].append(-1)

        if s[i][j] == "#":
            dp1[i][j] = 0
        elif j == 0:
            dp1[i][j] = 1
        else:
            dp1[i][j] = 1 + dp1[i][j-1]

        if s[i][w-j-1] == "#":
            dp2[i][w-j-1] = 0
        elif w-j-1 == w-1:
            dp2[i][w-j-1] = 1
        else:
            dp2[i][w-j-1] = 1 + dp2[i][w-j]

        if s[i][j] == "#":
            dp3[i][j] = 0
        elif i == 0:
            dp3[i][j] = 1
        else:
            dp3[i][j] = 1 + dp3[i-1][j]

        if s[h-i-1][j] == "#":
            dp4[h-i-1][j] = 0
        elif h-i-1 == h-1:
            dp4[h-i-1][j] = 1
        else:
            dp4[h-i-1][j] = 1 + dp4[h-i][j]



        

a = 1
for i in range(h):
    for j in range(w):
        a = max(a,dp1[i][j]+dp2[i][j]+dp3[i][j]+dp4[i][j]-3)


print(a)
### BUGGY CODE ###

import sys
sys.setrecursionlimit(2 *(10**5) + 1)
def main():
    N, Q = map(int, input().split())
    d = {}
    for _ in range(N-1):
        a, b = map(int, input().split())
        if a in d:
            d[a].append(b)
        else:
            d[a] = [b]
        if b in d:
            d[b].append(a)
        else:
            d[b] = [a]
    dc = {}
    for _ in range(Q):
        p, x = map(int, input().split())
        if p in dc:
            dc[p] += x
        else:
            dc[p] = x
    v = set()
    l = [0] * N
    def dfs(n, p):
        v.add(n)
        if n in dc:
            p += dc[n]
        for i in d[n]:
            if i not in v:
                dfs(i, p)
        l[n-1] = p
    dfs(1, 0)
    a = ''
    for i in l:
        a += str(i) + ' '
    return a[:-1]

print(main())


### FIXED CODE ###

def main():
    N, Q = map(int, input().split())
    d = {}
    for _ in range(N-1):
        a, b = map(int, input().split())
        if a in d:
            d[a].append(b)
        else:
            d[a] = [b]
        if b in d:
            d[b].append(a)
        else:
            d[b] = [a]
    dc = {}
    for _ in range(Q):
        p, x = map(int, input().split())
        if p in dc:
            dc[p] += x
        else:
            dc[p] = x
    v = set()
    vr = set(range(1,N+1))
    l = [0] * N
    n = [(1,0)]
    c = []
    while v != vr:
        for i, j in n:
            v.add(i)
            if i in dc:
                j += dc[i]
            l[i-1] = j
            for k in d[i]:
                if k not in v:
                    c.append((k, j))
        n = c
        c = []
    return ' '.join(str(i) for i in l)

print(main())
### BUGGY CODE ###

N, D, A=map(int, input().split())
XH=[list(map(int, input().split())) for _ in range(N)]
XH.sort(key=lambda x:x[0])
bomb_j=[0 for _ in range(N)]

for i in range(N):
  X=XH[i][0]
  left, right=0, N-1
  while left<=right:
    mid=(left+right)//2
    if XH[mid][0]>X+2*D:
      right=mid-1
    else:
      left=mid+1
  bomb_j[i]=left-1

ans, pos=0, 0
Hs=[H for X, H in XH]
while pos<N:
  attack=(Hs[pos]-1)//A+1
  start, end=pos, bomb_j[pos]+1
  for i in range(start, end):
    Hs[i]=max(0, Hs[i]-attack*A)
  while pos<N and Hs[pos]==0:
    pos+=1
  ans+=attack
print(ans)

### FIXED CODE ###

from collections import deque

N, D, A=map(int, input().split())
XH=[list(map(int, input().split())) for _ in range(N)]
XH.sort(key=lambda x:x[0])
total=0
q=deque()
ans=0

for i in range(N):
  X, H=XH[i]
  while len(q) and q[0][0]<X:
    total-=q.popleft()[1]
  H-=total
  if H>0:
    attack_cnt=(H-1)//A+1
    ans+=attack_cnt
    damage=attack_cnt*A
    total+=damage
    q.append([X+2*D, damage])
print(ans)
### BUGGY CODE ###

r,g,b,n=map(int,input().split())
ans=chk=0
for i in range(n+1):
    for j in range(n+1):
        if (i*r+j*g)>n: break
        if (n-(i*r+j*g))%b==0:
            ans+=1
print(ans)

### FIXED CODE ###

r,g,b,n=map(int,input().split())
ans=chk=0
for i in range(n+1):
    for j in range(n+1):
        if (i*r+j*g)>n: break
        if (n-(i*r+j*g))%b==0:
            ans+=1
print(ans)
### BUGGY CODE ###

import sys
from collections import deque, Counter, defaultdict
from fractions import gcd
input = sys.stdin.readline

def eprint(s):
    if type(s) != type('str'):
        s=str(s)

    sys.stderr.write(s+'\n')
    return

def main():
    l, r = map(int, input().split())
    if l <= 2019 and 2019 <= r:
        print(0)
        return 
    if l-r >= 2019:
        print(0)
        return

    m = 10**9

    for i in range(l, r):
        for j in range(i+1,r+1):
            m = min(m, i*j % 2019)
    print(m)
    return

if __name__ == '__main__':
    main()

### FIXED CODE ###

import sys
from collections import deque, Counter, defaultdict
from fractions import gcd
input = sys.stdin.readline

def eprint(s):
    if type(s) != type('str'):
        s=str(s)

    sys.stderr.write(s+'\n')
    return

def main():
    l, r = map(int, input().split())
    if l <= 2019 and 2019 <= r:
        print(0)
        return 
    if r-l >= 2019:
        print(0)
        return

    m = 10**9

    for i in range(l, r):
        for j in range(i+1,r+1):
            m = min(m, i*j % 2019)
    print(m)
    return

if __name__ == '__main__':
    main()
### BUGGY CODE ###

import sys
def Is(): return [int(x) for x in sys.stdin.readline().split()]

N,M,Q = Is()
lis = [[0]*(N+1) for _ in range(N+1)]
for i in range(M):
    l,r = Is()
    lis[l][r] += 1

dp = [[0]*(N+1) for _ in range(N+1)]
dp2 = [[0]*(N+1) for _ in range(N+1)]
for _ in range(Q): 
    p,q = Is()
    if dp2[p][q]:
        print(dp2[p][q])
        continue
    t = 0
    for i in range(p,q+1):
        if dp[i][q]:
            t += dp[i][q]
        else:
            dp[i][q] = sum(lis[i][i:q+1])
            t += dp[i][q]
    dp2[p][q] = t
    print(t)
    

### FIXED CODE ###

import sys
def Is(): return [int(x) for x in sys.stdin.readline().split()]

N,M,Q = Is()
lis = [[0]*(N+1) for _ in range(N+1)]
for i in range(M):
    l,r = Is()
    lis[l][r] += 1

cs = [[0]*(N+1) for _ in range(N+1)]
for i in range(1,N+1):
    for j in range(1,N+1):
        cs[i][j] = cs[i][j-1] + sum(lis[i][:j+1])

for _ in range(Q): 
    p,q = Is()
    t = 0
    for i in range(p,q+1):
        t += cs[i][q] - cs[i][q-1]
    print(t)
### BUGGY CODE ###

n = int(input())
t = []
a = []

for i in range(0, n):
    ti, ai = [int(_) for _ in input().split()]
    t.append(ti)
    a.append(ai)

newest_t = t[0]
newest_a = a[0]

for i in range(0, n):
    tmin = t[i]
    amin = a[i]
    while True:
        if tmin >= newest_t and amin >= newest_a:
            newest_t = tmin
            newest_a = amin
            break
        else:
            tmin += t[i]
            amin += a[i]

print(newest_t + newest_a)


### FIXED CODE ###

n = int(input())
t = []
a = []

for i in range(0, n):
    ti, ai = [int(_) for _ in input().split()]
    t.append(ti)
    a.append(ai)

newest_t = t[0]
newest_a = a[0]

for i in range(0, n):
    div_t = newest_t // t[i]
    div_a = newest_a // a[i]

    for j in range(max(div_t, div_a)-1, max(div_t, div_a)+2):
        if j * t[i] >= newest_t and j * a[i] >= newest_a:
            newest_t = j * t[i]
            newest_a = j * a[i]
            break

print(newest_t + newest_a)
### BUGGY CODE ###

x, y, z, k = map(int, input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
c=list(map(int,input().split()))

#a.sort(reverse=True)
##sb.sort(reverse=True)
#c.sort(reverse=True)            

ab=[]

for i in a:
    for j in b:
        ab.append(i+j)
ab.sort(reverse=True)

abc=[]
for i in ab[:k]:
    for j in c:
        abc.append(i+j)
abc.sort(reverse=True)

for i in abc[:k]:
    print(i)

### FIXED CODE ###

x, y, z, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))

a.sort(reverse=True)
b.sort(reverse=True)
c.sort(reverse=True)

ab = sorted(x+y for x in a for y in b)[:-k-1:-1]
abc = sorted(x+y for x in ab[:k] for y in c)[:-k-1:-1]
print(*abc, sep='\n')
### BUGGY CODE ###

def main():
    mod = 1000000007
    S = input()
    n = len(S)

    # dp[i][j] 左からi文字目までの数で, 余りがjになるものの総数
    dp = [[0] * 13 for i in range(n)]

    s = S[0]
    if s == "?":
        for i in range(10):
            dp[0][i] = 1
    else:
        dp[0][int(s)] = 1

    for i in range(1,n):
        s = S[i]
        if s == "?":
            for rem in range(10):
                for pre in range(13):
                    dp[i][(10*pre + rem) % 13] += dp[i-1][pre]
                    dp[i][(10*pre + rem) % 13] %= mod

        else:
            rem = int(s)
            for pre in range(13):
                    dp[i][(10*pre + rem) % 13] += dp[i-1][pre]
                    dp[i][(10*pre + rem) % 13] %= mod

    res = dp[n-1][5]
    print(res)


if __name__ == "__main__":
    main()

### FIXED CODE ###

def main():
    mod = 1000000007
    S = input()
    n = len(S)

    # dp[i][j] 左からi文字目までの数で, 余りがjになるものの総数
    dp = [[0] * 13 for i in range(n)]

    s = S[0]
    if s == "?":
        for i in range(10):
            dp[0][i] = 1
    else:
        dp[0][int(s)] = 1

    for i in range(1,n):
        s = S[i]
        if s == "?":
            for rem in range(10):
                for pre in range(13):
                    dp[i][(10*pre + rem) % 13] += dp[i-1][pre]
                    dp[i][(10*pre + rem) % 13] %= mod

        else:
            rem = int(s)
            for pre in range(13):
                    dp[i][(10*pre + rem) % 13] += dp[i-1][pre]
                    dp[i][(10*pre + rem) % 13] %= mod

    res = dp[n-1][5]
    print(res)


if __name__ == "__main__":
    main()
### BUGGY CODE ###

n=int(input())
a=list(map(int,input().split()))
M=10**9+7
s=0
for i in range(n-1):
  for j in range(i+1,n):
    s+=a[i]^a[j]
print(s%M)

### FIXED CODE ###

N=int(input())
A=list(map(int,input().split()))
M=10**9+7

ans = 0
for i in range(61):
  ones=sum([1 for a in A if (a>>i)&1])
  ans += (1<<i)*ones*(N-ones)
  ans %= M

print(ans)
### BUGGY CODE ###

n,k=map(int,input().split())
a=list(map(int,input().split()))
for i in range(k):
  b=[0]*(n+1)
  for j in range(n):
    b[max(0,j-a[j])]+=1
    b[min(n,j+a[j]+1)]-=1
  for i in range(n):
    b[i+1]+=b[i]
  a=b[:-1]
  if a==[n]*n:exit(print(*a))
print(*a)

### FIXED CODE ###

n,k=map(int,input().split())
a=list(map(int,input().split()))
for i in range(k):
  b=[0]*(n+1)
  for j in range(n):
    b[max(0,j-a[j])]+=1
    b[min(n,j+a[j]+1)]-=1
  for j in range(n):
    b[j+1]+=b[j]
  a=b[:-1]
  if a==[n]*n:exit(print(*a))
print(*a)
### BUGGY CODE ###

mod=10**9+7
def num2bi(x, k):
  #10進数xを2進数k桁(string)で出力
  out=format(x, '0'+str(k)+'b')
  return out

N=int(input())
A=list(map(int, input().split()))
ma=max(A)
keta=1
while ma>=2**keta:
  keta+=1
  
onekazu=[0 for i in range(keta)]
k=str(keta)
for i in range(N):
  temp=format(A[i], '0'+str(k)+'b')
  onekazu=[onekazu[j]+int(temp[j]) for j in range(keta)]
    
out=0
for i in range(keta):
  temp=onekazu[i]*(N-onekazu[i])%mod
  temp*=(2**(keta-1-i))%mod
  out+=temp
  out%=mod
  
print(out)

### FIXED CODE ###

mod=10**9+7
def num2bi(x, k):
  #10進数xを2進数k桁(string)で出力
  out=format(x, '0'+str(k)+'b')
  return out

N=int(input())
A=list(map(int, input().split()))
ma=max(A)
keta=1
while ma>=2**keta:
  keta+=1
  
onekazu=[0 for i in range(keta)]
k=str(keta)
for i in range(N):
  temp=format(A[i], '0'+str(k)+'b')
  onekazu=[onekazu[j]+1 if temp[j]=='1' else onekazu[j] for j in range(keta)]
    
out=0
out=sum([(onekazu[i]*(N-onekazu[i])%mod)*(2**(keta-1-i))%mod for i in range(keta)])
out%=mod  
print(out)
### BUGGY CODE ###

import math

N,P = map(int,input().split())

if N ==1:
    print(P)
    exit()
 

q = math.floor(math.sqrt(P))+1

ans = 1

for i in range(2,q+1):
    a = i**N
    if P%a == 0:
        ans = i
        
print(ans)

### FIXED CODE ###


N,P=map(int,input().split())
if N==1:
    print(P)
    exit()
elif P==1:
    print(1)
    exit()

PO=P
K=[0]*(int(P**0.5)+1)

for i in range(2,int(P**0.5)):
    while P%i==0:
        K[i]+=1
        P=P//i
    K[i]=K[i]//N
    if P==1:
        break

ans=1
for i in range(2,int(PO**0.5)):
    if K[i]!=0:
        ans*=i**K[i]

print(ans)
### BUGGY CODE ###

N = int(input())
path = []
ans = 0
for i in range(N):
    A = int(input())
    path.append([])
    for j in range(A):
        x, y = map(int,input().split())
        x -= 1
        path[-1].append((x, y))
for i in range(1<<N):
    i |= (1<<N)
    judge = True
    for j in range(N):
        if (1<<j) & i:
            for k in range(len(path[j])):
                if path[j][k][1] != int(bin(i)[-path[j][k][0] - 1]):
                    judge = False
    if judge:
        if ans < bin(i).count("1") - 1:
            ans = bin(i).count("1") - 1
print(ans)

### FIXED CODE ###

N = int(input())
path = []
ans = 0
for i in range(N):
    A = int(input())
    path.append([])
    for j in range(A):
        x, y = map(int,input().split())
        x -= 1
        path[-1].append((x, y))
for i in range(1<<N):
    i |= (1<<N)
    judge = True
    for j in range(N):
        if not judge:
            break
        if (1<<j) & i:
            for k in range(len(path[j])):
                if path[j][k][1] != int(bin(i)[-path[j][k][0] - 1]):
                    judge = False
                    break
    if judge:
        if ans < bin(i).count("1") - 1:
            ans = bin(i).count("1") - 1
print(ans)
### BUGGY CODE ###

def main():
    N, K = (int(i) for i in input().split())
    WD = [[int(i) for i in input().split()] for j in range(N)]
    W = [w[0] for w in WD]
    D = [d[1] for d in WD]

    def is_ok(X):
        need = 0
        for i in range(N):
            need += (X - W[i]) // D[i] + 1
        if need >= K:
            return True
        else:
            return False

    def binary_search_meguru():
        ng = -1
        ok = 10**36 + 1
        while abs(ok - ng) > 1:
            mid = ng + (ok - ng) // 2
            if is_ok(mid):
                ok = mid
            else:
                ng = mid
        return ok

    print(binary_search_meguru())


if __name__ == '__main__':
    main()


### FIXED CODE ###

def main():
    import sys
    input = sys.stdin.buffer.readline
    N, K = (int(i) for i in input().split())
    WD = [[int(i) for i in input().split()] for j in range(N)]
    ma_w = 0
    ma_d = 0
    for w, d in WD:
        if ma_w < w:
            ma_w = w
        if ma_d < d:
            ma_d = d

    def is_ok(X):
        need = 0
        for i in range(N):
            if WD[i][0] <= X:
                need += ((X - WD[i][0]) // WD[i][1]) + 1
        if need >= K:
            return True
        else:
            return False

    def binary_search_meguru():
        ng = -1
        ok = ma_w + K * ma_d + 1
        while abs(ok - ng) > 1:
            mid = ng + (ok - ng) // 2
            if is_ok(mid):
                ok = mid
            else:
                ng = mid
        return ok

    print(binary_search_meguru())


if __name__ == '__main__':
    main()
### BUGGY CODE ###

#!/usr/bin/env python3
import sys

def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)
    # divisors.sort()
    return divisors

def lef(a, b):
    return a*a*a*a + a*a*a*b + a*a*b*b + a*b*b*b + b*b*b*b

def solve(X: int):
    divs = make_divisors(X)
    for d in divs:
        base = 0
        upper = X // d
        while(lef(base+d,base) < upper):
            base+=1
        if lef(base+d,base) == upper:
            print(base+d,base)
            return
        base = -1
        while(lef(base+d,base) < upper) or (base+d >-1 ):
            base -= 1
            if lef(base+d, base) == upper:
                print(base+d, base)
                return
    raise
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    X = int(next(tokens))  # type: int
    solve(X)

if __name__ == '__main__':
    main()


### FIXED CODE ###

#!/usr/bin/env python3
import sys

def solve(X: int):

    for i in range(-150, 150):
        for j in range(-150, 150):
            if j**5 - i**5 == X:
                print(j, i)
                return
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    X = int(next(tokens))  # type: int
    solve(X)

if __name__ == '__main__':
    main()
### BUGGY CODE ###

import sys
sys.setrecursionlimit(4100000)
import math
import itertools
INF = float('inf')
from heapq import heapify, heappop, heappush

def main():
    n,k = map(int, input().split())
    A = list(map(int, input().split()))

    before = 1
    for i in range(k):
        before *= A[k-1-i]
    for i in range(k,n):
        after = (before//A[i-k])*A[i]
        if before < after:
            print('Yes')
        else:
            print('No')
        before = after


if __name__ == '__main__':
    main()


### FIXED CODE ###

import sys
sys.setrecursionlimit(4100000)
import math
import itertools
INF = float('inf')
from heapq import heapify, heappop, heappush

def main():
    n,k = map(int, input().split())
    A = list(map(int, input().split()))

    for i in range(k,n):
        if A[i-k]<A[i]:
            print('Yes')
        else:
            print('No')


if __name__ == '__main__':
    main()
### BUGGY CODE ###

import sys


# sys.stdin = open('d1.in')


def read_int_list():
    return list(map(int, input().split()))


def read_str_list():
    return input().split()


def read_int():
    return int(input())


def read_str():
    return input()


def main():
    inf = 10 ** 20
    n = read_int()
    adj = [[] for u in range(n)]
    for i in range(n - 1):
        a, b, c = read_int_list()
        a -= 1
        b -= 1
        adj[a].append((b, c))
        adj[b].append((a, c))

    q, k = read_int_list()
    k -= 1
    d = [inf for u in range(n)]

    d[k] = 0
    Q = [(k, -1)]
    while Q:
        u, parent = Q.pop(0)
        for v, w in adj[u]:
            if v == parent:
                continue
            d[v] = d[u] + w
        Q.extend([(v, u) for v, w in adj[u] if v != parent])

    res = [0] * q
    for i in range(q):
        x, y = read_int_list()
        x -= 1
        y -= 1
        res[i] = d[x] + d[y]
    print(*res, sep='\n')


main()


### FIXED CODE ###

import sys
from collections import deque


# sys.stdin = open('d1.in')


def read_int_list():
    return list(map(int, input().split()))


def read_str_list():
    return input().split()


def read_int():
    return int(input())


def read_str():
    return input()


def main():
    n = read_int()
    adj = [[] for u in range(n+1)]
    for i in range(n - 1):
        a, b, c = map(int, input().split())
        adj[a].append((b, c))
        adj[b].append((a, c))

    d = [0] * (n+1)
    q, k = read_int_list()

    # Q = [(k, 0, 0)]
    Q = deque([(k, 0, 0)])
    while Q:
        u, parent, dist = Q.popleft()
        for v, w in adj[u]:
            if v != parent:
                Q.append((v, u, dist+w))
        d[u] = dist

    res = [0] * q
    for i in range(q):
        x, y = map(int, input().split())
        res[i] = d[x] + d[y]
    print(*res, sep='\n')


main()