Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Runtime Error):
import sys
import numpy as np
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

N, K, S = map(int, read().split())

S_half = [S // 2, S - S // 2]

ans = []
for _ in range(N // 2 + 1):
    ans += S_half
ans = np.array(ans[:N])
i = 0
cnt = 0
for i in range(N):
    if cnt == K:
        ans[i:N] = S - 1
        idx = i
        break
    if sum(ans[i:i + 2]) == S:
        cnt += 1
        continue

if ans[idx - 1:idx + 1].sum() != S:
    for s_ in S_half:
        if ans[idx - 1] + s_ == S:
            ans[idx] = s_

print(*ans)


Fixed submission:
import sys
import numpy as np
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

N, K, S = map(int, read().split())

if S != 1:
    ans = [S - 1] * N
else:
    ans = [S + 1] * N
for i in range(K):
    ans[i] = S

print(*ans)

Buggy python submission (verdict: Runtime Error):
import sys
import numpy as np
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

N, K, S = map(int, read().split())

S_half = [S // 2, S - S // 2]

ans = []
for _ in range(N // 2 + 1):
    ans += S_half
ans = np.array(ans[:N])
i = 0
cnt = 0
for i in range(N):
    if cnt == K:
        ans[i:N] = 0
        idx = i
        break
    if sum(ans[i:i + 2]) == S:
        cnt += 1
        continue

if ans[idx - 1:idx + 1].sum() != S:
    for s_ in S_half:
        if ans[idx - 1] + s_ == S:
            ans[idx] = s_

print(*ans)


Fixed submission:
import sys
import numpy as np
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

N, K, S = map(int, read().split())

if S != 1:
    ans = [S - 1] * N
else:
    ans = [S + 1] * N
for i in range(K):
    ans[i] = S

print(*ans)
Buggy python submission (verdict: Time Limit Exceeded):
import sys
import numpy as np
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

N, M = list(map(int, readline().split()))
p = []
S = []
for i in range(M):
    _p, s = readline().decode('utf-8').split()
    p.append(int(_p))
    S.append(s)
pset = list(set(p))

AC_cnt = 0
WA_cnt = 0

for i in pset:
    S_acc = [s for (s, p_) in zip(S, p) if p_ == i]
    if "AC" in S_acc:
        AC_cnt += 1
        idx = S_acc.index("AC")
        WA_cnt += idx

print(AC_cnt, WA_cnt)

Fixed submission:
import sys
import numpy as np
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

N, M = list(map(int, readline().split()))
pS = [readline().decode('utf-8').split() for _ in range(M)]
cnt_list = [0] * (N + 1)
ACed_list = [0] * (N + 1)

for p_, s in pS:
    p_ = int(p_)

    if s == 'AC':
        if ACed_list[p_] == 0:
            ACed_list[p_] = 1
    else:
        if ACed_list[p_] == 0:
            cnt_list[p_] += 1

for i in range(N + 1):
    if ACed_list[i] == 0:
        cnt_list[i] = 0

print(sum(ACed_list), sum(cnt_list))

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import numpy as np
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

N, M = list(map(int, readline().split()))
p = []
S = []
for i in range(M):
    _p, s = readline().decode('utf-8').split()
    p.append(int(_p))
    S.append(s)
p = np.array(p)

AC_list = []
AC_cnt = 0
WA_cnt = 0

for i in range(1, N + 1):
    if i in p:
        S_acc = [s for (s, p_) in zip(S, p) if p_ == i]
        if "AC" in S_acc:
            AC_cnt += 1
            for s in S_acc:
                if s == "WA":
                    WA_cnt += 1
                else:
                    break
print(AC_cnt, WA_cnt)

Fixed submission:
import sys
import numpy as np
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

N, M = list(map(int, readline().split()))
pS = [readline().decode('utf-8').split() for _ in range(M)]
cnt_list = [0] * (N + 1)
ACed_list = [0] * (N + 1)

for p_, s in pS:
    p_ = int(p_)

    if s == 'AC':
        if ACed_list[p_] == 0:
            ACed_list[p_] = 1
    else:
        if ACed_list[p_] == 0:
            cnt_list[p_] += 1

for i in range(N + 1):
    if ACed_list[i] == 0:
        cnt_list[i] = 0

print(sum(ACed_list), sum(cnt_list))
Buggy python submission (verdict: Runtime Error):
import sys
import numpy as np
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

MOD = 1_000_000_000 + 7
N = int(readline())
C = np.array(read().split(), np.int64)

C.sort()

ans = (C * np.arange(N+1, 1, -1) % MOD).sum() % MOD
ans *= pow(2, N+N-2, MOD)

print(ans % MOD)

Fixed submission:
import sys
import numpy as np
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

MOD = 1000000000 + 7
N = int(readline())
C = np.array(read().split(), np.int64)

C.sort()

ans = (C * np.arange(N+1, 1, -1) % MOD).sum() % MOD
ans *= pow(2, N+N-2, MOD)

print(ans % MOD)

Buggy python submission (verdict: Runtime Error):
import sys
import numpy as np
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

MOD = 1_000_000_000 + 7
N = int(readline())
C = np.array(read().split(), np.int64)

C.sort()

ans = (C * np.arange(N+1, 1, -1) % MOD).sum() % MOD
ans *= pow(2, N+N-2, MOD)

print(ans % MOD)

Fixed submission:
import sys
import numpy as np
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

MOD = 1000000000 + 7
N = int(readline())
C = np.array(read().split(), np.int64)

C.sort()

ans = (C * np.arange(N+1, 1, -1) % MOD).sum() % MOD
ans *= pow(2, N+N-2, MOD)

print(ans % MOD)
Buggy python submission (verdict: Runtime Error):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

s, t = map(str, input().split())

print(f'{t}{s}')


Fixed submission:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

s, t = map(str, input().split())

print('{}{}'.format(t, s))

Buggy python submission (verdict: Runtime Error):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

s, t = map(str, input().split())

print(f'{t}{s}')


Fixed submission:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

s, t = map(str, input().split())

print('{}{}'.format(t, s))
Buggy python submission (verdict: Time Limit Exceeded):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

N = int(readline())
S = readline().decode().rstrip()

ans = 0
for i in range(1000):
    i_ = str(i).zfill(3)
    idx = 0

    for s in S:
        if s == i_[idx]:
            idx += 1
        if idx == 3:
            ans += 1
            break

print(ans)


Fixed submission:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

N = int(readline())
S = readline().decode().rstrip()

ans = 0
for i in range(1000):
    i_ = '{:>03}'.format(i)
    idx = 0

    for s in S:
        if s == i_[idx]:
            idx += 1
        if idx == 3:
            ans += 1
            break

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

N = int(readline())
S = readline().decode().rstrip()

ans = 0
for i in range(1000):
    i_ = '{:>03}'.format(i)
    idx = 0

    for s in S:
        if s == i_[idx]:
            idx += 1
        if idx == 3:
            ans += 1
            break

print(ans)


Fixed submission:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

N = int(readline())
S = readline().decode().rstrip()

ans = 0
for i in range(1000):
    i_ = '{:>03}'.format(i)
    idx = 0

    for s in S:
        if s == i_[idx]:
            idx += 1
        if idx == 3:
            ans += 1
            break

print(ans)
Buggy python submission (verdict: Runtime Error):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

S = readline().decode("utf-8").rstrip()
K = int(readline())

last = ""
cnt = 0
for s in S:
    if last == s:
         += 1
        last = ""
    else:
        last = s
        
print(cnt * K)

Fixed submission:
#! /usr/bin/env python3

import sys
import numpy as np
from itertools import groupby
int1 = lambda x: int(x) - 1
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

S = list(readline().decode().rstrip())
K = int(readline())

g = [len(list(g)) for _, g in groupby(S)]

if len(g) == 1:
    print(len(S) * K // 2)
else:
    acc = sum(l // 2 for l in g) * K
    if S[0] == S[-1] and g[0] % 2 == 1 and g[-1] % 2 == 1:
        acc += K - 1
    print(acc)

Buggy python submission (verdict: Runtime Error):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

S = int(readline().decode("utf-8").rstip())
K = int(readline())

last = ""
cnt = 0
for s in S:
    if last == s:
        cnt += 1
        last = ""
    else:
        last = s
        
print(cnt * K)


Fixed submission:
#! /usr/bin/env python3

import sys
import numpy as np
from itertools import groupby
int1 = lambda x: int(x) - 1
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

S = list(readline().decode().rstrip())
K = int(readline())

g = [len(list(g)) for _, g in groupby(S)]

if len(g) == 1:
    print(len(S) * K // 2)
else:
    acc = sum(l // 2 for l in g) * K
    if S[0] == S[-1] and g[0] % 2 == 1 and g[-1] % 2 == 1:
        acc += K - 1
    print(acc)
Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
from sys import stdin

n, m = map(int, stdin.readline().split())
a = [int(num) for num in stdin.readline().split()]

cnt_dict = {}
mtr = [stdin.readline().rstrip().split() for _ in range(m)]
for row in mtr:
    cnt_dict.setdefault(int(row[1]), 0)
    cnt_dict[int(row[1])] += int(row[0])
for a_ in a:
    cnt_dict.setdefault(a_, 0)
    cnt_dict[a_] += 1

cnt_dict = sorted(cnt_dict.items(), key=lambda x: -x[0])

cnt = 0
ans = 0

for (k, v) in cnt_dict:
    v_ = np.min([v, n - cnt])
    ans += v_ * k
    cnt += v_
    if cnt == n:
        break


print(ans)


Fixed submission:
from sys import stdin

n, m = map(int, stdin.readline().split())
a = [int(num) for num in stdin.readline().split()]

cnt_list = [(a[i], 1) for i in range(n)]
for i in range(m):
    b, c = map(int, stdin.readline().split())
    cnt_list.append((c, b))

cnt_list.sort()
cnt_list.reverse()

cnt = n
ans = 0

for (k, v) in cnt_list:
    acc_v = min(v, cnt)
    ans += k * acc_v
    cnt -= acc_v


print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np

n, m = map(int, input().split())
a = [int(num) for num in input().split()]

cnt_dict = {}
for i in range(m):
    b, c = map(int, input().split())
    cnt_dict.setdefault(c, 0)
    cnt_dict[c] += b
for a_ in a:
    cnt_dict.setdefault(a_, 0)
    cnt_dict[a_] += 1

cnt_dict = sorted(cnt_dict.items(), key=lambda x: -x[0])

cnt = 0
ans = 0

for (k, v) in cnt_dict:
    v_ = np.min([v, n - cnt])
    ans += v_ * k
    cnt += v_
    if cnt == n:
        break


print(ans)


Fixed submission:
from sys import stdin

n, m = map(int, stdin.readline().split())
a = [int(num) for num in stdin.readline().split()]

cnt_list = [(a[i], 1) for i in range(n)]
for i in range(m):
    b, c = map(int, stdin.readline().split())
    cnt_list.append((c, b))

cnt_list.sort()
cnt_list.reverse()

cnt = n
ans = 0

for (k, v) in cnt_list:
    acc_v = min(v, cnt)
    ans += k * acc_v
    cnt -= acc_v


print(ans)
Buggy python submission (verdict: Runtime Error):
#! /usr/bin/env python3

import sys
import numpy as np
int1 = lambda x: int(x) - 1
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

S = readline().decode().rstrip()

res = 0
b_cnt = 0

for c in S:
    if c == 'B':
        b_cnt += 1
    elif c == 'W':
        res += b_cnt

print(res)


Fixed submission:
#! /usr/bin/env python3

import sys
import numpy as np
int1 = lambda x: int(x) - 1
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

S = readline().decode().rstrip()

res = 0
b_cnt = 0

for c in S:
    if c == 'B':
        b_cnt += 1
    elif c == 'W':
        res += b_cnt

print(res)

Buggy python submission (verdict: Time Limit Exceeded):
#! /usr/bin/env python3

import sys
import numpy as np
int1 = lambda x: int(x) - 1
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

S = list(readline().decode().rstrip())[::-1]
l = len(S)

cnt = 0
for _ in range(l - 1):
    for i in range(l - 1):
        if S[i:i + 2] == ['W', 'B']:
            S[i:i + 2] = ['B', 'W']
            cnt += 1

print(cnt)


Fixed submission:
#! /usr/bin/env python3

import sys
import numpy as np
int1 = lambda x: int(x) - 1
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

S = readline().decode().rstrip()

res = 0
b_cnt = 0

for c in S:
    if c == 'B':
        b_cnt += 1
    elif c == 'W':
        res += b_cnt

print(res)
Buggy python submission (verdict: Runtime Error):
n = int(input())
for i in range(111,999,111):
    if i< n < i+111:
        ans = i + 111
        break
print(int(ans))


Fixed submission:
n = int(input())
if (n % 111) == 0:
	ans = (n // 111) * 111
else:
	ans = (n // 111 +1) * 111
print(int(ans))
Buggy python submission (verdict: Runtime Error):
import numpy as np

s = list(str(input()))
t = list(str(input()))

_, s_c = np.unique(s, return_counts=True)
_, t_c = np.unique(t, return_counts=True)
if np.array_equal(s_c, t_c):
	print('Yes')
else:
	print('No')

Fixed submission:
import numpy as np
from string import ascii_lowercase
s = list(input())
t = list(input())

s_c = []
t_c = []

for a in ascii_lowercase:
  if s.count(a) != 0:
    s_c.append(s.count(a))
  if t.count(a) != 0:
    t_c.append(t.count(a))

if np.array_equal(sorted(s_c), sorted(t_c)):
	print('Yes')
else:
	print('No')
Buggy python submission (verdict: Runtime Error):
import numpy as np

n, k = list(map(int, input().split()))
x = np.array(list(map(int, input().split())))

result = np.inf
for i in range(len(x) - k):
    tmp1 = np.abs(x[i+k-1] - x[i]) + np.abs(x[i])
    tmp2 = np.abs(x[i+k-1] - x[i]) + np.abs(x[i+k-1])
    result = np.min([tmp1, tmp2, result])

print(int(result))

Fixed submission:
import numpy as np

n, k = list(map(int, input().split()))
x = np.array(list(map(int, input().split())))

result = np.inf
for i in range(len(x) - k +1):
    tmp1 = np.abs(x[i+k-1] - x[i]) + np.abs(x[i])
    tmp2 = np.abs(x[i+k-1] - x[i]) + np.abs(x[i+k-1])
    result = np.min([tmp1, tmp2, result])

print(int(result))
Buggy python submission (verdict: Runtime Error):
T = int(input())
a = []
for i in range(T):
    a.append(list(map(int, input().split())))
for r in a:
    s, b, c, d = r[0], r[1], r[2], r[3]
    mod_list = []
    if (s < b) or (b>d):
        print('No')
        continue
    if (c>b):
        print('Yes')
        continue
    while True:
        s = s%b
        if s > c:
            print('No')
            break
        s += d
        if s in mod_list:
            print('Yes')
            break
        mod_list.append(s)

Fixed submission:
N = map(int, input())
a = list(map(int, input().split()))
all_set = set(range(1, 10000))

use_set = list(set(all_set - set(a)))

count = 0
for i in range(len(a)-1):
    if a[i] == a[i+1]:
        a[i+1] = use_set[0]
        use_set = use_set[1:]
        count += 1
    
print(count)

Buggy python submission (verdict: Runtime Error):
T = int(input())
a = []
for i in range(T):
    a.append(list(map(int, input().split())))
for r in a:
    s, b, c, d = r[0], r[1], r[2], r[3]
    mod_list = []
    if (s < b) or (b>d):
        print('No')
        break
    if (c>b):
        print('Yes')
        break
    while True:
        s = s%b
        if s > c:
            print('No')
            break
        s += d
        if s in mod_list:
            print('Yes')
            break
        mod_list.append(s)

Fixed submission:
N = map(int, input())
a = list(map(int, input().split()))
all_set = set(range(1, 10000))

use_set = list(set(all_set - set(a)))

count = 0
for i in range(len(a)-1):
    if a[i] == a[i+1]:
        a[i+1] = use_set[0]
        use_set = use_set[1:]
        count += 1
    
print(count)
Buggy python submission (verdict: Runtime Error):
#! /usr/bin/env python3

import sys
import numpy as np
int1 = lambda x: int(x) - 1
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

c, *N = str(int(readline())+1)
print(int(c) + 9 *len(N)- 1)


Fixed submission:
#! /usr/bin/env python3

import sys
import numpy as np
int1 = lambda x: int(x) - 1
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

c, *N = str(int(readline())+1)
print(int(c) + 9 *len(N)- 1)

Buggy python submission (verdict: Runtime Error):
#! /usr/bin/env python3

import sys
import numpy as np
int1 = lambda x: int(x) - 1
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

N = int(readline())
N_chr = str(N)
c = N_chr[0]

acc = int(c) + 9 *(len(N_chr)-1)
if N == acc:
    print(acc)
else:
    print(acc-1)
    


Fixed submission:
#! /usr/bin/env python3

import sys
import numpy as np
int1 = lambda x: int(x) - 1
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

c, *N = str(int(readline())+1)
print(int(c) + 9 *len(N)- 1)
Buggy python submission (verdict: Runtime Error):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

s = read().decode("utf-8").rstrip()
l = 0
r = len(s) - 1

cnt = 0
while l != r:
    if s[l] == s[r]:
        l += 1
        r -= 1
    elif s[l] == 'x':
        l += 1
        cnt += 1
    elif s[r] == 'x':
        r -= 1
        cnt += 1
    else:
        print(-1)
        sys.exit()

print(cnt)


Fixed submission:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

s = read().decode("utf-8").rstrip()
l = 0
r = len(s) - 1

cnt = 0
while len(s) != 0:
    if s[0] == s[-1]:
        s = s[1:-1]
    elif s[0] == 'x':
        s += 'x'
        cnt += 1
    elif s[-1] == 'x':
        s = 'x' + s
        cnt += 1
    else:
        print(-1)
        sys.exit()

print(cnt)
Buggy python submission (verdict: Runtime Error):
#! /usr/bin/env python3

import sys
import numpy as np
int1 = lambda x: int(x) - 1
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

Q, H, S, D, N = map(int, read().split())

dp = [1<<60] * (N + 2)

dp[0] = 0

for i in range(1, N+1):
    dp[i] = min([dp[i-1] + Q * 4, dp[i-1] + Q*2 + H, dp[i-1] + H*2, dp[i-1] + S])
    if i >= 2:
        dp[i] = min(dp[i], dp[i-2] + D)

print(dp[N])


Fixed submission:
#! /usr/bin/env python3

import sys
import numpy as np
int1 = lambda x: int(x) - 1
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

Q, H, S, D, N = map(int, read().split())
H = min(Q * 2, H)
S = min(H *2 , S)

if S * 2 < D:
    print(N * S)
else:
    print(N // 2 * D + N % 2 * S)
Buggy python submission (verdict: Time Limit Exceeded):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

INF = 10**18

N = int(readline())
ABC = [list(map(int, readline().split())) for _ in range(N - 1)]
Q, K = map(int, readline().split())
XY = [list(map(int, readline().split())) for _ in range(Q)]


def warshall_floyd(dist):
    for k, _ in enumerate(dist):
        for i, _ in enumerate(dist):
            for j, _ in enumerate(dist):
                dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j])
    return dist


dist = [[INF] * (N + 1) for _ in range(N + 1)]
for abc in ABC:
    dist[abc[0] - 1][abc[1] - 1] = abc[2]
    dist[abc[1] - 1][abc[0] - 1] = abc[2]

dist = warshall_floyd(dist)

for x, y in XY:
    print(dist[x - 1][K - 1] + dist[K - 1][y - 1])


Fixed submission:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

N = int(readline())

edges = [[] for _ in range(N + 1)]
for _ in range(N - 1):
    a, b, c = map(int, readline().split())
    edges[a].append((b, c))
    edges[b].append((a, c))

Q, K = map(int, readline().split())

dist = [0] * (N + 1)


def dfs(v, p, d):
    dist[v] = d

    for n, dd in edges[v]:
        if n == p:
            continue
        dfs(n, v, d + dd)


dfs(K, -1, 0)

for _ in range(Q):
    x, y = map(int, readline().split())
    print(dist[x] + dist[y])