Harryxun/stf_run huggingface dataset:
check=dict()
N,b,d=0,[],0
#ttfは235、chaはabc

def dfs_sub(n,ttf,cha):
    global check,d
    check[0]=check[n]+n*d,check[0]
    if n//ttf in check:
        if check[n//ttf]>check[n]+(n%ttf)*d+cha:
            check[n//ttf]=check[n]+(n%ttf)*d+cha
            dfs(n//ttf)
    else:
        check[n//ttf]=check[n]+(n%ttf)*d+cha
        dfs(n//ttf)
    if 1+n//ttf in check:
        if check[1+n//ttf]>check[n]+(ttf-n%ttf)*d+cha:
            check[1+n//ttf]=check[n]+(ttf-n%ttf)*d+cha
            dfs(1+n//ttf)
    else:
        check[1+n//ttf]=check[n]+(ttf-n%ttf)*d+cha
        dfs(1+n//ttf)


def dfs(n):
    global b,check,d
    for i,j in zip([2,3,5],b):
        dfs_sub(n,i,j)

t=int(input())
for i in range(t):
    N,*b,d=map(int,input().split())
    check={N:0}
    dfs(N)
    print(check[0])


### FIXED CODE ###

import sys
check=dict()
N,a,b,c,d=0,0,0,0,0
#ttfは235、chaはabc
def dfs_sub(n,ttf,cha):
    global check,d
    if 0 in check:
        check[0]=min(check[n]+n*d,check[0])
    else:
        check[0]=check[n]+n*d
    if n//ttf==0:
        if n//ttf in check:
            if check[n//ttf]>check[n]+(n%ttf)*d:
                check[n//ttf]=check[n]+(n%ttf)*d
        else:
            check[n//ttf]=check[n]+(n%ttf)*d
    else:
        if n//ttf in check:
            if check[n//ttf]>check[n]+(n%ttf)*d+cha:
                check[n//ttf]=check[n]+(n%ttf)*d+cha
                dfs(n//ttf)
        else:
            check[n//ttf]=check[n]+(n%ttf)*d+cha
            dfs(n//ttf)
    if 1+n//ttf in check:
        if check[1+n//ttf]>check[n]+(ttf-n%ttf)*d+cha:
            check[1+n//ttf]=check[n]+(ttf-n%ttf)*d+cha
            dfs(1+n//ttf)
    else:
        check[1+n//ttf]=check[n]+(ttf-n%ttf)*d+cha
        dfs(1+n//ttf)


def dfs(n):
    global check,a,b,c,d
    dfs_sub(n,2,a)
    dfs_sub(n,3,b)
    dfs_sub(n,5,c)

t=int(input())
ans=[0]*t
for i in range(t):
    N,a,b,c,d=map(int,input().split())
    check={N:0}
    dfs(N)
    ans[i]=check[0]
for i in range(t):
    print(ans[i])
a,b=map(int(input()).split());print([a-1,a][b>=a])


### FIXED CODE ###

a,b=map(int,input().split());print([a-1,a][b>=a])
N,M=map(int,input().split())
mp=[[] for i in range(N+1)]
for i in range(M):
  L,R,D=map(int,input().split())
  mp[L].append([R,D])
  mp[R].append([L,-D])

dist=[None]*(N+1)

def dfs(n):
  for np,nd in mp[n]:
    if dist[np] is None:
      dist[np]=dist[n]+nd
      if not dfs(np):
        return False
    else:
      if dist[np]!=dist[n]+nd:
        return False
  return True

ans=True
for i in range(1,N+1):
  if dist[i] is None:
    dist[i]=0
    if not dfs(i):
      ans=False
      break
if ans:
  print("Yes")
else:
  print("No")

### FIXED CODE ###

import sys
sys.setrecursionlimit(200002)

N,M=map(int,input().split())
mp=[[] for i in range(N+1)]
for i in range(M):
  L,R,D=map(int,input().split())
  mp[L].append([R,D])
  mp[R].append([L,-D])

dist=[None]*(N+1)

def dfs(n):
  for np,nd in mp[n]:
    if dist[np] is None:
      dist[np]=dist[n]+nd
      if not dfs(np):
        return False
    else:
      if dist[np]!=dist[n]+nd:
        return False
  return True
ans=True
for i in range(1,N+1):
  if dist[i] is None:
    dist[i]=0
    if not dfs(i):
      ans=False
      break
if ans:
  print("Yes")
else:
  print("No")
Q,H,S,D = map(int,input().spllit())
N = int(input())

h = min(H , 2*Q)
s = min(2*h,S)
d = min(2*s,D)

print((N//2)*d + (N%2)*s)

### FIXED CODE ###

Q,H,S,D = map(int,input().split())
N = int(input())
h = min(2*Q,H)
s = min(2*h,S)
d = min(2*s,D)
print((N//2)*d + (N%2)*s)
N, K = map(int, input().split())
A = [int(a) for a in input().split()]
X = [0] * (N*2+100) # 後手必勝のところが1

X[K] = 1
for i in range(K)[::-1]:
    for a in A:
        if X[i+a] == 1:
            break
    else:
        X[i] = 1
        
print(["First", "Second"][X[0]])

### FIXED CODE ###

N, K = map(int, input().split())
A = [int(a) for a in input().split()]
X = [0] * (K + 1) # 後手必勝のところが1

X[K] = 1
for i in range(K)[::-1]:
    for a in A:
        if i + a <= K:
            if X[i+a] == 1:
                break
    else:
        X[i] = 1
        
print(["First", "Second"][X[0]])
l=str(s1,s2,s3)
print(l.count(1)


### FIXED CODE ###

s=str(input())
print(s.count('1'))
a, b, c, d = map(int, input().split())
if a+b > c+d:
    pirnt('Left')
elif a+b == c+d :
    print('Balanced')
else :
    print('Right')

### FIXED CODE ###

a, b, c, d = map(int, input().split())
if (a+b) > (c+d):
    print('Left')
elif (a+b) == (c+d) :
    print('Balanced')
else :
    print('Right')
n = int(input())
if n<10:
    print(n)
elif n<100:
    print(9)
elif n<1000:
    print(n-(99-9))
elif n<10000:
    print(999-(99-9))
elif n<100000:
    print(n-(9999-999)-(99-9))
elif n==100000:
    print(99999-(9999-999)-(99-9))



### FIXED CODE ###

a,b,c = map(int, input().split())
d = c-(a-b)
if d<0:
    print(0)
else:
    print(d)
N=int(input())
T,A=map(int,input().split())
List=list(map(int,input().split())
a=[]
for i in range(N):
    a.append(abs(T-List[i]*0.006-A))
print(index(min(a))+1)       


### FIXED CODE ###

N=int(input())
T,A=map(int,input().split())
List=list(map(int,input().split()))
a=[]
for i in range(N):
    a.append(abs(T-List[i]*0.006-A))
print(a.index(min(a))+1)
k, n = map(int,input().split())
length = list(map(int,input().split()))
distance =[]
for i in range(n):
  distance.append(length[i+1]-length[i])
distance.append(k-length[n-1])
print(k-max(distance))

### FIXED CODE ###

k, n = map(int,input().split())
length = list(map(int,input().split()))
length.append(k+length[0])
distance =[]
for i in range(n):
  distance.append(length[i+1]-length[i])
print(k-max(distance))
coding: utf-8
H, W = map(int, input().split())

for _ in range(H):
    l = input()
    print(l, l, sep="\n")


### FIXED CODE ###

# coding: utf-8
H, W = map(int, input().split())

for _ in range(H):
    l = input()
    print(l)
    print(l)
import re

pattern = re.compile("R+")
print(len(re.search(pattern, input()).group(0)))

### FIXED CODE ###

s = input()

if "RRR" in s:
    print(3)
elif "RR" in s:
    print(2)
elif "R" in s:
    print(1)
else:
    print(0)
import itertools as itl

n = int(input())
A = list(map(int, input().split()))
q = int(input())
M = list(map(int, input().split()))

C = [False for i in range(q)]
for i in range(1, n+1):
    for j in itl.combinations(A, i):
        for mi in range(q):
            if sum(j) == M[mi]:
                C[mi] = True
                
for c in C:
    if c: print("yes")
    else: print("no")



### FIXED CODE ###

import itertools as itl

n = int(input())
A = list(map(int, input().split()))
q = int(input())
M = list(map(int, input().split()))
S = []

for i in range(n):
    cmb = list(itl.combinations(A, i+1))
    for j in cmb:
        S.append(sum(j))

for m in M:
    flg = False
    for s in S:
        if s == m:
            flg = True
            break
    if flg:
        print("yes")
    else:
        print("no")
taka, aoki, times= map(int,input().split())
if taka + aoki > times:
    if taka < times:
        print(0,aoki - (times - taka))
    elif taka > times:
        print(taka - times, aoki)
    else:
        print(0,0)
elif taka = times:
    print(0,aoki)
else:
    print(0,0)


### FIXED CODE ###

taka, aoki, times= map(int,input().split())
if taka + aoki > times:
    if taka < times:
        print(0,aoki - (times - taka))
    elif taka > times:
        print(taka - times, aoki)
    else:
        print(0,aoki)
else:
    print(0,0)
n, m = map(int, input().split())
ans = list("000")
x = [list(map(int, input().split())) for i in range(m)]
x = list(map(list, set(map(tuple, x))))
for s, c in x:
    if ans[s-1] == "0":
        ans[s-1] = str(c)
    else:
        print("-1")
        exit()

if n == 1:
    ans = int(ans[2:])
    for i in range(10):
        if i == ans:
            print(i)
            exit() 

if n == 2:
    ans = int(ans[1:])
    for i in range(10, 100):
        if i == ans:
            print(i)
            exit() 

if n == 3:
    ans = int(ans[0:])
    for i in range(100, 1000):
        if i == ans:
            print(i)
            exit() 

### FIXED CODE ###

n, m = map(int, input().split())
x = [list(map(int, input().split())) for i in range(m)]
count = 0
for i in range(1000):
    count = 0
    i = str(i)
    if len(i) == n:
        for s, c in x:
            if i[s-1] == str(c):
                count += 1
            else:
                break
        if count == m:
            print(i)
            exit()
print(-1)
#coding: utf-8
import math
import heapq
import bisect
import numpy as np
from collections import Counter
#from scipy.misc import comb

N,M = map(int, input().split())
G = [[] for _ in range(N)]

E = []
for _ in range(M):
    a,b = map(int, input().split())
    G[a-1].append(b-1)
    G[b-1].append(a-1)
    E.append((a-1,b-1))

d = [0] + [-1]*(N-1)

def dfs(n):
    for v in G[n]:
        if d[v] == -1:
            dfs(v)

ans = 0
for a,b in E:
    G[a].remove(b)
    G[b].remove(a)

    dfs(0)

    if -1 in d:
        ans += 1
    
    for i in range(1,N):
        d[i] = -1

print(ans)

### FIXED CODE ###

#coding: utf-8
import math
import heapq
import bisect
import numpy as np
from collections import Counter
#from scipy.misc import comb

N,M = map(int, input().split())
G = [[] for _ in range(N)]

E = []
for _ in range(M):
    a,b = map(int, input().split())
    G[a-1].append(b-1)
    G[b-1].append(a-1)
    E.append((a-1,b-1))

d = [0] + [-1]*(N-1)

def dfs(n):
    d[n] = 0
    for v in G[n]:
        if d[v] == -1:
            dfs(v)

ans = 0
for a,b in E:
    G[a].remove(b)
    G[b].remove(a)

    dfs(0)

    if -1 in d:
        ans += 1
    
    for i in range(1,N):
        d[i] = -1
    G[a].append(b)
    G[b].append(a)

print(ans)
n,m,k = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
ans = 0
while True:
    if len(a) < 1 and len(b) < 1:
        break
    elif len(a) < 1:
        k -= b[0]
        del b[0]
        ans += 1
    elif len(b) < 1:
        k -= a[0]
        del a[0]
        ans += 1
    else:
        if a[0] > b[0]:
            k -= b[0]
            del b[0]
            ans += 1
        elif a[0] < b[0]:
            k -= a[0]
            del a[0]
            ans += 1
        else:
            if a[1] > b[1]:
                k -= b[0]
                del b[0]
                ans += 1
            else:
                k -= a[0]
                del a[0]
                ans += 1
    if k < 0:
        break
if k < 0:
    print(ans-1)
else:
    print(ans)

### FIXED CODE ###

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

A = [0]
B = [0]

for i in range(n):
    A.append(A[i]+a[i])

for i in range(m):
    B.append(B[i]+b[i])
    
ans,j = 0,m
for i in range(n+1):
    if A[i] > k:
        break
    while B[j] > k - A[i]:
        j -= 1
    ans = max(ans,i+j)
print(ans)
n=int(input)
i=1
while i*2<=n:
  i*=2
print(i)
    

### FIXED CODE ###

n=int(input())
i=1
while i*2<=n:
  i*=2
print(i)
s = list(map(int,input().split( ))
karians = []
karians.append(karians[0]-karians[1])
karians.append(karians[0]+karians[1])
karians.append(karians[0]*karians[1])
ans = max(karians)
print(ans)

### FIXED CODE ###

s = list(map(int,input().split( )))
x = []
x.append(s[0]+s[1])
x.append(s[0]-s[1])
x.append(s[0]*s[1])
ans = max(x)
print(ans)
import sys
sys.setrecursionlimit(3000)
N = int(input())
X = input()
zero_pop = X.count('1') + 1
one_pop = X.count('1') - 1
one_mod, zero_mod = 0, 0

for b in X:
    if one_pop != 0:
        one_mod = (one_mod * 2 + int(b)) % one_pop
    zero_mod = (zero_mod * 2 + int(b)) % zero_pop

pop = {0:0}
def popcount(n):
    if n in pop:
        return pop[n]
    pop[n] = popcount(n // 2) + n % 2
    return pop[n]
memo = {0:0}
def f_count(n):
    if n in memo:
        return memo[n]
    memo[n] = f_count(n % popcount(n)) + 1
    return memo[n]
one_p = {0:1}
def one_pow(n):
    if n in one_p:
        return one_p[n]
    one_p[n] = one_pow(n - 1) * 2 % one_pop
    return one_p[n]
zero_p = {0:1}
def zero_pow(n):
    if n in zero_p:
        return zero_p[n]
    zero_p[n] = zero_pow(n - 1) * 2 % zero_pop
    return zero_p[n]
for i in range(N):
    if X[i] == '1' and one_pop == 0:
        print(0)
        continue
    if X[i] == '1':
        x = one_mod
        x -= one_pow(N-1-i)
        x %= one_pop
    elif X[i] == '0':
        x = zero_mod
        x += zero_pow(N-1-i)
        x %= zero_pop
    
    print(f_count(x) + 1)

### FIXED CODE ###

import sys
sys.setrecursionlimit(10000000)
N = int(input())
X = input()
zero_pop = X.count('1') + 1
one_pop = X.count('1') - 1
one_mod, zero_mod = 0, 0

for b in X:
    if one_pop != 0:
        one_mod = (one_mod * 2 + int(b)) % one_pop
    zero_mod = (zero_mod * 2 + int(b)) % zero_pop

pop = {0:0}
def popcount(n):
    if n in pop:
        return pop[n]
    pop[n] = popcount(n // 2) + n % 2
    return pop[n]
memo = {0:0}
def f_count(n):
    if n in memo:
        return memo[n]
    memo[n] = f_count(n % popcount(n)) + 1
    return memo[n]
one_p = {0:1}
def one_pow(n):
    if n in one_p:
        return one_p[n]
    one_p[n] = one_pow(n - 1) * 2 % one_pop
    return one_p[n]
zero_p = {0:1}
def zero_pow(n):
    if n in zero_p:
        return zero_p[n]
    zero_p[n] = zero_pow(n - 1) * 2 % zero_pop
    return zero_p[n]
for i in range(N):
    if X[i] == '1' and one_pop == 0:
        print(0)
        continue
    if X[i] == '1':
        x = one_mod
        x -= one_pow(N-1-i)
        x %= one_pop
    elif X[i] == '0':
        x = zero_mod
        x += zero_pow(N-1-i)
        x %= zero_pop
    
    print(f_count(x) + 1)
M,K = list(map(int,input().split()))

if M==0:
    if K==0:
        print("0 0")
    else:
        print(-1)
elif M==1:
    if K==0:
        print("0 0 1 1")
    else:
        print(-1)
else:
    if K==0:
        ans = []
        for i in range(2**M):
            ans.append(i)
            ans.append(i)
        ans = list(map(str,ans))
        print(*ans)
    elif K<2**M:
        ans = [0,K,0]
        ansc = []
        for i in range(1,2**M):
            if i!=K:
                ansc.append(i)
                
        ans =ans+ansc + [K] + ansb +ansc[::-1]
        ans = list(map(str,ans))
        print(*ans)
    else:
        print(-1)

### FIXED CODE ###


M,K = list(map(int,input().split()))

if M==0:
    if K==0:
        print("0 0")
    else:
        print(-1)
elif M==1:
    if K==0:
        print("0 0 1 1")
    else:
        print(-1)
else:
    if K==0:
        ans = []
        for i in range(2**M):
            ans.append(i)
            ans.append(i)
        #ans = list(map(str,ans))
        print(*ans)
    elif K<2**M:
        ans = [0,K,0]
        ansc = []
        for i in range(1,2**M):
            if i!=K:
                ansc.append(i)
                
        ans =ans+ansc + [K] +ansc[::-1]
        #ans = list(map(str,ans))
        print(*ans)
    else:
        print(-1)
l, r = map(int, input().split())
if r //2019 > l //2019:
    print(0)
else:
    res = 2018
    for i in range(l%2019, (r+1)%2019):
        for j in range(i+1, (r+1)%2019):
            res = min(res, (i*j)%2019)
    print(res)

### FIXED CODE ###

n = int(input())
aa = list(map(int, input().split()))
total_odd = sum(aa[1::2])
total_eve = sum(aa[0::2])
tmp = total_eve - total_odd
print(tmp)
for i in range(1,n):
    tmp = 2*aa[i-1] - tmp
    print(tmp)
N, K, X, Y = map(int, input().split())
total = K*X + (N-K)*Y
print(total)

### FIXED CODE ###

N = int(input())
K = int(input())
X = int(input())
Y = int(input())
if N > K:
    total = K*X + (N-K)*Y
    print(total)
else:
    total = N*X
    print(total)
a = 20
b = map(int, input().split())
print("Odd" if a * b % 2 else "Even")

### FIXED CODE ###

a, b = map(int, input().split())
if (a*b) % 2:
  print('Odd')
else:
  print('Even')
a = input()
s = input()

if a < 3200:
    print(s)
elif a >= 3200:
    print("red")

### FIXED CODE ###

a = int(input())
s = input()

if a >= 3200:
    print(s)
else:
    print("red")
N = int(input())
d = []
visited = [False for _ in range(N+1)]
for _ in range(N):
  d.append(int(input())-1)
  
def dfs(i,cnt):
  if i == 1:
    return cnt
  elif cnt >= N:
    return -1
  elif visited[i]:
    return -1
  else:
    visited[i] = True
    return dfs(d[i],cnt+1)
  
print(dfs(0,0))

### FIXED CODE ###

N = int(input())
d = []
visited = [False for _ in range(N+1)]
for _ in range(N):
  d.append(int(input())-1)

i = 0
cnt = 0
while True:
  if i == 1:
    break
  elif cnt >= N:
    cnt = -1
    break
  elif visited[i]:
    cnt = -1
    break
  else:
    visited[i] = True
    cnt += 1
    i = d[i]
  
print(cnt)
K = input()
S = input()
count = len(S)
list = 0
a = ""
for i in raige(K):
  a = a+S[list]
  list = list + 1

if (count <= K):
  print(S)
else:
  print(a+"...")

### FIXED CODE ###

k = int(input())
s = str(input())
if len(s) <= k:
    print(s)
else:
    print((s[:k]) + "...")
def gcd(a, b):
    if a < b:
        a, b = b, a
    if b == 0:
        return a
    else:
        return gcd(b, a%b)

def lcm(a, b):
    return a * b / gcd(a, b)

N = input()
V = 1
for i in range(int(N)):
    T = input()
    V = lcm(V, int(T))
    
print(int(V))

### FIXED CODE ###

def gcd(a, b):
    if a < b:
        a, b = b, a
    if b == 0:
        return a
    else:
        return gcd(b, a%b)

def lcm(a, b):
    return a // gcd(a, b) * b

N = input()
V = 1
for i in range(int(N)):
    T = input()
    V = lcm(V, int(T))

print(V)
N, K = list(map(int, input().split()))
S = str(input())
m = chr(ord(S[K]) - 32)
print(N[:m] + m + N[m+1:])




### FIXED CODE ###

N, K = list(map(int, input().split()))
S = str(input())
m = chr(ord(S[K-1]) + 32)
print(S[:K-1] + m + S[K:])
a, b = int(input().split())
if a > 8 or b > 8:
  print(":(")
elif:
  print("Yay!")

### FIXED CODE ###

a, b = map(int, input().split())
if a <= 8 and b <= 8:
  print("Yay!")
else:
  print(":(")
n, m = map(int, input().split())
q = []
for i in range(m):
    a, b = map(int, input().split())
    q.append({a, b})
cnt = 0
while cnt != 10000:
    a = q.pop()
    for j in q:
        if len(j & a) != 0:  # jとaに共通の値があるならば
            j |= a
            count = 0
            break
        else:  # jとaに共通の値がなければ
            q = [a] + q
    if count > len(q):
        break
    count += 1
    for i in q:
        tmp = list(i)
        if tmp == array:
            cnt += 1
print(cnt)


### FIXED CODE ###

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

path_matrix = []

#無向行列の初期化
for n in range(N):
    path_matrix.append([False] * N)

#まず無向グラフを表す行列を作る
for m in range(M):
    a,b = map(int, input().split())
    path_matrix[a-1][b-1] = True
    path_matrix[b-1][a-1] = True
#無向グラフを表す行列はpath_matrix[i][j]iの部分が各頂点でjが他の頂点との繋がりを意味する
#最初はvisited、つまり訪問したことのあるノードはひとつもないので、Falseにする
visited = [False] * N #この問題は各頂点をすべて通る方法を調べること
def dfs(now,depth):
    if visited[now]:#もし現在位置が訪問済みなら
        return 0
    if depth == N-1:#もし深さが最大までいったらすべての頂点を探索し終えたので１を返す
        return 1
    
    visited[now] = True#現在位置を訪問済みにする(これをしないとループし続ける)
    total_path = 0
    for i in range(N):
        if path_matrix[now][i]:#もし飛べる先があるのなら、その飛んだ先のノードで今いる位置と深さを更新した上で、もっと掘り下げてみる
            total_path += dfs(i,depth+1)#もし深さが最大まで行ったらすべての頂点について探索が終了したので1加える

    visited[now] = False

    return total_path


print(dfs(0,0))
n=int(input())
ans=1
a=[]
for i in range(n):
  a.append(int(input()))
x=a[0]
if x=-2:
  print(ans)
  exit()
for i in range(n):
  x=a[x-1]
  if x==2:
    print(ans+1)
    exit()
  else:
    ans+=1
print(-1)

### FIXED CODE ###

n=int(input())
ans=1
a=[]
for i in range(n):
  a.append(int(input()))
x=a[0]
if x==2:
  print(ans)
  exit()
for i in range(n):
  x=a[x-1]
  if x==2:
    print(ans+1)
    exit()
  else:
    ans+=1
print(-1)
sx,sy=map(int,input().split())
tx,ty=map(int,input().split())
dx,dy=tx-sx,ty-sy
print('U'*dy+'R'*dx+'D'*dy+'L'*dx+'L'+'U'*(dy+1)+'R'*(dx+1)+'D'+'R'+'D'*(dy+1)+'L'*(dx+1)+'U')

### FIXED CODE ###

sx,sy,tx,ty=map(int,input().split())
dx,dy=tx-sx,ty-sy
print('U'*dy+'R'*dx+'D'*dy+'L'*dx+'L'+'U'*(dy+1)+'R'*(dx+1)+'D'+'R'+'D'*(dy+1)+'L'*(dx+1)+'U')
import sys
from math import gcd
def input(): return sys.stdin.readline().strip()

def main():
    N = int(input())
    A =list(map(int, input().split()))
    A.sort()

    idx = 0
    while A[idx] == 1: idx += 1
    A = A[idx:]

    all_gcd = A[0]
    for a in A:
        all_gcd = gcd(all_gcd, a)
    if all_gcd != 1:
        print("not coprime")
        return

    prod = A[0]
    for i, a in enumerate(A):
        if i == 0: continue
        if gcd(prod, a) != 1:
            print("setwise coprime")
            return
        prod *= a
    print("pairwise coprime")




if __name__ == "__main__":
    main()


### FIXED CODE ###

import sys
from math import gcd
def input(): return sys.stdin.readline().strip()

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

    all_gcd = A[0]
    max_num = A[0]
    for a in A:
        all_gcd = gcd(all_gcd, a)
        max_num = max(max_num, a)
    if all_gcd != 1:
        print("not coprime")
        return

    # エラトステネスの篩でprimes[i] = (iを割り切る最小の素数)を記録する
    min_prime = [i for i in range(max_num + 1)]
    for p in range(2, int(max_num**0.5) + 1):
        if min_prime[p] != p: continue
        for i in range(2, max_num // p + 1): min_prime[i * p] = p

    used = [0] * (max_num + 1)
    for a in A:
        while a > 1:
            p = min_prime[a]
            if used[p]:
                print("setwise coprime")
                return
            while a % p == 0: a //= p
            used[p] = 1
    print("pairwise coprime")




if __name__ == "__main__":
    main()
x, y, z = input.split(",")

print(x, y, z)

### FIXED CODE ###

x, y, z = input().split(",")
print(x, y, z)
s = [input() for i in range(2)]
N = int(s[0])
S = list(s[1])
eng = list('ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ')
idx = np.array([eng.index(S[i]) for i in range(len(S))])
N_l = np.array([N]*len(S))
new_idx = idx + N_l
ans = [eng[i] for i in new_idx]
fin_ans = ''
for x in ans:
    fin_ans += x
print(fin_ans)

### FIXED CODE ###

import numpy as np
s = [input() for i in range(2)]
N = int(s[0])
S = list(s[1])
eng = list('ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ')
idx = np.array([eng.index(S[i]) for i in range(len(S))])
N_l = np.array([N]*len(S))
new_idx = idx + N_l
ans = [eng[i] for i in new_idx]
fin_ans = ''
for x in ans:
    fin_ans += x
print(fin_ans)
S=input()
T=input()
i=0
j=0
match = 0
for i in Range(Len(S)-Len(T)):
  count = 0
  for j in Range(Len(T)):
    if S[i+j]==T[j]:
      count=count+1
  
  if match<count:
    match=count

print(Len(T)-match)
      
    

### FIXED CODE ###

S=input()
T=input()
match=0
for i in range(len(S)-len(T)+1):
    count=0
    for j in range(len(T)):
        if S[i+j]==T[j]:
            count=count+1
    if match<count:
        match=count
print(len(T)-match)
import sys
#limit=sys.getrecursionlimit()
sys.setrecursionlimit(100000)
#sys.setrecursionlimit(1000000000)

V=int(input().strip())
E=V-1
inf=3*10**15
G=[]
dist=[inf]*V

for i in range(V):
    G.append([])
for i in range(E):
    at,bt,ct=map(int,input().strip().split(' '))
    G[at-1].append([ct,bt-1])
    G[bt-1].append([ct,at-1])

visited=[False]*V
def dfs(v,p,d):
    #global d
    dist[v]=d
    for q in G[v]:
        if q[1]==p:
            continue
        dfs(q[1],v,d+q[0])

Q, K=map(int,input().strip().split(' '))
K-=1 #0-indexedに直す

dfs(K,-1,0)

x=[]
y=[]

for i in range(Q):
    xt, yt=map(int,input().strip().split(' '))
    x.append(xt-1)#0-indexedに直して入れる
    y.append(yt-1)#0-indexedに直して入れる
for i in range(Q):
    print(dist[x[i]]+dist[y[i]])





### FIXED CODE ###

import sys
#limit=sys.getrecursionlimit()
sys.setrecursionlimit(10000000)
#sys.setrecursionlimit(1000000000)

V=int(input().strip())
E=V-1
inf=3*10**15
G=[]
dist=[inf]*V

for i in range(V):
    G.append([])
for i in range(E):
    at,bt,ct=map(int,input().strip().split(' '))
    G[at-1].append([ct,bt-1])
    G[bt-1].append([ct,at-1])

visited=[False]*V
def dfs(v,p,d):
    #global d
    dist[v]=d
    for q in G[v]:
        if q[1]==p:
            continue
        dfs(q[1],v,d+q[0])

Q, K=map(int,input().strip().split(' '))
K-=1 #0-indexedに直す

dfs(K,-1,0)

x=[]
y=[]

for i in range(Q):
    xt, yt=map(int,input().strip().split(' '))
    x.append(xt-1)#0-indexedに直して入れる
    y.append(yt-1)#0-indexedに直して入れる
for i in range(Q):
    print(dist[x[i]]+dist[y[i]])