#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define fi first
#define se second
#define pi acos(-1)
#define inf 0x3f3f3f3f
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define rep(i,x,n) for(int i=x;i<n;i++)
#define per(i,n,x) for(int i=n;i>=x;i--)
using namespace std;
typedef pair<int,int>P;
const int MAXN=100010;
int gcd(int a,int b){return b?gcd(b,a%b):a;}
bool book[1000];
int main()
{
	string s, t;
	cin >> s;
	for(int i = 0; i < s.size(); i++)
	{
		book[s[i]] = 1;
	}
	cin >> s;
	int n;
	cin >> n;
	while(n--)
	{
		cin >> t;
		int i, j;
		bool ok = 1;
		for(i = 0, j = 0; i < t.size() && j < s.size(); i++, j++)
		{
			if(s[j] == t[i]) continue;
			if(s[j] == '?')
			{
				if(book[t[i]])
				continue;
				else
				{
					ok = 0;
					break;
				}
			}
			if(s[j] == '*')
			{
				while(i < t.size() && !book[t[i]] && t.size() - i < s.size() - j) i++;
				i--;
			}
			else
			ok = 0;
		}
		if(j < s.size() || i < t.size() || (j == s.size() - 1 && s[s.size() - 1] == '*'))
		ok = 0;
		if(ok)
		cout << "YES\n";
		else
		cout << "NO\n";
	}
 	return 0;
}