It's hard times now. Today Petya needs to score 100 points on Informatics exam. The tasks seem easy to Petya, but he thinks he lacks time to finish them all, so he asks you to help with one..

There is a glob pattern in the statements (a string consisting of lowercase English letters, characters "?" and "*"). It is known that character "*" occurs no more than once in the pattern.

Also, n query strings are given, it is required to determine for each of them if the pattern matches it or not.

Everything seemed easy to Petya, but then he discovered that the special pattern characters differ from their usual meaning.

A pattern matches a string if it is possible to replace each character "?" with one good lowercase English letter, and the character "*" (if there is one) with any, including empty, string of bad lowercase English letters, so that the resulting string is the same as the given string.

The good letters are given to Petya. All the others are bad.


-----Input-----

The first line contains a string with length from 1 to 26 consisting of distinct lowercase English letters. These letters are good letters, all the others are bad.

The second line contains the pattern — a string s of lowercase English letters, characters "?" and "*" (1 ≤ |s| ≤ 10^5). It is guaranteed that character "*" occurs in s no more than once.

The third line contains integer n (1 ≤ n ≤ 10^5) — the number of query strings.

n lines follow, each of them contains single non-empty string consisting of lowercase English letters — a query string.

It is guaranteed that the total length of all query strings is not greater than 10^5.


-----Output-----

Print n lines: in the i-th of them print "YES" if the pattern matches the i-th query string, and "NO" otherwise.

You can choose the case (lower or upper) for each letter arbitrary.


-----Examples-----
Input
ab
a?a
2
aaa
aab

Output
YES
NO

Input
abc
a?a?a*
4
abacaba
abaca
apapa
aaaaax

Output
NO
YES
NO
YES



-----Note-----

In the first example we can replace "?" with good letters "a" and "b", so we can see that the answer for the first query is "YES", and the answer for the second query is "NO", because we can't match the third letter.

Explanation of the second example.   The first query: "NO", because character "*" can be replaced with a string of bad letters only, but the only way to match the query string is to replace it with the string "ba", in which both letters are good.  The second query: "YES", because characters "?" can be replaced with corresponding good letters, and character "*" can be replaced with empty string, and the strings will coincide.  The third query: "NO", because characters "?" can't be replaced with bad letters.  The fourth query: "YES", because characters "?" can be replaced with good letters "a", and character "*" can be replaced with a string of bad letters "x".

Difficulty: interview

Starter code:

Solutions:
g = set(input())
s = input()
n = int(input())
a = s.find("*")
for _ in range(n):
    temp = input()
    if a == -1:
        if len(temp) != len(s):
            print("NO")
        else:
            for i in range(len(s)):
                if s[i] == '?':
                    if temp[i] not in g:
                        print("NO")
                        break
                elif s[i] != temp[i]:
                    print("NO")
                    break
            else:
                print("YES")
    else:
        if len(temp) < len(s)-1:
            print("NO")
        else:
            for i in range(a):
                if s[i] == '?':
                    if temp[i] not in g:
                        print("NO")
                        break
                elif s[i] != temp[i]:
                    print("NO")
                    break
            else:
                for i in range(-(len(s) - a-1), 0):
                    if s[i] == '?':
                        if temp[i] not in g:
                            print("NO")
                            break
                    elif s[i] != temp[i]:
                        print("NO")
                        break
                else:
                    for i in range(a, len(temp)-(len(s) - a-1)):
                        if temp[i] in g:
                            print("NO")
                            break
                    else:
                        print("YES")

good = set(input().strip())
pattern = input().strip()
n = int(input())

minlen = len(pattern)

is_star = '*' in pattern

if is_star:
    minlen -= 1
    maxlen = 1000000000
    leftlen = pattern.find('*')
    rightlen = len(pattern) - leftlen - 1
else:
    maxlen = minlen

def check_simple_pattern(task, pattern):
    #print(task +" -> " + pattern)
    for i in range(len(task)):
        if pattern[i] != task[i] and not (pattern[i] == '?' and task[i] in good):
            return False
    return True

def check(task):
    if len(task) < minlen or len(task) > maxlen:
        return False
    if is_star:
        if rightlen == 0:
            cond = all([i not in good for i in task[leftlen:]])
        else:
            cond = all([i not in good for i in task[leftlen:-rightlen]])
        return check_simple_pattern(task[:leftlen], pattern[:leftlen]) \
                and (rightlen == 0 or check_simple_pattern(task[-rightlen:], pattern[-rightlen:])) \
                and cond
    else:
        return check_simple_pattern(task, pattern)

for i in range(n):
    task = input().strip()
    if check(task):
        print("YES")
    else:
        print("NO")

def m(good, pat, s):
  if len(pat) != len(s):
    return False
  else:
    for u, v in zip(pat, s):
      if u == "?": 
        if v not in good:
          return False
      elif u != v:
        return False
    return True      
    
def mm(good, p, l0, l1, s):
  if l0 + l1 > len(s):
    return False
  if l0 and not m(good, p[0], s[:l0]):
    return False
  if l1 and not m(good, p[1], s[-l1:]):
    return False
  if l1:
    ss = s[l0:-l1]    
  else:
    ss = s[l0:]    
  return not good.intersection(ss)

good = set(input().strip())
pat = input().strip()    
p = pat.split("*")
f = len(p) > 1
if f:
  l0 = len(p[0])
  l1 = len(p[1])    

n = int(input())
for _ in range(n):
  s = input().strip()
  r = mm(good, p, l0, l1, s) if f else m(good, pat, s)
  print("YES" if r else "NO")

     


def compare(a,b):
	nonlocal good,replacements
	if(a==b):
		return 1
	elif(b=='?' and (a in good)):
		return 1
	elif(b=='*' and (a not in good)):
		if(replacements<0):
			return 0
		else:
			return replacements+10
	elif(b=='*' and (a in good)):
		return 2
	else:
		return 0

g = input()
good={}
for val in g:
	good[val]=1
patt = input()
n=int(input())
for i in range(n):
	q=input()
	replacements = len(q)-len(patt)+1
	it=0
	c=0
	while(c<len(q) and it<len(patt)):
		result=compare(q[c],patt[it])
		if(result==2):
			it+=1
		elif(result==1):
			it+=1
			c+=1
		elif(result>9):
			if(replacements==0):
				it+=1
			else:
				c+=1
				replacements-=1
		else:
			break
	if(it<len(patt)):
		if(patt[it]=='*'):
			it+=1
	if(c==len(q) and it==len(patt)):
		print("YES")
	else:
		print("NO")


# IAWT
goods = input()

ptrn = input()
n = int(input())
qs = [input() for i in range(n)]

data = [False for i in range(26)]

for i in range(len(goods)):
    data[ord(goods[i])-97] = True

star_in = '*' in ptrn
if star_in:
    sind = ptrn.index('*')

theL = len(ptrn)

def f(q):
    if not star_in:
        if len(q) != theL:
            return False
        for i in range(theL):
            if ptrn[i] != '?': # So should be exactly equal
                if ptrn[i] != q[i]:
                    return False
            elif not data[ord(q[i])-97]:
                return False
        return True
    if len(q) < theL-1:
        return False
    for i in range(sind):
        if ptrn[i] == '?':
            if(not data[ord(q[i])-97]):
                return False
        elif ptrn[i] != q[i]:
            return False
    j = len(q) - 1
    for i in range(theL-1, sind, -1):
        if ptrn[i] == '?':
            if (not data[ord(q[j])-97]):
                return False
        elif ptrn[i] != q[j]:
            return False
        j -= 1
    for i in range(sind, j+1):
        if data[ord(q[i])-97]:
            return False
    return True

for i in range(n):
    if f(qs[i]):
        print('YES')
    else:
        print('NO')


import string

def single_check(p, a):
    if not p:
        return 1
    if len(a) != len(p):
        return 0
    else:
        for i in range(len(a)):
            if (p[i] != a[i] and p[i] != '?') or (p[i] == '?' and a[i] not in good):
                break
        else:
            return 1
        return 0


good = input()
bad = set(string.ascii_lowercase) - set(good)
pattern = input()
n = int(input())
patspl = pattern.split('*')
for _ in range(n):
    a = input()
    if len(patspl) == 1:
        print('YES' if single_check(pattern, a) else 'NO')
    else:
        if len(a) < len(pattern) - 1:
            print('NO')
        else:
            if single_check(patspl[0], a[:len(patspl[0])]) and single_check(patspl[1], a[-len(patspl[1]):]):
                for c in a[len(patspl[0]):(-len(patspl[1]) or len(a))]:
                    if c not in bad:
                        print('NO')
                        break
                else:
                    print('YES')
            else:
                print('NO')




good = input()
mask = input()
flag = 0
if '*' in mask:
    flag = 1


def check(a, b):
    if a == b:
        return 1;
    if a == '?':
        for i in range(len(good)):
            if good[i] == b:
                return 1
    return 0


n = int(input())
for i in range(n):
    s = input()
    accept = 1
    if not flag:
        if len(s) != len(mask):
            print('NO')
            continue
        for j in range(len(s)):
            if not check(mask[j], s[j]):
                accept = 0
        if not accept:
            print("NO")
        else:
            print("YES")
    else:
        if len(mask) - 1 > len(s):
            print("NO")
            continue
        for j in range(len(mask)):
            if mask[j] != '*':
                if not check(mask[j], s[j]):
                    accept = 0
                    break
            else:
                l = j
                break
        for j in range(len(s)):
            ind = len(mask) - 1 - j
            cur = len(s) - 1 - j
            if mask[ind] != '*':
                if not check(mask[ind], s[cur]):
                    accept = 0
                    break
            else:
                r = cur
                break
        if not accept:
            print('NO')
            continue
        if (len(s) + 1 == len(mask)):
            print('YES')
            continue
        for j in range(l, r + 1):
            for k in range(len(good)):
                if (good[k] == s[j]):
                    accept = 0
        if not accept:
            print('NO')
        else:
            print('YES')

#!/usr/local/bin/python3

import sys

good_letters = set(input())
pattern = input()

input()

for line in sys.stdin:
    query = line.strip()

    diff = len(query) - len(pattern) + 1

    if diff < 0:
        print("NO")
        continue

    query_index = 0
    pattern_index = 0
    question = None

    result = "NO"

    while (pattern_index < len(pattern)) and (query_index < len(query)):

        current_letter = query[query_index]
        current_pattern = pattern[pattern_index]

        if current_pattern == '*':
            if diff == 0:
                pattern_index += 1
            else:
                if current_letter in good_letters:
                    break
                else:
                    query_index += 1
                    diff -= 1
        elif current_pattern == '?':
            if current_letter in good_letters:
                    query_index += 1
                    pattern_index += 1
            else:
                break
        else:
            if current_letter != current_pattern:
                break
            else:
                query_index += 1
                pattern_index += 1
    
    if (query_index == len(query)) and (pattern_index == len(pattern) - 1) and (pattern[pattern_index] == '*'):
        result = "YES"

    if (query_index == len(query)) and (pattern_index == len(pattern)):
        result = "YES"

    print(result)


good = input()
pat = input()
n = int(input())
for i in range(n):
    s = input()
    q = True
    sc = 0
    x = 0
    while x < len(pat) and sc < len(s):
        if pat[x] == '*':
            dif = len(s)-len(pat)+1
            cont = False
            sc -= 1
            for j in range(dif):
                sc += 1
                if s[sc] in good:
                    cont = True
                    break
            if cont:
                q = False
                print("NO")
                break
        elif s[sc] != pat[x]:
            if pat[x] != '?' or s[sc] not in good:
                q = False
                print("NO")
                break
        x += 1
        sc += 1
    if x < len(pat):
        if pat[x] == '*':
            x += 1
    if q:
        if x < len(pat) or sc < len(s):
            q = False
            print("NO")
    if q:
        print("YES")


s = input()
good = {}
for i in 'abcdefghijklmnopqrstuvwxyz':
    good[i] = 0
for i in s:
    good[i] = 1
s = input()
tmp = s
n = int(input())
for i in range(n):
    q = input()
    while (s[0] == q[0] or (s[0] == '?' and good[q[0]] == 1)):
        s = s[1:]
        q = q[1:]
        if (s == '' or q == '') : break
    if (s != '' and q != '') :
        while (s[-1] == q[-1] or (s[-1] == '?' and good[q[-1]] == 1)):
            s = s[:-1]
            q = q[:-1]
            if (s == '' or q == '') : break
    ans = 1
    if (s == '*'):
        for j in q:
            if (good[j] == 1): ans = 0
    elif (s == q) : ans = 1
    else : ans = 0
    if (ans == 1): print("YES")
    else: print("NO")
    s = tmp


good = input()
s = input()
pos = -1
for j in range(len(s)):
	if s[j] == '*':
		pos = j
		break	
n = int(input())
hasStar = ('*' in s)
for i in range(n):
	t = input()
	if len(t) < len(s)- 1:
		print('NO')
		continue
	if hasStar:
		flag = True
		if len(t) <= (pos + 1) - 2:
			print('NO')
			continue
		if len(t) == (pos + 1) - 1:
			if len(s) >= (pos + 1) + 1:
				print('NO')
				continue
			
		for j in range(pos):
			if (t[j] not in good and s[j] == '?') or (s[j] != '?' and s[j] != t[j]):
				flag = False
				break
		if not flag:
			print('NO')
			continue
		l = pos
		r0 = len(t) - (len(s) - pos - 1)
		r = r0
		for j in range(pos + 1, len(s)):
			if (t[r] not in good and s[j] == '?') or (s[j] != '?' and s[j] != t[r]):
				flag = False
				break
			r += 1
		if not flag:
			print('NO')
			continue
		for j in range(l, r0):
			if t[j] in good:
				flag = False
				break
		if not flag:
			print('NO')
			continue
		print('YES')
	else:
		if len(s) != len(t):
			print('NO')
			continue
		flag = True
		for j in range(len(t)):
			if (t[j] not in good and s[j] == '?') or (s[j] != '?' and s[j] != t[j]):
				flag = False
				break
		if not flag:
			print('NO')
			continue
		print('YES')


l=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
s1=input().strip()
s1=list(s1)
s2=[]
for i in l:
    if i not in s1:
        s2.append(i)
pattern=input().strip()
pattern=list(pattern)
if ("*" not in pattern):
    length_main=len(pattern)
    n=int(input().strip())
    for i in range(n):
        f=0
        string=input().strip()
        length=len(string)
        if (length_main==length):
            for j in range(length_main):
                if (string[j]!=pattern[j]):
                    if (pattern[j]!="?"):
                        print ('NO')
                        f=1
                        break
                    else:
                        if (string[j] not in s1):
                            print ('NO')
                            f=1
                            break
            if (f==0):
                print ("YES")
        else:
            print ('NO')
else:
    ind=pattern.index("*")
    length_main=len(pattern)
    n=int(input().strip())
    for i in range(n):
        string=input().strip()
        length=len(string)
        if (length<length_main-1):
            print ('NO')
        else:
            f=0
            for j in range(ind):
                if (string[j]!=pattern[j]):
                    if (pattern[j]!="?"):
                        print ('NO')
                        f=1
                        break
                    else:
                        if (string[j] not in s1):
                            print ('NO')
                            f=1
                            break
            if (f!=1):
                q=length_main-length
                for j in range(length_main-1,ind,-1):
                    if (string[j-q]!=pattern[j]):
                        if (pattern[j]!="?"):
                            print ('NO')
                            f=1
                            break
                        else:
                            if (string[j-q] not in s1):
                                print ('NO')
                                f=1
                                break
                if (f!=1):
                    save= ind+1-q
                    string=list(string)
                    x=string[ind:save]
                    for j in x:
                        if (j not in s2):
                            print ('NO')
                            f=1
                            break
                    if (f==0):
                        print ('YES')

s = input()
good = {}
for i in 'abcdefghijklmnopqrstuvwxyz':
    good[i] = 0
for i in s:
    good[i] = 1
s = input()
tmp = s
n = int(input())
for i in range(n):
    q = input()
    while (s[0] == q[0] or (s[0] == '?' and good[q[0]] == 1)):
        s = s[1:]
        q = q[1:]
        if (s == '' or q == '') : break
    if (s != '' and q != '') :
        while (s[-1] == q[-1] or (s[-1] == '?' and good[q[-1]] == 1)):
            s = s[:-1]
            q = q[:-1]
            if (s == '' or q == '') : break
    ans = 1
    if (s == '*'):
        for j in q:
            if (good[j] == 1): ans = 0
    elif (s == q) : ans = 1
    else : ans = 0
    if (ans == 1): print("YES")
    else: print("NO")
    s = tmp


l = input()
s = input()
n = int(input())
a = []
for _ in range(n):
    c = input()
    nl = s
    if '*' in nl:
        if len(nl) <= len(c):
            nl = nl.replace('*', '*'*(abs(len(nl)-len(c))+1))
        elif len(nl)==len(c)+1:
            nl = nl.replace('*', '')
    if len(c)!=len(nl):
        a.append("NO")
    else:
        for i in range(len(c)):
            if nl[i] == '*':
                if c[i] in l:
                    a.append("NO")
                    break
            elif nl[i] == '?':
                if c[i] not in l:
                    a.append("NO")
                    break
            else:
                if nl[i] != c[i]:
                    a.append("NO")
                    break
        else:
            a.append("YES")
for i in a:
    print(i)


def check(s, start, end, mp):
    if len(s) < len(start) + len(end):
        return 0
    for i in range(len(start)):
        if start[i] == '?':
            if mp.get(s[i], 0) != 1:
                return 0
        if start[i] != '?' and start[i] != s[i]:
            return 0
    for i in range(1, len(end) + 1):
        if end[-i] == '?':
            if mp.get(s[-i], 0) != 1:
                return 0
        if end[-i] != '?' and end[-i] != s[-i]:
            return 0
    for i in range(len(start), len(s) - len(end)):
        if mp.get(s[i], 0) == 1:
            return 0
    return 1


good = input()
template = input()
res = template.find('*')
if res != -1:
    x = template.split('*')
    start, end = x[0], x[1]
else:
    start = template
    end = ''
n = int(input())
x = {}
for i in good:
    x[i] = 1

for i in range(n):
    s = input()
    if res == -1 and len(s) > len(start) + len(end):
        print('NO')
    elif check(s, start, end, x):
        print('YES')
    else:
        print('NO')


string = input()
good = list(string)
s = input()
a = len(s)
condition = "*" in s
n = int(input())
results = []
for x in range(n):
    temp = s
    t = input()
    b = len(t)
    condition1 = a == b + 1 and not condition
    if a > b + 1 or condition1:
        result = "NO"
    elif b > a and not condition:
        result = "NO"
    else:
        result = "YES"
        if condition:
            p = s.index("*")
            q = a - p
            if q == 1:
                r = t[p:]
            else:
                r = t[p:-q + 1]
            for x in r:
                if x in good:
                    result = "NO"
                    break
            temp = s[:p] + r + s[p + 1:]
        if result == "YES":
            for x in range(b):
                if temp[x] == "?":
                    if not t[x] in good:
                        result = "NO"
                        break
                elif temp[x] != t[x]:
                    result = "NO"
                    break
    results.append(result)
for x in results:
    print(x)

import sys



def main():
    g = list(sys.stdin.readline().rstrip())
    s = list(sys.stdin.readline().rstrip())
    n = int(sys.stdin.readline())
    star = -1
    for i in range(len(s)):
        if s[i]=='*':
            star = i


    for i in range(n):
        t=sys.stdin.readline().rstrip()
        ok = True
        if (star == -1 and len(s) > len(t)) or (star!=-1 and len(s)-1>len(t)):
            print("NO")
            continue
        for j in range(star):
            if s[j] =='?':
                if t[j] not in g:
                    ok = False
                    break
            elif t[j]!=s[j]:
                ok = False
                break
        if not ok:
            print("NO")
            continue
        if len(t)< len(s)-star-1:
            print("NO")
            continue
        for j in range(len(s)-star-1):
            k = len(t)-j-1
            m = len(s)-j-1
            if s[m] =='?':
                if t[k] not in g:
                    ok = False
                    break
            elif t[k]!=s[m]:
                ok = False
                break
        if not ok:
            print("NO")
            continue
        if star == -1 and len(s)==len(t):
            print("YES")
            continue
        if star == -1:
            print("NO")
            continue
        for j in range(star, len(t)-(len(s)-star-1)):
            if t[j] in g:
                ok = False
                break
        if not ok:
            print("NO")
            continue
        print("YES")

    

    
main()


good = set(list(input()))
s = input()
st = '*' in list(s)

def f(x):
	d = len(x) - len(s) + 1
	if d < 0 or (len(x) != len(s) and not st):
		return False
	ind = -1
	for i in range(len(x)):
		if s[i] == '*':
			ind = i
			break
		elif s[i] == '?' and x[i] not in good:
			return False
		elif 'a' <= s[i] <= 'z' and s[i] != x[i]:
			return False

	if ind == -1:
		return True

	for i in range(ind, ind + d):
		if x[i] in good:
			return False

	for i in range(ind + d, len(x)):
		if s[i - d + 1] == '?' and x[i] not in good:
			return False
		elif 'a' <= s[i - d + 1] <= 'z' and s[i - d + 1] != x[i]:
			return False

	return True

n = int(input())
for i in range(n):
	c = input()
	if f(c):
		print("YES")
	else:
		print("NO")

a=input()
s=input()
k=int(input())

def qw(c):
	t=True
	w=-1
	if len(s)>len(c)+1: t=False
	try:
		for j in range(len(s)):
			w+=1
			if s[j]=='?':
				if c[w] not in a: t=False
			elif s[j]=='*':
				b=len(c)-len(s)+1
				for e in c[j:j+b]:
					if e in a:
						t=False
				w+=b-1
			else:
				if s[j]!=c[w]: t=False
			if t==False: break
	except IndexError:
		return False
	return t if len(c)==w+1 else False

for i in range(k):
	c=input()
	print('YES') if qw(c) else print('NO')

import re
abc = set(list('abcdefghijklmnopqrstuvwxyz'))

good = set(input())
sp = {'?', '*'}

bad = abc - good
ans = []

pat = input()
pl = len(pat)
for _ in range(int(input())):
    hasstar = False
    answered = False
    q = input()
    d = len(q) - pl
    if d < -1 or (d == -1 and "*" not in pat):
        ans.append("NO")
        continue
    else:
        i = 0
        j = 0
        while i < pl:
            if (pat[i] == '?' and q[j] in bad) or (pat[i] not in sp and q[j] != pat[i]):
                ans.append("NO")
                answered = True
                break
            elif pat[i] == '*':
                hasstar = True
                if any(q[k] in good for k in range(j, j+d+1)):
                    ans.append("NO")
                    answered = True
                    break
                j += d
            i += 1
            j += 1
        if not answered:
            if not hasstar:
                if pl == len(q):
                    ans.append('YES')
                    answered = True
                else:
                    ans.append('NO')
                    answered = True
            else:
                ans.append("YES")
                answered = True
print('\n'.join(ans))


alp = set(input())
pt = input()
lp= pt.split('*')[0]
rp = ''
if len(pt.split('*')) > 1:
    rp = pt.split('*')[1]
n = int(input())

def check(p, qp):
    for i in range(len(p)):
        if p[i] == qp[i] or (p[i] == '?' and qp[i] in alp):
            continue
        else:
            return False
    return True

def checks(qp):
    for i in range(len(qp)):
        if qp[i] not in alp:
            continue
        else: 
            return False
    return True

for i in range(n):
    q = input()
    if len(lp) + len(rp) > len(q) or ('*' not in pt and len(lp) < len(q)):
        print('NO')
        continue
    qlp = q[:len(lp)]
    qrp = q[(len(q) - len(rp)):]
    star = q[len(lp):(len(q) - len(rp))]
    if check(lp, qlp) and check(rp, qrp) and checks(star):
        print('YES')
    else:
        print('NO')

good = list(input())
s = list(input())
stin = '*' in s
if stin:
    stind = s.index('*')
    s.pop(stind)
n = len(s)
for p in range(int(input())):
    a = list(input())
    ans = True
    if stin:
        while stind < len(a) > n and a[stind] not in good:
            a.pop(stind)
    if len(a) != n:
        ans = False
    else:
        for i in range(n):
            if a[i] == s[i] or a[i] in good and s[i] == '?':
                None
            else:
                ans = False
                break
    if ans:
        print('YES')
    else:
        print('NO')

def local_right_to_global(test, local_right):
    return(len(test) + local_right)
    
def local_left_to_global(test, local_left):
    return(local_left)

def check_left(test, template, good_symbols):
    left_idx = -1
    for symbol in template:
        left_idx += 1
        if symbol == '*':
            return(local_left_to_global(test, left_idx))
        try:
            if test[left_idx] != template[left_idx]:
                if template[left_idx] != '?':
                    return('NO')
                elif test[left_idx] not in good_symbols:
                    return('NO')
        except:
            return('NO')

def check_right(test, template, good_symbols):
    right_idx = 0
    for symbol in reversed(template):
        right_idx -= 1
        if symbol == '*':
            return(local_right_to_global(test, right_idx))
        try:
            if test[right_idx] != template[right_idx]:
                if template[right_idx] != '?':
                    return('NO')
                elif test[right_idx] not in good_symbols:
                    return('NO')            
        except:
            return('NO')
    
good_symbols = set(list(input()))
template = input()
tests_nr = int(input())
tests = []
for _ in range(tests_nr):
    tests.append(input())
    
abaca = '*' not in template
for test in tests:
    if abaca:
        pos = -1
        flag = True
        if len(test) != len(template):
            print('NO')
            continue
        for symbol in template:
            pos += 1
            try:
                if test[pos] != template[pos]:
                    if template[pos] != '?':
                        print('NO')
                        flag = False
                        break
                    elif test[pos] not in good_symbols:
                        print('NO')
                        flag = False
                        break
            except:
                print('NO')
                flag = False
                break
        if flag:
            print('YES')
        continue
    left_idx = check_left(test, template, good_symbols)
    right_idx = check_right(test, template, good_symbols)
    # print(left_idx, right_idx)
    if left_idx == 'NO':
        print('NO')
    elif right_idx == 'NO':
        print('NO')
    else:
        if right_idx < left_idx - 1:
            print('NO')
            continue
        flag = True
        for pos in range(left_idx, right_idx + 1):
            if test[pos] in good_symbols:
                print('NO')
                flag = False
                break
        if flag:
            print('YES')