#include <bits/stdc++.h>
using namespace std;
const double PI = 2.0 * acos(0.0);
const int MOD = 1e9 + 7;
const int oo = MOD << 1;
long long gcd(long long x, long long y) { return !y ? x : gcd(y, x % y); }
int main() {
  string good, ptr;
  int n;
  string str;
  cin >> good >> ptr >> n;
  int idx = -1;
  bool nc[26] = {};
  for (int i = 0; i < (int)good.size(); i++) nc[good[i] - 'a'] = 1;
  for (int i = 0; i < (int)ptr.size(); i++)
    if (ptr[i] == '*') idx = i;
  while (n-- && cin >> str) {
    if (idx == -1) {
      if ((int)str.size() != (int)ptr.size())
        puts("NO");
      else {
        bool f = 1;
        for (int i = 0; i < (int)str.size(); i++)
          if (isalpha(ptr[i]) && ptr[i] != str[i]) {
            f = 0;
            break;
          }
        puts(f ? "YES" : "NO");
      }
    } else if ((int)ptr.size() > (int)str.size() + 1)
      puts("NO");
    else {
      bool f = 1;
      int l = 0, r = (int)str.size() - 1, it = 0;
      while (it < (int)ptr.size() && l < (int)str.size() && ptr[it] != '*') {
        if (isalpha(ptr[it]) && ptr[it] != str[l] ||
            !isalpha(ptr[it]) && !nc[str[l] - 'a']) {
          f = 0;
          break;
        }
        ++it, ++l;
      }
      it = (int)ptr.size() - 1;
      while (it > -1 && r > -1 && ptr[it] != '*') {
        if (isalpha(ptr[it]) && ptr[it] != str[r] ||
            !isalpha(ptr[it]) && !nc[str[r] - 'a']) {
          f = 0;
          break;
        }
        --it, --r;
      }
      for (int i = l; i < r + 1; i++)
        if (nc[str[i] - 'a']) f = 0;
      puts(f ? "YES" : "NO");
    }
  }
  return 0;
}
