Harryxun/stf_run huggingface dataset:
def main():
    s = input()
    k = int(input())
    print(solve(s, k))

def solve(s, k):
    ans = 0
    if len(set(s)) == 1:
        ans += replace_count(len(s)*k)
    else:
        # 接続部分
        start = 1
        end = 0
        i = 1
        # issii
        while (s[0] == s[i]):
            i +=1
            start +=1
        j = -1
        while (s[0] == s[j]):
            j -=1
            end +=1
        ans += replace_count(start+end) * (k-1)
        ans += replace_count(start) 
        ans += replace_count(end) 

        # 単語内部
        same = 1
        for idx in range(i, len(s)+j+1):
            if idx >= len(s):
                break
            if s[idx] == s[idx+1]:
                same += 1
            else:
                ans += replace_count(same) * k
                same = 1
    return ans


def replace_count(n):
    return n//2

main()




### FIXED CODE ###


def main():
    s = input()
    k = int(input())
    print(solve(s, k))

def solve(s, k):
    ans = 0
    if len(set(s)) == 1:
        ans += replace_count(len(s)*k)
    else:
        same = 1
        for i in range(0, len(s)-1):
            if s[i] == s[i+1]:
                same += 1
            else:
                ans += replace_count(same) * k
                same = 1
        ans += replace_count(same) * k
        if s[0] == s[i]:
            a = 1
            b = 1
            a_idx = 0
            b_idx = -1
            while s[a_idx] == s[a_idx+1]:
                a += 1
                a_idx += 1
            while s[b_idx] == s[b_idx-1]:
                b += 1
                b_idx -= 1
            dup = (replace_count(a) + replace_count(b) - replace_count(a+b)) * (k-1)
            ans -= dup

    return ans


def replace_count(n):
    return n//2

main()
s = str(input())
l = set(list(s))
if range(l) == 2:
  print("Yes")
else:
  print("No")

### FIXED CODE ###

s = str(input())
if len(set(s)) == 2:
  print("Yes")
else:
  print("No")
n = int(input())
INF = 10**9
P = []
for i in range(n):
    p = map(int, input().split())
    P.append()
m = [[0]*n for i in range(n)]

for l in range(2, n + 1):
    for i in range(n - l + 1):
        j = i + l - 1
        m[i][j] = INF
        for k in range(i, j):
            m[i][j] = min(m[i][j], m[i][k] + m[k + 1]m[j] + p[i - 1][0] * p[k][1] * p[j][1])

print(m[0][n - 1])

### FIXED CODE ###

n = int(input())
INF = 10**9
P = [list(map(int, input().split())) for i in range(n)]
m = [[0]*n for i in range(n)]

for l in range(2, n + 1):
    for i in range(n - l + 1):
        j = i + l - 1
        m[i][j] = INF
        for k in range(i, j):
            m[i][j] = min(m[i][j], m[i][k] + m[k + 1][j] + P[i][0] * P[k][1] * P[j][1])

print(m[0][n - 1])
a = [int(i) for i in a]
if sum(a) >= 22:
    print("bust")
else:
    print("win")

### FIXED CODE ###

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

if sum(a) >= 22:
    print("bust")
else:
    print("win")
n = input()
N = int(n)
a = [input() for i in range(N)]
a = int(a)
if max(a)　>=　sum(a) - max(a):
  print("Yes")
else:
  print("No")

### FIXED CODE ###

n=input()
a=list(map(int, input().split()))
if max(a)*2 < sum(a):
    print("Yes")
else:
    print("No")
N = int(input)
p = sorted([int(input()) for _ in range(N)], reverse=True)
ans = p[0] // 2 + sum(p[1:])
print(ans)

### FIXED CODE ###

N = int(input())
p = sorted([int(input()) for _ in range(N)], reverse=True)
ans = p[0] // 2 + sum(p[1:])
print(ans)
n = input()
if n == 1:
    print("Hello World")
else:
    a = int(input())
    b = int(input())
    print(a+b)

### FIXED CODE ###

n = input()
if n == "1":
    print("Hello World")
else:
    a = int(input())
    b = int(input())
    print(a+b)
[a, b, c] = [x for x in map(int, input().split())]
k = int(input())
a_b_flg = False if a >= b else True
b_c_flg = False if b >= c else True

for kdx in range(0, k):
  if not a_b_flg:
    b = b*2
  elif not b_c_flg:
    c = c*2
  a_b_flg = False if a >= b else True
  b_c_flg = False if b >= c else True
  if a_b_flgw and b_c_flg:
    print('Yes')
    break
if not (a_b_flgw and b_c_flg):
  print('No')

### FIXED CODE ###

[a, b, c] = [int(x) for x in input().split(" ")]
k = int(input())
a_b_flg = False if a >= b else True
b_c_flg = False if b >= c else True

for kdx in range(0, k):
  if not a_b_flg:
    b = b*2
  elif not b_c_flg:
    c = c*2
  a_b_flg = False if a >= b else True
  b_c_flg = False if b >= c else True
  if a_b_flg and b_c_flg:
    print('Yes')
    break
if not (a_b_flg and b_c_flg):
  print('No')
ans = [''] * len(A)

for i in range(len(op)):
    sum = int(A[0])
    if op[i] == '+':
        sum += int(A[1])
        ans[0] = op[i]
    else:
        sum -= int(A[1])
        ans[0] = op[i]
    for j in range(len(op)):
        if op[j] == '+':
            sum += int(A[2])
            ans[1] = op[j]
        else:
            sum -= int(A[2])
            ans[1] = op[j]
        for k in range(len(op)):
            if op[k] == '+':
                sum += int(A[3])
                ans[2] = op[k]
            else:
                sum -= int(A[3])
                ans[2] = op[k]
            if sum == 7:
                print(A[0]+ans[0]+A[1]+ans[1]+A[2]+ans[2]+A[3]+'=7')
                exit()


### FIXED CODE ###

A = input()

op = ['+', '-']

for i in range(len(op)):
    for j in range(len(op)):
        for k in range(len(op)):
            if eval(A[0]+op[i]+A[1]+op[j]+A[2]+op[k]+A[3]) == 7:
                print(A[0]+op[i]+A[1]+op[j]+A[2]+op[k]+A[3]+'=7')
                exit()
N, M = map(int, input().split())
A_list = []
B_list = []

for _ in range(N):
    A_list.append(list(input()))
for _ in range(M):
    B_list.append(list(input()))
  
match_list =[]
for i in range(N-M+1):
    for j in range(N-M+1):
        if B_list[0] == A_list[i][j:(j+M)]:
            match_list.append([i, j])
        else:
            pass

whole_flag = 0
for k in match_list:
    flag =0
    for l in range(M):
        if A_list[k[0]+l][k[1]:k[1]+M] ==B_list[k[0]+l]:
            flag += 1
        else:
            break
            
    if flag == M:
        print("Yes")
        whole_flag += 1
        
if whole_flag == 0:
    print("No")

### FIXED CODE ###

N, M = map(int, input().split())
A_list = []
B_list = []
for _ in range(N):
    A_list.append(list(input()))
for _ in range(M):
    B_list.append(list(input()))
    

whole_flag = 0
for i in range(N-M+1):
    for j in range(N-M+1):
        flag = 0
        for k in range(M):
            if B_list[k] == A_list[i+k][j:j+M]:
                flag += 1
            else:
                pass
        if flag == M:
            whole_flag += 1
            
if whole_flag == 0:
    print("No")
else:
    print("Yes")
enemy, num = input().split()
skills = list(map(input().split()))
print("Yes") if max(skills) >= enemy else print("No")

### FIXED CODE ###

enemy, num = map(int,input().split())
skills = list(map(int,input().split()))
print("Yes") if sum(skills) >= enemy else print("No")
import heapq

R,C,K = map(int,input().split())

cost = [[0 for i in range(C)] for j in range(R)] 

dp = [[0 for i in range(C)] for j in range(R+1)]

pick_up = [[[] for i in range(C)] for j in range(R)] 

for _ in range(K):
    r,c,v = map(int,input().split())
    r,c = r-1,c-1
    cost[r][c] = v

for h in range(R):
    for w in range(C):
        if w == 0:
            dp[h+1][w] = dp[h][w]+cost[h][w]
            if cost[h][w] != 0:
                heapq.heappush(pick_up[h][w],cost[h][w])
        else:
            if len(pick_up[h][w-1]) < 3:
                dp[h+1][w] = max(dp[h][w]+cost[h][w], dp[h+1][w-1]+cost[h][w])
                if cost[h][w] != 0:
                    if dp[h+1][w] == dp[h][w]+cost[h][w]:
                        heapq.heappush(pick_up[h][w],cost[h][w])
                    else:
                        tmp_array1 = copy.copy(pick_up[h][w-1])
                        heapq.heappush(tmp_array1,cost[h][w])
                        pick_up[h][w] = copy.copy(tmp_array1)
                else:
                    if dp[h+1][w] == dp[h][w]+cost[h][w]:
                        continue
                    else:
                        pick_up[h][w] = pick_up[h][w-1]
                    
            else:
                if cost[h][w] != 0:
                    tmp_array2 = copy.copy(pick_up[h][w-1])
                    minus = sum(tmp_array2)
                    heapq.heappush(tmp_array2,cost[h][w])
                    heapq.heappop(tmp_array2)
                    dp[h+1][w] = max(dp[h][w]+cost[h][w], dp[h+1][w-1]-minus+sum(tmp_array2))
                    if dp[h+1][w] == dp[h][w]+cost[h][w]:
                        heapq.heappush(pick_up[h][w],cost[h][w])
                    else:
                        pick_up[h][w] = copy.copy(tmp_array2)
                    
                else:
                    dp[h+1][w] = max(dp[h][w]+cost[h][w], dp[h+1][w-1]+cost[h][w])
                    if dp[h+1][w] == dp[h][w]:
                        continue
                    else:
                        pick_up[h][w] = copy.copy(pick_up[h][w-1])
    
            
print(dp[-1][-1])
#for i in cost:
    #print(*i)

#for i in pick_up:
    #print(*i)


### FIXED CODE ###

R,C,K = map(int,input().split())

item = [[0] * (C + 1) for _ in range(R + 1)]
for i in range(K):
    r,c,v = map(int,input().split())
    item[r][c] = v

dp = [[[0]*(C+1) for _ in range(R+1)] for _ in range(4)]

for h in range(R+1):
    for w in range(C+1):
        for k in range(4):
            position = dp[k][h][w]
            if h+1 <= R:
                # 移動先のアイテムを取らない場合
                dp[0][h+1][w] = max(dp[0][h+1][w], position)
                 # 移動先のアイテムを取る場合
                dp[1][h+1][w] = max(dp[1][h+1][w], position+item[h+1][w])
            if w+1 <= C:
                # 移動先のアイテムを取らない場合
                dp[k][h][w+1] = max(dp[k][h][w+1], position)
                # 現在のkが3未満のときのみ, 移動先のアイテムを取ることが可能
                if k < 3:
                    dp[k+1][h][w+1] = max(dp[k+1][h][w+1], position+item[h][w+1])

ans = 0
for k in range(4):
    ans = max(ans, dp[k][-1][-1])

print(ans)
a,b=map(int,input().split())
if a>2*b:
print(a-2*b)
else if:
print(0)

### FIXED CODE ###

a,b = map(int,input().split())
c = 2*b
d = a-c
if d > 0:
 print(d)
elif d <= 0:
 print(0)
#c-(a-b)
print(max(-1*int(input())+int(input())+int(input()),0))

### FIXED CODE ###

a,b,c=map(int,input().split())
print(max(c-a+b,0))
n, m = map(int, input().split())
xy = []
for _ in range(m):
    x, y, _ = map(int, input().split())
    xy.append((x-1, y-1))

class UnionFind():
    def __init__(self, n):
        self.li = list(range(n))
    def root(self, x):
        if self.li[x]==x:
            return x
        self.li[x] = self.root(self.li[x])
        return self.li[x]
    def unite(self, x, y):
        rx = self.root(x)
        ry = self.root(y)
        if rx==ry:
            return
        self.li[ry] = rx
    def same(self, x, y):
        return self.root(x)==self.root(y)

uf = UnionFind(n)
for x, y in xy:
    uf.unite(x, y)
for i in range(n):
    uf.root(i)
print(len(set(uf.li)))

### FIXED CODE ###

import sys
sys.setrecursionlimit(10**6)
n, m = map(int, input().split())
xy = []
for _ in range(m):
    x, y, _ = map(int, input().split())
    xy.append((x-1, y-1))

class UnionFind():
    def __init__(self, n):
        self.li = list(range(n))
    def root(self, x):
        if self.li[x]==x:
            return x
        self.li[x] = self.root(self.li[x])
        return self.li[x]
    def unite(self, x, y):
        rx = self.root(x)
        ry = self.root(y)
        if rx==ry:
            return
        self.li[ry] = rx
    def same(self, x, y):
        return self.root(x)==self.root(y)

uf = UnionFind(n)
for x, y in xy:
    uf.unite(x, y)
for i in range(n):
    uf.root(i)
print(len(set(uf.li)))
s = input()
t = input()
result = len([e for a,b in zip(s,t) if a != b])
print(result)

### FIXED CODE ###

s = input()
t = input()
result = len([a for a,b in zip(s,t) if a != b])
print(result)
a, b = map(int, input().split())

for c in range(1,4) :
    if (a*b*c)%2 == 1 :
        print("Yes")
        sys.exit(0)

print("No")

### FIXED CODE ###

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

if (a%2 == 1 ) and (b%2 == 1 ) :
    print("Yes")
else :
    print("No")
k, s = map(int, input().split(" "))
answer = 0

for i in range(k + 1):
    for j in range(k + 1):
        rem = 3 * k - i - j
        if 0 <= rem <= k:
            answer += 1
print(answer)


### FIXED CODE ###

k, s = map(int, input().split(" "))
answer = 0

for i in range(k + 1):
    for j in range(k + 1):
        rem = s - i - j
        if 0 <= rem <= k:
            answer += 1
print(answer)
import sys
sys.setrecursionlimit(10*6)

def MI(): return map(int, input().split())
from collections import deque
N,Q=MI()
N1=N
#Edge1=[[] for _ in range(N)]
#print(Edge1)
Edge=[[] for _ in range(N1)]
#print(Edge)
for i in range(N-1):
  a,b=MI()
  Edge[a-1].append(b-1)
  Edge[b-1].append(a-1)
Point=[0]*N
for i in range(Q):
  p,x=MI()
  Point[p-1]+=x
def dfs(now,pre=-1):
  for nxt in Edge[now]:
    if nxt==pre:
      continue
    Point[nxt]+=Point[now]
    dfs(nxt,now)
dfs(0)
print(*Point)


### FIXED CODE ###

#ika tako
import sys
n, q = map(int, input().split())
lines = sys.stdin.readlines()
links = [set() for _ in range(n)]
for line in lines[:n - 1]:
    a, b = map(int, line.split())
    a -= 1
    b -= 1
    links[a].add(b)
    links[b].add(a)
adds = [0] * n
for line in lines[n - 1:]:
    p, x = map(int, line.split())
    p -= 1
    adds[p] += x
ans = [-1] * n
q = [(0, 0)]
while q:
    v, a = q.pop()
    a += adds[v]
    ans[v] = a
    for u in links[v]:
        if ans[u] == -1:
            q.append((u, a))
print(*ans)
n,m = map(int,raw_input().split())
a = []
b = []
 
for i in range(n):
    a.append(map(int, raw_input().split()))
for i in range(m):
    b.append(input())
for i in range(n):
    ans = 0
for j in range(m):
    ans += a[i][j]*b[j]
print ans:

### FIXED CODE ###

n,m = map(int,raw_input().split())
a = []
b = []
 
for i in range(n):
  a.append(map(int, raw_input().split()))
for i in range(m):
  b.append(input())
for i in range(n):
  ans = 0
  for j in range(m):
    ans += a[i][j]*b[j]
  print ans
arr = [1, 1, 1, 2, 1, 2, 1, 5, 2, 2, 1, 5, 1, 2, 1, 14, 1, 5, 1, 5, 2, 2, 1, 15, 2, 2, 5, 4, 1, 4, 1, 51]
k = input()
print(arr[k])

### FIXED CODE ###

arr = [1, 1, 1, 2, 1, 2, 1, 5, 2, 2, 1, 5, 1, 2, 1, 14, 1, 5, 1, 5, 2, 2, 1, 15, 2, 2, 5, 4, 1, 4, 1, 51]
k = int(input())
print(arr[k-1])
m = map(int, raw_input().split())
a = m[0]
b = m[1]
c = m[2]

for i in xrange(a,b):
 if c % i == 0:
  count = count + 1
print count

### FIXED CODE ###

m = map(int, raw_input().split())
c = 0


for i in xrange(m[0],m[1]+1):
  if m[2] % i == 0:
   c = c + 1
print c
dic = {}
m = 0

for i in L:
    if i in dic:
        dic[i] = dic[i] + 1
    else:
        dic[i] = 1
        
    if m < dic[i]:
        m = dic[i]

n = []
for i in dic:
    if dic[i] == m:
        n.append(i)
n.sort()
for i in n:
    print(i)

### FIXED CODE ###


N = int(input())
L = [ input() for i in range(N)]
#print(L)
    
dic = {}
m = 0

for i in L:
    if i in dic:
        dic[i] = dic[i] + 1
    else:
        dic[i] = 1
        
    if m < dic[i]:
        m = dic[i]

n = []
for i in dic:
    if dic[i] == m:
        n.append(i)
n.sort()
for i in n:
    print(i)
import sys
sys.setrecursionlimit(100000)

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

tree_list=[]
for i in range(N+1):
  tree_list.append([])

for i in range(N-1):
  a,b=map(int,input().split())
  tree_list[a].append(b)
  
dic_score={}
for i in range(Q):
  p,x=map(int,input().split())
  if p in dic_score:
    dic_score[p]+=x
  else:
    dic_score[p]=x

#print(tree_list)  
ans_list=[0]*(N+1)

def dfs(v,s):
  score=s
  if v in dic_score:
    score+=dic_score[v]
    
  ans_list[v]+=score
  for vv in tree_list[v]:
    dfs(vv,score)

dfs(1,0)
#print(ans_list)

ans_list_str=[]
for i in range(1,N+1):
  #ans_list_str.append(str(ans_list[i]))

print(" ".join(ans_list_str))

### FIXED CODE ###

import sys
sys.setrecursionlimit(10**9)
input=sys.stdin.readline

def dfs(u,par):
  ans_list[u]+=ans_list[par]  
  for v in tree_list[u]:
    if v!=par:
      dfs(v,u)      
  return

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

tree_list=[[] for _ in range(N+1)]
for i in range(N-1):
  a,b=map(int,input().split())
  tree_list[a].append(b)
  tree_list[b].append(a)
#print(tree_list)

ans_list=[0]*(N+1)
for i in range(Q):
  p,x=map(int,input().split())
  ans_list[p]+=x

dfs(1,0)

print(*ans_list[1:])
n, *XYH = map(int, open(0).read().split())
X, Y, H = XYH[::3], XYH[1::3], XYH[2::3]
A = []
for cx, cy in product(range(101), repeat=2):
    B = set()
    C = set()
    for x, y, h in zip(X, Y, H):
        s = h + abs(x-cx) + abs(y-cy)
        if h == 0:
            B.add(s)
        else:
            C.add(s)
    if len(C) == 1:
        h = C.pop()
        if all(h <= b for b in B):
            A.append((cx, cy, h))
print(*A.pop())

### FIXED CODE ###

from itertools import product
n, *XYH = map(int, open(0).read().split())
X, Y, H = XYH[::3], XYH[1::3], XYH[2::3]
A = []
for cx, cy in product(range(101), repeat=2):
    B = set()
    C = set()
    for x, y, h in zip(X, Y, H):
        s = h + abs(x-cx) + abs(y-cy)
        if h == 0:
            B.add(s)
        else:
            C.add(s)
    if len(C) == 1:
        h = C.pop()
        if all(h <= b for b in B):
            A.append((cx, cy, h))
print(*A.pop())
import math
import fractions


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


if __name__ == '__main__':
    a, b, c, d = map(int, input().split())
    warikireru_c = math.floor(b / c) - math.ceil(a / c) + 1
    warikireru_d = math.floor(b / d) - math.ceil(a / d) + 1
    lcm_1 = lcm(c, d)
    warikireru_lcm = math.floor(b / lcm_1) - math.ceil(a / lcm_1) + 1
    num = b - a + 1
    print(num - (warikireru_c + warikireru_d - warikireru_lcm))


### FIXED CODE ###

import re

s = input()

if re.findall(r"(.)\1+",s):
    print("Bad")
else:
    print("Good")
from collections import defaultdict, deque
from heapq import heappop, heappush
import sys
input = sys.stdin.readline

N, M, P = map(int, input().split())
d = defaultdict(list)
candidate = []
l = []

for _ in range(M):
    a, b, c = map(int, input().split())
    
    if a==b:
        if P-c<0:
            candidate.append(a)
    else:
        l.append((a, b, P-c))
        d[a].append((P-c, b))

def check():
    if candidate:
        visited = [False]*(N+1)
        visited[1] = True
        q = deque([1])
        while q:
            s = q.popleft()
            for _, t in d[s]:
                if visited[t]:continue
                visited[t] = True
                q.append(t)
        
        candidate2 = [i for i in candidate if visited[i]]
        for i in candidate2:
            visited = [False]*(N+1)
            visited[i] = True
            q = deque([i])
            while q:
                s = q.popleft()
                for _, t in d[s]:
                    if visited[t]:continue
                    visited[t] = True
                    q.append(t)
            if visited[N]:
                return False
    return True

def shortest_path(s):
    INF = 10**18
    dist = [INF]*(N+1)
    dist[s] = 0

    while 1:
        update = False

        for a, b, c in l:
            if dist[a]!=INF and dist[b]>dist[a]+c:
                dist[b] = dist[a]+c
        if not update:
            break
    return dist

if check():
    d1 = shortest_path(1)
    dN = shortest_path(N)
    if dN[1]!=INF and d1[N]+dN[1]<0:
        print(-1)
    elif d1[N]<0:
        print(-d1[N])
    else:
        print(0)
else:
    print(-1)

### FIXED CODE ###

from collections import defaultdict, deque
from heapq import heappop, heappush
import sys
input = sys.stdin.readline

N, M, P = map(int, input().split())
d = defaultdict(list)
e = defaultdict(list)
cd = defaultdict(list)
l = []

for _ in range(M):
    a, b, c = map(int, input().split())
    cd[a].append((b, P-c))
    d[a].append(b)
    e[b].append(a)

def check_visited(d, start):
    visited = [False]*(N+1)
    visited[start] = True
    q = deque([start])
    while q:
        s = q.popleft()
        for t in d[s]:
            if visited[t]:continue
            visited[t] = True
            q.append(t)
    
    s = set()
    for i in range(1, N+1):
        if visited[i]:
            s.add(i)
    return s

def shortest_path(s):
    INF = 10**18
    dist = [float("inf")]*(N+1)
    dist[s] = 0

    for i in range(N):
        for a, b, c in l:
            if dist[b]>dist[a]+c:
                dist[b] = dist[a]+c
                if i==N-1:
                    return dist, True
    return dist, False

add_l = check_visited(d, 1)&check_visited(e, N)
for a in add_l:
    for b, c in cd[a]:
        l.append((a, b, c))
d1, neg = shortest_path(1)
if neg:
    print(-1)
elif d1[N]<0:
    print(-d1[N])
else:
    print(0)
if __name__ == '__main__':
    N = int(input())
    A = list(map(int, input().split()))

    A.sort(reverse=True)

    v = []
    while len(A) != 0:
        if len(v) == 2:
            break

        if A[0] == A[1]:
            v.append(A[0])
            A.pop(0)
            A.pop(0)
        else:
            A.pop(0)

    if len(v) == 1 or len(v) == 0:
        print(0)
    else:
        print(v[0] * v[1])


### FIXED CODE ###

if __name__ == '__main__':
    N = int(input())
    A = list(map(int, input().split()))

    A.sort(reverse=True)

    v = []
    while len(A) > 1:
        if len(v) == 2:
            break

        if A[0] == A[1]:
            v.append(A[0])
            A.pop(0)
            A.pop(0)
        else:
            A.pop(0)

    if len(v) == 1 or len(v) == 0:
        print(0)
    else:
        print(v[0] * v[1])
n,k=mat(int, input().split())

ans=k*(k-1)**(n-1)

print(ans)

### FIXED CODE ###


def penki(a, b, c):
  if a==b and b==c:
    return 1
  elif a!=b and b!=c and c!=a:
    return 3
  else:
    return 2

a,b,c=map(int, input().split())
print(penki(a, b, c))
# encoding:utf-8

def main():
	n = int(input())
	vers = [list(map(int, input().split())) for _ in range(n - 1)]
	q, k = list(map(int, input().split()))
	queries = [list(map(int, input().split())) for _ in range(q)]
	#print(vers)
	#print(queries)
	dist = calcMinDist(n, vers, k, queries)
	calcQueries(dist, queries, k)
	
def calcMinDist(n, vers, k, queries):
	dist = [10000 for _ in range(n)]
	flag = [False for _ in range(n - 1)]
	dist[k - 1] = 0
	dfs(vers, dist, flag, k)
	return dist

def dfs(vers, dist, flag, frm):
	#print(frm)
	for i, ver in enumerate(vers):
		if flag[i] == False:
			if ver[0] == frm:
				to = ver[1]
				dist[to - 1] = dist[frm - 1] + ver[2]
				flag[i] = True
				vers, dist, flag, _ = dfs(vers, dist, flag, to)
			elif ver[1] == frm:
				to = ver[0]
				dist[to - 1] = dist[frm - 1] + ver[2]
				flag[i] = True
				vers, dist, flag, _ = dfs(vers, dist, flag, to)

	return [vers, dist, flag, frm]

def calcQuery(dist, query, k):
	print(dist[query[0] - 1] + dist[query[1] - 1])

def calcQueries(dist, queries, k):
	for query in queries:
		calcQuery(dist, query, k)

if __name__ == '__main__':
	main()

### FIXED CODE ###

# encoding:utf-8
import sys
 
def main():
	n = int(input())
	vers = [list(map(int, input().split())) for _ in range(n - 1)]
	q, k = list(map(int, input().split()))
	queries = [list(map(int, input().split())) for _ in range(q)]
 
	#n = 10 ** 5
	#vers = [[i + 1, i + 2, 10 ** 9] for i in range(n - 1)]
 
	new_vers = mapping(n, vers)
	dist = calcMinDist(n, new_vers, k, queries)
	calcQueries(dist, queries, k)
	
def mapping(n, vers):
	new_vers = {i :[] for i in range(n)}
	for ver in vers:
		new_vers[ver[0] - 1].append((ver[1], ver[2]))
		new_vers[ver[1] - 1].append((ver[0], ver[2]))
	return new_vers
 
def calcMinDist(n, new_vers, k, queries):
	dist = [- 1 for _ in range(n)]
	flag = [False for _ in range(n)]
	dist[k - 1] = 0
	flag[k - 1] = True
	dfs(new_vers, dist, flag, k)
	return dist
 
def dfs(new_vers, dist, flag, frm):
	flag[frm - 1] = True
	vs = new_vers[frm - 1]
	for v in vs:
		if flag[v[0] - 1] == False:
			dist[v[0] - 1] = dist[frm - 1] + v[1]
			new_ver, dist, flag, _ = dfs(new_vers, dist, flag, v[0])
 
	return [new_vers, dist, flag, frm]
 
def calcQuery(dist, query, k):
	print(dist[query[0] - 1] + dist[query[1] - 1])
 
def calcQueries(dist, queries, k):
	for query in queries:
		calcQuery(dist, query, k)
 
if __name__ == '__main__':
	sys.setrecursionlimit(1000000)
	main()