Harryxun/sft_time huggingface dataset:
### BUGGY CODE ###

N = int(input())
C=list(input())
type=set(C)
L=0
R=0
X=0
if 'R' in type and 'W' in type:
    for i in range(N):
      L=i
      if L==N-1-R:
        break
      if C[i]=='W':
        for ii in range(N-L-R):
          if C[N-1-R-ii]=='R':
            C[L]='R'
            C[N-1-R-ii]='W'
            X+=1
            R+=ii+1
            break
          if L==N-1-R:
            break
print(X)

### FIXED CODE ###

N = int(input())
C=list(input())
type=set(C)
L=0
R=0
RR=0
X=0
if 'R' in type and 'W' in type:
    for i in range(N):
      L=i
      R=RR
      if L>=N-1-R:
        break
      if C[i]=='W':
          for ii in range(N-L-R):
            if C[N-1-R-ii]=='R':
              C[L]='R'
              C[N-1-R-ii]='W'
              X+=1
              break
            RR+=1
            if L>=N-1-R:
              break
print(X)
### BUGGY CODE ###

n, q = map(int, input().split())
s = input()
for i in range(q):
    l, r = map(int, input().split())
    start = s[l-1:r].find('A')
    end = s[l-1:r].rfind("C")
    if start == -1 or end== -1 or end <= start:
        print(0)
        continue
    cand = s[l-1:r][start:end+1]
    print(cand.count("AC"))

### FIXED CODE ###

N, Q = map(int, input().split())
S = input()

t = [0] * (N)
for i in range(N-1):
    t[i+1] = t[i] + (1 if S[i:i+2]=="AC" else 0)

for j in range(Q):
    l, r = map(int, input().split())
    print(t[r-1] - t[l-1])
### BUGGY CODE ###

from copy import deepcopy
from sys import stdin
def S(): return stdin.readline().rstrip()
def I(): return int(stdin.readline().rstrip())

n = I()
s = list(S())
sj = deepcopy(s) 

c = 0
for i in s:
    sj.pop(0)
    sk = deepcopy(sj)
    if len(sk) <= 0:
        break
    cj = 0
    for j in sj:
        cj += 1
        sk.pop(0)
        if i != j:
            ck = 0
            for k in sk:
                ck += 1
                if i!=k and j!=k and cj != ck:
                    c += 1

print(c)

### FIXED CODE ###

from sys import stdin
def S(): return stdin.readline().rstrip()
def I(): return int(stdin.readline().rstrip())
 
n = I()
s = list(S())

r = s.count('R')
g = s.count('G')
b = s.count('B')
 
ans = r*g*b
for i in range(n-1):
    for j in range(1,n):
        if i-j<0 or n<=i+j:
            break
        else:
            if s[i-j]!=s[i] and s[i]!=s[i+j] and s[i-j]!=s[i+j]:
                ans-=1
 
print(ans)
### BUGGY CODE ###

n=int(input())
a=list(map(int,input().split()))
cnt=0
for i in range(n):
    for j in range(i+1,n):
        if a[a[i]-1]==i+1:
            cnt+=1
            a[i]=0
            break
print(cnt)

### FIXED CODE ###

n=int(input())
a=list(map(int,input().split()))
cnt=0
for i in range(n):
    if a[a[i]-1]==i+1:
        cnt+=1
        a[i]=0
        continue
print(cnt)
### BUGGY CODE ###

#input
K = int(input())
S = str(input())

#output
# (o以外) o (o以外) o (f以外) f (何でも)


#こちらもmodで割った余り。maxが決まっていて、大きくない時はこちらが早い。
mod = pow(10, 9) + 7

def cmb(n, r):
    res = 1
    fac = 1
    for i in range(r):
        res *= (n-i)
        res %= mod
        fac *= (i+1)
        fac %= mod
    return res*pow(fac, mod-2, mod) % mod

#重複組み合わせはH(n, r) = cmb(n+r-1, r)

N = len(S)
answer = 0
for i in range(K+1):
    answer += pow(25, i, mod) * pow(26, K-i, mod) * cmb(N-1+i, N-1) % mod
print(answer % mod)

### FIXED CODE ###

#atcoder template
def main():
    import sys
    imput = sys.stdin.readline
    #文字列入力の時は上記はerrorとなる。
    #ここにコード

    #N = 1のときなどcorner caseを確認！
if __name__ == "__main__":
    main()

#input
K = int(input())
S = str(input())

#output
# (o以外) o (o以外) o (f以外) f (何でも)

#こちらもmodで割った余り。maxが決まっていて、大きくない時はこちらが早い。
mod = pow(10, 9) + 7

n_ = 5 * pow(10, 6)
fun = [1] * (n_+1)
for i in range(1, n_+1):
    fun[i] = fun[i-1] * i % mod
rev = [1] * (n_+1)
rev[n_] = pow(fun[n_], mod-2, mod)
for i in range(n_-1, 0, -1):
    rev[i] = rev[i+1] * (i+1) % mod
def cmb(n,r):
    if n < 0 or r < 0 or r > n: return 0
    return fun[n] * rev[r] % mod * rev[n-r] % mod

#重複組み合わせはH(n, r) = cmb(n+r-1, r)

L = len(S)
answer = 0
for i in range(L, L+K+1):
    answer += pow(25, i-L, mod) * pow(26, L+K-i, mod) * cmb(i-1, L-1)
    answer %= mod
print(answer)
### BUGGY CODE ###

from collections import deque
# 全部ゼロ始まり
n, x, y = map(int, input().split())
G = [[] for _ in range(n)]
for i in range(n-1):
    G[i].append(i+1)
for i in range(1, n):
    G[i].append(i-1)
x -= 1
y -= 1
G[x].append(y)
G[y].append(x)

# iとjの最短距離を返す
def bfs(i, j):
    i -= 1
    j -= 1
    q = deque()
    seen = [False] * n
    dist = [-1] * n

    q.append(i)
    seen[i] = True
    dist[i] = 0
    while len(q) > 0:
        now_v = q.popleft()
        for new_v in G[now_v]:
            if seen[new_v] is True:
                continue
            q.append(new_v)
            seen[new_v] = True
            dist[new_v] = dist[now_v] + 1
    return dist[j]

cnt = [0] * (n-1)
for i in range(n-1):
    for j in range(i+1, n):
        cnt[bfs(i, j)-1] += 1

for ans in cnt:
    print(ans)

### FIXED CODE ###

n, x, y = map(int, input().split())
x -= 1
y -= 1
cnt = [0] * n
for i in range(n-1):
    for j in range(i+1, n):
        direct = j - i
        detour = abs(x-i) + abs(y-j) + 1
        d = min(direct, detour)
        cnt[d] += 1
for k in range(1, n):
    print(cnt[k])
### BUGGY CODE ###

N,M = map(int,input().split())
for gcd in range(M // N,0,-1):
 if M % gcd == 0:
  print(gcd)
  break

### FIXED CODE ###

def calc():
 N,M = map(int,input().split())
 for gcd in range(M // N,0,-1):
  if M % gcd == 0:
   break
 return(gcd)
print(calc())
### BUGGY CODE ###

n = int(input())
A = list(map(int,input().split()))
money = 1000
stock = 0
for i in range(n-1):
    if A[i] >= A[i+1]:
        while stock:
            stock -= 1
            money += A[i]
    else:
        while money >= A[i]:
            money -= A[i]
            stock += 1

print(stock * A[n-1] + money)

### FIXED CODE ###

n = int(input())
A = list(map(int,input().split()))
money = 1000
stock = 0
for i in range(n-1):
    if A[i] >= A[i+1]:
        money += stock * A[i]
        stock = 0
    elif money >= A[i]:
        stock = money // A[i]
        money -= stock * A[i]
print(stock * A[n-1] + money)
### BUGGY CODE ###

from functools import lru_cache


@lru_cache(maxsize=None)
def f(x):
    if x == 0:
        return 0
    return f(x % bin(x).count('1')) + 1


n = int(input())
x = int(input(), base=2)
buf = []

for i in range(n):
    buf.append(f(x ^ (1 << i)))

buf.reverse()

print('\n'.join(map(str, buf)))


### FIXED CODE ###

from functools import lru_cache


@lru_cache(maxsize=None)
def f(x):
    if x == 0:
        return 0
    return f(x % bin(x).count('1')) + 1


n = int(input())
x = input()
d = x.count('1')
y = int(x, base=2)
y0 = y % (d + 1)
y1 = y % (d - 1) if d > 1 else 0
buf = []

for i in range(n):
    if x[i] == '0':
        m = d + 1
        z = (y0 + pow(2, n - i - 1, m)) % m
        # print(0, y0, pow(2, n - i - 1, m))
    else:
        m = d - 1
        if m == 0:
            buf.append(0)
            continue
        z = (y1 - pow(2, n - i - 1, m)) % m
        # print(1, y1, pow(2, n - i - 1, m))
    # print(i, z)
    buf.append(f(z) + 1)

print('\n'.join(map(str, buf)))
### BUGGY CODE ###

#!/usr/bin/env python3
from itertools import product, permutations, combinations
from bisect import bisect_right,bisect_left
INF = float('inf')

N, M=map(int, input().split())
dp = [INF] * 2**N
dp[0] = 0
for i in range(M):
    A, B = map(int, input().split())
    C = sum([2**(int(i)-1) for i in input().split()])
    for s in range(2**N):
        t = s | C
        dp[t] = min(dp[t], dp[s] + A)
ans = dp[-1]
if ans == INF:
    ans = -1
print(ans)


### FIXED CODE ###

#!/usr/bin/env python3
n, m = map(int, input().split())
dp = [float('inf')] * 2**n
dp[0] = 0
for _ in range(m):
    a, _ = map(int, input().split())
    c = sum([2**(int(i)-1) for i in input().split()])
    for s in range(2**n):
            t = s|c
            if dp[t] > dp[s] + a:
                dp[t] = dp[s] + a
ans = dp[-1]
if ans == float('inf'):
    ans = -1
print(ans)
### BUGGY CODE ###

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

A = set(a)

ans = 0
for i in A:
    cnt = 0
    J = [i, i - 1, i + 1]
    for j in J:
        if j in a:
            cnt += a.count(j)
    ans = max(ans, cnt)
print(ans)


### FIXED CODE ###

N = int(input())
a = sorted(list(map(int, input().split())))

cnt = [0] * (10**5 + 2)

for i in a:
    cnt[i] += 1
    cnt[i + 1] += 1
    cnt[i + 2] += 1

print(max(cnt))
### BUGGY CODE ###

n, m = map(int, input().split())
c = [0]*(n+1)
for i in range(m):
    l, r = map(int, input().split())
    for j in range(l, r+1):
        c[j] += 1

ans = 0
for ci in c:
    if ci==m:
        ans+=1
print(ans)

### FIXED CODE ###

n, m = map(int, input().split())
c = [0]*(n+1)
L, R = [], []
for i in range(m):
    l, r = map(int, input().split())
    L.append(l)
    R.append(r)
maxL = max(L)
minR = min(R)
print(max(minR+1-maxL, 0))
### BUGGY CODE ###

def judge(numlist):
    if numlist[2] + numlist[0] == 2 * numlist[1]:
        return False
    else:
        return True

N = int(input())
S = list(input())
R = []
G = []
B = []

for i in range(N):
    if S[i] == 'R':
        R.append(i)
    elif S[i] == 'G':
        G.append(i)
    else:
        B.append(i)

RGcombs = []

for i in R:
    for j in G:
        if i < j:
            RGcombs.append([i,j])
        else:
            RGcombs.append([j,i])

ans = 0
for comb in RGcombs:
    for k in B:
        if k < comb[0]:
            if judge([k]+comb):
                ans += 1
        elif k > comb[1]:
            if judge(comb+[k]):
                ans += 1
        else:
            if judge([comb[0],k,comb[1]]):
                ans += 1

print(ans)

### FIXED CODE ###

N = int(input())
S = list(input())
r = 0
g = 0
b = 0

for i in range(N):
    if S[i] == 'R':
        r += 1
    elif S[i] == 'G':
        g += 1
    else:
        b += 1

allcnt = r * g * b

sub = 0
for i in range(N):
    for j in range(i+1,N):
        if S[i] == S[j]:
            None
        else:
            k = 2*j-i
            if k >= N or S[i] == S[k] or S[j] == S[k]:
                None
            else:
                sub += 1

print(allcnt - sub)
### BUGGY CODE ###

import functools
import operator

N = int(input())
v = list(map(int, input().split()))
answer = functools.reduce(operator.mul, v) if functools.reduce(operator.mul, v) <= 1000000000000000000 else -1
print(answer)

### FIXED CODE ###

N = int(input())
v = list(map(int, input().split()))
limit = 10 ** 18

answer = 1
if 0 in v:
  answer = 0
else:
  for i in v:
    answer = answer * i
    if answer <= limit:
      continue
    else:
      answer = -1
      break
print(answer)
### BUGGY CODE ###

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

minimum = max(A,B)

for i in range(minimum, A*B+1):
  if i % A == 0 and i % B == 0:
    print(i)
    break

### FIXED CODE ###

import math

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

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

print(lcm(A,B))
### BUGGY CODE ###

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

i = M // N
while M % i:
    i -= 1

print(i)


### FIXED CODE ###

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

div = M
for i in range(1, int(math.sqrt(M))+1):
    if M % i:
        continue
    i2 = M // i
    if i >= N:
        div = min(div, i)
    if i2 >= N:
        div = min(div, i2)

print(M//div)
### BUGGY CODE ###

def main():
  n,k = (int(x) for x in input().split())
  min = n

  while True:
    n = abs(n - k)
    if(n < min):
      min = n
    else:
      break

  print(min)

if __name__ == '__main__':
  main()


### FIXED CODE ###

def main():
  n,k = (int(x) for x in input().split())
  n = n % k
  min = n
  while True:
    n = abs(n - k)
    if(n < min):
      min = n
    else:
      break

  print(min)

if __name__ == '__main__':
  main()
### BUGGY CODE ###

import math
n = int(input())
a = list(map(int,input().split()))
s = []
t = [0]*169
u = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97,
     101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199,
     211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293,
     307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397,
     401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499,
     503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599,
     601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691,
     701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797,
     809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887,
     907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997, 1009]

m = 169
for i in range(n):
    for j in range(m):
        if(a[i]%u[j] == 0):
            t[j] += 1
            while(a[i]%u[j] == 0):
                a[i] //= u[j]
            if(a[i] == 1):
                break
    if(a[i] != 1):
        s.append(a[i])
if(max(t) == n):
    print('not coprime')
elif(max(t) <= 1):
    z = 0
    for i in range(1,len(s)):
        for j in range(i):
            if(math.gcd(s[i],s[j]) > 1):
                z = 1
                break
    if(z == 1):
        print('setwise coprime')
    else:
        print('pairwise coprime')
else:
    print('setwise coprime')

### FIXED CODE ###

n = int(input())
a = list(map(int,input().split()))
r = max(a)+1
s = []
t = [0]*r
for i in range(r):
    s.append([])
for i in range(2,r):
    if(s[i] == []):
        for j in range(1,-(-r//i)):
            s[i*j].append(i)
for i in range(n):
    for j in range(len(s[a[i]])):
        t[s[a[i]][j]] += 1
if(max(t) == n):
    print('not coprime')
elif(max(t) <= 1):
    print('pairwise coprime')
else:
    print('setwise coprime')
### BUGGY CODE ###

n=int(input())
a=[0]*n
b=[0]*n
for i in range(n):
  a[i],b[i]=map(int,input().split())

for i in range(n):
  suma=sum(a)
  maxb=max(b)
  if suma<=maxb:
    index=b.index(maxb)
    del a[index]
    del b[index]
  else:
    print('No')
    exit()
print('Yes')


### FIXED CODE ###

n = int(input())
tasks = [list(map(int, input().split())) for i in range(n)]
tasks.sort(key=lambda x: x[1])

time = 0
for task in tasks:
    time += task[0]
    if time > task[1]:
        print("No")
        exit()
print("Yes")
### BUGGY CODE ###

import sys
import itertools
# import numpy as np
import time
import math

sys.setrecursionlimit(10 ** 7)

from collections import defaultdict

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

s = readline().decode().strip()
q = int(readline())
is_reversed = False
qs = read().decode().split("\n")
for i in range(q):
    query = qs[i].split()
    t = int(query[0])
    if t == 1:
        is_reversed = not is_reversed
    elif t == 2:
        f = int(query[1])
        c = query[2]
        if f == 1:
            if is_reversed:
                s += c
            else:
                s = c + s
        elif f == 2:
            if is_reversed:
                s = c + s
            else:
                s += c
if is_reversed:
    print(s[::-1])
else:
    print(s)

### FIXED CODE ###

import sys
import itertools
# import numpy as np
import time
import math

sys.setrecursionlimit(10 ** 7)

from collections import defaultdict

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

s = readline().decode().strip()
q = int(readline())
is_reversed = False
qs = read().decode().split("\n")
left = []
right = [] 
for i in range(q):
    query = qs[i].split()
    t = int(query[0])
    if t == 1:
        is_reversed = not is_reversed
    elif t == 2:
        f = int(query[1])
        c = query[2]
        if f == 1:
            if is_reversed:
                right.append(c)
            else:
                left.append(c)
        elif f == 2:
            if is_reversed:
                left.append(c)
            else:
                right.append(c)

s = "".join(reversed(left)) + s + "".join(right)
if is_reversed:
    print(s[::-1])
else:
    print(s)
### BUGGY CODE ###

from fractions import gcd
mod = 10**9+7
n = int(input())
a = list(map(int,input().split()))
l = 1
ans = 0
for i in range(n):
    l = l*a[i]//gcd(l,a[i])
for i in range(n):
    ans = (ans + l//a[i]) % mod
print(ans)

### FIXED CODE ###

from fractions import gcd
mod = 10**9+7
n = int(input())
a = list(map(int,input().split()))
ans = 0
l = 1
for i in a:
    l = l*i//gcd(l,i)
l %= mod
for i in a:
    ans = (ans +l*pow(i,mod-2,mod)) % mod
print(ans)
### BUGGY CODE ###

n = int(input())
x = [int(i) for i in input().split()]
a = x.copy()
x.sort()
for i in a:
    num = x.index(i)
    if num < n//2:
        print(x[int(n//2)])
    else:
        print(x[int(n//2)-1])

### FIXED CODE ###

n = int(input())
x = [int(i) for i in input().split()]
y = x.copy()
y.sort()
y_e = y[n//2]
y_o = y[n//2 - 1]
for xx in x:
    if xx >= y_e:
        print(y_o)
    else:
        print(y_e)
### BUGGY CODE ###

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

bc = []
for i in range(m):
    b, c = map(int, input().split())
    bc += [c] * b

print(sum(sorted(a + bc, reverse=True)[:len(a)]))

### FIXED CODE ###

from collections import Counter

n, m = map(int, input().split())
a = map(int, input().split())

abc = Counter(a).most_common()
for i in range(m):
    b, c = map(int, input().split())
    abc.append((c, b))

abc.sort(reverse=True)

ans = 0
count = n
for c, b in abc:
    rep = min(count, b)
    ans += c * rep
    count -= rep
print(ans)
### BUGGY CODE ###

S = str(input())
Q = int(input())

isreverse = False

for i in range(Q):
    query = input()
    if str(query)[0]=="1":
        isreverse = isreverse^True
    else:
        F = int(query.split()[1])
        C = str(query.split()[2])
        if (F==1 and isreverse==False) or (F==2 and isreverse==True):
            S = C+S
        else:
            S = S+C
if isreverse==False:
    print(S)
else:
    print(S[::-1])

### FIXED CODE ###

S = str(input())
Q = int(input())

isreverse = False
head=[]
tail=[]

for i in range(Q):
    query = input()
    if str(query)[0]=="1":
        isreverse = isreverse^True
    else:
        F = int(query.split()[1])
        C = str(query.split()[2])
        if (F==1 and isreverse==False) or (F==2 and isreverse==True):
            head.append(C)
        else:
            tail.append(C)


if isreverse==False:
    print("".join(reversed(head))+S+"".join(tail))
else:
    print("".join(reversed(tail))+S[::-1]+"".join(head))
### BUGGY CODE ###

n = int(input())
a = [int(s) for s in input().split()]
ans = 1
for i in a:
    ans *= i
print(ans) if ans <= (10 ** 18) else print(-1)

### FIXED CODE ###

def main():
    n = int(input())
    a = [int(s) for s in input().split()]
    ans = 1
    if 0 in a:
        print(0)
        return
    for i in a:
        ans *= i
        if ans > (10 ** 18):
            ans = -1
            break
    print(ans)
main()
### BUGGY CODE ###

import sys
N,K,Q = map(int,input().split())
if K > Q:
    for i in range(N):
        print('Yes')
    sys.exit(0)
else:
  res = dict()
  for i in range(Q):
    answ = int(input())
    if answ not in res:
      res[answ] = 0
    res[answ] += 1
res2 = [answ[0] for answ in res.items() if answ[1] >= (Q-K+1)]
for i in range(1,N+1):
  if i in res2:
    print('Yes')
  else:
    print('No')

### FIXED CODE ###

import sys
N,K,Q = map(int,input().split())
if K > Q:
    for i in range(N):
        print('Yes')
    sys.exit(0)
else:
  res = dict()
  for i in range(N+1):
      res[i] = 0
  for i in range(Q):
    answ = int(input())
    if answ not in res:
      res[answ] = 0
    res[answ] += 1
res2 = [answ[0] for answ in res.items() if answ[1] >= (Q-K+1)]
for i in res2:
    res[i] = -1
for i in range(1,N+1):
    if (res[i] == -1):
        print('Yes')
    else:
        print('No')
### BUGGY CODE ###

N=int(input())

S=[input() for _ in range(N)]
b=[]
i=0
for k in S:
  if k not in b:
    i+=1
    b.append(k)
  else:
    continue
print(i)

### FIXED CODE ###

N=int(input())
 
S=[input() for _ in range(N)]



print(len(set(S)))
### BUGGY CODE ###

n,q=map(int,input().split())
pairs=list(tuple(map(lambda x:int(x)-1,input().split())) for _ in range(n-1))
point=list(tuple(map(lambda x:int(x)-1, input().split())) for _ in range(q))

node=[[] for _ in range(n)]
parent=[None for _ in range(n)]
parent[0]=0
res=[0 for _ in range(n)]

for p in pairs:
  node[p[0]].append(p[1])
  node[p[1]].append(p[0])
  
for s in point:
  res[s[0]]+=(s[1]+1)
    
for i in range(n):
  for c in node[i]:
    if parent[c]==None:
      parent[c]=i
      res[c]+=res[i]
      
print(*res)

### FIXED CODE ###

#import sys
#import numpy as np
import math
#import itertools
#from fractions import Fraction
#import itertools
from collections import deque
from collections import Counter
#import heapq
#from fractions  import gcd
#input=sys.stdin.readline
#import bisect
n,q=map(int,input().split())
v=tuple(tuple(map(lambda x: int(x)-1,input().split())) for _ in range(n-1))

node=[[] for _ in range(n)]
d=deque()
d.append(0)
for pair in v:
    node[pair[0]].append(pair[1])
    node[pair[1]].append(pair[0])
ans=[0]*n
for i in range(q):
    p,x=map(int,input().split())
    ans[p-1]+=x
seen=[0]*n
seen[0]=1
while True:
    if not d:
        break
    i=d.pop()
    for j in node[i]:
        if seen[j]==0:
            d.append(j)
            seen[j]=1
            ans[j]+=ans[i]
print(*ans)
### BUGGY CODE ###

N = int(input())
graph=[[float("INF")]*(N+1) for _ in range(N+1)]
for i in range(N-1):
  a,b,c = [int(x) for x in input().split()]
  graph[a][b] =c
  graph[b][a] =c
for k in range(N+1):
  for i in range(N+1):
    for j in range(N+1):
      if i==j:
        graph[i][j]=0
      graph[i][j] = min(graph[i][j],graph[i][k]+graph[k][j])
Q,K = [int(x) for x in input().split()]
for i in range(Q):
  a,b = [int(x) for x in input().split()]
  print(graph[a][K]+graph[K][b])

### FIXED CODE ###

N = int(input())
E ={i:[] for i in range(N+1)}
for i in range(N-1):
  a,b,c = [int(x) for x in input().split()]
  E[a].append([b,c])
  E[b].append([a,c])
Q,K = [int(x) for x in input().split()]
cost= [0]*(N+1)
stk=[[K,-1,0]]
while stk:
  v,p,c = stk.pop()
  if cost[v]!=0:
    continue
  cost[v]=c
  for i,j in E[v]:
    if i!=p:
      stk.append([i,v,c+j])
for i in range(Q):
  a,b = [int(x) for x in input().split()]
  print(cost[a]+cost[b])