#include <bits/stdc++.h>
using namespace std;
string s, s1, s2;
int n;
int main() {
  cin >> s >> s1 >> n;
  bool f;
  int len1 = s1.length(), len2;
  f = (s1.find('*') == -1);
  for (int i = 0; i < n; i++) {
    cin >> s2;
    len2 = s2.length();
    int flag = 1;
    if (f) {
      if (len2 != len1) {
        printf("NO\n");
        continue;
      }
      for (int i = 0; i < len1 && flag; i++) {
        if (s1[i] == s2[i] || (s1[i] == '?' && s.find(s2[i]) != -1))
          continue;
        else
          flag = 0;
      }
      if (flag)
        printf("YES\n");
      else
        printf("NO\n");
    } else {
      if (len2 < len1 - 1) {
        printf("NO\n");
        continue;
      }
      int bound = len2 - len1;
      for (int i = 0, j = 0; j < len2 && flag; i++, j++) {
        if (s2[j] == s1[i]) continue;
        if (s1[i] == '?' && s.find(s2[j]) != -1)
          continue;
        else if (s1[i] == '?')
          flag = 0;
        if (s1[i] == '*') {
          if (bound == -1)
            j--;
          else {
            for (int k = 0; k <= bound && flag; k++, j++) {
              if (s.find(s2[j]) == -1)
                continue;
              else
                flag = 0;
            }
            j--;
          }
        } else
          flag = 0;
      }
      if (flag)
        printf("YES\n");
      else
        printf("NO\n");
    }
  }
  return 0;
}
