#include<bits/stdc++.h>
#define ll long long
#define pi acos(-1)
#define MAXN 100010
#define inf 0x3f3f3f3f
using namespace std;
typedef pair<int,int>P;
int book[1000];
string s,t,ss;
int main()
{
	int L;
	cin >> s;
	int a = 0, cnt = 0;
	for(int i = 0; i < s.size(); i++)
	{
		book[s[i]] = 1;
	}
	cin >> s;
	for(int i = 0; i < s.size(); i++)
	{
		if(s[i] == '*')
		L = i,a++;
	}
	int n;
	cin >> n;
	while (n--)
	{
		cnt = 0;
		cin >> t;
		bool flag = 0, ok = 1;
		for(int i = 0; i < t.size(); i++)
		{
			if(!book[t[i]])
			{
				if(flag||!a||i!=L)
				{
					ok = 0;
					break;
				}
				int l = i;
				while(i<t.size() && !book[t[i]]) i++;
				t.erase(l, i - l);
				i = l;
				//cout << t << endl;
				flag = 1;
			}
		}
		if(!ok)
		{
			cout <<"NO\n";
			continue;
		}
		if(t.size() == s.size()-a)
		ok = 1;
		else
		ok = 0;
		if(!ok)
		{
			cout <<"NO\n";
			continue;
		}
		for(int i = 0, j = 0; i < t.size() && j < s.size(); i++, j++)
		{
			if(s[j] == '*')
			{
				j++;
				continue;
			}
			if(s[j] == t[i])
			continue;
			if(s[j] != '?')
			ok = 0;
		}
		if(!ok)
		{
			cout <<"NO\n";
			continue;
		}
		else
		cout << "YES\n";
	 }
    return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define pi acos(-1)
#define MAXN 100010
#define inf 0x3f3f3f3f
using namespace std;
typedef pair<int,int>P;
int book[1000];
string s,t,ss;
int main()
{
	int L;
	cin >> s;
	int a = 0, cnt = 0;
	for(int i = 0; i < s.size(); i++)
	{
		book[s[i]] = 1;
	}
	cin >> s;
	for(int i = 0; i < s.size(); i++)
	{
		if(s[i] == '*')
		a++;
	}
	int n;
	cin >> n;
	while (n--)
	{
		cnt = 0;
		cin >> t;
		bool flag = 0, ok = 1;
		int i,j;
		for(i = 0,j = 0; i < t.size(); i++,j++)
		{
			if(!book[t[i]])
			{
				if(flag||!a||s[j]!='*')
				{
					ok = 0;
					break;
				}
				while(i<t.size() && !book[t[i]]) i++;
				i--;
				flag = 1;
			}
			else
			{
				if(j>=s.size())
				{
					ok = 0;
					break;
				}
				if(t[i]==s[j] || s[j] == '*')
				continue;
				if(s[j] == '?')
				{
					continue;
				}
				else
				{
					ok = 0;
					break;
				}
			}
		}
		if(j<s.size()-a)
		ok = 0;
		if(!ok)
		{
			cout <<"NO\n";
			continue;
		}
		else
		cout << "YES\n";
	 }
    return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define pi acos(-1)
#define MAXN 100010
#define inf 0x3f3f3f3f
using namespace std;
typedef pair<int,int>P;
int book[1000];
string s,t,ss;
int main()
{
	int L;
	cin >> s;
	int a = 0, cnt = 0;
	for(int i = 0; i < s.size(); i++)
	{
		book[s[i]] = 1;
	}
	cin >> s;
	for(int i = 0; i < s.size(); i++)
	{
		if(s[i] == '*')
		a++;
	}
	int n;
	cin >> n;
	while (n--)
	{
		cnt = 0;
		cin >> t;
		bool flag = 0, ok = 1;
		int i,j;
		for(i = 0,j = 0; i < t.size(); i++,j++)
		{
			if(!book[t[i]])
			{
				if(flag||!a||s[j]!='*')
				{
					ok = 0;
					break;
				}
				while(i<t.size() && !book[t[i]]) i++;
				i--;
				flag = 1;
			}
			else
			{
				if(j>=s.size())
				{
					ok = 0;
					break;
				}
				if(s[j] == '*')
				{
					i--;
					continue;
				}
				if(t[i]==s[j])
				continue;
				if(s[j] == '?')
				{
					continue;
				}
				else
				{
					ok = 0;
					break;
				}
			}
		}
		if(j<s.size()-a)
		ok = 0;
		if(!ok)
		{
			cout <<"NO\n";
			continue;
		}
		else
		cout << "YES\n";
	 }
    return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define pi acos(-1)
#define MAXN 100010
#define inf 0x3f3f3f3f
using namespace std;
typedef pair<int,int>P;
int book[1000];
string s,t,ss;
int main()
{

	cin >> s;
	int a = 0, cnt = 0;
	for(int i = 0; i < s.size(); i++)
	{
		book[s[i]] = 1;
	}
	cin >> s;
	for(int i = 0; i < s.size(); i++)
	{
		if(s[i] == '*')
		a++;
	}
	int n;
	cin >> n;
	while (n--)
	{
		cnt = 0;
		cin >> t;
		bool flag = 0, ok = 1;
		for(int i = 0; i < t.size(); i++)
		{
			if(!book[t[i]])
			{
				if(flag||!a)
				{
					ok = 0;
					break;
				}
				int l = i;
				while(i<t.size() && !book[t[i]]) i++;
				t.erase(l, i - l);
				//cout << t << endl;
				flag = 1;
			}
		}
		if(!ok)
		{
			cout <<"NO\n";
			continue;
		}
		if(t.size() == s.size()||t.size() == s.size()-a)
		ok = 1;
		else
		ok = 0;
		if(!ok)
		{
			cout <<"NO\n";
			continue;
		}

		for(int i = 0; i < t.size(); i++)
		{
			if(s[i] == t[i]||s[i] == '*')
			continue;
			if(s[i]!='?')
			ok = 0;
		}
		if(!ok)
		{
			cout <<"NO\n";
			continue;
		}
		else
		cout << "YES\n";
	 }
    return 0;
}


#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 - 1)
				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;
}


#include<bits/stdc++.h>
#define ll long long
#define pi acos(-1)
#define MAXN 100010
#define inf 0x3f3f3f3f
using namespace std;
typedef pair<int,int>P;
int book[1000];
int main()
{
	string s,t,ss;
	cin >> s;
	int a = 0, cnt = 0;
	for(int i = 0; i < s.size(); i++)
	{
		book[s[i]] = 1;
	}
	cin >> s;
	for(int i = 0; i < s.size(); i++)
	{
		if(s[i] == '*')
		a++;
		else
		ss[i]=s[i];
	}

	int n;
	cin >> n;
	while (n--)
	{
		cnt = 0;
		cin >> t;
		bool flag = 0, ok = 1;
		for(int i = 0; i < t.size(); i++)
		{
			if(!book[t[i]])
			{
				if(flag||!a)
				{
					ok = 0;
					break;
				}
				int l = i;
				while(i<t.size() && !book[t[i]]) i++;
				t.erase(l, i - l);
				//cout << t << endl;
				flag = 1;
			}
		}
		if(!ok)
		{
			cout <<"NO\n";
			continue;
		}
		if(t.size() == s.size()||t.size() == s.size()-a)
		ok = 1;
		else
		ok = 0;
		if(!ok)
		{
			cout <<"NO\n";
			continue;
		}

		for(int i = 0; i < t.size(); i++)
		{
			if(ss[i] == t[i])
			continue;
			if(ss[i]!='?')
			ok = 0;
		}
		if(!ok)
		{
			cout <<"NO\n";
			continue;
		}
		else
		cout << "YES\n";
	 }
    return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define pi acos(-1)
#define MAXN 100010
#define inf 0x3f3f3f3f
using namespace std;
typedef pair<int,int>P;
int book[1000];
string s,t,ss;
int main()
{
	int L;
	cin >> s;
	int a = 0, cnt = 0;
	for(int i = 0; i < s.size(); i++)
	{
		book[s[i]] = 1;
	}
	cin >> s;
	for(int i = 0; i < s.size(); i++)
	{
		if(s[i] == '*')
		a++;
	}
	int n;
	cin >> n;
	while (n--)
	{
		cnt = 0;
		cin >> t;
		bool flag = 0, ok = 1;
		int i,j;
		for(i = 0,j = 0; i < t.size(); i++,j++)
		{
			if(t[i]==s[j])
				continue;
				if(s[j] == '?' && book[t[i]])
				{
					continue;
				}
			if(!book[t[i]])
			{
				if(flag||!a||s[j]!='*')
				{
					ok = 0;
					break;
				}
				while(i<t.size() && !book[t[i]] && t.size() - i > s.size() - j - 1) i++;
				i--;
				flag = 1;
			}
			else
			{
				if(j>=s.size())
				{
					ok = 0;
					break;
				}
				if(s[j] == '*')
				{
					i--;
					continue;
				}
				if(t[i]==s[j])
				continue;
				if(s[j] == '?' && book[t[i]])
				{
					continue;
				}
				else
				{
					ok = 0;
					break;
				}
			}
		}
		if(j<s.size()-a)
		ok = 0;
		if(!ok)
		{
			cout <<"NO\n";
			continue;
		}
		else
		cout << "YES\n";
	 }
    return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define pi acos(-1)
#define MAXN 100010
#define inf 0x3f3f3f3f
using namespace std;
typedef pair<int,int>P;
int book[1000];
string s,t,ss;
int main()
{

	cin >> s;
	int a = 0, cnt = 0;
	for(int i = 0; i < s.size(); i++)
	{
		book[s[i]] = 1;
	}
	cin >> s;
	for(int i = 0; i < s.size(); i++)
	{
		if(s[i] == '*')
		a++;
	}
	int n;
	cin >> n;
	while (n--)
	{
		cnt = 0;
		cin >> t;
		bool flag = 0, ok = 1;
		for(int i = 0; i < t.size(); i++)
		{
			if(!book[t[i]])
			{
				if(flag||!a)
				{
					ok = 0;
					break;
				}
				int l = i;
				while(i<t.size() && !book[t[i]]) i++;
				t.erase(l, i - l);
				//cout << t << endl;
				flag = 1;
			}
		}
		if(!ok)
		{
			cout <<"NO\n";
			continue;
		}
		if(t.size() == s.size()-a)
		ok = 1;
		else
		ok = 0;
		if(!ok)
		{
			cout <<"NO\n";
			continue;
		}
		for(int i = 0, j = 0; i < t.size() && j < s.size(); i++, j++)
		{
			if(s[j] == '*')
			{
				j++;
				continue;
			}
			if(s[j] == t[i])
			continue;
			if(s[j] != '?')
			ok = 0;
		}
		if(!ok)
		{
			cout <<"NO\n";
			continue;
		}
		else
		cout << "YES\n";
	 }
    return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define pi acos(-1)
#define MAXN 100010
#define inf 0x3f3f3f3f
using namespace std;
typedef pair<int,int>P;
int book[1000];
string s,t,ss;
int main()
{

	cin >> s;
	int a = 0, cnt = 0;
	for(int i = 0; i < s.size(); i++)
	{
		book[s[i]] = 1;
	}
	cin >> s;
	for(int i = 0; i < s.size(); i++)
	{
		if(s[i] == '*')
		a++;
	}
	int n;
	cin >> n;
	while (n--)
	{
		cnt = 0;
		cin >> t;
		bool flag = 0, ok = 1;
		for(int i = 0; i < t.size(); i++)
		{
			if(!book[t[i]])
			{
				if(flag||!a)
				{
					ok = 0;
					break;
				}
				int l = i;
				while(i<t.size() && !book[t[i]]) i++;
				t.erase(l, i - l);
				//cout << t << endl;
				flag = 1;
			}
		}
		if(!ok)
		{
			cout <<"NO\n";
			continue;
		}
		if(t.size() == s.size()||t.size() == s.size()-a)
		ok = 1;
		else
		ok = 0;
		if(!ok)
		{
			cout <<"NO\n";
			continue;
		}

		for(int i = 0,j = 0; i < t.size(); i++,j++)
		{
			if(s[j]=='*')
			{
				j++;
				continue;
			}
			if(s[j] == t[i])
			continue;
			if(s[j]!='?')
			ok = 0;
		}
		if(!ok)
		{
			cout <<"NO\n";
			continue;
		}
		else
		cout << "YES\n";
	 }
    return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define pi acos(-1)
#define MAXN 100010
#define inf 0x3f3f3f3f
using namespace std;
typedef pair<int,int>P;
int book[1000];
string s,t,ss;
int main()
{

	cin >> s;
	int a = 0, cnt = 0;
	for(int i = 0; i < s.size(); i++)
	{
		book[s[i]] = 1;
	}
	cin >> s;
	for(int i = 0; i < s.size(); i++)
	{
		if(s[i] == '*')
		a++;
	}
	int n;
	cin >> n;
	while (n--)
	{
		cnt = 0;
		cin >> t;
		bool flag = 0, ok = 1;
		for(int i = 0; i < t.size(); i++)
		{
			if(!book[t[i]])
			{
				if(flag||!a)
				{
					ok = 0;
					break;
				}
				int l = i;
				while(i<t.size() && !book[t[i]]) i++;
				t.erase(l, i - l);
				i = l;
				//cout << t << endl;
				flag = 1;
			}
		}
		if(!ok)
		{
			cout <<"NO\n";
			continue;
		}
		if(t.size() == s.size()-a)
		ok = 1;
		else
		ok = 0;
		if(!ok)
		{
			cout <<"NO\n";
			continue;
		}
		for(int i = 0, j = 0; i < t.size() && j < s.size(); i++, j++)
		{
			if(s[j] == '*')
			{
				j++;
				continue;
			}
			if(s[j] == t[i])
			continue;
			if(s[j] != '?')
			ok = 0;
		}
		if(!ok)
		{
			cout <<"NO\n";
			continue;
		}
		else
		cout << "YES\n";
	 }
    return 0;
}


#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 - 1)
				i++;
				i--;
			}
			else
			ok = 0;
		}
		if(i < t.size())
		ok = 0;
		if(j < s.size() && !(j == s.size() - 1 && s[s.size() - 1] == '*'))
		ok = 0;
		if(ok)
		cout << "YES\n";
		else
		cout << "NO\n";
	}
 	return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define pi acos(-1)
#define MAXN 100010
#define inf 0x3f3f3f3f
using namespace std;
typedef pair<int,int>P;
int book[1000];
string s,t,ss;
int main()
{
	int L;
	cin >> s;
	int a = 0, cnt = 0;
	for(int i = 0; i < s.size(); i++)
	{
		book[s[i]] = 1;
	}
	cin >> s;
	for(int i = 0; i < s.size(); i++)
	{
		if(s[i] == '*')
		a++;
	}
	int n;
	cin >> n;
	while (n--)
	{
		cnt = 0;
		cin >> t;
		bool flag = 0, ok = 1;
		int i,j;
		for(i = 0,j = 0; i < t.size(); i++,j++)
		{
			if(!book[t[i]])
			{
				if(flag||!a||s[j]!='*')
				{
					ok = 0;
					break;
				}
				while(i<t.size() && !book[t[i]]) i++;
				i--;
				flag = 1;
			}
			else
			{
				if(j>=s.size())
				{
					ok = 0;
					break;
				}
				if(t[i]==s[j])
				continue;
				if(s[j] == '?')
				{
					continue;
				}
				else
				{
					ok = 0;
					break;
				}
			}
		}
		if(j<s.size())
		ok = 0;
		if(!ok)
		{
			cout <<"NO\n";
			continue;
		}
		else
		cout << "YES\n";
	 }
    return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define pi acos(-1)
#define MAXN 100010
#define inf 0x3f3f3f3f
using namespace std;
typedef pair<int,int>P;
int book[1000];
int main()
{
	string s,t;
	cin >> s;
	int a = 0, cnt = 0;
	for(int i = 0; i < s.size(); i++)
	{
		book[s[i]] = 1;
	}
	cin >> s;
	for(int i = 0; i < s.size(); i++)
	if(s[i] == '*')
		a++;
	int n;
	cin >> n;
	while (n--)
	{
		cnt = 0;
		cin >> t;
		bool flag = 0, ok = 1;
		for(int i = 0; i < t.size(); i++)
		{
			if(!book[t[i]])
			{
				if(flag||!a)
				{
					ok = 0;
					break;
				}
				int l = i;
				while(i<t.size() && !book[t[i]]) i++;
				t.erase(l, i - l);
				//cout << t << endl;
				flag = 1;
			}
		}
		if(!ok)
		{
			cout <<"NO\n";
			continue;
		}
		if(t.size() == s.size()||t.size() == s.size()-a)
		ok = 1;
		else
		ok = 0;
		if(!ok)
		{
			cout <<"NO\n";
			continue;
		}
		for(int i = 0; i < t.size(); i++)
		{
			if(s[i] == t[i])
			continue;
			if(s[i]!='?')
			ok = 0;
		}
		if(!ok)
		{
			cout <<"NO\n";
			continue;
		}
		else
		cout << "YES\n";
	 }
    return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define pi acos(-1)
#define MAXN 100010
#define inf 0x3f3f3f3f
using namespace std;
typedef pair<int,int>P;
int book[1000];
string s,t,ss;
int main()
{
	int L;
	cin >> s;
	int a = 0, cnt = 0;
	for(int i = 0; i < s.size(); i++)
	{
		book[s[i]] = 1;
	}
	cin >> s;
	for(int i = 0; i < s.size(); i++)
	{
		if(s[i] == '*')
		a++;
	}
	int n;
	cin >> n;
	while (n--)
	{
		cnt = 0;
		cin >> t;
		bool flag = 0, ok = 1;
		int i,j;
		for(i = 0,j = 0; i < t.size(); i++,j++)
		{
			if(!book[t[i]])
			{
				if(flag||!a||s[j]!='*')
				{
					ok = 0;
					break;
				}
				while(i<t.size() && !book[t[i]]) i++;
				i--;
				flag = 1;
			}
			else
			{
				if(j>=s.size())
				{
					ok = 0;
					break;
				}
				if(t[i]==s[j])
				continue;
				if(s[j] == '?')
				{
					continue;
				}
				else
				{
					ok = 0;
					break;
				}
			}
		}
		if(j<s.size()-a)
		ok = 0;
		if(!ok)
		{
			cout <<"NO\n";
			continue;
		}
		else
		cout << "YES\n";
	 }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ios ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define lop ios; ll t; cin>>t; while(t--)
#define pb push_back
#define vi vector<ll>
#define vivi vector<vector<ll>>
#define vipa vector<pair<ll,ll>>
#define F first
#define S second
#define M 1000000007
#define kl "\n"
typedef long long ll;
ll lcm(ll x, ll y){ll k=x*y; k/=(__gcd(x,y)); return k;}
ll power(ll x,ll y){ ll r= 1;x = x % M;if(x==0) return 0; while(y>0) {if(y&1) r=(r*x)%M; y=y>>1; x=(x*x)%M;} return r;}
void solve()
{
string a,b;
cin>>a>>b;
ll flag=0;
set<char>s;
for(auto i:a) s.insert(i);
for(auto i:b) if(i=='*') flag=1;

ll n;
cin>>n;
while(n--){
    string y;
    cin>>y;
    if(flag==0){
        if(y.size()!=b.size()){
            cout<<"NO\n";
            continue;
        }
        ll f=0;
        for(ll i=0;i<b.size();i++){
            if(b[i]!='?' && b[i]!=y[i]){
                cout<<"NO\n";
                f=1;
                break;
            }
            if(b[i]=='?'){
                if(s.find(y[i])==s.end()){
                    cout<<"NO\n";
                    f=1;
                    break;
                }
            }
        }
        if(!f){
            cout<<"YES\n";
        }
    }
    else{
        ll i,j;
        ll f=0;
        if(y.size()<b.size()){
            cout<<"NO\n";
            continue;
        }
        for(i=0;i<y.size();i++){
            if(b[i]!='?' && b[i]!='*' && b[i]!=y[i]){
                 cout<<"NO\n";
                f=1;
                break;
            }
            if(b[i]=='?'){
                if(s.find(y[i])==s.end()){
                    cout<<"NO\n";
                    f=1;
                    break;
                }
            }
            if(b[i]=='*'){
                break;
            }
        }

        if(!f){
            ll zz;
            for(j=y.size()-1,zz=0;zz<(b.size()-i-1);j--,zz++){
               if(b[i]!='?' && b[i]!='*' && b[i]!=y[i]){
                 cout<<"NO\n";
                f=1;
                break;
            }
            if(b[i]=='?'){
                if(s.find(y[i])==s.end()){
                    cout<<"NO\n";
                    f=1;
                    break;
                }
             }
            }
        }
        if(!f){
            for(ll k=i;k<=j;k++){
                if(s.find(y[i])!=s.end()){
                    cout<<"NO\n";
                    f=1;
                    break;
                }
            }
        }
        if(!f){
            cout<<"YES\n";
        }

    }
}

}
int main()
{
    //lop
    solve();
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ios ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define lop ios; ll t; cin>>t; while(t--)
#define pb push_back
#define vi vector<ll>
#define vivi vector<vector<ll>>
#define vipa vector<pair<ll,ll>>
#define F first
#define S second
#define M 1000000007
#define kl "\n"
typedef long long ll;
ll lcm(ll x, ll y){ll k=x*y; k/=(__gcd(x,y)); return k;}
ll power(ll x,ll y){ ll r= 1;x = x % M;if(x==0) return 0; while(y>0) {if(y&1) r=(r*x)%M; y=y>>1; x=(x*x)%M;} return r;}
void solve()
{
string a,b;
cin>>a>>b;
ll flag=0;
set<char>s;
for(auto i:a) s.insert(i);
for(auto i:b) if(i=='*') flag=1;

ll n;
cin>>n;
while(n--){
    string y;
    cin>>y;
    if(flag==0){
        if(y.size()!=b.size()){
            cout<<"NO\n";
            continue;
        }
        ll f=0;
        for(ll i=0;i<b.size();i++){
            if(b[i]!='?' && b[i]!=y[i]){
                cout<<"NO\n";
                f=1;
                break;
            }
            if(b[i]=='?'){
                if(s.find(y[i])==s.end()){
                    cout<<"NO\n";
                    f=1;
                    break;
                }
            }
        }
        if(!f){
            cout<<"YES\n";
        }
    }
    else{
        ll i,j;
        ll f=0;
        if(y.size()<b.size()-1){
            cout<<"NO\n";
            continue;
        }
        for(i=0;i<y.size();i++){
            if(b[i]!='?' && b[i]!='*' && b[i]!=y[i]){
                 cout<<"NO\n";
                f=1;
                break;
            }
            if(b[i]=='?'){
                if(s.find(y[i])==s.end()){
                    cout<<"NO\n";
                    f=1;
                    break;
                }
            }
            if(b[i]=='*'){
                break;
            }
        }

        if(!f){
            ll zz;
            for(j=y.size()-1,zz=0;zz<(b.size()-i-1);j--,zz++){
               if(b[j]!='?' && b[j]!='*' && b[j]!=y[j]){
                 cout<<"NO\n";
                f=1;
                break;
            }
            if(b[j]=='?'){
                if(s.find(y[j])==s.end()){
                    cout<<"NO\n";
                    f=1;
                    break;
                }
             }
            }
        }
        if(!f){
            for(ll k=i;k<=j;k++){
                if(s.find(y[i])!=s.end()){
                    cout<<"NO\n";
                    f=1;
                    break;
                }
            }
        }
        if(!f){
            cout<<"YES\n";
        }

    }
}

}
int main()
{
    //lop
    solve();
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ios ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define lop ios; ll t; cin>>t; while(t--)
#define pb push_back
#define vi vector<ll>
#define vivi vector<vector<ll>>
#define vipa vector<pair<ll,ll>>
#define F first
#define S second
#define M 1000000007
#define kl "\n"
typedef long long ll;
ll lcm(ll x, ll y){ll k=x*y; k/=(__gcd(x,y)); return k;}
ll power(ll x,ll y){ ll r= 1;x = x % M;if(x==0) return 0; while(y>0) {if(y&1) r=(r*x)%M; y=y>>1; x=(x*x)%M;} return r;}
void solve()
{
string a,b;
cin>>a>>b;
ll flag=0;
set<char>s;
for(auto i:a) s.insert(i);
for(auto i:b) if(i=='*') flag=1;

ll n;
cin>>n;
while(n--){
    string y;
    cin>>y;
    if(flag==0){
        if(y.size()!=b.size()){
            cout<<"NO\n";
            continue;
        }
        ll f=0;
        for(ll i=0;i<b.size();i++){
            if(b[i]!='?' && b[i]!=y[i]){
                cout<<"NO\n";
                f=1;
                break;
            }
            if(b[i]=='?'){
                if(s.find(y[i])==s.end()){
                    cout<<"NO\n";
                    f=1;
                    break;
                }
            }
        }
        if(!f){
            cout<<"YES\n";
        }
    }
    else{
        ll i,j;
        ll f=0;
        if(y.size()<b.size()-1){
            cout<<"NO\n";
            continue;
        }
        for(i=0;i<y.size();i++){
            if(b[i]!='?' && b[i]!='*' && b[i]!=y[i]){
                 cout<<"NO\n";
                f=1;
                break;
            }
            if(b[i]=='?'){
                if(s.find(y[i])==s.end()){
                    cout<<"NO\n";
                    f=1;
                    break;
                }
            }
            if(b[i]=='*'){
                break;
            }
        }

        if(!f){
            ll zz,jj;
            for(j=y.size()-1,jj=b.size()-1,zz=0;zz<(b.size()-i-1);j--,jj--,zz++){
               if(b[jj]!='?' && b[jj]!='*' && b[jj]!=y[j]){
                 cout<<"NO\n";
                f=1;
                break;
            }
            if(b[jj]=='?'){
                if(s.find(y[j])==s.end()){
                    cout<<"NO\n";
                    f=1;
                    break;
                }
             }
            }
        }
        if(!f){
            for(ll k=i;k<=j;k++){
                if(s.find(y[k])!=s.end()){
                    cout<<"NO\n";
                    f=1;
                    break;
                }
            }
        }
        if(!f){
            cout<<"YES\n";
        }

    }
}

}
int main()
{
    //lop
    solve();
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define LLMAX (1ll << 60) - 1
#define INTMAX (1 << 30) - 1
#define MODDD 1000000007
#define NMAX 1000*100+1

#define numberOfSetBits(S) __builtin_popcount(S) // __builtin_popcountl(S) __builtin_popcountll(S)
#define MSET(x,y) memset(x,y,sizeof(x))
#define gcd(a,b) __gcd(a,b)
#define all(x)  x.begin(),x.end()
#define isOn(S, j) (S & (1 << j))
#define endl '\n'
#define setBit(S, j) (S |= (1 << j))
#define clearBit(S, j) (S &= ~(1 << j))
#define toggleBit(S, j) (S ^= (1 << j))
#define print(x)    for(auto it:x) cout<<it<<' '; cout<<endl;
#define printii(x)  for(auto it:x) cout<<it.F<<' '<<it.S<<'\t';  cout<<endl;
#define in(x,n)   for(int e=0;e<n;e++){ll y;cin>>y;x.pb(y);}

#define vi vector<ll>
#define vvi vector<vi>
#define ii pair<ll,ll>
#define pll pair<ll,ll>
#define vii vector<ii>
#define vvii vector<vii>
#define viii vector<pair<ii,ll>>
#define pb push_back
#define F first
#define S second
#define mp make_pair
#define mc(a,b,c) mp(mp(a,b),c)
bool boo[26];

template <typename A, typename B>
string to_string(pair<A, B> p);

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);

string to_string(const string& s) {
  return '"' + s + '"';
}

string to_string(const char* s) {
  return to_string((string) s);
}

string to_string(bool b) {
  return (b ? "true" : "false");
}

string to_string(vector<bool> v) {
  bool first = true;
  string res = "{";
  for (int i = 0; i < static_cast<int>(v.size()); i++) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(v[i]);
  }
  res += "}";
  return res;
}

template <size_t N>
string to_string(bitset<N> v) {
  string res = "";
  for (size_t i = 0; i < N; i++) {
    res += static_cast<char>('0' + v[i]);
  }
  return res;
}

template <typename A>
string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}

template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")";
}

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}

void debug_out() { cout << endl; }

template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cout << " " << to_string(H);
  debug_out(T...);
}

#define debug(...) cout << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll n,x,t,m,y,z;
    string s;
    cin>>s;
    for(auto it:s){
        boo[it-'a']=true;
    }
    cin>>s;
    cin>>t;
    bool ok = false;
    for(auto it:s){
        if(it=='*'){
            ok=true;
        }
    }
    string q;
    while(t--){
        cin>>q;
        n = q.size();
        if(!ok){
            if(n!=s.size()){
                cout<<"NO\n";
                continue;
            }
            bool foo = true;
            for(int i=0;i<n;i++){
                if(s[i]!=q[i]){
                    if(((s[i]=='?')&&(!boo[q[i]-'a']))||(s[i]!='?')){
                        foo = false;

                        break;
                    }
                }
            }
            if(foo){
                cout<<"YES\n";
            }
            else{
                cout<<"NO\n";
            }
            continue;
        }
        else{
            if(n<(s.size()-1)){
                cout<<"NO\n";
                continue;
            }
            string s1,s2,s3;
            bool foo = true;
            for(int i=0;s[i]!='*';i++){
                if(s[i]!=q[i]){
                    if(((s[i]=='?')&&(!boo[q[i]-'a']))||(s[i]!='?')){
                        foo = false;
                        break;
                    }
                }
                s1.pb(q[i]);
            }
            reverse(all(s));
            reverse(all(q));
            for(int i=0;s[i]!='*';i++){
                if(s[i]!=q[i]){
                    if(((s[i]=='?')&&(!boo[q[i]-'a']))||(s[i]!='?')){
                        foo = false;
                        break;
                    }
                }
                s2.pb(q[i]);
            }
            reverse(all(s));
            reverse(all(q));
            reverse(all(s2));
            for(int i=0;i<n-s1.size()-s2.size();i++){
                s3.pb('#');
            }
            s1 = s1+s3+s2;
            for(int i=0;i<n;i++){
                if((s1[i]=='#')&&(boo[q[i]-'a']))foo = false;
            }

            if(foo){
                cout<<"YES\n";
            }
            else{
                cout<<"NO\n";
            }
            continue;
        }
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define LLMAX (1ll << 60) - 1
#define INTMAX (1 << 30) - 1
#define MOD 1000000007
#define N 1000*100+1

#define numberOfSetBits(S) __builtin_popcount(S) // __builtin_popcountl(S) __builtin_popcountll(S)
#define MSET(x,y) memset(x,y,sizeof(x))
#define gcd(a,b) __gcd(a,b)
#define all(x)  x.begin(),x.end()
#define isOn(S, j) (S & (1 << j))
#define endl '\n'
#define setBit(S, j) (S |= (1 << j))
#define clearBit(S, j) (S &= ~(1 << j))
#define toggleBit(S, j) (S ^= (1 << j))
#define print(x)    for(auto it:x) cout<<it<<' '; cout<<endl;
#define printii(x)  for(auto it:x) cout<<it.F<<' '<<it.S<<'\t';  cout<<endl;
#define in(x,n)   for(int e=0;e<n;e++){ll y;cin>>y;x.pb(y);}

#define vi vector<ll>
#define vvi vector<vi>
#define ii pair<ll,ll>
#define pll pair<ll,ll>
#define vii vector<ii>
#define vvii vector<vii>
#define viii vector<pair<ii,ll>>
#define pb push_back
#define F first
#define S second
#define mp make_pair
#define mc(a,b,c) mp(mp(a,b),c)
bool boo[26];

template <typename A, typename B>
string to_string(pair<A, B> p);

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);

string to_string(const string& s) {
  return '"' + s + '"';
}

string to_string(const char* s) {
  return to_string((string) s);
}

string to_string(bool b) {
  return (b ? "true" : "false");
}

string to_string(vector<bool> v) {
  bool first = true;
  string res = "{";
  for (int i = 0; i < static_cast<int>(v.size()); i++) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(v[i]);
  }
  res += "}";
  return res;
}

template <size_t N>
string to_string(bitset<N> v) {
  string res = "";
  for (size_t i = 0; i < N; i++) {
    res += static_cast<char>('0' + v[i]);
  }
  return res;
}

template <typename A>
string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}

template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")";
}

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}

void debug_out() { cout << endl; }

template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cout << " " << to_string(H);
  debug_out(T...);
}

#define debug(...) cout << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll n,x,t,m,y,z;
    string s;
    cin>>s;
    for(auto it:s){
        boo[it-'a']=true;
    }
    cin>>s;
    cin>>t;
    bool ok = false;
    for(auto it:s){
        if(it=='*'){
            ok=true;
        }
    }
    string q;
    while(t--){
        cin>>q;
        n = q.size();
        if(!ok){
            if(n!=s.size()){
                cout<<"NO\n";
                continue;
            }
            bool foo = true;
            for(int i=0;i<n;i++){
                if(s[i]!=q[i]){
                    if(((s[i]=='?')&&(!boo[q[i]-'a']))||(s[i]!='?')){
                        foo = false;

                        break;
                    }
                }
            }
            if(foo){
                cout<<"YES\n";
            }
            else{
                cout<<"NO\n";
            }
            continue;
        }
        else{
            if(n<(s.size()-1)){
                cout<<"NO\n";
                continue;
            }
            string s1,s2,s3;
            bool foo = true;
            for(int i=0;s[i]!='*';i++){
                if(s[i]!=q[i]){
                    if(((s[i]=='?')&&(!boo[q[i]-'a']))||(s[i]!='?')){
                        foo = false;
                        break;
                    }
                }
                s1.pb(q[i]);
            }
            reverse(all(s));
            reverse(all(q));
            for(int i=0;s[i]!='*';i++){
                if(s[i]!=q[i]){
                    if(((s[i]=='?')&&(!boo[q[i]-'a']))||(s[i]!='?')){
                        foo = false;
                        break;
                    }
                }
                s2.pb(q[i]);
            }
            reverse(all(s));
            reverse(all(q));
            reverse(all(s2));
            for(int i=0;i<n-s1.size()-s2.size();i++){
                s3.pb('#');
            }
            s1 = s1+s3+s2;
            for(int i=0;i<n;i++){
                if((s1[i]=='#')&&(boo[q[i]-'a']))foo = false;
            }

            if(foo){
                cout<<"YES\n";
            }
            else{
                cout<<"NO\n";
            }
            continue;
        }
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ios ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define lop ios; ll t; cin>>t; while(t--)
#define pb push_back
#define vi vector<ll>
#define vivi vector<vector<ll>>
#define vipa vector<pair<ll,ll>>
#define F first
#define S second
#define M 1000000007
#define kl "\n"
typedef long long ll;
ll lcm(ll x, ll y){ll k=x*y; k/=(__gcd(x,y)); return k;}
ll power(ll x,ll y){ ll r= 1;x = x % M;if(x==0) return 0; while(y>0) {if(y&1) r=(r*x)%M; y=y>>1; x=(x*x)%M;} return r;}
void solve()
{
string a,b;
cin>>a>>b;
ll flag=0;
set<char>s;
for(auto i:a) s.insert(i);
for(auto i:b) if(i=='*') flag=1;

ll n;
cin>>n;
while(n--){
    string y;
    cin>>y;
    if(flag==0){
        if(y.size()!=b.size()-1){
            cout<<"NO\n";
            continue;
        }
        ll f=0;
        for(ll i=0;i<b.size();i++){
            if(b[i]!='?' && b[i]!=y[i]){
                cout<<"NO\n";
                f=1;
                break;
            }
            if(b[i]=='?'){
                if(s.find(y[i])==s.end()){
                    cout<<"NO\n";
                    f=1;
                    break;
                }
            }
        }
        if(!f){
            cout<<"YES\n";
        }
    }
    else{
        ll i,j;
        ll f=0;
        if(y.size()<b.size()-1){
            cout<<"NO\n";
            continue;
        }
        for(i=0;i<y.size();i++){
            if(b[i]!='?' && b[i]!='*' && b[i]!=y[i]){
                 cout<<"NO\n";
                f=1;
                break;
            }
            if(b[i]=='?'){
                if(s.find(y[i])==s.end()){
                    cout<<"NO\n";
                    f=1;
                    break;
                }
            }
            if(b[i]=='*'){
                break;
            }
        }

        if(!f){
            ll zz;
            for(j=y.size()-1,zz=0;zz<(b.size()-i-1);j--,zz++){
               if(b[j]!='?' && b[j]!='*' && b[j]!=y[j]){
                 cout<<"NO\n";
                f=1;
                break;
            }
            if(b[j]=='?'){
                if(s.find(y[j])==s.end()){
                    cout<<"NO\n";
                    f=1;
                    break;
                }
             }
            }
        }
        if(!f){
            for(ll k=i;k<=j;k++){
                if(s.find(y[i])!=s.end()){
                    cout<<"NO\n";
                    f=1;
                    break;
                }
            }
        }
        if(!f){
            cout<<"YES\n";
        }

    }
}

}
int main()
{
    //lop
    solve();
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ios ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define lop ios; ll t; cin>>t; while(t--)
#define pb push_back
#define vi vector<ll>
#define vivi vector<vector<ll>>
#define vipa vector<pair<ll,ll>>
#define F first
#define S second
#define M 1000000007
#define kl "\n"
typedef long long ll;
ll lcm(ll x, ll y){ll k=x*y; k/=(__gcd(x,y)); return k;}
ll power(ll x,ll y){ ll r= 1;x = x % M;if(x==0) return 0; while(y>0) {if(y&1) r=(r*x)%M; y=y>>1; x=(x*x)%M;} return r;}
void solve()
{
string a,b;
cin>>a>>b;
ll flag=0;
set<char>s;
for(auto i:a) s.insert(i);
for(auto i:b) if(i=='*') flag=1;

ll n;
cin>>n;
while(n--){
    string y;
    cin>>y;
    if(flag==0){
        if(y.size()!=b.size()){
            cout<<"NO\n";
            continue;
        }
        ll f=0;
        for(ll i=0;i<b.size();i++){
            if(b[i]!='?' && b[i]!=y[i]){
                cout<<"NO\n";
                f=1;
                break;
            }
            if(b[i]=='?'){
                if(s.find(y[i])==s.end()){
                    cout<<"NO\n";
                    f=1;
                    break;
                }
            }
        }
        if(!f){
            cout<<"YES\n";
        }
    }
    else{
        ll i,j;
        ll f=0;
        if(y.size()<b.size()-1){
            cout<<"NO\n";
            continue;
        }
        for(i=0;i<y.size();i++){
            if(b[i]!='?' && b[i]!='*' && b[i]!=y[i]){
                 cout<<"NO\n";
                f=1;
                break;
            }
            if(b[i]=='?'){
                if(s.find(y[i])==s.end()){
                    cout<<"NO\n";
                    f=1;
                    break;
                }
            }
            if(b[i]=='*'){
                break;
            }
        }

        if(!f){
            ll zz;
            for(j=y.size()-1,zz=0;zz<(b.size()-i-1);j--,zz++){
               if(b[i]!='?' && b[i]!='*' && b[i]!=y[i]){
                 cout<<"NO\n";
                f=1;
                break;
            }
            if(b[i]=='?'){
                if(s.find(y[i])==s.end()){
                    cout<<"NO\n";
                    f=1;
                    break;
                }
             }
            }
        }
        if(!f){
            for(ll k=i;k<=j;k++){
                if(s.find(y[i])!=s.end()){
                    cout<<"NO\n";
                    f=1;
                    break;
                }
            }
        }
        if(!f){
            cout<<"YES\n";
        }

    }
}

}
int main()
{
    //lop
    solve();
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define LLMAX (1ll << 60) - 1
#define INTMAX (1 << 30) - 1
#define MOD 1000000007
#define NMX 1000*100+1

#define numberOfSetBits(S) __builtin_popcount(S) // __builtin_popcountl(S) __builtin_popcountll(S)
#define MSET(x,y) memset(x,y,sizeof(x))
#define gcd(a,b) __gcd(a,b)
#define all(x)  x.begin(),x.end()
#define isOn(S, j) (S & (1 << j))
#define endl '\n'
#define setBit(S, j) (S |= (1 << j))
#define clearBit(S, j) (S &= ~(1 << j))
#define toggleBit(S, j) (S ^= (1 << j))
#define print(x)    for(auto it:x) cout<<it<<' '; cout<<endl;
#define printii(x)  for(auto it:x) cout<<it.F<<' '<<it.S<<'\t';  cout<<endl;
#define in(x,n)   for(int e=0;e<n;e++){ll y;cin>>y;x.pb(y);}

#define vi vector<ll>
#define vvi vector<vi>
#define ii pair<ll,ll>
#define pll pair<ll,ll>
#define vii vector<ii>
#define vvii vector<vii>
#define viii vector<pair<ii,ll>>
#define pb push_back
#define F first
#define S second
#define mp make_pair
#define mc(a,b,c) mp(mp(a,b),c)
bool boo[26];

template <typename A, typename B>
string to_string(pair<A, B> p);

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);

string to_string(const string& s) {
  return '"' + s + '"';
}

string to_string(const char* s) {
  return to_string((string) s);
}

string to_string(bool b) {
  return (b ? "true" : "false");
}

string to_string(vector<bool> v) {
  bool first = true;
  string res = "{";
  for (int i = 0; i < static_cast<int>(v.size()); i++) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(v[i]);
  }
  res += "}";
  return res;
}

template <size_t N>
string to_string(bitset<N> v) {
  string res = "";
  for (size_t i = 0; i < N; i++) {
    res += static_cast<char>('0' + v[i]);
  }
  return res;
}

template <typename A>
string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}

template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")";
}

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}

void debug_out() { cout << endl; }

template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cout << " " << to_string(H);
  debug_out(T...);
}

#define debug(...) cout << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll n,x,t,m,y,z;
    string s;
    cin>>s;
    for(auto it:s){
        boo[it-'a']=true;
    }
    cin>>s;
    cin>>t;
    bool ok = false;
    for(auto it:s){
        if(it=='*'){
            ok=true;
        }
    }
    string q;
    while(t--){
        cin>>q;
        n = q.size();
        if(!ok){
            if(n!=s.size()){
                cout<<"NO\n";
                continue;
            }
            bool foo = true;
            for(int i=0;i<n;i++){
                if(s[i]!=q[i]){
                    if(((s[i]=='?')&&(!boo[q[i]-'a']))||(s[i]!='?')){
                        foo = false;

                        break;
                    }
                }
            }
            if(foo){
                cout<<"YES\n";
            }
            else{
                cout<<"NO\n";
            }
            continue;
        }
        else{
            if(n<(s.size()-1)){
                cout<<"NO\n";
                continue;
            }
            string s1,s2,s3;
            bool foo = true;
            for(int i=0;s[i]!='*';i++){
                if(s[i]!=q[i]){
                    if(((s[i]=='?')&&(!boo[q[i]-'a']))||(s[i]!='?')){
                        foo = false;
                        break;
                    }
                }
                s1.pb(q[i]);
            }
            reverse(all(s));
            reverse(all(q));
            for(int i=0;s[i]!='*';i++){
                if(s[i]!=q[i]){
                    if(((s[i]=='?')&&(!boo[q[i]-'a']))||(s[i]!='?')){
                        foo = false;
                        break;
                    }
                }
                s2.pb(q[i]);
            }
            reverse(all(s));
            reverse(all(q));
            reverse(all(s2));
            for(int i=0;i<n-s1.size()-s2.size();i++){
                s3.pb('#');
            }
            s1 = s1+s3+s2;
            for(int i=0;i<n;i++){
                if((s1[i]=='#')&&(boo[q[i]-'a']))foo = false;
            }

            if(foo){
                cout<<"YES\n";
            }
            else{
                cout<<"NO\n";
            }
            continue;
        }
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define LLMAX (1ll << 60) - 1
#define INTMAX (1 << 30) - 1
#define MOD 1000000007
#define NMAX 1000*100+1

#define numberOfSetBits(S) __builtin_popcount(S) // __builtin_popcountl(S) __builtin_popcountll(S)
#define MSET(x,y) memset(x,y,sizeof(x))
#define gcd(a,b) __gcd(a,b)
#define all(x)  x.begin(),x.end()
#define isOn(S, j) (S & (1 << j))
#define endl '\n'
#define setBit(S, j) (S |= (1 << j))
#define clearBit(S, j) (S &= ~(1 << j))
#define toggleBit(S, j) (S ^= (1 << j))
#define print(x)    for(auto it:x) cout<<it<<' '; cout<<endl;
#define printii(x)  for(auto it:x) cout<<it.F<<' '<<it.S<<'\t';  cout<<endl;
#define in(x,n)   for(int e=0;e<n;e++){ll y;cin>>y;x.pb(y);}

#define vi vector<ll>
#define vvi vector<vi>
#define ii pair<ll,ll>
#define pll pair<ll,ll>
#define vii vector<ii>
#define vvii vector<vii>
#define viii vector<pair<ii,ll>>
#define pb push_back
#define F first
#define S second
#define mp make_pair
#define mc(a,b,c) mp(mp(a,b),c)
bool boo[26];

template <typename A, typename B>
string to_string(pair<A, B> p);

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);

string to_string(const string& s) {
  return '"' + s + '"';
}

string to_string(const char* s) {
  return to_string((string) s);
}

string to_string(bool b) {
  return (b ? "true" : "false");
}

string to_string(vector<bool> v) {
  bool first = true;
  string res = "{";
  for (int i = 0; i < static_cast<int>(v.size()); i++) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(v[i]);
  }
  res += "}";
  return res;
}

template <size_t N>
string to_string(bitset<N> v) {
  string res = "";
  for (size_t i = 0; i < N; i++) {
    res += static_cast<char>('0' + v[i]);
  }
  return res;
}

template <typename A>
string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}

template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")";
}

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}

void debug_out() { cout << endl; }

template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cout << " " << to_string(H);
  debug_out(T...);
}

#define debug(...) cout << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll n,x,t,m,y,z;
    string s;
    cin>>s;
    for(auto it:s){
        boo[it-'a']=true;
    }
    cin>>s;
    cin>>t;
    bool ok = false;
    for(auto it:s){
        if(it=='*'){
            ok=true;
        }
    }
    string q;
    while(t--){
        cin>>q;
        n = q.size();
        if(!ok){
            if(n!=s.size()){
                cout<<"NO\n";
                continue;
            }
            bool foo = true;
            for(int i=0;i<n;i++){
                if(s[i]!=q[i]){
                    if(((s[i]=='?')&&(!boo[q[i]-'a']))||(s[i]!='?')){
                        foo = false;

                        break;
                    }
                }
            }
            if(foo){
                cout<<"YES\n";
            }
            else{
                cout<<"NO\n";
            }
            continue;
        }
        else{
            if(n<(s.size()-1)){
                cout<<"NO\n";
                continue;
            }
            string s1,s2,s3;
            bool foo = true;
            for(int i=0;s[i]!='*';i++){
                if(s[i]!=q[i]){
                    if(((s[i]=='?')&&(!boo[q[i]-'a']))||(s[i]!='?')){
                        foo = false;
                        break;
                    }
                }
                s1.pb(q[i]);
            }
            reverse(all(s));
            reverse(all(q));
            for(int i=0;s[i]!='*';i++){
                if(s[i]!=q[i]){
                    if(((s[i]=='?')&&(!boo[q[i]-'a']))||(s[i]!='?')){
                        foo = false;
                        break;
                    }
                }
                s2.pb(q[i]);
            }
            reverse(all(s));
            reverse(all(q));
            reverse(all(s2));
            for(int i=0;i<n-s1.size()-s2.size();i++){
                s3.pb('#');
            }
            s1 = s1+s3+s2;
            for(int i=0;i<n;i++){
                if((s1[i]=='#')&&(boo[q[i]-'a']))foo = false;
            }

            if(foo){
                cout<<"YES\n";
            }
            else{
                cout<<"NO\n";
            }
            continue;
        }
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ios ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define lop ios; ll t; cin>>t; while(t--)
#define pb push_back
#define vi vector<ll>
#define vivi vector<vector<ll>>
#define vipa vector<pair<ll,ll>>
#define F first
#define S second
#define M 1000000007
#define kl "\n"
typedef long long ll;
ll lcm(ll x, ll y){ll k=x*y; k/=(__gcd(x,y)); return k;}
ll power(ll x,ll y){ ll r= 1;x = x % M;if(x==0) return 0; while(y>0) {if(y&1) r=(r*x)%M; y=y>>1; x=(x*x)%M;} return r;}
void solve()
{
string a,b;
cin>>a>>b;
ll flag=0;
set<char>s;
for(auto i:a) s.insert(i);
for(auto i:b) if(i=='*') flag=1;

ll n;
cin>>n;
while(n--){
    string y;
    cin>>y;
    if(flag==0){
        if(y.size()!=b.size()){
            cout<<"NO\n";
            continue;
        }
        ll f=0;
        for(ll i=0;i<b.size();i++){
            if(b[i]!='?' && b[i]!=y[i]){
                cout<<"NO\n";
                f=1;
                break;
            }
            if(b[i]=='?'){
                if(s.find(y[i])==s.end()){
                    cout<<"NO\n";
                    f=1;
                    break;
                }
            }
        }
        if(!f){
            cout<<"YES\n";
        }
    }
    else{
        ll i,j;
        ll f=0;
        if(y.size()<b.size()-1){
            cout<<"NO\n";
            continue;
        }
        for(i=0;i<y.size();i++){
            if(b[i]!='?' && b[i]!='*' && b[i]!=y[i]){
                 cout<<"NO\n";
                f=1;
                break;
            }
            if(b[i]=='?'){
                if(s.find(y[i])==s.end()){
                    cout<<"NO\n";
                    f=1;
                    break;
                }
            }
            if(b[i]=='*'){
                break;
            }
        }

        if(!f){
            ll zz;
            for(j=y.size()-1;;j--){
               if(b[j]!='?' && b[j]!='*' && b[j]!=y[j]){
                 cout<<"NO\n";
                f=1;
                break;
            }
            if(b[j]=='?'){
                if(s.find(y[j])==s.end()){
                    cout<<"NO\n";
                    f=1;
                    break;
                }
             }
            }
            if(b[j]=='*') break;
        }
        if(!f){
            for(ll k=i;k<=j;k++){
                if(s.find(y[i])!=s.end()){
                    cout<<"NO\n";
                    f=1;
                    break;
                }
            }
        }
        if(!f){
            cout<<"YES\n";
        }

    }
}

}
int main()
{
    //lop
    solve();
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(a,b,c) for(int i=(a);i!=(b);i+=(c))

int main()
{
	string te,s; cin>>te>>s;
	bool good[26];
	rep(0,26,1) good[i]=false;
	rep(0,te.size(),1) good[te[i]-'a']=true;
	int t; cin>>t;
	while(t--)
	{
		string test; cin>>test;
		bool fail=false;

		int i=0,j=0;
		while(i<test.size() and j<s.size() and !fail)
		{
			if(s[j]==test[i]) i++,j++;
			else if(s[j]=='?' and good[test[i]-'a']) i++,j++;
			else if(s[j]=='*')
			{
				int c=0;
				while(i<test.size() and !good[test[i]-'a'] and c<(test.size()-s.size()+1)) i++,c++;
				if(c!=(test.size()-s.size()+1)) fail=true;
				j++;
			}
			else fail=true;
		}

		if(j<s.size() and s[j]=='*') j++;
		fail=(fail or i<test.size() or j<s.size());
		if(!fail) cout<<"YES\n";
		else cout<<"NO\n";
	}
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define LLMAX (1ll << 60) - 1
#define INTMAX (1 << 30) - 1
#define MOD 100000007
#define NMAX 1000*100+1

#define numberOfSetBits(S) __builtin_popcount(S) // __builtin_popcountl(S) __builtin_popcountll(S)
#define MSET(x,y) memset(x,y,sizeof(x))
#define gcd(a,b) __gcd(a,b)
#define all(x)  x.begin(),x.end()
#define isOn(S, j) (S & (1 << j))
#define endl '\n'
#define setBit(S, j) (S |= (1 << j))
#define clearBit(S, j) (S &= ~(1 << j))
#define toggleBit(S, j) (S ^= (1 << j))
#define print(x)    for(auto it:x) cout<<it<<' '; cout<<endl;
#define printii(x)  for(auto it:x) cout<<it.F<<' '<<it.S<<'\t';  cout<<endl;
#define in(x,n)   for(int e=0;e<n;e++){ll y;cin>>y;x.pb(y);}

#define vi vector<ll>
#define vvi vector<vi>
#define ii pair<ll,ll>
#define pll pair<ll,ll>
#define vii vector<ii>
#define vvii vector<vii>
#define viii vector<pair<ii,ll>>
#define pb push_back
#define F first
#define S second
#define mp make_pair
#define mc(a,b,c) mp(mp(a,b),c)
bool boo[26];

template <typename A, typename B>
string to_string(pair<A, B> p);

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);

string to_string(const string& s) {
  return '"' + s + '"';
}

string to_string(const char* s) {
  return to_string((string) s);
}

string to_string(bool b) {
  return (b ? "true" : "false");
}

string to_string(vector<bool> v) {
  bool first = true;
  string res = "{";
  for (int i = 0; i < static_cast<int>(v.size()); i++) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(v[i]);
  }
  res += "}";
  return res;
}

template <size_t N>
string to_string(bitset<N> v) {
  string res = "";
  for (size_t i = 0; i < N; i++) {
    res += static_cast<char>('0' + v[i]);
  }
  return res;
}

template <typename A>
string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}

template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")";
}

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}

void debug_out() { cout << endl; }

template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cout << " " << to_string(H);
  debug_out(T...);
}

#define debug(...) cout << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll n,x,t,m,y,z;
    string s;
    cin>>s;
    for(auto it:s){
        boo[it-'a']=true;
    }
    cin>>s;
    cin>>t;
    bool ok = false;
    for(auto it:s){
        if(it=='*'){
            ok=true;
        }
    }
    string q;
    while(t--){
        cin>>q;
        n = q.size();
        if(!ok){
            if(n!=s.size()){
                cout<<"NO\n";
                continue;
            }
            bool foo = true;
            for(int i=0;i<n;i++){
                if(s[i]!=q[i]){
                    if(((s[i]=='?')&&(!boo[q[i]-'a']))||(s[i]!='?')){
                        foo = false;

                        break;
                    }
                }
            }
            if(foo){
                cout<<"YES\n";
            }
            else{
                cout<<"NO\n";
            }
            continue;
        }
        else{
            if(n<(s.size()-1)){
                cout<<"NO\n";
                continue;
            }
            string s1,s2,s3;
            bool foo = true;
            for(int i=0;s[i]!='*';i++){
                if(s[i]!=q[i]){
                    if(((s[i]=='?')&&(!boo[q[i]-'a']))||(s[i]!='?')){
                        foo = false;
                        break;
                    }
                }
                s1.pb(q[i]);
            }
            reverse(all(s));
            reverse(all(q));
            for(int i=0;s[i]!='*';i++){
                if(s[i]!=q[i]){
                    if(((s[i]=='?')&&(!boo[q[i]-'a']))||(s[i]!='?')){
                        foo = false;
                        break;
                    }
                }
                s2.pb(q[i]);
            }
            reverse(all(s));
            reverse(all(q));
            reverse(all(s2));
            for(int i=0;i<n-s1.size()-s2.size();i++){
                s3.pb('#');
            }
            s1 = s1+s3+s2;
            for(int i=0;i<n;i++){
                if((s1[i]=='#')&&(boo[q[i]-'a']))foo = false;
            }

            if(foo){
                cout<<"YES\n";
            }
            else{
                cout<<"NO\n";
            }
            continue;
        }
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define LLMAX (1ll << 60) - 1
#define INTMAX (1 << 30) - 1
#define MOD 1000000007
#define NMAX 1000*10+1

#define numberOfSetBits(S) __builtin_popcount(S) // __builtin_popcountl(S) __builtin_popcountll(S)
#define MSET(x,y) memset(x,y,sizeof(x))
#define gcd(a,b) __gcd(a,b)
#define all(x)  x.begin(),x.end()
#define isOn(S, j) (S & (1 << j))
#define endl '\n'
#define setBit(S, j) (S |= (1 << j))
#define clearBit(S, j) (S &= ~(1 << j))
#define toggleBit(S, j) (S ^= (1 << j))
#define print(x)    for(auto it:x) cout<<it<<' '; cout<<endl;
#define printii(x)  for(auto it:x) cout<<it.F<<' '<<it.S<<'\t';  cout<<endl;
#define in(x,n)   for(int e=0;e<n;e++){ll y;cin>>y;x.pb(y);}

#define vi vector<ll>
#define vvi vector<vi>
#define ii pair<ll,ll>
#define pll pair<ll,ll>
#define vii vector<ii>
#define vvii vector<vii>
#define viii vector<pair<ii,ll>>
#define pb push_back
#define F first
#define S second
#define mp make_pair
#define mc(a,b,c) mp(mp(a,b),c)
bool boo[26];

template <typename A, typename B>
string to_string(pair<A, B> p);

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);

string to_string(const string& s) {
  return '"' + s + '"';
}

string to_string(const char* s) {
  return to_string((string) s);
}

string to_string(bool b) {
  return (b ? "true" : "false");
}

string to_string(vector<bool> v) {
  bool first = true;
  string res = "{";
  for (int i = 0; i < static_cast<int>(v.size()); i++) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(v[i]);
  }
  res += "}";
  return res;
}

template <size_t N>
string to_string(bitset<N> v) {
  string res = "";
  for (size_t i = 0; i < N; i++) {
    res += static_cast<char>('0' + v[i]);
  }
  return res;
}

template <typename A>
string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}

template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")";
}

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}

void debug_out() { cout << endl; }

template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cout << " " << to_string(H);
  debug_out(T...);
}

#define debug(...) cout << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll n,x,t,m,y,z;
    string s;
    cin>>s;
    for(auto it:s){
        boo[it-'a']=true;
    }
    cin>>s;
    cin>>t;
    bool ok = false;
    for(auto it:s){
        if(it=='*'){
            ok=true;
        }
    }
    string q;
    while(t--){
        cin>>q;
        n = q.size();
        if(!ok){
            if(n!=s.size()){
                cout<<"NO\n";
                continue;
            }
            bool foo = true;
            for(int i=0;i<n;i++){
                if(s[i]!=q[i]){
                    if(((s[i]=='?')&&(!boo[q[i]-'a']))||(s[i]!='?')){
                        foo = false;

                        break;
                    }
                }
            }
            if(foo){
                cout<<"YES\n";
            }
            else{
                cout<<"NO\n";
            }
            continue;
        }
        else{
            if(n<(s.size()-1)){
                cout<<"NO\n";
                continue;
            }
            string s1,s2,s3;
            bool foo = true;
            for(int i=0;s[i]!='*';i++){
                if(s[i]!=q[i]){
                    if(((s[i]=='?')&&(!boo[q[i]-'a']))||(s[i]!='?')){
                        foo = false;
                        break;
                    }
                }
                s1.pb(q[i]);
            }
            reverse(all(s));
            reverse(all(q));
            for(int i=0;s[i]!='*';i++){
                if(s[i]!=q[i]){
                    if(((s[i]=='?')&&(!boo[q[i]-'a']))||(s[i]!='?')){
                        foo = false;
                        break;
                    }
                }
                s2.pb(q[i]);
            }
            reverse(all(s));
            reverse(all(q));
            reverse(all(s2));
            for(int i=0;i<n-s1.size()-s2.size();i++){
                s3.pb('#');
            }
            s1 = s1+s3+s2;
            for(int i=0;i<n;i++){
                if((s1[i]=='#')&&(boo[q[i]-'a']))foo = false;
            }

            if(foo){
                cout<<"YES\n";
            }
            else{
                cout<<"NO\n";
            }
            continue;
        }
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
//long long a[10000];

int main()
{
    cin.sync_with_stdio(false);
    long long i,j,k,l,d,c,b,v,nb[5003],bb[5003],n,m;
    string s,s1,s2,s3;
    while(cin>>s)
    {
        map<char,ll>mp;
        mp.clear();
        for(i=0; i<s.size(); i++)
        {
            mp[s[i]]=1;
        }
        cin>>s1;
        cin>>m;
        while(m--)
        {
            cin>>s2;
            n=s1.size();
            k=s2.size();
            c=0;
            for(i=0; i<k; i++)
            {
                if(s1[i]=='*')
                {
                    c=1;
                    break;
                }
                else if(s1[i]=='?')
                {
                    if(mp[s2[i]]==0)
                    {
                        c=2;
                        cout<<"NO\n";
                        break;
                    }
                }
                else if(s1[i]!=s2[i])
                {
                    cout<<"NO\n";
                    c=2;
                    break;
                }

            }
            if(c==1)
            {
                l=i;
                c=0;
                for(i=k-1,j=n-1; j>=0; i--,j--)
                {
                    if(s1[j]=='*')
                    {
                        c=1;
                        break;
                    }
                    else if(s1[j]=='?')
                    {
                        if(mp[s2[i]]==0)
                        {
                            c=2;
                            cout<<"NO\n";
                            break;
                        }
                    }
                    else if(s1[j]!=s2[i])
                    {
                        c=2;
                        cout<<"NO\n";
                        break;
                    }

                }
                if(c==1)
                {
                    c=0;
                    for(k=l; k<=i; k++)
                    {
                        if(mp[s2[k]]==1)
                        {
                            c=1;
                            break;
                        }
                    }
                    if(c==0)
                        cout<<"YES\n";
                    else
                        cout<<"NO\n";
                }
            }
            else if(c==0)
                cout<<"YES\n";

        }

    }
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
//long long a[10000];

int main()
{
    cin.sync_with_stdio(false);
    long long i,j,k,l,d,c,b,v,nb[5003],bb[5003],n,m;
    string s,s1,s2,s3;
    while(cin>>s)
    {
        map<char,ll>mp;
        mp.clear();
        for(i=0; i<s.size(); i++)
        {
            mp[s[i]]=1;
        }
        cin>>s1;
        cin>>m;
        while(m--)
        {
            cin>>s2;
            n=s1.size();
            k=s2.size();
            if(k<n)
                cout<<"No"<<endl;
            else{
            c=0;
            for(i=0; i<k; i++)
            {
                if(s1[i]=='*')
                {
                    c=1;
                    break;
                }
                else if(s1[i]=='?')
                {
                    if(mp[s2[i]]==0)
                    {
                        c=2;
                        cout<<"NO\n";
                        break;
                    }
                }
                else if(s1[i]!=s2[i])
                {
                    cout<<"NO\n";
                    c=2;
                    break;
                }

            }
            if(c==1)
            {
                l=i;
                c=0;
                for(i=k-1,j=n-1; j>=0; i--,j--)
                {
                    if(s1[j]=='*')
                    {
                        c=1;
                        break;
                    }
                    else if(s1[j]=='?')
                    {
                        if(mp[s2[i]]==0)
                        {
                            c=2;
                            cout<<"NO\n";
                            break;
                        }
                    }
                    else if(s1[j]!=s2[i])
                    {
                        c=2;
                        cout<<"NO\n";
                        break;
                    }

                }
                if(c==1)
                {
                    c=0;
                    for(k=l; k<=i; k++)
                    {
                        if(mp[s2[k]]==1)
                        {
                            c=1;
                            break;
                        }
                    }
                    if(c==0)
                        cout<<"YES\n";
                    else
                        cout<<"NO\n";
                }
            }
            else if(c==0)
                cout<<"YES\n";

        }
        }
    }
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
//long long a[10000];

int main()
{
    cin.sync_with_stdio(false);
    long long i,j,k,l,d,c,b,v,nb[5003],bb[5003],n,m;
    string s,s1,s2,s3;
    while(cin>>s)
    {
        map<char,ll>mp;
        mp.clear();
        for(i=0; i<s.size(); i++)
        {
            mp[s[i]]=1;
        }
        cin>>s1;
        cin>>m;
        n=s1.size();
        v=0;
        for(i=0; i<n; i++)
        {
            if(s1[i]=='*')
            {
                v=1;
                break;
            }
        }
        while(m--)
        {
            cin>>s2;
            k=s2.size();

            if(v==1&&k<(n-1)||v==0&&k<(n))
                cout<<"NO"<<endl;
            else
            {
                c=0;
                for(i=0; i<k; i++)
                {
                    if(s1[i]=='*')
                    {
                        c=1;
                        break;
                    }
                    else if(s1[i]=='?')
                    {
                        if(mp[s2[i]]==0)
                        {
                            c=2;
                            cout<<"NO\n";
                            break;
                        }
                    }
                    else if(s1[i]!=s2[i])
                    {
                        cout<<"NO\n";
                        c=2;
                        break;
                    }

                }
                if(c==1)
                {
                    l=i;
                    c=0;
                    for(i=k-1,j=n-1; j>=0; i--,j--)
                    {
                        if(s1[j]=='*')
                        {
                            c=1;
                            break;
                        }
                        else if(s1[j]=='?')
                        {
                            if(mp[s2[i]]==0)
                            {
                                c=2;
                                cout<<"NO\n";
                                break;
                            }
                        }
                        else if(s1[j]!=s2[i])
                        {
                            c=2;
                            cout<<"NO\n";
                            break;
                        }

                    }
                    if(c==1)
                    {
                        c=0;
                        for(k=l; k<=i; k++)
                        {
                            if(mp[s2[k]]==1)
                            {
                                c=1;
                                break;
                            }
                        }
                        if(c==0)
                            cout<<"YES\n";
                        else
                            cout<<"NO\n";
                    }
                }
                else if(c==0)
                    cout<<"YES\n";

            }
        }
    }
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
//long long a[10000];

int main()
{
    cin.sync_with_stdio(false);
    long long i,j,k,l,d,c,b,v,nb[5003],bb[5003],n,m;
    string s,s1,s2,s3;
    while(cin>>s)
    {
        map<char,ll>mp;
        mp.clear();
        for(i=0; i<s.size(); i++)
        {
            mp[s[i]]=1;
        }
        cin>>s1;
        cin>>m;
        while(m--)
        {
            cin>>s2;
            n=s1.size();
            k=s2.size();
            c=0;
            for(i=0; i<k; i++)
            {
                if(s1[i]=='*')
                {
                    c=1;
                    break;
                }
                else if(s1[i]=='?')
                {
                    if(mp[s2[i]]==0)
                    {
                        cout<<"NO";
                        break;
                    }
                }
                else if(s1[i]!=s2[i])
                {
                    cout<<"NO";
                    break;
                }

            }
            if(c==1)
            {
                l=i;
                c=0;
                for(i=k-1,j=n-1;j>=0; i--,j--)
                {
                    if(s1[j]=='*')
                    {

                        break;
                    }
                    else if(s1[j]=='?')
                    {
                        if(mp[s2[i]]==0)
                        {
                            cout<<"NO";
                            break;
                        }
                    }
                    else if(s1[j]!=s2[i])
                    {
                        cout<<"NO";
                        break;
                    }

                }
                c=0;
                for(k=l;k<=i;k++)
                {
                    if(mp[s2[k]]==1)
                    {
                        c=1;
                        break;
                    }
                }
                if(c==0)
                    cout<<"YES";
                else
                    cout<<"NO";
            }
            else
                cout<<"YES";

        }

    }
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
//long long a[10000];

int main()
{
    cin.sync_with_stdio(false);
    long long i,j,k,l,d,c,b,v,nb[5003],bb[5003],n,m;
    string s,s1,s2,s3;
    while(cin>>s)
    {
        map<char,ll>mp;
        mp.clear();
        for(i=0; i<s.size(); i++)
        {
            mp[s[i]]=1;
        }
        cin>>s1;
        cin>>m;
        while(m--)
        {
            cin>>s2;
            n=s1.size();
            k=s2.size();
            if(k<s1)
                cout<<"No"<<endl;
            else{
            c=0;
            for(i=0; i<k; i++)
            {
                if(s1[i]=='*')
                {
                    c=1;
                    break;
                }
                else if(s1[i]=='?')
                {
                    if(mp[s2[i]]==0)
                    {
                        c=2;
                        cout<<"NO\n";
                        break;
                    }
                }
                else if(s1[i]!=s2[i])
                {
                    cout<<"NO\n";
                    c=2;
                    break;
                }

            }
            if(c==1)
            {
                l=i;
                c=0;
                for(i=k-1,j=n-1; j>=0; i--,j--)
                {
                    if(s1[j]=='*')
                    {
                        c=1;
                        break;
                    }
                    else if(s1[j]=='?')
                    {
                        if(mp[s2[i]]==0)
                        {
                            c=2;
                            cout<<"NO\n";
                            break;
                        }
                    }
                    else if(s1[j]!=s2[i])
                    {
                        c=2;
                        cout<<"NO\n";
                        break;
                    }

                }
                if(c==1)
                {
                    c=0;
                    for(k=l; k<=i; k++)
                    {
                        if(mp[s2[k]]==1)
                        {
                            c=1;
                            break;
                        }
                    }
                    if(c==0)
                        cout<<"YES\n";
                    else
                        cout<<"NO\n";
                }
            }
            else if(c==0)
                cout<<"YES\n";

        }
        }
    }
}


#include <iostream>
#include <string>
#include <set>
#include <algorithm>
#include <cmath>

int main(int argc, char *argv[]) {
	std::string good_letter, pattern;
	std::set<char> letter_set;
	letter_set.clear();
	std::cin >> good_letter >> pattern;
	for(int i = 0; i < good_letter.length(); i++) {
		letter_set.insert(good_letter[i]);
	}
	int n;
	std::cin >> n;
	bool flag = 0;
	for(int i = 0; i < n; i++) {
		std::string s;
		s.clear();
		std::cin >> s;
		flag = 0;
		int len = s.length();
		if(pattern.length() - 1 > len) {
			std::cout << "NO\n";
			continue;
		}
		int j = 0;
		for(int i = 0; i < pattern.length(); i++) {
			//std::cout << i << " " << j << "\n";
			if(pattern[i] == '*') {
				int l = len - pattern.length();
				if(l >= 1) {
					int k = j;
					for(; k <= j + l; k++) {
						if(letter_set.count(s[k]) == 1) {
							std::cout << "NO\n";
							flag = 1;
							goto outside;
						}
					}
					i++;
					j += l;
					continue;
				}
			}
			if(pattern[i] == s[j]) {
				j++;
				continue;
			} else if(pattern[i] == '?' && letter_set.count(s[j]) == 1) {
				j++;
				continue;
			} else if(pattern[i] == '*' && letter_set.count(s[j]) == 0) {
				j++;
				continue;
			} else {
				std::cout << "no\n";
				flag = 1;
				break;
			}
		}
		outside:
		if(!flag) {
			std::cout << "YES\n";
		}
	}
	return 0;
}


#include <iostream>
#include <string>
#include <set>
#include <algorithm>
#include <cmath>

int main(int argc, char *argv[]) {
	std::string good_letter, pattern;
	std::set<char> letter_set;
	std::cin >> good_letter >> pattern;
	for(int i = 0; i < good_letter.length(); i++) {
		letter_set.insert(good_letter[i]);
	}
	int n;
	std::cin >> n;
	bool flag;
	for(int i = 0; i < n; i++) {
		std::string s;
		std::cin >> s;
		flag = 0;
		int len = s.length();
		if(pattern.length() - 1 > s.length()) {
			std::cout << "NO\n";
			continue;
		}
		int j = 0;
		for(int i = 0; i < len; i++) {
			//std::cout << i << " " << j << "\n";
			if(pattern[i] == '*') {
				int l = len - pattern.length();
				if(l >= 1) {
					int k;
					for(k = j; k <= j + l; k++) {
						if(letter_set.find(s[k]) == letter_set.end()) {
							continue;
						} else {
							std::cout << "NO\n";
							flag = 1;
							goto outside;
						}
					}
					i++;
					j += (l + 1);
				}
				continue;
			} else if(pattern[i] == s[j]) {
				j++;
				continue;
			} else if(pattern[i] == '?' && letter_set.find(s[j]) != letter_set.end()) {
				j++;
				continue;
			} else if(pattern[i] == '*' && letter_set.find(s[j]) == letter_set.end()) {
				j++;
				continue;
			} else {
				std::cout << "no\n";
				flag = 1;
				break;
			}
		}
		outside: if(!flag) {
			std::cout << "YES\n";
		}
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int n,s,sum,i,j,a[1010];
int main(){
	cin>>n>>s;
	for(i=1;i<=n;i++){
		cin>>a[i];
	}
	for(i=1;i<=n;i++){
		for(j=i+1;j<=n;j++){
			if((max(a[i],a[j])-min(a[i],a[j]))<=s){
				sum+=2;
			}
		}
	}
	cout<<sum;
	return 0;
}


#include<iostream>
#include<algorithm>
using namespace std;
int main(){

	int n,d,a[1001];
	int c=0;
	cin >> n >> d;
	for(int i=0;i<n;i++)
		cin >> a[i];
	for(int i=1;i<n;i++){
		for(int j=0;j<i;j++){
			if(abs(a[i]-a[j])<=d){
				c++;
			}
		}

	}
	cout << c*2;
}


#include<bits/stdc++.h>
using namespace std;
int n,d;
int a[1005];
int main(){
    scanf ("%d%d",&n,&d);
    for (int i=1;i<=n;i++)
        scanf ("%d",&a[i]);
    int ans=0;
    for (int i=1;i<=n;i++)
        for (int j=i+1;j<=n;j++)
            if (abs(a[i]-a[j])<=d) ans+=2;
    printf ("%d",ans);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n,d,a[1000005],ans;
int main()
{
    scanf("%d%d",&n,&d);
    for(int i=1;i<=n;i++)cin>>a[i];
    for(int i=1;i<=n;i++)
    	for(int j=i+1;j<=n;j++)
    		if(abs(a[i]-a[j])<=d)ans++;
    cout<<ans*2;
}


#include<iostream>
#include<cstdio>
using namespace std;
int a[1001],n,d,ans;
int main(){
    scanf("%d%d",&n,&d);
    for(int i=1;i<=n;i++)
        scanf("%d",&a[i]);
    for(int i=2;i<=n;i++)
        for(int j=1;j<i;j++)
            if(a[i]-a[j]<=d&&a[i]-a[j]>=-d)
                ans+=2;
    printf("%d\n",ans);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int a[1001];
int main()
{
	int n,z,i,cai=0,e;
	cin>>n>>z;
	for(i=1;i<=n;i++)	cin>>a[i];
	sort(a+1,a+1+n);
	for(i=2;i<=n;i++)
	{
		e=i;
		if(a[i]-a[i-1]<=z)
			while(a[i]-a[e-1]<=z)
			{
				cai++;
				e--;
			}
	}
	cout<<cai*2;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int const N=1000+10;
int a[N];
int main()
{
	int i,j,n,d;
	int s=0;
	cin>>n>>d;
	for(i=1;i<=n;i++)
		cin>>a[i];
	for(i=1;i<=n;i++)
		for(j=1;j<=n;j++)
		{
			if(i==j)
				continue;
			if(abs(a[i]-a[j])<=d)
				s++;
		}
	cout<<s<<endl;
	return 0;
}


#include<iostream>
#include<cstdio>
using namespace std;
int a[1001],n,d,ans;
int main(){
    scanf("%d%d",&n,&d);
    for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    for(int i=2;i<=n;i++)
        for(int j=1;j<i;j++)
            if(a[i]-a[j]<=d&&a[i]-a[j]>=-d) ans+=2;
    printf("%d\n",ans);
    return 0;
}


#include<iostream>
#include<cstdio>
using namespace std;
int a[1005],n,d,ans;
int main(){
    scanf("%d%d",&n,&d);
    for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    for(int i=2;i<=n;i++)
        for(int j=1;j<i;j++)
            if(a[i]-a[j]<=d&&a[i]-a[j]>=-d) ans++;
    printf("%d",ans*2);
    return 0;
}


#include<stdio.h>
#include<math.h>
int n,d,s[1001],cnt;
int main()
{
	scanf("%d%d",&n,&d);
	for(int i=0;i<n;i++)
		scanf("%d",&s[i]);
	for(int i=0;i<n;i++)
		for(int j=0;j<n;j++)if(std::abs(s[i]-s[j])<=d&&i!=j)cnt++;
	printf("%d",cnt);
}


#include <bits/stdc++.h>
using namespace std;
int n, a[1005], ans, d;
int main() {
    cin >> n >> d;
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j < i; j++) {
            if(abs(a[i] - a[j]) <= d) ans += 2;
        }
    }
    cout << ans;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n,d,a[1001],sum=0;
int main(){
	scanf("%d%d",&n,&d);
	for(int i=1;i<=n;++i)
		scanf("%d",&a[i]);
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			if(i==j)
				continue;
			if(abs(a[i]-a[j])<=d)
				sum++;
		}
	}
	cout<<sum<<endl;
	return 0;
}


#include<bits/stdc++.h>
#include<cstdio>
using namespace std;
int main(){
	int n,d,a[10001],sum=0;
	scanf("%d%d",&n,&d);
	for(int i=0;i<n;i++){
		scanf("%d",&a[i]);
	}
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			if(abs(a[i]-a[j])<d) sum+=2;
		}
	}
	cout<<sum<<endl;
	return 0;
}


#include<iostream>
using namespace std;
int main(){
    long long a[1001],n,d,ans;
    cin>>n>>d;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
    }
    for(int i=2;i<=n;i++)
    {
        for(int j=1;j<i;j++)
        {
            if(a[i]-a[j]<=d&&a[i]-a[j]>=-d)
            {
                ans+=2;
            }
        }
    }
    cout<<ans<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n,d;
int a[1005];
int main(){
    scanf ("%d%d",&n,&d);
    for (int i=1;i<=n;i++)
        scanf ("%d",&a[i]);
    int ans=0;
    for (int i=1;i<=n;i++)
        for (int j=i+1;j<=n;j++)
            if (i!=j&&abs(i-j)<=d) ans+=2;
    printf ("%d",ans);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int a[1002];
    int n,b,i=0,j=0,sum=0;
    cin>>n>>b;
    for(i=1;i<=n;i++) cin>>a[i];
    for(i=1;i<=n;i++)
      for(j=i+1;j<=n;j++)
        if (i!=j && abs(a[i]-a[j])<=b) sum+=2;
    cout<<sum;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int a[1005];
int main(){
    int n,d,ans=0; cin>>n>>d;
    for (int i=1;i<=n;i++) cin>>a[i];
    for (int i=2;i<=n;i++)
    for (int j=1;j<i;j++)
    if (abs(a[i]-a[j])<=d) ans+=3;
    cout<<ans;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main(){
	int n, d, cnt = 0;
	cin >> n;
	int a[n+10];
	for(int i=1 ; i<=n ; i++){
		for(int j=1 ; j<=n ; j++){
			if ( abs(a[i]-a[j]) <= d)
				cnt ++;
		}
	}
	cout << cnt;
}


#include<iostream>
using namespace std;
int a[1050],n,m,ans;
int main()
{
    cin>>n>>m;
    for(int i=1;i<=n;i++)
		cin>>a[i];
    for(int i=2;i<=n;i++)
        for(int j=1;j<i;j++){
            if(a[i]-a[j]<=m&&a[i]-a[j]>=-m)
				ans+=2;
		}
    cout<<ans;
    return 0;
}


#include<iostream>
using namespace std;
int n,d,sum=0,a[1001];
int main()
{
    cin>>n>>d;
    for (int i=1; i<=n; i++) cin>>a[i];
    for(int i=1; i<=n; i++)
    	for(int j=1; j<i; j++)
    		if (a[i]-a[j]<=d&&a[i]-a[j]>=-d) sum+=2;
    cout<<sum<<'\n';
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
long long n,a[1010],c[1010],d;
int main()
{
	scanf("%d%d",&n,&d);
	for(int i=1;i<=n;++i)
	{
		scanf("%d",&a[i]);
		c[i]+=a[i];
	}
	sort(a+1,a+n+1);
	int ans=0;
	for(int i=1;i<n;i++)
	{
		for(int j=i+1;j<=n;++j)
		{
			if(c[j]-c[i]<=d)ans+=2;
		}
	}
	cout<<ans;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int a[1005],n,d,ans;
int main(){
    scanf("%d%d",&n,&d);
    for (int i=1;i<=n;i++) scanf("%d",&a[i]);
    for (int i=2;i<=n;i++)
    for (int j=1;j<i;j++) if (abs(a[i]-a[j])<=d) ans+=2;
    printf("%d\n",ans);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n,d,a[1005],ans;
int main()
{
	cin>>n>>d;
	for(int i=0;i<n;i++)
		cin>>a[i];
	for(int i=0;i<n;i++)
		for(int j=i+1;j<n;j++)
			if(max(a[i]-a[j],a[j]-a[i])<=d)ans++;
	cout<<ans*2<<endl;
	return 0;
}


#include<iostream>
#include<cstdio>
using namespace std;
int a[1001],n,d,ans;
int main(){
    scanf("%d%d",&n,&d);
    for(int i=1;i<=n;i++)
    scanf("%d",&a[i]);
    for(int i=2;i<=n;i++)
    for(int j=1;j<i;j++)
        if(a[i]-a[j]<=d&&a[i]-a[j]>=-d) ans+=2;
    printf("%d\n",ans);
    return 0;


#include<bits/stdc++.h>
using namespace std;
int a[1001],n,d,ans;
int main(){
    scanf("%d%d",&n,&d);
    for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    for(int i=2;i<=n;i++)
        for(int j=1;j<i;j++)
            if(a[i]-a[j]<=d&&a[i]-a[j]>=-d) ans+=2;
    printf("%d\n",ans);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main(){
	int n, d, cnt = 0;
	cin >> n >> d;
	int a[n+10];
	for(int i=1; i <= n ;i++)
		cin >> a[i];
	for(int i=1 ; i<=n ; i++){
		for(int j=1 ; j<=n ; j++){
			if ( abs(a[i]-a[j]) <= d)
				cnt ++;
		}
	}
	cout << cnt - n ;
}


#include<bits/stdc++.h>
#include<cstdio>
using namespace std;
int main(){
	int n,d,a[10001],sum=0;
	scanf("%d%d",&n,&d);
	for(int i=0;i<n;i++){
		scanf("%d",&a[i]);
	}
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			if(abs(a[i]-a[j])<d&&i!=j) sum+=2;
		}
	}
	cout<<sum<<endl;
	return 0;
}


#include<stdio.h>

int n,d;
int a[1005];
int ans;

int main() {
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);
	scanf("%d %d",&n,&d);
	for(register int i=1; i<=n; scanf("%d",a+i++));
	for(register int i=2; i<=n; ++i) {
		for(register int j=1; j<i; ++j) {
			if(a[i]-a[j]<=d&&a[i]-a[j]>=-d)
				ans+=2;
		}
	}
	printf("%d\n",ans);
	return 0;
}


#include <iostream>
#include <cmath>
using namespace std;
int main()
{
	int n,d;
	cin>>n>>d;
	int a[n];
	int t=0;
	for(int i=0;i<n;i++){
		cin>>a[i];
	}
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			if(abs(a[i]-a[j])<=d)t++;
		}
	}
	cout<<t-n;
}


#include<bits/stdc++.h>
using namespace std;
int n,d,a[1010],ans,s[1010];
bool p[1010];
void dfs(int t) {
	if(t>2) {
		ans++;
		return;
	}
	for(int i=1; i<=n; i++) {
		s[0]=a[i];
		if(abs(a[i]-s[t-1])<=d&&!p[i]) {
			p[i]=1;
			s[t]=a[i];
			dfs(t+1);
			p[i]=0;
		}
	}
}
int main() {
	cin>>n>>d;
	for(int i=1; i<=n; i++) {
		cin>>a[i];
	}
	dfs(1);
	cout<<ans<<endl;
	return 0;
}


#include<cstdio>
#include<algorithm>
#define abs(a) (a<0 ? -a : a)

using namespace std;

bool cmp(int a,int b){return a>b;}

int main(){
    short n;
    int d,x[1000],i,j,ans;
    scanf("%d%d",&n,&d);
    for(i=0;i<n;i++) scanf("%d",x+i);

    sort(x,x+n,cmp);

    for(i=0;i<n-1;i++){
        for(j=i;abs(x[i]-x[j])<=d;j++){
            ans+=2;
        }
    }

    printf("%d\n",ans);
    return 0;
}


#include<cstdio>
#include<algorithm>

using namespace std;

bool cmp(int a,int b){return a>b;}

int main(){
    short n;
    int d,x[1000],i,j,ans;
    scanf("%d%d",&n,&d);
    for(i=0;i<n;i++) scanf("%d",x+i);

    sort(x,x+n,cmp);

    for(i=1;i<n;i++){
        for(j=0;j<i;j++){
            if(x[i]-x[j]<=d&&x[i]-x[j]>=-d) ans+=2;
        }
    }

    printf("%d\n",ans);
    return 0;
}


#include<cstdio>
#include<algorithm>

using namespace std;

int main(){
    int n,d,x[1000],i,j,ans=0;
    scanf("%d%d",&n,&d);
    for(i=0;i<n;i++) scanf("%d",x+i);

    for(i=1;i<n;i++){
        for(j=0;j<i;j++){
            if(x[i]-x[j]<=d&&x[i]-x[j]>=-d) ans+=2;
        }
    }

    printf("%d\n",ans);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
          int n,d,sum=0;
          cin>>n>>d;
          int a[n];
          for(int i=0;i<n;i++)
                    cin>>a[i];
          sort(a,a+n);
          for(int i=0;i<n;i++)
          {
                    for(int j=i+1;j<n;j++)
                    {
                              if(abs(a[i]-a[j]<=d))
                                        sum++;
                              else
                                        break;
                    }

          }
          cout<<sum*2;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
          int n,d,sum=0;
          cin>>n>>d;
          int a[n];
          for(int i=0;i<n;i++)
                    cin>>a[i];
          sort(a,a+n);
          for(int i=0;i<n-1;i++)
          {
                    if(a[i+1]-a[i]<=d)
                              sum+=2;
          }
          cout<<sum;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
          int n,d,sum=0;
          cin>>n>>d;
          int a[n];
          for(int i=0;i<n;i++)
                    cin>>a[i];
          sort(a,a+n);
          for(int i=0;i<n;i++)
          {
                    for(int j=i+1;j<n;j++)
                    {
                              if(abs(a[i]-a[j])<=d)
                                        sum++;
                              else
                                        break;
                    }

          }
          cout<<sum*2;
}


#include <iostream>
#include <cmath>
using namespace std;

int main()
{
    int *solarray,i,j,soldierscount,maxheightdiff,wayscount=0;
    cin >> soldierscount >> maxheightdiff;
    solarray = new int[soldierscount];
    for (i=0;i<soldierscount;i++)
      cin >> solarray[i];
    for (i=0;i<soldierscount;i++)
      for (j=0;j<soldierscount;j++)
        if ((i!=j) && (abs(solarray[i]-solarray[j])<=maxheightdiff))
          wayscount++;
    delete solarray;
    cout << wayscount;
    return 0;
}


#include <iostream>
#include <cmath>
using namespace std;

int main()
{
    int *solarray,i,j,soldierscount,maxheightdiff,wayscount=0;
    cin >> soldierscount >> maxheightdiff;
    solarray = new int[soldierscount];
    for (i=0;i<soldierscount;i++)
      cin >> solarray[i];

    for (i=0;i<soldierscount;i++)
      for (j=i+1;j<soldierscount;j++)
        if ((i!=j) && (abs(solarray[i]-solarray[j])<=maxheightdiff))
          wayscount += 2;
    delete solarray;
    cout << wayscount;
    return 0;
}


#include <bits/stdc++.h>
#include<math.h>
using namespace std;

#define ll long long

ll x,y,z;
string s1,s2,s;

void memset_ll(ll a[], ll v, ll n)
{
    for(ll i=0; i<n; i++)
        a[i]=v;
}
void memset_s(char a[], char v, ll n)
{
    for(ll i=0; i<n; i++)
        a[i]=v;
}
int compare(const void * a, const void * b)
{
    return ( *(ll*)a - *(ll*)b );
}
void q_sort(ll a[], ll n)
{
    qsort(a, n, sizeof(ll), compare);
}

static int compare1 (const void * a, const void * b)
{
    if (*(double*)a > *(double*)b)
        return 1;
    else if (*(double*)a < *(double*)b)
        return -1;
    else
        return 0;
}
void q_sort_d(double a[], ll n)
{
    qsort(a, n, sizeof(double), compare1);
}

ll sum(ll n)
{
    ll i=0,a[6],j,k=0;
    while(n>0)
    {
        a[i]=n%10;
        n/=10;
        i++;
    }
    for(j=0; j<i; j++)
        k+=a[j];
    return k;
}
string trans_upper(string ss)
{
    transform(ss.begin(),ss.end(),ss.begin(), ::toupper);
    return ss;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL) ;
    cout.tie(NULL) ;

    ll t,l,p,q,r,i,j,h,k,e=0,f=0,g=0;
    ll temp=0,n,m,d=0,c,nl,np;
    cin>>n>>d;
    ll a[n];
    for(i=0;i<n;i++)
        cin>>a[i];
    q_sort(a,n);
    for(i=0;i<n-1;i++)
    {
        for(j=i+1;j<n;j++){
        p=fabs(a[i]-a[j]);
        if(p<=d)
            e++;}
    }
    f=2*e;
    cout<<f;

    return 0;
}


#include <bits/stdc++.h>
#include<math.h>
using namespace std;

#define ll long long

ll x,y,z;
string s1,s2,s;

void memset_ll(ll a[], ll v, ll n)
{
    for(ll i=0; i<n; i++)
        a[i]=v;
}
void memset_s(char a[], char v, ll n)
{
    for(ll i=0; i<n; i++)
        a[i]=v;
}
int compare(const void * a, const void * b)
{
    return ( *(ll*)a - *(ll*)b );
}
void q_sort(ll a[], ll n)
{
    qsort(a, n, sizeof(ll), compare);
}

static int compare1 (const void * a, const void * b)
{
    if (*(double*)a > *(double*)b)
        return 1;
    else if (*(double*)a < *(double*)b)
        return -1;
    else
        return 0;
}
void q_sort_d(double a[], ll n)
{
    qsort(a, n, sizeof(double), compare1);
}

ll sum(ll n)
{
    ll i=0,a[6],j,k=0;
    while(n>0)
    {
        a[i]=n%10;
        n/=10;
        i++;
    }
    for(j=0; j<i; j++)
        k+=a[j];
    return k;
}
string trans_upper(string ss)
{
    transform(ss.begin(),ss.end(),ss.begin(), ::toupper);
    return ss;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL) ;
    cout.tie(NULL) ;

    ll t,l,p,q,r,i,j,h,k,e=0,f=0,g=0;
    ll temp=0,n,m,d=0,c,nl,np;
    cin>>n>>d;
    ll a[n];
    for(i=0;i<n;i++)
        cin>>a[i];
    q_sort(a,n);
    for(i=0;i<n-1;i++)
    {
        p=fabs(a[i]-a[i+1]);
        if(p<=d)
            e++;
    }
    f=2*e;
    cout<<f;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int a[1001];
    int n,i,j,k,cnt=0,x,d;
    cin>>n>>d;
    for(i=0; i<n; i++)
    {
        cin>>a[i];
    }
    for(i=0; i<n; i++)
    {
        for(j=0; j<n; j++)
        {
            if(j==i)
            {
                j++;
            }

            x=a[i]-a[j];
            if(x>=(-1)*d&&x<=d)
            {
                cnt++;
            }
        }
    }
    cout<<cnt;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int a[1001];
    int n,i,j,k,cnt=0,x,d;
    cin>>n>>d;
    for(i=0; i<n; i++)
    {
        cin>>a[i];
    }
    sort(a,a+n);
    for(i=0; i<n; i++)
    {
        for(j=0; j<n; j++)
        {
            if(j==i)
            {
                j++;
            }

            x=a[j]-a[i];
            if(x<=d)
            {
                cnt++;
            }
        }
    }
    cout<<cnt;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int a[1001];
    int n,i,j,k,cnt=0,x,d;
    cin>>n>>d;
    for(i=0;i<n;i++)
    {
        cin>>a[i];
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
        {
            if(j==i)
            {
                j++;
            }
            x=a[i]-a[j];
            if(x>=(-1)*d&&x<=d)
            {
                cnt++;
            }
        }
    }
    cout<<cnt;
}


#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cmath>
#include<cstring>
using namespace std;
int a[1005];
int main()
{
    int n,d,ans=0; cin>>n>>d;
    for (int i=1;i<=n;i++) cin>>a[i];
    for (int i=2;i<=n;i++)
    for (int j=1;j<i;j++)
    if (abs(a[i]-a[j])<=d) ans+=2;
    cout<<ans;
    return 0;
}


#include<iostream>
using namespace std;
int main(){
	int a[1010],n,d,ans;
    cin>>n>>d;
    for(int i=1;i<=n;i++) cin>>a[i];
    for(int i=2;i<=n;i++)
        for(int j=1;j<i;j++)
            if(a[i]-a[j]<=d&&a[i]-a[j]>=-d) ans+=2;
    cout<<ans;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int a[1001],n,d,ans;
int main()
{
    cin>>n>>d;
    for(int i=1;i<=n;i++)
	cin>>a[i];
    for(int i=2;i<=n;i++)
	{
		for(int j=1;j<i;j++)
		{
		if(a[i]-a[j]<=d&&a[i]-a[j]>=-d)
		ans+=2;
		}
	}
    cout<<ans;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int a[1001];
int main()
{
	int n,z,i,cai=0;
	cin>>n>>z;
	for(i=1;i<=n;i++)	cin>>a[i];
	sort(a+1,a+1+n);
	for(i=2;i<=n;i++)
	{
		if(a[i]-a[i-1]<=z)	cai++;
	}
	cout<<cai*2;
	return 0;
}


#include<iostream>
using namespace std;
typedef long long ll;
int w[1005];
int abs(int e)
{
	return e > 0 ? e : -e;
}
int main(void)
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	int n, d, cnt = 0;
	cin>>n>>d;
	for(int i = 0; i < n; i++)
		cin>>w[i];
	for(int i = 0; i < n; i++)
		for(int j = 0; j < n; j++)
			if (i != j && abs(w[i]-w[j]) <= d) cnt++;
	cout<<cnt;
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main(){
	ll n,d,ans=0;
	cin>>n>>d;
	int a[n+10];
	for(int i=0;i<n;i++){
		cin>>a[i];
	}
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			if(i!=j&&abs(a[i]-a[j])<=d){
				ans++;
			}
		}
	}
	cout<<ans<<endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int a[1002];
    int n,b,i=0,j=0,sum=0;
    cin>>n>>b;
    for(i=1;i<=n;i++)
      for(j=1;j<=n;j++)
        if (i!=j && abs(a[i]-a[j])>=b) sum++;
    cout<<sum;
    return 0;
}


#include<iostream>
#include<cmath>
using namespace std;
int n,m,s,a[10010];
int main()
{
	cin>>n>>m;
	for(int i=1;i<=n;i++) cin>>a[i];
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)
			if(i!=j&&abs(a[i]-a[j])<=m) s++;
	cout<<s;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int a[1001];
int main()
{
	int n,m,s=0;
	cin>>n>>m;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
	}
	for(int i=2;i<=n;i++)
	{
		for(int j=1;j<i;j++)
		{
			if(abs(a[i]-a[j])<=m)s++;
		}
	}
	cout<<s*2;
	return 0;
}


#include<iostream>
using namespace std;
typedef long long ll;
int w[105];
int abs(int e)
{
	return e > 0 ? e : -e;
}
int main(void)
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	int n, d, cnt = 0;
	cin>>n>>d;
	for(int i = 0; i < n; i++)
		cin>>w[i];
	for(int i = 0; i < n; i++)
		for(int j = 0; j < n; j++)
			if (i != j && abs(w[i]-w[j]) <= d) cnt++;
	cout<<cnt;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int a[1002];
    int n,b,i=0,j=0,sum=0;
    cin>>n>>b;
    for(i=1;i<=n;i++) cin>>a[i];
    for(i=1;i<=n;i++)
      for(j=i+1;j<=n;j++)
        if (i!=j && abs(a[i]-a[j])>=b) sum+=2;
    cout<<sum;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n,m,s=0,a[1001];
int main()
{
    cin>>n>>m;
    for(int i=1;i<=n;i++)
    cin>>a[i];
    for(int i=1;i<=n;i++)
    for(int j=1;j<=n;j++)
    if(i-j&&abs(a[i]-a[j])<=m) s++;
    cout<<s;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int n, a[1010], ans, d;
int main() {
	cin >> n >> d;
	for (register int i = 1; i <= n; ++i) scanf("%d", a + i);
	for (register int i = 1; i <= n; ++i)
		for (register int j = 1; j <= n; ++j)
			if (j != i && abs(a[i] - a[j]) <= d) ++ans;
	cout << ans;
}


#include <bits/stdc++.h>
using namespace std;
int i,j,a[1001],ans=0;
int main()
{
	int n,d;
	cin>>n>>d;
	for(i=1;i<=n;i++)
	cin>>a[i];
	for(i=2;i<=n;i++)
	{
		for(j=1;j<i;j++)
		if(a[i]-a[j]<=d&&a[i]-a[j]>=-d)
		ans+=2;
	}
	cout<<ans;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int const N=1000+10;
int a[N];
int main()
{
	int n,d,i,b=0;
	cin>>n>>d;
	for(i=1;i<=n;i++) cin>>a[i];
	sort(a+1,a+n+1);
	for(i=1;i<n;i++)
	for(int j=i+1;j<=n;j++)
	if(a[i]+d>=a[j]){ b=b+2;
	}
	cout<<b<<endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n,d;
int a[1005];
int main(){
    scanf ("%d%d",&n,&d);
    for (int i=1;i<=n;i++)
        scanf ("%d",a+i);
    sort (a+1,a+n+1);
    int ans=0;
    for (int i=1;i<=n;i++)
        for (int j=1;j<=n;j++)
            if (i!=j&&abs(i-j)<=d) ans++;
    printf ("%d",ans);
    return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main(){
	ll n,d,ans=0;
	cin>>n>>d;
	int a[n+10];
	for(int i=0;i<n;i++){
		cin>>a[i];
	}
	sort(a,a+n);
	for(int i=0;i<n-1;i++){
		if(abs(a[i+1]-a[i])<=d){
			ans+=2;
		}
	}
	cout<<ans<<endl;
	return 0;
}