Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-

import sys
sys.setrecursionlimit(1000000000)

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


def countPatty(N, X):

    if X == 0:
        return 0, 0

    if N == 0:
        return 1, X - 1

    # First inner burger
    nPatty, restX = countPatty(N - 1, X - 1)

    if restX == 1:
        nPatty, restX = nPatty + 1, 0
    elif restX >= 2:
        tPatty, restX = countPatty(N - 1, restX - 1)
        nPatty, restX = nPatty + tPatty + 1, restX - 1

    return nPatty, restX


nPatty, restX = countPatty(N, X)
print(nPatty)


Fixed submission:
# -*- coding: utf-8 -*-

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

L, P = [1], [1]
for i in range(N - 1):
    L.append(L[-1] * 2 + 3)
    P.append(P[-1] * 2 + 1)


def countPatty(N, X):
    if N == 0:
        return 1 if X >= 1 else 0
    elif X <= 1:
        return 0
    elif 1 < X <= L[N - 1] + 1:
        return countPatty(N - 1, X - 1)
    elif L[N - 1] + 1 < X:
        return P[N - 1] + 1 + countPatty(N - 1, X - (L[N - 1] + 2))


print(countPatty(N, X))
Buggy python submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-

N = int(input())
X, Y, H = [], [], []
for _ in range(N):
    x, y, h = map(int, input().split())
    X.append(x)
    Y.append(y)
    H.append(h)

for x in range(101):
    for y in range(101):
        hc = {H[i] + abs(X[i] - x) + abs(Y[i] - y) for i in range(N) if H[i] > 0}
        h0 = {H[i] + abs(X[i] - x) + abs(Y[i] - y) for i in range(N) if H[i] == 0}
        if len(hc) == 1 and max(h0) <= max(hc):
            print(x, y, list(hc)[0])
            break


Fixed submission:
# -*- coding: utf-8 -*-

N = int(input())
X, Y, H = [], [], []
for _ in range(N):
    x, y, h = map(int, input().split())
    X.append(x)
    Y.append(y)
    H.append(h)

for x in range(101):
    for y in range(101):
        hc = {H[i] + abs(X[i] - x) + abs(Y[i] - y) for i in range(N) if H[i] > 0}
        h0 = {H[i] + abs(X[i] - x) + abs(Y[i] - y) for i in range(N) if H[i] == 0}
        if len(hc) == 1:
            if (len(h0) > 0 and min(h0) >= list(hc)[0]) or len(h0) == 0:
                print(x, y, list(hc)[0])
                break
Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-

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

for i in range(1, M // 2 + 1):
    if M % i == 0 and i >= N:
        print(M // i)
        exit()

print(1)


Fixed submission:
# -*- coding: utf-8 -*-

import bisect

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

y = [1, M]
for i in range(2, int(M ** 0.5) + 1):
    if M % i == 0:
        y.append(i)
        y.append(M // i)

y.sort()
print(M // y[bisect.bisect_left(y, N)])

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-

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

for i in range(1, M + 1):
    if M % i == 0 and i >= N:
        print(M // i)
        break


Fixed submission:
# -*- coding: utf-8 -*-

import bisect

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

y = [1, M]
for i in range(2, int(M ** 0.5) + 1):
    if M % i == 0:
        y.append(i)
        y.append(M // i)

y.sort()
print(M // y[bisect.bisect_left(y, N)])

Buggy python submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-

N = int(input())
X, Y, H = [], [], []
for _ in range(N):
    x, y, h = map(int, input().split())
    X.append(x)
    Y.append(y)
    H.append(h)

for x in range(101):
    for y in range(101):
        hc = {H[i] + abs(X[i] - x) + abs(Y[i] - y) for i in range(N) if H[i] > 0}
        h0 = {H[i] + abs(X[i] - x) + abs(Y[i] - y) for i in range(N) if H[i] == 0}
        if len(hc) == 1:
            if (len(h0) > 0 and min(h0) >= list(hc)[0]) or len(h0) == 0:
                print(x, y, list(hc)[0])
                break


Fixed submission:
# -*- coding: utf-8 -*-

import bisect

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

y = [1, M]
for i in range(2, int(M ** 0.5) + 1):
    if M % i == 0:
        y.append(i)
        y.append(M // i)

y.sort()
print(M // y[bisect.bisect_left(y, N)])
Buggy python submission (verdict: Runtime Error):
S = input()
K = int(input())
lc1 = -1
while S[lc1 + 1] == '1':
    lc1 += 1

if lc1 == -1:
    print(S[0])
else:
    print("1" if lc1 + 1 >= K else S[lc1 + 1])


Fixed submission:
S = input()
K = int(input())
firstNot1 = ([i for i in range(len(S)) if S[i] != '1'] + [len(S)])[0]

print('1' if firstNot1 >= K else S[firstNot1])

Buggy python submission (verdict: Runtime Error):
S = input()
K = int(input())
firstNot1 = [i for i in range(len(S)) if S[i] != '1'][0]

print('1' if firstNot1 >= K else S[firstNot1])


Fixed submission:
S = input()
K = int(input())
firstNot1 = ([i for i in range(len(S)) if S[i] != '1'] + [len(S)])[0]

print('1' if firstNot1 >= K else S[firstNot1])
Buggy python submission (verdict: Time Limit Exceeded):
H, W, D = map(int, input().split())
X, Y = [-1] * H * W, [-1] * H * W

for h in range(H):
    A = list(map(int, input().split()))
    for w in range(W):
        X[A[w] - 1], Y[A[w] - 1] = h, w

Q = int(input())
for _ in range(Q):
    L, R = map(int, input().split())
    ans = 0
    while L != R:
        ans += abs(X[L - 1] - X[L + D - 1]) + abs(Y[L - 1] - Y[L + D - 1])
        L += D
    print(ans)


Fixed submission:
H, W, D = map(int, input().split())
X, Y, ans = [-1] * H * W, [-1] * H * W, [0] * H * W

for h in range(H):
    A = list(map(int, input().split()))
    for w in range(W):
        X[A[w] - 1], Y[A[w] - 1] = h, w

for d in range(D):
    while d + D < H * W:
        ans[d + D] = ans[d] + abs(X[d] - X[d + D]) + abs(Y[d] - Y[d + D])
        d += D

Q = int(input())
for _ in range(Q):
    L, R = map(int, input().split())
    print(ans[R - 1] - ans[L - 1])
Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-

from collections import deque

neig = [(1, 0), (-1, 0), (0, 1), (0, -1)]

H, W = map(int, input().split())

F = []
nW = 0
for _ in range(H):
    F.append(list(input()))
    nW += sum([1 for s in F[-1] if s == '.'])

ans = -1
q = deque()
q.append((0, 0, 1))
while q:
    y, x, count = q.popleft()
    F[y][x] = 'V' # Visited
    if y == H - 1 and x == W - 1:
        ans = nW - count
    for n in neig:
        ny, nx = y + n[0], x + n[1]
        if 0 <= ny < H and 0 <= nx < W:
            if F[ny][nx] == '.':
                q.append((ny, nx, count + 1))

print(ans)


Fixed submission:
# -*- coding: utf-8 -*-

from collections import deque

neig = [(1, 0), (-1, 0), (0, 1), (0, -1)]

H, W = map(int, input().split())

F = []
nW = 0
for _ in range(H):
    F.append(list(input()))
    nW += sum([1 for s in F[-1] if s == '.'])

ans = -1
q = deque()
q.append((0, 0, 1))
F[0][0] = 'V' # Visited

while q:
    y, x, count = q.popleft()
    if y == H - 1 and x == W - 1:
        ans = nW - count
        break
    for n in neig:
        ny, nx = y + n[0], x + n[1]
        if 0 <= ny < H and 0 <= nx < W:
            if F[ny][nx] == '.':
                q.append((ny, nx, count + 1))
                F[ny][nx] = 'V' # Visited

print(ans)
Buggy python submission (verdict: Runtime Error):
S = input()
b = []
for i in range(len(S) - 1):
    if S[i:i + 2] == "01" or S[i:i + 2] == "10":
        b.append(len(S) - (i + 1) if i < len(S) // 2 else i + 1)

print(min(b))


Fixed submission:
S = input()
b = []
for i in range(len(S) - 1):
    if S[i:i + 2] == "01" or S[i:i + 2] == "10":
        b.append(len(S) - (i + 1) if i < len(S) // 2 else i + 1)

print(min(b) if len(b) != 0 else len(S))
Buggy python submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-

A, B = input().split()

if int(A, 16) = int(B, 16):
    print('=')
elif int(A, 16) > int(B, 16):
    print('>')
elif int(A, 16) < int(B, 16):
    print('<')


Fixed submission:
# -*- coding: utf-8 -*-

A, B = input().split()

if int(A, 16) == int(B, 16):
    print('=')
elif int(A, 16) > int(B, 16):
    print('>')
elif int(A, 16) < int(B, 16):
    print('<')
Buggy python submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-

A, B = input().split()

if int(A, 16) = int(B, 16):
    print('=')
elif int(A, 16) > int(B, 16):
    print('>')
elif int(A, 16) < int(B, 16):
    print('<')


Fixed submission:
# -*- coding: utf-8 -*-

X, Y, Z = map(int, input().split())

print(int((X - Z)/(Z+Y)))
Buggy python submission (verdict: Runtime Error):
import collections

N = int(input())

q = collections.deque([(1, 0)])
v = {1}

while len(q) > 0:
    c, p = deque.popleft()
    v.add(c)
    if c == 0:
        break

    n1, n10 = (c + 1) % N, (c * 10) % n
    if not n1 in v:
        q.append((n1, p + 1))
    if not n10 in v:
        q.appendleft((n10, p))

print(p + 1)


Fixed submission:
# -*- coding: utf-8 -*-

import collections

n = int(input())

cursor = 1
deque = collections.deque([(cursor, 0)])
visited = {cursor}

min_path = 10e10
while len(deque) > 0:
    cursor, min_path = deque.popleft()
    visited.add(cursor)
    if cursor == 0:
        break

    pone, mten = (cursor + 1) % n, (cursor * 10) % n
    if not pone in visited:
        deque.append((pone, min_path + 1))
    if not mten in visited:
        deque.appendleft((mten, min_path))

print(min_path + 1)

Buggy python submission (verdict: Runtime Error):
import collections

N = int(input())

q = collections.deque([(1, 0)])
v = {1}

while len(q) > 0:
    c, p = deque.popleft()
    v.add(c)
    if c == 0:
        break

    n1, n10 = (c + 1) % N, (c * 10) % n
    if not n1 in v:
        q.append((n1, p + 1))
    if not n10 in v:
        q.appendleft((n10, p))

print(p + 1)


Fixed submission:
# -*- coding: utf-8 -*-

import collections

n = int(input())

cursor = 1
deque = collections.deque([(cursor, 0)])
visited = {cursor}

min_path = 10e10
while len(deque) > 0:
    cursor, min_path = deque.popleft()
    visited.add(cursor)
    if cursor == 0:
        break

    pone, mten = (cursor + 1) % n, (cursor * 10) % n
    if not pone in visited:
        deque.append((pone, min_path + 1))
    if not mten in visited:
        deque.appendleft((mten, min_path))

print(min_path + 1)
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
T = list(map(int, input().split()))
V = [0] + list(map(int, input().split())) + [0]

Tc = [0, 0] + [sum(T[:i + 1]) for i in range(N)] + [sum(T)]

ans = 0
for i in range(sum(T) * 2):
    t1, t2 = i * 0.5, (i + 1) * 0.5
    maxV1, maxV2 = [], []
    for j in range(N + 2):
        for t, maxV in [(t1, maxV1), (t2, maxV2)]:
            if t <= Tc[j]:
                maxV.append(V[j] + (Tc[j] - t))
            elif Tc[j] < t <= Tc[j + 1]:
                maxV.append(V[j])
            else:
                maxV.append(V[j] + (t - Tc[j + 1]))
    ans += 0.25 * (min(maxV1) + min(maxV2))

print(ans)


Fixed submission:
N = int(input())
T = list(map(int, input().split()))
V = [0] + list(map(int, input().split())) + [0]

Tc = [0, 0] + [sum(T[:i + 1]) for i in range(N)] + [sum(T)]

ans = 0
for i in range(sum(T) * 2):
    t1, t2 = i * 0.5, (i + 1) * 0.5
    maxV1, maxV2 = [], []
    for j in range(N + 2):
        for t, maxV in [(t1, maxV1), (t2, maxV2)]:
            if t <= Tc[j]:
                maxV.append(V[j] + (Tc[j] - t))
            elif Tc[j] < t <= Tc[j + 1]:
                maxV.append(V[j])
            else:
                maxV.append(V[j] + (t - Tc[j + 1]))
    ans += 0.25 * (min(maxV1) + min(maxV2))

print(ans)
Buggy python submission (verdict: Runtime Error):
H, W = map(int, input().split())
S = ['.' * (W + 2)] + ['.' + input() + '.' for _ in range(H)] + ['.' * (W + 2)]
ans = [[] for _ in range(H)]
N = [(1, 1), (1, 0), (1, -1), (0, 1), (0, -1), (-1, 1), (-1, 0), (-1, -1)]
for h in range(1, H + 1):
    for w in range(1, W + 1):
        if S[h][w] == '#':
            ans[h - 1].append('#')
        else:
            ans[h - 1].append(sum([S[h + n[0]][w + n[1]] == '#' for n in N]))

for h in range(H):
    print(''.join(ans[h]))


Fixed submission:
H, W = map(int, input().split())
S = ['.' * (W + 2)] + ['.' + input() + '.' for _ in range(H)] + ['.' * (W + 2)]

N = [(0, 1), (1, 0), (1, 1), (0, -1), (-1, 0), (1, -1), (-1, 1), (-1, -1)]
ans = [[] for _ in range(H)]
for h in range(1, H + 1):
    for w in range(1, W + 1):
        if S[h][w] == '.':
            ans[h - 1].append(sum([S[h + n[0]][w + n[1]] == '#' for n in N]))
        else:
            ans[h - 1].append('#')

for h in range(H):
    print(''.join(map(str, ans[h])))

Buggy python submission (verdict: Runtime Error):
H, W = map(int, input().split())
S = ['.' * (W + 2)] + ['.' + input() + '.' for _ in range(H)] + ['.' * (W + 2)]
ans = [[] for _ in range(H)]
N = [(1, 1), (1, 0), (1, -1), (0, 1), (0, -1), (-1, 1), (-1, 0), (-1, -1)]
for h in range(H):
    for w in range(W):
        if S[h][w] == '#':
            ans[h - 1].append('#')
        else:
            ans[h - 1].append(sum([S[h + n[0]][w + n[1]] == '#' for n in N]))

for h in range(H):
    print(''.join(ans[h]))


Fixed submission:
H, W = map(int, input().split())
S = ['.' * (W + 2)] + ['.' + input() + '.' for _ in range(H)] + ['.' * (W + 2)]

N = [(0, 1), (1, 0), (1, 1), (0, -1), (-1, 0), (1, -1), (-1, 1), (-1, -1)]
ans = [[] for _ in range(H)]
for h in range(1, H + 1):
    for w in range(1, W + 1):
        if S[h][w] == '.':
            ans[h - 1].append(sum([S[h + n[0]][w + n[1]] == '#' for n in N]))
        else:
            ans[h - 1].append('#')

for h in range(H):
    print(''.join(map(str, ans[h])))
Buggy python submission (verdict: Time Limit Exceeded):
N, K = map(int, input().split())
XY = [tuple(map(int, input().split())) for _ in range(N)]

ans = 10 ** 20

for x1 in range(N):
    for x2 in range(x1 + 1, N):
        for y1 in range(N):
            for y2 in range(y1 + 1, N):
                u = max(XY[x1][0], XY[x2][0])
                d = min(XY[x1][0], XY[x2][0])
                r = max(XY[y1][1], XY[y2][1])
                l = min(XY[y1][1], XY[y2][1])
                c = sum([d <= x <= u and l <= y <= r for x, y in XY])
                ans = min(ans, (u - d) * (r - l)) if c >= K else ans

print(ans)


Fixed submission:
N, K = map(int, input().split())
XY = sorted([tuple(map(int, input().split())) for _ in range(N)])

ans = 10 ** 20
for xl in range(N):
    for xr in range(xl + 1, N):
        Ysub = sorted([y for x, y in XY[xl:xr + 1]])
        for i in range(xr - xl - K + 2):
            Yk = Ysub[i:i + K] + [XY[xr][1], XY[xl][1]]
            ans = min(ans, (XY[xr][0] - XY[xl][0]) * (max(Yk) - min(Yk)))

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N, K = map(int, input().split())
XY = [tuple(map(int, input().split())) for _ in range(N)]

ans = 10 ** 20

for x1 in range(N):
    for x2 in range(x1, N):
        for y1 in range(N):
            for y2 in range(y1, N):
                u = max(XY[x1][0], XY[x2][0])
                d = min(XY[x1][0], XY[x2][0])
                r = max(XY[y1][1], XY[y2][1])
                l = min(XY[y1][1], XY[y2][1])
                c = sum([d <= x <= u and l <= y <= r for x, y in XY])
                ans = min(ans, (u - d) * (r - l)) if c >= K else ans

print(ans)


Fixed submission:
N, K = map(int, input().split())
XY = sorted([tuple(map(int, input().split())) for _ in range(N)])

ans = 10 ** 20
for xl in range(N):
    for xr in range(xl + 1, N):
        Ysub = sorted([y for x, y in XY[xl:xr + 1]])
        for i in range(xr - xl - K + 2):
            Yk = Ysub[i:i + K] + [XY[xr][1], XY[xl][1]]
            ans = min(ans, (XY[xr][0] - XY[xl][0]) * (max(Yk) - min(Yk)))

print(ans)
Buggy python submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-

n = int(input())

a = []

for i in range(n):
    a_list = list(map(int, input().split()))
    a_dict = {i:e for i,e in enumerate(a_list[:i])}
    a.append(a_dict)

if n == 1:
    print("-1")
    exit()

elif n == 2:
    print(a[0][1])
    exit()

# Minimam total path length
min_total = 0

for i in range(n):
    dim = i+1
    if dim == 1:
        continue
    elif dim == 2:
        min_total += a[1][0]
        continue

    paths_to_subgraph = []
    a_sorted = sorted(a[i].items(), key=lambda e:e[1])

    dest, path = a_sorted.pop(0)
    paths_to_subgraph.append((dest, path))
    min_total += path

    for dest, path in a_sorted:
        create_path = True
        for detour in paths_to_subgraph:
            detour_path = detour[1] + a[max([dest,detour[0]])][min([dest,detour[0]])]
            if path > detour_path:
                print("-1")
                exit()
            elif path == detour_path:
                create_path = False

        if create_path:
            for detour in paths_to_subgraph:
                detour_path = detour[1] + path
                minpath = a[max([dest,detour[0]])][min([dest,detour[0]])]
                if minpath > detour_path:
                    print("-1")
                    exit()
                elif minpath == detour_path:
                  # Delete path
                  min_total -= minpath
            paths_to_subgraph.append((dest, path))
            min_total += path

print(min_total)


Fixed submission:
# -*- coding: utf-8 -*-

n = int(input())

a = []

for i in range(n):
    a_list = list(map(int, input().split()))
    a_dict = {i:e for i,e in enumerate(a_list[:i])}
    a.append(a_dict)

if n == 1:
    print("-1")
    exit()

elif n == 2:
    print(a[1][0])
    exit()

# Minimam total path length
min_total = 0

for i in range(n):
    dim = i+1
    if dim == 1:
        continue
    elif dim == 2:
        min_total += a[1][0]
        continue

    paths_to_subgraph = []
    a_sorted = sorted(a[i].items(), key=lambda e:e[1])

    dest, path = a_sorted.pop(0)
    paths_to_subgraph.append((dest, path))
    min_total += path

    for dest, path in a_sorted:
        create_path = True
        for detour in paths_to_subgraph:
            detour_path = detour[1] + a[max([dest,detour[0]])][min([dest,detour[0]])]
            if path > detour_path:
                print("-1")
                exit()
            elif path == detour_path:
                create_path = False

        if create_path:
            for detour in paths_to_subgraph:
                detour_path = detour[1] + path
                minpath = a[max([dest,detour[0]])][min([dest,detour[0]])]
                if minpath > detour_path:
                    print("-1")
                    exit()
                elif minpath == detour_path:
                  # Delete path
                  min_total -= minpath
            paths_to_subgraph.append((dest, path))
            min_total += path

print(min_total)
Buggy python submission (verdict: Runtime Error):
def warshall_floyd(G):
    import copy
    ret = copy.deepcopy(G)
    for k in range(N):
        for i in range(N):
            for j in range(N):
                ret[i][j] = min(ret[i][j], ret[i][k] + ret[k][j])
    return ret


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

if A == warshall_floyd(A):
    ans = 0
    for i in range(1, N):
        for j in range(0, i):
            if min([A[i][k] + A[k][j] - A[i][j] for k in range(N) if k != i and k != j]) > 0:
                ans += A[i][j]
    print(ans)
else:
    print(-1)


Fixed submission:
def warshall_floyd(G):
    import copy
    ret = copy.deepcopy(G)
    for k in range(N):
        for i in range(N):
            for j in range(N):
                ret[i][j] = min(ret[i][j], ret[i][k] + ret[k][j])
    return ret


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

if A == warshall_floyd(A):
    ans = 0
    for i in range(1, N):
        for j in range(0, i):
            if min([A[i][k] + A[k][j] - A[i][j] for k in range(N) if k != i and k != j] + [1]) > 0:
                ans += A[i][j]
    print(ans)
else:
    print(-1)

Buggy python submission (verdict: Runtime Error):
def warshall_floyd(G):
    import copy
    ret = copy.deepcopy(G)
    for k in range(N):
        for i in range(N):
            for j in range(N):
                ret[i][j] = min(ret[i][j], ret[i][k] + ret[k][j])
    return ret


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

if A == warshall_floyd(A):
    ans = 0
    for i in range(1, N):
        for j in range(0, i):
            if min([A[i][k] + A[k][j] - A[i][j] for k in range(N) if k != i and k != j]) > 0:
                ans += A[i][j]
    print(ans)
else:
    print(-1)


Fixed submission:
def warshall_floyd(G):
    import copy
    ret = copy.deepcopy(G)
    for k in range(N):
        for i in range(N):
            for j in range(N):
                ret[i][j] = min(ret[i][j], ret[i][k] + ret[k][j])
    return ret


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

if A == warshall_floyd(A):
    ans = 0
    for i in range(1, N):
        for j in range(0, i):
            if min([A[i][k] + A[k][j] - A[i][j] for k in range(N) if k != i and k != j] + [1]) > 0:
                ans += A[i][j]
    print(ans)
else:
    print(-1)
Buggy python submission (verdict: Runtime Error):
from functools import reduce
import math


def lcm(x, y):
    return (x * y) // math.gcd(x, y)


N = int(input())
T = [int(input()) for _ in range(N)]
print(reduce(lcm, T, 1))


Fixed submission:
def lcm(x, y):
    from fractions import gcd
    return (x * y) // gcd(x, y)


N, ans = int(input()), 1
for _ in range(N):
    ans = lcm(ans, int(input()))
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10 ** 7)

N = int(input())
T, D = {i: [] for i in range(N)}, [-1] * N
for _ in range(N - 1):
    a, b, c = map(int, input().split())
    T[a - 1].append((b - 1, c))
    T[b - 1].append((a - 1, c))


def dfs(p, c, d):
    D[c] = d
    for c2, d2 in T[c]:
        if c2 != p:
            dfs(c, c2, d + d2)


Q, K = map(int, input().split())
dfs(-1, K - 1, 0)
for _ in range(Q):
    x, y = map(int, input().split())
    print(D[x - 1] + D[y - 1])


Fixed submission:
N = int(input())
neig = [[] for _ in range(N)]

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

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

S, D = [(K - 1, K - 1)], [0] * N
while len(S) > 0:
    p, pp = S.pop()
    for c, d in neig[p]:
        if not c == pp:
            D[c] = D[p] + d
            S.append((c, p))

for _ in range(Q):
    x, y = map(int, input().split())
    print(D[x - 1] + D[y - 1])

Buggy python submission (verdict: Runtime Error):
N = int(input())
T, D = {i: [] for i in range(N)}, [-1] * N
for _ in range(N - 1):
    a, b, c = map(int, input().split())
    T[a - 1].append((b - 1, c))
    T[b - 1].append((a - 1, c))


def dfs(p, c, d):
    D[c] = d
    for c2, d2 in T[c]:
        if c2 != p:
            dfs(c, c2, d + d2)


Q, K = map(int, input().split())
dfs(-1, K - 1, 0)
for _ in range(Q):
    x, y = map(int, input().split())
    print(D[x - 1] + D[y - 1])


Fixed submission:
N = int(input())
neig = [[] for _ in range(N)]

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

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

S, D = [(K - 1, K - 1)], [0] * N
while len(S) > 0:
    p, pp = S.pop()
    for c, d in neig[p]:
        if not c == pp:
            D[c] = D[p] + d
            S.append((c, p))

for _ in range(Q):
    x, y = map(int, input().split())
    print(D[x - 1] + D[y - 1])
Buggy python submission (verdict: Runtime Error):
N, M = map(int, input().split())
AB = [tuple(map(int, input().split())) for _ in range(N)]
CD = [tuple(map(int, input().split())) for _ in range(N)]

for a, b in AB:
    ans, maxD = 0, 10 ** 10
    for i, (c, d) in enumerate(CD):
        if abs(a - c) + abs(b - d) < maxD:
            ans, maxD = i + 1, abs(a - c) + abs(b - d)
    print(ans)


Fixed submission:
N, M = map(int, input().split())
AB = [tuple(map(int, input().split())) for _ in range(N)]
CD = [tuple(map(int, input().split())) for _ in range(M)]

for a, b in AB:
    ans, maxD = 0, 10 ** 10
    for i, (c, d) in enumerate(CD):
        if abs(a - c) + abs(b - d) < maxD:
            ans, maxD = i + 1, abs(a - c) + abs(b - d)
    print(ans)