Harryxun/stf_run huggingface dataset:
X,N = map(int, input().split())
P = list(map(int, input().split()))

table = [0] * 102
ans = X
dist = 102

for p in range(P):
  table[p] = 1

for i in range(102):
  if abs(i - X) < dist:
    ans = i
    dist = abs(i - X)
  elif abs(i - X) == dist:
    ans = min(i, ans)

print(str(ans))

### FIXED CODE ###

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

table = [0] * 102
ans = X
dist = 102

for p in P:
  table[p] = 1

for i in range(102):
  if table[i] != 0:
    continue
  if abs(i - X) < dist:
    ans = i
    dist = abs(i - X)
  elif abs(i - X) == dist:
    ans = min(i, ans)

print(str(ans))
x, n = map(int, input().split())
if n == 0:
    print(x)
    sys.exit()

p = list(map(int, input().split()))

p.sort()

for i in range(101):
    if x -i not in p:
        print(x-i)
        exit()
    elif x +i not in p:
        print(x+i)
        exit()

### FIXED CODE ###

x, n = map(int, input().split())
if n == 0:
    print(x)
    exit()

p = list(map(int, input().split()))

#p.sort()

for i in range(101):
    if x -i not in p:
        print(x-i)
        exit()
    elif x +i not in p:
        print(x+i)
        exit()
N, A, B = map(int, input().split())

counter = [0 for _ in range(N)]
ans = 0

for i in range(1,N+1):
    for j in str(i):
        counter[i+1] += int(j)
    if A<= counter[i+1] <=B :
        ans += i

print(ans)

### FIXED CODE ###

N, A, B = map(int, input().split())
ans = 0

for i in range(1, N+1):
    num_sum = 0
    for j in str(i):
        num_sum += int(j)
    if A<= num_sum <= B:
        ans += i

print(ans)
c=input()
c=chr(ord(c+1))
print(c)

### FIXED CODE ###

c=input()
print(chr(ord(c)+1))
import math as m
r=int(input())
print("%.6f"%(r**2*m.pi),"%.6f"%(r*2*m.pi))

### FIXED CODE ###

# encoding:utf-8
import math

r = float(input())

area = r**2*math.pi
circumference = r * 2 * math.pi

area = ('%03.6f' % area)
circumference = ('%03.6f' % circumference)

print(area),
print(circumference)
a=int(input())
b=int(input())
l=[]
l.append(a+b)
l.append(a-b)
l.append(a*b)
print(max(l))


### FIXED CODE ###

l= [int(x) for x in input().split()]
p=[]
p.append(l[0]-l[1])
p.append(l[0]+l[1])
p.append(l[0]*l[1])
print(max(p))
n, k = map(int, input().split())
A = list(map(int, input().split()))
A = [x-1 for x in A]

town = 0
cnt = 0

memo = []
visited = [False] * n
visited[0] = -1

start = 0
for i in range(min(n, k)):
    town = A[town]
    memo.append(town)
    if visited[town] != False:
        start_cnt = i + 1
        loop = i - visited[town]
        break
    else:
        visited[town] = i

for i in range((k-start_cnt)%loop):
    town = A[town]

print(town+1)

### FIXED CODE ###

n, k = map(int, input().split())
A = list(map(int, input().split()))
A = [x-1 for x in A]

town = 0
cnt = 0

# memo = []
# visited = [False] * n
# visited[0] = -1

# start = 0
# loop = 10**8
# start_cnt = 0
# for i in range(n*2):
#     town = A[town]
#     memo.append(town)
#     if visited[town] != False:
#         start_cnt = i + 1
#         loop = i - visited[town]
#         break
#     else:
#         visited[town] = i
#
# for i in range((k-start_cnt)%loop):
#     town = A[town]
#
# print(town+1)

visited = [False] * n
visited[0] = 0

for i in range(n):
    town = A[town]
    if visited[town]:
        good_town = town
        break
    visited[town] = i+1

town = good_town

for i in range(n):
    town = A[town]
    if town == good_town:
        loop = i + 1

if k >= visited[good_town]:
    k = ((k-visited[good_town])%loop)+visited[good_town]


town = 0
for i in range(k):
    town = A[town]
print(town+1)
n = int(input)
keta = n//10 + 1
count = 0
for i in range(0, keta):
  if n[i] == 7:
    count = count
  else:
    count = count + 1
    
if count == keta:
  print('No')
else:
  print('Yes')


### FIXED CODE ###

n = int(input())
count = 0
tenwari = n%10
baiwari = n%100
ichi = tenwari
jyu = (baiwari-tenwari)//10
hyaku = (n-jyu*10-ichi)//100

if ichi != 7:
  count = count + 1
if jyu != 7:
  count = count + 1
if hyaku != 7:
  count = count + 1
  
if count == 3:
  print('No')
else:
  print('Yes')
N, L = map(int, input().split())
 
#標準入力からリストの作成
list = []
for _ in (1,N):
 i = input()
 list.append(i)
 
#リストの結合
mojiretu = ''.join(list)
 
print(mojiretu

### FIXED CODE ###

N, L = map(int, input().split())
 
#標準入力からリストの作成
list = []
for _ in range(N):
 i = str(input())
 list.append(i)

#リストのソート
list_a = sorted(list)
 
#リストの結合
mojiretu = ''.join(list_a)
 
print(mojiretu)
if __name__ == "__main__":
    N = int(input())
    K = int(input())
    x = map(int, input().split())
    ans = 0
    for i in range(N):
        r = min(x[i],abs(K-x[i]))
        ans += 2*r
    print(ans)

### FIXED CODE ###

if __name__ == "__main__":
    N = int(input())
    K = int(input())
    x = list(map(int, input().split()))
    ans = 0
    for i in range(N):
        r = min(x[i],abs(K-x[i]))
        ans += 2*r
    print(ans)
a=list(map(int, input().split()))
b=2＊a[3]＊a[2]
c=a[2]＊a[4]＊2+a[0]＊(a[3]-a[4])
d=2＊a[4]＊a[2]
e=a[2]＊a[3]＊2+a[1]＊(a[4]-a[3])
f=a[0]＊a[3]+a[1]＊a[4]
if a[3]>=a[4]:
  print(min(b,c,f))
else:
  print(min(d,e,f))


### FIXED CODE ###

a, b, c, x, y = map(int, input().split())
res = 0
if a+b > c*2:
    mi = min(x, y)
    res += c*2 * mi
    x -= mi
    y -= mi
res += x * min(a, c*2)
res += y * min(b, c*2)
print(res)
s=input()
MOD=10**9+7
dp=[[0 for i in range(13)] for j in range(len(s)+1)]

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

for i in range(1,len(s)):
  c = -1 if s[i]=='?' else int(s[i])
  for j in range(10):
    if c!=-1 and c!=j:
      continue
    for k in range(13):
      dp[i][(k*10+j)%13]+=dp[i-1][k]
  for j in range(13):
    dp[i][j]%=MOD

print(dp[len(s)-1][5])


### FIXED CODE ###

S=input()
a=[0]*13
a[0]= 1

for c in S:
  dp=[0]*13
  for i in range(13):
    dp[(i*10)%13]=a[i]%(10**9+7)
  dp+=dp

  if c=='?':
    for i in range(13):
      a[i]=sum(dp[i+4:i+14])
  else:
    for i in range(13):
      a[i]=dp[i+13-int(c)]

print(a[5]%(10**9+7))
import math

a, b, x = [int(x) for x in input().split()]

bootle_volume = a * a * b
half_bootle_volume = 0.5 * bootle_volume

res = 0
if half_bootle_volume == x:
    res = math.degrees(math.atan(1))
elif half_bootle_volume > x:
    y = a - ((2 * x) / (a * b))
    degree = math.degrees(math.atan((a - y) / b))
    res = 90 - degree
elif half_bootle_volume < x:
    y = ((2 * x) / (a ** 2)) - b
    degree = math.degrees(math.atan(a / (b - y)))
    res = 90 - degree

print(res)


### FIXED CODE ###

import math

a, b, x = [int(x) for x in input().split()]

bootle_volume = a * a * b
half_bootle_volume = 0.5 * bootle_volume

res = 0
if half_bootle_volume == x:
    res = math.degrees(math.atan(1))
elif half_bootle_volume > x:
    y = a - ((2 * x) / (a * b))
    degree = math.degrees(math.atan((a - y) / b))
    res = 90 - degree
elif half_bootle_volume < x:
    y = ((2 * x) / (a ** 2)) - b
    if y == b:
        res = 0
    else:
        degree = math.degrees(math.atan(a / (b - y)))
        res = 90 - degree

print(res)
n = int(input())
s = input()
c = 0
for i in range(2,n+1):
  if s[i-2] == "A" and s[i-1] == "B" and s[i] == "C":
    c += 1
print(c)

### FIXED CODE ###

n = int(input())
s = input()
c = 0
for i in range(2,n):
  if s[i-2] == "A" and s[i-1] == "B" and s[i] == "C":
    c += 1
print(c)
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 = sorted(A,reverse=True)
#B = sorted(B,reverse=True)
C = sorted(C,reverse=True)

AB=[]

for i in range(X):
  for j in range(Y):
    AB.append(A[i]+B[j])
    
ans = []

ABl = len(AB)
for i in rnage(ABl):
  for j in range(K):
    ans.append(AB[i]+C[j])

ans = sorted(ans, reverse=True)

for i in range(K):
  print(ans[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 = sorted(A, reverse = True)
B = sorted(B, reverse = True)
C = sorted(C, reverse = True)

ans = []

for i in range(min(K,X)):
  for j in range(min(K//(i+1),Y)):
    for k in range(min(K//(i+1)//(j+1),Z)):
      ans.append(A[i]+B[j]+C[k])
    
ans = sorted(ans, reverse=True)

for i in range(K):
  print(ans[i])
a,b = map(int,input().split()) print(sum(range(b-a))-a)

### FIXED CODE ###

N = int(input())

if N<=999:print('ABC')
else:print('ABD')
mod=10**9+7
N=int(input())
C=[0]
C.extend(sorted(list(map(int,input().split())),reverse=True)) # 1-indexed

# print(C)

ans=0
for j in range(1,N+1):
    tmp=C[j]*(j+1)*pow(2,N-2,mod)%mod
    # print(tmp)
    ans=(ans+tmp)%tmp
    # print(j,ans)
ans=ans*pow(2,N,mod)%mod

print(ans)

### FIXED CODE ###

# inverse x^(-1)
def inv(x):
    global mod
    return pow(x,mod-2,mod)

mod=10**9+7
N = int(input())
C = [0]
C.extend(sorted(list(map(int,input().split())), reverse=True)) # 1-indexed, 降順

# print(C)
# C[i]が最後からj番目に操作される（コストが C[i]*j 掛かる）のは，c[1]~C[i-1]にj-1個操作すべきもの，すなわち，C[i]以上のものがあるとき。
# C[i+1]~C[N]の選び方は自由。
# ⇒ combi(i-1, j-1)*2**(N-i) を掛ける。
# 総和を計算すると，結局，2**N * Σ[i=1->N](2**(N-i)*Ci*(2**(i-2)*(i-1) + 2**(i-1))) になる

pow2 = [1]
for k in range(1, N+1):    
    pow2.append(pow2[-1] * 2 % mod)

# print(pow2)

ans=0
for i in range(1, N+1):
    temp = pow2[N - i] * C[i] * ((0 if i == 1 else (pow2[i - 2] * (i - 1))) + pow2[i - 1]) % mod
    ans = (ans + temp) % mod

ans = ans * pow2[N] % mod

print(ans)
while 1:
    m, f, r = raw_input().split()
    sum = m + f
    if sum == -2: break
    elif min(m, f) == -1: print 'F'
    elif sum > 80: print 'A'
    elif sum > 65: print 'B'
    elif sum > 50: print 'C'
    elif sum > 30:
        if r > 50: print 'C'
        else: print 'D'
    else: print 'F'

### FIXED CODE ###

while 1:
    m, f, r = map(int, raw_input().split())
    sum = m + f
    if sum == -2: break
    elif min(m, f) == -1: print 'F'
    elif sum >= 80: print 'A'
    elif sum >= 65: print 'B'
    elif sum >= 50: print 'C'
    elif sum >= 30:
        if r >= 50: print 'C'
        else: print 'D'
    else: print 'F'
print('YES' if int(input) in [3,5,7] else 'NO')

### FIXED CODE ###

print('YES' if int(input()) in [3,5,7] else 'NO')
s = input()
t = input()

if s[0:len(s)] == t[0:len(t)]:
  print("Yes")
eles:
    print("No")
  

### FIXED CODE ###

s = input()
t = input()
 
if s[0:len(s)] == t[0:len(t)-1]:
  print("Yes")
else:
    print("No")
S=str(input())
T=str(input())
sum=0

for i in range(len(S)):
    if S[i]!=T[i]:
        sum++

print(sum)

### FIXED CODE ###

S=str(input())
T=str(input())
sum=0

for i in range(len(S)):
    if S[i]!=T[i]:
        sum+=1

print(sum)
time = [int(input()) for i in range(5)]
result = 0
a = []

for i in range(5):
    if time[i] % 10 == 0:
        result += time[i]
    else:
        result += ((time[i] // 10)+1)*10
        a.append(time[i] % 10)
print(result - (10 - min(a)))


### FIXED CODE ###

time = [int(input()) for i in range(5)]
result = 0
a = [10]

for i in range(5):
    if time[i] % 10 == 0:
        result += time[i]
    else:
        result += ((time[i] // 10)+1)*10
        a.append(time[i] % 10)
print(result - (10 - min(a)))
cnt = int(input())

input_line = input()


if len(input_line) > cnt :
	print(input_line[:cnt] + "...")

else :
	print(input_line)

### FIXED CODE ###

b = int(input())
a = b % 10

if a == 2 or a == 4 or a == 5 or a == 7 or a == 9 :
  print("hon")
elif a == 0 or a == 1 or a == 6 or a == 8 :
  print("pon")
elif a == 3 :
  print("bon")
n, k = map(int, input().split())
a = list(map(int, input().split()))
b = a.copy()
count = 1
d = 1
while k >= d:
    count += 1
    d *= 2
else:
    count 1= 1
    d //= 2
anst = 0
for i in range(count):
    level = 0
    for j in range(n):
        tmpb = b[j] // d
        if tmpb >= 1 and tmpb % 2 == 1:
            level += 1
        b[j] %= d
    if k >= anst + d and n > level * 2:
        anst += d
    d //= 2

ans = 0
for i in range(n):
    ans += a[i] ^ anst
print(ans)

### FIXED CODE ###

n, k = map(int, input().split())
a = list(map(int, input().split()))
b = a.copy()
count = 1
d = 1
while k >= d:
    count += 1
    d *= 2
else:
    count -= 1
    d //= 2
anst = 0
for i in range(count):
    level = 0
    for j in range(n):
        tmpb = b[j] // d
        if tmpb >= 1 and tmpb % 2 == 1:
            level += 1
        b[j] %= d
    if k >= anst + d and n > level * 2:
        anst += d
    d //= 2

ans = 0
for i in range(n):
    ans += a[i] ^ anst
print(ans)
n,k = map(int,input().split())
a = [int(input()) for _ in range(n)]
"""
b := ruisekiwa
b[r]-b[l] >= (r-l)k
b[r]-rk >= b[l]-lk (1-indexed)
c := b[i]-ik
"""
b = [0 for _ in range(n)]
b[0] = a[0]
for i in range(1, n):
	b[i] = b[i-1] + a[i]
 
c = [b[i]-(i+1)*k for i in range(n)]
 
"""
cr >= cl なる ペア(l, r)の個数を求めたい.
for i in range(n), find (i,j)
"""
 
class BIT:
	"""
	<Attention> 0-indexed.
	query ... return the sum [0 to m]
	sum ... return the sum [a to b]
	sumall ... return the sum [all]
	add ... 'add' number to element (be careful that it doesn't set a value.)
	search ... the sum version of bisect.right
	output ... return the n-th element
	listout ... return the BIT list
	"""
	def query(self, m):
		res = 0
		while m > 0:
			res += self.bit[m]
			m -= m&(-m)
		return res
	
	def sum(self, a, b):
		return self.query(b)-self.query(a)
	
	def sumall(self):
		bitlen = self.bitlen-1
		return self.query(bitlen)
 
	def add(self, m, x):
		m += 1
		bitlen = len(self.bit)
		while m <= bitlen-1:
			self.bit[m] += x
			m += m&(-m)
		return
	
	def search(self, a):
		tmpsum = 0
		i = 0
		k = (self.bitlen-1).bit_length()
		while k >= 0:
			tmpk = 2**k
			if i+tmpk <= self.bitlen-1:
				if tmpsum + self.bit[i+tmpk] < a:
					tmpsum += self.bit[i+tmpk]
					i += tmpk
			k = k - 1
		return i+1
	
	def output(self, a):
		return self.query(a+1)-self.query(a)
	
	def listout(self):
		return self.bit
	
	def __init__(self, a):
		self.bitlen = len(a)+1
		self.bit = [0]*(len(a)+1)
		for i, j in enumerate(a):
			self.add(i, j)
 
def compress(arr):
	*XS, = set(arr)
	XS.sort()
	target = {e: i for i, e in enumerate(XS)}
	rtl = []
	for i in range(n):
		rtl.append(target[arr[i]])
	return rtl
 
d = compress(c)
 
bit = BIT([0 for _ in range(n)])
ans = 0
for i in range(n):
	bit.add(d[i], 1)
	ans += bit.query(d[i]+1)
	if c[i] < 0:
		ans -= 1
print(ans)

### FIXED CODE ###

n = int(input())
s = [int(input()) for _ in range(n)]
k = sum(s)
if k % 10 != 0:
	print(k)
else:
	for i in sorted(s):
		if i % 10 != 0:
			print(k-i)
			exit()
	print(0)
import math
A, B, N = map(int, input().split())

x = list(range(1, N+1))

ans = -10**10
for i in x:
  y = math.floor(A*i/B) - A * math.floor(i/B)
  if y > ans:
    ans = y
  elif y < ans:
    break
print(y)

### FIXED CODE ###

import math
A, B, N = map(int, input().split())

x = min(B-1, N)
y = math.floor(A*x/B) - A * math.floor(x/B)
print(y)
N=int(input())
S=list(input())
A=''
for i in S:
    if ord(i)+N<ord('Z'):
        A+=chr(ord(i)+N)
    else:
        A+=chr(ord(i)+N-ord('z'))
print(A)


### FIXED CODE ###

N=int(input())
S=list(input())
A=''
for i in S:
    if ord(i)+N<=ord('Z'):
        A+=chr(ord(i)+N)
    else:
        A+=chr(ord(i)+N-(ord('Z')-ord('A')+1))
print(A)
n = input()
arr = input().split(' ')
arr = [int(a) for a in arr]
# arr = [12, 1, 12, 12, 12]
def gcd(a, b):
    while b is not 0:
        a, b = b, a % b
    return a
def lcm(a, b):
    return a *b//gcd(a, b)
tmp = arr[0]
for i in range(len(arr)-1):
    tmp = lcm(tmp,arr[i+1])
ans = sum([tmp/a for a in arr])
ans = int(ans % (1e+9+7))
print(ans)


### FIXED CODE ###

import sys
from fractions import gcd
input = sys.stdin.readline

n = input()
arr = list(map(int, input().split()))
tmp = arr[0]
for i in arr:
    tmp = tmp*i//gcd(tmp,i)
print(sum(tmp//a for a in arr) % (10**9+7))
N,Q=map(int,input().split())
AB=[list(map(int,input().split())) for _ in range(N-1)]
PX=[list(map(int,input().split())) for _ in range(Q)]

link=[[] for _ in range(N)]
for i in range(N-1):
  a=AB[i][0]-1
  b=AB[i][1]-1
  link[a].append(b)

ans=[0]*N

def px(p,x):
  for i in link[p]:
    ans[i]+=x
    px(i,x)

for i in range(Q):
  p=PX[i][0]-1
  x=PX[i][1]
  ans[p]+=x
  px(p,x)

print(*ans)

### FIXED CODE ###

import sys
sys.setrecursionlimit(10**9)

N,Q,*abpx=map(int,sys.stdin.read().split())

graph=[[] for _ in range(N)]
for a,b in zip(*[iter(abpx[:2*(N-1)])]*2):
    a-=1
    b-=1
    graph[a].append(b)
    graph[b].append(a)

f=[0]*N
for p,x in zip(*[iter(abpx[2*(N-1):])]*2):
    p-=1
    f[p]+=x

ans=[0 for _ in range(N)]
visited=[False]*N
def c(i,p):
    if visited[i]:
        return 
    visited[i]=True
    w=f[i]+p
    ans[i]=w
    ch=graph[i]
    for x in ch:
        c(x,w)

c(0,0)
print(*ans)
import math

n = int(raw_input())

def vector_rot(vector, radian):
	new_x = vector[0] * math.cos(radian) - vector[1] * math.sin(radian)
	new_y = vector[0] * math.sin(radian) + vector[1] * math.cos(radian)
	return [new_x, new_y]

def vector_add(v1, v2):
	new_v = []
	for i in range(2):
		add = v1[i] + v2[i]
		new_v.append(add)
	return new_v

def makepoint(p_A1, p_A2):
	x_p_C1 = (p_A2[0] - p_A1[0]) / 3 + p_A1[0]
	y_p_C1 = (p_A2[1] - p_A1[1]) / 3 + p_A1[1]
	x_p_C3 = (p_A2[0] - p_A1[0]) * 2 / 3 + p_A1[0]
	y_p_C3 = (p_A2[1] - p_A1[1]) * 2 / 3 + p_A1[1]

	p_C1 = [x_p_C1, y_p_C1]
	p_C3 = [x_p_C3, y_p_C3]

	v01 = [x_p_C1 - p_A1[0], y_p_C1 - p_A1[1]]
	rot_v01 = vector_rot(v01, 60 * math.pi / 180)
	v02 = vector_add(v01, rot_v01)
	p_C2 = vector_add(p_A1, v02)	
	
	C = [p_C1, p_C2, p_C3]
	return C

A = [[0., 0.],[100., 0.]]
for i in range(n):
	B = []
	for j in range(len(A)-1): #A[j]??¨A[j+1]
		C = makepoint(A[j], A[j+1]) #C = [p_C1, p_C2, p_C3]
		B.append(A[j])
		for p in C:
			B.append(p)
		#A[j+1]????¬????makepoint?????????append??????
	B.append(A[-1]) #??????????????\?????\??????
	A = B

for x in A:
	print("{:.8f}".format(x[0])),
	print("{:.8f}".format(x[1]))

### FIXED CODE ###

# coding: utf-8

import math

n = int(raw_input())

def vector_rot(vector, radian):
	new_x = vector[0] * math.cos(radian) - vector[1] * math.sin(radian)
	new_y = vector[0] * math.sin(radian) + vector[1] * math.cos(radian)
	return [new_x, new_y]

def vector_add(v1, v2):
	new_v = []
	for i in range(2):
		add = v1[i] + v2[i]
		new_v.append(add)
	return new_v

def makepoint(p_A1, p_A2):
	x_p_C1 = (p_A2[0] - p_A1[0]) / 3 + p_A1[0]
	y_p_C1 = (p_A2[1] - p_A1[1]) / 3 + p_A1[1]
	x_p_C3 = (p_A2[0] - p_A1[0]) * 2 / 3 + p_A1[0]
	y_p_C3 = (p_A2[1] - p_A1[1]) * 2 / 3 + p_A1[1]

	p_C1 = [x_p_C1, y_p_C1]
	p_C3 = [x_p_C3, y_p_C3]

	v01 = [x_p_C1 - p_A1[0], y_p_C1 - p_A1[1]]
	rot_v01 = vector_rot(v01, 60 * math.pi / 180)
	v02 = vector_add(v01, rot_v01)
	p_C2 = vector_add(p_A1, v02)	
	
	C = [p_C1, p_C2, p_C3]
	return C

A = [[0., 0.],[100., 0.]]
for i in range(n):
	B = []
	for j in range(len(A)-1): #A[j]??¨A[j+1]
		C = makepoint(A[j], A[j+1]) #C = [p_C1, p_C2, p_C3]
		B.append(A[j])
		for p in C:
			B.append(p)
		#A[j+1]????¬????makepoint?????????append??????
	B.append(A[-1]) #??????????????\?????\??????
	A = B

for x in A:
	print("{:.8f}".format(x[0])),
	print("{:.8f}".format(x[1]))
import sys
import numpy as np
from scipy.sparse.csgraph import shortest_path

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N = ir()
graph = np.zeros((N, N)) #0-indexed
for _ in range(N-1):
    a, b, c = lr()
    graph[a-1, b-1] = c

dist = shortest_path(graph, directed=False).astype(int)
Q, K = lr()
for _ in range(Q):
    x, y = lr()
    print(dist[x-1, K-1] + dist[K-1, y-1])

# 20

### FIXED CODE ###

import sys
from collections import deque
sys.setrecursionlimit(10 ** 7)

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

def measure(x):
    dist = [INF] * (N)
    dist[x] = 0
    que = deque()
    que.append(x)
    while que:
        cur = que.popleft()
        cur_dist = dist[cur]
        for next, c in edges[cur]:
            if dist[next] == INF:
                dist[next] = cur_dist + c
                que.append(next)
    
    return dist

N = ir()
INF = 10 ** 15
edges = [[] for _ in range(N)]
for _ in range(N-1):
    a, b, c = lr()
    edges[a-1].append((b-1, c))
    edges[b-1].append((a-1, c))

Q, K = lr()
dist = measure(K-1)
for _ in range(Q):
    x, y = lr()
    print(dist[x-1] + dist[y-1])


# 20
n=int(input())
lw=[input() for i in range(n)]
used=set()
last=lw[0][0]
for w in lw:
    if w in used or last !=w[0]:
        cond=False
        break
    used.add(w)
    last=w[-1]
print("Yes" if cond else "No")

### FIXED CODE ###

n=int(input())
lw=[input() for i in range(n)]
used=set()
cond=True
last=lw[0][0]
for w in lw:
    if w in used or last !=w[0]:
        cond=False
        break
    used.add(w)
    last=w[-1]
print("Yes" if cond else "No")
a,b,k = map(int,input().split())
c=2
d=0
while d<k:
  if a%c==0 and b%c==0:
	d+=1
  else
	c+=1
print(d)

### FIXED CODE ###

temp = []
a,b,k = map(int,input().split())
for i in range (1, min(a,b)+1):
  if a%i==0 and b%i==0:
    temp.append(i)
print(temp[-k])
size = int(input())
a,b = list(map(int,input().split()))
ans = ""
for i in size:
  ans += a[i]
  ans += b[i]
print(ans)

### FIXED CODE ###

size = int(input())
a,b = list(map(str,input().split()))
ans = ""
for i in range(size):
  ans += a[i]
  ans += b[i]
print(ans)
# coding: utf-8
import sys
#from operator import itemgetter
sysread = sys.stdin.readline
read = sys.stdin.read
#from heapq import heappop, heappush
#from collections import defaultdict
sys.setrecursionlimit(10**7)
#import math
#from itertools import combinations, product
#import bisect# lower_bound etc
#import numpy as np
def run():
    N = int(input())
    ret = 0
    for _ in range(N):
        S = sysread()
        for s in S:
            ret += dic[s]
    if ret == 0:
        print('Yes')
    else:
        print('No')
if __name__ == "__main__":
    run()

### FIXED CODE ###

# coding: utf-8
import sys
from operator import itemgetter
sysread = sys.stdin.readline
read = sys.stdin.read
from heapq import heappop, heappush
#from collections import defaultdict
sys.setrecursionlimit(10**7)
#import math
#from itertools import combinations, product
#import bisect# lower_bound etc
#import numpy as np
#import queue# queue,get(), queue.put()
def run():
    N = int(input())
    current = 0
    ways = []
    dic = {'(': 1, ')': -1}
    SS = read().split()

    for S in SS:
        path = [0]
        for s in S:
            path.append(path[-1]+ dic[s])
        ways.append((path[-1], min(path)))

    ways_pos = sorted([(a,b) for a,b in ways if a >= 0], key = lambda x:x[0], reverse=True)
    ways_neg = sorted([(a,b) for a,b in ways if a < 0], key = lambda x:(x[0] - x[1]), reverse=True)

    tmp = []
    for go, max_depth in ways_pos:
        if current + max_depth >= 0:
            current += go
        else:
            tmp.append((go, max_depth))

    for go, max_depth in tmp:
        if current + max_depth >= 0:
            current += go
        else:
            print('No')
            return None

    tmp =[]
    for go, max_depth in ways_neg:
        if current + max_depth >= 0:
            current += go
        else:
            tmp.append((go, max_depth))

    for go, max_depth in tmp:
        if current + max_depth >= 0:
            current += go
        else:
            print('No')
            return None


    if current == 0:
        print('Yes')
    else:
        print('No')

if __name__ == "__main__":
    run()
from operator import mul
from functools import reduce
 
n = int(input())
lis = list(map(int, input().split()))
 
ans = reduce(mul, lis)
 
if ans > 10**18:
    print("-1")
else:
    print(ans)

### FIXED CODE ###

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

print(a*b)
n,m=map(int,input().split())
a = [0]*n
if m!=0:
  for i in range(m):
    a[int(input())-1] = 1

s=[0]*n
if a[0]==1 and a[1] == 1:
  s[0] = 0
  s[1] = 0
elif a[0] == 1:
  s[0] = 0
  s[1] = 1
elif a[1] == 1:
  s[0] = 1
  s[1] = 0
else:
  s[0] = 1
  s[1] = 2
  
for i in range(2,n):
  #壊れている
  if a[i] == 1:
    s[i] = 0
  else:
    s[i] = s[i-1]+s[i-2]
print(s[n-1]%1000000007)


### FIXED CODE ###

n,m=map(int,input().split())
a = [0]*n
if n!=1:
  if m!=0:
    for i in range(m):
      a[int(input())-1] = 1

  s=[0]*n
  if a[0]==1 and a[1] == 1:
    s[0] = 0
    s[1] = 0
  elif a[0] == 1:
    s[0] = 0
    s[1] = 1
  elif a[1] == 1:
    s[0] = 1
    s[1] = 0
  else:
    s[0] = 1
    s[1] = 2

  for i in range(2,n):
    #壊れている
    if a[i] == 1:
      s[i] = 0
    else:
      s[i] = s[i-1]+s[i-2]
  print(s[n-1]%1000000007)
else:
  print(1)