Harryxun/stf_run huggingface dataset:
N = int(input())
A = list(map(int, input().split()))
wh = int(input())
A[wh] = int(input())
ans = 1
for i in range(max(A), 0, -1):
    if all([x % i == 0 for x in A]):
        ans = i
        break
print(ans)

### FIXED CODE ###

from fractions import gcd

N = int(input())
As = list(map(int, input().split()))
re_As = As[::-1]
gcds = [0]*(N)
rgcds = [0]*(N)
gcds[0] = 0
rgcds[0] = 0

for i in range(N-1):
    gcds[i+1] = gcd(As[i], gcds[i])
    rgcds[i+1] = gcd(re_As[i], rgcds[i])

rgcds = rgcds[::-1]

ans = [0]*(N+1)

for i, (s, r) in enumerate(zip(gcds, rgcds)):
    ans[i] = gcd(s, r)
    
print(max(ans))
t = [0]
x = [0]
y = [0]
N = int(input())
for i in range(1,N+1):
  ti,xi,yi = map(int,input(),split())
  t.append(ti)
  x.append(xi)
  y.append(yi)
  
flg = True

for i in range(0,N):
  if t[i+1]-t[i] <　x[i+1]-x[i] + y[i+1]-y[i]:
    flg = False
  else (t[i+1]-t[i])%2 - (x[i+1]-x[i] + y[i+1]-y[i])%2==0:
    flg = False
if (flg):
  print('Yes')
else:
  print('No')


### FIXED CODE ###

a,b = map(int, input().split())
if a*b % 2 == 0:
  print("Even")
else:
  print("Odd")
a, b = input().split()
print("Yes") if a % 2 != 0 and b % 2 != 0 else print("No")
                 

### FIXED CODE ###

a, b = map(int, input().split())
print("Yes") if a % 2 != 0 and b % 2 != 0 else print("No")
# D TLE
import bisect

N = map(int, input().split())
L = list(map(int, input().split()))

L.sort()
value = 0


for i in range(0, len(L)-2)
    for j in range(i, len(L)-1)
    index = bisect.bisect_left(L[j+1:], L[i]+L[j])
    value+=index

print(value)

### FIXED CODE ###

n = int(input())
l = list(map(int, input().split()))
l.sort()
 
from bisect import bisect_left
ans = 0
for i in range(n):
  for j in range(i+1, n):
    t = l[i]+l[j]
    idx = bisect_left(l, t)
    ans += max(0, idx-j-1)
print(ans)
keys = input()
outputs = []

for key in keys:
    if key == 'B':
        if outputs == None:
            pass
        else:
            outputs.pop()
    else:
        outputs.append(key)

for output in outputs:
    print(output, end='')


### FIXED CODE ###

keys = input()
outputs = []

for key in keys:
    if key == 'B':
        if not outputs:
            pass
        else:
            outputs.pop()
    else:
        outputs.append(key)

for output in outputs:
    print(output, end='')
n = input()
num = []
num = input().split(" ")

t = 0
while True:
  num2 = []
  for i in num:
    if i%2 !=0:
      break
    num2.append(i/2)
   t+=1
   num = num2
prtin(t)

### FIXED CODE ###

n = input()
num = []
num = input().split(" ")
 
t = 0 
a=0 
while True: 
  num2=[] 
  for i in num: 
    if not int(i)%2==0:
      a =1
      break
    num2.append(int(i)/2)
  num = num2
  if a==1: 
     print(t) 
     break
  t+=1
import sys
n,m,x= map(int, sys.stdin.readline().rstrip("\n").split())
lines = [ [int(s) for s in line.split()] for line in sys.stdin ]
n = 3
price = -1
skill = {}
for i in range(2**n-1):
    i = format(i+1, 'b').zfill(n)
    price_ = 0
    for j in range (m):
        skill[j] = 0
    for j in range(n):
        if i[j] == '1':
            price_  += lines[j][0]
            for k in range(m):
                skill[k] = skill[k] + lines[j][k+1]
        for k in range(m):
            if skill[k] < x:
                break
        else:
            if price == -1:
                price = price_
            else:
                price = min(price, price_)
print(price)

### FIXED CODE ###

import sys
s = sys.stdin.readline().rstrip("\n")
t = sys.stdin.readline().rstrip("\n")
if s == t[:-1]:
    print('Yes')
else:
    print('No')
#ABC157 Guess the number
n,m = map(int,input().split())
sc = [input().split() for _ in range(m)]
if len(sc) == 0:
    print('0')
    exit()

if n == 1:
    ans = []
    for i in range(m):
        ans.append(sc[m][1])
    if set(ans) == 1:
        print(ans[0])
    else:
        print('-1')
    exit()
    
for i in range(10**(n-1),10**n):
    cnt = 0
    seq = str(i)
    for j in range(m):
        if seq[int(sc[j][0])-1] == sc[j][1]:
            cnt += 1
    if cnt == m:
        ans.append(i)
        
if len(ans) == 0:
    print('-1')
else:
    print(min(ans))

### FIXED CODE ###

#ABC157 Guess the number
n,m = map(int,input().split())
sc = [input().split() for _ in range(m)]
ans =[]

if n == 1:
    if len(sc)== 0:
        print('0')
    else:
        ans = []
        for i in range(m):
            ans.append(sc[i][1])
        if len(set(ans)) == 1:
            print(ans[0])
        else:
            print('-1')
    
else:
    for i in range(10**(n-1),10**n):
        cnt = 0
        seq = str(i)
        for j in range(m):
            if seq[int(sc[j][0])-1] == sc[j][1]:
                cnt += 1
        if cnt == m:
            ans.append(i)
        
    if len(ans) == 0:
        print('-1')
    else:
        print(min(ans))
import numpy as np
import math
import sys
sys.float_info.max
H, W, A, B = map(int, input().split())

StoB = np.zeros(B)
BtoE = np.zeros(B)
sum = 0
# print(StoB)
# print(BtoE)
all = math.factorial(H + W - 2) / (math.factorial(H - 1) * math.factorial(W - 1))
# print(all)
for i in range(B):
    # print(i)
    # print(H - A)
    StoB[i] = math.factorial((i) + (H - A - 1)) / (math.factorial(i) * math.factorial(H - A - 1))
    # print(StoB[i])
# print(StoB)
    BtoE[i] = math.factorial((W - i) + A - 2) / (math.factorial(W - i - 1) * math.factorial(A - 1))
# print(BtoE)
    sum += StoB[i] * BtoE[i]
print(all - sum)


### FIXED CODE ###

h,w,a,b = map(int,input().split()) #入力
mod = 10**9+7 

#前計算　f[i] = i!
f=[1]
for i in range(h+w):
  f.append(f[i]*(i+1)%mod)

#C(n,r,p) = ( n! * r!^(p-2) * (n-r)!^(p-2) )%p
#pythonだとpow()で二分累乗法が使える
def comb(n, r, p):
  return f[n] * pow(f[r], p-2, p) * pow(f[n-r], p-2, p) % p

#紫点ごとに調べる
ans=0
for i in range(b, w):
  ans+= comb(h+i-a-1, i, mod) * comb(w-1-i+a-1, a-1, mod) % mod

print(ans%mod)
# -*- coading: utf-8 -#-

A = raw_input()
B = A.split( )
C, D = int(B[0]), int(B[1])

if C == 1:
  if D == 1:
    print 'Draw'
  else:
    print 'Alice'
else:
  if D == 1:
    print 'Bob'
  else:
    if C ＞ D:
      print 'Alice'
    elif C ＜ D:
      print 'Bob'
    else:
      print 'Draw'

### FIXED CODE ###

# -*- coading: utf-8 -#-

A = raw_input()
B = A.split( )
C, D = int(B[0]), int(B[1])

if C == 1:
  if D == 1:
    print 'Draw'
  else:
    print 'Alice'
else:
  if D == 1:
    print 'Bob'
  else:
    if C > D:
      print 'Alice'
    elif C < D:
      print 'Bob'
    else:
      print 'Draw'
import math
N = input()
answers = []
for val in range(1,N+1):
	x = map(float,raw_input().split(' '))
	d = math.sqrt((x[0]-x[3])**2 - (x[1]-x[4])**2))
	math.fabs(d)
	s = x[2]+x[5]
	r = math.fabs(x[2]-x[5])
	if r >= d:
		if x[2] > x[5]:
			answers.append(2)
		else:
			answers.append(-2)
	elif s < d:
		answers.append(0)
	else:
		answers.append(1)
for val in answers:
	print val

### FIXED CODE ###

import math
N = input()
answers = []
for val in range(1,N+1):
	x = map(float,raw_input().split(' '))
	d = (x[0]-x[3])**2 + (x[1]-x[4])**2
	d = math.sqrt(d)
	d = math.fabs(d)
	s = x[2]+x[5]
	r = math.fabs(x[2]-x[5])
	if d > s:
		answers.append(0)
	elif d == s:
		answers.append(1)
	elif d < s:
		if d < r:
			if x[2] > x[5]:
				answers.append(2)
			else:
				answers.append(-2)
		elif d >= r:
			answers.append(1)
for val in answers:
	print val
a,b,c=map(int,input().split())
l=list(range(a,a+c))
m=list(range(b,a-1,-1))
ans=sorted(set(l+m))
print(*ans)

### FIXED CODE ###

a,b,c=map(int,input().split()) 
l=list(range(a,b+1)[0:c]) 
m=list(range(b,a-1,-1)[0:c]) 
ans=sorted(set(l+m)) 
print(*ans)
h1,m1,h2,m2,K = map(int,input().split() )

H=h2-h1
M=m2-m1

if(M<0):
  m += 60
  H -= 1
if(H<0):
  H += 24

M += H*60

ans =M - K
print(ans)

### FIXED CODE ###

h1,m1,h2,m2,K = map(int,input().split() )

H=h2-h1
M=m2-m1

if(M<0):
  M += 60
  H -= 1
if(H<0):
  H += 24

M += H*60

ans =M - K
print(ans)
import networkx as nx

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

index = lambda i,j: i*M+j

G=nx.DiGraph()
dx,dy=(1,0),(0,1)
for i in range(N):
  for j in range(M):
    if S[i][j]=='#':continue
    if (i+j) %2:
      G.add_edge(index(i,j),'sink',capacity=1)
    else:
      G.add_edge('source',index(i,j),capacity=1)
    for k in range(2):
      v1,v2 = index(i,j),index(i+dx[k],j+dy[k])
      if not (0<=i+dx[k]<N and 0<=j+dy[k]<M):
        continue
      if S[i+dx[k]][j+dy[k]] == '#': continue
      if (i+j) % 2:
        G.add_edge(v2,v1,capacity=1)
      else:
        G.add_edge(v1,v2,capacity=1)
        
flow_value, flow_dict = nx.maximum_flow(G, "source", "sink")

ans = [list(S[i]) for i in range(N)]
for v1 in flow_dict.keys():
  if v1 in ('sink','source'):continue
  for v2 in flow_dict[v1]:
    if v2 in ('sink','source'):continue
    if flow_dict[v1][v2]:
      i1,j1 = divmod(v1,M)
      i2,j2 = divmod(v2,M)
      (i1,j1),(i2,j2) = sorted([(i1,j1),(i2,j2)])
      if i1 + 1 == i2:
        ans[i1][j1] = 'v'
        ans[i2][j2] = '^'
      else:
        ans[i1][j1] = '>'
        ans[i2][j2] = '<'

print(flow_value)
for i in range(N):
  print(''.join(ans[i]))

### FIXED CODE ###

import networkx as nx

def main():
  N,M=map(int,input().split())
  S = [list(input()) for _ in range(N)]

  if N == M == 1:
    print(0)
    print(S[0][0])
    exit()

  index = lambda i,j: i*M+j

  G=nx.Graph()
  dx,dy=(1,0),(0,1)
  E = []
  n1,n2 = [],[]
  for i in range(N):
    for j in range(M):
      if S[i][j]=='#':continue
      if (i+j)%2:
        n1.append(index(i,j))
      else:
        n2.append(index(i,j))
      for k in range(2):
        v1,v2 = index(i,j),index(i+dx[k],j+dy[k])
        if not (0<=i+dx[k]<N and 0<=j+dy[k]<M):
          continue
        if S[i+dx[k]][j+dy[k]] == '#': continue
        if (i+j) % 2:
          E.append((v2,v1))
        else:
          E.append((v1,v2))
     
  G.add_nodes_from(n1, bipartite=0)
  G.add_nodes_from(n2, bipartite=1)
  G.add_edges_from(E)
  u = [n for n in G.nodes if G.nodes[n]['bipartite'] == 0]
  match = nx.bipartite.maximum_matching(G, top_nodes=u)
  match = {v:match[v] for v in match.keys() if v < match[v]}
 
  for v in match.keys():
    i1,j1 = divmod(v,M)
    i2,j2 = divmod(match[v],M)
    if i1 > i2 or j1 > j2:
      (i1,j1),(i2,j2) = (i2,j2),(i1,j1)
    if i1 < i2:
      S[i1][j1] = 'v'
      S[i2][j2] = '^'
    else:
      S[i1][j1] = '>'
      S[i2][j2] = '<'

  print(len(match))
  for i in range(N):
    print(''.join(S[i]))
    
if __name__ == "__main__":
  main()
N, M=int(input())
if N==M:
	print('Yes')
else:
	print('No')

### FIXED CODE ###

N, M=map(int,input().split())
if N==M:
	print('Yes')
else:
	print('No')
from collections import defaultdict
from math import gcd

def main():
    n = int(input())
    mod = 1000000007
    zeroes = 0
    quad1 = defaultdict(int)
    quad2 = defaultdict(int)
    for _ in range(n):
        a, b = map(int, input().split())
        g = gcd(a, b)
        a, b = a//g, b//g

        if a == b == 0:
            zeroes += 1
            continue
        
        if b < 0:
            a, b = -a ,-b
        
        if a <= 0:
            a, b = b, -a
            quad1[(a,b)] += 0
            quad2[(a,b)] += 1
        else:
            quad1[(a,b)] += 1
            quad2[(a,b)] += 0

    ans = 1
    for k, v in quad1.items():
        now = 1
        now += pow(2, v, mod) -1
        now += pow(2, quad2[k], mod) -1
        ans = ans * now % mod
    
    ans += (zeroes - 1)
    print (ans % mod)


if __name__ == '__main__':
    main()

### FIXED CODE ###

from collections import defaultdict
import math


def main():
    n = int(input())
    mod = 1000000007
    zeroes = 0
    counter1 = defaultdict(int)
    counter2 = defaultdict(int)
    for _ in range(n):
        x, y = [int(x) for x in input().split()]

        if x == y == 0:
            zeroes += 1
            continue

        denominator = math.gcd(x, y)
        x, y = x // denominator, y // denominator

        if y < 0:
            # quadrant III, IV -> I, II
            x, y = -x, -y

        if x <= 0:
            # round 90° from quadrant II to I
            x, y = y, -x
            counter2[(x, y)] += 1
            counter1[(x, y)] += 0
        else:
            counter1[(x, y)] += 1
            counter2[(x, y)] += 0

    ans = 1
    for k, v in counter1.items():
        now = 1
        now += pow(2, v, mod) - 1
        now += pow(2, counter2[k], mod) - 1
        ans = ans * now % mod
    ans += zeroes
    ans -= 1  # choose no fish
    return ans % mod


if __name__ == '__main__':
    print(main())
https://onlinejudge.u-aizu.ac.jp/courses/lesson/2/ITP1/7/ITP1_7_D


### FIXED CODE ###

string=input()
print(string.swapcase())
n,m=map(int,input().split())
l=list(map(int,input().split()))
l.sort(reverse=True)
print(l[0]+l[1]+l[2])

### FIXED CODE ###

n,m=map(int,input().split())
l=list(map(int,input().split()))
l.sort(reverse=True)
ans=0
for i in range(m):
  ans+=l[i]
print(ans)
n = int(input())
x = list(map(int, input().split()))
y = sorted(set(x))
ms = y[len(y)//2-1]
mc = y[len(y)//2]
ml = y[len(y)//2+1]

#print(ms, mc, ml)

for i in range(n):
    if x[i] == mc:
        print(ml)
    if x[i] < mc:
        print(ml)
    elif x[i] > mc:
        print(mc)

### FIXED CODE ###

n = int(input())
x = list(map(int, input().split()))
xx = sorted(x, reverse=True)

m1 = xx[n//2-1]
m2 = xx[n//2]

# print(m1, m2)

for i in range(len(x)):
    if x[i] < m1:
        print(m1)
    elif x[i] >= m1:
        print(m2)
n,m = map(int, input().split())
xy = [map(int,input().split()) for _ in range(m)]
x,y = [list(i) for i in zip(*xy)]
l = x + y


d = {}
for i in l:
    if i not in d:
        d[i] = 0
    d[i] += 1

for j in range(1,n+1):
    print(d[j])



### FIXED CODE ###


n,m = map(int, input().split())
xy = [map(int,input().split()) for _ in range(m)]
x,y = [list(i) for i in zip(*xy)]
l = x + y


d = {}#辞書同士の追加はupdate

for i in range(1,n+1):
    d[i] =0
for i in l:
    if i not in d:
        d[i] = 0
    d[i] += 1

for j in range(1,n+1):
    print(d[j])
import collections

n,m = map(int,raw_input().split(' '))
operations = [map(int, raw_input().split(' ')) for _ in range(m)]
def f(operations, n):

	res = [-1] * n

	for s,c in operations:
		s -=1
		if res[s] == -1:
			res[s] = c
		elif res[s] != c:
			return (False, res)
	
	for i in range(len(res)):
		if res[i] == -1: 
			res[i] = 0 if (i or n == 1) else 1
	
	if res[0] == 0 and n:
		return (false, res)
	return (True, res)
f,r = f(operations, n)
print ''.join(map(str,r)) if f else -1

### FIXED CODE ###

import collections

n,m = map(int,raw_input().split(' '))
operations = [map(int, raw_input().split(' ')) for _ in range(m)]
def f(operations, n):

	res = [-1] * n

	for s,c in operations:
		s -=1
		if res[s] == -1:
			res[s] = c
		elif res[s] != c:
			return (False, res)
	
	for i in range(len(res)):
		if res[i] == -1: 
			res[i] = 0 if (i or n == 1) else 1
	
	if res[0] == 0 and n > 1:
		return (False, res)
	return (True, res)
f,r = f(operations, n)
print ''.join(map(str,r)) if f else -1
num_room, num_path = map(int, input().split())

room_paths_dict = {}

start_room_to_paths = []
result = {}

for i in range(num_path):
    a_room_num, b_room_num = map(int, input().split())
    if a_room_num == 1:
        start_room_to_paths.append(b_room_num)
        continue
    elif b_room_num == 1:
        start_room_to_paths.append(a_room_num)
        continue

    result[a_room_num] = 0
    result[b_room_num] = 0

    if not room_paths_dict.get(a_room_num):
        room_paths_dict[a_room_num] = set()
    if not room_paths_dict.get(b_room_num):
        room_paths_dict[b_room_num] = set()

    room_paths_dict[a_room_num].add(b_room_num)
    room_paths_dict[b_room_num].add(a_room_num)

def search(room_num):
    targets = []
    for next_room in room_paths_dict[room_num]:
        if result[next_room] == 0:
            result[next_room] = room_num
            targets.append(room_num)

    for t in targets:
        search(t)

for start_room_to_path in start_room_to_paths:
    result[start_room_to_path] = 1
    search(start_room_to_path)

if 0 in result.values():
    print('No')
else:
    print('Yes')
    for key in sorted(result.keys()):
        print(result[key])


### FIXED CODE ###


from collections import deque

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

links = [[] for _ in range(N + 1)]
for a, b in AB:
    links[a].append(b)
    links[b].append(a)

result = [-1] * (N + 1)
q = deque([1])
while q:
    i = q.popleft()
    for j in links[i]:
        if result[j] == -1:
            result[j] = i
            q.append(j)
print('Yes')
print('\n'.join(str(i) for i in result[2:]))
n, x = map(int, input().split())
l = list(map(int, input().split()))

dist, count = 0, 1
for i in range(n+1):
    dist += l[i]
    if dist > x:
        break
    count += 1

print(count)

### FIXED CODE ###

n, x = map(int, input().split())
l = list(map(int, input().split()))

dist, count = 0, 1
for i in range(n):
    dist += l[i]
    if dist > x:
        break
    count += 1

print(count)
N=int(input())
L=sorted(list(map(int,input().split())))
L=list(set(L))
count=0
d2breakFlag=False
for d1 in range(N-2):
    for d2 in range(d1+1,N-1):
        Temp=L[d1]+L[d2]

        """for d3 in range(d2+1,N):
            if L[d1]+L[d2]>L[d3]:count+=1
            else:break
print(count)"""


### FIXED CODE ###

import bisect
N=int(input())
L=sorted(list(map(int,input().split())))
count=0
for d1 in range(N-2):
    for d2 in range(d1+1,N-1):
        m=bisect.bisect_left(L,L[d1]+L[d2])
        if m>d2+1:count+=m-(d2+1)
print(count)
t=[]
for i in range(3):
    t.append([int(i) for i in input().split()])
n = int(input())
lst = []
for i in range(n):lst+=[int(input())]
def ligne(i):
    for j in range(3):
        if not t[i][j] in lst:
            return False
    return True
def col(j):
    for i in range(3):
        if not t[i][j] in lst:
            retur False
    return True
def diag1():
    for i in range(3):
        if not t[i][i] in lst:
            return False
    return True
def diag2():
    for i in range(3):
        if not t[2-i][i] in lst:
            return False
    return True
ans = False
for i in range(3):
    ans = ans or ligne(i)
    ans = ans or col(i)
ans  = ans or diag1() or diag2()
if ans :
    print("Yes")
else:
    print("No")

### FIXED CODE ###

t=[]
for i in range(3):
    t.append([int(i) for i in input().split()])
n = int(input())
lst = []
for i in range(n):lst+=[int(input())]
def ligne(i):
    for j in range(3):
        if not t[i][j] in lst:
            return False
    return True
def col(j):
    for i in range(3):
        if not t[i][j] in lst:
            return False
    return True
def diag1():
    for i in range(3):
        if not t[i][i] in lst:
            return False
    return True
def diag2():
    for i in range(3):
        if not t[2-i][i] in lst:
            return False
    return True
ans = False
for i in range(3):
    ans = ans or ligne(i)
    ans = ans or col(i)
ans  = ans or diag1() or diag2()
if ans :
    print("Yes")
else:
    print("No")
N = int(input())
XY = [[int(_) for _ in input().split()] for i in range(N)]

dxy = [
    (XY[i][0] - XY[j][0], XY[i][1] - XY[j][1])
    for i in range(N)
    for j in range(i + 1, N)
]

from collections import Counter

cdxy = Counter(dxy)

m = max(cdxy.values())

#print(XY)
#print(dxy)
#print(cdxy)

result = N - m
print(result)


### FIXED CODE ###

N = int(input())
XY = [tuple([int(_) for _ in input().split()]) for i in range(N)]

if N == 1:

    result = 1

else:

    XY.sort()

    dxy = [
        (XY[i][0] - XY[j][0], XY[i][1] - XY[j][1])
        for i in range(N)
        for j in range(i + 1, N)
    ]

    from collections import Counter

    cdxy = Counter(dxy)

    m = max(cdxy.values())

    #print(XY)
    #print(dxy)
    #print(cdxy)

    result = N - m

print(result)
'''input
2 2
3
2 1 1
'''
from collections import Counter
n = input()
arr = map(int,raw_input().split())
c = Counter(arr)
li = []
for i in c:
	if c[i] >= 2:
		li.append(i)
li.sort()
if li == 1:
	print li[-1]**2
else:
	if c[li[-1]] >= 4:
		print li[-1]**2
	else:
		print li[-1]*li[-2]

### FIXED CODE ###

'''input
4
1 2 3 4
'''
from collections import Counter
n = input()
arr = map(int,raw_input().split())
c = Counter(arr)
li = []
for i in c:
	if c[i] >= 2:
		li.append(i)
li.sort()
if len(li) == 0:
	print "0"
elif len(li) == 1:
	print li[-1]**2
else:
	if c[li[-1]] >= 4:
		print li[-1]**2
	else:
		print li[-1]*li[-2]
import numpy as np

def main():
    H, W = list(map(int, input().split()))
    s = [input() for i in ragne(H)]
    ans = []
    for row in s:
        ans.append(list(row))
    
    diffs = [(-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1)]
    for y in range(H):
        for x in range(W):
            if s[y][x] == '#': continue
            count = 0
            for dx, dy in diffs:
                if (x + dx < 0) or (x + dx > W - 1) or (y + dy < 0) or (y + dy > H - 1):
                    continue
                if s[y + dy][x + dx] == '#':
                    count += 1
            ans[y][x] = str(count)
    
    for i in range(H):
        print(''.join(ans[i]))

if __name__ == "__main__":
    main()

### FIXED CODE ###

import numpy as np

def main():
    H, W = list(map(int, input().split()))
    s = [input() for i in range(H)]
    ans = []
    for row in s:
        ans.append(list(row))
    
    diffs = [(-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1)]
    for y in range(H):
        for x in range(W):
            if s[y][x] == '#': continue
            count = 0
            for dx, dy in diffs:
                if (x + dx < 0) or (x + dx > W - 1) or (y + dy < 0) or (y + dy > H - 1):
                    continue
                if s[y + dy][x + dx] == '#':
                    count += 1
            ans[y][x] = str(count)
    
    for i in range(H):
        print(''.join(ans[i]))

if __name__ == "__main__":
    main()
import math
a = float(input())
b = float(input())
h = float(input())
m = float(input())
t1 = h/12 * math.PI
t2 = m/60 * math.PI
dellt = abs(t2 - t1)
ans = b*b + a*a - 2*a*b*math.cos(dellt)
print(ans**0.5)

### FIXED CODE ###

import math
s = input()
a,b,h,m = s.split(' ')
a = int(a)
b = (int)(b)
h = int(h)
m = int(m)
t1 = h/6 * math.pi + math.pi/6*m/60
t2 = m/30 * math.pi
dellt = abs(t2 - t1)
ans = b*b + a*a - 2*a*b*math.cos(dellt)
print(ans**0.5)
k = str(input())
list = []
def lunlun(k):
    for i in range(len(k) - 1):
        if abs(int(k[i]) - int(k[i+1])) > 1:
            return False
            break
    return True

i = 1
while len(list) == int(k):
    if lunlun(i):
        list.append(i)
    i += 1

print(lunlun(len(k)))

### FIXED CODE ###

import sys
sys.setrecursionlimit(10**7) #再帰関数の呼び出し制限

K = int(input())

l = [1,2,3,4,5,6,7,8,9] #１桁のルンルン数
ln = [1,2,3,4,5,6,7,8,9] #既にわかっている分をセット。重複がいやなのでset

def dfs(i):
    n = int(str(i)[-1]) #1桁目を取る
    a,b,c = n-1, n, n+1 #差の絶対値が１未満、-1/+0/+1
    if a != -1:
        ln.append(i*10+a)
    ln.append(i*10+b)
    if c != 10:
        ln.append(i*10+c)
    
    if len(str(i)) <= 10:
        if a != -1:
            dfs(i*10 + a)
        dfs(i*10 + b)
        
        if c != 10:
            dfs(i*10 + c)

for i in l: #１桁のルンルン数を順番に再帰
    dfs(i)

print(sorted(ln)[K-1]) #0-indexを意識してリストのK-1番目を出す
N = int(input())

tree = {}

for _ in range(N-1):
  A, B, C = [int(i) for i in input().split()]
  if A not in tree:
    tree[A] = []
  if B not in tree:
    tree[B] = []
  tree[A].append((B, C))
  tree[B].append((A, C))


c = {}
q = []
visited = set()

def dfs(a, acc):
  c[a] = acc
  for n, cost in tree[a]:
    if n not in visited:
      visited.add(n)
      dfs(n, acc + cost)


Q, K = [int(i) for i in input().split()]
visited.add(K)
c[K] = 0
dfs(K, 0)

for _ in range(Q):
  x, y = [int(i) for i in input().split()]
  print(c[x] + c[y])


### FIXED CODE ###

N = int(input())

tree = {}

for _ in range(N-1):
  A, B, C = [int(i) for i in input().split()]
  if A not in tree:
    tree[A] = []
  if B not in tree:
    tree[B] = []
  tree[A].append((B, C))
  tree[B].append((A, C))


c = {}
q = []
visited = set()

def dfs(a, acc):
  q.append((a, acc))

  while q:
    n, r = q.pop()
    c[n] = r
    visited.add(n)
    for b, cost in ((b, cost) for b, cost in tree[n] if b not in visited):
      q.append((b, cost + r))


Q, K = [int(i) for i in input().split()]
dfs(K, 0)

for _ in range(Q):
  x, y = [int(i) for i in input().split()]
  print(c[x] + c[y])