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

X,Y = map(int,input().split())
count = 1

while True:
  if X*2 <= Y:
    count +=1
    X = X*2
    
print(count)

### FIXED CODE ###

import math

x,y = map(int,input().split())
"""
値をどんどん2倍していって、それがyを超えないかを検証し、越えなければカウントを増やす
"""

count = 0
origin = x
while True:
  if origin <= y:
    count+=1
    origin = origin * 2
  else:
    break
    
print(count)
### BUGGY CODE ###

import copy
N, K = [int(i) for i in input().split()]
wvs = [(int(w), int(v)) for w,v in [input().split() for _ in range(N)]]
dp = [0]*(K)
for i, wv in enumerate(wvs):
  w,v = wv
  tmp_dp = copy.deepcopy(dp)
  for j in range(i,K):
    if (j+w) <= K:
      tmp_dp[j+w-1] = max(dp[j+w-1], dp[j-1] + v)
  dp = tmp_dp
print(max(dp))

### FIXED CODE ###

from copy import deepcopy
N, K = [int(i) for i in input().split()]
wvs = [(int(w), int(v)) for w,v in [input().split() for _ in range(N)]]
dp = [0]*(K)
for i, wv in enumerate(wvs):
  w,v = wv
  tmp_dp = deepcopy(dp)
  for j in range(i,K):
    if (j+w) <= K:
      tmp_dp[j+w-1] = max(dp[j+w-1], dp[j-1] + v)
  dp = tmp_dp
print(max(dp))
### BUGGY CODE ###

def warshall_floyd(d):
    for k in range(len(d)):
        for i in range(len(d)):
            for j in range(len(d)):
                d[i][j] = min(d[i][j], d[i][k] + d[k][j])
    return d


if __name__ == "__main__":
    n, x, y = map(int, input().split())

    INF = 10 ** 5

    d = [[INF] * n for _ in range(n)]

    for i in range(n-1):
        d[i][i+1] = 1
        d[i+1][i] = 1

    d[x-1][y-1] = 1
    d[y-1][x-1] = 1

    ans = [0] * (n - 1)

    d = warshall_floyd(d)

    for i in range(n-1):
        for j in range(i+1, n):
            ans[d[i][j]-1] += 1

    for aa in ans:
        print(aa)


### FIXED CODE ###

from collections import deque
from collections import defaultdict


class Graph(object):
    """
    隣接リストによる無向グラフ
    重み無しで距離は１
    """

    def __init__(self):
        self.graph = defaultdict(list)

    def __len__(self):
        return len(self.graph)

    def add_edge(self, src, dst):
        self.graph[src].append(dst)
        self.graph[dst].append(src)

    def get_nodes(self):
        return self.graph.keys()


if __name__ == "__main__":
    n, x, y = map(int, input().split())
    x -= 1
    y -= 1

    g = Graph()
    for i in range(n-1):
        g.add_edge(i, i+1)
    g.add_edge(x, y)

    ans = [0] * (n-1)

    # BFSによる解法
    for i in range(n-1):
        # 始点からの距離
        # -1を未訪問とする
        dist = [-1] * n

        # 初期条件
        # 今回は頂点iから探索を行う
        dist[i] = 0
        que = deque([i])

        while len(que) != 0:
            vertex = que.popleft()
            for v in g.graph[vertex]:
                if dist[v] != -1:
                    pass
                else:
                    tmp = dist[vertex] + 1
                    dist[v] = tmp
                    if v > i:
                        ans[tmp-1] += 1
                    que.extend(g.graph[vertex])

    for a in ans:
        print(a)
### BUGGY CODE ###

N, K = map( int, input().split())
A = []
for i in range(N):
    a, b = map( int, input().split())
    A += [a]*b
    A.sort()
    A = A[:K]
print(A[K-1])

### FIXED CODE ###

N, K = map( int, input().split())
A = [ [0]*2 for _ in range(N)]
for i in range(N):
    A[i] = list(map( int, input().split()))
A.sort()
L = 0
for i in range(N):
    L += A[i][1]
    if L >= K:
        print(A[i][0])
        break
### BUGGY CODE ###

import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
# input=sys.stdin.readline
def resolve():
    #%% import
    from itertools import product
    from collections import defaultdict,deque

    #%% input
    n=int(input())
    # list-likeでhashableなのはstrとtuple
    V=[(i,j) for i,j in product(range(n),range(n)) if i<j]
    N=len(V) # N=n*(n-1)//2

    #%% adjacency list
    outs={v:[] for v in V}
    indeg={v:0 for v in V}
    for i in range(n):
        edge=list(map(lambda x:int(x)-1,input().split()))
        for j in range(len(edge)-1): # 各iにつきn-1個の辺が与えられる
            # from (min(i,edge[j]),max(i,edge[j])) to (min(i,edge[j+1]),max(i,edge[j+1]))
            v0 = (min(i,edge[j]),max(i,edge[j]))
            v1 = (min(i,edge[j+1]),max(i,edge[j+1]))
            outs[v0].append(v1)
            indeg[v1]+=1

    #%% topological sort
    Q=deque(v for v in V if indeg[v]==0)
    res=[]
    #%%
    while(Q):
        v0=Q.popleft()
        res.append(v0)
        for v1 in outs[v0]:
            indeg[v1]-=1
            if indeg[v1]==0: Q.append(v1)

    #%% judge whether G is DAG
    if len(res)<N:
        print("-1")
        return

    #%% LPP(Longest Path Problem)
    LP={v:1 for v in V}
    for v0 in res:
        for v1 in outs[v0]:
            LP[v1]=max(LP[v1],LP[v0]+1)
    print(max(LP.values()))

resolve()

### FIXED CODE ###

import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
def resolve():
    from itertools import product
    n=int(input())
    match=[[0]*n for _ in range(n)]
    V=0 # V=n(n-1)/2
    for i,j in product(range(n),repeat=2):
        if(i>=j): continue
        match[i][j]=V
        V+=1

    def toID(i,j):
        if(i>j): i,j=j,i
        return match[i][j]

    E=[[] for _ in range(V)]
    for i in range(n):
        A=list(map(lambda x:int(x)-1,input().split()))
        for j in range(1,n-1):
            E[toID(i,A[j-1])].append(toID(i,A[j]))
    # あとは頂点VのグラフをdfsしてLongest pathの長さを求める
    # -1: unchecked, -2: checked and uncalculated
    dp=[-1]*V
    flag=True
    def dfs(v):
        global flag
        dp[v]=-2
        length=0
        for nv in E[v]:
            if(dp[nv]==-2):
                print(-1)
                exit()
            if(dp[nv]==-1):
                dfs(nv)
            length=max(length,dp[nv]+1)
        dp[v]=length
    # calculate
    for i in range(V): dfs(i)
    print(max(dp)+1 if(flag) else -1)
resolve()
### BUGGY CODE ###

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

i=0
r="YES"

while(i<l):
  j=i+1
  while(j<l):
    if(list[i]==list[j]):
      r="NO"
    j+=1
  i+=1

print(r)

### FIXED CODE ###

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

def has_duplicates(seq):
    return len(seq) != len(set(seq))

r=has_duplicates(list)

if(str(r)=="False"):
  print("YES")
else:
  print("NO")
### BUGGY CODE ###

import copy
#In=[list(map(int,i.split())) for i in Input.split('\n')]
#N=In[0][0]
#A=In[1]
N=int(input())
A=list(map(int,input().split()))
counter=[0 for i in range(N+1)]
ans2=[0 for i in range(N+1)]
B=[]
for i in A:
    counter[i]+=1
ans=[0 for i in range(N+1)]
for i,value in enumerate(counter):
    if value>0:
        ans[i]=(value*(value-1)//2)
        ans2[i]=((value-1)*(value-2)//2)
for i in A:
    ans1=copy.deepcopy(ans)
    ans1[i]=ans2[i]
    print(sum(ans1))


### FIXED CODE ###

import copy
# In=[list(map(int,i.split())) for i in Input.split('\n')]
# N=In[0][0]
# A=In[1]
N=int(input())
A=list(map(int,input().split()))
counter=[0 for i in range(N+1)]
ans2=[0 for i in range(N+1)]
B=[]
for i in A:
    counter[i]+=1
ans=[0 for i in range(N+1)]
for i,value in enumerate(counter):
    if value>0:
        ans[i]=(value*(value-1)//2)
        ans2[i]=((value-1)*(value-2)//2)
total=sum(ans)
for i in A:
    temp=total
    temp-=ans[i]
    temp+=ans2[i]
    print(temp)
### BUGGY CODE ###

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

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


ans = 10**100

for k in range(1,N+1):
    cur = (N+k) * X

    for i in range(1,N+1):
        pos = (i + k - 1) // k
        if pos == 1:
            cur += 5*a[N-i]
        else:
            cur += (2*pos+1) * a[N-i]
    
    ans = min(ans, cur)

print(ans)


### FIXED CODE ###


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

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

a = a[::-1]

dp = [0 for i in range(N+1)]
for i in range(1,N+1):
    dp[i] = a[i-1] + dp[i-1]

ans = 10**100

for k in range(1,N+1):
    cur = (N+k) * X


    count = 1
    i = k
    while 1:
        first = i - k
        last = min(N,i)
        flag = False
        if i > N:
            flag = True

        if count == 1:
            cur += dp[last] * 5
        else:
            cur += (dp[last] - dp[first]) * (2*count + 1)

        count += 1
        i += k
        
        if flag:
            break


        
    ### print(dp, cur)
    ans = min(ans, cur)

print(ans)
### BUGGY CODE ###

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

cnt = 1
ls = [P[0]]
for i in range(1,N):
    if min(ls) > P[i]:
        cnt += 1
    ls.append(P[i])
print(cnt)

### FIXED CODE ###

import numpy as np

N = int(input())
P = np.array(input().split(),np.int32)

Q = np.minimum.accumulate(P)
cnt = np.count_nonzero(P==Q)

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

N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))
A.sort()
B.sort()
C.sort()
ans = 0
for i in range(N):
    for j in range(N):
        if A[i] < B[j]:
            for k in range(N):
                if B[j] < C[N-1-k]:
                    ans += 1
                else:
                    break
print(ans)

### FIXED CODE ###

import bisect
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))
A.sort()
B.sort()
C.sort()
ans = 0
for i in range(N):
    a = bisect.bisect_left(A, B[i])
    c = bisect.bisect_right(C, B[i])
    ans += a*(N-c)

print(ans)
### BUGGY CODE ###

def main():
    inputs = input().split()
    inputs = [int(i) for i in inputs]

    a = inputs[0]
    b = inputs[1]

    g = gcd(a, b)
    d = {}

    min_n = min([a, b])

    for i in range(2, min_n + 1):
        while True:
            if g % i == 0:
                g /= i
                d[i] = i
            else:
                break

    print(len(d) + 1)

def gcd(x, y):
    if y == 0:
        return x
    return gcd(y, x % y)

if __name__ == "__main__":
    main()


### FIXED CODE ###

import math


def main():
    inputs = input().split()
    inputs = [int(i) for i in inputs]

    a = inputs[0]
    b = inputs[1]

    g = n = gcd(a, b)
    d = {}

    for i in range(2, int(math.sqrt(n)) + 1):
        if n % i:
            continue
        d[i] = i
        while n % i == 0:
            n /= i

    if n != 1:
        d[n] = n

    print(len(d) + 1)


def gcd(x, y):
    if y == 0:
        return x
    return gcd(y, x % y)


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

N,Q=map(int,input().split())
S=input()
for _ in range(Q):
  L = tuple(map(int, input().split()))

  p = L[0] - 1
  c = 0
  while True:
    p = S.find("AC", p)

    if p < 0 or p >= L[1]-1:
      break

    c+=1
    p+=2
  print(c)


### FIXED CODE ###

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

lis = [0] * len(S)
for i in range(len(S)):

  if i>0:
    lis[i] += lis[i-1]
  if i < len(S)-1 and S[i] == 'A' and S[i+1] == 'C':
    lis[i+1] += 1


for _ in range(Q):
  L = tuple(map(int, input().split()))

  c = 0
  print(lis[L[1]-1]-lis[L[0]-1])
### BUGGY CODE ###

N, K, Q = map(int, input().split())
li = [int(input()) for i in range(Q)]
member = [x+1 for x in range(N)]
for i in member:
    ans = K
    for j in li:
        if i != j:
            ans -= 1
    if ans <= 0:
        print('No')
    else:
        print('Yes')

### FIXED CODE ###

N, K, Q = map(int, input().split())
li = [int(input()) for i in range(Q)]
member = [0 for x in range(N)]
for i in li:
    member[i-1] += 1
for j in member:
    ans = j-Q+K
    if ans <= 0:
        print('No')
    else:
        print('Yes')
### BUGGY CODE ###

n = int(input())
s = [input() for _ in range(n)]
dic1 = {}
for i in s:
    if i not in dic1.keys():
        dic1[i] = s.count(i)
dic2 = sorted(dic1.items())

for k in range(len(dic2)):
    if dic2[k][1] == max(dic1.values()):
        print(dic2[k][0])

### FIXED CODE ###

n = int(input())
dic1 = {}
for i in range(n):
    s = input()
    if s not in dic1.keys():
        dic1[s] = 1
    else:
        dic1[s] += 1
dic2 = sorted(dic1.items(), key=lambda j: j[1], reverse=True)

ans_ls = []
for k in range(len(dic2)):
    if dic2[k][1] != dic2[0][1]:
        break
    ans_ls.append(dic2[k][0])
ans_ls.sort()
for _ in ans_ls:
  	print(_)
### BUGGY CODE ###

n,k = map(int, input().split())
ans = 0

while True:
    if n >= k: 
        n //= k
        ans += 1
    elif n <= 1: break

print(ans+1)

### FIXED CODE ###

n,k = map(int, input().split())
ans = 0

while True:
    n //= k
    ans += 1
    if n == 0: break

print(ans)
### BUGGY CODE ###

import numpy as np
N = int(input())
L = list(map(int,input().split()))
count = 0

for n in np.array(range(N)):
    l = L[n]
    L_s = L.copy()
    L_s.pop(n)
    for l_s in L_s:
        if l%l_s == 0:
            break
    else:
        count += 1

print(count)

### FIXED CODE ###

n = int(input())
data = list(map(int, input().split()))
mx = max(data)
marked = [0]*(mx + 1)
for a in data:
    for x in range(a, mx+1, a):
        marked[x] += 1
ans = 0
for a in data:
    if marked[a] == 1:
        ans += 1
print(ans)
### BUGGY CODE ###

n = int(input())
a = [int(input()) for x in range(n)]
b = sorted(a,reverse=True)
for i in range(n):
    if max(a) == a[i]:
        print(b[1])
    else:
        print(b[0])

### FIXED CODE ###

n = int(input())
a = [int(input()) for x in range(n)]
b = sorted(a,reverse=True)
for i in range(n):
    if b[0] == a[i]:
        print(b[1])
    else:
        print(b[0])
### BUGGY CODE ###

import math
import sys
import collections
N=int(input())
Tr=list(input())
R=[]
G=[]
B=[]
for i in range(N):
   if Tr[i]=="R":
       R.append(i)
   if Tr[i]=="G":
       G.append(i)
   if Tr[i]=="B":
       B.append(i)
cnt=0
for i in R:
    for j in G:
        for k in B:
            if abs(i-j)!=abs(j-k) and abs(j-k)!=abs(k-i) and abs(j-i)!=abs(i-k):
                cnt+=1
print(cnt)


### FIXED CODE ###

import math
import sys
import collections

def main():
    N=int(input())
    Tr=list(input())
    R=[]
    G=[]
    B=[]
    for i in range(N):
       if Tr[i]=="R":
           R.append(i)
           continue
       if Tr[i]=="G":
           G.append(i)
           continue
       if Tr[i]=="B":
           B.append(i)
           continue
    cnt=len(R)*len(G)*len(B)
    for j in range(N):
        for i in range(j):
            if 2*j-i<=N-1:
                if Tr[2*j-i]!=Tr[i] and Tr[2*j-i]!=Tr[j] and Tr[j]!=Tr[i]:
                    cnt-=1
    print(cnt)
if __name__ == '__main__':
    main()
### BUGGY CODE ###

import fractions
from functools import reduce
N = int(input())
A = list(map(int,input().split()))
S = []
def gcd(*numbers):
    return reduce(fractions.gcd, numbers)
for i in range(N):
  B = A.copy()
  del B[i]
  m = gcd(*B)
  S.append(m)
print(max(S))

### FIXED CODE ###

def gcd(a, b):
    while b != 0:
        a, b = b, a % b
    return a
 
 
N = int(input())
A = list(map(int, input().split()))
L = [0 for i in range(N)]
R = [0 for i in range(N)]
T = [0 for i in range(N)]
 
for i in range(N):
    if i == 0:
        L[i] = 0
    else:
        L[i] = gcd(L[i-1], A[i-1])
for i in range(N-1, -1, -1):
    if i == N-1:
        R[i] = 0
    else:
        R[i] = gcd(R[i+1], A[i+1])
for i in range(N):
    T[i] = gcd(L[i], R[i])
 
print(max(T))
### BUGGY CODE ###

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

def calc(x):
    y = math.floor(A * x / B) - A * math.floor(x / B)
    return y

max_v = 0
for x in range(N+1):
    max_v = max(max_v, calc(x))

print(max_v)


### FIXED CODE ###

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

def calc(x):
    y = math.floor(A * x / B) - A * math.floor(x / B)
    return y

max_v = 0

if N < B:
    max_v = calc(N)
else:
    max_v = calc(B-1)

# for x in range(N+1):
#     print(x,max_v) # x＝Bでさちる
#     max_v = max(max_v, calc(x))

print(max_v)
### BUGGY CODE ###

from collections import defaultdict
def generate_primes(n):
    is_prime = [True] * (n + 1)
    is_prime[0] = False
    is_prime[1] = False
    for i in range(2, int(n**0.5) + 1):
        if not is_prime[i]:
            continue
        for j in range(i * 2, n + 1, i):
            is_prime[j] = False
    return [i for i in range(n + 1) if is_prime[i]]

def main():  
    n = int(input())
    A = list(map(int, input().split()))
    d = defaultdict(int)
    primes = generate_primes(max(A))
    for a in A:
        for p in primes:
            if a < p: break
            if a % p == 0:
                d[p] += 1
    d[1] = 0
    m = max(d.values())
    if m == n:
        print('not coprime')
    elif m >= 2:
        print('setwise coprime')
    else:
        print('pairwise coprime')
    

if __name__ == '__main__':
    main()



### FIXED CODE ###

from functools import reduce
from math import gcd
def main():   
    n = int(input())
    A = list(map(int, input().split()))
    C = [0] * 1000001
    for a in A: C[a] += 1
    if reduce(gcd, A) != 1:
        print('not coprime')
    elif all(sum(C[i::i]) <= 1 for i in range(2, 1000001)):
        print('pairwise coprime')
    else:
        print('setwise coprime')

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

def main():
    def add(i,x):
        while i<=size:
            tree[i]+=x
            i+=i&-i
    n,*a=map(int,open(0).read().split())
    size=n-~n
    m=n*-~n//2+1>>1
    ng=max(a)+1
    ok=0
    while ng-ok>1:
        mid=(ok+ng)//2
        tree=[0]*-~size
        c=n+1
        s=0
        add(c,1)
        for t in a:
            c+=1if t>=mid else-1
            i=c
            while i:
                s+=tree[i]
                i-=i&-i
            add(c,1)
        if s>=m:ok=mid
        else:ng=mid
    print(ok)
main()

### FIXED CODE ###

class BIT:
    def __init__(self,n):
        self.size=n
        self.tree=[0]*-~n
    def sum(self,i):
        s=0
        while i:
            s+=self.tree[i]
            i-=i&-i
        return s
    def add(self,i,x):
        while i<=self.size:
            self.tree[i]+=x
            i+=i&-i
n,*a=map(int,open(0).read().split())
m=n*-~n//2+1>>1
ng=max(a)+1
ok=0
while ng-ok>1:
    mid=(ok+ng)//2
    b=BIT(n-~n)
    c=n+1
    s=0
    b.add(c,1)
    for t in a:
        c+=1if t>=mid else-1
        s+=b.sum(c)
        b.add(c,1)
    if s>=m:ok=mid
    else:ng=mid
print(ok)
### BUGGY CODE ###

import sys
h,w,m = map(int,input().split())
H = [0]*(h)
W = [0]*(w)
ls = []
for _ in range(m):
    a,b = map(int,input().split())
    ls.append([a-1,b-1])
    H[a-1] += 1
    W[b-1] += 1
hm = max(H)
wm = max(W)
for i in range(len(H)):
    for j in range(len(W)):
        if W[j] == wm and H[i] == hm and [i,j] not in ls:
            print(hm+wm)
            sys.exit()
print(hm+wm-1)



### FIXED CODE ###

h,w,m = map(int,input().split())
x = {}
y = {}
dic = {}
for _ in range(m):
    a,b = map(int,input().split())
    dic[(a,b)] = 1
    x[a] = x.get(a,0) + 1
    y[b] = y.get(b,0) + 1
p = max(x.values())
q = max(y.values())
cnt = 1
a = [i for i in x if x[i] == p]
b = [j for j in y if y[j] == q]
for i in a:
    for j in b:
        if dic.get((i,j),-1) == -1:
            cnt = 0
            break
    if not cnt:
        break
print(p+q-cnt)
### BUGGY CODE ###

import sys
from collections import defaultdict
sys.setrecursionlimit(1 << 25)
read = sys.stdin.readline

def mina(*argv, sub=1): return list(map(lambda x: x - sub, argv))

def ints():
    return list(map(int, read().split()))

#読み込み
N, Q = ints()
tree = defaultdict(lambda: [])
for _ in range(N - 1):
    a, b = mina(*ints())
    tree[a].append(b)
    tree[b].append(a)
cnt = [0] * N
for _ in range(Q):
    q, x = ints()
    cnt[q - 1] += x
cnt.append(0)  # -1アクセス用

#本処理
def dfs(u, p):  # uは現在のノード、pは親のノード
    cnt[u] += cnt[p]
    for nv in tree[u]:
        if nv == p:
            continue
        dfs(nv, u)
dfs(0, -1)
print(*cnt[:-1])

### FIXED CODE ###

# https://atcoder.jp/contests/abc138/tasks/abc138_d
# 下に数を最初に合計してから深さ優先探索なり、幅優先探索なりで下にカウンターを伝播させていけば良い。
# 累積和の木構造バージョン


import sys
read = sys.stdin.readline


def read_ints():
    return list(map(int, read().split()))


def read_col(H, n_cols):
    '''
    H is number of rows
    n_cols is number of cols

    A列、B列が与えられるようなとき
    '''
    ret = [[] for _ in range(n_cols)]
    for _ in range(H):
        tmp = list(map(int, read().split()))
        for col in range(n_cols):
            ret[col].append(tmp[col])

    return ret


# 再帰の上限を緩和する(引数は適当)
import sys
sys.setrecursionlimit(1 << 25)
from collections import defaultdict

N, Q = read_ints()
child = defaultdict(lambda: [])  # ノード0は存在しない
score = [0] * (N + 1)
for _ in range(N - 1):
    a, b = read_ints()
    child[a].append(b)
    child[b].append(a)
for _ in range(Q):
    p, x = read_ints()
    score[p] += x

# 木を走査しながらscoreを合計していく
ans = [0] * (N + 1)


def dfs(p, u, s):  # ノードuの親pにおけるスコアs
    s += score[u]
    ans[u] = s
    for c in child[u]:
        if p == c:
            continue
        dfs(u, c, s)


dfs(-1, 1, 0)
print(*ans[1:])
### BUGGY CODE ###

S=input()
Q=int(input())
snt=1
for i in range(Q):
    inp=input().split()
    if inp[0]=="1":
        snt*=(-1)
    else:
        F=int(inp[1])
        C=inp[2]
        if (2*F-3)*snt>0:
            S=S+C
        else:
            S=C+S
if snt==1:
    print(S)
else:
    for i in range(len(S)):
        print(S[len(S)-1-i],end="")
    print()


### FIXED CODE ###

from collections import deque
S=deque(input())
Q=int(input())
snt=1
for i in range(Q):
    inp=input().split()
    if inp[0]=="1":
        snt*=(-1)
    else:
        F=int(inp[1])
        C=inp[2]
        if (2*F-3)*snt>0:
            #S=S+C
            S.append(C)
        else:
            #S=C+S
            S.appendleft(C)
if snt==1:
    #print(S)
    for i in range(len(S)):
        print(S.popleft(),end="")
    print()
else:
    for i in range(len(S)):
        #print(S[len(S)-1-i],end="")
        print(S.pop(),end="")
    print()
### BUGGY CODE ###

def make_Graph(N, edges):#頂点の名前はi - 1で受け付ける
    graph = {}
    for i in range(N):
        graph[i] = {}
    for edge in edges:
        a, b, c = edge
        graph[a][b] = c
        graph[b][a] = c
    return graph

def BFS(N, graph, start):
    Q = []
    V = []
    prev = {}
    d = {}
    for i in range(N):
        prev[i] = None
        d[i] = float("inf")
    Q.append(start)
    d[start] = 0
    while len(V) < N:
        a = Q.pop(0)
        V.append(a)
        for b, c in graph[a].items():
            if not b in V:
                Q.append(b)
                prev[b] = a
                d[b] = d[a] + c
    return d

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

graph = make_Graph(N, edges)

Q, K =(int(i) for i in input().split())

d = BFS(N, graph, K-1)
for i in range(Q):
    x, y = (int(a) for a in input().split())
    print(d[x-1] + d[y-1])

### FIXED CODE ###

import sys
input = sys.stdin.readline

def make_Graph(N, edges):#頂点の名前はi - 1で受け付ける
    graph = [ [] for i in range(N)]
    for edge in edges:
        a, b, c = edge
        graph[a].append((b,c))
        graph[b].append((a,c))
    return graph

def BFS(N, graph, start):
    Q = []
    V = []
    prev = [None] * N
    dd = [float("inf")] * N
    Q.append(start)
    dd[start] = 0
    while len(V) < N:
        a = Q.pop(0)
        V.append(a)
        for b, c in graph[a]:
            if b != prev[a]:
                Q.append(b)
                prev[b] = a
                dd[b] = dd[a] + c
    return dd

def DFS(N, graph, a, p, d):
    dd[a] = d
    for b, c in graph[a]:
        if b == p or dd[b] != 0:
            continue
        else:
            DFS(N, graph, b, a, d + c)

N = int(input())
graph = [ [] for i in range(N)]
for i in range(N-1):
    a, b, c = (int(j) for j in input().split())
    graph[a-1].append((b-1,c))
    graph[b-1].append((a-1,c))

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

try:
    DFS(N, graph, K-1, -1, 0)
except:
    dd = BFS(N, graph, K-1)

for i in range(Q):
    x, y = (int(a) for a in input().split())
    print(dd[x-1] + dd[y-1])