#include <bits/stdc++.h>
using namespace std;
class dop {
 public:
  string g, s, v;
  long long int n, p, q;
  bool y, z;
  void get();
};
void dop::get() {
  cin >> g >> s;
  vector<bool> a(26, false);
  for (long long int i = 0; i < g.length(); i++) {
    a[g[i] - 'a'] = true;
  }
  cin >> n;
  y = true;
  for (long long int j = 0; j < s.length(); j++) {
    if (s[j] == '*') {
      p = j;
      y = false;
      break;
    }
  }
  for (long long int i = 0; i < n; i++) {
    cin >> v;
    z = false;
    if (y) {
      if (v.length() != s.length()) {
        cout << "NO" << '\n';
        continue;
      }
      for (long long int j = 0; j < v.length(); j++) {
        if (s[j] == '?') {
          if (!a[v[j] - 'a']) {
            z = true;
            cout << "NO" << '\n';
            break;
          }
        } else if (s[j] != v[j]) {
          z = true;
          cout << "NO" << '\n';
          break;
        }
      }
      if (z) {
        continue;
      }
      cout << "YES" << '\n';
      continue;
    } else {
      if (v.length() < s.length() - 1) {
        cout << "NO" << '\n';
        continue;
      }
      for (long long int j = 0; j < p; j++) {
        if (s[j] == '?') {
          if (!a[v[j] - 'a']) {
            z = true;
            cout << "NO" << '\n';
            break;
          }
        } else if (s[j] != v[j]) {
          z = true;
          cout << "NO" << '\n';
          break;
        }
      }
      q = v.length() - 1;
      if (z) {
        continue;
      }
      for (long long int j = s.length() - 1; j > p; j--) {
        if (s[j] == '?') {
          if (!a[v[q] - 'a']) {
            z = true;
            cout << "NO" << '\n';
            break;
          }
        } else if (s[j] != v[q]) {
          z = true;
          cout << "NO" << '\n';
          break;
        }
        q--;
      }
      if (z) {
        continue;
      }
      for (long long int j = p; j <= q; j++) {
        if (a[v[j] - 'a']) {
          z = true;
          cout << "NO" << '\n';
          break;
        }
      }
      if (z) {
        continue;
      }
      cout << "YES" << '\n';
      continue;
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int t = 1;
  dop obj[t];
  for (long long int i = 0; i < t; i++) {
    obj[i].get();
  }
  return 0;
}
