#include <bits/stdc++.h>
using namespace std;
void solve() {
  long long int i, j, k, l, m, n, o, t;
  string second, p, q;
  cin >> q;
  map<char, long long int> mp;
  for (auto c : q) {
    mp[c]++;
  }
  cin >> p;
  long long int x = 0;
  for (auto c : p) {
    if (c != '*') {
      x++;
    }
  }
  cin >> t;
  n = p.length();
  long long int flag = (x == p.length());
  while (t--) {
    cin >> second;
    m = second.length();
    i = 0;
    j = 0;
    if (x > m) {
      cout << "NO\n";
      continue;
    }
    long long int ans = 1;
    while (i < n && p[i] != '*' && j < m) {
      if (p[i] == '?') {
        if (!mp[second[j]]) {
          break;
        }
      } else {
        if (second[j] != p[i]) {
          break;
        }
      }
      i++;
      j++;
    }
    if (flag == 1) {
      if (i == n && j == m) {
        cout << "YES\n";
      } else {
        cout << "NO\n";
      }
      continue;
    }
    k = n - 1;
    l = m - 1;
    while (k >= 0 && p[k] != '*' && l >= 0) {
      if (p[k] == '?') {
        if (!mp[second[l]]) {
          break;
        }
      } else {
        if (second[l] != p[k]) {
          break;
        }
      }
      k--;
      l--;
    }
    if (i > k) {
      ans &= j > l;
    } else
      ans &= (k == i && p[i] == '*');
    for (; j <= l; j++) {
      ans &= (!mp[second[j]]);
    }
    if (ans) {
      cout << "YES\n";
    } else {
      cout << "NO\n";
    }
  }
}
int main() {
  long long int t = 1, i = 1;
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  while (i <= t) {
    solve();
    i++;
  }
  return 0;
}
