Harryxun/GPT-100-sft-dataset huggingface dataset:
Problem p02990 (python)

Fixed submission:
def comb(n, r):
  if n < 0 or r < 0:
    raise ValueError
  r = min(r, n - r)
  if n == 0 or r == 0:
    return 1
  return (n - r + 1) * comb(n, r - 1) // r

N, K = list(map(int, input().split()))
for i in range(K):
  if N - K + 1 < i + 1:
    print('0')
    continue
  print(comb(N - K + 1, i + 1) * comb(K - 1, i) % (10 ** 9 + 7))

Buggy submission (verdict: Runtime Error):
def comb(n, r):
  if n < r or n < 0 or r < 0:
    return 0
  r = min(r, n - r)
  if n == 0 or r == 0:
    return 1
  return (n - r + 1) * comb(n, r - 1) // r

N, K = list(map(int, input().split()))
for i in range(K):
  print(comb(N - K + 1, i + 1) * comb(K - 1, i) % (10 ** 9 + 7))

Buggy submission (verdict: Runtime Error):
def comb(n, r):
  if n < r:
    return 0
  r = min(r, n - r)
  if n == 0 or r == 0:
    return 1
  return (n - r + 1) * comb(n, r - 1) // r

N, K = list(map(int, input().split()))
for i in range(K):
  print(comb(N - K + 1, i + 1) * comb(K - 1, i) % (10 ** 9 + 7))

Buggy submission (verdict: Runtime Error):
def comb(n, r):
  if n < 0 or r < 0:
    return 0
  r = min(r, n - r)
  if n == 0 or r == 0:
    return 1
  return (n - r + 1) * comb(n, r - 1) // r

N, K = list(map(int, input().split()))
for i in range(K):
  print(comb(N - K + 1, i + 1) * comb(K - 1, i) % (10 ** 9 + 7))

Buggy submission (verdict: Runtime Error):
def comb(n, r):
  if n <= 0 or r < 0:
    raise ValueError
  r = min(r, n - r)
  if r == 0:
    return 1
  return (n - r + 1) * comb(n, r - 1) // r

N, K = list(map(int, input().split()))
for i in range(K):
  if N - K + 1 < i + 1:
    print('0')
    continue
  print(comb(N - K + 1, i + 1) * comb(K - 1, i) % (10 ** 9 + 7))

Buggy submission (verdict: Runtime Error):
def comb(n, r):
  if n <= 0 or r < 0:
    raise ValueError
  if r > n - r:
    r = n - r
  if r == 0:
    return 1
  return (n - r + 1) * comb(n, r - 1) // r

N, K = list(map(int, input().split()))
for i in range(N - K + 1):
  print(comb(N - K + 1, i + 1) * comb(K - 1, i) % 1000000007)

Buggy submission (verdict: Runtime Error):
import math

def perm(n, r):
  return math.factorial(n) // math.factorial(n - r)

def comb(n, r):
  return perm(n, r) // math.factorial(r)

N, K = list(map(int, input().split()))
for i in range(K):
  print(comb(N - K + 1, i + 1) * comb(K - 1, i) % 1000000007)

Buggy submission (verdict: Runtime Error):
def comb(n, r):
  if r > n - r:
    r = n - r
  if n == 1 or r == 0:
    return 1
  return (n - r + 1) * comb(n, r - 1) // r

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

for i in range(K):
  print(comb(N - K + 1, i + 1) * comb(K - 1, i) % 1000000007)

Buggy submission (verdict: Runtime Error):
def comb(n, r):
  if r > n - r:
    r = n - r
  if r == 0:
    return 1
  return (n - r + 1) * comb(n, r - 1) // r

N, K = list(map(int, input().split()))
for i in range(K):
  print(comb(N - K + 1, i  + 1) * comb(K - 1, i) % 1000000007)
Problem p03012 (python)

Fixed submission:
N = int(input())
W = list(map(int, input().split()))
dif = [abs(sum(W[:T + 1]) - sum(W[T + 1:])) for T in range(N - 1)]
print(min(dif))

Buggy submission (verdict: Runtime Error):
N = int(input())
W = list(map(int, input().split()))
dif = [sum(W[:T + 1] - W[T + 1:]) for T in range(N - 1)]
print(min(dif))

Buggy submission (verdict: Runtime Error):
N = int(input())
W = list(map(int, input().split()))
dif = [sum(W[:T + 1] - W[T + 1:]) for T in range(N)]
print(min(dif))
Problem p03013 (python)

Fixed submission:
# coding: utf-8
# Your code here!

from collections import defaultdict

N, M = map(int, input().rstrip().split(' '))
if M == 0:
    A = [0]
else:
    A = sorted([int(input()) for _ in range(M)]) + [N + 1]
    
dp = [0] * (N+1)
dp[0] = 1
if A[0] != 1:
    dp[1] = 1
    index_A = 0
else:
    dp[1] = -1
    index_A = 1
    
for i in range(2,N+1):
    if A[index_A] == i:
        dp[i] = -1
        index_A += 1
    else:
        if dp[i-1] != -1:
            dp[i] += dp[i-1]
        if dp[i-2] != -1:
            dp[i] += dp[i-2]
            
print(dp[N] %  (1000000007))

Buggy submission (verdict: Runtime Error):

from collections import defaultdict

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

A = [0] * (N + 1)
for _ in range(M):
    A[int(input())] = 1

dp = defaultdict(lambda: -1)
def dfs(n):
    if n > N:
        return 0
    if A[n] == 1:
        return 0
    if n == N:
        return 1
    if dp[n] != -1:
        return dp[n]
        
    count = 0
    count += dfs(n + 1)
    count += dfs(n + 2)
    
    dp[n] = count
    
    return count
    
print(dfs(0) % (1000000007))
Problem p03104 (python)

Fixed submission:
# coding: utf-8
# Your code here!

A, B = map(int, input().rstrip().split(' '))

if A == B:
    print(A)
    
else:
    max_bit = 0
    copied_B = B
    while copied_B > 0:
        copied_B = copied_B // 2
        max_bit += 1
    
    count = 0
    for digit in range(max_bit):
        period = 2 ** (digit + 1)
        pos_A = A % period
        pos_B = B % period
        
        num_one = 0
        if pos_A >= period // 2:
            num_one += period - pos_A
        else:
            num_one += period // 2
         
        if pos_B >= period // 2:
            num_one += pos_B - period // 2 + 1
            
        if digit == 0:
            num_one = 0
            num_one += B % 2
            num_one += A % 2
            if B % 2 == 1 and A % 2 == 1:
                num_one += (B - A - 1) // 2
            else:
                num_one += (B - A) // 2
            
        if num_one % 2 != 0:
            count += 2 ** digit
         
    print(count)

Buggy submission (verdict: Runtime Error):

A, B = map(int, input().rstrip().split(' '))
values = list(range(A, B + 1))
#xor_bit = [0] * (B - A)

xor = 0
digit = 0
while True:
    if values[-1] == 0:
        break
    
    num_zeros = 0
    for v in values:
        if v == 0:
            num_zeros = 0
        else:
            break
    
    values = values[num_zeros:]
    
    num_one = 0
    for i, v in enumerate(values):
        num_one += v % 2
        values[i] = v // 2
      
    xor += 2 ** digit * (num_one % 2)
    
    digit += 1
    
print(xor)
Problem p03107 (python)

Fixed submission:
# coding: utf-8
# Your code here!

import copy

string = [x for x in input()]

concat_string = copy.copy(string)
num_delete = 0

i = 0
j = 1
not_delete_list = []
while True:
    if j >= len(concat_string) or i < 0:
        break
    if concat_string[i] != concat_string[j]:
        num_delete += 2
        
        if i == 0:
            i = j + 1
            j = i + 1
        else:
            if len(not_delete_list) > 0:
                i = not_delete_list.pop(-1)
                j = j + 1
            else:
                i = j + 1
                j = i + 1
    else:
        not_delete_list.append(i)
        i = j
        j = i + 1


print(num_delete)

Buggy submission (verdict: Runtime Error):
# coding: utf-8
# Your code here!

import copy

string = [x for x in input()]

concat_string = copy.copy(string)
count = 0
while True:
    num_delete = 0
    flags_delete = [False] * len(string)
    
    i = 0
    j = 1
    while i < len(concat_string) - 1:
        if concat_string[i] != concat_string[j]:
            flags_delete[i] = True
            flags_delete[j] = True
            num_delete += 2
            
            if i == 0:
                i = j + 1
                j = i + 1
            else:
                i = i - 1
                j = j + 1
        else:
            i = j
            j = i + 1
           
    if num_delete == 0:
        break
    
    count += num_delete
    concat_string = [concat_string[i] for i in range(len(concat_string)) if flags_delete[i] == False]

print(count)

Buggy submission (verdict: Runtime Error):
# coding: utf-8
# Your code here!

import copy

string = [*input()]

concat_string = copy.copy(string)
count = 0
while True:
    num_delete = 0
    flags_delete = [False] * len(string)
    
    i = 0
    j = 1
    while i < len(concat_string) - 1:
        if concat_string[i] != concat_string[j]:
            flags_delete[i] = True
            flags_delete[j] = True
            num_delete += 2
            
            if i == 0:
                i = j + 1
                j = i + 1
            else:
                i = i - 1
                j = j + 1
        else:
            i = j
            j = i + 1
           
    if num_delete == 0:
        break
    
    count += num_delete
    concat_string = [concat_string[i] for i in range(len(concat_string)) if flags_delete[i] == False]

print(count)

Buggy submission (verdict: Runtime Error):
# coding: utf-8
# Your code here!

import copy

string = [*input()]

concat_string = copy.copy(string)
count = 0
while True:
    num_delete = 0
    flags_delete = [False] * len(string)
    
    i = 0
    while i < len(concat_string) - 1:
        if concat_string[i] != concat_string[i + 1]:
            flags_delete[i] = True
            flags_delete[i + 1] = True
            num_delete += 2
            i += 2
        else:
            i += 1
           
    if num_delete == 0:
        break
    
    count += num_delete
    concat_string = [concat_string[i] for i in range(len(concat_string)) if flags_delete[i] == False]

print(count)
Problem p03286 (python)

Fixed submission:
# coding: utf-8
# Your code here!

import bisect

N = int(input())

if N == 0:
    print(0)
else:
    plus = [2 ** x for x in range(0, 35, 2)]
    minus = [2 ** x for x in range(1, 35, 2)]
    
    cum_plus = [sum(plus[:x]) for x in range(1, len(plus))]
    cum_minus = [sum(minus[:x]) for x in range(1, len(minus))]
    
    flags = []
    answer = 0
    while answer != N:
        if N - answer > 0:
            insert_index = bisect.bisect_left(cum_plus, N - answer)
            flags.append(insert_index * 2)
            answer += plus[insert_index]
            cum_plus = cum_plus[:insert_index]
        else:
            insert_index = bisect.bisect_left(cum_minus, abs(N - answer))
            flags.append(insert_index * 2 + 1)
            answer -= minus[insert_index]
            cum_minus = cum_minus[:insert_index]
            
    output = [0] * (flags[0] + 1)
    for flag in flags:
        output[flag] = 1
     
    print(''.join([str(x) for x in output[::-1]]))

Buggy submission (verdict: Runtime Error):
# coding: utf-8
# Your code here!

import bisect

N = int(input())

plus = [2 ** x for x in range(0, 35, 2)]
minus = [2 ** x for x in range(1, 35, 2)]

cum_plus = [sum(plus[:x]) for x in range(1, len(plus))]
cum_minus = [sum(minus[:x]) for x in range(1, len(minus))]

flags = []
answer = 0
while answer != N:
    if N - answer > 0:
        insert_index = bisect.bisect_left(cum_plus, N - answer)
        flags.append(insert_index * 2)
        answer += plus[insert_index]
        cum_plus = cum_plus[:insert_index]
    else:
        insert_index = bisect.bisect_left(cum_minus, abs(N - answer))
        flags.append(insert_index * 2 + 1)
        answer -= minus[insert_index]
        cum_minus = cum_minus[:insert_index]
        
output = [0] * (flags[0] + 1)
for flag in flags:
    output[flag] = 1
 
print(''.join([str(x) for x in output[::-1]]))
Problem p03329 (python)

Fixed submission:
# coding: utf-8
# Your code here!
import math
from collections import defaultdict
 
N = int(input())
 
memo = defaultdict(lambda: -1)
memo[0] = 0
def recursive(n):
    if memo[n] != -1:
        return memo[n]
    
    min_num_operation = n
    
    index = 0
    temp_n = n
    while temp_n > 0:
        if temp_n % 9 == 0:
            index += 1
            temp_n //= 9
        else:
            index += int(math.log(temp_n, 9))
            break
    if index > 0:
        min_num_operation = min(min_num_operation, 1 + recursive(int(n - math.pow(9, index))))
        
    index = 0
    temp_n = n
    while temp_n > 0:
        if temp_n % 6 == 0:
            index += 1
            temp_n //= 6
        else:
            index += int(math.log(temp_n, 6))
            break
    if index > 0:
        min_num_operation = min(min_num_operation, 1 + recursive(int(n - math.pow(6, index))))
        
    memo[n] = min_num_operation
    
    return min_num_operation
 
print(recursive(N))

Buggy submission (verdict: Runtime Error):
# coding: utf-8
# Your code here!
import math

N = int(input())

memo = [-1] * int(1e5)
memo[0] = 0
def recursive(n):
    if memo[n] != -1:
        return memo[n]
    
    min_num_operation = int(1e9)
    
    index = int(math.log(n, 9))
    if index > 0:
        min_num_operation = min(min_num_operation, 1 + recursive(int(n - math.pow(9, index))))
        
    index = int(math.log(n, 6))
    if index > 0:
        min_num_operation = min(min_num_operation, 1 + recursive(int(n - math.pow(6, index))))
        
    min_num_operation = min(min_num_operation, n)
    
    memo[n] = min_num_operation
    
    return min_num_operation

print(recursive(N))

Buggy submission (verdict: Runtime Error):
# coding: utf-8
# Your code here!
import math

N = int(input())

memo = [-1] * int(1e5)
def recursive(n):
    if memo[n] != -1:
        return memo[n]
    
    min_num_operation = int(1e9)
    
    index = int(math.log(n, 9))
    if index > 0:
        min_num_operation = min(min_num_operation, 1 + recursive(int(n - math.pow(9, index))))
        
    index = int(math.log(n, 6))
    if index > 0:
        min_num_operation = min(min_num_operation, 1 + recursive(int(n - math.pow(6, index))))
        
    min_num_operation = min(min_num_operation, n)
    
    memo[n] = min_num_operation
    
    return min_num_operation
    
 
print(recursive(N))
Problem p02831 (python)

Fixed submission:
from fractions import gcd
a, b = map(int, input().split())
print(a * b // gcd(a, b))

Buggy submission (verdict: Runtime Error):
from math import gcd

a, b = map(int, input().split())
print(int(a * b / gcd(a, b)))

Buggy submission (verdict: Runtime Error):
from math import gcd

a, b = map(int, input().split())
print(int(a * b / gcd(a, b)))

Buggy submission (verdict: Runtime Error):
from math import gcd

a, b = map(int, input().split())
print(int(a * b / gcd(a, b)))

Buggy submission (verdict: Runtime Error):
n = int(input())
s, t = input().split()
a = ''
for i in range(n):
    a += s[i] + t[i]
print(a)
Problem p02954 (python)

Fixed submission:
from bisect import bisect_left

s = input()
n = len(s)
ans = [0] * n
l = [i for i in range(n - 1) if s[i] == 'R' and s[i + 1] == 'L']
for i in range(n):
    b = bisect_left(l, i)
    if s[i] == 'R':
        if (l[b] - i) % 2 == 1:
            ans[l[b] + 1] += 1
        else:
            ans[l[b]] += 1
    elif s[i] == 'L':
        if (i - l[b - 1]) % 2 == 1:
            ans[l[b - 1] + 1] += 1
        else:
            ans[l[b - 1]] += 1
a = ''
for i in range(n):
    if i != n - 1:
        a += str(ans[i]) + " "
    else:
        a += str(ans[i])
print(a)

Buggy submission (verdict: Runtime Error):
from bisect import bisect_left

s = input()
n = len(s)
ans = [0] * n
l = [i for i in range(n - 1) if s[i] == 'R' and s[i + 1] == 'L']
for i in range(n):
    b = bisect_left(l, i)
    print(b, l)
    if s[i] == 'R':
        if (l[b] - i) % 2 == 1:
            ans[l[b] + 1] += 1
        else:
            ans[l[b]] += 1
    elif s[i] == 'L':
        if (i - l[b - 1]) % 2 == 1:
            ans[l[b - 1] + 1] += 1
        else:
            ans[l[b - 1]] += 1
print(ans)
Problem p03077 (python)

Fixed submission:
n = int(input())
l = [int(input()) for i in range(5)]
l2 = []
for i in range(5):
    if n % l[i] == 0:
        l2.append(n // l[i])
    else:
        l2.append(n // l[i] + 1)
l2.sort(reverse = True)
print(l2[0] + 4)

Buggy submission (verdict: Runtime Error):
n = int(input())
l = (int(input()) for i in range(5))
l2 = []
for i in range(5):
    if n % l[i] == 0:
        l2.append(n // l[i])
    else:
        l2.append(n // l[i] + 1)
l2.sort(reverse = True)
print(l2[0] + 4)
Problem p03127 (python)

Fixed submission:
from fractions import gcd

n = int(input())
a = list(map(int, input().split()))
g = a[0]
for i in range(1, n):
    g = gcd(g, a[i])
print(g)

Buggy submission (verdict: Runtime Error):
from math import gcd

n = int(input())
a = list(map(int, input().split()))
g = a[0]
for i in range(1, n):
    g = gcd(g, a[i])
print(g)
Problem p02689 (python)

Fixed submission:
import numpy as np

n,m = map(int,input().split())
h=list(map(int,input().split()))

l=[0]*n
for i in range(m):
    x,y=map(int,input().split())
    if h[x-1]<=h[y-1]:
        l[x-1]=1
    if h[x-1]>=h[y-1]:
        l[y-1]=1

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

print(count)

Buggy submission (verdict: Runtime Error):
import numpy as np

n,m = map(int,input().split())
h=list(map(int,input().split()))

mat=np.zeros((n,n),np.uint8)
for i in range(m):
    x,y= map(int,input().split())
    mat[x-1,y-1]+=1
    mat[y-1,x-1]+=1

count=0
for i in range(n):
    for j in range(n):
        if mat[i,j]!=0 and h[i]<=h[j] and i!=j:
            break
        elif j==n-1:
            count+=1

print(count)
Problem p02682 (python)

Fixed submission:
A,B,C,D = map(int,input().split())

if D > A:
 
    ancer = 1 * A + 0 * (D - A) + (-1 *(D - A - B))
    
else:
    ancer = 1 * D

print(ancer)

Buggy submission (verdict: Runtime Error):
A,B,C,D = map(int,input().split())
# a = [1] * A + [0] * B + [-1] * C
if (A+B+C) == D:
    
    a = 1 * A + 0 * B + (-1 * C)
    
elif (B >= (D-A)) and (C >= (D-A+B)):
    a = 1 * A + 0 * B + (-1 * (D-A+B))
    
elif A >= D:
    a = 1 * D
    
print(a)

Buggy submission (verdict: Runtime Error):
A,B,C,D = map(int,input().split())
a = [1] * A + [0] * B + [-1] * C

ancer = 0
for i in range(D):
    ancer += a[i]
    
print(ancer)
Problem p02897 (python)

Fixed submission:
# -*- coding: utf-8 -*-
# 整数の入力
import math
 
a = float(input())
 
gusu = a / 2
ngusu = math.floor(gusu)
 
if gusu > ngusu:
  kisu = ngusu + 1
else:
  kisu = ngusu
kaku = kisu / a 

print(kaku)

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
# 整数の入力
import math
 
a = int(input())
 
gusu = a // 2
ngusu = math.floor(gusu)
 
if gusu > ngusu:
  kisu = ngusu + 1
else:
  kisu = ngusu
kaku = kisu // a 
print(raund(kaku,6)

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
# 整数の入力
import math

a = int(input())
 
gusu = a // 2
ngusu = math.floor(gusu)
 
if gusu > ngusu:
  kisu = ngusu + 1
else:
  kisu = ngusu

kakuritu　= kisu // a 
print(kakuritu)
Problem p02693 (python)

Fixed submission:
K = int(input())
A, B = map(int, input().split())
flag=0
for i in range(A,B+1):
  if i%K==0:
    print('OK')
    flag=1
    break
if flag==0:
	print('NG')

Buggy submission (verdict: Runtime Error):
A, B, N = map(int, input().split())
Ymax=0
if A*N < B:
  print(0)
else:
  x = B-1
  Ymax = A*x//B - A*(x//B)
  print(Ymax)
Problem p02987 (python)

Fixed submission:
S = input()
S=str(S)

if S[0]==S[1]:
  if S[2]==S[3]:
    if S[0] != S[2]:
      print('Yes')
    else:
      print('No')
  else:
  	print('No')
elif S[0]==S[2]:
  if S[1]==S[3]:
    if S[0] != S[1]:
      print('Yes')
    else:
      print('No')
  else:
  	print('No')
elif S[0]==S[3]:
  if S[1]==S[2]:
    if S[0] != S[1]:
      print('Yes')
    else:
      print('No')
  else:
  	print('No')
else:
	print('No')

Buggy submission (verdict: Runtime Error):
S = input()
S=str(S)

if S[0]==S[1]:
  if S[2]==S[3]:
    if S[0] != S[2]:
    	print('Yes')
    else:
      print('No')
  else:
  	print('No')
elif S[0]==S[2]:
  if S[1]==S[3]:
    if S[0] != S[1]:
    	print('Yes')
    else:
      print('No')
  else:
  	print('No')
elif S[0]==S[3]:
  if S[1]==S[2]:
    if S[0] != S[1]:
    print('Yes')
    else:
      print('No')
  else:
  	print('No')
else:
	print('No')
Problem p02659 (python)

Fixed submission:
a,b=map(str,input().split())
a = int(a)
b = list(b)
del b[1]
b = int(''.join(b))
print(a*b//100)

Buggy submission (verdict: Runtime Error):
a,b = map(str,input().split())
b.replace(".")
print(a*b//100)
Problem p02659 (python)

Fixed submission:
import math

A,B=input().split()

A = int(A)
N,F = B.split('.')
ans = (A * 100 * int(N) + A * int(F)) //100

print(ans)

Buggy submission (verdict: Runtime Error):
N = int(input())
A = list(map(int, input().split()))
ans = 1

limit = pow(10,18)

f = 0
for i in range(N):
    if A[i] != 0:
        ans *= A[i]
    else:
        print(0)
        exit()

    if ans > limit:
        f = 1

if f == 1:
    print(-1)
else:
    print(ans)
Problem p03038 (python)

Fixed submission:
N, M = list(map(int, input().split()))
A = list(map(int, input().split()))
CB = []
for i in range(M):
    B, C = list(map(int, input().split()))
    CB.append([C, B])
 
A.sort()
A.reverse()
CB.sort()
CB.reverse()
 
ans = 0
a_index = 0
cb_index = 0
f = 0
 
while True:
    if f == 1 or A[a_index] >= CB[cb_index][0]:
        ans += A[a_index]
        a_index += 1
        N -= 1
        if N == 0:
            break
    else:
        if N <= CB[cb_index][1]:
            ans += CB[cb_index][0] * N
            break
        else:
            ans += CB[cb_index][0] * CB[cb_index][1]
            N -= CB[cb_index][1]
            cb_index += 1
            if len(CB) == cb_index:
              f = 1
 
print(ans)

Buggy submission (verdict: Runtime Error):
N, M = list(map(int, input().split()))
A = list(map(int, input().split()))
CB = []
for i in range(M):
    B, C = list(map(int, input().split()))
    CB.append([C, B])
 
A.sort()
A.reverse()
CB.sort()
CB.reverse()
 
ans = 0
a_index = 0
cb_index = 0
f = 0
 
while True:
    if f = 1 or A[a_index] >= CB[cb_index][0]:
        ans += A[a_index]
        a_index += 1
        N -= 1
        if N == 0:
            break
    else:
        if N <= CB[cb_index][1]:
            ans += CB[cb_index][0] * N
            break
        else:
            ans += CB[cb_index][0] * CB[cb_index][1]
            N -= CB[cb_index][1]
            cb_index += 1
            if len(CB) == cb_index:
              f = 1
 
print(ans)
Problem p03634 (python)

Fixed submission:
import sys

sys.setrecursionlimit(10 ** 7)

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

Q, K = map(int, input().split())
K -= 1
dist = [pow(10, 9) + 1] * N
dist[K] = 0


def dfs(s, before=-1):
    for edge in edges[s]:
        to = edge[0]
        c = edge[1]
        if to != before:
            dist[to] = dist[s] + c
            dfs(to, s)


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

Buggy submission (verdict: Runtime Error):
N = int(input())
edges = [[] for _ in range(N)]
for _ in range(1, N):
    a, b, c = map(int, input().split())
    a, b = a - 1, b - 1
    edges[a].append([b, c])
    edges[b].append([a, c])

Q, K = map(int, input().split())
K -= 1
dist = [10**9 + 1] * N
dist[K] = 0


def dfs(s, before=-1):
    for edge in edges[s]:
        to = edge[0]
        c = edge[1]
        if to != before:
            dist[to] = dist[s] + c
            dfs(to, s)


dfs(K)
X = []
Y = []
for _ in range(Q):
    x, y = map(int, input().split())
    X.append(x-1)
    Y.append(y-1)

for i in range(Q):
    print(dist[X[i]] + dist[Y[i]])

Buggy submission (verdict: Runtime Error):
N = int(input())
edges = [[] for _ in range(N)]
for _ in range(1, N):
    a, b, c = map(int, input().split())
    a, b = a - 1, b - 1
    edges[a].append([b, c])
    edges[b].append([a, c])

Q, K = map(int, input().split())
K -= 1
dist = [pow(10, 9) + 1] * N
dist[K] = 0


def dfs(s, before=-1):
    for edge in edges[s]:
        to = edge[0]
        c = edge[1]
        if to != before:
            dist[to] = dist[s] + c
            dfs(to, s)


dfs(K)
X = []
Y = []
for _ in range(Q):
    x, y = map(int, input().split())
    X.append(x-1)
    Y.append(y-1)

for i in range(Q):
    print(dist[X[i]] + dist[Y[i]])

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

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

dist = [pow(10, 9) + 1] * N
dist[K] = 0


def dfs(s, before=-1):
    for edge in edges[s]:
        to = edge[0]
        c = edge[1]
        if to != before:
            dist[to] = dist[s] + c
            dfs(to, s)


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