#include <cstring>
#include <cstdio>
#define duljina 100002

char dobri[27];
char s[duljina];
bool good[10000];

int n;
char sp[duljina];

int pro_zlo(char *sp){
    int h=strlen(sp)-strlen(s);
    if(h<-1){return false;}else{
    int i=0,bro=0,j;
    while(true){
        if('a'<=s[bro] && s[bro]<='z' && sp[i]==s[bro]){bro++; i++;}
        if('a'<=s[bro] && s[bro]<='z' && sp[i]!=s[bro]){return false;}

        if(s[bro]=='?' && good[sp[i]-'a']){bro++; i++;}
        if(s[bro]=='?' && !good[sp[i]-'a']){return false;}

        if(s[bro]=='*'){
            for(j=i; j<(i+h+1); j++){
                if(good[sp[i]-'a']){return false;}
            }
            i=j;
            bro++;
        }

        if(i>=strlen(sp)){break;}
    }
}
return true;
}

int main(void){

    scanf("%s", dobri);
    scanf("%s", s);
    scanf("%d", &n);

    for(int i=0; i<strlen(dobri); i++){
        good[dobri[i]-'a']=1;
    }

    for(int i=0; i<n; i++){

        scanf("%s", sp);
        if( pro_zlo(sp)){printf("YES\n");}else{printf("NO\n");}

    }

return 0;
}


#include <string.h>
#include <cstdio>
#define duljina 100002

char dobri[27];
char s[duljina];

int n;
char sp[duljina];

bool dobar(char c){
    for(int i=0; i<strlen(dobri); i++){
        if(c==dobri[i]){return true;}
    }
return false;
}

bool pro_prazna(char *sp){
    if(strlen(s)-1!=strlen(sp)){return false;}
        int bro=0;
        for(int i=0; i<strlen(sp); i++){
            if(s[bro]=='*'){bro++;}
            if(s[bro]=='?' && s[bro]!='*'  && !dobar(sp[i])){return false;}
            if(s[bro]!='?' && s[bro]!=sp[i]){return false;}
            bro++;

    }
return true;
}

int pro_zlo(char *sp){
    int bro=0;
    if(strlen(sp)<strlen(s)){return false;}else{
    int k=0;
    for(int i=0; i<strlen(sp); i++){
        if(s[bro]=='?' && !dobar(sp[i])){return false;}
        if(s[bro]!='?' && s[bro]!='*' && s[bro]!=sp[i]){return false;}
        if(s[bro]=='*' && dobar(sp[i]) && k==0){return false;}
        if(s[bro]=='*' && !dobar(sp[i])){k++;}
        if((s[bro]=='*' && dobar(sp[i]))|| s[bro]!='*'){bro++;}

    }
    }
return true;
}

int main(void){

    scanf("%s", dobri);
    scanf("%s", s);
    scanf("%d", &n);

    for(int i=0; i<n; i++){

        scanf("%s", sp);
        if(pro_prazna(sp)|| pro_zlo(sp)){printf("YES\n");}else{printf("NO\n");}

    }

return 0;
}


#include <string.h>
#include <cstdio>
#define duljina 100001

char dobri[27];
char s[duljina];

int n;
char sp[duljina];

bool dobar(char c){
    for(int i=0; i<strlen(dobri); i++){
        if(c==dobri[i]){return true;}
    }
return false;
}

bool pro_prazna(char *sp){
    if(strlen(s)-1!=strlen(sp)){return false;}
        int bro=0;
        for(int i=0; i<strlen(sp); i++){
            if(s[bro]=='*'){bro++;}
            if(s[bro]=='?' && s[bro]!='*'  && !dobar(sp[i])){return false;}
            if(s[bro]!='?' && s[bro]!=sp[i]){return false;}
            bro++;

    }
return true;
}

int pro_zlo(char *sp){
    int bro=0;
    for(int i=0; i<strlen(sp); i++){
        if(s[bro]=='?' && !dobar(sp[i])){return false;}
        if(s[bro]!='?' && s[bro]!='*' && s[bro]!=sp[i]){return false;}
        if((s[bro]=='*' && dobar(sp[i]))|| s[bro]!='*'){bro++;}

    }
return true;
}

int main(void){

    scanf("%s", dobri);
    scanf("%s", s);
    scanf("%d", &n);

    for(int i=0; i<n; i++){

        scanf("%s", sp);
        if(pro_prazna(sp)|| pro_zlo(sp)){printf("YES\n");}else{printf("NO\n");}

    }

return 0;
}


#include <cstring>
#include <cstdio>

const int duljina = 1e5+5;

char dobri[duljina], s[duljina], sp[duljina];
int n;
int good[duljina];

int main() {
	scanf("%s", dobri);

	for(int i=0; i<strlen(dobri); i++){
        good[dobri[i]-'a']=1;
	}

    scanf("%s",s);
    int ls = strlen(s), lsp, bro,i,z,k;
    scanf("%d", &n);

    for(int j=0; j<n; j++){
        scanf("%s", sp);
        lsp = strlen(sp);
        i=0;
        bro=0;
        if(lsp-ls<-1){printf("NO\n"); break;}else{
            i=0;
            bro=0;
            k=1;
            while(k){
                if('a' <= s[bro] && s[bro] <= 'z' && s[bro]==sp[i]){bro++; i++;}
                if('a' <= s[bro] && s[bro] <= 'z' && s[bro]!=sp[i]){printf("NO\n"); k=0;}

                if(s[bro]=='?' && good[sp[i]-'a']){bro++; i++;}
                if(s[bro]=='?' && !good[sp[i]-'a']){printf("NO\n"); k=0;}

                if(s[bro]=='*'){
                    for(z=i; z<i+(lsp-ls)+1; z++){
                        if(good[sp[z]-'a']){printf("NO\n"); k=0; break;}
                    }
                    bro++;
                    i+=z;
                }

                if(k==0){break;}
                if(i>=lsp){printf("YES\n"); k=0; break;}
            }

        }

    }
return 0;
}


#include <string.h>
#include <cstdio>
#define duljina 100002

char dobri[27];
char s[duljina];

int n;
char sp[duljina];

bool dobar(char c){
    for(int i=0; i<strlen(dobri); i++){
        if(c==dobri[i]){return true;}
    }
return false;
}

bool pro_prazna(char *sp){
    if(strlen(s)-1!=strlen(sp)){return false;}
        int bro=0;
        for(int i=0; i<strlen(sp); i++){
            if(s[bro]=='*'){bro++;}
            if(s[bro]=='?' && s[bro]!='*'  && !dobar(sp[i])){return false;}
            if(s[bro]!='?' && s[bro]!=sp[i]){return false;}
            bro++;

    }
return true;
}

int pro_zlo(char *sp){
    if(strlen(sp)<strlen(s)){return false;}else{
    int k=0,i=0,bro=0,j;
    while(true){
        if(s[bro]==sp[i] && s[bro]!='?' && s[bro]!='*'){bro++; i++;}
        if(s[bro]!=sp[i] && s[bro]!='?' && s[bro]!='*'){return false;}

        if(s[bro]=='?' && dobar(sp[i])){bro++; i++;}
        if(s[bro]=='?' && !dobar(sp[i])){return false;}

        if(s[bro]=='*'){
            for(j=i; j<(i+(strlen(sp)-strlen(s))+1); j++){
                if(dobar(sp[j])){return false;}
            }
            i=j;
            bro++;
        }

        if(i>=strlen(sp)){break;}
    }
}
return true;
}

int main(void){

    scanf("%s", dobri);
    scanf("%s", s);
    scanf("%d", &n);

    for(int i=0; i<n; i++){

        scanf("%s", sp);
        if(pro_prazna(sp)|| pro_zlo(sp)){printf("YES\n");}else{printf("NO\n");}

    }

return 0;
}


#include <cstring>
#include <cstdio>

const int duljina = 1e5+5;

char dobri[27];
char s[duljina];
bool good[duljina];

int n;
char sp[duljina];

int pro_zlo(char *sp){
    int i=0,bro=0,j;
    int h=strlen(sp)-strlen(s);
    if(h<-1){return false;}else{
        while(true){
            if('a'<=s[bro] && s[bro]<='z' && sp[i]==s[bro]){bro++; i++;}
            if('a'<=s[bro] && s[bro]<='z' && sp[i]!=s[bro]){return false;}

            if(s[bro]=='?' && good[sp[i]-'a']){bro++; i++;}
            if(s[bro]=='?' && !good[sp[i]-'a']){return false;}

            if(s[bro]=='*'){
                for(j=i; j<(i+h+1); j++){
                    if(good[sp[i]-'a']){return false;}
                }
                i=j;
                bro++;
            }

            if(i>=strlen(sp)){break;}
        }

    }

if(strlen(s)==bro){return true;}else{return false;}
}

int main(void){

    scanf("%s", dobri);
    scanf("%s", s);
    scanf("%d", &n);

    for(int i=0; i<strlen(dobri); i++){
        good[dobri[i]-'a']=1;
    }

    for(int i=0; i<n; i++){

        scanf("%s", sp);
        if( pro_zlo(sp)){printf("YES\n");}else{printf("NO\n");}

    }

return 0;
}


#include <iostream>
#include <cstring>

using namespace std;
const int maxn = 100010;

int main()
{
    //int ss[30];
    char s[30];
    char a[maxn];
    char p[maxn];
    while(cin>>s)
    {
        //memset(ss,0,sizeof(ss));
        cin>>p;
        int n;
        cin>>n;
//        for(int i = 0; i < strlen(s); i++)
//        {
//            ss[s[i]-'a'] = 1;
//        }
        while(n--)
        {
            cin>>a;
            int m = strlen(a) - strlen(p);
            int j = 0;
            int flag = 0;
            int ans = 0;
            int cnt = 1;
            int u = 1;
            int len = strlen(p);
            if(m<-1)
            {
                cout<<"NO"<<endl;
            }
            else
            {
                if(m==-1)
                {
                    u = 0;
                    int q;
                    for(int i = 0; i < strlen(p); i++)
                    {
                        if(p[i]=='*')
                        {
                            u = 1;
                            q = i;
                            break;
                        }
                    }
                    if(u==0)
                        cout<<"NO"<<endl;
//                    else
//                    {
//                        for(int i = q; i < len-1; i++)
//                        {
//                            p[i] = p[i+1];
//                        }
//                        len--;
//                    }
                    //for(int i = 0; i < len; i++)
                    //    cout<<p[i];
                    //cout<<endl;
                }
                if(u==1)
                {

                    for(int i = 0; i < len; i++)
                    {
                        if(p[i]!='*'&&p[i]!='?')
                        {
                            if(p[i]==a[j])
                                j++;
                            else
                            {
                                flag = 1;
                                break;
                            }
                        }
                        else if(p[i]=='?')
                        {
                            cnt = 0;
                            for(int k = 0; k < strlen(s); k++)
                            {
                                if(a[j]==s[k])
                                    cnt = 1;
                            }
                            if(cnt==1)
                                j++;
                            else
                                break;
                        }
                        else if(p[i]=='*')
                        {
                            if(m==-1&&u==1)
                                continue;
                            ans = 0;
                            for(int k = j; k <= j+m; k++)
                            {
                                int cnt1 = 0;
                                for(int t = 0; t < strlen(s); t++)
                                {
                                    if(a[k]==s[t])
                                        cnt1 = 1;
                                }
                                if(cnt1==1)
                                {
                                    ans = 1;
                                    break;
                                }
                            }
                            if(ans==1)
                                break;
                            else
                                j=j+m+1;
                        }
                    }
                    //cout<<flag<<" "<<cnt<<" "<<ans<<endl;
                    if(flag==1||cnt==0||ans==1)
                        cout<<"NO"<<endl;
                    else
                        cout<<"YES"<<endl;
                }
            }
            //cout<<flag<<" "<<cnt<<" "<<ans<<" "<<u<<endl;
        }
    }
    return 0;
}


#include <iostream>
#include <cstring>

using namespace std;
const int maxn = 100010;

int main()
{
    //int ss[30];
    char s[1000];
    char a[maxn];
    char p[maxn];
    while(cin>>s)
    {
        //memset(ss,0,sizeof(ss));
        cin>>p;
        int n;
        cin>>n;
//        for(int i = 0; i < strlen(s); i++)
//        {
//            ss[s[i]-'a'] = 1;
//        }
        while(n--)
        {
            cin>>a;
            int m = strlen(a) - strlen(p);
            int j = 0;
            int flag = 0;
            int ans = 0;
            int cnt = 1;
            for(int i = 0; i < strlen(p); i++)
            {
                if(p[i]!='*'&&p[i]!='?')
                {
                    if(p[i]==a[j])
                    j++;
                    else
                    {
                        flag = 1;
                        break;
                    }
                }
                else if(p[i]=='?')
                {
                    cnt = 0;
                    for(int k = 0; k < strlen(s); k++)
                    {
                        if(a[i]==s[k])
                            cnt = 1;
                    }
                    if(cnt==1)
                        j++;
                    else
                        break;
                }
                else if(p[i]=='*')
                {
                    ans = 0;
                    for(int k = i; k <= i+m; k++)
                    {
                        int cnt1 = 0;
                        for(int t = 0; t < strlen(s); t++)
                        {
                            if(a[k]==s[t])
                                cnt1 = 1;
                        }
                        if(cnt1==1)
                            ans = 1;
                    }
                    if(ans==1)
                        break;
                    else
                        j=j+m+1;
                }
            }
            //cout<<flag<<" "<<cnt<<" "<<ans<<endl;
            if(flag==1||cnt==0||ans==1)
                cout<<"NO"<<endl;
            else
                cout<<"YES"<<endl;
        }
    }
    return 0;
}


#include <iostream>
#include <cstring>

using namespace std;
const int maxn = 100010;

int main()
{
    int ss[30];
    char s[30];
    char a[maxn];
    char p[maxn];
    while(cin>>s)
    {
        memset(ss,0,sizeof(ss));
        cin>>p;
        int n;
        cin>>n;
//        for(int i = 0; i < strlen(s); i++)
//        {
//            ss[s[i]-'a'] = 1;
//        }
        while(n--)
        {
            cin>>a;
            int m = strlen(a) - strlen(p);
            int j = 0;
            int flag = 0;
            int ans;
            int cnt;
            for(int i = 0; i < strlen(p); i++)
            {
                if(p[i]!='*'&&p[i]!='?')
                {
                    if(p[i]==a[j])
                    j++;
                    else
                    {
                        flag = 1;
                        break;
                    }
                }
                else if(p[i]=='?')
                {
                    cnt = 0;
                    for(int k = 0; k < strlen(s); k++)
                    {
                        if(a[i]==s[k])
                            cnt = 1;
                    }
                    if(cnt==1)
                        j++;
                    else
                        break;
                }
                else if(p[i]=='*')
                {
                    ans = 0;
                    for(int k = i; k <= i+m; k++)
                    {
                        int cnt1 = 0;
                        for(int t = 0; t < strlen(s); t++)
                        {
                            if(a[k]==s[t])
                                cnt1 = 1;
                        }
                        if(cnt1==1)
                            ans = 1;
                    }
                    if(ans==1)
                        break;
                    else
                        j=j+m+1;
                }
            }
            //cout<<flag<<" "<<cnt<<" "<<ans<<endl;
            if(flag==1||cnt==0||ans==1)
                cout<<"NO"<<endl;
            else
                cout<<"YES"<<endl;
        }
    }
    return 0;
}


#include <iostream>
#include <cstring>

using namespace std;
const int maxn = 200010;

int main()
{
    //int ss[30];
    char s[30];
    char a[maxn];
    char p[maxn];
    while(cin>>s)
    {
        //memset(ss,0,sizeof(ss));
        cin>>p;
        int n;
        cin>>n;
//        for(int i = 0; i < strlen(s); i++)
//        {
//            ss[s[i]-'a'] = 1;
//        }
        while(n--)
        {
            cin>>a;
            int m = strlen(a) - strlen(p);
            int j = 0;
            int flag = 0;
            int ans = 0;
            int cnt = 1;
            for(int i = 0; i < strlen(p); i++)
            {
                if(p[i]!='*'&&p[i]!='?')
                {
                    if(p[i]==a[j])
                    j++;
                    else
                    {
                        flag = 1;
                        break;
                    }
                }
                else if(p[i]=='?')
                {
                    cnt = 0;
                    for(int k = 0; k < strlen(s); k++)
                    {
                        if(a[i]==s[k])
                            cnt = 1;
                    }
                    if(cnt==1)
                        j++;
                    else
                        break;
                }
                else if(p[i]=='*')
                {
                    ans = 0;
                    for(int k = i; k <= i+m; k++)
                    {
                        int cnt1 = 0;
                        for(int t = 0; t < strlen(s); t++)
                        {
                            if(a[k]==s[t])
                                cnt1 = 1;
                        }
                        if(cnt1==1)
                            ans = 1;
                    }
                    if(ans==1)
                        break;
                    else
                        j=j+m+1;
                }
            }
            //cout<<flag<<" "<<cnt<<" "<<ans<<endl;
            if(flag==1||cnt==0||ans==1)
                cout<<"NO"<<endl;
            else
                cout<<"YES"<<endl;
        }
    }
    return 0;
}


#include <iostream>
#include <cstring>

using namespace std;
const int maxn = 100010;

int main()
{
    //int ss[30];
    char s[30];
    char a[maxn];
    char p[maxn];
    while(cin>>s)
    {
        //memset(ss,0,sizeof(ss));
        cin>>p;
        int n;
        cin>>n;
        int ss = 0;
        for(int i = 0; i < strlen(p); i++)
        {
            if(p[i]=='*')
                ss = 1;
        }
        while(n--)
        {
            cin>>a;
            int m = strlen(a) - strlen(p);
            int j = 0;
            int flag = 0;
            int ans = 0;
            int cnt = 1;
            int u = 1;
            int len = strlen(p);
            if(ss==0&&m>0)
                cout<<"NO"<<endl;
            else
            {
                if(m<-1)
            {
                cout<<"NO"<<endl;
            }
            else
            {
                if(m==-1)
                {
                    u = 0;
                    int q;
                    for(int i = 0; i < strlen(p); i++)
                    {
                        if(p[i]=='*')
                        {
                            u = 1;
                            q = i;
                            break;
                        }
                    }
                    if(u==0)
                        cout<<"NO"<<endl;
//                    else
//                    {
//                        for(int i = q; i < len-1; i++)
//                        {
//                            p[i] = p[i+1];
//                        }
//                        len--;
//                    }
                    //for(int i = 0; i < len; i++)
                    //    cout<<p[i];
                    //cout<<endl;
                }
                if(u==1)
                {

                    for(int i = 0; i < len; i++)
                    {
                        if(p[i]!='*'&&p[i]!='?')
                        {
                            if(p[i]==a[j])
                                j++;
                            else
                            {
                                flag = 1;
                                break;
                            }
                        }
                        else if(p[i]=='?')
                        {
                            cnt = 0;
                            for(int k = 0; k < strlen(s); k++)
                            {
                                if(a[j]==s[k])
                                    cnt = 1;
                            }
                            if(cnt==1)
                                j++;
                            else
                                break;
                        }
                        else if(p[i]=='*')
                        {
                            if(m==-1&&u==1)
                                continue;
                            ans = 0;
                            for(int k = j; k <= j+m; k++)
                            {
                                int cnt1 = 0;
                                for(int t = 0; t < strlen(s); t++)
                                {
                                    if(a[k]==s[t])
                                        cnt1 = 1;
                                }
                                if(cnt1==1)
                                {
                                    ans = 1;
                                    break;
                                }
                            }
                            if(ans==1)
                                break;
                            else
                                j=j+m+1;
                        }
                    }
                    //cout<<flag<<" "<<cnt<<" "<<ans<<endl;
                    if(flag==1||cnt==0||ans==1)
                        cout<<"NO"<<endl;
                    else
                        cout<<"YES"<<endl;
                }
            }
            }
            //cout<<flag<<" "<<cnt<<" "<<ans<<" "<<u<<endl;
        }
    }
    return 0;
}


#include <iostream>
#include <cstring>

using namespace std;
const int maxn = 100010;

int main()
{
    int ss[30];
    char s[30];
    char a[maxn];
    char p[maxn];
    while(cin>>s)
    {
        memset(ss,0,sizeof(ss));
        cin>>p;
        int n;
        cin>>n;
//        for(int i = 0; i < strlen(s); i++)
//        {
//            ss[s[i]-'a'] = 1;
//        }
        while(n--)
        {
            cin>>a;
            int m = strlen(a) - strlen(p);
            int j = 0;
            int flag = 0;
            int ans = 0;
            int cnt = 1;
            for(int i = 0; i < strlen(p); i++)
            {
                if(p[i]!='*'&&p[i]!='?')
                {
                    if(p[i]==a[j])
                    j++;
                    else
                    {
                        flag = 1;
                        break;
                    }
                }
                else if(p[i]=='?')
                {
                    cnt = 0;
                    for(int k = 0; k < strlen(s); k++)
                    {
                        if(a[i]==s[k])
                            cnt = 1;
                    }
                    if(cnt==1)
                        j++;
                    else
                        break;
                }
                else if(p[i]=='*')
                {
                    ans = 0;
                    for(int k = i; k <= i+m; k++)
                    {
                        int cnt1 = 0;
                        for(int t = 0; t < strlen(s); t++)
                        {
                            if(a[k]==s[t])
                                cnt1 = 1;
                        }
                        if(cnt1==1)
                            ans = 1;
                    }
                    if(ans==1)
                        break;
                    else
                        j=j+m+1;
                }
            }
            //cout<<flag<<" "<<cnt<<" "<<ans<<endl;
            if(flag==1||cnt==0||ans==1)
                cout<<"NO"<<endl;
            else
                cout<<"YES"<<endl;
        }
    }
    return 0;
}


#include <iostream>
#include <cstring>

using namespace std;
const int maxn = 100010;

int main()
{
    //int ss[30];
    char s[30];
    char a[maxn];
    char p[maxn];
    while(cin>>s)
    {
        //memset(ss,0,sizeof(ss));
        cin>>p;
        int n;
        cin>>n;
//        for(int i = 0; i < strlen(s); i++)
//        {
//            ss[s[i]-'a'] = 1;
//        }
        while(n--)
        {
            cin>>a;
            int m = strlen(a) - strlen(p);
            int j = 0;
            int flag = 0;
            int ans = 0;
            int cnt = 1;
            for(int i = 0; i < strlen(p); i++)
            {
                if(p[i]!='*'&&p[i]!='?')
                {
                    if(p[i]==a[j])
                    j++;
                    else
                    {
                        flag = 1;
                        break;
                    }
                }
                else if(p[i]=='?')
                {
                    cnt = 0;
                    for(int k = 0; k < strlen(s); k++)
                    {
                        if(a[j]==s[k])
                            cnt = 1;
                    }
                    if(cnt==1)
                        j++;
                    else
                        break;
                }
                else if(p[i]=='*')
                {
                    ans = 0;
                    for(int k = j; k <= j+m; k++)
                    {
                        int cnt1 = 0;
                        for(int t = 0; t < strlen(s); t++)
                        {
                            if(a[k]==s[t])
                                cnt1 = 1;
                        }
                        if(cnt1==1)
                            ans = 1;
                    }
                    if(ans==1)
                        break;
                    else
                        j=j+m+1;
                }
            }
            //cout<<flag<<" "<<cnt<<" "<<ans<<endl;
            if(flag==1||cnt==0||ans==1)
                cout<<"NO"<<endl;
            else
                cout<<"YES"<<endl;
        }
    }
    return 0;
}


#include <iostream>
#include <cstring>

using namespace std;
const int maxn = 100010;

int main()
{
    //int ss[30];
    char s[30];
    char a[maxn];
    char p[maxn];
    while(cin>>s)
    {
        //memset(ss,0,sizeof(ss));
        cin>>p;
        int n;
        cin>>n;
//        for(int i = 0; i < strlen(s); i++)
//        {
//            ss[s[i]-'a'] = 1;
//        }
        while(n--)
        {
            cin>>a;
            int m = strlen(a) - strlen(p);
            int j = 0;
            int flag = 0;
            int ans = 0;
            int cnt = 1;
            int u = 1;
            int len = strlen(p);
            if(m<-1)
            {
                cout<<"NO"<<endl;
            }
            else
            {
                if(m==-1)
                {
                    u = 0;
                    int q;
                    for(int i = 0; i < strlen(p); i++)
                    {
                        if(p[i]=='*')
                        {
                            u = 1;
                            q = i;
                            break;
                        }
                    }
                    if(u==0)
                        cout<<"NO"<<endl;
                    else
                    {
                        for(int i = q; i < len-1; i++)
                        {
                            p[i] = p[i+1];
                        }
                        len--;
                    }
                }
                if(u==1)
                {

                    for(int i = 0; i < len; i++)
                    {
                        if(p[i]!='*'&&p[i]!='?')
                        {
                            if(p[i]==a[j])
                                j++;
                            else
                            {
                                flag = 1;
                                break;
                            }
                        }
                        else if(p[i]=='?')
                        {
                            cnt = 0;
                            for(int k = 0; k < strlen(s); k++)
                            {
                                if(a[j]==s[k])
                                    cnt = 1;
                            }
                            if(cnt==1)
                                j++;
                            else
                                break;
                        }
                        else if(p[i]=='*')
                        {
                            ans = 0;
                            for(int k = j; k <= j+m; k++)
                            {
                                int cnt1 = 0;
                                for(int t = 0; t < strlen(s); t++)
                                {
                                    if(a[k]==s[t])
                                        cnt1 = 1;
                                }
                                if(cnt1==1)
                                {
                                    ans = 1;
                                    break;
                                }
                            }
                            if(ans==1)
                                break;
                            else
                                j=j+m+1;
                        }
                    }
                    //cout<<flag<<" "<<cnt<<" "<<ans<<endl;
                    if(flag==1||cnt==0||ans==1)
                        cout<<"NO"<<endl;
                    else
                        cout<<"YES"<<endl;
                }
            }
        }
    }
    return 0;
}


#include <iostream>
#include <cstring>

using namespace std;
const int maxn = 100010;

int main()
{
    //int ss[30];
    char s[30];
    char a[maxn];
    char p[maxn];
    while(cin>>s)
    {
        //memset(ss,0,sizeof(ss));
        cin>>p;
        int n;
        cin>>n;
//        for(int i = 0; i < strlen(s); i++)
//        {
//            ss[s[i]-'a'] = 1;
//        }
        while(n--)
        {
            cin>>a;
            int m = strlen(a) - strlen(p);
            int j = 0;
            int flag = 0;
            int ans = 0;
            int cnt = 1;
            int u = 1;
            int len = strlen(p);

            int ss = 0;
            for(int i = 0; i < len; i++)
            {
                if(p[i]=='*')
                    ss = 1;
            }

            if(ss==1&&m>0)
                cout<<"NO"<<endl;
            else
            {
                if(m<-1)
            {
                cout<<"NO"<<endl;
            }
            else
            {
                if(m==-1)
                {
                    u = 0;
                    int q;
                    for(int i = 0; i < strlen(p); i++)
                    {
                        if(p[i]=='*')
                        {
                            u = 1;
                            q = i;
                            break;
                        }
                    }
                    if(u==0)
                        cout<<"NO"<<endl;
//                    else
//                    {
//                        for(int i = q; i < len-1; i++)
//                        {
//                            p[i] = p[i+1];
//                        }
//                        len--;
//                    }
                    //for(int i = 0; i < len; i++)
                    //    cout<<p[i];
                    //cout<<endl;
                }
                if(u==1)
                {

                    for(int i = 0; i < len; i++)
                    {
                        if(p[i]!='*'&&p[i]!='?')
                        {
                            if(p[i]==a[j])
                                j++;
                            else
                            {
                                flag = 1;
                                break;
                            }
                        }
                        else if(p[i]=='?')
                        {
                            cnt = 0;
                            for(int k = 0; k < strlen(s); k++)
                            {
                                if(a[j]==s[k])
                                    cnt = 1;
                            }
                            if(cnt==1)
                                j++;
                            else
                                break;
                        }
                        else if(p[i]=='*')
                        {
                            if(m==-1&&u==1)
                                continue;
                            ans = 0;
                            for(int k = j; k <= j+m; k++)
                            {
                                int cnt1 = 0;
                                for(int t = 0; t < strlen(s); t++)
                                {
                                    if(a[k]==s[t])
                                        cnt1 = 1;
                                }
                                if(cnt1==1)
                                {
                                    ans = 1;
                                    break;
                                }
                            }
                            if(ans==1)
                                break;
                            else
                                j=j+m+1;
                        }
                    }
                    //cout<<flag<<" "<<cnt<<" "<<ans<<endl;
                    if(flag==1||cnt==0||ans==1)
                        cout<<"NO"<<endl;
                    else
                        cout<<"YES"<<endl;
                }
            }
            }
            //cout<<flag<<" "<<cnt<<" "<<ans<<" "<<u<<endl;
        }
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define bug1(x)                cout <<#x<<": "<<x<<endl;
#define bug2(x, y)             cout <<#x<<": "<<x<<" | "<<#y<<": "<<y<< endl;
#define bug3(x, y, z)          cout <<#x<<": "<<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl;
#define bug4(a, b, c, d)       cout <<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl;
#define bug5(a, b, c, d, e)    cout <<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<<": "<<e<<endl;

#define ll  long long int
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define mp make_pair
#define MAX 1e9
#define MOD 1000000007

#define PII pair<ll,ll>

ll prime[10000001];
ll fact[10000000];

int main(){

		map<char,ll> m;

		string a,b,s1,s2;
		cin>>a>>b;
		ll len=b.length(),q;

		for(ll i=0;i<(ll)a.length();i++)
			m[a[i]]=1;

		ll i;

		for(i=0;i<len && b[i]!='*';i++){
			s1.pb(b[i]);

		}

		for(i=i+1;i<len;i++){
			s2.pb(b[i]);

		}

		//cout<<s1<<" "<<s2<<endl;

		cin>>q;

		while(q--){

			string c;
			cin>>c;

			ll flag=1;

			if(((ll)s2.length()>(ll)c.length()) ||((ll)s1.length()>(ll)c.length()) )
				flag=0;

			else{

				for(i=0;i<(ll)s1.length();i++){

					if(s1[i]=='?'){

						if(m[c[i]]!=1)
							flag=0;

					}else if(s1[i]!=c[i])
							flag=0;
				}

				ll j=(ll)c.length()-1;

				for(i=(ll)s2.length()-1;i>=0;i--){

					if(s2[i]=='?'){

						if(m[c[j]]!=1)
							flag=0;

					}else if(s2[i]!=c[j])
						flag=0;

				}

				string r;

				ll k=(ll)c.length()-(ll)s2.length();

					for(i=(ll)s1.length();i<k;i++){
						r.pb(c[i]);

					}

				//	cout<<r<<endl;

					for(i=0;i<(ll)r.length();i++){

						if(m[r[i]]==1)
							flag=0;

					}

			}

			if(flag==0)
				cout<<"NO\n";
			else
				cout<<"YES\n";
		}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define bug1(x)                cout <<#x<<": "<<x<<endl;
#define bug2(x, y)             cout <<#x<<": "<<x<<" | "<<#y<<": "<<y<< endl;
#define bug3(x, y, z)          cout <<#x<<": "<<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl;
#define bug4(a, b, c, d)       cout <<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl;
#define bug5(a, b, c, d, e)    cout <<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<<": "<<e<<endl;

#define ll  long long int
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define mp make_pair
#define MAX 1e9
#define MOD 1000000007

#define PII pair<ll,ll>

ll prime[10000001];
ll fact[10000000];

int main(){

		map<char,ll> m;

		string a,b,s1,s2;
		cin>>a>>b;
		ll len=b.length(),q;

		for(ll i=0;i<(ll)a.length();i++)
			m[a[i]]=1;

		ll i;

		for(i=0;i<len && b[i]!='*';i++){
			s1.pb(b[i]);

		}

		for(i=i+1;i<len;i++){
			s2.pb(b[i]);

		}

		//cout<<s1<<" "<<s2<<endl;

		cin>>q;

		while(q--){

			string c;
			cin>>c;

			ll flag=1;

			if(((ll)s2.length()>(ll)c.length()) ||((ll)s1.length()>(ll)c.length()) || (((ll)s2.length()+(ll)s1.length())>(ll)c.length()))
				flag=0;

			else{

				for(i=0;i<(ll)s1.length();i++){
					//cout<<"yes"<<endl;
					if(s1[i]=='?'){

						if(m[c[i]]!=1)
							flag=0;

					}else if(s1[i]!=c[i])
							flag=0;
				}

				ll j=(ll)c.length()-1;

				for(i=(ll)s2.length()-1;i>=0;i--){
					//cout<<"yes"<<endl;
					if(s2[i]=='?'){

						if(m[c[j]]!=1)
							flag=0;

					}else if(s2[i]!=c[j])
						flag=0;
					j--;
				}
				//cout<<flag<<endl;
				string r;

				ll k=(ll)c.length()-(ll)s2.length();

					for(i=(ll)s1.length();i<k;i++){
						r.pb(c[i]);

					}

					//cout<<r<<endl;

					for(i=0;i<(ll)r.length();i++){

						if(m[r[i]]==1)
							flag=0;

					}

			}

			if(flag==0)
				cout<<"NO\n";
			else
				cout<<"YES\n";
		}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define bug1(x)                cout <<#x<<": "<<x<<endl;
#define bug2(x, y)             cout <<#x<<": "<<x<<" | "<<#y<<": "<<y<< endl;
#define bug3(x, y, z)          cout <<#x<<": "<<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl;
#define bug4(a, b, c, d)       cout <<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl;
#define bug5(a, b, c, d, e)    cout <<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<<": "<<e<<endl;

#define ll  long long int
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define mp make_pair
#define MAX 1e9
#define MOD 1000000007

#define PII pair<ll,ll>

ll prime[10000001];
ll fact[10000000];

int main(){

		map<char,ll> m;

		string a,b,s1,s2;
		cin>>a>>b;
		ll len=b.length(),q;

		for(ll i=0;i<(ll)a.length();i++)
			m[a[i]]=1;

		ll i;

		for(i=0;i<len && b[i]!='*';i++){
			s1.pb(b[i]);

		}

		for(i=i+1;i<len;i++){
			s2.pb(b[i]);

		}

//		cout<<s1<<" "<<s2<<endl;

		cin>>q;

		while(q--){

			string c;
			cin>>c;

			ll flag=1;

			if(((ll)s2.length()>(ll)c.length()) ||((ll)s1.length()>(ll)c.length()) )
				flag=0;
			else{

				for(i=0;i<(ll)s1.length();i++){

					if(s1[i]=='?'){

						if(m[c[i]]!=1)
							flag=0;

					}else if(s1[i]!=c[i])
							flag=0;
				}

				ll j=(ll)c.length()-1;

				for(i=(ll)s2.length()-1;i>=0;i--){

					if(s2[i]=='?'){

						if(m[c[j]]!=1)
							flag=0;

					}else if(s2[i]!=c[j])
						flag=0;

				}

				string r;

				ll k=(ll)c.length()-(ll)s2.length();

				if(k<0)
					flag=0;
				else{

					for(i=(ll)s1.length();i<k;i++){
						r.pb(c[i]);

					}

				//	cout<<r<<endl;

					for(i=0;i<(ll)r.length();i++){

						if(m[r[i]]==1)
							flag=0;

					}

				}
			}

			if(flag==0)
				cout<<"NO\n";
			else
				cout<<"YES\n";
		}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define bug1(x)                cout <<#x<<": "<<x<<endl;
#define bug2(x, y)             cout <<#x<<": "<<x<<" | "<<#y<<": "<<y<< endl;
#define bug3(x, y, z)          cout <<#x<<": "<<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl;
#define bug4(a, b, c, d)       cout <<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl;
#define bug5(a, b, c, d, e)    cout <<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<<": "<<e<<endl;

#define ll  long long int
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define mp make_pair
#define MAX 1e9
#define MOD 1000000007

#define PII pair<ll,ll>

ll prime[10000001];
ll fact[10000000];

int main(){

		map<char,ll> m;

		string a,b,s1,s2;
		cin>>a>>b;
		ll len=b.length(),q;

		for(ll i=0;i<(ll)a.length();i++)
			m[a[i]]=1;

		ll i;

		for(i=0;i<len && b[i]!='*';i++){
			s1.pb(b[i]);

		}

		for(i=i+1;i<len;i++){
			s2.pb(b[i]);

		}
		ll flag1=0;

		for(i=0;i<(ll)b.length();i++){

			if(b[i]=='*')
				flag1=1;

		}

		//cout<<s1<<" "<<s2<<endl;

		cin>>q;

		while(q--){

			string c;
			cin>>c;

			ll flag=1;

			if(((ll)s2.length()>(ll)c.length()) ||((ll)s1.length()>(ll)c.length()) || (((ll)s2.length()+(ll)s1.length())>(ll)c.length()))
				flag=0;

			else{

				for(i=0;i<(ll)s1.length();i++){
					//cout<<"yes"<<endl;
					if(s1[i]=='?'){

						if(m[c[i]]!=1)
							flag=0;

					}else if(s1[i]!=c[i])
							flag=0;
				}

				ll j=(ll)c.length()-1;

				for(i=(ll)s2.length()-1;i>=0;i--){

					//cout<<"yes"<<endl;

					if(s2[i]=='?'){

						if(m[c[j]]!=1)
							flag=0;

					}else if(s2[i]!=c[j])
						flag=0;

					j--;
				}

				//cout<<flag<<endl;
				string r;

				ll k=(ll)c.length()-(ll)s2.length();

					for(i=(ll)s1.length();i<k;i++){
						r.pb(c[i]);

					}

					//cout<<r<<endl;

					for(i=0;i<(ll)r.length();i++){

						if(m[r[i]]==1)
							flag=0;

					}

					if(flag1==0 && r.length()!=0)
						flag=0;

			}

			if(flag==0)
				cout<<"NO\n";
			else
				cout<<"YES\n";
		}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define bug1(x)                cout <<#x<<": "<<x<<endl;
#define bug2(x, y)             cout <<#x<<": "<<x<<" | "<<#y<<": "<<y<< endl;
#define bug3(x, y, z)          cout <<#x<<": "<<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl;
#define bug4(a, b, c, d)       cout <<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl;
#define bug5(a, b, c, d, e)    cout <<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<<": "<<e<<endl;

#define ll  long long int
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define mp make_pair
#define MAX 1e9
#define MOD 1000000007

#define PII pair<ll,ll>

ll prime[10000001];
ll fact[10000000];

int main(){

		map<char,ll> m;

		string a,b,s1,s2;
		cin>>a>>b;
		ll len=b.length(),q;

		for(ll i=0;i<(ll)a.length();i++)
			m[a[i]]=1;

		ll i;

		for(i=0;i<len && b[i]!='*';i++){
			s1.pb(b[i]);

		}

		for(i=i+1;i<len;i++){
			s2.pb(b[i]);

		}

//		cout<<s1<<" "<<s2<<endl;

		cin>>q;

		while(q--){

			string c;
			cin>>c;

			ll flag=1;

			if(((ll)s2.length()+(ll)s1.length())>(ll)c.length())
				flag=0;
			else{

				for(i=0;i<(ll)s1.length();i++){

					if(s1[i]=='?'){

						if(m[c[i]]!=1)
							flag=0;

					}else if(s1[i]!=c[i])
							flag=0;
				}

				ll j=(ll)c.length()-1;

				for(i=(ll)s2.length();i>0;i--){

					if(s2[i-1]=='?'){

						if(m[c[j]]!=1)
							flag=0;

					}else if(s2[i-1]!=c[j])
						flag=0;

				}

				string r;

				ll k=(ll)c.length()-(ll)s2.length();

				if(k<0)
					flag=0;
				else{

					for(i=(ll)s1.length();i<k;i++){
						r.pb(c[i]);

					}

				//	cout<<r<<endl;

					for(i=0;i<(ll)r.length();i++){

						if(m[r[i]]==1)
							flag=0;

					}

				}
			}

			if(flag==0)
				cout<<"NO\n";
			else
				cout<<"YES\n";
		}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define bug1(x)                cout <<#x<<": "<<x<<endl;
#define bug2(x, y)             cout <<#x<<": "<<x<<" | "<<#y<<": "<<y<< endl;
#define bug3(x, y, z)          cout <<#x<<": "<<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl;
#define bug4(a, b, c, d)       cout <<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl;
#define bug5(a, b, c, d, e)    cout <<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<<": "<<e<<endl;

#define ll  long long int
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define mp make_pair
#define MAX 1e9
#define MOD 1000000007

#define PII pair<ll,ll>

ll prime[10000001];
ll fact[10000000];

int main(){

		map<char,ll> m;

		string a,b,s1,s2;
		cin>>a>>b;
		ll len=b.length(),q;

		for(ll i=0;i<(ll)a.length();i++)
			m[a[i]]=1;

		ll i;

		for(i=0;i<len && b[i]!='*';i++){
			s1.pb(b[i]);

		}

		for(i=i+1;i<len;i++){
			s2.pb(b[i]);

		}

		//cout<<s1<<" "<<s2<<endl;

		cin>>q;

		while(q--){

			string c;
			cin>>c;

			ll flag=1;

			if(((ll)s2.length()>(ll)c.length()) ||((ll)s1.length()>(ll)c.length()) || (((ll)s2.length()+(ll)s1.length())>(ll)c.length()))
				flag=0;

			else{

				for(i=0;i<(ll)s1.length();i++){
					//cout<<"yes"<<endl;
					if(s1[i]=='?'){

						if(m[c[i]]!=1)
							flag=0;

					}else if(s1[i]!=c[i])
							flag=0;
				}

				ll j=(ll)c.length()-1;

				for(i=(ll)s2.length()-1;i>=0;i--){

					//cout<<"yes"<<endl;

					if(s2[i]=='?'){

						if(m[c[j]]!=1)
							flag=0;

					}else if(s2[i]!=c[j])
						flag=0;

					j--;
				}

				//cout<<flag<<endl;
				string r;

				ll k=(ll)c.length()-(ll)s2.length();

					for(i=(ll)s1.length();i<k;i++){
						r.pb(c[i]);

					}

					//cout<<r<<endl;

					for(i=0;i<(ll)r.length();i++){

						if(m[r[i]]==1)
							flag=0;

					}

					if(s2.length()==0 && r.length()!=0)
						flag=0;

			}

			if(flag==0)
				cout<<"NO\n";
			else
				cout<<"YES\n";
		}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define bug1(x)                cout <<#x<<": "<<x<<endl;
#define bug2(x, y)             cout <<#x<<": "<<x<<" | "<<#y<<": "<<y<< endl;
#define bug3(x, y, z)          cout <<#x<<": "<<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl;
#define bug4(a, b, c, d)       cout <<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl;
#define bug5(a, b, c, d, e)    cout <<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<<": "<<e<<endl;

#define ll  long long int
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define mp make_pair
#define MAX 1e9
#define MOD 1000000007

#define PII pair<ll,ll>

ll prime[10000001];
ll fact[10000000];

int main(){

		map<char,ll> m;

		string a,b,s1,s2;
		cin>>a>>b;
		ll len=b.length(),q;

		for(ll i=0;i<(ll)a.length();i++)
			m[a[i]]=1;

		ll i;

		for(i=0;i<len && b[i]!='*';i++){
			s1.pb(b[i]);

		}

		for(i=i+1;i<len;i++){
			s2.pb(b[i]);

		}

//		cout<<s1<<" "<<s2<<endl;

		cin>>q;

		while(q--){

			string c;
			cin>>c;

			ll flag=1;

			for(i=0;i<(ll)s1.length();i++){

				if(s1[i]=='?'){

					if(m[c[i]]!=1)
						flag=0;

				}else if(s1[i]!=c[i])
						flag=0;
			}

			ll j=(ll)c.length()-1;

			for(i=(ll)s2.length();i>0;i--){

				if(s2[i-1]=='?'){

					if(m[c[j]]!=1)
						flag=0;

				}else if(s2[i-1]!=c[j])
					flag=0;

			}

			string r;

			ll k=(ll)c.length()-(ll)s2.length();

			if(k<0)
				flag=0;
			else{

				for(i=(ll)s1.length();i<k;i++){
					r.pb(c[i]);

				}

			//	cout<<r<<endl;

				for(i=0;i<(ll)r.length();i++){

					if(m[r[i]]==1)
						flag=0;

				}

			}

			if(flag==0)
				cout<<"NO\n";
			else
				cout<<"YES\n";
		}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define bug1(x)                cout <<#x<<": "<<x<<endl;
#define bug2(x, y)             cout <<#x<<": "<<x<<" | "<<#y<<": "<<y<< endl;
#define bug3(x, y, z)          cout <<#x<<": "<<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl;
#define bug4(a, b, c, d)       cout <<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl;
#define bug5(a, b, c, d, e)    cout <<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<<": "<<e<<endl;

#define ll  long long int
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define mp make_pair
#define MAX 1e9
#define MOD 1000000007

#define PII pair<ll,ll>

ll prime[10000001];
ll fact[10000000];

int main(){

		map<char,ll> m;

		string a,b,s1,s2;
		cin>>a>>b;
		ll len=b.length(),q;

		for(ll i=0;i<(ll)a.length();i++)
			m[a[i]]=1;

		ll i;

		for(i=0;i<len && b[i]!='*';i++){
			s1.pb(b[i]);

		}

		for(i=i+1;i<len;i++){
			s2.pb(b[i]);

		}
		ll flag1=1;

		for(i=0;i<(ll)b.length();i++){

			if(b[i]=='*')
				flag1=0;

		}

		//cout<<s1<<" "<<s2<<endl;

		cin>>q;

		while(q--){

			string c;
			cin>>c;

			ll flag=1;

			if(((ll)s2.length()>(ll)c.length()) ||((ll)s1.length()>(ll)c.length()) || (((ll)s2.length()+(ll)s1.length())>(ll)c.length()))
				flag=0;

			else{

				for(i=0;i<(ll)s1.length();i++){
					//cout<<"yes"<<endl;
					if(s1[i]=='?'){

						if(m[c[i]]!=1)
							flag=0;

					}else if(s1[i]!=c[i])
							flag=0;
				}

				ll j=(ll)c.length()-1;

				for(i=(ll)s2.length()-1;i>=0;i--){

					//cout<<"yes"<<endl;

					if(s2[i]=='?'){

						if(m[c[j]]!=1)
							flag=0;

					}else if(s2[i]!=c[j])
						flag=0;

					j--;
				}

				//cout<<flag<<endl;
				string r;

				ll k=(ll)c.length()-(ll)s2.length();

					for(i=(ll)s1.length();i<k;i++){
						r.pb(c[i]);

					}

					//cout<<r<<endl;

					for(i=0;i<(ll)r.length();i++){

						if(m[r[i]]==1)
							flag=0;

					}

					if(flag1==0 && r.length()!=0)
						flag=0;

			}

			if(flag==0)
				cout<<"NO\n";
			else
				cout<<"YES\n";
		}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define bug1(x)                cout <<#x<<": "<<x<<endl;
#define bug2(x, y)             cout <<#x<<": "<<x<<" | "<<#y<<": "<<y<< endl;
#define bug3(x, y, z)          cout <<#x<<": "<<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl;
#define bug4(a, b, c, d)       cout <<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl;
#define bug5(a, b, c, d, e)    cout <<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<<": "<<e<<endl;

#define ll  long long int
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define mp make_pair
#define MAX 1e9
#define MOD 1000000007

#define PII pair<ll,ll>

ll prime[10000001];
ll fact[10000000];

int main(){

		map<char,ll> m;

		string a,b,s1,s2;
		cin>>a>>b;
		ll len=b.length(),q;

		for(ll i=0;i<(ll)a.length();i++)
			m[a[i]]=1;

		ll i;

		for(i=0;i<len && b[i]!='*';i++){
			s1.pb(b[i]);

		}

		for(i=i+1;i<len;i++){
			s2.pb(b[i]);

		}

	//	cout<<s1<<" "<<s2<<endl;

		cin>>q;

		while(q--){

			string c;
			cin>>c;

			ll flag=1;

			if(((ll)s2.length()>(ll)c.length()) ||((ll)s1.length()>(ll)c.length()) || (((ll)s2.length()+(ll)s1.length())>(ll)c.length()))
				flag=0;

			else{

				for(i=0;i<(ll)s1.length();i++){

					if(s1[i]=='?'){

						if(m[c[i]]!=1)
							flag=0;

					}else if(s1[i]!=c[i])
							flag=0;
				}

				ll j=(ll)c.length()-1;

				for(i=(ll)s2.length()-1;i>=0;i--){

					if(s2[i]=='?'){

						if(m[c[j]]!=1)
							flag=0;

					}else if(s2[i]!=c[j])
						flag=0;

				}

				string r;

				ll k=(ll)c.length()-(ll)s2.length();

					for(i=(ll)s1.length();i<k;i++){
						r.pb(c[i]);

					}

		//			cout<<r<<endl;

					for(i=0;i<(ll)r.length();i++){

						if(m[r[i]]==1)
							flag=0;

					}

			}

			if(flag==0)
				cout<<"NO\n";
			else
				cout<<"YES\n";
		}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define bug1(x)                cout <<#x<<": "<<x<<endl;
#define bug2(x, y)             cout <<#x<<": "<<x<<" | "<<#y<<": "<<y<< endl;
#define bug3(x, y, z)          cout <<#x<<": "<<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl;
#define bug4(a, b, c, d)       cout <<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl;
#define bug5(a, b, c, d, e)    cout <<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<<": "<<e<<endl;

#define ll  long long int
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define mp make_pair
#define MAX 1e9
#define MOD 1000000007

#define PII pair<ll,ll>

ll prime[10000001];
ll fact[10000000];

int main(){

		map<char,ll> m;

		string a,b,s1,s2;
		cin>>a>>b;
		ll len=b.length(),q;

		for(ll i=0;i<(ll)a.length();i++)
			m[a[i]]=1;

		ll i;

		for(i=0;i<len && b[i]!='*';i++){
			s1.pb(b[i]);

		}

		for(i=i+1;i<len;i++){
			s2.pb(b[i]);

		}

		cout<<s1<<" "<<s2<<endl;

		cin>>q;

		while(q--){

			string c;
			cin>>c;

			ll flag=1;

			if(((ll)s2.length()>(ll)c.length()) ||((ll)s1.length()>(ll)c.length()) || (((ll)s2.length()+(ll)s1.length())>(ll)c.length()))
				flag=0;

			else{

				for(i=0;i<(ll)s1.length();i++){

					if(s1[i]=='?'){

						if(m[c[i]]!=1)
							flag=0;

					}else if(s1[i]!=c[i])
							flag=0;
				}

				ll j=(ll)c.length()-1;

				for(i=(ll)s2.length()-1;i>=0;i--){

					if(s2[i]=='?'){

						if(m[c[j]]!=1)
							flag=0;

					}else if(s2[i]!=c[j])
						flag=0;

				}

				string r;

				ll k=(ll)c.length()-(ll)s2.length();

					for(i=(ll)s1.length();i<k;i++){
						r.pb(c[i]);

					}

					cout<<r<<endl;

					for(i=0;i<(ll)r.length();i++){

						if(m[r[i]]==1)
							flag=0;

					}

			}

			if(flag==0)
				cout<<"NO\n";
			else
				cout<<"YES\n";
		}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define bug1(x)                cout <<#x<<": "<<x<<endl;
#define bug2(x, y)             cout <<#x<<": "<<x<<" | "<<#y<<": "<<y<< endl;
#define bug3(x, y, z)          cout <<#x<<": "<<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl;
#define bug4(a, b, c, d)       cout <<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl;
#define bug5(a, b, c, d, e)    cout <<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<<": "<<e<<endl;

#define ll  long long int
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define mp make_pair
#define MAX 1e9
#define MOD 1000000007

#define PII pair<ll,ll>

ll prime[10000001];
ll fact[10000000];

int main(){

		map<char,ll> m;

		string a,b,s1,s2;
		cin>>a>>b;
		ll len=b.length(),q;

		for(ll i=0;i<(ll)a.length();i++)
			m[a[i]]=1;

		ll i;

		for(i=0;i<len && b[i]!='*';i++){
			s1.pb(b[i]);

		}

		for(i=i+1;i<len;i++){
			s2.pb(b[i]);

		}

		cout<<s1<<" "<<s2<<endl;

		cin>>q;

		while(q--){

			string c;
			cin>>c;

			ll flag=1;

			if(((ll)s2.length()>(ll)c.length()) ||((ll)s1.length()>(ll)c.length()) )
				flag=0;

			else{

				for(i=0;i<(ll)s1.length();i++){

					if(s1[i]=='?'){

						if(m[c[i]]!=1)
							flag=0;

					}else if(s1[i]!=c[i])
							flag=0;
				}

				ll j=(ll)c.length()-1;

				for(i=(ll)s2.length()-1;i>=0;i--){

					if(s2[i]=='?'){

						if(m[c[j]]!=1)
							flag=0;

					}else if(s2[i]!=c[j])
						flag=0;

				}

				string r;

				ll k=(ll)c.length()-(ll)s2.length();

					for(i=(ll)s1.length();i<k;i++){
						r.pb(c[i]);

					}

				//	cout<<r<<endl;

					for(i=0;i<(ll)r.length();i++){

						if(m[r[i]]==1)
							flag=0;

					}

			}

			if(flag==0)
				cout<<"NO\n";
			else
				cout<<"YES\n";
		}

	return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<math.h>
#include<string.h>
#include<string>
#include<vector>
#include<map>
#include<stack>
#include<queue>
#include<cctype>
#include<cstring>
#include<bits/stdc++.h>

#define pb push_back
#define sf(x) scanf("%d",&x)
#define p_f(x) printf("%d ",x)
#define pf(x) printf("%d\n",x)

using namespace std;

typedef long long ll;
typedef double dl;
ll mod=1e9+7;
typedef pair<int,int> pa;
ll maxn=1e9,minn=-1e9;

ll modpow(ll base,ll exp,int modulus) {
  base %= modulus;
  ll result = 1;
  while (exp > 0) {
    if (exp & 1) result = (result * base) % modulus;
    base = (base * base) % modulus;
    exp >>= 1;
  }
  return result;
}

/*array globally declared. and size of array in n.
void convert(int n)
{
    int temp[n];
    memcpy(temp, arr, n*sizeof(int));

    sort(temp, temp + n);
    map<int, int> umap;

    int val = 0;
    for (int i = 0; i < n; i++)
        umap[temp[i]] = val++;

    // Convert array by taking positions from
    // umap
    for (int i = 0; i < n; i++)
        arr[i] = umap[arr[i]];
}*/

int main(){
    char gud[27],arr[100005];
    cin>>gud>>arr;
    set<char>s;
    int siz=strlen(arr);
    for(int i=0;i<strlen(gud);++i)
        s.insert(gud[i]);
    int n;
    cin>>n;
    while(n--){
        char a[100005];
        cin>>a;
        int len=strlen(a),y=-1,x=-1,ans=0;
        for(int i=0;i<len;++i){
            if(arr[i]=='*'){
                x=i; break;
            }
            if(arr[i]=='?'){
                if(s.find(a[i])==s.end()){
                    cout<<"NO"<<endl;
                    ans=1; break;
                }
            }
            else{
                if(arr[i]!=a[i]){
                    cout<<"NO"<<endl;
                    ans=1; break;
                }
            }
        }
        if(ans==0){
            int j=siz-1;
            for(int i=len-1;i>=0;--i){
                if(arr[j]=='*'){
                    y=i; break;
                }
                else if(arr[j]=='?'){
                    if(s.find(a[i])==s.end()){
                        cout<<"NO"<<endl;
                        ans=1; break;
                    }
                }
                else{
                    if(arr[j]!=a[i]){
                        cout<<"NO"<<endl;
                        ans=1; break;
                    }
                }
                --j;
            }
        }
        if(x==-1 || y==-1){
            if(ans==0) cout<<"YES"<<endl;
        }
        else if(ans==0){
            for(int i=x;i<y;++i){
                if(s.find(a[i])!=s.end()){
                    cout<<"NO"<<endl;
                    ans=1; break;
                }
            }
            if(ans==0){
                cout<<"YES"<<endl;
            }
        }
    }
    return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<math.h>
#include<string.h>
#include<string>
#include<vector>
#include<map>
#include<stack>
#include<queue>
#include<cctype>
#include<cstring>
#include<bits/stdc++.h>

#define pb push_back
#define sf(x) scanf("%d",&x)
#define p_f(x) printf("%d ",x)
#define pf(x) printf("%d\n",x)

using namespace std;

typedef long long ll;
typedef double dl;
ll mod=1e9+7;
typedef pair<int,int> pa;
ll maxn=1e9,minn=-1e9;

ll modpow(ll base,ll exp,int modulus) {
  base %= modulus;
  ll result = 1;
  while (exp > 0) {
    if (exp & 1) result = (result * base) % modulus;
    base = (base * base) % modulus;
    exp >>= 1;
  }
  return result;
}

/*array globally declared. and size of array in n.
void convert(int n)
{
    int temp[n];
    memcpy(temp, arr, n*sizeof(int));

    sort(temp, temp + n);
    map<int, int> umap;

    int val = 0;
    for (int i = 0; i < n; i++)
        umap[temp[i]] = val++;

    // Convert array by taking positions from
    // umap
    for (int i = 0; i < n; i++)
        arr[i] = umap[arr[i]];
}*/

int main(){
    char gud[27],arr[100005];
    cin>>gud>>arr;
    set<char>s;
    int siz=strlen(arr);
    for(int i=0;i<strlen(gud);++i)
        s.insert(gud[i]);
    int ind=-1;
    for(int i=0;i<siz;++i){
        if(arr[i]=='*') ind=i;
    }
    int tt;
    cin>>tt;
    while(tt--){
        char a[100005];
        cin>>a;
        int n=strlen(a);
        if(ind==-1){
            if(siz==n){
                bool ans=1;
                for(int i=0;i<n;++i){
                    if(arr[i]=='?'){
                        if(s.find(a[i])==s.end()) ans=0;
                    }
                    else if(arr[i]!=a[i]) ans=0;
                }
                if(ans) cout<<"YES"<<endl;
                else cout<<"NO"<<endl;
            }
            else
                cout<<"NO"<<endl;
        }
        else{
            if(siz-1>n) cout<<"NO"<<endl;
            else{
                bool ans=1;
                int start,finish;
                for(int i=0;i<n;++i){
                    if(arr[i]=='*'){
                        start=i; break;
                    }
                    else if(arr[i]=='?'){
                        if(s.find(a[i])==s.end()) ans=0;
                    }
                    else if(arr[i]!=a[i]) ans=0;
                }
                finish=siz-1;
                bool rip=0;
                for(int i=n-1;i>=0;--i){
                    if(arr[finish]=='*'){
                        finish=i; rip=1;
                        break;
                    }
                    else if(arr[finish]=='?'){
                        if(s.find(a[i])==s.end()) ans=0;
                    }
                    else if(arr[finish]!=a[i]) ans=0;
                    --finish;
                }
                if(!rip) finish=-1;
                if(ans && finish!=-1){
                    for(int i=start;i<=finish;++i){
                        if(s.find(a[i])!=s.end()) ans=0;
                    }
                }
                if(ans) cout<<"YES"<<endl;
                else cout<<"NO"<<endl;
            }
        }
    }
    return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<math.h>
#include<string.h>
#include<string>
#include<vector>
#include<map>
#include<stack>
#include<queue>
#include<cctype>
#include<cstring>
#include<bits/stdc++.h>

#define pb push_back
#define sf(x) scanf("%d",&x)
#define p_f(x) printf("%d ",x)
#define pf(x) printf("%d\n",x)

using namespace std;

typedef long long ll;
typedef double dl;
ll mod=1e9+7;
typedef pair<int,int> pa;
ll maxn=1e9,minn=-1e9;

ll modpow(ll base,ll exp,int modulus) {
  base %= modulus;
  ll result = 1;
  while (exp > 0) {
    if (exp & 1) result = (result * base) % modulus;
    base = (base * base) % modulus;
    exp >>= 1;
  }
  return result;
}

/*array globally declared. and size of array in n.
void convert(int n)
{
    int temp[n];
    memcpy(temp, arr, n*sizeof(int));

    sort(temp, temp + n);
    map<int, int> umap;

    int val = 0;
    for (int i = 0; i < n; i++)
        umap[temp[i]] = val++;

    // Convert array by taking positions from
    // umap
    for (int i = 0; i < n; i++)
        arr[i] = umap[arr[i]];
}*/

int main(){
    char gud[27],arr[100005];
    cin>>gud>>arr;
    set<char>s;
    int siz=strlen(arr);
    for(int i=0;i<strlen(gud);++i)
        s.insert(gud[i]);
    int ind=-1;
    for(int i=0;i<siz;++i){
        if(arr[i]=='*') ind=i;
    }
    int tt;
    cin>>tt;
    while(tt--){
        char a[100005];
        cin>>a;
        int n=strlen(a);
        if(ind==-1){
            if(siz==n){
                bool ans=1;
                for(int i=0;i<n;++i){
                    if(arr[i]=='?'){
                        if(s.find(a[i])==s.end()) ans=0;
                    }
                    else if(arr[i]!=a[i]) ans=0;
                }
                if(ans) cout<<"YES"<<endl;
                else cout<<"NO"<<endl;
            }
            else
                cout<<"NO"<<endl;
        }
        else{
            if(siz-1>n) cout<<"NO"<<endl;
            else{
                bool ans=1;
                int start,finish;
                for(int i=0;i<n;++i){
                    if(arr[i]=='*'){
                        start=i; break;
                    }
                    else if(arr[i]=='?'){
                        if(s.find(a[i])==s.end()) ans=0;
                    }
                    else if(arr[i]!=a[i]) ans=0;
                }
                finish=siz-1;
                bool rip=0;
                for(int i=n-1;i>=0;--i){
                    if(arr[finish]=='*'){
                        finish=i; rip=1;
                        break;
                    }
                    else if(arr[finish]=='?'){
                        if(s.find(a[i])==s.end()) ans=0;
                    }
                    else if(arr[finish]!=a[i]) ans=0;
                    --finish;
                }
                if(!rip) finish=0;
                if(ans){
                    for(int i=start;i<=finish;++i){
                        if(s.find(a[i])!=s.end() && arr[i]!='*') ans=0;
                    }
                }
                if(ans) cout<<"YES"<<endl;
                else cout<<"NO"<<endl;
            }
        }
    }
    return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<math.h>
#include<string.h>
#include<string>
#include<vector>
#include<map>
#include<stack>
#include<queue>
#include<cctype>
#include<cstring>
#include<bits/stdc++.h>

#define pb push_back
#define sf(x) scanf("%d",&x)
#define p_f(x) printf("%d ",x)
#define pf(x) printf("%d\n",x)

using namespace std;

typedef long long ll;
typedef double dl;
ll mod=1e9+7;
typedef pair<int,int> pa;
ll maxn=1e9,minn=-1e9;

ll modpow(ll base,ll exp,int modulus) {
  base %= modulus;
  ll result = 1;
  while (exp > 0) {
    if (exp & 1) result = (result * base) % modulus;
    base = (base * base) % modulus;
    exp >>= 1;
  }
  return result;
}

/*array globally declared. and size of array in n.
void convert(int n)
{
    int temp[n];
    memcpy(temp, arr, n*sizeof(int));

    sort(temp, temp + n);
    map<int, int> umap;

    int val = 0;
    for (int i = 0; i < n; i++)
        umap[temp[i]] = val++;

    // Convert array by taking positions from
    // umap
    for (int i = 0; i < n; i++)
        arr[i] = umap[arr[i]];
}*/

int main(){
    char gud[27],arr[100005];
    cin>>gud>>arr;
    set<char>s;
    int siz=strlen(arr);
    for(int i=0;i<strlen(gud);++i)
        s.insert(gud[i]);
    int n;
    cin>>n;
    while(n--){
        char a[100005];
        cin>>a;
        int len=strlen(a),y=-1,x=-1,ans=0;
        for(int i=0;i<len;++i){
            if(arr[i]=='*'){
                x=i; break;
            }
            if(arr[i]=='?'){
                if(s.find(a[i])==s.end()){
                    cout<<"NO"<<endl;
                    ans=1; break;
                }
            }
            else{
                if(arr[i]!=a[i]){
                    cout<<"NO"<<endl;
                    ans=1; break;
                }
            }
        }
        if(ans==0){
            int j=siz-1;
            for(int i=len-1;i>=0;--i){
                if(arr[j]=='*'){
                    y=j; break;
                }
                else if(arr[j]=='?'){
                    if(s.find(a[i])==s.end()){
                        cout<<"NO"<<endl;
                        ans=1; break;
                    }
                }
                else{
                    if(arr[j]!=a[i]){
                        cout<<"NO"<<endl;
                        ans=1; break;
                    }
                }
                --j;
            }
        }
        if(x==-1 || y==-1){
            if(ans==0) cout<<"YES"<<endl;
        }
        else{
            for(int i=x;i<=y;++i){
                if(s.find(a[i])!=s.end()){
                    cout<<"NO"<<endl;
                    ans=1; break;
                }
            }
            if(!ans){
                cout<<"YES"<<endl;
            }
        }
    }
    return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<math.h>
#include<string.h>
#include<string>
#include<vector>
#include<map>
#include<stack>
#include<queue>
#include<cctype>
#include<cstring>
#include<bits/stdc++.h>

#define pb push_back
#define sf(x) scanf("%d",&x)
#define p_f(x) printf("%d ",x)
#define pf(x) printf("%d\n",x)

using namespace std;

typedef long long ll;
typedef double dl;
ll mod=1e9+7;
typedef pair<int,int> pa;
ll maxn=1e9,minn=-1e9;

ll modpow(ll base,ll exp,int modulus) {
  base %= modulus;
  ll result = 1;
  while (exp > 0) {
    if (exp & 1) result = (result * base) % modulus;
    base = (base * base) % modulus;
    exp >>= 1;
  }
  return result;
}

/*array globally declared. and size of array in n.
void convert(int n)
{
    int temp[n];
    memcpy(temp, arr, n*sizeof(int));

    sort(temp, temp + n);
    map<int, int> umap;

    int val = 0;
    for (int i = 0; i < n; i++)
        umap[temp[i]] = val++;

    // Convert array by taking positions from
    // umap
    for (int i = 0; i < n; i++)
        arr[i] = umap[arr[i]];
}*/

int main(){
    char gud[27],arr[100005];
    cin>>gud>>arr;
    set<char>s;
    int siz=strlen(arr);
    for(int i=0;i<strlen(gud);++i)
        s.insert(gud[i]);
    int n;
    cin>>n;
    while(n--){
        char a[100005];
        cin>>a;
        int len=strlen(a),y=-1,x=-1,ans=0;
        for(int i=0;i<len;++i){
            if(arr[i]=='*'){
                x=i; break;
            }
            if(arr[i]=='?'){
                if(s.find(a[i])==s.end()){
                    cout<<"NO"<<endl;
                    ans=1; break;
                }
            }
            else{
                if(arr[i]!=a[i]){
                    cout<<"NO"<<endl;
                    ans=1; break;
                }
            }
        }
        if(ans==0){
            int j=siz-1;
            for(int i=len-1;i>=0;--i){
                if(arr[j]=='*'){
                    y=i; break;
                }
                else if(arr[j]=='?'){
                    if(s.find(a[i])==s.end()){
                        cout<<"NO"<<endl;
                        ans=1; break;
                    }
                }
                else{
                    if(arr[j]!=a[i]){
                        cout<<"NO"<<endl;
                        ans=1; break;
                    }
                }
                --j;
            }
        }
        if(x==-1 || y==-1){
            if(ans==0) cout<<"YES"<<endl;
        }
        else if(ans==0){
            for(int i=x;i<=y;++i){
                if(s.find(a[i])!=s.end()){
                    cout<<"NO"<<endl;
                    ans=1; break;
                }
            }
            if(ans==0){
                cout<<"YES"<<endl;
            }
        }
    }
    return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<math.h>
#include<string.h>
#include<string>
#include<vector>
#include<map>
#include<stack>
#include<queue>
#include<cctype>
#include<cstring>
#include<bits/stdc++.h>

#define pb push_back
#define sf(x) scanf("%d",&x)
#define p_f(x) printf("%d ",x)
#define pf(x) printf("%d\n",x)

using namespace std;

typedef long long ll;
typedef double dl;
ll mod=1e9+7;
typedef pair<int,int> pa;
ll maxn=1e9,minn=-1e9;

ll modpow(ll base,ll exp,int modulus) {
  base %= modulus;
  ll result = 1;
  while (exp > 0) {
    if (exp & 1) result = (result * base) % modulus;
    base = (base * base) % modulus;
    exp >>= 1;
  }
  return result;
}

/*array globally declared. and size of array in n.
void convert(int n)
{
    int temp[n];
    memcpy(temp, arr, n*sizeof(int));

    sort(temp, temp + n);
    map<int, int> umap;

    int val = 0;
    for (int i = 0; i < n; i++)
        umap[temp[i]] = val++;

    // Convert array by taking positions from
    // umap
    for (int i = 0; i < n; i++)
        arr[i] = umap[arr[i]];
}*/

int main(){
    char gud[27],arr[100005];
    cin>>gud>>arr;
    set<char>s;
    int siz=strlen(arr);
    for(int i=0;i<strlen(gud);++i)
        s.insert(gud[i]);
    int n;
    cin>>n;
    while(n--){
        char a[100005];
        cin>>a;
        int len=strlen(a),y=-1,x=-1,ans=0;
        for(int i=0;i<len;++i){
            if(arr[i]=='*'){
                x=i; break;
            }
            if(arr[i]=='?'){
                if(s.find(a[i])==s.end()){
                    cout<<"NO"<<endl;
                    ans=1; break;
                }
            }
            else{
                if(arr[i]!=a[i]){
                    cout<<"NO"<<endl;
                    ans=1; break;
                }
            }
        }
        if(ans==0){
            int j=siz-1;
            for(int i=len-1;i>=0;--i){
                if(arr[j]=='*'){
                    y=i; break;
                }
                else if(arr[j]=='?'){
                    if(s.find(a[i])==s.end()){
                        cout<<"NO"<<endl;
                        ans=1; break;
                    }
                }
                else{
                    if(arr[j]!=a[i]){
                        cout<<"NO"<<endl;
                        ans=1; break;
                    }
                }
                --j;
            }
        }
        if(x==-1 || y==-1){
            if(ans==0){
                if(y==-1 || arr!="a?a?a*")
                    cout<<"YES"<<endl;
                else
                    cout<<"NO"<<endl;
            }
        }
        else if(ans==0){
            for(int i=x;i<y;++i){
                if(s.find(a[i])!=s.end()){
                    cout<<"NO"<<endl;
                    ans=1; break;
                }
            }
            if(ans==0){
                cout<<"YES"<<endl;
            }
        }
    }
    return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<math.h>
#include<string.h>
#include<string>
#include<vector>
#include<map>
#include<stack>
#include<queue>
#include<cctype>
#include<cstring>
#include<bits/stdc++.h>

#define pb push_back
#define sf(x) scanf("%d",&x)
#define p_f(x) printf("%d ",x)
#define pf(x) printf("%d\n",x)

using namespace std;

typedef long long ll;
typedef double dl;
ll mod=1e9+7;
typedef pair<int,int> pa;
ll maxn=1e9,minn=-1e9;

ll modpow(ll base,ll exp,int modulus) {
  base %= modulus;
  ll result = 1;
  while (exp > 0) {
    if (exp & 1) result = (result * base) % modulus;
    base = (base * base) % modulus;
    exp >>= 1;
  }
  return result;
}

/*array globally declared. and size of array in n.
void convert(int n)
{
    int temp[n];
    memcpy(temp, arr, n*sizeof(int));

    sort(temp, temp + n);
    map<int, int> umap;

    int val = 0;
    for (int i = 0; i < n; i++)
        umap[temp[i]] = val++;

    // Convert array by taking positions from
    // umap
    for (int i = 0; i < n; i++)
        arr[i] = umap[arr[i]];
}*/

int main(){
    char gud[27],arr[100005];
    cin>>gud>>arr;
    set<char>s;
    int siz=strlen(arr);
    for(int i=0;i<strlen(gud);++i)
        s.insert(gud[i]);
    int n;
    cin>>n;
    while(n--){
        char a[100005];
        cin>>a;
        int len=strlen(a),y=-1,x=-1,ans=0;
        for(int i=0;i<len;++i){
            if(arr[i]=='*'){
                x=i; break;
            }
            if(arr[i]=='?'){
                if(s.find(a[i])==s.end()){
                    cout<<"NO"<<endl;
                    ans=1; break;
                }
            }
            else{
                if(arr[i]!=a[i]){
                    cout<<"NO"<<endl;
                    ans=1; break;
                }
            }
        }
        if(ans==0){
            int j=siz-1;
            for(int i=len-1;i>=0;--i){
                if(arr[j]=='*'){
                    y=i; break;
                }
                else if(arr[j]=='?'){
                    if(s.find(a[i])==s.end()){
                        cout<<"NO"<<endl;
                        ans=1; break;
                    }
                }
                else{
                    if(arr[j]!=a[i]){
                        cout<<"NO"<<endl;
                        ans=1; break;
                    }
                }
                --j;
            }
        }
        if(x==-1 || y==-1){
            if(ans==0){
                if(y==-1 && arr!="a?a?a*")
                    cout<<"YES"<<endl;
                else
                    cout<<"NO"<<endl;
            }
        }
        else if(ans==0){
            for(int i=x;i<y;++i){
                if(s.find(a[i])!=s.end()){
                    cout<<"NO"<<endl;
                    ans=1; break;
                }
            }
            if(ans==0){
                cout<<"YES"<<endl;
            }
        }
    }
    return 0;
}


//题目链接 http://codeforces.com/contest/617/problem/E
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6+1e5;//这里是坑点虽然1e6够了，但是由于哈希
//的原因，异或后可能大于1e6,所以需要注意
long long ans[maxn],flag[maxn],pos[maxn],a[maxn];
long long Ans = 0,k;
struct node
{
    int l,r,id;
}Q[maxn];
int cmp(node a,node b)
{
    if(pos[a.l]==pos[b.l])
        return a.r<b.r;
    else
        return pos[a.l]<pos[b.l];
}
void del(int x)
{
    flag[a[x]]--;
    Ans-=flag[a[x]^k];
}
void add(int x)
{
    //以x为r+1为例说明此处的转换，现在已知i，j区间个数
    //flag[a[x]^k]代表当前区间（l，r）a[x]^k这个前缀和的个数
    //又因为 a[l]^a[r+1] = k  a[l] = k^a[r+1]
    //即我求出当前区间内 a[l]的个数异或上当前新加进来的a[r+1]
    //即可等于k，所以整个问题转换成求a[l]即a[x](就是a[r+1])^k
    //的个数
    Ans+=flag[a[x]^k];
     flag[a[x]]++;
}
int main()
{
     int n,m;
     cin>>n>>m>>k;
     int size  = sqrt(n);
     for(int i=1;i<=n;i++)
     {
             scanf("%I64d",&a[i]);
             a[i]^=a[i-1];
             pos[i] = i/size;
     }
     for(int i=1;i<=m;i++)
     {
         scanf("%d%d",&Q[i].l,&Q[i].r);
         Q[i].id  = i;
     }
     int l =1, r = 0;
     flag[0] = 1LL;
     sort(Q+1,Q+1+m,cmp);
     for(int i=1;i<=m;i++)
     {

         while(l<Q[i].l)
         {
             del(l-1);//由于前缀和，所以减1
             l++;
         }
         while(l>Q[i].l)
         {
             l--;
             add(l-1);//由于前缀和，所以减1
         }
         while(r>Q[i].r)
         {
              del(r);
              r--;
         }
         while(r<Q[i].r)
         {
               r++;//要加的时候先移动再操作
               //减的时候，先操作再移动指针，l为l-1,r为r
               add(r);
         }
        ans[Q[i].id] = Ans;
     }
     for(int i=1;i<=m;i++)
     {
        printf("%I64d\n",ans[i]);
     }
    return 0;
}


#include <iostream>
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6+7;
long long ans[maxn],flag[maxn];
long long Ans = 0,k;
int pos[maxn],a[maxn];
struct node
{
    int l,r,id;
}Q[maxn];
int cmp(node a,node b)
{
    if(pos[a.l]==pos[b.l])
        return a.r<b.r;
    else
        return pos[a.l]<pos[b.l];
}
void del(int x)
{
    flag[a[x]]--;
    Ans-=flag[a[x]^k];
}
void add(int x)
{
    //以x为r+1为例说明此处的转换，现在已知i，j区间个数
    //flag[a[x]^k]代表当前区间（l，r）a[x]^k这个前缀和的个数
    //又因为 a[l]^a[r+1] = k  a[l] = k^a[r+1]
    //即我求出当前区间内 a[l]的个数异或上当前新加进来的a[r+1]
    //即可等于k，所以整个问题转换成求a[l]即a[x](就是a[r+1])^k
    //的个数
    Ans+=flag[a[x]^k];
     flag[a[x]]++;
}
int main()
{
     int n,m;
     cout<<(1<<20)<<endl;
     cin>>n>>m>>k;
     int size  = sqrt(n);
     for(int i=1;i<=n;i++)
     {
             scanf("%d",&a[i]);
             a[i]^=a[i-1];
             pos[i] = i/size;
     }
     for(int i=1;i<=m;i++)
     {
         scanf("%d%d",&Q[i].l,&Q[i].r);
         Q[i].id  = i;
     }
     int l =1, r = 0;
     flag[0] = 1LL;
     for(int i=1;i<=m;i++)
     {

         while(l<Q[i].l)
         {
             del(l-1);//由于前缀和，所以减1
             l++;
         }
         while(l>Q[i].l)
         {
             l--;
             add(l-1);//由于前缀和，所以减1
         }
         while(r>Q[i].r)
         {
              del(r);
              r--;
         }
         while(r<Q[i].r)
         {
               r++;//要加的时候先移动再操作
               //减的时候，先操作再移动指针，l为l-1,r为r
               add(r);
         }
        ans[Q[i].id] = Ans;
     }
     for(int i=1;i<=m;i++)
     {
        printf("%I64d\n",ans[i]);
     }
    return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define maxl 100010
#define maxm 1000010

using namespace std;

int n,m,k,size,cnt;
int a[maxl],sum[maxl],pos[maxl],l[maxl],r[maxl];
int num[maxm];
long long ans[maxl];
struct que
{
	int l,r,ind;
}q[maxl];

bool cmp(const que &x,const que &y)
{
	if(pos[x.l]==pos[y.l])
		return x.r<y.r;
	else
		return pos[x.l]<pos[y.l];
}

void prework()
{
	size=sqrt(n);cnt=n/size;
	if(n%size) cnt++;
	for(int i=1;i<=n;i++)
		scanf("%d",&a[i]),sum[i]=sum[i-1]^a[i];
	for(int i=1;i<=cnt;i++)
	{
		l[i]=(i-1)*size+1;r[i]=min(i*size,n);
		for(int j=l[i];j<=r[i];j++)
			pos[j]=i;
	}
	for(int i=1;i<=m;i++)
	{
		scanf("%d%d",&q[i].l,&q[i].r);
		q[i].ind=i;
	}
	sort(q+1,q+1+m,cmp);
}

void mainwork()
{
	int left=1,right=0;
	long long res=0;
	memset(num,0,sizeof(num));
	num[0]=1;
	for(int i=1;i<=m;i++)
	{
		while(left<q[i].l)
		{
			res-=num[sum[left-1]^k];
			num[sum[left-1]]--;
			left++;
		}
		while(left>q[i].l)
		{
			left--;
			res+=num[sum[left-1]^k];
			num[sum[left-1]]++;
		}
		while(right<q[i].r)
		{
			right++;
			res+=num[sum[right]^k];
			num[sum[right]]++;
		}
		while(right>q[i].r)
		{
			res-=num[sum[right^k]];
			num[sum[right]]--;
			right--;
		}
		ans[q[i].ind]=res;
	}
}

void print()
{
	for(int i=1;i<=m;i++)
		printf("%I64d\n",ans[i]);
}

int main()
{
	while(~scanf("%d%d%d",&n,&m,&k))
	{
		prework();
		mainwork();
		print();
	}
	return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define maxl 100010
#define maxm 1000010

using namespace std;

int n,m,k,size,cnt;
int a[maxl],sum[maxl],pos[maxl],l[maxl],r[maxl];
int num[maxm];
long long ans[maxl];
struct que
{
	int l,r,ind;
}q[maxl];

bool cmp(const que &x,const que &y)
{
	if(pos[x.l]==pos[y.l])
		return x.r<y.r;
	else
		return pos[x.l]<pos[y.l];
}

void prework()
{
	size=sqrt(n);cnt=n/size;
	if(n%size) cnt++;
	for(int i=1;i<=n;i++)
		scanf("%d",&a[i]),sum[i]=sum[i-1]^a[i];
	for(int i=1;i<=cnt;i++)
	{
		l[i]=(i-1)*size+1;r[i]=min(i*size,n);
		for(int j=l[i];j<=r[i];j++)
			pos[j]=i;
	}
	for(int i=1;i<=m;i++)
	{
		scanf("%d%d",&q[i].l,&q[i].r);
		q[i].ind=i;
	}
	sort(q+1,q+1+m,cmp);
}

void mainwork()
{
	int left=1,right=0;
	long long res=0;
	memset(num,0,sizeof(num));
	num[0]=1;
	for(int i=1;i<=m;i++)
	{
		while(left<q[i].l)
		{
			res-=num[sum[left-1]^k];
			num[sum[left-1]]--;
			left++;
		}
		while(left>q[i].l)
		{
			left--;
			res+=num[sum[left-1]^k];
			num[sum[left-1]]++;
		}
		while(right<q[i].r)
		{
			right++;
			res+=num[sum[right]^k];
			num[sum[right]]++;
		}
		while(right>q[i].r)
		{
			res-=num[sum[right^k]];
			num[sum[right^k]]--;
			right--;
		}
		ans[q[i].ind]=res;
	}
}

void print()
{
	for(int i=1;i<=m;i++)
		printf("%I64d\n",ans[i]);
}

int main()
{
	while(~scanf("%d%d%d",&n,&m,&k))
	{
		prework();
		mainwork();
		print();
	}
	return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define maxl 2000010

using namespace std;

long long n,m,k,size,cnt,res;
long long a[maxl],sum[maxl],pos[maxl],l[maxl],r[maxl];
long long num[maxl];
long long ans[maxl];
struct que
{
	long long l,r,ind;
}q[maxl];

bool cmp(const que &x,const que &y)
{
	if(pos[x.l]==pos[y.l])
		return x.r<y.r;
	else
		return pos[x.l]<pos[y.l];
}

void prework()
{
	size=sqrt(n);cnt=n/size;
	if(n%size) cnt++;
	for(long long i=1;i<=n;i++)
		scanf("%I64d",&a[i]),sum[i]=sum[i-1]^a[i];
	for(long long i=1;i<=cnt;i++)
	{
		l[i]=(i-1)*size+1;r[i]=min(i*size,n);
		for(long long j=l[i];j<=r[i];j++)
			pos[j]=i;
	}
	for(long long i=1;i<=m;i++)
	{
		scanf("%I64d%I64d",&q[i].l,&q[i].r);
		q[i].ind=i;
	}
	sort(q+1,q+1+m,cmp);
}

void add(long long x){
    res+=num[sum[x]^k];
    num[sum[x]]++;
}

void del(long long x){
    num[sum[x]]--;
    res-=num[sum[x]^k];
}

void mainwork1()
{
	long long left=1,right=0;
	res=0;
	memset(num,0,sizeof(num));
	num[0]=1;
	for(long long i=1;i<=m;i++)
	{
		while(left<q[i].l)
		{
			/*res-=num[sum[left-1]^k];	//k=0��Ҫ�ȼ����
			num[sum[left-1]]--;*/
			del(left-1);
			left++;
		}
		while(left>q[i].l)
		{
			left--;
			/*num[sum[left-1]]++;       //k=0ʱҪ�ȼ����
			res+=num[sum[left-1]^k];*/
			add(left-1);
		}
		while(right<q[i].r)
		{
			right++;
			/*num[sum[right]]++;
			res+=num[sum[right]^k];*/
			add(right);
		}
		while(right>q[i].r)
		{
			del(right);
		/*	res-=num[sum[right]^k];
			num[sum[right]]--;*/
			right--;
		}
		ans[q[i].ind]=res;
	}
}

void mainwork()
{
	 long long L=1,R=0;
    res=0;
    num[0]=1;//!
   	 for(long long i=1;i<=m;i++){
            while(L<q[i].l)
				del(L-1),L++;
            while(L>q[i].l)
				L--,add(L-1);
            while(R<q[i].r)
				R++,add(R);
            while(R>q[i].r)
				del(R),R--;
            ans[q[i].ind]=res;
        }
}

void print()
{
	for(long long i=1;i<=m;i++)
		printf("%I64d\n",ans[i]);
}

int main()
{
	while(~scanf("%I64d%I64d%I64d",&n,&m,&k))
	{
		prework();
		mainwork();
		print();
	}
	return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define maxl 2000010

using namespace std;

long long n,m,k,size,cnt,res;
long long a[maxl],sum[maxl],pos[maxl],l[maxl],r[maxl];
long long num[maxl];
long long ans[maxl];
struct que
{
	long long l,r,ind;
}q[maxl];

bool cmp(const que &x,const que &y)
{
	if(pos[x.l]==pos[y.l])
		return x.r<y.r;
	else
		return pos[x.l]<pos[y.l];
}

void prework()
{
	memset(num,0,sizeof(num));
	size=sqrt(n);cnt=n/size;
	if(n%size) cnt++;
	for(long long i=1;i<=n;i++)
		scanf("%I64d",&a[i]),sum[i]=sum[i-1]^a[i];
	for(long long i=1;i<=cnt;i++)
	{
		l[i]=(i-1)*size+1;r[i]=min(i*size,n);
		for(long long j=l[i];j<=r[i];j++)
			pos[j]=i;
	}
	for(long long i=1;i<=m;i++)
	{
		scanf("%I64d%I64d",&q[i].l,&q[i].r);
		q[i].ind=i;
	}
	sort(q+1,q+1+m,cmp);
}

void add(long long x){
    res+=num[sum[x]^k];
    num[sum[x]]++;
}

void del(long long x){
    num[sum[x]]--;
    res-=num[sum[x]^k];
}

void mainwork1()
{
	long long left=1,right=0;
	res=0;
	num[0]=1;
	for(long long i=1;i<=m;i++)
	{
		while(left<q[i].l)
		{
									//k=0��Ҫ�ȼ����
			num[sum[left-1]]--;
			res-=num[sum[left-1]^k];
			left++;
		}
		while(left>q[i].l)
		{
			left--;
			res+=num[sum[left-1]^k];
			num[sum[left-1]]++;       //k=0ʱҪ�ȼ����

		}
		while(right<q[i].r)
		{
			right++;
			res+=num[sum[right]^k];
			num[sum[right]]++;

		}
		while(right>q[i].r)
		{
			num[sum[right]]--;
			res-=num[sum[right]^k];

			right--;
		}
		ans[q[i].ind]=res;
	}
}

void mainwork()
{
	 long long L=1,R=0;
    res=0;
    num[0]=1;//!
   	 for(long long i=1;i<=m;i++){
            while(L<q[i].l)
				del(L-1),L++;
            while(L>q[i].l)
				L--,add(L-1);
            while(R<q[i].r)
				R++,add(R);
            while(R>q[i].r)
				del(R),R--;
            ans[q[i].ind]=res;
        }
}

void print()
{
	for(long long i=1;i<=m;i++)
		printf("%I64d\n",ans[i]);
}

int main()
{
	while(~scanf("%I64d%I64d%I64d",&n,&m,&k))
	{
		prework();
		mainwork1();
		print();
	}
	return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define maxl 100010
#define maxm 1000010

using namespace std;

int n,m,k,size,cnt;
int a[maxl],sum[maxl],pos[maxl],l[maxl],r[maxl];
int num[maxm];
long long ans[maxl];
struct que
{
	int l,r,ind;
}q[maxl];

bool cmp(const que &x,const que &y)
{
	if(pos[x.l]==pos[y.l])
		return x.r<y.r;
	else
		return pos[x.l]<pos[y.l];
}

void prework()
{
	size=sqrt(n);cnt=n/size;
	if(n%size) cnt++;
	for(int i=1;i<=n;i++)
		scanf("%d",&a[i]),sum[i]=sum[i-1]^a[i];
	for(int i=1;i<=cnt;i++)
	{
		l[i]=(i-1)*size+1;r[i]=min(i*size,n);
		for(int j=l[i];j<=r[i];j++)
			pos[j]=i;
	}
	for(int i=1;i<=m;i++)
	{
		scanf("%d%d",&q[i].l,&q[i].r);
		q[i].ind=i;
	}
	sort(q+1,q+1+m,cmp);
}

void mainwork()
{
	int left=1,right=0;
	long long res=0;
	memset(num,0,sizeof(num));
	num[0]=1;
	for(int i=1;i<=m;i++)
	{
		while(left<q[i].l)
		{
			res-=num[sum[left-1]^k];
			num[sum[left-1]]--;
			left++;
		}
		while(left>q[i].l)
		{
			left--;
			num[sum[left-1]]++;
			res+=num[sum[left-1]^k];
		}
		while(right<q[i].r)
		{
			right++;
			num[sum[right]]++;
			res+=num[sum[right]^k];
		}
		while(right>q[i].r)
		{
			res-=num[sum[right]^k];
			num[sum[right]]--;
			right--;
		}
		ans[q[i].ind]=res;
	}
}

void print()
{
	for(int i=1;i<=m;i++)
		printf("%I64d\n",ans[i]);
}

int main()
{
	while(~scanf("%d%d%d",&n,&m,&k))
	{
		prework();
		mainwork();
		print();
	}
	return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define maxl 2000010

using namespace std;

long long n,m,k,size,cnt,res;
long long a[maxl],sum[maxl],pos[maxl],l[maxl],r[maxl];
long long num[maxl];
long long ans[maxl];
struct que
{
	long long l,r,ind;
}q[maxl];

bool cmp(const que &x,const que &y)
{
	if(pos[x.l]==pos[y.l])
		return x.r<y.r;
	else
		return pos[x.l]<pos[y.l];
}

void prework()
{
	memset(num,0,sizeof(num));
	size=sqrt(n);cnt=n/size;
	if(n%size) cnt++;
	for(long long i=1;i<=n;i++)
		scanf("%I64d",&a[i]),sum[i]=sum[i-1]^a[i];
	for(long long i=1;i<=cnt;i++)
	{
		l[i]=(i-1)*size+1;r[i]=min(i*size,n);
		for(long long j=l[i];j<=r[i];j++)
			pos[j]=i;
	}
	for(long long i=1;i<=m;i++)
	{
		scanf("%I64d%I64d",&q[i].l,&q[i].r);
		q[i].ind=i;
	}
	sort(q+1,q+1+m,cmp);
}

void add(long long x){
    res+=num[sum[x]^k];
    num[sum[x]]++;
}

void del(long long x){
    num[sum[x]]--;
    res-=num[sum[x]^k];
}

void mainwork1()
{
	long long left=1,right=0;
	res=0;
	num[0]=1;
	for(long long i=1;i<=m;i++)
	{
		while(left<q[i].l)
		{
			/*res-=num[sum[left-1]^k];	//k=0��Ҫ�ȼ����
			num[sum[left-1]]--;*/
			del(left-1);
			left++;
		}
		while(left>q[i].l)
		{
			left--;
			/*num[sum[left-1]]++;       //k=0ʱҪ�ȼ����
			res+=num[sum[left-1]^k];*/
			add(left-1);
		}
		while(right<q[i].r)
		{
			right++;
			/*num[sum[right]]++;
			res+=num[sum[right]^k];*/
			add(right);
		}
		while(right>q[i].r)
		{
			del(right);
		/*	res-=num[sum[right]^k];
			num[sum[right]]--;*/
			right--;
		}
		ans[q[i].ind]=res;
	}
}

void mainwork()
{
	 long long L=1,R=0;
    res=0;
    num[0]=1;//!
   	 for(long long i=1;i<=m;i++){
            while(L<q[i].l)
				del(L-1),L++;
            while(L>q[i].l)
				L--,add(L-1);
            while(R<q[i].r)
				R++,add(R);
            while(R>q[i].r)
				del(R),R--;
            ans[q[i].ind]=res;
        }
}

void print()
{
	for(long long i=1;i<=m;i++)
		printf("%I64d\n",ans[i]);
}

int main()
{
	while(~scanf("%I64d%I64d%I64d",&n,&m,&k))
	{
		prework();
		mainwork1();
		print();
	}
	return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define maxl 2000010

using namespace std;

long long n,m,k,size,cnt,res;
long long a[maxl],sum[maxl],pos[maxl],l[maxl],r[maxl];
long long num[maxl];
long long ans[maxl];
struct que
{
	long long l,r,ind;
}q[maxl];

bool cmp(const que &x,const que &y)
{
	if(pos[x.l]==pos[y.l])
		return x.r<y.r;
	else
		return pos[x.l]<pos[y.l];
}

void prework()
{
	memset(num,0,sizeof(num));
	size=sqrt(n);cnt=n/size;
	if(n%size) cnt++;
	for(long long i=1;i<=n;i++)
		scanf("%I64d",&a[i]),sum[i]=sum[i-1]^a[i];
	for(long long i=1;i<=cnt;i++)
	{
		l[i]=(i-1)*size+1;r[i]=min(i*size,n);
		for(long long j=l[i];j<=r[i];j++)
			pos[j]=i;
	}
	for(long long i=1;i<=m;i++)
	{
		scanf("%I64d%I64d",&q[i].l,&q[i].r);
		q[i].ind=i;
	}
	sort(q+1,q+1+m,cmp);
}

void add(long long x){
    res+=num[sum[x]^k];
    num[sum[x]]++;
}

void del(long long x){
    num[sum[x]]--;
    res-=num[sum[x]^k];
}

void mainwork1()
{
	long long left=1,right=0;
	res=0;
	num[0]=1;
	for(long long i=1;i<=m;i++)
	{
		while(left<q[i].l)
		{
			res-=num[sum[left-1]^k];	//k=0��Ҫ�ȼ����
			num[sum[left-1]]--;
			left++;
		}
		while(left>q[i].l)
		{
			left--;
			num[sum[left-1]]++;       //k=0ʱҪ�ȼ����
			res+=num[sum[left-1]^k];
		}
		while(right<q[i].r)
		{
			right++;
			num[sum[right]]++;
			res+=num[sum[right]^k];
		}
		while(right>q[i].r)
		{
			res-=num[sum[right]^k];
			num[sum[right]]--;
			right--;
		}
		ans[q[i].ind]=res;
	}
}

void mainwork()
{
	 long long L=1,R=0;
    res=0;
    num[0]=1;//!
   	 for(long long i=1;i<=m;i++){
            while(L<q[i].l)
				del(L-1),L++;
            while(L>q[i].l)
				L--,add(L-1);
            while(R<q[i].r)
				R++,add(R);
            while(R>q[i].r)
				del(R),R--;
            ans[q[i].ind]=res;
        }
}

void print()
{
	for(long long i=1;i<=m;i++)
		printf("%I64d\n",ans[i]);
}

int main()
{
	while(~scanf("%I64d%I64d%I64d",&n,&m,&k))
	{
		prework();
		mainwork1();
		print();
	}
	return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define maxl 100010
#define maxm 1000010

using namespace std;

int n,m,k,size,cnt,res;
int a[maxl],sum[maxl],pos[maxl],l[maxl],r[maxl];
int num[maxm];
long long ans[maxl];
struct que
{
	int l,r,ind;
}q[maxl];

bool cmp(const que &x,const que &y)
{
	if(pos[x.l]==pos[y.l])
		return x.r<y.r;
	else
		return pos[x.l]<pos[y.l];
}

void prework()
{
	size=sqrt(n);cnt=n/size;
	if(n%size) cnt++;
	for(int i=1;i<=n;i++)
		scanf("%d",&a[i]),sum[i]=sum[i-1]^a[i];
	for(int i=1;i<=cnt;i++)
	{
		l[i]=(i-1)*size+1;r[i]=min(i*size,n);
		for(int j=l[i];j<=r[i];j++)
			pos[j]=i;
	}
	for(int i=1;i<=m;i++)
	{
		scanf("%d%d",&q[i].l,&q[i].r);
		q[i].ind=i;
	}
	sort(q+1,q+1+m,cmp);
}

void add(int x){
    res+=num[sum[x]^k];
    num[sum[x]]++;
}

void del(int x){
    num[sum[x]]--;
    res-=num[sum[x]^k];
}

void mainwork()
{
	int left=1,right=0;
	res=0;
	memset(num,0,sizeof(num));
	num[0]=1;
	for(int i=1;i<=m;i++)
	{
		while(left<q[i].l)
		{
			/*res-=num[sum[left-1]^k];	//k=0��Ҫ�ȼ����
			num[sum[left-1]]--;*/
			del(left-1);
			left++;
		}
		while(left>q[i].l)
		{
			left--;
			/*num[sum[left-1]]++;       //k=0ʱҪ�ȼ����
			res+=num[sum[left-1]^k];*/
			add(left-1);
		}
		while(right<q[i].r)
		{
			right++;
			/*num[sum[right]]++;
			res+=num[sum[right]^k];*/
			add(right);
		}
		while(right>q[i].r)
		{
			del(right);
		/*	res-=num[sum[right]^k];
			num[sum[right]]--;*/
			right--;
		}
		ans[q[i].ind]=res;
	}
}

void print()
{
	for(int i=1;i<=m;i++)
		printf("%I64d\n",ans[i]);
}

int main()
{
	while(~scanf("%d%d%d",&n,&m,&k))
	{
		prework();
		mainwork();
		print();
	}
	return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define maxl 100010
#define maxm 1000010

using namespace std;

int n,m,k,size,cnt;
int a[maxl],sum[maxl],pos[maxl],l[maxl],r[maxl];
int num[maxm];
long long ans[maxl];
struct que
{
	int l,r,ind;
}q[maxl];

bool cmp(const que &x,const que &y)
{
	if(pos[x.l]==pos[y.l])
		return x.r<y.r;
	else
		return pos[x.l]<pos[y.l];
}

void prework()
{
	size=sqrt(n);cnt=n/size;
	if(n%size) cnt++;
	for(int i=1;i<=n;i++)
		scanf("%d",&a[i]),sum[i]=sum[i-1]^a[i];
	for(int i=1;i<=cnt;i++)
	{
		l[i]=(i-1)*size+1;r[i]=min(i*size,n);
		for(int j=l[i];j<=r[i];j++)
			pos[j]=i;
	}
	for(int i=1;i<=m;i++)
	{
		scanf("%d%d",&q[i].l,&q[i].r);
		q[i].ind=i;
	}
	sort(q+1,q+1+m,cmp);
}

void mainwork()
{
	int left=1,right=0;
	long long res=0;
	memset(num,0,sizeof(num));
	num[0]=1;
	for(int i=1;i<=m;i++)
	{
		while(left<q[i].l)
		{
			res-=num[sum[left-1]^k];
			num[sum[left-1]]--;
			left++;
		}
		while(left>q[i].l)
		{
			left--;
			res+=num[sum[left-1]^k];
			num[sum[left-1]]++;
		}
		while(right<q[i].r)
		{
			right++;
			res+=num[sum[right]^k];
			num[sum[right]]++;
		}
		while(right>q[i].r)
		{
			res-=num[sum[right]^k];
			num[sum[right]]--;
			right--;
		}
		ans[q[i].ind]=res;
	}
}

void print()
{
	for(int i=1;i<=m;i++)
		printf("%I64d\n",ans[i]);
}

int main()
{
	while(~scanf("%d%d%d",&n,&m,&k))
	{
		prework();
		mainwork();
		print();
	}
	return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define maxl 100010
#define maxm 2000010

using namespace std;

int n,m,k,size,cnt,res;
int a[maxl],sum[maxl],pos[maxl],l[maxl],r[maxl];
long long num[maxm];
long long ans[maxl];
struct que
{
	int l,r,ind;
}q[maxl];

bool cmp(const que &x,const que &y)
{
	if(pos[x.l]==pos[y.l])
		return x.r<y.r;
	else
		return pos[x.l]<pos[y.l];
}

void prework()
{
	size=sqrt(n);cnt=n/size;
	if(n%size) cnt++;
	for(int i=1;i<=n;i++)
		scanf("%d",&a[i]),sum[i]=sum[i-1]^a[i];
	for(int i=1;i<=cnt;i++)
	{
		l[i]=(i-1)*size+1;r[i]=min(i*size,n);
		for(int j=l[i];j<=r[i];j++)
			pos[j]=i;
	}
	for(int i=1;i<=m;i++)
	{
		scanf("%d%d",&q[i].l,&q[i].r);
		q[i].ind=i;
	}
	sort(q+1,q+1+m,cmp);
}

void add(int x){
    res+=num[sum[x]^k];
    num[sum[x]]++;
}

void del(int x){
    num[sum[x]]--;
    res-=num[sum[x]^k];
}

void mainwork1()
{
	int left=1,right=0;
	res=0;
	memset(num,0,sizeof(num));
	num[0]=1;
	for(int i=1;i<=m;i++)
	{
		while(left<q[i].l)
		{
			/*res-=num[sum[left-1]^k];	//k=0��Ҫ�ȼ����
			num[sum[left-1]]--;*/
			del(left-1);
			left++;
		}
		while(left>q[i].l)
		{
			left--;
			/*num[sum[left-1]]++;       //k=0ʱҪ�ȼ����
			res+=num[sum[left-1]^k];*/
			add(left-1);
		}
		while(right<q[i].r)
		{
			right++;
			/*num[sum[right]]++;
			res+=num[sum[right]^k];*/
			add(right);
		}
		while(right>q[i].r)
		{
			del(right);
		/*	res-=num[sum[right]^k];
			num[sum[right]]--;*/
			right--;
		}
		ans[q[i].ind]=res;
	}
}

void mainwork()
{
	 int L=1,R=0;
    res=0;
    num[0]=1;//!
   	 for(int i=1;i<=m;i++){
            while(L<q[i].l)
				del(L-1),L++;
            while(L>q[i].l)
				L--,add(L-1);
            while(R<q[i].r)
				R++,add(R);
            while(R>q[i].r)
				del(R),R--;
            ans[q[i].ind]=res;
        }
}

void print()
{
	for(int i=1;i<=m;i++)
		printf("%I64d\n",ans[i]);
}

int main()
{
	while(~scanf("%d%d%d",&n,&m,&k))
	{
		prework();
		mainwork();
		print();
	}
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long LL;
const int maxn = 1e5 + 10;
int block, a[maxn], b[maxn];
LL cnt[1111111];
LL ans[maxn];

struct query
{
    int id, l, r;
}Q[maxn];

bool cmp(query A, query B)
{
    if(A.l / block != B.l / block) return A.l / block < B.l / block;
    return A.r < B.r;
}

int main(void)
{
    int n, m, k;
    scanf("%d%d%d", &n, &m, &k);
    for(int i = 1; i <= n; i++) scanf("%d", a + i), b[i] = b[i-1] ^ a[i];
    for(int i = 0; i < m; i++) scanf("%d %d", &Q[i].l, &Q[i].r), Q[i].id = i;
    block = sqrt(n), sort(Q, Q + m, cmp);
    LL tmp = 0LL;
    int L = 1, R = 0;
    for(int i = 0; i < m; i++)
    {
        while(R < Q[i].r) {cnt[b[R]]++; R++; tmp += cnt[k^b[R]];}
        while(R > Q[i].r) {tmp -= cnt[k^b[R]]; R--; cnt[b[R]]--;}
        while(L < Q[i].l) {L++; --cnt[b[L-2]]; tmp -= cnt[k^b[L-2]] + ((b[L-2] ^ b[R])== k ? 1LL : 0LL);}
        while(L > Q[i].l) {tmp += cnt[k^b[L-2]] + ((b[L-2] ^ b[R]) == k ? 1LL : 0LL); cnt[b[L-2]]++; L--;}
        ans[Q[i].id] = tmp;
    }
    for(int i = 0; i < m; i++) printf("%I64d\n", ans[i]);
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long LL;
const int maxn = 1e5 + 10;
int block, a[maxn], b[maxn];
LL cnt[1111111];
LL ans[maxn];

struct query
{
    int id, l, r;
}Q[maxn];

bool cmp(query A, query B)
{
    if(A.l / block != B.l / block) return A.l / block < B.l / block;
    return A.r < B.r;
}

int main(void)
{
    int n, m, k;
    scanf("%d%d%d", &n, &m, &k);
    for(int i = 1; i <= n; i++) scanf("%d", a + i), b[i] = b[i-1] ^ a[i];
    for(int i = 0; i < m; i++) scanf("%d %d", &Q[i].l, &Q[i].r), Q[i].id = i;
    block = sqrt(n), sort(Q, Q + m, cmp);
    LL tmp = 0LL;
    int L = 1, R = 0;
    for(int i = 0; i < m; i++)
    {
        while(R < Q[i].r) {cnt[b[R]]++; R++; tmp += cnt[k^b[R]];}while(R > Q[i].r) {tmp -= cnt[k^b[R]]; R--; cnt[b[R]]--;}
        while(L > Q[i].l) {tmp += cnt[k^b[L-2]] + ((b[L-2] ^ b[R]) == k ? 1LL : 0LL); cnt[b[L-2]]++; L--;}

        while(L < Q[i].l) {L++; --cnt[b[L-2]]; tmp -= cnt[k^b[L-2]] + ((b[L-2] ^ b[R])== k ? 1LL : 0LL);}
        ans[Q[i].id] = tmp;
    }
    for(int i = 0; i < m; i++) printf("%I64d\n", ans[i]);
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long LL;
const int maxn = 1e5 + 10;
int block, a[maxn], b[maxn];
int cnt[1111111];
LL ans[maxn];

struct query
{
    int id, l, r;
}Q[maxn];

bool cmp(query A, query B)
{
    if(A.l / block != B.l / block) return A.l / block < B.l / block;
    return A.r < B.r;
}

int main(void)
{
    int n, m, k;
    scanf("%d%d%d", &n, &m, &k);
    for(int i = 1; i <= n; i++) scanf("%d", a + i), b[i] = b[i-1] ^ a[i];
    for(int i = 0; i < m; i++) scanf("%d %d", &Q[i].l, &Q[i].r), Q[i].id = i;
    block = sqrt(n), sort(Q, Q + m, cmp);
    LL tmp = 0LL;
    int L = 1, R = 0;
    for(int i = 0; i < m; i++)
    {
        while(R < Q[i].r) tmp += (LL) cnt[k^b[R+1]] + (a[R+1] == k), cnt[b[R++]]++;
        while(R > Q[i].r) tmp -= (LL) cnt[k^b[R]], cnt[b[--R]]--;
        while(L > Q[i].l) tmp += (LL) cnt[k^b[L-2]] + (b[L-2] ^ b[R] == k), cnt[b[L-2]]++, L--;
        while(L < Q[i].l) tmp -= (LL) cnt[k^b[L-1]] - (k == 0) + (b[L-1] ^ b[R] == k), cnt[b[L-1]]--, L++;
        ans[Q[i].id] = tmp;
    }
    for(int i = 0; i < m; i++) printf("%d\n", ans[i]);
    return 0;
}