#include <bits/stdc++.h>
using namespace std;
int main() {
  string g, p;
  int n;
  cin >> g >> p >> n;
  set<char> mys;
  for (int i = 0; i <= 25; i++) {
    mys.insert('a' + i);
  }
  set<char> myg;
  for (int i = 0; i < g.size(); i++) {
    myg.insert(g[i]);
  }
  for (int i = 0; i < g.size(); i++) {
    char t;
    t = g[i];
    if (mys.find(t) != mys.end()) {
      mys.erase(mys.find(t));
    }
  }
  int len = p.size();
  int count = 0;
  for (int i = 0; i < p.size(); i++) {
    if (p[i] == '*') count++;
  }
  while (n--) {
    string t;
    cin >> t;
    int l = t.size();
    int diff = l - len;
    if (diff < -1 || (diff < 0 && count == 0)) {
      cout << "NO"
           << "\n";
      continue;
    }
    if (diff == -1) {
      int j = 0;
      bool flag = true;
      for (int i = 0; i < l; i++) {
        if (p[j] == t[i]) {
          j++;
          continue;
        } else if (p[j] == '?') {
          if (myg.find(t[i]) == myg.end()) {
            flag = false;
            break;
          } else {
            j++;
            continue;
          }
        } else if (p[j] == '*') {
          j++;
          i--;
        } else {
          flag = false;
          break;
        }
      }
      if (flag)
        cout << "YES"
             << "\n";
      else
        cout << "NO"
             << "\n";
      continue;
    } else if (l == len) {
      int j = 0;
      bool flag = true;
      for (int i = 0; i < l; i++) {
        if (p[j] == t[i]) {
          j++;
          continue;
        } else if (p[j] == '?') {
          if (myg.find(t[i]) == myg.end()) {
            flag = false;
            break;
          } else {
            j++;
            continue;
          }
        } else if (p[j] == '*') {
          if (mys.find(t[i]) == mys.end()) {
            flag = false;
            break;
          } else {
            j++;
            continue;
          }
        } else {
          flag = false;
          break;
        }
      }
      if (flag)
        cout << "YES"
             << "\n";
      else
        cout << "NO"
             << "\n";
      continue;
    } else {
      int j = 0;
      bool flag = true;
      bool in = false;
      for (int i = 0; i < l; i++) {
        if (p[j] == t[i]) {
          j++;
          continue;
        } else if (p[j] == '?') {
          if (myg.find(t[i]) == myg.end()) {
            flag = false;
            break;
          } else {
            j++;
            continue;
          }
        } else if (p[j] == '*') {
          for (int k = 0; k <= diff; k++) {
            if (mys.find(t[i + k]) == mys.end()) {
              flag = false;
              in = true;
              break;
            }
          }
          i += diff;
          j++;
        } else {
          flag = false;
          break;
        }
        if (in) break;
      }
      if (flag)
        cout << "YES"
             << "\n";
      else
        cout << "NO"
             << "\n";
      continue;
    }
  }
  return 0;
}
