Harryxun/stf_run huggingface dataset:
import sys
n,m,k = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
t = k
start = 0
while True:
  if start == len(b):
    break
  else:
    if t - b[start] < 0:
      break
    else:  
      t -= b[start]
      start += 1

num = start
lst = [0]
M = 0
print(start,t)
for i in range(len(a)):
  while a[i] > t:
    b_ = b.pop()
    t = t+b_
    num -= 1
  t -= a[i]
  num += 1
  print(num,t)
 
  if lst[-1] < num:
    M = num
  lst.append(num)    
    
print(M)    
  

### FIXED CODE ###

import sys
n,m,k = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
b2 = []
t = k
start = 0
while True:
  if start == len(b):
    break
  else:
    if t - b[start] < 0:
      break
    else:  
      t -= b[start]
      b2.append(b[start])
      start += 1
num = start
lst = [start]
M = start

for i in range(len(a)):
  if a[i] > t and b2 == []:
    print(M)
    sys.exit()
  while a[i] > t and b2 != []:
    b_ = b2.pop()
    t = t+b_
    num -= 1
    
  if a[i] <= t:
    t -= a[i]
    num += 1
  
 
  if M < num:
    M = num
  lst.append(num)    
    
  
  
  
  
print(M)
import sys
#input = sys.stdin.readline

def inpl(): return list(map(int, input().split()))
A, B, C, D, E, F = inpl()
waters = [100*A*a + 100*B*b for a in range(30//A + 1) for b in range((30-A*a)//B + 1)]
sugars = [C*c + D*d for c in range(3000//C + 1) for d in range((3000-C*c)//D + 1)]

waters = sorted(set(waters))[1:]
sugars = sorted(set(sugars))


def check(m):
    if m > E:
        return False, None, None
    for water in waters:
        sugar_ix = bisect_left(sugars, m*water/(100-m))
        if (sugar_ix == len(sugars)):
            continue
        sugar = sugars[sugar_ix]
        if sugar + water <= F and sugar <= water*E//100:
            return True, sugar, water
    else:
        return False, None, None

OK = 0
NG = E + 1

while abs(OK - NG)>1e-8:
    mid = (OK+NG)/2
    if check(mid)[0]:
        OK = mid
    else:
        NG = mid

_, sugar, water = check(OK)
print(sugar+water, sugar)

### FIXED CODE ###

from bisect import bisect, bisect_left
from collections import Counter
from functools import reduce
from itertools import accumulate
from operator import itemgetter, xor, mul

import sys
#input = sys.stdin.readline

def inpl(): return list(map(int, input().split()))
A, B, C, D, E, F = inpl()
waters = [100*A*a + 100*B*b for a in range(30//A + 1) for b in range((30-A*a)//B + 1)]
sugars = [C*c + D*d for c in range(3000//C + 1) for d in range((3000-C*c)//D + 1)]

waters = sorted(set(waters))[1:]
sugars = sorted(set(sugars))


def check(m):
    if m > E:
        return False, None, None
    for water in waters:
        sugar_ix = bisect_left(sugars, m*water/(100-m))
        if (sugar_ix == len(sugars)):
            continue
        sugar = sugars[sugar_ix]
        if sugar + water <= F and sugar <= water*E//100:
            return True, sugar, water
    else:
        return False, None, None

OK = 0
NG = E + 1

for _ in range(1000):
    mid = (OK+NG)/2
    if check(mid)[0]:
        OK = mid
    else:
        NG = mid

_, sugar, water = check(OK)
print(sugar+water, sugar)
import collections
n = int(raw_input())
pts = [map(int , raw_input().split()) for _ in range(n)]

def f(pts):
	for cx,cy in [(ii,jj) for ii in range(100+1) for jj in range(100+1)]:	
		H = min([abs(cx - x) + abs(cy - y) for x,y,h in pts if h == 0] or [10 ** 9])
		
		cc = collections.Counter()
		for x,y,h in pts:
			q = abs(cx - x) + abs(cy - y)
			if h > 0:
				Y = h + q
				if 1<=Y <= H:
					cc[Y]+=1
				else:
					cc[10 ** 9 + 2] += 1
		if len(cc) == 1:
			for k in cc:
				if 1<=k <= 10 ** 9:
					return k,cx,cy

h,x,y = f(pts)

print x,y,h

### FIXED CODE ###

import collections
n = int(raw_input())
pts = [map(int , raw_input().split()) for _ in range(n)]

def f(pts):
	for cx,cy in [(ii,jj) for ii in range(100+1) for jj in range(100+1)]:	
		H = min([abs(cx - x) + abs(cy - y) for x,y,h in pts if h == 0] or [+float('inf')])
		
		cc = collections.Counter()
		for x,y,h in pts:
			q = abs(cx - x) + abs(cy - y)
			if h > 0:
				Y = h + q
				if 1<=Y <= H:
					cc[Y]+=1
				else:
					cc[-1] += 1
		if len(cc) == 1:
			for k in cc:
				if 1<=k <= H:
					return k,cx,cy

h,x,y = f(pts)

print x,y,h
n,x = map(int,input().split())
a = list(map(int,input().split()))

def gcd(a,b):
  while b!=0:
    a,b=b,a%b
  return a

d = []
for i in range(n):
  y = abs(a[i] - x)
  d.append(y)

a = gcd(d[0],d[1])
for i in range(n):
	a = gcd(a,d[i])
print(a)

### FIXED CODE ###

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

def gcd(a,b):
  while b!=0:
    a,b=b,a%b
  return a

if n == 1:
  a.append(a[0])
  n = n+1
d = []
for i in range(n):
  y = abs(a[i] - x)
  d.append(y)

b = gcd(d[0],d[1])
for i in range(n):
	b = gcd(b,d[i])
print(b)
A,B=int(input())
if A%3*B%3*(A+B)%3==0:print('Possible')
else:print('Impossible')

### FIXED CODE ###

import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
def s(): return input()
def i(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 9)
INF = 10**9
mod = 10**9+7

A,B = I()
if A%3 == 0 or B%3 == 0 or (A+B)%3 == 0:
    print('Possible')
else:
    print('Impossible')
a, b = int(input().split)

if (a*b )%2 = 1
    print('odd')
    
    else
            print('even')

### FIXED CODE ###

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

if (a*b ) % 2:
    print('Odd')
    
else:
    print('Even')
nums  = int(input())
array = [int(n) for n in input().split()]

for i in range( 1, nums ):
    v = array[i]
    j = i - 1
    while j >= 0 and array[j] > v:
        array[j+1] = array[j]
        j -= 1
        array[j+1] = v
        print array

### FIXED CODE ###

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

if __name__ == '__main__':
    print(" ".join(map(str, A)))
    for i in range(1, N):
        target_num = A[i]
        j = i - 1
        while j >= 0 and A[j] > target_num:
            A[j+1] = A[j]
            j -= 1
            A[j+1] = target_num
        print(" ".join(map(str, A)))
def calc_sum_digits(n):
    sumd = 0
    while(n > 1):
        sumd += int(n%10)
        n /= 10
        
    return sumd

N = int(input())
sumd = 0
sumd = calc_sum_digits(N)
if(N%sumd == 0):
     print('yes')
else:
     print('No')

### FIXED CODE ###

def calc_sum_digits(n):
    sumd = 0
    while(int(n) > 0):
        sumd += int(n%10)
        n /= 10
    return sumd

N = int(input())
sumd = calc_sum_digits(N)
if(N%sumd == 0):
     print('Yes')
else:
     print('No')
n=int,input()
a=list(map(int,input().split()))
m=max(a)
n=min(a)
for i in range(n,m+1):
    pre=0
    for j in range(len(a)):
        if i-1<=a[j]<=i+1:
            pre+=1
    ans=max(ans,pre)
    
print(ans)

### FIXED CODE ###

from collections import Counter
n=int(input())
a=list(map(int,input().split()))
for i in range(n):
    a.append(a[i]-1)
    a.append(a[i]+1)
c=Counter(a).most_common()
print(c[0][1])
import collections
n = int(input)
A = list(map(int, input().split()))
cnt = collections.Counter(A)
for i in range(n):
  print(cnt[i])
  

### FIXED CODE ###

import collections
n = int(input())
A = list(map(int, input().split()))
cnt = collections.Counter(A)
for i in range(1,n+1):
  print(cnt[i])
n = int(raw_input())
l = set()
max = -10^9
x=0
for i in range(n-1)
    l.add(int(raw_input())

for s in range(n-1)
 for t in range(s-1)
  x= l[n-s-1]-l[n-t-1]
  if max<x:
   max=x
printf('%d')%(max)

### FIXED CODE ###

n = int(raw_input())
max = -10**9
x1 = input() 
min = x1

for i in range(n-1):
 x = int(raw_input())

 if max < x-min:
  max = x- min
 if x< min:
  min = x
  
print max
a,b =map(int,input().split(' ')
if a + b == 15:
    print('+')
elsif a ＊ b == 15:
    print('＊')
else:
    print('x')

### FIXED CODE ###

a,b = map(int,input().split(' '))
if a + b == 15:
    print('+')
elif a * b == 15:
    print('*')
else:
    print('x')
a, b, c = map(int, input().split())
print(a*b*(0.5)

### FIXED CODE ###

a, b, c = map(int, input().split())
print(int(a*b*0.5))
import numpy as np
n, k = map(int, input().split())
h = np.array(list(map(int, input().split())))

print(np.sum(h>=k))

### FIXED CODE ###

n, k = map(int, input().split())
h = list(map(int, input().split()))
print(len([i for i in h if i>=k]))
li=list(map(int,input().split()))
print("YES" li.sort()==[5,5,7] else "NO")


### FIXED CODE ###

li=list(map(int,input().split()))
print("YES" if sorted(li)==[5,5,7] else "NO")
a = int
b = int
1<=a and a<=10000
1<=b and b<=10000
x = a*b
if x%2 == 0:
    print('Even')
else:
    print('Odd')

### FIXED CODE ###

a,b = map(int,input().split())
if (a*b)%2 ==0:
    print('Even')
else:
    print('Odd')
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

n = int(input())
arr = [int(input()) for _ in range(N)]
count = arr[0]-1
m = 1
for a in arr[1:]:
    if a == m:
        m += 1
    if a > m:
        count += (a - 1) // (m+1)
print(count)

### FIXED CODE ###

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

n = int(input())
arr = []
for i in range(n):
    arr.append(int(input()))
count = max(arr[0]-1,0)
index = 2
for i in range(1,n):
    if arr[i] > index:
        count += (arr[i] - 1) // index
    if arr[i] == index:
        index += 1
print(count)
n, a, b = list(map(int, input().split(' ')))
tmp = 0
count = 0
if a == 0:
    print(0)
while count < 10**100:
    n -= a
    tmp += a
    if n <= 0:
        print(tmp + n)
        break
    n -= b
    if n <= 0:
        print(tmp)
        break
    

### FIXED CODE ###

s = list(input())
if s[0] == s[1] == s[2]:
    print("No")
else:
    print("Yes")
n = int(input())
coins = []
for i in range(7):
    if 6**i <= 10**5:
        coins.append(6**i)
    if 9**i <= 10**5:
        coins.append(9**i)
dp = [10**5]*(n+1)

def f(i):
    if i==0:
        return 0
    if dp[i] != 10**5:
        return dp[i]
    candidates = [f(i-coin)+1 for coin in coins if i-coin >= 0]
    dp[i] = min(candidates)
    return dp[i]

print(f(n))

### FIXED CODE ###

# -*- coding: utf-8 -*-
"""
Created on Sun Jun 10 21:28:22 2018

@author: maezawa
"""

n = int(input())

c = [1, 6, 36, 216, 1296, 7776, 46656,
     9, 81, 729, 6561, 59049]
m = len(c)

dp = [float('inf') for _ in range(n+1)]
dp[0] = 0
#print(n,m)
#print(c)
#print(*dp, sep='\n')

for j in range(1,n+1):
    temp = []
    for i in range(m):
        if j < c[i]:
            continue
        else:
            temp.append(dp[j-c[i]]+1)
    dp[j] = min(temp)

print(dp[n])
n,m=map(int,input().split())
a=[]
b=[0]*(n+1)
b[1]=1
b[0]=1
MOD=(10**9)+7

a.append(int(input()),for _ in range(m))
  
if 1 in a:
  b[1]=0
else:
  b[1]=1
  
for j in range(2,n+1):
  if j not in a:
    b[j]=(b[j-1]+b[j-2])%MOD
    
print(b[n]%MOD)    
    
  
  
  



### FIXED CODE ###

n,m=map(int,input().split())
mod=10**9+7
a=[0]*(n+1)
dp=[0]*(n+1)
dp[0]=1
for i in range(m):
    a[int(input())]=1
    
if a[1]==1:
    dp[1]=0
else:
    dp[1]=1
    
for j in range(2,n+1):
    if a[j]==0:
        dp[j]=dp[j-1]+dp[j-2] 
                  
print(dp[n]%mod)
a = int(input())
b = int(input())

seki = a*b

if seki % 2 == 0:
    print("Even")

elif seki % 2 ==1:
    print("Odd")


### FIXED CODE ###

a, b = map(int, input().split())
if a*b % 2 == 0:
    print("Even")
else:
    print("Odd")
a=A
b=B
if 1<=a<=9:
  if 1<=b<=9:
    print(a*b)
else:
  print(-1)

### FIXED CODE ###

A,B=map(int,input().split())
if 1<=A<=9:
        if 1<=B<=9:
            print(A*B)
        else:
            print(-1)
else: print(-1)
from collections import deque

n = int(input())
d = deque()
for _i in range(n):
    line = input().split()
    order = line[0]
    if order in ('insert', 'delete'):
        key = int(line[1])
    if order == 'insert':
        d.appendleft(key)
    elif order == 'delete':
        d.remove(key)
    elif order == 'deleteFirst':
        d.popleft()
    elif order == 'deleteLast':
        d.pop()
    else:
        raise ValueError('Invalid order: {order}')

i = 0
while True:
    try:
        if i == 0:
            print(d.popleft(), end='')
        else:
            print(f' {d.popleft()}', end='')
        i += 1
    except IndexError:
        break
print()


### FIXED CODE ###

from collections import deque

n = int(input())
d = deque()
for _i in range(n):
    order = input()
    if order == 'deleteFirst':
        d.popleft()
    elif order == 'deleteLast':
        d.pop()
    else:
        order, key = order.split()
        if order == 'insert':
            d.appendleft(key)
        elif order == 'delete':
            try:
                d.remove(key)
            except ValueError:
                pass
        else:
            raise ValueError('Invalid order: {order}')

print(' '.join(d))
def count_zero(num):
    i = 10
    count = 0
    while(i <= num):
        count += int(num/i)
        i = i*5
    return count
n = int(input())
print(factorial(n))
if(n % 2 == 0):
    print(count_zero(n))
else:
    print(0)

### FIXED CODE ###

n=int(input())
if n%2 == 0:
    num=10
    c=0;
    while num <= n:
        c += (n//num)
        num *= 5
    print(c)
else:
    print(0)
A,B,C=map(int,input().split())
list=［A,B,C］
M=max(list)
D=9＊M+A+B+C
print(D)

### FIXED CODE ###

A,B,C=map(int,input().split())
list=[A,B,C]
M=max(list)
N=A+B+C
D=9*M+N
print(D)
N, M, K = list(map(int, input().split()))
A = list(map(int, input().split()))
B = list(map(int, input().split()))

A_sum = [0]
B_sum = [0]
for i in range(N):
    A_sum.append(A_sum[i]+A[i])
for i in range(M):
    B_sum.append(B_sum[i]+B[i])


num = 0
J = M
for i in range(N+1):
    if A_sum[i] > K:
        break
    for j in (J,-1,-1):
        if A_sum[i] + B_sum[j] <= K:
            if num < i+j:
                num = i+j
            J = j
            break

print(num)


### FIXED CODE ###

a = int(input())
print(a+a**2+a**3)
import math

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

aa=a*a
bb=b*b

hh=h*30
mm=m*6

cos = math.cos(math.radians(abs(hh-mm)))

cc=aa+bb-2*a*b*cos

c=math.sqrt(cc)

print(c)



### FIXED CODE ###

a=input()

if a[-1]=='3':
    print('bon')
elif a[-1]=='0' or a[-1]=='1' or a[-1]=='6' or a[-1]=='8':
    print('pon')
elif a[-1]=='2' or a[-1]=='4' or a[-1]=='5' or a[-1]=='7' or a[-1]=='9':
    print('hon')
A,B=map(int,input().split())
min_a=12.5*A
max_a=(100*A+1)/8
min_b=10*B
max_b=min_B+1
if(max_b<min_a or max_a<min_b):
  print(-1)
else:
  print(int(round(max(12.5*A,10*B)+0.4999999,0)))

### FIXED CODE ###

A, B =map(int, input().split())
max_A=(A+1)*25/2
max_B=10*(B+1)
p=int(round(max([A*12.5,B*10])+0.4999999999,0))
print(p if(p<min([(A+1)*25/2, 10*(B+1)]) and not(4<5*A-4*B) and not(5*A-4*B<-5)) else -1)
# Widespread
n,a,b = list(map(int, input().split()))
h = []
for i in range(n):
    h.append(int(input()))

import math

sup = sum([hh // a + 1 for hh in h])
inf = 1
 
while sup - inf > 1:
    x = (sup + inf) // 2
    c = sum([math.ceil((hh - x * b)/(a-b)) for hh in h if hh > x * b])
    if c <= x:
        sup = x
    else:
        inf = x
 
print(sup)

### FIXED CODE ###

#Restricted

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

if ( a+b >9 ):
    print("error")
else:
    print(a+b)
x=input()
print(x**3)

### FIXED CODE ###

a=input()
a=int(a)
a=a**3
print (a)
#!/usr/bin/env python
# -*- coding: utf-8 -*-

def dfs(s,Map,H,W,c):
    dxdy = [(1,0),(-1,0),(0,1),(0,-1),(1,1),(-1,-1),(-1,1),(1,-1)]
    for i in range(4):
        Map[s[0]][s[1]] = c
        nextPos = (s[0]+dxdy[i][0],s[1]+dxdy[i][1])
        if nextPos[0] >= 0 and nextPos[1] >= 0 and nextPos[0] < H and nextPos[1] < W:
            if Map[nextPos[0]][nextPos[1]] == 1:
                dfs(nextPos,Map,H,W,c)

while True:
    W,H = map(int,input().split(" "))
    if W == 0 and H == 0:
        break
    Map = [list(map(int,input().split())) for i in range(0,H)]
    c = 1
    for i in range(H):
        for j in range(W):
            if Map[i][j] == 1:
                c += 1
                dfs((i,j),Map,H,W,c)
    print(c-1)

### FIXED CODE ###

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
sys.setrecursionlimit(10**6)

def dfs(s,Map,H,W,c):
    dxdy = [(1,0),(-1,0),(0,1),(0,-1),(1,1),(-1,-1),(-1,1),(1,-1)]
    for i in range(len(dxdy)):
        Map[s[0]][s[1]] = c
        nextPos = (s[0]+dxdy[i][0],s[1]+dxdy[i][1])
        if nextPos[0] >= 0 and nextPos[1] >= 0 and nextPos[0] < H and nextPos[1] < W:
            if Map[nextPos[0]][nextPos[1]] == 1:
                dfs(nextPos,Map,H,W,c)

while True:
    W,H = map(int,input().split(" "))
    if W == 0 and H == 0:
        break
    Map = [list(map(int,input().split())) for i in range(0,H)]
    c = 1
    for i in range(H):
        for j in range(W):
            if Map[i][j] == 1:
                c += 1
                dfs((i,j),Map,H,W,c)
    print(c-1)
#!/usr/bin/env python3
import sys


def solve(N: int, a: "List[int]", b: "List[int]"):
    from itertools import count
    edges = [[] for _ in range(N)]
    for i, (aa, bb) in enumerate(zip(a, b)):
        edges[aa-1].append((bb-1, i))
        edges[bb-1].append((aa-1, i))
    ans = [None] * (N-1)
    visited = set()
    def dfs(n, c):
        if n in visited:
            return
        visited.add(n)
        for (nn, i), cc in zip(filter(lambda x: x[0] not in visited, edges[n]),
                          filter(lambda y: y != c, count(1))):
            ans[i] = cc
            dfs(nn, cc)
    dfs(0, None)
    return [max(ans)] + ans

# Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    a = [int()] * (N - 1)  # type: "List[int]"
    b = [int()] * (N - 1)  # type: "List[int]"
    for i in range(N - 1):
        a[i] = int(next(tokens))
        b[i] = int(next(tokens))
    print(*solve(N, a, b), sep="\n")

def test():
    import doctest
    doctest.testmod()

if __name__ == '__main__':
    #test()
    main()


### FIXED CODE ###

#!/usr/bin/env python3
import sys
sys.setrecursionlimit(200_000)

def solve(N: int, a: "List[int]", b: "List[int]"):
    from itertools import count
    edges = [[] for _ in range(N)]
    for i, (aa, bb) in enumerate(zip(a, b)):
        edges[aa-1].append((bb-1, i))
        edges[bb-1].append((aa-1, i))
    ans = [None] * (N-1)
    visited = set()
    def dfs(n, c):
        if n in visited:
            return
        visited.add(n)
        for (nn, i), cc in zip(filter(lambda x: x[0] not in visited, edges[n]),
                          filter(lambda y: y != c, count(1))):
            ans[i] = cc
            dfs(nn, cc)
    dfs(0, None)
    return [max(ans)] + ans

# Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    a = [int()] * (N - 1)  # type: "List[int]"
    b = [int()] * (N - 1)  # type: "List[int]"
    for i in range(N - 1):
        a[i] = int(next(tokens))
        b[i] = int(next(tokens))
    print(*solve(N, a, b), sep="\n")

def test():
    import doctest
    doctest.testmod()

if __name__ == '__main__':
    #test()
    main()
import math
from math import gcd,pi,sqrt
INF = float("inf")

import sys
sys.setrecursionlimit(10**6)
import itertools
from collections import Counter,deque
def i_input(): return int(input())
def i_map(): return map(int, input().split())
def i_list(): return list(i_map())
def i_row(N): return [i_input() for _ in range(N)]
def i_row_list(N): return [i_list() for _ in range(N)]
def s_input(): return input()
def s_map(): return input().split()
def s_list(): return list(s_map())
def s_row(N): return [s_input for _ in range(N)]
def s_row_str(N): return [s_list() for _ in range(N)]
def s_row_list(N): return [list(s_input()) for _ in range(N)]


def main():
    s = input() # 暗号
    t = input() # 含まれる文字

    s_len = len(s)
    t_len = len(t)

    for i in range(s_len - t_len,-1,-1):
        flg = True
        for j in range(t_len):
            if s[i+j] != t[j] and s[i+j] != "?":
                flg = False
                break
        if flg == True:
            flg = i
            break

    if flg == False:
        print("UNRESTORABLE")
        exit()

    ans = ""
    for i in s[:flg]:
        if i == "?":
            ans = ans + "a"
        else:
            ans = ans + i
    ans = ans + t

    for i in s[(flg+t_len):]:
        if i == "?":
            ans = ans + "a"
        else:
            ans = ans + i

    print(ans)














if __name__=="__main__":
    main()


### FIXED CODE ###

import math
from math import gcd,pi,sqrt
INF = float("inf")

import sys
sys.setrecursionlimit(10**6)
import itertools
from collections import Counter,deque
def i_input(): return int(input())
def i_map(): return map(int, input().split())
def i_list(): return list(i_map())
def i_row(N): return [i_input() for _ in range(N)]
def i_row_list(N): return [i_list() for _ in range(N)]
def s_input(): return input()
def s_map(): return input().split()
def s_list(): return list(s_map())
def s_row(N): return [s_input for _ in range(N)]
def s_row_str(N): return [s_list() for _ in range(N)]
def s_row_list(N): return [list(s_input()) for _ in range(N)]


def main():
    s = input() # 暗号
    t = input() # 含まれる文字

    s_len = len(s)
    t_len = len(t)
    flg = False

    for i in range(s_len - t_len,-1,-1):
        flg = True
        for j in range(t_len):
            if s[i+j] != t[j] and s[i+j] != "?":
                flg = False
                break
        if flg == True:
            trial = i
            break

    if flg == False:
        print("UNRESTORABLE")
        exit()

    ans = ""
    for i in s[:trial]:
        if i == "?":
            ans = ans + "a"
        else:
            ans = ans + i
    ans = ans + t

    for i in s[(trial+t_len):]:
        if i == "?":
            ans = ans + "a"
        else:
            ans = ans + i

    print(ans)














if __name__=="__main__":
    main()
N,M = map(int,input().split())
li = []
numb = []
ans = ''
for i in range(M):
  a,b = map(int,input().split())
  if a == 1:
    li.append(b)
  elif b == N:
    numb.append(a)
for i in range(M):
  if li[i] in numb:
    ans = 'POSSIBLE'
    break
if ans == '':
  print('IMPOSSIBLE')
else:
  print(ans)

### FIXED CODE ###

N,M = map(int,input().split())
li = []
numb = []
for i in range(M):
    a,b = map(int,input().split())
    if a == 1:
        li.append(b)
    elif b == N:
        numb.append(a)
ans = set(li) & set(numb)
if len(ans) >= 1:
    print('POSSIBLE')
else:
    print('IMPOSSIBLE')
"""
--- アルゴリズム概要 ---
前計算で頂点Kから各ノードへの最短路を記録しておき、
各クエリに対しては、dp[a]+dp[b]を返せばよい。

頂点Kから各ノードへの最短ルートはdfsとかで記録していけばよい。
"""

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

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

path = {K:""}
dp = [0]*(N+1)
def dfs(n,cost):
    path[n]=""
    dp[n]=cost
    for i,c in AB[n].items():
        if i not in path:
            dfs(i,cost+c)
dfs(K,0)
for i in range(Q):
    x,y=map(int,input().split())
    print(dp[x]+dp[y])

### FIXED CODE ###

"""
--- アルゴリズム概要 ---
前計算で頂点Kから各ノードへの最短路を記録しておき、
各クエリに対しては、dp[a]+dp[b]を返せばよい。

頂点Kから各ノードへの最短ルートはdfsとかで記録していけばよい。
"""
def main():
    from collections import deque
    import sys
    sys.setrecursionlimit(20000000)
    input = sys.stdin.readline
    N = int(input())
    AB = [[]for _ in range(N+1)]
    for i in range(N-1):
        a,b,c = map(int,input().split())
        AB[a].append((b,c))
        AB[b].append((a,c))

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

    path = {K:""}
    dp = [0]*(N+1)
    que = deque()
    for n,c in AB[K]:
        que.append((n,c))
    while que:
        now,cost = que.pop()
        path[now]=""
        dp[now]=cost
        for n,c in AB[now]:
            if n not in path:
                que.append((n,cost+c))
    for i in range(Q):
        x,y=map(int,input().split())
        print(dp[x]+dp[y])
main()