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: MEDIUM_HARD
Tags: String algorithms

Implementation

Starter code:

Solutions:
def do(cur) -> str:
	if len(cur) < len(pt) - 1:
		return 'NO'
	i = j = 0
	while j < len(cur):
		if pt[i] == '?':
			if cur[j] not in good:
				return 'NO'
		elif pt[i] == '*':
			b = len(cur) - len(pt) + 1
			for x in cur[j:j + b]:
				if x in good:
					return 'NO'
			i += 1
			j += b
			continue
		elif pt[i] != cur[j]:
			return 'NO'
		i += 1
		j += 1
	return 'YES'
good = set(input())
pt = input()
pi = True if '*' not in pt else False
for _ in range(int(input())):
	cur = input()
	if pi and len(cur) != len(pt):
		print('NO')
	else:
		print(do(cur))


goodLet = input()
template = input()
isExist = template.count('*')
starIndex = template.find('*')
t1 = ''.join([template[j] if j < starIndex else '' for j in range(len(template))])
t2 = ''.join([template[j] if j > starIndex else '' for j in range(len(template))])
n = int(input())
for i in range(n):
	s = input()
	isChecked = True
	if not isExist:
		if len(s) != len(template):
			print('NO')
			continue
		for j in range(len(s)):
			if template[j] == '?':
				if s[j] not in goodLet:
					isChecked = False
					break
			elif template[j] != s[j]:
				isChecked = False
				break
	else:
		if len(s) < len(t1) + len(t2):
			print('NO')
			continue
		s1 = s[0:len(t1)]
		s2 = s[-len(t2):] if len(t2) > 0 else ''
		sbad = s[len(t1):-len(t2) if len(t2) > 0 else len(s)]
		for j in range(len(t1)):
			if t1[j] == '?':
				if s1[j] not in goodLet:
					isChecked = False
					break
			elif s1[j] != t1[j]:
				isChecked = False
				break
		for j in range(len(t2)):
			if t2[j] == '?':
				if s2[j] not in goodLet:
					isChecked = False
					break
			elif s2[j] != t2[j]:
				isChecked = False
				break
		for j in sbad:
			if j in goodLet:
				isChecked = False
				break
	if isChecked:
		print('YES')
	else:
		print('NO')


(good, p, n) = (input(), input(), int(input()))
plen = len(p)
if '*' not in p:
	pflag = True
else:
	pflag = False
for c in range(n):
	s = input()
	(slen, i, j, flag) = (len(s), 0, 0, True)
	if slen < plen:
		if pflag:
			flag = False
		elif slen < plen - 1:
			flag = False
	while i < plen and flag:
		if p[i] == '*':
			break
		elif p[i] == '?':
			if s[j] not in good:
				flag = False
		elif p[i] != s[j]:
			flag = False
		i += 1
		j += 1
	if i < plen and flag:
		sp = j
		(i, j) = (plen - 1, slen - 1)
		while i >= 0 and flag:
			if p[i] == '*':
				break
			elif p[i] == '?':
				if s[j] not in good:
					flag = False
			elif p[i] != s[j]:
				flag = False
			i -= 1
			j -= 1
		while j >= sp and flag:
			if s[j] in good:
				flag = False
			j -= 1
	elif j < slen:
		flag = False
	if flag:
		print('YES')
	else:
		print('NO')


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')


a = input()
b = input()
s = ''
n = int(input())
l = ''
r = ''
flag = 0

def match(x, y):
	if len(x) != len(y):
		return 0
	for i in range(len(x)):
		if x[i] == '?':
			if y[i] in a:
				continue
			else:
				return 0
		if x[i] != y[i]:
			return 0
	return 1

def allbad():
	for letter in s[len(l):len(s) - len(r)]:
		if letter in a:
			return 0
	return 1
for i in range(len(b)):
	if b[i] == '*':
		l = b[0:i]
		r = b[i + 1:]
		flag = 1
		break
for i in range(n):
	s = input()
	if not flag:
		if len(b) != len(s):
			print('NO\n')
		elif match(b, s):
			print('YES\n')
		else:
			print('NO\n')
	elif len(b) - 1 > len(s):
		print('NO\n')
	else:
		l2 = s[0:len(l)]
		r2 = s[len(s) - len(r):]
		if allbad() and match(l, l2) and match(r, r2):
			print('YES\n')
		else:
			print('NO\n')


g = set(input())
p = input()

def match(s, t):
	for i in range(len(s)):
		if t[i] == '?':
			if s[i] not in g:
				return False
		elif s[i] != t[i]:
			return False
	return True

def bad(s):
	for c in s:
		if c in g:
			return False
	return True
if '*' in p:
	(pA, pB) = p.split('*')
	n = int(input())
	l = len(pA) + len(pB)
	for _ in range(n):
		s = input()
		if len(s) < l:
			print('NO')
			continue
		(sA, sB, sC) = (s[:len(pA)], s[len(pA):len(s) - len(pB)], s[len(s) - len(pB):])
		if match(sA, pA) and match(sC, pB) and bad(sB):
			print('YES')
		else:
			print('NO')
else:
	n = int(input())
	for _ in range(n):
		s = input()
		if len(s) == len(p) and match(s, p):
			print('YES')
		else:
			print('NO')


import itertools
good_alphabet = input()
pattern = input()
n = int(input())
test_data = [input() for i in range(n)]
pattern_except_star = pattern.replace('*', '')
if '*' not in pattern:
	for data in test_data:
		flag = True
		if len(data) == len(pattern):
			for (i, j) in zip(data, pattern):
				if j != '?':
					if i != j:
						flag = False
						break
				elif i not in good_alphabet:
					flag = False
					break
		else:
			flag = False
		if flag:
			print('YES')
		else:
			print('NO')
elif len(pattern) == 1:
	for data in test_data:
		flag = True
		for i in data:
			if i in good_alphabet:
				flag = False
				break
		if flag:
			print('YES')
		else:
			print('NO')
elif pattern[0] == '*':
	good_part_len = len(pattern_except_star)
	for data in test_data:
		flag = True
		if len(data[-good_part_len:]) == len(pattern_except_star):
			for (i, j) in zip(data[-good_part_len:], pattern_except_star):
				if j != '?':
					if i != j:
						flag = False
						break
				elif i not in good_alphabet:
					flag = False
					break
			for i in data[:-good_part_len]:
				if i in good_alphabet:
					flag = False
		else:
			flag = False
		if flag:
			print('YES')
		else:
			print('NO')
elif pattern[-1] == '*':
	good_part_len = len(pattern_except_star)
	for data in test_data:
		flag = True
		if len(data[:good_part_len]) == len(pattern_except_star):
			for (i, j) in zip(data[:good_part_len], pattern_except_star):
				if j != '?':
					if i != j:
						flag = False
						break
				elif i not in good_alphabet:
					flag = False
					break
			for i in data[good_part_len:]:
				if i in good_alphabet:
					flag = False
		else:
			flag = False
		if flag:
			print('YES')
		else:
			print('NO')
else:
	star_index = pattern.find('*')
	last_part = len(pattern) - 1 - star_index
	for data in test_data:
		flag = True
		if len(data) < len(pattern_except_star):
			flag = False
		else:
			except_star = data[:star_index] + data[-last_part:]
			if len(except_star) == len(pattern_except_star):
				for (i, j) in zip(except_star, pattern_except_star):
					if j != '?':
						if i != j:
							flag = False
							break
					elif i not in good_alphabet:
						flag = False
						break
				for i in data[star_index:-last_part]:
					if i in good_alphabet:
						flag = False
						break
			else:
				flag = False
		if flag:
			print('YES')
		else:
			print('NO')


good = set(input())
pattern = input()
num = int(input())
pos = pattern.find('*')
rest = len(pattern) - pos - 1

def match(pat, s):
	if len(pat) != len(s):
		return False
	for i in range(len(pat)):
		if pat[i] != '?':
			if pat[i] != s[i]:
				return False
		elif s[i] not in good:
			return False
	return True
for _ in range(num):
	s = input()
	if pos == -1:
		print('YES') if match(pattern, s) else print('NO')
	else:
		if len(pattern) - 1 > len(s):
			print('NO')
			continue
		res = match(pattern[:pos], s[:pos])
		if not res:
			print('NO')
			continue
		if rest != 0:
			res = res and match(pattern[pos + 1:], s[-rest:])
			bad = s[pos:-rest]
		else:
			bad = s[pos:]
		if not res:
			print('NO')
			continue
		allbad = True
		for c in bad:
			if c in good:
				print('NO')
				allbad = False
				break
		if allbad:
			print('YES')


def check(pattern, string):
	if len(pattern) < len(string):
		return False
	for i in range(len(pattern)):
		if pattern[i] == '?':
			if string[i] not in good_chars:
				return False
		elif pattern[i] != string[i]:
			return False
	return True
alphabet = set('abcdefghijklmnopqrstuvxyz')
good_chars = set(input())
bad_chars = alphabet - good_chars
pattern = input().split('*')
n = int(input())
if len(pattern) == 1:
	for i in range(n):
		buf = input()
		if len(buf) < len(pattern[0]):
			print('NO')
		else:
			print('YES' if check(pattern[0], buf) else 'NO', end='' if i == n - 1 else '\n')
else:
	for i in range(n):
		buf = input()
		if len(buf) < len(pattern[0]) + len(pattern[1]):
			print('NO')
		else:
			res = check(pattern[0], buf[:len(pattern[0])])
			res &= check(pattern[1], buf[len(buf) - len(pattern[1]):])
			res &= len(set(buf[len(pattern[0]):len(buf) - len(pattern[1])]) & good_chars) == 0
			print('YES' if res else 'NO', end='' if i == n - 1 else '\n')


good_guy = input()
mask = input()
left_part = mask
right_part = ''
star_index = mask.find('*')
if star_index >= 0:
	left_part = mask[:star_index]
	right_part = mask[star_index + 1:]
n = int(input())
for i in range(n):
	string = input()
	is_ok = True
	left_str = string
	right_str = ''
	center_str = ''
	if star_index >= 0:
		left_str = string[:star_index]
		right_str = string[len(string) - len(right_part):]
		center_str = string[star_index:len(string) - len(right_part)]
	if mask == '*':
		for element in left_str:
			if element in good_guy:
				is_ok = False
		for element in center_str:
			if element in good_guy:
				is_ok = False
		for element in right_str:
			if element in good_guy:
				is_ok = False
	elif len(left_str) != len(left_part) or len(right_str) != len(right_part) or len(string) < len(left_str) + len(center_str) + len(right_str):
		is_ok = False
	else:
		for i in range(len(left_str)):
			if not (left_part[i] == '?' and left_str[i] in good_guy) and (not left_part[i] == left_str[i]):
				is_ok = False
		for i in range(len(right_str)):
			if not (right_part[len(right_part) - 1 - i] == '?' and right_str[len(right_str) - 1 - i] in good_guy) and (not right_part[len(right_part) - 1 - i] == right_str[len(right_str) - 1 - i]):
				is_ok = False
		for element in center_str:
			if element in good_guy:
				is_ok = False
	if is_ok:
		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')
	elif len(a) < len(pattern) - 1:
		print('NO')
	elif 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')


def without(text1, check, good):
	if len(text1) != len(check):
		return 'NO'
	for i in range(len(text1)):
		if text1[i] == '?':
			if check[i] not in good:
				return 'NO'
		elif text1[i] != check[i]:
			return 'NO'
	return 'YES'

def with1(text1, check, good, len_text, l):
	L = len(check)
	extra = len(check) - len_text + 1
	if extra < 0:
		return 'NO'
	for i in range(l):
		if text1[i] == '?':
			if check[i] not in good:
				return 'NO'
		elif text1[i] != check[i]:
			return 'NO'
	for i in range(l, l + extra):
		if check[i] in good:
			return 'NO'
	for i in range(l + extra, L):
		if text1[i - extra + 1] == '?':
			if check[i] not in good:
				return 'NO'
		elif text1[i - extra + 1] != check[i]:
			return 'NO'
	return 'YES'
good = str(input())
good = list(good)
text1 = str(input())
t = int(input())
l = -1
len_text = len(text1)
for i in range(len(text1)):
	if text1[i] == '*':
		l = i
		break
for k in range(t):
	check = str(input())
	if l == -1:
		print(without(text1, check, good))
	else:
		print(with1(text1, check, good, len_text, l))


goods = input()
query = input().split('*')
l = [len(s) for s in query]

def match(pat, s):
	if len(pat) != len(s):
		return False
	for (x, y) in zip(pat, s):
		if x == '?':
			if y not in goods:
				return False
		elif x != y:
			return False
	return True
for tc in range(int(input())):
	s = input()
	if len(query) == 1:
		left = match(query[0], s)
		mid = True
		right = True
	else:
		left = match(query[0], s[:l[0]])
		mid = len(s) >= sum(l) and all((c not in goods for c in s[l[0]:len(s) - l[1]]))
		right = match(query[1], s[len(s) - l[1]:])
	print('YES' if left and mid and right else 'NO')


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')


good = input()
s = input()
n = int(input())
query = [None] * n
for i in range(n):
	query[i] = input()
letters = [False] * 26
for i in good:
	letters[ord(i) - ord('a')] = True
len_s = len(s)
if '*' not in s:
	for i in query:
		if len(i) != len_s:
			print('NO')
		else:
			for j in range(len_s):
				if s[j] != '?':
					if s[j] != i[j]:
						print('NO')
						break
				elif not letters[ord(i[j]) - ord('a')]:
					print('NO')
					break
			else:
				print('YES')
else:
	pos_asterisk = s.index('*')
	for i in query:
		if len(i) < len_s - 1:
			print('NO')
		else:
			for j in range(pos_asterisk):
				if s[j] != '?':
					if s[j] != i[j]:
						print('NO')
						break
				elif not letters[ord(i[j]) - ord('a')]:
					print('NO')
					break
			else:
				odd = i[pos_asterisk:pos_asterisk + len(i) - len_s + 1]
				for j in odd:
					if letters[ord(j) - ord('a')]:
						print('NO')
						break
				else:
					for j in range(len_s - pos_asterisk - 1):
						if s[~j] != '?':
							if s[~j] != i[~j]:
								print('NO')
								break
						elif not letters[ord(i[~j]) - ord('a')]:
							print('NO')
							break
					else:
						print('YES')


from collections import defaultdict, deque, Counter
from sys import stdin, stdout
from heapq import heappush, heappop
import math
import io
import os
import math
import bisect

def isPrime(x):
	for i in range(2, x):
		if i * i > x:
			break
		if x % i == 0:
			return False
	return True

def ncr(n, r, p):
	num = den = 1
	for i in range(r):
		num = num * (n - i) % p
		den = den * (i + 1) % p
	return num * pow(den, p - 2, p) % p

def primeFactors(n):
	l = []
	while n % 2 == 0:
		l.append(2)
		n = n / 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			l.append(int(i))
			n = n / i
	if n > 2:
		l.append(n)
	return list(set(l))

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def sieve(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	return prime

def digits(n):
	c = 0
	while n > 0:
		n //= 10
		c += 1
	return c

def ceil(n, x):
	if n % x == 0:
		return n // x
	return n // x + 1

def mapin():
	return map(int, input().split())

def check2(a, b):
	for i in a:
		if b[ord(i) - 97] != 0:
			return 1
	return 0

def check(b, c, l):
	i = 0
	j = 0
	if len(b) != len(c):
		return 1
	while i < len(b) and j < len(c):
		if b[i] == c[j]:
			i += 1
			j += 1
		elif b[i] == '?':
			if l[ord(c[j]) - 97] == 1:
				i += 1
				j += 1
			else:
				return 1
		else:
			return 1
	return 0
a = input()
b = input()
n = int(input())
l = [0] * 26
for x in a:
	l[ord(x) - 97] = 1
for _ in range(n):
	err = 0
	c = input()
	if '*' not in b:
		err = check(b, c, l)
	else:
		tt = b.find('*')
		if len(c) < len(b) - 1:
			err = 1
		else:
			err = 0
			s1 = c[:tt]
			s2 = c[len(c) - (len(b) - tt - 1):len(c)]
			s3 = c[tt:len(c) - (len(b) - tt - 1)]
			s4 = b[:tt]
			s5 = b[tt + 1:]
			if check(s4, s1, l) == 0 and check(s5, s2, l) == 0 and (check2(s3, l) == 0):
				err = 0
			else:
				err = 1
	if err == 1:
		print('NO')
	else:
		print('YES')


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')
	elif 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')


l = list(input())
s = list(input())
n = int(input())
ans = []
les = len(s)
if not '*' in s:
	for _ in range(n):
		x = list(input())
		lex = len(x)
		if les != lex:
			ans += ['NO']
		else:
			for i in range(les):
				if s[i] == '?':
					if x[i] in l:
						continue
					else:
						ans += ['NO']
						break
				elif s[i] == x[i]:
					continue
				else:
					ans += ['NO']
					break
			else:
				ans += ['YES']
else:
	(s0, s1) = (s[:s.index('*')], s[s.index('*') + 1:])
	s1 = s1[::-1]
	(les0, les1) = (len(s0), len(s1))
	for _ in range(n):
		x = list(input())
		lex = len(x)
		if les0 + les1 > lex:
			ans += ['NO']
		else:
			for i in range(les0):
				if s0[i] == '?':
					if x[i] in l:
						continue
					else:
						ans += ['NO']
						break
				elif s0[i] == x[i]:
					continue
				else:
					ans += ['NO']
					break
			else:
				for i in range(les1):
					if s1[i] == '?':
						if x[-1 - i] in l:
							continue
						else:
							ans += ['NO']
							break
					elif s1[i] == x[-1 - i]:
						continue
					else:
						ans += ['NO']
						break
				else:
					for i in range(les0, lex - les1):
						if x[i] in l:
							ans += ['NO']
							break
					else:
						ans += ['YES']
print('\n'.join(ans))


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')


a = input()
b = input()
m = int(input())
for i in range(m):
	ans = 'YES'
	n = input()
	w = b
	if '*' in w:
		if len(w) <= len(n):
			w = w.replace('*', '*' * (abs(len(w) - len(n)) + 1))
		elif len(w) == len(n) + 1:
			w = w.replace('*', '')
	if len(n) != len(w):
		ans = 'NO'
	else:
		for i in range(len(n)):
			if w[i] == '*':
				if n[i] in a:
					ans = 'NO'
					break
			elif w[i] == '?':
				if n[i] not in a:
					ans = 'NO'
					break
			elif w[i] != n[i]:
				ans = 'NO'
				break
		else:
			ans = 'YES'
	print(ans)


def check(s1, s2):
	if len(s2) == 0:
		return True
	if len(s1) != len(s2):
		return False
	for i in range(len(s1)):
		if s1[i] != s2[i] and (s2[i] != '?' or s1[i] not in good):
			return False
	return True
good = input()
bad = ''
for i in range(ord('a'), ord('z') + 1):
	if chr(i) not in good:
		bad += chr(i)
s = input()
if '*' in s:
	s1 = s[:s.index('*')]
	s2 = s[s.index('*') + 1:]
else:
	s1 = s
	s2 = ''
ins = '*' in s
for i in range(int(input())):
	curr = input()
	if len(curr) + ins < len(s):
		print('NO')
	elif not check(curr[:len(s1)], s1) or not check(curr[-len(s2):], s2):
		print('NO')
	elif not ins:
		if len(curr) == len(s):
			print('yes')
		else:
			print('no')
	else:
		b = True
		ber = curr[len(s1):-len(s2)]
		if len(s2) == 0:
			ber = curr[len(s1):]
		for elem in ber:
			if elem in good:
				b = False
		if b:
			print('yes')
		else:
			print('no')


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):
	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')


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')


import sys
input = sys.stdin.readline
c = input().rstrip()
can = set(list(c))
cannot = set([chr(i) for i in range(ord('a'), ord('z') + 1)]) - can
pattern = input().rstrip()
tmp = '*' in pattern
q = int(input())
for _ in range(q):
	s = input().rstrip()
	ls = len(s)
	p = pattern
	l = len(p)
	if tmp:
		if l <= ls:
			p = p.replace('*', '*' * (ls - l + 1))
		elif l == ls + 1:
			p = p.replace('*', '')
	if len(p) != ls:
		print('NO')
		continue
	flag = True
	for i in range(ls):
		if p[i] == '?' and s[i] not in can:
			flag = False
		if p[i] == '*' and s[i] not in cannot:
			flag = False
		if p[i] != '?' and p[i] != '*' and (p[i] != s[i]):
			flag = False
	if flag:
		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)


s = input()
ls = [0 for i in range(26)]
for i in range(len(s)):
	ls[ord(s[i]) - 97] = 1
s = input()
n = int(input())
while n:
	q = input()
	if len(q) < len(s) - 1:
		print('NO')
	elif '*' not in s:
		if len(s) != len(q):
			print('NO')
		else:
			flag = True
			for j in range(len(s)):
				if s[j] != q[j]:
					if s[j] == '?':
						if ls[ord(q[j]) - 97] != 1:
							flag = False
							break
					else:
						flag = False
						break
			if flag:
				print('YES')
			else:
				print('NO')
	else:
		flag = True
		i = s.index('*')
		for j in range(i):
			if s[j] != q[j]:
				if s[j] == '?':
					if ls[ord(q[j]) - 97] != 1:
						flag = False
						break
				else:
					flag = False
					break
		for j in range(i, len(q) - (len(s) - i - 1)):
			if ls[ord(q[j]) - 97] != 0:
				flag = False
				break
		k = len(q) - (len(s) - i - 1)
		for j in range(i + 1, len(s)):
			if s[j] != q[k]:
				if s[j] == '?':
					if ls[ord(q[k]) - 97] != 1:
						flag = False
						break
				else:
					flag = False
					break
			k += 1
		if flag:
			print('YES')
		else:
			print('NO')
	n -= 1


def match(a, b):
	if a == '*':
		if len(b) == 0:
			return True
		for i in b:
			if i in good:
				return False
		return True
	if len(a) != len(b):
		return False
	for i in range(len(a)):
		if a[i] == '?' and b[i] not in good:
			return False
		if a[i] != '?' and a[i] != b[i]:
			return False
	return True
good = input()
pattern = input().split('*')
n = int(input())
for i in range(n):
	query = input()
	if len(pattern) == 1:
		if match(pattern[0], query):
			print('yes')
		else:
			print('no')
	else:
		l = len(pattern[0])
		r = len(pattern[1])
		n = len(query)
		if n < l + r:
			print('no')
		elif match(pattern[0], query[:l]) and match('*', query[l:n - r]) and match(pattern[1], query[n - r:]):
			print('yes')
		else:
			print('no')


import os
import sys
import math
from io import BytesIO, IOBase
from fractions import Fraction
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
s = input()
ls = [0 for i in range(26)]
for i in range(len(s)):
	ls[ord(s[i]) - 97] = 1
s = input()
n = int(input())
while n:
	q = input()
	if len(q) < len(s) - 1:
		print('NO')
	elif '*' not in s:
		if len(s) != len(q):
			print('NO')
		else:
			flag = True
			for j in range(len(s)):
				if s[j] != q[j]:
					if s[j] == '?':
						if ls[ord(q[j]) - 97] != 1:
							flag = False
							break
					else:
						flag = False
						break
			if flag:
				print('YES')
			else:
				print('NO')
	else:
		flag = True
		i = s.index('*')
		for j in range(i):
			if s[j] != q[j]:
				if s[j] == '?':
					if ls[ord(q[j]) - 97] != 1:
						flag = False
						break
				else:
					flag = False
					break
		for j in range(i, len(q) - (len(s) - i - 1)):
			if ls[ord(q[j]) - 97] != 0:
				flag = False
				break
		k = len(q) - (len(s) - i - 1)
		for j in range(i + 1, len(s)):
			if s[j] != q[k]:
				if s[j] == '?':
					if ls[ord(q[k]) - 97] != 1:
						flag = False
						break
				else:
					flag = False
					break
			k += 1
		if flag:
			print('YES')
		else:
			print('NO')
	n -= 1


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
			elif 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')


global good

def check(s, h):
	if len(s) != len(h):
		return 0
	for i in range(len(s)):
		if h[i] == '?':
			if s[i] in good:
				continue
			return 0
		if h[i] != s[i]:
			return 0
	return 1
good = input()
h = input()
n = int(input())
e = 0
(l, r) = ('', '')
for i in range(len(h)):
	if h[i] == '*':
		(l, r) = (h[:i], h[i + 1:])
		e = 1
		break
for i in range(n):
	s = input()
	if not e:
		if check(s, h):
			print('YES')
		else:
			print('NO')
	elif len(h) - 1 > len(s):
		print('NO')
	else:
		l1 = s[:len(l)]
		r1 = s[len(s) - len(r):]
		f = 1
		for x in s[len(l):len(s) - len(r)]:
			if x in good:
				f = 0
				break
		if check(l1, l) and check(r1, r) and f:
			print('YES')
		else:
			print('NO')


A = set(input())
B2 = input()
N = int(input())
for i in range(N):
	B = B2
	S = input()
	if len(B) > len(S) + 1:
		print('NO')
		continue
	flag = True
	try:
		if '*' in B:
			for n in range(2):
				for j in range(len(B)):
					if B[j] == '*':
						B = B[j:]
						S = S[j:]
						break
					elif B[j] == '?':
						if S[j] not in A:
							flag = False
							break
					elif B[j] != S[j]:
						flag = False
						break
				if flag == False:
					break
				S = S[::-1]
				B = B[::-1]
			for s in S:
				if s in A:
					flag = False
					break
		elif len(B) != len(S):
			flag = False
		else:
			for j in range(len(B)):
				if B[j] == '?':
					if S[j] not in A:
						flag = False
						break
				elif B[j] != S[j]:
					flag = False
					break
		if flag:
			print('YES')
		else:
			print('NO')
	except:
		print('NO')


I = input
(good, pattern) = (set(I()), I())
(L, pos) = (len(pattern), pattern.find('*'))
(n, m) = (L, 0) if pos < 0 else (pos, L - pos - 1)
J1 = {i for i in range(n) if pattern[i] == '?'}
J2 = {i for i in range(-m, 0)[::-1] if pattern[i] == '?'}
for _ in range(int(I())):
	s = I()
	L1 = len(s)
	if (pos < 0 and L1 == L or (pos >= 0 and L1 > L - 2)) and all((i in J1 and s[i] in good or (i not in J1 and s[i] == pattern[i]) for i in range(n))) and all((i in J2 and s[i] in good or (i not in J2 and s[i] == pattern[i]) for i in range(-m, 0)[::-1])) and (not set(s[n:-m] if m else s[n:]) & good):
		print('YES')
	else:
		print('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')


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')


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))


s = input()
t = input()
n = int(input())
for i in range(n):
	r = input()
	(q, z, x) = (True, 0, 0)
	while x < len(t) and z < len(r):
		if t[x] == '*':
			k = False
			z -= 1
			for j in range(len(r) - len(t) + 1):
				z += 1
				if r[z] in s:
					k = True
					break
			if k:
				q = False
				print('NO')
				break
		elif r[z] != t[x]:
			if t[x] != '?' or r[z] not in s:
				q = False
				print('NO')
				break
		x += 1
		z += 1
	if x < len(t):
		if t[x] == '*':
			x += 1
	if q:
		if x < len(t) or z < len(r):
			q = False
			print('NO')
	if q:
		print('YES')


from itertools import zip_longest

def is_in(symbol, ord___is_good):
	return ord___is_good[ord(symbol) - ord('a')]

def good_symbols___bool_good_symbols(good_symbols):
	ord___is_good = [False] * 30
	for symbol in good_symbols:
		ord___is_good[ord(symbol) - ord('a')] = True
	return ord___is_good

def is_match(test_symbol, template_symbol, good_symbols):
	if template_symbol == '?':
		return is_in(test_symbol, good_symbols)
	else:
		return test_symbol == template_symbol

def solve_no_star(test, template, good_symbols):
	if len(test) != len(template):
		return 'NO'
	for (idx, test_sym) in enumerate(test):
		template_sym = template[idx]
		if not is_match(test_sym, template_sym, good_symbols):
			return 'NO'
	return 'YES'

def find_left_closed(test, template, good_symbols):
	for (idx, (template_sym, test_sym)) in enumerate(zip_longest(template, test)):
		if not test_sym:
			if template_sym == '*':
				return idx
			return 100000000
		if template_sym == '*':
			return idx
		if not is_match(test_sym, template_sym, good_symbols):
			return 100000000
	return 100000000

def find_right_opened(test, template, good_symbols):
	ans = find_left_closed(reversed(test), reversed(template), good_symbols)
	return len(test) - ans

def solve_star(test, template, good_symbols):
	left_cl = find_left_closed(test, template, good_symbols)
	right_op = find_right_opened(test, template, good_symbols)
	if right_op < left_cl:
		return 'NO'
	for test_sym in test[left_cl:right_op]:
		if is_in(test_sym, good_symbols):
			return 'NO'
	return 'YES'
good_symbols = good_symbols___bool_good_symbols(list(input()))
template = input()
tests_nr = int(input())
template_has_star = '*' in template
for _ in range(tests_nr):
	test = input()
	if template_has_star:
		print(solve_star(test, template, good_symbols))
	else:
		print(solve_no_star(test, template, good_symbols))


goodLetters = input()
template = input()
arr = []
for i in range(int(input())):
	arr.append(input())
for q in arr:
	if len(template) - 1 > len(q):
		print('NO')
		continue
	elif '*' not in template and len(template) < len(q):
		print('NO')
		continue
	elif '*' not in template and len(template) > len(q):
		print('NO')
		continue
	flag = True
	index = 0
	for letter in template:
		if letter == '?':
			if q[index] not in goodLetters:
				flag = False
		elif letter == '*':
			t = len(q) - len(template) + 1
			if t == 0:
				continue
			temp = q[index:index + t]
			for i in temp:
				if i in goodLetters:
					flag = False
					break
			index += len(temp)
			continue
		elif letter != q[index]:
			flag = False
		if not flag:
			break
		index += 1
	if flag:
		print('YES')
	else:
		print('NO')


alphabet = 'abcdefghijklmnopqrstuvwxyz'
good = input()
bad = ''
for i in alphabet:
	if i not in good:
		bad += i
template = input()
n = int(input())

def match(string, template):
	if len(string) + 1 < len(template):
		return False
	if '*' in template:
		i = 0
		pos = template.find('*')
		if len(template) - 1 > len(string):
			return False
		for letter in template[:pos]:
			i += 1
			if letter != '?' and letter != string[i - 1]:
				return False
			if letter == '?' and string[i - 1] in bad:
				return False
		j = len(string)
		for letter in template[:pos:-1]:
			j -= 1
			if letter != '?' and letter != string[j]:
				return False
			if letter == '?' and string[j] in bad:
				return False
		substring = string[pos:j]
		for letter in substring:
			if letter in good:
				return False
	else:
		if len(string) != len(template):
			return False
		i = 0
		for letter in string:
			i += 1
			if template[i - 1] != '?' and template[i - 1] != letter:
				return False
	return True
for number in range(n):
	string = input()
	if match(string, template):
		print('YES')
	else:
		print('NO')


from sys import stdin

def fun(s1, s2, s3):
	LEN_S2 = len(s2)
	LEN_S3 = len(s3)
	if LEN_S2 == LEN_S3:
		for i in range(LEN_S3):
			s2_temp = s2[i]
			s3_temp = s3[i]
			if s2_temp != '?' and s2_temp != '*':
				if s2_temp == s3_temp:
					continue
				else:
					return 'NO'
			elif s2_temp == '?':
				if s3_temp in s1:
					continue
				else:
					return 'NO'
			elif s2_temp == '*':
				if s3_temp in s1:
					return 'NO'
				else:
					continue
	elif LEN_S3 == LEN_S2 - 1:
		index = s2.find('*')
		if index != -1:
			ss2_temp = s2.replace('*', '')
			if len(ss2_temp) == LEN_S3:
				for i in range(LEN_S3):
					s2_temp = ss2_temp[i]
					s3_temp = s3[i]
					if s2_temp != '?':
						if s2_temp == s3_temp:
							continue
						else:
							return 'NO'
					elif s2_temp == '?':
						if s3_temp in s1:
							continue
						else:
							return 'NO'
			else:
				return 'NO'
		else:
			return 'NO'
	elif LEN_S3 > LEN_S2:
		index = s2.find('*')
		if index != -1:
			n = LEN_S3 - LEN_S2 + 1
			n2 = '*' * n
			ss2_temp = s2.replace('*', n2)
			for i in range(LEN_S3):
				s2_temp = ss2_temp[i]
				s3_temp = s3[i]
				if s2_temp != '?' and s2_temp != '*':
					if s2_temp == s3_temp:
						continue
					else:
						return 'NO'
				elif s2_temp == '?':
					if s3_temp in s1:
						continue
					else:
						return 'NO'
				elif s2_temp == '*':
					if s3_temp in s1:
						return 'NO'
					else:
						continue
				else:
					return 'NO'
		else:
			return 'NO'
	else:
		return 'NO'
	return 'YES'
ss1 = stdin.readline().strip()
ss2 = stdin.readline().strip()
n = int(input().strip())
result = []
for i in range(n):
	ss3 = stdin.readline().strip()
	t = fun(ss1, ss2, ss3)
	result.append(t)
for i in range(n):
	print(result[i])


hor = input()
mask = input()
lenmask = len(mask)
for i in range(int(input())):
	a = input()
	lena = len(a)
	f = True
	if lena < lenmask - 1 and '*' in mask:
		print('NO')
		continue
	elif lena < lenmask and '*' not in mask:
		print('NO')
		continue
	elif lena > lenmask and '*' not in mask:
		print('NO')
		continue
	for j in range(lenmask):
		if mask[j] == '?':
			if a[j] not in hor:
				f = False
				break
		elif mask[j] == '*':
			if lenmask - 1 == lena:
				a = a[:j] + 'j' + a[j:]
			else:
				srez = a[j:j + lena - lenmask + 1]
				for el in srez:
					if el in hor:
						f = False
						break
				if not f:
					break
				a = a[:j] + a[j + lena - lenmask:]
		elif a[j] != mask[j]:
			f = False
			break
	if f:
		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')


s = input()
a = {}
for i in 'abcdefghijklmnopqrstuvwxyz':
	a[i] = 0
for i in s:
	a[i] = 1
s = input()
b = s
for i in range(int(input())):
	k = input()
	f = 1
	while s[0] == k[0] or (s[0] == '?' and a[k[0]] == 1):
		s = s[1:]
		k = k[1:]
		if s == '' or k == '':
			break
	if s != '' and k != '':
		while s[-1] == k[-1] or (s[-1] == '?' and a[k[-1]] == 1):
			s = s[:-1]
			k = k[:-1]
			if s == '' or k == '':
				break
	if s == '*':
		for j in k:
			if a[j] == 1:
				f = 0
	elif s == k:
		f = 1
	else:
		f = 0
	if f == 1:
		print('YES')
	else:
		print('NO')
	s = b


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)
	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')


def is_good(string, template):
	global good_string
	if len(string) == len(template):
		for j in range(len(string)):
			if template[j] == '?':
				if string[j] not in good:
					good_string = False
					break
			elif template[j] != string[j]:
				good_string = False
				break
	else:
		good_string = False
alphabet = ['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']
good = input()
bad = [x for x in alphabet if x not in good]
template = input()
n = int(input())
b = False
for i in range(len(template)):
	if template[i] == '*':
		b = True
		break
pos = i
for i in range(n):
	string = input()
	good_string = True
	if not b:
		is_good(string, template)
	else:
		(template_1, template_2) = (template[:pos], template[pos + 1:])
		if len(string) >= len(template) - 1:
			string_1 = string[:pos]
			string_2 = string[pos:len(string) - len(template_2)]
			string_3 = string[len(string) - len(template_2):]
			is_good(string_1, template_1)
			is_good(string_3, template_2)
			for el in string_2:
				if el not in bad:
					good_string = False
					break
		else:
			good_string = False
	if good_string:
		print('YES')
	else:
		print('NO')


bukvi = input()
question = input()
arr = []
for i in range(int(input())):
	arr.append(input())
for k in arr:
	if len(question) - 1 > len(k):
		print('NO')
		continue
	elif '*' not in question and len(question) < len(k):
		print('NO')
		continue
	elif '*' not in question and len(question) > len(k):
		print('NO')
		continue
	bool = True
	index = 0
	for letter in question:
		if letter == '?':
			if k[index] not in bukvi:
				bool = False
		elif letter == '*':
			t = len(k) - len(question) + 1
			if t == 0:
				continue
			temp = k[index:index + t]
			for i in temp:
				if i in bukvi:
					bool = False
					break
			index += len(temp)
			continue
		elif letter != k[index]:
			bool = False
		if not bool:
			break
		index += 1
	if bool > 0:
		print('YES')
	else:
		print('NO')


z = input
g = z()
p = z()
n = int(z())

def m(p, t):
	d = len(t) - len(p)
	if d < -1:
		return 0
	p = p.replace('*', '#' * (d + 1))
	if len(p) != len(t):
		return 0
	for i in range(len(p)):
		if p[i] == '?':
			if t[i] not in g:
				return 0
		elif p[i] == '#':
			if t[i] in g:
				return 0
		elif t[i] != p[i]:
			return 0
	return 1
for i in range(n):
	t = z()
	print('YES' if m(p, t) else 'NO')


from sys import stdin
from collections import defaultdict as dd
good = dd(lambda : 0)
st = input()
for ch in st:
	good[ch] = 1
s = input()
star = int(s.find('*') != -1)
loc = s.find('*')
n = int(stdin.readline())
for i in range(n):
	st = input()
	if len(st) < len(s) - star:
		print('No')
	else:
		flg = True
		l = len(st)
		if star:
			i = 0
			j = l - 1
			for idx in range(loc):
				if s[idx] == '?':
					if not good[st[i]]:
						flg = False
				elif s[idx] != st[i]:
					flg = False
				i += 1
			for idx in range(len(s) - 1, loc, -1):
				if s[idx] == '?':
					if not good[st[j]]:
						flg = False
				elif s[idx] != st[j]:
					flg = False
				j -= 1
			for idx in range(i, j + 1):
				if good[st[idx]]:
					flg = False
		else:
			if len(st) != len(s):
				flg = False
			for i in range(len(s)):
				if s[i] == '?':
					if not good[st[i]]:
						flg = False
				elif s[i] != st[i]:
					flg = False
		if flg:
			print('YES')
		else:
			print('NO')


import pdb
good_letters = set(input())
pattern = input()
n = int(input())

def ismatch(pattern, query):
	if len(pattern) != len(query):
		return False
	zipped = zip(pattern, query)
	for (a, b) in zipped:
		if a.isalpha():
			if b != a:
				return False
		elif a == '?':
			if b not in good_letters:
				return False
	return True
if pattern.find('*') == -1:
	for i in range(n):
		if ismatch(pattern, input()):
			print('YES')
		else:
			print('NO')
else:
	(p1, sep, p2) = pattern.partition('*')
	for i in range(n):
		query = input()
		if len(query) < len(pattern) - 1:
			print('NO')
		else:
			s1 = query[:len(p1)]
			if len(p2) > 0:
				s2 = query[-len(p2):]
				mid = set(query[len(p1):-len(p2)])
			else:
				s2 = ''
				mid = set(query[len(p1):])
			if ismatch(p1, s1) and ismatch(p2, s2) and mid.isdisjoint(good_letters):
				print('YES')
			else:
				print('NO')


good = set(input())
ex = input()
n = int(input())

def bad(s):
	for c in s:
		if c in good:
			return False
	return True
if ex == '*':
	for _ in range(n):
		if bad(input()):
			print('YES')
		else:
			print('NO')
else:
	before = 0
	after = 0

	def process(exp):
		global before, after
		out = exp.split('*')
		if len(out) == 2:
			before = len(out[0])
			after = len(out[1])
		return out
	pex = process(ex)

	def bad(s):
		for c in s:
			if c in good:
				return False
		return True

	def pureCompare(e, s):
		if len(e) != len(s):
			return False
		for i in range(len(e)):
			if e[i] == '?':
				if not s[i] in good:
					return False
			elif e[i] != s[i]:
				return False
		return True

	def compare(e, s):
		if len(pex) == 2:
			s1 = s[:before]
			s2 = s[len(s) - after:]
			s3 = s[before:len(s) - after]
			return pureCompare(e[0], s1) and pureCompare(e[1], s2) and bad(s3)
		else:
			return pureCompare(pex[0], s)
	for _ in range(n):
		st = input()
		if len(st) >= before + after:
			if compare(pex, st):
				print('YES')
			else:
				print('NO')
		else:
			print('NO')


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
			elif nl[i] != c[i]:
				a.append('NO')
				break
		else:
			a.append('YES')
for i in a:
	print(i)


good = input()
s = input()
le = len(s)
n = int(input())
star = False
pos = s.find('*')
if pos != -1:
	star = True

def check(string, test):
	for i in range(len(string)):
		if string[i] == '?':
			if test[i] not in good:
				return False
		elif string[i] != test[i]:
			return False
	return True
for i in range(n):
	t = input()
	tle = len(t)
	ans = True
	if not star:
		if tle != le:
			ans = False
		else:
			ans = check(s, t)
	elif tle >= le - 1:
		ans = check(s[:pos], t[:pos])
		if ans and check(s[pos + 1:], t[tle - (le - pos - 1):]):
			ans = True
		else:
			ans = False
		if ans:
			for j in range(pos, tle - (le - pos - 1)):
				if t[j] in good:
					ans = False
	else:
		ans = False
	if ans:
		print('YES')
	else:
		print('NO')


def compare(good_letters, template, string):
	x = 0
	while x < len(template) and template[x] != '*':
		if x > len(string) - 1:
			return 'NO'
		if template[x] != string[x]:
			if template[x] == '?':
				if string[x] not in good_letters:
					return 'NO'
			else:
				return 'NO'
		x += 1
	if str(template).__contains__('*'):
		star = x
		x = len(template) - 1
		y = len(string) - 1
		while x > star:
			if y < star:
				return 'NO'
			if template[x] != string[y]:
				if template[x] == '?':
					if string[y] not in good_letters:
						return 'NO'
				else:
					return 'NO'
			x -= 1
			y -= 1
		for k in range(star, y + 1):
			if string[k] in good_letters:
				return 'NO'
	elif len(string) != len(template):
		return 'NO'
	return 'YES'
g = input()
t = input()
numbers = int(input())
for i in range(0, numbers):
	s = input()
	print(compare(g, t, s))


import os
import sys
from io import BytesIO, IOBase
import math

def main():
	pass
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def binary(n):
	return bin(n).replace('0b', '')

def decimal(s):
	return int(s, 2)

def pow2(n):
	p = 0
	while n > 1:
		n //= 2
		p += 1
	return p

def primeFactors(n):
	l = []
	while n % 2 == 0:
		l.append(2)
		n = n / 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			l.append(i)
			n = n / i
	if n > 2:
		l.append(int(n))
	return l

def isPrime(n):
	if n == 1:
		return False
	else:
		root = int(n ** 0.5)
		root += 1
		for i in range(2, root):
			if n % i == 0:
				return False
		return True

def maxPrimeFactors(n):
	maxPrime = -1
	while n % 2 == 0:
		maxPrime = 2
		n >>= 1
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			maxPrime = i
			n = n / i
	if n > 2:
		maxPrime = n
	return int(maxPrime)

def chk(s, s2):
	f = True
	for i in range(0, len(s)):
		if s[i] == '?':
			if s2[i] not in g:
				f = False
				break
		elif s[i] != s2[i]:
			f = False
			break
	return f
g = input()
s = input()
for xyz in range(0, int(input())):
	s2 = input()
	if '*' not in s:
		if len(s) == len(s2):
			if chk(s, s2):
				print('YES')
			else:
				print('NO')
		else:
			print('NO')
	elif len(s) - 1 > len(s2):
		print('NO')
	else:
		ps = s.index('*')
		lp1 = ps
		lp2 = len(s) - 1 - ps
		if chk(s[:ps], s2[:ps]) and chk(s[ps + 1:], s2[len(s2) - lp2:]):
			lb = ps
			up = len(s2) - lp2
			f = True
			for i in range(lb, up):
				if s2[i] in g:
					f = False
					break
			if f:
				print('YES')
			else:
				print('NO')
		else:
			print('NO')


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_letters_str = input()
good_letters = set(good_letters_str)
template = input()
n = int(input())

def simple_template(t, s):
	if len(t) != len(s):
		return 'NO'
	i = 0
	j = 0
	s_n = ''
	t_n = ''
	while t[i:].count('?') > 0:
		(i, j) = (i + t[i:].index('?'), i)
		k = 0
		while i + k + 1 < len(t) and t[i + k + 1] == '?':
			k += 1
		if len(set(s[i:i + 1 + k]) - good_letters) > 0 or s[j:i] != t[j:i]:
			return 'NO'
		i += 1 + k
	if t[i:] != s[i:]:
		return 'NO'
	return 'YES'
if '*' in template:
	(t1, t2) = template.split('*')
	for _ in range(n):
		s = input()
		if len(t1) + len(t2) > len(s):
			print('NO')
			continue
		elif len(set(s[len(t1):len(s) - len(t2)]) & good_letters) >= 1:
			print('NO')
			continue
		else:
			print(simple_template(t1 + t2, s[:len(t1)] + s[len(s) - len(t2):]))
else:
	for _ in range(n):
		print(simple_template(template, input()))


good = list(input())
list_good = [0 for i in range(26)]
for i in good:
	list_good[ord(i) - 97] = 1
st = list(input())
for q in range(int(input())):
	check = list(input())
	i = 0
	b = 0
	j = 0
	while i < len(st) and j < len(check):
		if st[i] != '*' and st[i] != '?':
			if st[i] == check[j]:
				i += 1
				j += 1
			else:
				b = 1
				break
		elif st[i] == '?':
			if list_good[ord(check[j]) - 97] == 1:
				i += 1
				j = j + 1
			else:
				b = 1
				break
		else:
			x = len(st) - i
			y = len(check) - j
			y = y - x + 1
			for o in range(j, j + y):
				if list_good[ord(check[o]) - 97] != 0:
					b = 1
					break
			if b == 0 and y >= 0:
				i = i + 1
				j = j + y
			else:
				b = 1
				break
	if b == 1:
		print('NO')
	elif i == len(st) and j == len(check):
		print('YES')
	elif i == len(st) - 1 and st[i] == '*' and (j == len(check)):
		print('YES')
	else:
		print('NO')


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')


def check(pattern, query, good):
	if len(pattern) != len(query):
		return False
	else:
		b = True
		for k in range(len(pattern)):
			if pattern[k] != '?':
				if pattern[k] != query[k]:
					b = False
			elif query[k] not in good:
				b = False
		return b
good = list(input())
pattern = input()
nPattern = len(pattern)
if '*' not in pattern:
	index = '*'
	pattern_1 = pattern
	pattern_2 = pattern
else:
	index = list(pattern).index('*')
	pattern_1 = pattern[0:index]
	pattern_2 = pattern[index + 1:]
n = int(input())
for i in range(n):
	query = input()
	if index == '*':
		bool = check(pattern, query, good)
	else:
		query_1 = query[0:index]
		query_2 = query[index - nPattern + len(query) + 1:]
		boolk = True
		q = list(query[index:index - nPattern + len(query) + 1])
		if q != []:
			for k in q:
				if k in good:
					boolk = False
		if len(query_1) + len(query_2) > len(query):
			bool = False
		else:
			bool = check(pattern_1, query_1, good) and check(pattern_2, query_2, good) and boolk
	if bool:
		print('YES')
	else:
		print('NO')


def main():
	goodletter = input()
	isgood = {}
	for i in range(0, len(goodletter)):
		isgood[goodletter[i]] = 1
	sstr = input()
	n = int(input())
	starpos = sstr.find('*')
	for i in range(0, n):
		gstr = input()
		if len(gstr) < len(sstr) - 1:
			print('NO')
			continue
		elif starpos == -1 and len(gstr) == len(sstr) - 1:
			print('NO')
			continue
		elif starpos == -1 and len(gstr) > len(sstr):
			print('NO')
			continue
		(j, k) = (0, 0)
		while j < len(gstr):
			if gstr[j] == sstr[k]:
				j += 1
				k += 1
				continue
			elif sstr[k] == '?' and gstr[j] in isgood:
				j += 1
				k += 1
				continue
			elif sstr[k] == '*':
				k += 1
				if len(gstr) < len(sstr):
					continue
				else:
					p = j
					while j < p + len(gstr) - len(sstr) + 1:
						if gstr[j] in isgood:
							print('NO')
							break
						j += 1
					if j != p + len(gstr) - len(sstr) + 1:
						break
			else:
				print('NO')
				break
		else:
			print('YES')
main()


a = [0] * 26
s = str(input())
for c in s:
	a[ord(c) - 97] = 1
t = str(input())
n = len(t)
idx = n
for (j, c) in enumerate(t):
	if c == '*':
		idx = j
		break
q = int(input())
for i in range(q):
	p = str(input())
	m = len(p)
	(l, r) = (0, 0)
	possible = True
	if idx == n:
		if n != m:
			possible = False
		else:
			for j in range(n):
				if t[j] == '?':
					if not a[ord(p[j]) - 97]:
						possible = False
						break
				elif t[j] != p[j]:
					possible = False
					break
		if possible:
			print('YES')
		else:
			print('NO')
	else:
		while t[l] != '*':
			if l == m:
				possible = False
				break
			if t[l] == '?':
				if not a[ord(p[l]) - 97]:
					possible = False
					break
			elif t[l] != p[l]:
				possible = False
				break
			l += 1
		while t[n - r - 1] != '*':
			if r == m:
				possible = False
				break
			if t[n - r - 1] == '?':
				if not a[ord(p[m - r - 1]) - 97]:
					possible = False
					break
			elif t[n - r - 1] != p[m - r - 1]:
				possible = False
				break
			r += 1
		for j in range(l, l + m - r - l):
			if a[ord(p[j]) - 97]:
				possible = False
				break
		if l + r > m:
			possible = False
		if possible:
			print('YES')
		else:
			print('NO')


p = q = []
g = input()
a = input()
for _ in range(int(input())):
	ans = 1
	x = input()
	if len(x) < len(a) - 1 or ('*' not in a and len(x) != len(a)):
		ans = 0
	if ans:
		i = 0
		while i < min(len(a), len(x)) and a[i] != '*':
			if a[i] == '?':
				if x[i] not in g:
					ans = 0
					break
			elif a[i] != x[i]:
				ans = 0
				break
			i += 1
		j = i
	if ans and i < len(a):
		i = len(a) - 1
		z = len(x) - 1
		while a[i] != '*':
			if a[i] == '?':
				if x[z] not in g:
					ans = 0
					break
			elif a[i] != x[z]:
				ans = 0
				break
			i -= 1
			z -= 1
		k = z + 1
		if ans:
			for i in range(j, k):
				if x[i] in g:
					ans = 0
					break
	print(['NO', 'YES'][ans])


from sys import stdin, stdout

def main():
	good = stdin.readline().strip()
	isGood = dict()
	for c in good:
		isGood[c] = True
	p = stdin.readline().strip()
	p = list(p)
	n = int(stdin.readline().strip())
	while n > 0:
		q = stdin.readline().strip()
		q = list(q)
		if len(p) > len(q) + 1:
			stdout.write('NO\n')
			n -= 1
			continue
		ans = 'YES'
		t = p.copy()
		if '*' not in t:
			if len(t) != len(q):
				ans = 'NO'
			else:
				for (i, c) in enumerate(t):
					if c != '?' and c != q[i] or (c == '?' and q[i] not in isGood):
						ans = 'NO'
						break
		else:
			idx = t.index('*')
			for i in range(0, idx):
				if t[i] == '?' and q[i] not in isGood:
					ans = 'NO'
					break
				if t[i] != '?' and t[i] != q[i]:
					ans = 'NO'
					break
			if ans == 'YES':
				suffixLen = len(t) - idx - 1
				suffixStart = len(q) - suffixLen
				j = suffixStart
				for i in range(idx + 1, len(t)):
					if t[i] == '?' and q[j] not in isGood:
						ans = 'NO'
						break
					if t[i] != '?' and t[i] != q[j]:
						ans = 'NO'
						break
					j += 1
			if ans == 'YES':
				for i in range(idx, suffixStart):
					if q[i] in isGood:
						ans = 'NO'
						break
		stdout.write(ans + '\n')
		n -= 1
main()


alpha = 'abcdefghijklmnopqrstuvwxyz'
good = [i for i in input()]
bad = [' ']
for i in alpha:
	if i not in good:
		bad.append(i)
s = [i for i in input()]
k = int(input())
n = len(s)
for i in range(k):
	ok = False
	s1 = [i for i in input()]
	if len(s1) == n - 1 and '*' in s:
		ind = 0
		s2 = []
		for i in range(n):
			if s[i] == '*':
				s2.append('')
			else:
				s2.append(s1[ind])
				ind += 1
		s1 = s2
	(i, j) = (0, 0)
	while i < n and j < len(s1):
		if s[i] == s1[j]:
			i += 1
			j += 1
			continue
		elif s[i] == '?':
			if s1[j] in bad:
				ok = 'NO'
				break
		elif s[i] == '*':
			if s1[j] in good:
				ok = 'NO'
				break
			for k in range(j, j + abs(n - len(s1)) + 1):
				if k == len(s1):
					break
				elif s1[k] in bad:
					continue
				elif s1[k] in good:
					ok = 'NO'
					break
			j += abs(n - len(s1))
		elif s[i] != s1[j]:
			ok = 'NO'
			break
		i += 1
		j += 1
	if ok == 'NO':
		print('NO')
	elif i == n and j == len(s1):
		print('YES')
	else:
		print('NO')


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()


from itertools import zip_longest

def is_in(symbol, good_symbols):
	return symbol in good_symbols

def is_match(test_symbol, template_symbol, good_symbols):
	if template_symbol == '?':
		return is_in(test_symbol, good_symbols)
	else:
		return test_symbol == template_symbol

def solve_no_star(test, template, good_symbols):
	if len(test) != len(template):
		return 'NO'
	for (idx, test_sym) in enumerate(test):
		template_sym = template[idx]
		if not is_match(test_sym, template_sym, good_symbols):
			return 'NO'
	return 'YES'

def find_left_closed(test, template, good_symbols):
	for (idx, (template_sym, test_sym)) in enumerate(zip_longest(template, test)):
		if not test_sym:
			if template_sym == '*':
				return idx
			return 100000000
		if template_sym == '*':
			return idx
		if not is_match(test_sym, template_sym, good_symbols):
			return 100000000
	return 100000000

def find_right_opened(test, template, good_symbols):
	ans = find_left_closed(reversed(test), reversed(template), good_symbols)
	return len(test) - ans

def solve_star(test, template, good_symbols):
	left_cl = find_left_closed(test, template, good_symbols)
	right_op = find_right_opened(test, template, good_symbols)
	if right_op < left_cl:
		return 'NO'
	for test_sym in test[left_cl:right_op]:
		if is_in(test_sym, good_symbols):
			return 'NO'
	return 'YES'
good_symbols = set(list(input()))
template = input()
tests_nr = int(input())
template_has_star = '*' in template
for _ in range(tests_nr):
	test = input()
	if template_has_star:
		print(solve_star(test, template, good_symbols))
	else:
		print(solve_no_star(test, template, good_symbols))


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
			elif 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
		elif 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)


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
					elif 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
					elif 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
						elif 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')


valid_c = {x: True for x in input()}
s = input()
w = s.find('*')
if w == -1:
	left_size = len(s)
	right_size = 0
else:
	left_size = w
	right_size = len(s) - w - 1
n = int(input())
for i in range(n):
	q = input()
	if w != -1:
		w_size = max(0, len(q) - left_size - right_size)
	else:
		w_size = 0
	valid = True
	if len(q) < w_size + left_size + right_size:
		valid = False
	index_s = 0
	for index in range(len(q)):
		if index_s >= len(s):
			valid = False
			break
		if index < left_size or index >= left_size + w_size:
			if s[index_s] == '*':
				index_s += 1
			if s[index_s] == '?':
				valid = q[index] in valid_c and valid
			else:
				valid = q[index] == s[index_s] and valid
			index_s += 1
		else:
			valid = q[index] not in valid_c and valid
	print('YES' if valid else 'NO')


def quesMatch(s, pattern, goodchars):
	if len(s) != len(pattern):
		return False
	for (i, c) in enumerate(pattern):
		if c == '?' and s[i] not in goodchars:
			return False
		elif c != '?' and s[i] != c:
			return False
	return True
goodchars = set(list(input().strip()))
pattern = input().strip()
queryNum = int(input().strip())
starIdx = pattern.find('*')
subLen1 = subLen2 = 0
for _ in range(queryNum):
	s = input().strip()
	if starIdx == -1:
		if quesMatch(s, pattern, goodchars):
			print('YES')
		else:
			print('NO')
	else:
		if len(s) < len(pattern) - 1:
			print('NO')
			continue
		subLen1 = starIdx
		subLen2 = len(pattern) - starIdx - 1
		starStr = s
		if subLen1:
			if not quesMatch(s[:subLen1], pattern[:subLen1], goodchars):
				print('NO')
				continue
			starStr = starStr[subLen1:]
		if subLen2:
			if not quesMatch(s[-subLen2:], pattern[-subLen2:], goodchars):
				print('NO')
				continue
			starStr = starStr[:-subLen2]
		if len(starStr) == 0 or len(set(list(starStr)) & goodchars) == 0:
			print('YES')
		else:
			print('NO')


from sys import stdin, stdout
import string
good = set(stdin.readline().rstrip())
bad = set([letter for letter in string.ascii_lowercase if letter not in good])
pattern = list(stdin.readline().rstrip())
found = '*' in pattern
if found:
	foundAt = pattern.index('*')
	newPatt = [x for x in pattern if x != '*']
n = int(stdin.readline().rstrip())
for _ in range(n):
	string = stdin.readline().rstrip()
	if not found and len(pattern) != len(string):
		print('NO')
		continue
	elif not found:
		patChoice = pattern
	elif found:
		if len(pattern) > len(string) + 1:
			print('NO')
			continue
		else:
			toAdd = len(string) - len(newPatt)
			if foundAt == len(pattern) - 1:
				patChoice = newPatt[:foundAt] + ['*'] * toAdd
			else:
				patChoice = newPatt[:foundAt] + ['*'] * toAdd + newPatt[foundAt:]
	match = True
	for i in range(len(string)):
		if patChoice[i] == '*':
			if string[i] not in bad:
				match = False
				break
		elif patChoice[i] == '?':
			if string[i] not in good:
				match = False
				break
		elif patChoice[i] != string[i]:
			match = False
			break
	if not match:
		print('NO')
	else:
		print('YES')


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] != '?':
				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')


s1 = input()
s2 = input()
n = int(input())
good_words = set(s1)
s3_list = []
for _ in range(n):
	s3 = input()
	s3_list.append(s3)
for s3 in s3_list:
	flag = 1
	if '*' not in s2:
		if len(s2) != len(s3):
			flag = 0
		else:
			for (index, value) in enumerate(s2):
				if value == '?':
					if s3[index] not in good_words:
						flag = 0
						break
				elif s3[index] != s2[index]:
					flag = 0
					break
	elif len(s3) < len(s2) - 1:
		flag = 0
	else:
		idx = s2.index('*')
		s2_1 = s2[:idx]
		s2_2 = s2[idx + 1:]
		for (index, value) in enumerate(s2_1):
			if value == '?':
				if s3[index] not in good_words:
					flag = 0
					break
			elif s3[index] != s2_1[index]:
				flag = 0
				break
		s3_2 = s3[::-1][:len(s2_2)][::-1]
		for (index, value) in enumerate(s2_2):
			if value == '?':
				if s3_2[index] not in good_words:
					flag = 0
					break
			elif s3_2[index] != s2_2[index]:
				flag = 0
				break
		s3_1 = s3[:len(s2_1)]
		s3_m = s3[len(s3_1):len(s3) - len(s3_2)]
		if s3_m != '':
			for word in s3_m:
				if word in good_words:
					flag = 0
					break
	print('YES') if flag == 1 else print('NO')


all_letters = {chr(i) for i in range(97, 97 + 26)}
good_letters = {letter for letter in input()}
bad_letters = all_letters - good_letters
pattern = input()
n = int(input())
star_index = pattern.find('*')

def check(s, m_pattern):
	for i in range(len(s)):
		if m_pattern[i] == '$':
			if s[i] not in bad_letters:
				return False
		elif m_pattern[i] == '?':
			if s[i] not in good_letters:
				return False
		elif m_pattern[i] != s[i]:
			return False
	return True

def match(s):
	if len(s) < len(pattern) - 1:
		return False
	elif len(s) == len(pattern) - 1:
		if star_index == -1:
			return False
		else:
			return check(s, pattern[:star_index] + pattern[star_index + 1:])
	else:
		m_pattern = ''
		if star_index == -1:
			if len(s) > len(pattern):
				return False
			else:
				m_pattern = pattern[:]
		else:
			m_pattern += pattern[:star_index]
			m_sec_pattern = pattern[:star_index]
			for i in range(len(s) - len(pattern) + 1):
				m_pattern += '$'
			m_pattern += pattern[star_index + 1:]
		return check(s, m_pattern)
for i in range(n):
	qs = input()
	print('YES' if match(qs) else 'NO')


good = set(input())
bad = set()
for i in range(97, 97 + 26):
	bad.add(chr(i))
bad -= good
pattern = input()
index = pattern.find('*')
for _ in range(int(input())):
	st = input()
	if index == -1:
		if len(st) != len(pattern):
			print('NO')
			continue
		for i in range(len(st)):
			ptr_ch = pattern[i]
			str_ch = st[i]
			if ptr_ch != '?':
				if str_ch != ptr_ch:
					print('NO')
					break
			elif str_ch not in good:
				print('NO')
				break
		else:
			print('YES')
	else:
		if len(pattern) - 1 > len(st):
			print('NO')
			continue
		for i in range(index):
			ptr_ch = pattern[i]
			str_ch = st[i]
			if ptr_ch != '?':
				if str_ch != ptr_ch:
					print('NO')
					break
			elif str_ch not in good:
				print('NO')
				break
		else:
			for i in range(1, len(pattern) - index):
				ptr_ch = pattern[-i]
				str_ch = st[-i]
				if ptr_ch != '?':
					if str_ch != ptr_ch:
						print('NO')
						break
				elif str_ch not in good:
					print('NO')
					break
			else:
				if set(st[index:len(st) - (len(pattern) - index - 1)]) - bad:
					print('NO')
				else:
					print('YES')


good = set(input())
bad = set((chr(i) for i in range(97, 97 + 26))) - good
pattern = input()
star_idx = pattern.find('*')

def comparison(pattern, string):
	global good
	for i in range(len(pattern)):
		if pattern[i] != '?':
			if string[i] != pattern[i]:
				return False
		elif string[i] not in good:
			return False
	return True
for _ in range(int(input())):
	s = input()
	if star_idx == -1:
		if len(s) != len(pattern):
			print('NO')
		elif comparison(pattern, s):
			print('YES')
		else:
			print('NO')
	elif len(pattern) - 1 > len(s):
		print('NO')
	elif comparison(pattern[:star_idx], s[:star_idx]) and comparison(pattern[:star_idx:-1], s[::-1]):
		if set(s[star_idx:len(s) - (len(pattern) - star_idx - 1)]) - bad:
			print('NO')
		else:
			print('YES')
	else:
		print('NO')


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')


def k(s, p):
	if len(s) != len(p):
		return False
	for (s, p) in zip(s, p):
		if s != p and (not (s == '?' and p in g)):
			return False
	return True
(g, s, n) = (input(), input(), int(input()))
p = [input() for _ in range(n)]
if '*' in s:
	d = s.find('*')
	(sb, se) = (s[:d], s[d + 1:])
	for i in p:
		if len(i) < len(sb + se):
			print('No')
			continue
		(pb, pc, pe) = (i[:d], i[d:len(i) - len(s) + d + 1], i[len(i) - len(s) + d + 1:])

		def c():
			for (s, p) in ((sb, pb), (se, pe)):
				if not k(s, p):
					return False
			return True

		def o():
			for l in pc:
				if l in g:
					return False
			return True
		if c() and o():
			print('Yes')
		else:
			print('No')
else:
	for i in p:
		if k(s, i):
			print('Yes')
		else:
			print('No')


chara = input()
chara_candi = set(list(chara))
pattern = input()
pattern = pattern.strip()
split_pattern = pattern.split('*')
len_query = int(input())
is_star = False
if '*' in pattern:
	is_star = True

def check_patter(pattern, input_pat):
	global split_pattern, is_star
	lenght_pat = len(pattern)
	total_lenght = 0
	for i in range(len(split_pattern)):
		if i == 0:
			if split_pattern[i] == '':
				continue
			else:
				for s in range(len(split_pattern[i])):
					if len(input_pat) - 1 < s:
						return False
					elif split_pattern[i][s] == '?':
						if not input_pat[s] in chara_candi:
							return False
					elif not input_pat[s] == split_pattern[i][s]:
						return False
					total_lenght += 1
		elif split_pattern[i] == '':
			continue
		else:
			lenght = len(split_pattern[i])
			lenght_input = len(input_pat)
			for s in range(lenght):
				if len(input_pat) < lenght_input - s - 1:
					return False
				elif split_pattern[i][lenght - s - 1] == '?':
					if not input_pat[lenght_input - s - 1] in chara_candi:
						return False
				elif not input_pat[lenght_input - s - 1] == split_pattern[i][lenght - s - 1]:
					return False
				total_lenght += 1
	if total_lenght > len(input_pat):
		return False
	if not is_star:
		if len(input_pat) > total_lenght:
			return False
	if len(split_pattern) == 2:
		star_chara = input_pat[len(split_pattern[0]):len(input_pat) - len(split_pattern[1])]
		for i in range(len(star_chara)):
			if star_chara[i] in chara_candi:
				return False
	return True
for i in range(len_query):
	input_patter = input()
	input_patter = input_patter.strip()
	if check_patter(pattern, input_patter):
		print('YES')
	else:
		print('NO')