Problem:
 Score : 300 points    Problem Statement E869120 found a chest which is likely to contain treasure. However, the chest is locked. In order to open it, he needs to enter a string S consisting of lowercase English letters. He also found a string S' , which turns out to be the string S with some of its letters (possibly all or none) replaced with ? .  One more thing he found is a sheet of paper with the following facts written on it:   Condition 1: The string S contains a string T as a contiguous substring.  Condition 2: S is the lexicographically smallest string among the ones that satisfy Condition 1.   Print the string S . If such a string does not exist, print UNRESTORABLE .      Constraints  1 \leq |S'|, |T| \leq 50  S' consists of lowercase English letters and ? .  T consists of lowercase English letters.         Input Input is given from Standard Input in the following format:  S  T'       Output Print the string S . If such a string does not exist, print UNRESTORABLE instead.        Sample Input 1 ?tc????
coder      Sample Output 1 atcoder  There are 26 strings that satisfy Condition 1: atcoder , btcoder , ctcoder ,..., ztcoder .
Among them, the lexicographically smallest is atcoder , so we can say S = atcoder .       Sample Input 2 ??p??d??
abc      Sample Output 2 UNRESTORABLE  There is no string that satisfies Condition 1, so the string S does not exist.

Solution (Python):
import sys


def solve():
    readline = sys.stdin.buffer.readline
    mod = 10 ** 9 + 7
    s = str(readline().rstrip().decode('utf-8'))
    t = str(readline().rstrip().decode('utf-8'))
    lt = len(t)
    for i in range(len(s) - lt, -1, -1):
        ts = s[i:i+lt]
        b = True
        for j in range(len(ts)):
            if ts[j] != "?" and ts[j] != t[j]:
                b = False
                break
        if b:
            ans = []
            for j in range(len(s)):
                if i <= j < i + lt:
                    ans.append(t[j-i])
                else:
                    if s[j] == "?":
                        ans.append("a")
                    else:
                        ans.append(s[j])
            print("".join(ans))
            exit()
    print("UNRESTORABLE")


if __name__ == '__main__':
    solve()

Solution (Python):
S=input()
T=input()

s_len =len(S)
t_len =len(T)

flg=False

if s_len >= t_len:
    for head in range(len(S)-t_len,-1,-1):
        flg=True
        for s,t in zip(S[head:head+t_len],T):
            if not(s==t or "?"==s):
                flg=False
        if flg:
            break


if flg:
    S=list(S)
    S[head:head+t_len] = T
    S="".join(S)
    S=S.replace("?","a")
    print(S)
else:
    print("UNRESTORABLE")

Solution (Python):
import sys

if sys.platform =='ios':
	sys.stdin=open('Untitled.txt')
input = sys.stdin.readline
def MAP(): return [int(s) for s in input().split()]

S=input().rstrip()
T=input().rstrip()

ls=len(S)
lt=len(T)

cand=[]

for i in range(ls-lt+1):
	
	for j in range(lt):
		
		if S[i+j]!=T[j] and S[i+j]!='?':
			break
	else:
		candidate=S[:i]+T+S[i+lt:]
		cand.append(candidate.replace('?','a'))

if cand:
	print(min(cand))
else:
	print('UNRESTORABLE')

Solution (Python):
import sys
sys.setrecursionlimit(10**6)
def MAP(): return [int(s) for s in input().split()]

if sys.platform =='ios':
	sys.stdin=open('Untitled.txt')
input = sys.stdin.readline

S = input().rstrip()
T = input().rstrip()

ans = [s for s in S]
end = len(S)

if S.rfind('?'*len(T)) != -1:
	j = S.rfind('?'*len(T))
	for i in range(len(T)):
		ans[i+j] = T[i]
	print(''.join(ans).replace('?','a'))
	sys.exit()
else:
	for x in range(len(T)-1, -1, -1):
		while S.rfind(T[x],0,end) != -1:
			for i in range(len(T)-1, -1, -1):
				if S.rfind(T[i],0,end) != -1 and S.rfind(T[i],0,end)+len(T)-i<=len(S):
					j = S.rfind(T[i],0,end)
					flag = True
					for k in range(len(T)):
						
						if S[j+k-i] == T[k] or S[j+k-i] == '?':
							continue
						else:
							flag = False
							break
					else:
						if flag:
							for k in range(len(T)): ans[j+k-i] = T[k]
							print(''.join(ans).replace('?','a'))
							sys.exit()
			end -= 1
	print('UNRESTORABLE')

Solution (Python):
def main():
    S = input()
    T = input()

    
    def match(k):
        ans = True
        for i in range(len(T)):
            if S[k+i] != T[i] and S[k+i] != '?':
                ans = False
            
        return ans

    def construct(k):
        s = list(S)
        for i in range(len(T)):
            s[k+i] = T[i]
        return "".join(s)


    back = None
    for i in range(len(S) - len(T) + 1):
        if match(i):
            back = i

    if back is None:
        print('UNRESTORABLE')
    else:
        s = construct(back)
        s = s.replace('?', 'a')
        print(s)

if __name__ == "__main__":
    main()

Solution (Python):
a=input()
b=input()
c=len(a)
d=len(b)

for i in range(c-d+1):
  e=a[c-d-i:c-i]
  for j in range(d+1):
    if j==d:
      print((a[:c-d-i]+b+a[c-i:]).replace("?", "a"))
      exit()
    if e[j]=='?':
      continue
    elif e[j]!=b[j]:
      break
      
print("UNRESTORABLE")

Solution (Python):
s=input()
t=input()

tmp_i=0
tmp_j=0
ans_lst=[]
if len(s)<len(t):
    ans='UNRESTORABLE'
else:
    for i in range(len(s)-len(t)+1):
        cnt=0
        for j in range(len(t)):
            if s[i+j]==t[j] or s[i+j]=='?':
                cnt+=1
            if cnt==len(t):
                ans_s=s[:i]+t+s[i+len(t):]
                ans_lst.append(ans_s.replace('?','a'))
    else:
        ans='UNRESTORABLE'
ans_lst=sorted(ans_lst)
if len(ans_lst)==0:
    print(ans)
else:
    print(ans_lst[0])

Solution (Python):
def main():

    S = input()
    T = input()
    n = len(S)
    m = len(T)

    cand = []
    for i in range(n-m+1):
        flag = True
        for j in range(i, i+m):
            if S[j] != T[j-i] and S[j] != '?':
                flag = False
                break
        if flag:
            cand.append(S[:i] + T + S[i+m:])

    if not cand: return "UNRESTORABLE"
    ans = ""
    for U in cand:
        V = ""
        for c in U:
            if c == '?': V += "a"
            else: V += c
        if ans == "" or ans > V:
            ans = V
    return V

if __name__ == '__main__':
    print(main())

Solution (Python):
S_d,T = list(input()),list(input())

if len(S_d) < len(T):
  print("UNRESTORABLE");exit()

flag = 1
for i in range(-len(T),-len(S_d)-1,-1):
  key = 0
  for j in range(len(T)):
    if S_d[i+j]=="?" or S_d[i+j]==T[j]:
      key+=1
    else:
      break
  if key == len(T):
    st=len(S_d)+i
    break
  else:
    st=-1

if st==-1:
  print("UNRESTORABLE")
else:
  for i in range(len(S_d)):
    if st <= i and i < st+len(T):
      print(T[i-st],end="")
    elif S_d[i]=="?":
      print("a",end="")
    else:
      print(S_d[i],end="")

Solution (Python):
def main():
    S = input()
    T = input()

    SR = S[::-1]
    TR = T[::-1]

    restore = False
    pos = 0
    r = ""
    for i in range(len(S)):
        r += SR[:i]
        for j, si in enumerate(SR[i:]):
            if si == '?':
                r += TR[pos]
                pos += 1
            elif si == TR[pos]:
                r += TR[pos]
                pos += 1
            else:
                break
            
            if pos == len(T):
                restore = True
                break
        if restore:
            r += SR[i+j+1:]
            break
        pos = 0
        r = ""

    if restore:
        r = r.replace('?', 'a')
        r = r[::-1]
        print(r)
    else:
        print('UNRESTORABLE')


if __name__ == "__main__":
    main()

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

def main():
    def check(i):
        for j in range(tn):
            if s[i + j] != "?" and s[i + j] != t[j]:
                return False
        return True

    s = list(input())
    t = list(input())
    sn = len(s)
    tn = len(t)
    for i in range(sn - tn, -1, -1):
        if check(i):
            s = s[:i] + t + s[i + tn:]
            break
    else:
        print("UNRESTORABLE")
        exit()
    for i in range(sn):
        if s[i] == "?": s[i] = "a"
    print(*s, sep="")

main()

Solution (Python):
s, t = list(input()[::-1]), list(input()[::-1])
for i in range(len(s) - len(t) + 1):
    for j in range(len(t)):
        if s[i+j]!=t[j] and s[i+j]!="?": break
    else:
        for j in range(len(t)): s[i+j] = t[j]
        break
else: print("UNRESTORABLE");exit()
print("".join(s).replace("?", "a")[::-1])

Solution (Python):
import sys
Sd = input()
T = input()
candidate = []
ans = ""

for i in range(len(Sd) - len(T) + 1):
    flag = True
    for j in range(len(T)):
        if Sd[i+j] != T[j] and Sd[i+j] != "?":
            flag = False
    if flag:
        candidate.append(Sd[:i] + T + Sd[i+len(T):])

if not candidate:
    print("UNRESTORABLE")
    sys.exit(0)

for i in candidate:
    s = ""
    for j in i:
        if j =="?":
            s += "a"
        else:
            s += j
    if ans =="":
        ans = s
    else:
        ans = min(ans, s)
print(ans)

Solution (Python):
import sys


def solve():
    readline = sys.stdin.buffer.readline
    mod = 10 ** 9 + 7
    s = str(readline().rstrip().decode('utf-8'))
    t = str(readline().rstrip().decode('utf-8'))
    k = len(t)
    for i in range(len(s) - k, -1, -1):
        ts = s[i:i+k]
        b = True
        for j in range(k):
            if ts[j] != "?" and ts[j] != t[j]:
                b = False
                break
        if b:
            a = []
            f = True
            for j in range(len(s)):
                if j < i or i + k <= j:
                    if s[j] == "?":
                        a.append("a")
                    else:
                        a.append(s[j])
                else:
                    if f:
                        a = a + list(t)
                        f = False
            print("".join(a))
            exit()
    print("UNRESTORABLE")


if __name__ == '__main__':
    solve()

Solution (Python):
import sys
Sd = input()
T = input()
candidate = []
ans = ""

for i in range(len(Sd) - len(T) + 1):
    flag = True
    for j in range(len(T)):
        if Sd[i+j] != T[j] and Sd[i+j] != "?":
            flag = False
    if flag:
        candidate.append(Sd[:i] + T + Sd[i+len(T):])

if not candidate:
    print("UNRESTORABLE")
    sys.exit(0)

for i in candidate:
    s = ""
    for j in i:
        if j =="?":
            s += "a"
        else:
            s += j
    if ans =="":
        ans = s
    else:
        ans = min(ans, s)
print(ans)

Solution (Python):
s, t = list(input()[::-1]), list(input()[::-1])
for i in range(len(s) - len(t) + 1):
    for j in range(len(t)):
        if s[i+j]!=t[j] and s[i+j]!="?": break
    else:
        for j in range(len(t)): s[i+j] = t[j]
        break
else: print("UNRESTORABLE");exit()
print("".join(s).replace("?", "a")[::-1])

Solution (Python):

S = input()
T = input()

"""
アルゴリズム
Ｓの文字を一つずつずらしていって、Ｔが代入できるかどうかみる。
代入できるものなら代入してそのなかで辞書最小の文字列を保存する
全インデックスを試してみてできた文字列のなかで最小のものを出力
配列がなければＵＮ・・・を出力
"""
min_list = []

for i in range(len(S) - len(T) + 1):
    cut_s = S[i:i+len(T)]
    flg = True
    for s in range(len(cut_s)):
        if cut_s[s] == "?":
            continue
        if cut_s[s] == T[s]:
            continue
        else:
            flg = False
            break

    if flg:
        ano_s = S[:i] + T + S[i+len(T):]
        ano_s = ano_s.replace("?","a")
        min_list.append(ano_s)


if len(min_list) > 0:
    min_list.sort()
    print(min_list[0])
else:
    print("UNRESTORABLE")

Solution (Python):
s=input()
t=input()

sl=len(s)
tl=len(t)

for i in range(sl-tl+1):
  
  first=sl-tl-i
  flag="True"
  for j in range(tl): 
    if s[first+j]!="?": 
      if s[first+j]!=t[j]:
        flag="False" 
        break
  if flag=="False":
    
    continue
  else: 
    
    s=list(s)
    s[first:first+tl]=t
    for j,k in enumerate(s):
      if k=="?":
        s[j]="a"
    print("".join(s))
    exit()
print("UNRESTORABLE")

Solution (Python):
from sys import stdin
import math
import bisect
import heapq
import numpy as np
from math import factorial

s = stdin.readline().rstrip()
t = stdin.readline().rstrip()

atama = t[0]
lent = len(t)
lens = len(s)

lin = []

for i in range(lens-lent+1):
    flag = 0
    for j in range(lent):
        if s[i+j] != t[j] and s[i+j] != "?":
            flag = 1
    if flag == 0:
        lin.append((s[:i]+t+s[i+lent:]).replace("?","a"))
if lin == []:
    print("UNRESTORABLE")
    exit()

lin.sort()
print(lin[0])

Solution (Python):
s = list(input())
t = list(input())

len_t = len(t)
ans = []
for i in range(len(s)-len(t)+1):
  flag = True
  for j in range(i, i+len_t):
    if s[j] != '?' and s[j] != t[j-i]:
      flag = False

  if flag:
    tmp = s[:i] + t + s[i+len_t:]
    for k in range(len(tmp)):
      if tmp[k] == '?':
        tmp[k] = 'a'
    ans += [''.join(tmp)]
      
if len(ans) == 0:
  print('UNRESTORABLE')
  exit()
ans = sorted(ans)
print(ans[0])

Solution (Python):
s = input()
hint = input()
s_l = len(s)
h_l = len(hint)
not_q_ind = [i for i in range(s_l) if s[i] != "?"]
t_li = [ "?"*i + hint + "?"*(s_l-h_l - i) for i in range(s_l-h_l+1)]
t_li_l = len(t_li)

new_t_li = []
for i in range(t_li_l):
  f = 0
  for ind in not_q_ind:
    if (t_li[i][ind] == "?") or (t_li[i][ind] == s[ind]):
      t_li[i] = t_li[i][:ind] + s[ind] + t_li[i][ind+1:]
    else:
      f = 1
  if f == 0:
    new_t_li.append(t_li[i])
    
length = len(new_t_li)
if length == 0:
  print("UNRESTORABLE")
else:
  for i in range(length):
    new_t_li[i] = new_t_li[i].replace("?", "a")
  print(sorted(new_t_li)[0])

Solution (Python):
import copy

S = list(input())
T = list(input())

ans = 'UNRESTORABLE'
len_s = len(S)
len_t = len(T)

is_matched = False

for i in range(len_s - len_t + 1):
    s = copy.deepcopy(S)
    for j in range(len_t):
        sc = s[(-1) - i - j]
        tc = T[(-1) - j]
        if sc == tc or sc == '?':
            s[(-1) - i - j] = tc
            if len_t == (j + 1):
                is_matched = True
        else:
            break
    if  is_matched:
        ans = ''.join(s).replace('?', 'a')
        break
print(ans)

Solution (Python):
s = input()
t = input()

for i in range(len(s)-len(t), -1, -1):
    for j in range(len(t)):
        if s[i+j]=="?" or s[i+j]==t[j]:
            continue
        else:
            break
    else:
        s = s[:i] + t + s[i+len(t):]
        for k in range(len(s)):
            if s[k]=="?":
                s = s[:k] + "a" + s[k+1:]
        print(s)
        exit()

print("UNRESTORABLE")

Solution (Python):
import heapq as h
import math
import numpy as np
import copy
import itertools

 

s = input()
t = input()

candidates = []
count = 0
for i in range(len(s)):
    if ((s[i] == t[0]) or (s[i] == '?')) and ((len(s)-i) >= len(t)):
        count = 1
        for j in range(1,len(t)):
            if (s[i+j] == t[j]) or (s[i+j] == '?'):
                count += 1
            else:
                break
        if count == len(t):
            new = s[:i] + t
            if i+len(t) < len(s):
                new += s[i+len(t):]
            candidates.append(new.replace('?','a'))
if candidates != []:
    print(sorted(candidates)[0])
else:
    print('UNRESTORABLE')

Solution (Python):
s=input()
t=input()
found=False
st=0
for i in range(len(s)-len(t)+1):
  for j in range(len(t)):
    if s[i+j]!=t[j] and s[i+j]!="?":
      break
  else:
    found=True
    st=i

if not found:
  print("UNRESTORABLE")
else:
  ans=s[:st]+t+s[st+len(t):]
  print(ans.replace("?","a"))

Solution (Python):
s = list(input())
t = list(input())
ans = "UNRESTORABLE"
len_s = len(s)
len_t = len(t)
for i in range(len_s-len_t+1):
  j = len_s-len_t-i
  for S,T in zip(s[j:j+len_s],t):
    if S != T and S != "?":
      break
  else:
    ans = ""
    for k,TT in enumerate(t):
      s[j+k] = TT
    break
if ans == "UNRESTORABLE":
  print(ans)
else:
  for i in s:
    if i == "?":
      ans += "a"
    else:
      ans += i
  print(ans)

Solution (Python):
def examC():
    St = S(); T = S()
    N = len(St) - len(T)
    ans = -1
    for i in range(N,-1,-1):
        cur = 0
        for j in range(len(T)):
            if St[i+j]=="?" or St[i+j]==T[j]:
                cur +=1
                continue
            else:
                break
        if cur==len(T):
            ans =i
            break
    if ans == -1:
        ans="UNRESTORABLE"
    else:
        newS = ""
        for i in range(ans):
            if St[i]=="?":
                newS +="a"
            else:
                newS +=St[i]
        for i in range(ans,ans+len(T)):
            newS += T[i-ans]
        for i in range(ans+len(T),len(St)):
            if St[i]=="?":
                newS +="a"
            else:
                newS +=St[i]
        ans = newS
    print(ans)


import sys,copy,bisect,itertools
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
mod = 10**9 + 7
inf = float('inf')

examC()

Solution (Python):
S = input()
T = input()

ans = "z" * 100
for i in range(len(S) - len(T) + 1):
    s = list(S)
    for j in range(len(T)):
        if S[i+j] != "?" and S[i+j] != T[j]:
            break
        s[i+j] = T[j]
    else:
        s = "".join(s).replace("?", "a")
        if s < ans:
            ans = s
if ans == "z" * 100:
    print("UNRESTORABLE")
else:
    print(ans)

Solution (Python):
s=list(input())
T=list(input())

for i in range(len(s)-len(T)+1):
    F=0
    for j in range(len(T)):
        if s[len(s)-1-i-j]==T[len(T)-j-1] or s[len(s)-1-i-j]=="?":
            continue
        else:
            F=1
            break
    if F==0:
        for j in range(len(T)):
            if s[len(s)-1-i-j]=="?":
                s[len(s)-1-i-j]=T[len(T)-j-1]
        
        for j in range(len(s)):
            if s[j]=="?":
                s[j]="a"
        print("".join(s))
        exit()
print("UNRESTORABLE")

Solution (Python):
import copy
import sys
S = input()
T = input()
if len(T) > len(S):
    print("UNRESTORABLE")
    sys.exit()
I = []
S1 = []
for k in range(len(S)):
    if S[k] == '?':
        S1.append("a")
    else:
        S1.append(S[k])
for i in range(len(S)-len(T) + 1):
    S_ = copy.deepcopy(S1)
    flag = 1
    for j in range(i, i + len(T)):
        if S[j] != T[j-i] and S[j] != "?":
            flag = 0
            break
        else:
            S_[j] = T[j-i]     
    if flag:
        temp = ''.join(S_)
        I.append(temp)
S_ = S_.sort()
if len(I) > 0:
    print(I[-1])
else:
    print("UNRESTORABLE")

Solution (Python):
S=input()
T=input()
result = False
j=0
S=''.join(list(reversed(S)))
T=''.join(list(reversed(T)))

for i in range(len(S) - len(T) + 1):
    tmp_s=S
    if(S[i] == T[j] or S[i] == '?'):
        w=0
        for n in range(len(T)):
            if(S[i+n] == T[j+n] or S[i+n] == '?'):
                tmp_s = tmp_s[:i+n] + T[j+n] + tmp_s[i+n+1:]
                w+=1
            else:
                break
        if(w == len(T)):
            result = True
    if(result == True):
        break
    j=0
if(result == True):
    tmp_s=''.join(list(reversed(tmp_s)))
    print(tmp_s.replace('?','a'))
else:
    print('UNRESTORABLE')

Solution (Python):
def check(ii):
    j=0
    for i in range(ii,ii+len(t)):
        if s[i]!="?" and s[i]!=t[j]:
            return False
        j+=1
    return True

s=input()
t=input()
flag=False
for i in range(len(s)-len(t),-1,-1):
    if check(i):
        res=s[:i]+t+s[i+len(t):]
        print(res.replace("?","a"))
        flag=True
        break
if not flag:
    print("UNRESTORABLE")

Solution (Python):
def agree(s1, t1):
    for i in range(len(t1)):
        if not(s1[i] == t1[i] or s1[i] =='?'):
            return False
    return True

s = input()
t = input()  
ls, lt  = len(s), len(t)

ss =[]
for i in range(ls - lt +1):
    
    if agree(s[i:i+lt], t):
        ss.append( (s[:i] + t + s[i+lt:]).replace('?', 'a') )


if ss:
    print(min(ss))
else:
    print('UNRESTORABLE')

Solution (Python):
s = input()
t = input()
ls = len(s)
lt = len(t)

for i in range(ls, lt-1, -1):
  a = s[i-lt:i]
  if(all([a[j] == t[j] or a[j] == "?" for j in range(lt)])):
    s = s[:i-lt] + t + s[i:]
    print(s.replace("?","a"))
    exit()
    
print("UNRESTORABLE")

Solution (Python):
s = input()
t = input()
sl = len(s)
tl = len(t)
case = []

for i, si in enumerate(s):
    fault = False
    for j, tj in enumerate(t):
        if i + j >= sl:
            fault = True
            break
        if not s[i+j] in [tj, "?"]:
            fault = True
            break
    if not fault:
        case.append((s[0:i] + t + s[i+tl:]).replace("?", "a"))

if len(case) == 0:
    print("UNRESTORABLE")
else:
    print(sorted(case)[0])

Solution (Python):
S = list(input())[::-1]
T = list(input())[::-1]


for i in range(len(S) - len(T) + 1):
    for index, char in enumerate(S[i:i + len(T)], 0):
        if char != "?" and char != T[index]:
            break
    else:
        print("".join((S[:i] + T + S[i + len(T):])[::-1]).replace("?", "a"))
        break
else:
    print("UNRESTORABLE")

Solution (Python):
import sys
s = list(input())
t = list(input())

a = len(s)
b = len(t)

for i in range(a-b,-1,-1):
    t_kamo = s[i:i+b]
    count = 0
    for j in range(b):
        if t_kamo[j] == t[j] or t_kamo[j] == "?":
            count += 1
    if count == b:
        
        
        ans = "".join(s[:i]+t+s[i+b:])
        print(ans.replace("?","a"))
        sys.exit()
        
        
        
print("UNRESTORABLE")

Solution (Python):

S1 = input()
T = input()
n = len(S1)-len(T)
S = ""
p = 0
for i in range(n+1):
    k = 1
    for j in range(len(T)):
        if T[j] == S1[i+j] or S1[i+j] == "?":
            pass
        else:
            k = k - 1
    if k == 1:
        S = S1[0:i] + T + S1[i+len(T):len(S1)]
        p = 1
for i in range(len(S)):
    if S[i] == "?":
        S = S[0:i] + "a" + S[i+1:len(S)]
if p == 0:
    print("UNRESTORABLE")
else:
    print(S)

Solution (Python):
s,t=list(input()),list(input())
length_s,length_t=len(s),len(t)

flag=0
for i in reversed(range(length_s-length_t+1)):
    word=s[i:i+length_t]
    cnt=0
    for j in range(length_t):
        if word[j]==t[j] or word[j]=="?":
            cnt +=1
        if cnt==length_t:
            flag=1
            s[i:i+length_t]=t
    if flag==1:break
    
s="".join(s).replace("?","a")
print(s if flag else "UNRESTORABLE")

Solution (Python):
s,t=list(input()),list(input())
length_s,length_t=len(s),len(t)
flag=0
for i in reversed(range(length_s-length_t+1)):
    if flag==1:break
    word=s[i:i+length_t]
    cnt=0
    for j in range(length_t):
        if word[j]==t[j] or word[j]=="?":
            cnt +=1
        if cnt==length_t:
            flag=1
            s[i:i+length_t]=t
s="".join(s).replace("?","a")
print(s if flag else "UNRESTORABLE")

Solution (Python):
import sys


def main():
    S = list(input())[::-1]
    T = list(input())[::-1]
    d = len(S) - len(T) + 1

    for i in range(d):
        cnt = 0
        for j in range(len(T)):
            if S[i+j] == T[j] or S[i+j] == '?':
                cnt += 1

        if cnt == len(T):
            for k in range(len(T)):
                S[i+k] = T[k]
            break

    else:
        print('UNRESTORABLE')
        sys.exit()

    for i in range(len(S)):
        if S[i] == '?':
            S[i] = 'a'

    print(''.join(S)[::-1])


main()

Solution (Python):
S = input()
T = input()
len_S = len(S)
len_T = len(T)
qc = 0
ans = ''
for i in range(len_S-1, -1, -1):
    si = S[i]
    if (si == '?' or si == T[0]) and len_S - i >= len_T:
        flag = True
        for ii in range(1, len_T):
            if T[ii] == S[i+ii] or S[i+ii] == '?':
                continue
            else:
                flag = False
                break
        if flag:
            ans = S[:i] + T + S[i+len_T:]
            break

if ans:
    print(ans.replace('?', 'a'))
else:
    print('UNRESTORABLE')

Solution (Python):
s=input()
t=input()
score = 0
L = []
for i in range(len(s)-len(t)+1):
  k = s
  cut = k[i:i+len(t)]
  for j in range(len(t)):
    if cut[j] == t[j] or cut[j] =="?":
      score += 1
  if score == len(t):
    
    k = k[:i] + t + k[i+len(t):]
    L.append(k.replace("?","a"))
  score = 0
if L == []:
  print("UNRESTORABLE")
else:
  L = sorted(L)
  print(L[0])

Solution (Python):
S = input()
T = input()
def checker(s, t):
	for i in range(len(s)):
		if s[i] != "?" and s[i] != t[i]:
			return False
	return True


for i in reversed(range(len(S) - len(T) + 1)):
    if checker(S[i:(i + len(T))], T):
        S = S.replace('?', 'a')
        print(S[:i] + T + S[i + len(T):])
        exit()
print('UNRESTORABLE')
exit()

Solution (Python):
def main():
	s = input()
	t = input()
 
	for i in reversed(range(len(s) - len(t) + 1)):
		if checker(s[i:(i + len(t))], t):
			s = s.replace("?", "a")
			print(s[:i] + t + s[i+len(t):])
			return
	print("UNRESTORABLE")
 
def checker(s, t):
	for i in range(len(s)):
		if s[i] != "?" and s[i] != t[i]:
			return False
	return True
 
if __name__ == '__main__':
	main()

Solution (Python):
Sz=list(input())
T=list(input())
Flag=0

for i in range(len(Sz)-len(T),-1,-1):
    Flag=1
    for s in range(len(T)):

        if not (Sz[i+s]=="?" or Sz[i+s]==T[s]):
            Flag=0
            break
    if Flag:
        for s in range(len(T)):
             Sz[i+s]=T[s]
        break
for i in range(len(Sz)):
    if Sz[i]=="?":Sz[i]="a"

if Flag:print("".join(Sz))
else:print("UNRESTORABLE ")

Solution (Python):
S = list(input())
T = list(input())
lenS = len(S)
lenT = len(T)
change = 0
for i in range(lenS-lenT,-1,-1):
    a = 1
    for j in range(lenT):
        if S[i+j] != T[j] and S[i+j] != '?':
            a = 0
    if a == 1:
        for j in range(lenT):
            S[i+j] = T[j]
        change = 1
        break

for i in range(lenS):
    if S[i] == '?':
        S[i] = 'a'

if change == 1:
    print(''.join(S))
else:
    print('UNRESTORABLE')

Solution (Python):
s = list(input()[::-1])
t = list(input()[::-1])

for shift in range(len(s) - len(t) + 1):
  for i in range(len(t)):
    if s[i + shift] != "?" and s[i + shift] != t[i]:
      break
  else:
    s[shift:shift+len(t)] = t
    print("".join(s[::-1]).replace("?", "a"))
    break
else:
  print("UNRESTORABLE")

Solution (Python):
s=list(input())
t=input()

fin=[]

flag=0
for i in range(len(s)-len(t)+1):
    if(s[i]==t[0] or s[i]=='?'):
        ind=0
        f=1
        for j in range(i,i+len(t)):
            if(s[j]!='?' and s[j]!=t[ind]):
                f=0
                break
            ind+=1
        ind=0
        temp = s[:]
        if(f==1):
            flag=1
            for j in range(i,i+len(t)):
                if(s[j]=='?'):
                    temp[j]=t[ind]
                ind+=1
            fin.append(temp)
if(flag==0):
    print('UNRESTORABLE')
else:
    for i in range(len(fin)):
        for j in range(len(fin[i])):
            if(fin[i][j]=='?'):
                fin[i][j]='a'
    fin.sort()
    print(''.join(fin[0]))

Solution (Python):
s = list(input())
t = list(input())
f = -1
for i in range(len(s)-len(t)+1):
  for j,k,l in zip(s[i:i+len(t)],t,range(len(t))):
    if j != k and j != '?':
      break
    if l == len(t) -1:
      f = i
if f != -1:
  s[f:f+len(t)] = t
  for i in range(len(s)):
    if s[i] == '?':
      s[i] = 'a'
  print(*s,sep='')
else:
  print('UNRESTORABLE')

Solution (Python):
S = input()
T = input()

L = []
for i in range(len(S)-len(T)+1):
    cnt = 0
    for j in range(len(T)):
        if T[j] == S[i+j] or S[i+j] == "?":
            cnt += 1
    if cnt == len(T):
        temp = []
        for j in range(len(S)):
            if i <= j < i+len(T):
                temp.append(T[j-i])
            elif S[j] == "?":
                temp.append("a")
            else:
                temp.append(S[j])
        L.append("".join(temp))
        
if L:
    L.sort()
    print(L[0])
else:
    print("UNRESTORABLE")

Solution (Python):
s = input()
t = input()

n = len(s)
m = len(t)
if n < m:
  print("UNRESTORABLE")
  exit()
x = -1
for i in range(n-m+1):
  for j in range(m):
    if s[n-j-i-1] == "?" or s[n-j-i-1] == t[m-j-1]:
      continue
      
    else:
      
      
      
      
      break
  else:
    x = i
    
    break

if x == -1:
  print("UNRESTORABLE")
else:
  ans = s[:n-m-x]+t+s[n-x:]
  
  for i in range(n):
    if ans[i] == "?":
      ans = ans[:i] + "a" + ans[i+1:]
  print(ans)

Solution (Python):
def is_matched(i, inputS, T):
  for s,t in zip(inputS, T):
    if s == '?': continue
    if s != t: return False
  return True

S, T = input(), input()
lenS, lenT = len(S), len(T)
matched_i = None
for i in range(lenS-lenT+1):
  if is_matched(i, S[i:i+lenT], T):
    matched_i = i
if matched_i is None: print('UNRESTORABLE')
else: print(''.join([s for s in S[:matched_i] + T + S[matched_i+lenT:]]).replace('?', 'a'))

Solution (Python):
def is_matched(i, inputS, T):
  for s,t in zip(inputS, T):
    if s == '?': continue
    if s != t: return False
  return True

S, T = input(), input()
lenS, lenT = len(S), len(T)
matched_i = None
ans = []
for i in range(lenS-lenT+1):
  if is_matched(i, S[i:i+lenT], T):
    tmpS = S.replace('?', 'a')
    ans.append(''.join([s for s in S[:i].replace('?', 'a') + T + S[i+lenT:].replace('?', 'a')]))
if ans == []: print('UNRESTORABLE')
else: print(sorted(ans)[0])

Solution (Python):
S = input()
T = input()
t_len = len(T)
s_len = len(S)
output_list = [''] * len(S)
flag = False
hatena_to_a_str = ''
for i in range(s_len):
    if S[i] == '?':
        hatena_to_a_str += 'a'
    else:
        hatena_to_a_str += S[i]

current_min = 'z' * (s_len+1)
for i in reversed(range(s_len-t_len+1)):
    
    text = S[i:i+t_len]
    if S[i:t_len] == T:
        flag = False
    else:
        for j in range(t_len):
            if text[j] != T[j] and text[j] != '?':
                break 
            if j == t_len-1:
                flag = True
                for s in range(t_len):
                    output_list[i+s] = T[s]
    if flag:
        tmp = hatena_to_a_str[:i] + T + hatena_to_a_str[i+t_len:s_len]
        current_min = min(current_min,tmp)
        flag = False

if current_min == 'z' * (s_len+1):
    print('UNRESTORABLE')
else:
    print(current_min)

Solution (Python):
sd = input()
t = input()
sf = ""
for i in sd:
  if i == "?":
    sf += "a"
  else:
    sf += i
S = []
for i in range(len(sd)-len(t)+1):
  b = True
  for j in range(len(t)):
    if sd[i+j] != t[j] and sd[i+j] != "?":
      b = False
  if b:
    s = sf[:i] + t + sf[i+len(t):]
    S.append(s)
if S == []:
  print("UNRESTORABLE")
else:
  print(sorted(S)[0])

Solution (Python):
import pprint
s = input()
t = input()
candidate = []
for i in range(len(s) - len(t) + 1):
    can = True
    for j in range(len(t)):
        if s[i + j] == "?" or s[i + j] == t[j]:
            continue
        else:
            can = False
            break
    if can:
        res = []
        for j in range(i):
            if s[j] == "?":
                res.append("a")
            else:
                res.append(s[j])
        for j in range(len(t)):
            res.append(t[j])
        for j in range(len(s) - len(t) - i):
            if s[j + len(t) + i] == "?":
                res.append("a")
            else:
                res.append(s[j + len(t) + i])
        candidate.append("".join(res))

if len(candidate) != 0:
    candidate.sort()
    
    print(candidate[0])

else:
    print("UNRESTORABLE")