#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <cstdio>
#define inf 0x3f3f3f3f
using namespace std;
const int maxn = 100010;
string x1,x2;
int a[150];
int b[maxn];
int main()
{
    ios::sync_with_stdio(0);
    memset(a,0,sizeof(a));
    cin>>x1>>x2;
    int len = x1.size();
    int len1 = x2.size();
    for(int i = 0; i < len; ++i){
        a[x1[i]] = 1;
    }
    int p = 0,pos1 = -1;
    for(int i=0;i<len1;++i){
        if(x2[i]=='?')
            b[++p] = i;
        if(x2[i]=='*')
            pos1 = i;
    }

    int n;
    cin >> n;
    for(int i=1;i<=n;++i){
        int k = p;
        string x3;
        cin >> x3;
        int len2 = x3.size();
         string x5 = x2;
         if(len1>len2+1){
            cout << "NO" << endl;
            continue;
         }
         int t,len3;
         if(len1==len2+1){
            if(pos1==-1){
               cout << "NO" << endl;
            continue;
            }
           else if(pos1==len1-1)
            x5 = x2.substr(0,pos1);
            else
            x5 = x2.substr(0,pos1)+x2.substr(pos1+1,len1-pos1-1);
           len3 = x5.size();
           if(len3>len2){
             cout << "NO" << endl;
            continue;
           }
          // cout << x5 << endl;
            for(t=1;t<=k;++t){
                if((x5[b[t]]=='?'&&a[x3[b[t]]]!=1)){
                    break;

            }
        x5[b[t]] = x3[b[t]];
         }
         //cout << x5 <<t<< endl;
         if(t>k&&x5==x3){
            cout << "YES" << endl;
            continue;
        }
        else{
            cout << "NO" << endl;
            continue;
        }
         }
        if(len1==len2){
        x5 = x2;
        if(pos1!=-1)
        b[++k] = pos1;
        for(t=1;t<=k;++t){
            if((x2[b[t]]=='?'&&a[x3[b[t]]]!=1)||(x2[b[t]]=='*'&&a[x3[b[t]]]==1)){
                break;
            }
            x5[b[t]] = x3[b[t]];
        }
        //cout << x5 << k<<" "<<b[1]<<" "<<b[2]<<endl;
        if(t>k&&x5==x3){
            cout << "YES" << endl;
            continue;
        }
        else{
            cout << "NO" << endl;
            continue;
        }
         }
       if(len1<len2){
            //cout << "hh" << endl;
            if(pos1==-1){
            cout << "NO" << endl;
            continue;
            }
            else{
                x5 = x2.substr(0,pos1);
                if(x5!=x3.substr(0,pos1)){
                     cout << "NO" << endl;
                    continue;
                }
                 else if(pos1 == len1-1){
                       // cout << "HH" << endl;
                    for(t=pos1;t<len2;++t){
                        if(a[x3[t]]==1)
                            break;
                    }
                    if(t==len2){
                        cout << "YES" << endl;
                        continue;
                    }
                    else{
                        cout << "NO" << endl;
                        continue;
                    }
                 }
                 else{
                    string x6 = x3.substr(pos1+1,len2-pos1-1);
                    string x7 = x2.substr(pos1+1,len1-pos1-1);
                    if(x6.rfind(x7)==std::string::npos){
                         cout << "NO" << endl;
                        continue;
                    }
                    else{
                        int pos2 = x6.rfind(x7);
                        for(t=pos1;t<=pos2;++t){
                            if(a[x3[t]]==1)
                                break;
                        }
                        if(t==pos2+1){
                        cout << "YES" << endl;
                        continue;
                    }
                    else
                        cout << "NO" << endl;
                        continue;
                    }
                 }
            }
       }
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <cstdio>
#define inf 0x3f3f3f3f
using namespace std;
const int maxn = 100010;
string x1,x2;
int a[150];
int b[maxn];
int main()
{
    ios::sync_with_stdio(0);
    memset(a,0,sizeof(a));
    cin>>x1>>x2;
    int len = x1.size();
    int len1 = x2.size();
    for(int i = 0; i < len; ++i){
        a[x1[i]] = 1;
    }
    int p = 0,pos1 = -1;
    for(int i=0;i<len1;++i){
        if(x2[i]=='?')
            b[++p] = i;
        if(x2[i]=='*')
            pos1 = i;
    }

    int n;
    cin >> n;
    for(int i=1;i<=n;++i){
        int k = p;
        string x3;
        cin >> x3;
        int len2 = x3.size();
         string x5 = x2;
         if(len1>len2+1){
            cout << "NO" << endl;
            continue;
         }
         int t,len3;
         if(len1==len2+1){
            if(pos1==-1){
               cout << "NO" << endl;
            continue;
            }
           else if(pos1==len1-1)
            x5 = x2.substr(0,pos1);
            else
            x5 = x2.substr(0,pos1)+x2.substr(pos1+1,len1-pos1-1);
           len3 = x5.size();
           if(len3>len2){
             cout << "NO" << endl;
            continue;
           }
          // cout << x5 << endl;
            for(t=1;t<=k;++t){
                if((x5[b[t]]=='?'&&a[x3[b[t]]]!=1)){
                    break;

            }
        x5[b[t]] = x3[b[t]];
         }
         //cout << x5 <<t<< endl;
         if(t>k&&x5==x3){
            cout << "YES" << endl;
            continue;
        }
        else{
            cout << "NO" << endl;
            continue;
        }
         }
        if(len1==len2){
        x5 = x2;
        if(pos1!=-1)
        b[++k] = pos1;
        for(t=1;t<=k;++t){
            if((x2[b[t]]=='?'&&a[x3[b[t]]]!=1)||(x2[b[t]]=='*'&&a[x3[b[t]]]==1)){
                break;
            }
            x5[b[t]] = x3[b[t]];
        }
        //cout << x5 << k<<" "<<b[1]<<" "<<b[2]<<endl;
        if(t>k&&x5==x3){
            cout << "YES" << endl;
            continue;
        }
        else{
            cout << "NO" << endl;
            continue;
        }
         }
       if(len1<len2){
            //cout << "hh" << endl;
            if(pos1==-1){
            cout << "NO" << endl;
            continue;
            }
            else{
                x5 = x2.substr(0,pos1);
                if(x5!=x3.substr(0,pos1)){
                     cout << "NO" << endl;
                    continue;
                }
                 else if(pos1 == len1-1){
                       // cout << "HH" << endl;
                    for(t=pos1;t<len2;++t){
                        if(a[x3[t]]==1)
                            break;
                    }
                    if(t==len2){
                        cout << "YES" << endl;
                        continue;
                    }
                    else{
                        cout << "NO" << endl;
                        continue;
                    }
                 }
                 else{
                    string x6 = x3.substr(pos1+1,len2-pos1-1);
                    string x7 = x2.substr(pos1+1,len1-pos1-1);
                    int len4 = x7.size();
                    if(x6.rfind(x7)==std::string::npos||x6.rfind(x7)+len4!=len2){
                         cout << "NO" << endl;
                        continue;
                    }
                    else{
                        int pos2 = x6.rfind(x7);
                        for(t=pos1;t<=pos2;++t){
                            if(a[x3[t]]==1)
                                break;
                        }
                        if(t==pos2+1){
                        cout << "YES" << endl;
                        continue;
                    }
                    else
                        cout << "NO" << endl;
                        continue;
                    }
                 }
            }
       }
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <cstdio>
#define inf 0x3f3f3f3f
using namespace std;
const int maxn = 100010;
string x1,x2;
int a[150];
int b[maxn];
int main()
{
    ios::sync_with_stdio(0);
    memset(a,0,sizeof(a));
    cin>>x1>>x2;
    int len = x1.size();
    int len1 = x2.size();
    for(int i = 0; i < len; ++i){
        a[x1[i]] = 1;
    }
    int p = 0,pos1 = -1;
    for(int i=0;i<len1;++i){
        if(x2[i]=='?')
            b[++p] = i;
        if(x2[i]=='*')
            pos1 = i;
    }

    int n;
    cin >> n;
    for(int i=1;i<=n;++i){
        int k = p;
        string x3;
        cin >> x3;
        int len2 = x3.size();
         string x5 = x2;
         if(len1>len2+1){
            cout << "NO" << endl;
            continue;
         }
         int t,len3;
         if(len1==len2+1){
            if(pos1==-1){
               cout << "NO" << endl;
            continue;
            }
           else if(pos1==len1-1)
            x5 = x2.substr(0,pos1);
            else
            x5 = x2.substr(0,pos1)+x2.substr(pos1+1,len1-pos1-1);
           len3 = x5.size();
           if(len3>len2){
             cout << "NO" << endl;
            continue;
           }
          // cout << x5 << endl;
            for(t=1;t<=k;++t){
                if((x5[b[t]]=='?'&&a[x3[b[t]]]!=1)){
                    break;

            }
        x5[b[t]] = x3[b[t]];
         }
         //cout << x5 <<t<< endl;
         if(t>k&&x5==x3){
            cout << "YES" << endl;
            continue;
        }
        else{
            cout << "NO" << endl;
            continue;
        }
         }
        if(len1==len2){
        x5 = x2;
        if(pos1!=-1)
        b[++k] = pos1;
        for(t=1;t<=k;++t){
            if((x2[b[t]]=='?'&&a[x3[b[t]]]!=1)||(x2[b[t]]=='*'&&a[x3[b[t]]]==1)){
                break;
            }
            x5[b[t]] = x3[b[t]];
        }
        //cout << x5 << k<<" "<<b[1]<<" "<<b[2]<<endl;
        if(t>k&&x5==x3){
            cout << "YES" << endl;
            continue;
        }
        else{
            cout << "NO" << endl;
            continue;
        }
         }
       if(len1<len2){
            //cout << "hh" << endl;
            if(pos1==-1){
            cout << "NO" << endl;
            continue;
            }
            else{
                x5 = x2.substr(0,pos1);
                if(x5!=x3.substr(0,pos1)){
                     cout << "NO" << endl;
                    continue;
                }
                 else if(pos1 == len1-1){
                        //cout << "HH" << endl;
                    for(t=pos1+1;t<len2;++t){
                        if(a[x3[t]]!=1)
                            break;
                    }
                    if(t==len2){
                        cout << "YES" << endl;
                        continue;
                    }
                    else{
                        cout << "NO" << endl;
                        continue;
                    }
                 }
                 else{
                    string x6 = x3.substr(pos1+1,len2-pos1-1);
                    string x7 = x2.substr(pos1+1,len1-pos1-1);
                    if(x6.rfind(x7)==std::string::npos){
                         cout << "NO" << endl;
                        continue;
                    }
                    else{
                        int pos2 = x6.rfind(x7);
                        for(t=pos1+1;t<=pos2;++t){
                            if(a[x3[t]]!=1)
                                break;
                        }
                        if(t==pos2+1){
                        cout << "YES" << endl;
                        continue;
                    }
                    else
                        cout << "NO" << endl;
                        continue;
                    }
                 }
            }
       }
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <cstdio>
#define inf 0x3f3f3f3f
using namespace std;
const int maxn = 100010;
string x1,x2;
int a[150];
int b[maxn];
int main()
{
    ios::sync_with_stdio(0);
    memset(a,0,sizeof(a));
    cin>>x1>>x2;
    int len = x1.size();
    int len1 = x2.size();
    for(int i = 0; i < len; ++i){
        a[x1[i]] = 1;
    }
    int p = 0,pos1 = -1;
    for(int i=0;i<len1;++i){
        if(x2[i]=='?')
            b[++p] = i;
        if(x2[i]=='*')
            pos1 = i;
    }

    int n;
    cin >> n;
    for(int i=1;i<=n;++i){
        int k = p;
        string x3;
        cin >> x3;
        int len2 = x3.size();
         string x5 = x2;
         if(len1>len2+1){
            cout << "NO" << endl;
            continue;
         }
         int t,len3;
         if(len1==len2+1){
            if(pos1==-1){
               cout << "NO" << endl;
            continue;
            }
           else if(pos1==len1-1)
            x5 = x2.substr(0,pos1);
            else
            x5 = x2.substr(0,pos1)+x2.substr(pos1+1,len1-pos1-1);
           len3 = x5.size();
           if(len3>len2){
             cout << "NO" << endl;
            continue;
           }
          // cout << x5 << endl;
            for(t=1;t<=k;++t){
                if((x5[b[t]]=='?'&&a[x3[b[t]]]!=1)){
                    break;

            }
        x5[b[t]] = x3[b[t]];
         }
         //cout << x5 <<t<< endl;
         if(t>k&&x5==x3){
            cout << "YES" << endl;
            continue;
        }
        else{
            cout << "NO" << endl;
            continue;
        }
         }
        if(len1==len2){
        x5 = x2;
        if(pos1!=-1)
        b[++k] = pos1;
        for(t=1;t<=k;++t){
            if((x2[b[t]]=='?'&&a[x3[b[t]]]!=1)||(x2[b[t]]=='*'&&a[x3[b[t]]]==1)){
                break;
            }
            x5[b[t]] = x3[b[t]];
        }
        //cout << x5 << k<<" "<<b[1]<<" "<<b[2]<<endl;
        if(t>k&&x5==x3){
            cout << "YES" << endl;
            continue;
        }
        else{
            cout << "NO" << endl;
            continue;
        }
         }
       if(len1<len2){
            //cout << "hh" << endl;
            if(pos1==-1){
            cout << "NO" << endl;
            continue;
            }
            else{
                x5 = x2.substr(0,pos1);
                if(x5!=x3.substr(0,pos1)){
                     cout << "NO" << endl;
                    continue;
                }
                 else if(pos1 == len1-1){
                    for(t=pos1+1;t<len2;++t){
                        if(a[x3[t]]==1)
                            break;
                    }
                    if(t==len2){
                        cout << "YES" << endl;
                        continue;
                    }
                    else{
                        cout << "NO" << endl;
                        continue;
                    }
                 }
                 else{
                    string x6 = x3.substr(pos1+1,len2-pos1-1);
                    string x7 = x2.substr(pos1+1,len1-pos1-1);
                    if(x6.rfind(x7)==std::string::npos){
                         cout << "NO" << endl;
                        continue;
                    }
                    else{
                        int pos2 = x6.rfind(x7);
                        for(t=pos1+1;t<=pos2;++t){
                            if(a[x3[t]]==1)
                                break;
                        }
                        if(t==pos2+1){
                        cout << "YES" << endl;
                        continue;
                    }
                    else
                        cout << "NO" << endl;
                        continue;
                    }
                 }
            }
       }
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <cstdio>
#define inf 0x3f3f3f3f
using namespace std;
const int maxn = 100010;
string x1,x2;
int a[150];
int b[maxn];
int main()
{
    ios::sync_with_stdio(0);
    memset(a,0,sizeof(a));
    cin>>x1>>x2;
    int len = x1.size();
    int len1 = x2.size();
    for(int i = 0; i < len; ++i){
        a[x1[i]] = 1;
    }
    int k = 0;
    for(int i=0;i<len1;++i){
        if(x2[i]=='?'||x2[i]=='*')
            b[++k] = i;
    }
    int n;
    cin >> n;
    for(int i=1;i<=n;++i){
        string x3;
        cin >> x3;
        int len2 = x3.size();
        if(len1<len2){
            cout << "NO" << endl;
            continue;
        }
         string x5 = x2;
        if(len1>len2){
            string x4 = x2.substr(len2,len1-len2);
            int j;
            for( j=0;j<len1-len2;++j){
                if(x4[j]!='*')
                    break;
            }
            if(j!=len1-len2){
                cout << "NO" << endl;
                continue;
            }
        }
        x5 = x5.substr(0,len2);
        //cout << x5 << endl;
        int t;
        for(t=1;t<=k;++t){
            if((x2[b[t]]=='?'&&a[x3[b[t]]]!=1)||(x2[b[t]]=='*'&&a[x3[b[t]]]==1)){
                break;
            }
            x5[b[t]] = x3[b[t]];
        }
        //cout << x5 << k<<" "<<b[1]<<" "<<b[2]<<endl;
        if(t>k&&x5==x3){
            cout << "YES" << endl;
        }
        else
            cout << "NO" << endl;
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <cstdio>
#define inf 0x3f3f3f3f
using namespace std;
const int maxn = 100010;
string x1,x2;
int a[150];
int b[maxn];
int main()
{
    ios::sync_with_stdio(0);
    memset(a,0,sizeof(a));
    cin>>x1>>x2;
    int len = x1.size();
    int len1 = x2.size();
    for(int i = 0; i < len; ++i){
        a[x1[i]] = 1;
    }
    int p = 0,pos1 = -1;
    for(int i=0;i<len1;++i){
        if(x2[i]=='?')
            b[++p] = i;
        if(x2[i]=='*')
            pos1 = i;
    }

    int n;
    cin >> n;
    for(int i=1;i<=n;++i){
        int k = p;
        string x3;
        cin >> x3;
        int len2 = x3.size();
         string x5 = x2;
         if(len1>len2+1){
            cout << "NO" << endl;
            continue;
         }
         int t,len3;
         if(len1==len2+1){
            if(pos1==len1-1)
            x5 = x2.substr(0,pos1);
            else
            x5 = x2.substr(0,pos1)+x2.substr(pos1+1,len1-pos1-1);
           len3 = x5.size();
           if(len3>len2){
             cout << "NO" << endl;
            continue;
           }
          // cout << x5 << endl;
            for(t=1;t<=k;++t){
                if((x5[b[t]]=='?'&&a[x3[b[t]]]!=1)){
                    break;

            }
        x5[b[t]] = x3[b[t]];
         }
         //cout << x5 <<t<< endl;
         if(t>k&&x5==x3){
            cout << "YES" << endl;
            continue;
        }
        else{
            cout << "NO" << endl;
            continue;
        }
         }
        if(len1==len2){
        x5 = x2;
        if(pos1!=-1)
        b[++k] = pos1;
        for(t=1;t<=k;++t){
            if((x2[b[t]]=='?'&&a[x3[b[t]]]!=1)||(x2[b[t]]=='*'&&a[x3[b[t]]]==1)){
                break;
            }
            x5[b[t]] = x3[b[t]];
        }
        //cout << x5 << k<<" "<<b[1]<<" "<<b[2]<<endl;
        if(t>k&&x5==x3){
            cout << "YES" << endl;
            continue;
        }
        else{
            cout << "NO" << endl;
            continue;
        }
         }
       if(len1<len2){
            //cout << "hh" << endl;
            if(pos1==-1){
            cout << "NO" << endl;
            continue;
            }
            else{
                x5 = x2.substr(0,pos1);
                if(x5!=x3.substr(0,pos1)){
                     cout << "NO" << endl;
                    continue;
                }
                 else if(pos1 == len1-1){
                    for(t=pos1+1;t<len2;++t){
                        if(a[x3[t]]==1)
                            break;
                    }
                    if(t==len2){
                        cout << "YES" << endl;
                        continue;
                    }
                    else{
                        cout << "NO" << endl;
                        continue;
                    }
                 }
                 else{
                    string x6 = x3.substr(pos1+1,len2-pos1-1);
                    string x7 = x5.substr(pos1+1,len1-pos1-1);
                    if(x6.rfind(x7)==std::string::npos){
                         cout << "NO" << endl;
                        continue;
                    }
                    else{
                        int pos2 = x6.rfind(x7);
                        for(t=pos1+1;t<=pos2;++t){
                            if(a[x3[t]]==1)
                                break;
                        }
                        if(t==pos2+1){
                        cout << "YES" << endl;
                        continue;
                    }
                    else
                        cout << "NO" << endl;
                        continue;
                    }
                 }
            }
       }
    }
    return 0;
}


#include <iostream>
using namespace std;

bool isGood(char c,string Good){
	for(auto g:Good){
		if(c==g)return 1;
	}
	return 0;
}

bool isValid(string good,string reg,string in){
	int pos = 0;
	for(int i=0;i<reg.length();i++){
		switch(reg[i]){
			case '?':
				if(i>=in.length())return 0;
				else if(!isGood(in[pos++],good))return 0;
				break;

			case '*':
				if(i==reg.length()-1){
					for(int j=pos;j<in.length();j++){
						if(isGood(in[j],good))return 0;
					}
				}
				else{
					for(int j=pos;j<in.length();j++){
						if(!isGood(in[j],good)){
							pos = j;
							break;
						}
					}
				}
				break;

			default:
				if(reg[i]!=in[pos++])return 0;
				break;
		}
	}
	return 1;
}

int main(){
	string good,reg;
	cin>>good>>reg;
	int n;
	cin>>n;
	while(n--){
		string in;
		cin>>in;
		if(isValid(good,reg,in))cout<<"YES\n";
		else cout<<"NO\n";
	}
}


#include <iostream>
using namespace std;

int main(){
	string a,reg;
	cin>>a>>reg;
	int n;
	cin>>n;
	while(n--){
		string in;
		cin>>in;
		if(in.length()>reg.length()){
			cout<<"NO\n";
		}
		else{
			bool isValid = 1;
			for(int i=0;i<reg.length();i++){
				bool valid = 1;
				if(i>=in.length()&&reg[i]!='*'){
					valid = 0;
				}
				else if(reg[i]==in[i] || reg[i]=='*'){
					continue;
				}
				else if(reg[i]=='?'){
					bool isGood = 0;
					for(int j=0;j<a.length();j++){
						if(in[i]==a[j]){
							isGood = 1;
							break;
						}
					}
					if(!isGood)valid = 0;
				}
				else{
					valid = 0;
				}
				if(!valid){
					cout<<"NO\n";
					isValid = 0;
					break;
				}
			}
			if(isValid)cout<<"YES\n";
		}
	}
}


#include <iostream>
#include <list>
using namespace std;

bool isGood(char c,string Good){
	for(auto g:Good){
		if(c==g)return 1;
	}
	return 0;
}

bool isValid(string good,string reg,string in_str){
	int c;
	list<char> in;
	for(auto c:in_str)in.push_back(c);
	for(int i=0;i<reg.length();i++){
		c=i;
		if(in.empty()&&reg[i]!='*')return 0;
		if(reg[i]=='?'&&isGood(in.front(),good)||reg[i]==in.front()){
			in.pop_front();
		}
		else if(reg[i]=='*'){
			break;
		}
		else{
			return 0;
		}
	}
	if(c==reg.length()-1&&reg[c]!='*')return 1;
	for(int i=reg.length()-1;i>=c;i--){
		if(in.empty()&&reg[i]!='*')return 0;
		if(reg[i]=='?'&&isGood(in.back(),good)||reg[i]==in.back()){
			in.pop_back();
		}
		else if(reg[i]=='*'){
			break;
		}
		else{
			return 0;
		}
	}
	for(auto c:in){
		if(isGood(c,good))return 0;
	}
	return 1;
}

int main(){
	string good,reg;
	cin>>good>>reg;
	int n;
	cin>>n;
	while(n--){
		string in;
		cin>>in;
		if(isValid(good,reg,in))cout<<"YES\n";
		else cout<<"NO\n";
	}
}


#include <iostream>
using namespace std;

bool isGood(char c,string Good){
	for(auto g:Good){
		if(c==g)return 1;
	}
	return 0;
}

bool isValid(string good,string reg,string in){
	bool hasStar = 0;
	for(auto c:reg){
		if(c=='*'){
			hasStar = 1;
			break;
		}
	}
	int a=0,b=in.length()-1,c;
	for(int i=0;i<reg.length();i++){
		c=i;
		if(reg[i]=='?'&&isGood(in[a],good)||reg[i]==in[a]){
			a++;
		}
		else if(reg[i]=='*'){
			break;
		}
		else{
			return 0;
		}
	}
	if(c==reg.length())return 1;
	for(int i=reg.length()-1;i>=c;i--){
		if(reg[i]=='?'&&isGood(in[b],good)||reg[i]==in[b]){
			b--;
		}
		else if(reg[i]=='*'){
			break;
		}
		else{
			return 0;
		}
	}
	if(b==a-1&&hasStar)return 1;
	else if(b<a)return 0;
	for(int i=a;i<=b;i++){
		cout<<i;
		if(isGood(in[i],good))return 0;
	}
	return 1;
}

int main(){
	string good,reg;
	cin>>good>>reg;
	int n;
	cin>>n;
	while(n--){
		string in;
		cin>>in;
		if(isValid(good,reg,in))cout<<"YES\n";
		else cout<<"NO\n";
	}
}


#include <iostream>
using namespace std;

bool isGood(char c,string Good){
	for(auto g:Good){
		if(c==g)return 1;
	}
	return 0;
}

bool isValid(string good,string reg,string in){
	int a=0,b=0;
	while(1){
		//cout<<reg[a]<<" "<<in[b]<<endl;
		if(a>=reg.length()||b>=in.length()){
			if(a>=reg.length()&&b>=in.length())return 1;
			else if(reg[a]=='*'&&a==reg.length()-1)return 1;
			else return 0;
		}
		bool goodChar = isGood(in[b],good);
		if(reg[a]=='*'){
			if(reg[a+1]=='?'&&goodChar||reg[a+1]==in[b]){
				a+=2;
				b++;
			}
			else if(!goodChar){
				b++;
			}
			else{
				return 0;
			}
		}
		else if(reg[a]=='?'&&goodChar||reg[a]==in[b]){
			a++;
			b++;
		}
		else{
			return 0;
		}
	}
	return 1;
}

int main(){
	string good,reg;
	cin>>good>>reg;
	int n;
	cin>>n;
	while(n--){
		string in;
		cin>>in;
		if(isValid(good,reg,in))cout<<"YES\n";
		else cout<<"NO\n";
	}
}


#include <iostream>
using namespace std;

bool isGood(char c,string Good){
	for(auto g:Good){
		if(c==g)return 1;
	}
	return 0;
}

bool isValid(string good,string reg,string in){
	int a=0,b=reg.length()-1;
	for(int i=0;i<in.length();i++){
		bool goodChar = isGood(in[i],good);
		if(reg[a]=='?'&&goodChar||reg[a]==in[i]){
			a++;
		}
		else if(reg[a]=='*'){
			break;
		}
		else{
			return 0;
		}
	}
	for(int i=in.length()-1;i>=0;i--){
		bool goodChar = isGood(in[i],good);
		if(reg[b]=='?'&&goodChar||reg[b]==in[i]){
			b--;
		}
		else if(reg[b]=='*'){
			break;
		}
		else{
			return 0;
		}
	}
	return 1;
}

int main(){
	string good,reg;
	cin>>good>>reg;
	int n;
	cin>>n;
	while(n--){
		string in;
		cin>>in;
		if(isValid(good,reg,in))cout<<"YES\n";
		else cout<<"NO\n";
	}
}


#include <iostream>
using namespace std;

bool isGood(char c,string Good){
	for(auto g:Good){
		if(c==g)return 1;
	}
	return 0;
}

bool isValid(string good,string reg,string in){
	int a=0,b=in.length()-1,c;
	for(int i=0;i<reg.length();i++){
		c=i;
		if(reg[i]=='?'&&isGood(in[a],good)||reg[i]==in[a]){
			a++;
		}
		else if(reg[i]=='*'){
			break;
		}
		else{
			return 0;
		}
	}
	for(int i=reg.length()-1;i>=c;i--){
		if(reg[i]=='?'&&isGood(in[b],good)||reg[i]==in[b]){
			b--;
		}
		else if(reg[i]=='*'){
			break;
		}
		else{
			return 0;
		}
	}
	for(int i=a;i<=b;i++){
		if(isGood(in[i],good))return 0;
	}
	return 1;
}

int main(){
	string good,reg;
	cin>>good>>reg;
	int n;
	cin>>n;
	while(n--){
		string in;
		cin>>in;
		if(isValid(good,reg,in))cout<<"YES\n";
		else cout<<"NO\n";
	}
}


#include <iostream>
using namespace std;

bool isGood(char c,string Good){
	for(auto g:Good){
		if(c==g)return 1;
	}
	return 0;
}

bool isValid(string good,string reg,string in){
	int a=0,b=in.length()-1;
	for(int i=0;i<reg.length();i++){
		if(reg[i]=='?'&&isGood(in[a],good)||reg[i]==in[a]){
			a++;
		}
		else if(reg[i]=='*'){
			break;
		}
		else{
			return 0;
		}
	}
	for(int i=reg.length()-1;i>=0;i--){
		if(reg[i]=='?'&&isGood(in[b],good)||reg[i]==in[b]){
			b--;
		}
		else if(reg[i]=='*'){
			break;
		}
		else{
			return 0;
		}
	}
	for(int i=a;i<=b;i++){
		if(isGood(in[i],good))return 0;
	}
	return 1;
}

int main(){
	string good,reg;
	cin>>good>>reg;
	int n;
	cin>>n;
	while(n--){
		string in;
		cin>>in;
		if(isValid(good,reg,in))cout<<"YES\n";
		else cout<<"NO\n";
	}
}


#include <iostream>
using namespace std;

bool isGood(char c,string Good){
	for(auto g:Good){
		if(c==g)return 1;
	}
	return 0;
}

bool isValid(string good,string reg,string in){
	int pos = 0;
	for(int i=0;i<in.length();i++){
		if(pos>=reg.length()){
			return 0;
		}
		if(reg[pos]=='*'){
			bool goodChar = isGood(in[i],good);
			if(pos+1>=reg.length()&&goodChar)return 0;
			else if(reg[pos+1]=='?'&&goodChar){
				pos++;
				i--;
			}
			else if(reg[pos+1]==in[i]){
				pos++;
				i--;
			}
			else if(goodChar)return 0;
		}
		else if(reg[pos]=='?'){
			if(!isGood(in[i],good))return 0;
			pos++;
		}
		else{
			if(in[i]!=reg[pos++])return 0;
		}
	}
	if(pos<reg.length()-1)return 0;
	return 1;
}

int main(){
	string good,reg;
	cin>>good>>reg;
	int n;
	cin>>n;
	while(n--){
		string in;
		cin>>in;
		if(isValid(good,reg,in))cout<<"YES\n";
		else cout<<"NO\n";
	}
}


#include <iostream>
using namespace std;

bool isGood(char c,string Good){
	for(auto g:Good){
		if(c==g)return 1;
	}
	return 0;
}

bool isValid(string good,string reg,string in){
	int pos = 0;
	for(int i=0;i<in.length();i++){
		if(pos>=reg.length())return 0;
		if(reg[pos]=='*'){
			bool goodChar = isGood(in[i],good);
			if(pos+1>=reg.length()&&goodChar)return 0;
			else if(reg[pos+1]=='?'&&goodChar)pos++;
			else if(reg[pos+1]==in[i])pos++;
			else if(goodChar)return 0;
		}
		else if(reg[pos]=='?'){
			if(!isGood(in[i],good))return 0;
			pos++;
		}
		else{
			if(in[i]!=reg[pos++])return 0;
		}
	}
	if(pos<reg.length()-1)return 0;
	return 1;
}

int main(){
	string good,reg;
	cin>>good>>reg;
	int n;
	cin>>n;
	while(n--){
		string in;
		cin>>in;
		if(isValid(good,reg,in))cout<<"YES\n";
		else cout<<"NO\n";
	}
}


#include <iostream>
using namespace std;

bool isGood(char c,string Good){
	for(auto g:Good){
		if(c==g)return 1;
	}
	return 0;
}

bool isValid(string good,string reg,string in){
	int a=0,b=0;
	while(1){
		if(a>=reg.length()||b>=in.length()){
			if(a>=reg.length()&&b>=in.length())return 1;
			else if(reg[a]=='*'&&a==reg.length()-1)return 1;
			else return 0;
		}
		bool goodChar = isGood(in[b],good);
		if(reg[a]=='*'){
			if(reg[a+1]=='?'&&goodChar||reg[a+1]==in[b+1]){
				a+=2;
				b++;
			}
			else if(!goodChar){
				b++;
			}
			else{
				return 0;
			}
		}
		else if(reg[a]=='?'&&goodChar||reg[a]==in[b]){
			a++;
			b++;
		}
		else{
			return 0;
		}
	}
	return 1;
}

int main(){
	string good,reg;
	cin>>good>>reg;
	int n;
	cin>>n;
	while(n--){
		string in;
		cin>>in;
		if(isValid(good,reg,in))cout<<"YES\n";
		else cout<<"NO\n";
	}
}


#include <iostream>
using namespace std;

bool isGood(char c,string Good){
	for(auto g:Good){
		if(c==g)return 1;
	}
	return 0;
}

bool isValid(string good,string reg,string in){
	int a=0,b=in.length()-1,c,d;
	for(int i=0;i<reg.length();i++){
		if(reg[i]=='?'&&isGood(in[a],good)||reg[i]==in[a]){
			a++;
		}
		else if(reg[i]=='*'){
			break;
		}
		else{
			return 0;
		}
	}
	if(a==in.length())return 1;
	for(int i=reg.length()-1;i>=c;i--){
		if(reg[i]=='?'&&isGood(in[b],good)||reg[i]==in[b]){
			b--;
		}
		else if(reg[i]=='*'){
			break;
		}
		else{
			return 0;
		}
	}
	if(b<a)return 0;
	for(int i=a;i<=b;i++){
		if(isGood(in[i],good))return 0;
	}
	return 1;
}

int main(){
	string good,reg;
	cin>>good>>reg;
	int n;
	cin>>n;
	while(n--){
		string in;
		cin>>in;
		if(isValid(good,reg,in))cout<<"YES\n";
		else cout<<"NO\n";
	}
}


#include <iostream>
#include <list>
using namespace std;

bool isGood(char c,string Good){
	for(auto g:Good){
		if(c==g)return 1;
	}
	return 0;
}

bool isValid(string good,string reg,string in_str){
	int c;
	list<char> in;
	for(auto c:in_str){
		in.push_back(c);
	}
	for(int i=0;i<reg.length();i++){
		c=i;
		if(in.empty()&&reg[i]!='*')return 0;
		if(reg[i]=='?'&&isGood(in.front(),good)||reg[i]==in.front()){
			in.pop_front();
		}
		else if(reg[i]=='*'){
			break;
		}
		else{
			return 0;
		}
	}
	if(c==reg.length()-1&&reg[c]!='*'){
		if(in.empty())return 1;
		else return 0;
	}
	for(int i=reg.length()-1;i>=c;i--){
		if(in.empty()&&reg[i]!='*')return 0;
		if(reg[i]=='?'&&isGood(in.back(),good)||reg[i]==in.back()){
			in.pop_back();
		}
		else if(reg[i]=='*'){
			break;
		}
		else{
			return 0;
		}
	}
	for(auto c:in){
		if(isGood(c,good))return 0;
	}
	return 1;
}

int main(){
	string good,reg;
	cin>>good>>reg;
	int n;
	cin>>n;
	while(n--){
		string in;
		cin>>in;
		if(isValid(good,reg,in))cout<<"YES\n";
		else cout<<"NO\n";
	}
}


#include <iostream>
using namespace std;

bool isGood(char c,string Good){
	for(auto g:Good){
		if(c==g)return 1;
	}
	return 0;
}

bool isValid(string good,string reg,string in){
	int a=0,b=in.length()-1,c,d;
	for(int i=0;i<reg.length();i++){
		if(reg[i]=='?'&&isGood(in[a],good)||reg[i]==in[a]){
			a++;
		}
		else if(reg[i]=='*'){
			break;
		}
		else{
			return 0;
		}
	}
	for(int i=reg.length()-1;i>=c;i--){
		if(reg[i]=='?'&&isGood(in[b],good)||reg[i]==in[b]){
			b--;
		}
		else if(reg[i]=='*'){
			break;
		}
		else{
			return 0;
		}
	}
	if(b<a)return 0;
	for(int i=a;i<=b;i++){
		if(isGood(in[i],good))return 0;
	}
	return 1;
}

int main(){
	string good,reg;
	cin>>good>>reg;
	int n;
	cin>>n;
	while(n--){
		string in;
		cin>>in;
		if(isValid(good,reg,in))cout<<"YES\n";
		else cout<<"NO\n";
	}
}


#include <iostream>
using namespace std;

bool isGood(char c,string Good){
	for(auto g:Good){
		if(c==g)return 1;
	}
	return 0;
}

bool isValid(string good,string reg,string in){
	int a=0,b=in.length()-1,c;
	for(int i=0;i<reg.length();i++){
		c=i;
		if(reg[i]=='?'&&isGood(in[a],good)||reg[i]==in[a]){
			a++;
		}
		else if(reg[i]=='*'){
			break;
		}
		else{
			return 0;
		}
	}
	if(a==in.length())return 1;
	for(int i=reg.length()-1;i>=c;i--){
		if(reg[i]=='?'&&isGood(in[b],good)||reg[i]==in[b]){
			b--;
		}
		else if(reg[i]=='*'){
			break;
		}
		else{
			return 0;
		}
	}
	if(b<a)return 0;
	for(int i=a;i<=b;i++){
		if(isGood(in[i],good))return 0;
	}
	return 1;
}

int main(){
	string good,reg;
	cin>>good>>reg;
	int n;
	cin>>n;
	while(n--){
		string in;
		cin>>in;
		if(isValid(good,reg,in))cout<<"YES\n";
		else cout<<"NO\n";
	}
}


#include <iostream>
using namespace std;

bool isGood(char c,string Good){
	for(auto g:Good){
		if(c==g)return 1;
	}
	return 0;
}

bool isValid(string good,string reg,string in){
	int a=0,b=reg.length()-1;
	int c,d;
	for(int i=0;i<in.length();i++){
		c=i;
		bool goodChar = isGood(in[i],good);
		if(reg[a]=='?'&&goodChar||reg[a]==in[i]){
			a++;
		}
		else if(reg[a]=='*'){
			break;
		}
		else{
			return 0;
		}
	}
	for(int i=in.length()-1;i>=0;i--){
		d=i;
		bool goodChar = isGood(in[i],good);
		if(reg[b]=='?'&&goodChar||reg[b]==in[i]){
			b--;
		}
		else if(reg[b]=='*'){
			break;
		}
		else{
			return 0;
		}
	}
	for(int i=c;i<d;i++){
		if(isGood(in[i],good)){
			return 0;
		}
	}
	return 1;
}

int main(){
	string good,reg;
	cin>>good>>reg;
	int n;
	cin>>n;
	while(n--){
		string in;
		cin>>in;
		if(isValid(good,reg,in))cout<<"YES\n";
		else cout<<"NO\n";
	}
}


#include <bits/stdc++.h>
using namespace std;
string q,n,s;
bool a[300];
vector<int> v;
int query,k = -1;
bool check(string ch) {
	for(int i = 0;i < ch.length();++i) if(a[ch[i]] == false) return false;
	return true;
}
int main() {
	cin >> n >> s;
	for(int i = 0;i < n.length();++i) a[n[i]] = true;
	for(int i = 0;i < s.length();++i) {
		if(s[i] == '?') v.push_back(i);
		else if(s[i] == '*') k = i,s.erase(i,1);
	}
	cin >> query;while(query--) {
		string q; cin >> q;
		if(q.length() > s.length()) {
			if(check(q.sub(k,q.length() - s.length())))
				q.erase(k,q.length() - s.length());
			else {cout << "NO\n";continue;}
		}
		for(int i = 0;i < v.size();++i) {
			if(v[i] >= q.length()) break;
			else if(a[q[v[i]]] == true) q[v[i]] = '?';
		}
		cout << (s == q ? "YES\n" : "NO\n");
	}
}


#include <bits/stdc++.h>
using namespace std;
string q,n,s;
bool a[300];
vector<int> v;
int query,k = -1;
bool check(string ch) {
	for(int i = 0;i < ch.length();++i) if(a[ch[i]] == true) return false;
	return true;
}
int main() {
	cin >> n >> s;
	for(int i = 0;i < n.length();++i) a[n[i]] = true;
	for(int i = 0;i < s.length();++i) {
		if(s[i] == '?') v.push_back(i);
		else if(s[i] == '*') k = i,s.erase(i,1);
	}
	cin >> query;while(query--) {
		string q; cin >> q;
		if(q.length() < s.length()) {cout << "NO\n";continue;}
		if(q.length() > s.length() && k != -1)
			if(check(q.substr(k,q.length() - s.length())) == true)
				q.erase(k,q.length() - s.length());
		if(q.length() != s.length()) {cout << "NO\n";continue;}
		for(int i = 0;i < v.size();++i)
			if(q.length() <= v[i]) break;
			else if(a[q[v[i]]] == true) q[v[i]] = '?';
		cout << (s == q ? "YES\n" : "NO\n");
	}
}


#include <bits/stdc++.h>
using namespace std;
string q,n,s;
bool a[300];
vector<int> v;
int tttt,k = -1;
int main() {
	cin >> n >> s;
	for(int i = 0;i < n.length();++i) a[n[i]] = true;
	for(int i = 0;i < s.length();++i) {
		if(s[i] == '?') v.push_back(i);
		else if(s[i] == '*') k = i,s.erase(i,1);
	}
	cin >> tttt;while(tttt--) {
		bool OK = false;
		cin >> q;
		if(k != -1 && k < q.length()) {
			int c = (int)q.length() - (int)s.length();
			bool check = false;
			for(int i = k,dk = c + k;i < dk;++i) {
				if(a[q[i]] == true) {
					check = true;
					break;
				}
			}
			if(check == false && c >= 1) q.erase(k,c);
		}
		for(int i = 0;i < v.size();++i) {
			if(v[i] >= q.length()) break;
			if(a[q[v[i]]] == true) q[v[i]] = '?';
			else break;
		}
		if(q == s) OK = true;
		if(OK == true) cout << "YES" << endl;
		else cout << "NO" << endl;
	}
}


#include <bits/stdc++.h>
using namespace std;
string q,n,s;
bool a[300];
vector<int> v;
int tttt,k = -1;
int main() {
	cin >> n >> s;
	for(int i = 0;i < n.length();++i) a[n[i]] = true;
	for(int i = 0;i < s.length();++i) {
		if(s[i] == '?') v.push_back(i);
		else if(s[i] == '*') k = i;
	}
	cin >> tttt;while(tttt--) {
		bool OK = false;
		cin >> q;
		if(k != -1 && k < q.length()) {
			int c = (int)q.length() - (int)s.length() + 1;
			bool check = false;
			for(int i = k,dk = c + k;i < dk;++i) {
				if(a[q[i]] == true) {
					check = true;
					break;
				}
			}
			if(check == false && c >= 1) {
				q.erase(k,c);
				q.insert(k,"*");
			}
		}
		for(int i = 0;i < v.size();++i) {
			if(v[i] >= q.length()) break;
			if(a[q[v[i]]] == true) q[v[i]] = '?';
			else break;
		}
		if(q == s) OK = true;
		if(k != -1) {
			string zz = s;
			if(q == zz.erase(k,1)) OK = true;
		}
		if(OK == true) cout << "YES" << endl;
		else cout << "NO" << endl;
	}
}


#include <bits/stdc++.h>
using namespace std;
string n,s;
bool a[300];
vector<int> v;
int query,k = -1;
bool check(string ch) {
	for(int i = 0;i < ch.length();++i) if(a[ch[i]] == true) return false;
	return true;
}
int main() {
	cin >> n >> s;
	for(int i = 0;i < n.length();++i) a[n[i]] = true;
	for(int i = 0;i < s.length();++i) {
		if(s[i] == '?') v.push_back(i);
		else if(s[i] == '*') k = i,s.erase(i,1),i--;
	}
	cin >> query;while(query--) {
		string q; cin >> q;
		if(q.length() >= s.length() && k != -1)
			if(check(q.substr(k,q.length() - s.length())) == true)
				q.erase(k,q.length() - s.length());
		for(int i = 0,j;i < v.size();j = v[++i])
			if(a[q[j]] == true) q[j] = '?';else break;
		cout << (q == s ? "YES\n" : "NO\n");
	}
}


#include <bits/stdc++.h>
using namespace std;
string q,n,s;
bool a[300];
vector<int> v;
int query,k = -1;
bool check(string ch) {
	for(int i = 0;i < ch.length();++i) if(a[ch[i]] == true) return false;
	return true;
}
int main() {
	cin >> n >> s;
	for(int i = 0;i < n.length();++i) a[n[i]] = true;
	for(int i = 0;i < s.length();++i) {
		if(s[i] == '?') v.push_back(i);
		else if(s[i] == '*') k = i,s.erase(i,1);
	}
	cin >> query;while(query--) {
		string q; cin >> q;
		if(q.length() > s.length() && k != -1)
			if(check(q.substr(k,q.length() - s.length())) == true)
				q.erase(k,q.length() - s.length());
		for(int i = 0;i < v.size();++i)
			if(q.length() <= v[i]) break;
			else if(a[q[v[i]]] == true) q[v[i]] = '?';
		cout << (s == q ? "YES\n" : "NO\n");
	}
}


#include <bits/stdc++.h>
using namespace std;
string q,n,s;
bool a[300];
vector<int> v;
int tttt,k = -1;
int main() {
	cin >> n >> s;
	for(int i = 0;i < n.length();++i) a[n[i]] = true;
	for(int i = 0;i < s.length();++i) {
		if(s[i] == '?') v.push_back(i);
		else if(s[i] == '*') k = i;
	}
	cin >> tttt;while(tttt--) {
		bool OK = false;
		cin >> q;
		if(k != -1 && k < q.length()) {
			int c = (int)q.length() - (int)s.length() + 1;
			bool check = false;
			for(int i = k,dk = c + k;i < dk;++i) {
				if(a[q[i]] == true) {
					check = true;
					break;
				}
			}
			if(a[q[k]] == false && c >= 0) {
				q.erase(k,c);
				q.insert(k,"*");
			}
		}
		for(int i = 0;i < v.size();++i) {
			if(v[i] >= q.length()) break;
			if(a[q[v[i]]] == true) q[v[i]] = '?';
			else break;
		}
		if(q == s) OK = true;
		if(k != -1) {
			string zz = s;
			if(q == zz.erase(k,1)) OK = true;
		}
		if(OK == true) cout << "YES" << endl;
		else cout << "NO" << endl;
	}
}


#include <bits/stdc++.h>
using namespace std;
string q,n,s;
bool a[300];
vector<int> v;
int tttt,k = -1;
int main() {
	cin >> n >> s;
	for(int i = 0;i < n.length();++i) a[n[i]] = true;
	for(int i = 0;i < s.length();++i) {
		if(s[i] == '?') v.push_back(i);
		else if(s[i] == '*') k = i;
	}
	cin >> tttt;while(tttt--) {
		bool OK = false;
		cin >> q;
		if(k != -1 && k < q.length()) {
			int c = q.length() - s.length() + 1;
			bool check = false;
			for(int i = k,dk = c + k;i < dk;++i) {
				if(a[q[i]] == true) {
					check = true;
					break;
				}
			}
			if(a[q[k]] == false && c >= 1) {
				q.erase(k,c);
			//	if(tttt == 0) cout << q << endl;
				q.insert(k,"*");
			//	if(tttt == 0) cout << q << endl;
			}
		}
		for(int i = 0;i < v.size();++i) {
			if(v[i] >= q.length()) break;
			if(a[q[v[i]]] == true) q[v[i]] = '?';
			else break;
		}
		if(q == s) OK = true;
		if(k != -1) {
			string zz = s;
			if(q == zz.erase(k,1)) OK = true;
		}
//		cout << q << ' ' << s << ' ';
		if(OK == true) cout << "YES" << endl;
		else cout << "NO" << endl;
	}
}


#include <bits/stdc++.h>
using namespace std;
string q,n,s;
bool a[300];
vector<int> v;
int tttt,k = -1;
int main() {
	cin >> n >> s;
	for(int i = 0;i < n.length();++i) a[n[i]] = true;
	for(int i = 0;i < s.length();++i) {
		if(s[i] == '?') v.push_back(i);
		else if(s[i] == '*') k = i;
	}
	cin >> tttt;while(tttt--) {
		bool OK = false;
		cin >> q;
		if(k != -1 && k < q.length()) {
			int c = (int)q.length() - (int)s.length() + 1;
			bool check = false;
			for(int i = k,dk = c + k;i < dk;++i) {
				if(a[q[i]] == true) {
					check = true;
					break;
				}
			}
			if(c >= 0 && check == false) {
				q.erase(k,c);
				q.insert(k,"*");
			}
		}
		for(int i = 0;i < v.size();++i) {
			if(v[i] >= q.length()) break;
			if(a[q[v[i]]] == true) q[v[i]] = '?';
			else break;
		}
		if(q == s) OK = true;
		if(OK == true) cout << "YES" << endl;
		else cout << "NO" << endl;
	}
}


#include <bits/stdc++.h>
using namespace std;
string q,n,s;
bool a[300];
vector<int> v;
int query,k = -1;
bool check(string ch) {
	for(int i = 0;i < ch.length();++i) if(a[ch[i]] == true) return false;
	return true;
}
int main() {
	cin >> n >> s;
	for(int i = 0;i < n.length();++i) a[n[i]] = true;
	for(int i = 0;i < s.length();++i) {
		if(s[i] == '?') v.push_back(i);
		else if(s[i] == '*') k = i,s.erase(i,1);
	}
	cin >> query;while(query--) {
		string q; cin >> q;
		if(q.length() < s.length()) {cout << "NO\n";continue;}
		if(q.length() > s.length())
			if(check(q.substr(k,q.length() - s.length())) == true)
				q.erase(k,q.length() - s.length());
		for(int i = 0;i < v.size();++i)
			if(a[q[v[i]]] == true) q[v[i]] = '?';
		cout << (s == q ? "YES\n" : "NO\n");
	}
}


#include <bits/stdc++.h>
using namespace std;
string n,s;
bool a[300];
vector<int> v;
int query,k = -1;
bool check(string ch) {
	for(int i = 0;i < ch.length();++i) if(a[ch[i]] == true) return false;
	return true;
}
int main() {
	cin >> n >> s;
	for(int i = 0;i < n.length();++i) a[n[i]] = true;
	for(int i = 0;i < s.length();++i) {
		if(s[i] == '?') v.push_back(i);
		else if(s[i] == '*') k = i,s.erase(i,1),i--;
	}
	cin >> query;while(query--) {
		string q; cin >> q;
		if(q.length() > s.length() && k != -1)
			if(check(q.substr(k,q.length() - s.length())) == true)
				q.erase(k,q.length() - s.length());
		for(int i = 0;i < v.size();++i)
			if(q.length() <= v[i]) break;
			else if(a[q[v[i]]] == true) q[v[i]] = '?';else break;
		cout << (q == s ? "YES\n" : "NO\n");
	}
}


#include <bits/stdc++.h>
using namespace std;
string q,n,s;
bool a[300];
vector<int> v;
int query,k = -1;
bool check(string ch) {
	for(int i = 0;i < ch.length();++i) if(a[ch[i]] == true) return false;
	return true;
}
int main() {
	cin >> n >> s;
	for(int i = 0;i < n.length();++i) a[n[i]] = true;
	for(int i = 0;i < s.length();++i) {
		if(s[i] == '?') v.push_back(i);
		else if(s[i] == '*') k = i,s.erase(i,1);
	}
	cin >> query;while(query--) {
		string q; cin >> q;
		if(q.length() < s.length()) {cout << "NO\n";continue;}
		if(q.length() > s.length() && k != -1)
			if(check(q.substr(k,q.length() - s.length())) == true)
				q.erase(k,q.length() - s.length());
		for(int i = 0;i < v.size();++i)
			if(a[q[v[i]]] == true) q[v[i]] = '?';
		cout << (s == q ? "YES\n" : "NO\n");
	}
}


#include <bits/stdc++.h>
using namespace std;
string q,n,s;
bool a[300];
vector<int> v;
int tttt,k = -1;
int main() {
	cin >> n >> s;
	for(int i = 0;i < n.length();++i) a[n[i]] = true;
	for(int i = 0;i < s.length();++i) {
		if(s[i] == '?') v.push_back(i);
		else if(s[i] == '*') k = i;
	}
	cin >> tttt;while(tttt--) {
		bool OK = false;
		cin >> q;
		if(k != -1 && k < q.length()) {
			int c = (int)q.length() - (int)s.length() + 1;
			bool check = false;
			for(int i = k,dk = c + k;i < dk;++i) {
				if(a[q[i]] == true) {
					check = true;
					break;
				}
			}
			if(a[q[k]] == false && c >= 1) {
				q.erase(k,c);
				q.insert(k,"*");
			}
		}
		for(int i = 0;i < v.size();++i) {
			if(v[i] >= q.length()) break;
			if(a[q[v[i]]] == true) q[v[i]] = '?';
			else break;
		}
		if(q == s) OK = true;
		if(k != -1) {
			string zz = s;
			if(q == zz.erase(k,1)) OK = true;
		}
		if(OK == true) cout << "YES" << endl;
		else cout << "NO" << endl;
	}
}


#include <bits/stdc++.h>
using namespace std;
string n,s;
bool a[300];
vector<int> v;
int query,k = -1;
bool check(string ch) {
	for(int i = 0;i < ch.length();++i) if(a[ch[i]] == true) return false;
	return true;
}
int main() {
	cin >> n >> s;
	for(int i = 0;i < n.length();++i) a[n[i]] = true;
	for(int i = 0;i < s.length();++i) {
		if(s[i] == '?') v.push_back(i);
		else if(s[i] == '*') k = i,s.erase(i,1),i--;
	}
	cin >> query;while(query--) {
		string q; cin >> q;
		if(q.length() >= s.length() && k != -1)
			if(check(q.substr(k,q.length() - s.length())) == true)
				q.erase(k,q.length() - s.length());
		for(int i = 0;i < v.size();++i)
			if(a[q[v[i]]] == true) q[v[i]] = '?';else break;
		cout << (q == s ? "YES\n" : "NO\n");
	}
}


#include <bits/stdc++.h>
using namespace std;
string q,n,s;
bool a[300];
vector<int> v;
int query,k = -1;
bool check(string ch) {
	for(int i = 0;i < ch.length();++i) if(a[ch[i]] == true) return false;
	return true;
}
int main() {
	cin >> n >> s;
	for(int i = 0;i < n.length();++i) a[n[i]] = true;
	for(int i = 0;i < s.length();++i) {
		if(s[i] == '?') v.push_back(i);
		else if(s[i] == '*') k = i,s.erase(i,1);
	}
	cin >> query;while(query--) {
		string q; cin >> q;
		if(q.length() < s.length()) {cout << "NO\n";continue;}
		if(q.length() > s.length() && k != -1)
			if(check(q.substr(k,q.length() - s.length())) == true)
				q.erase(k,q.length() - s.length());
		for(int i = 0;i < v.size();++i)
			if(v[i] >= q.length()) break;
			else if(a[q[v[i]]] == true) q[v[i]] = '?';
		cout << (s == q ? "YES\n" : "NO\n");
	}
}


#include <bits/stdc++.h>
using namespace std;
string n,s;
bool a[300];
vector<int> v;
int query,k = -1,OK;
bool check(string ch) {
	for(int i = 0;i < ch.length();++i) if(a[ch[i]] == true) return false;
	return true;
}
int main() {
	cin >> n >> s;
	for(int i = 0;i < n.length();++i) a[n[i]] = true;
	for(int i = 0;i < s.length();++i) {
		if(s[i] == '?') v.push_back(i - OK);
		else if(s[i] == '*') k = i,OK = 1,s.erase(i,1);
	}
	cin >> query;while(query--) {
		string q; cin >> q;
		if(q.length() > s.length() && k != -1)
			if(check(q.substr(k,q.length() - s.length())) == true)
				q.erase(k,q.length() - s.length());
		for(int i = 0;i < v.size();++i)
			if(q.length() <= v[i]) break;
			else if(a[q[v[i]]] == true) q[v[i]] = '?';else break;
		cout << (q == s ? "YES\n" : "NO\n");
	}
}


#include <bits/stdc++.h>
using namespace std;
string q,n,s;
bool a[300];
vector<int> v;
int tttt,k = -1;
int main() {
	cin >> n >> s;
	for(int i = 0;i < n.length();++i) a[n[i]] = true;
	for(int i = 0;i < s.length();++i) {
		if(s[i] == '?') v.push_back(i);
		else if(s[i] == '*') k = i;
	}
	cin >> tttt;while(tttt--) {
		bool OK = false;
		cin >> q;
		if(k != -1 && k < q.length()) {
			int c = (int)q.length() - (int)s.length() + 1;
			bool check = false;
			for(int i = k,dk = c + k;i < dk;++i) {
				if(a[q[i]] == true) {
					check = true;
					break;
				}
			}
			if(a[q[k]] == false && c >= 1 && check == false) {
				q.erase(k,c);
				q.insert(k,"*");
			}
		}
		for(int i = 0;i < v.size();++i) {
			if(v[i] >= q.length()) break;
			if(a[q[v[i]]] == true) q[v[i]] = '?';
			else break;
		}
		if(q == s) OK = true;
		if(k != -1) {
			string zz = s;
			if(q == zz.erase(k,1)) OK = true;
		}
		if(OK == true) cout << "YES" << endl;
		else cout << "NO" << endl;
	}
}


#include <bits/stdc++.h>
using namespace std;
string q,n,s;
bool a[300];
vector<int> v;
int tttt,k = -1;
int main() {
	cin >> n >> s;
	for(int i = 0;i < n.length();++i) a[n[i]] = true;
	for(int i = 0;i < s.length();++i) {
		if(s[i] == '?') v.push_back(i);
		else if(s[i] == '*') k = i;
	}
	cin >> tttt;while(tttt--) {
		bool OK = false;
		cin >> q;
		if(k != -1 && k < q.length()) {
			int c = (int)q.length() - (int)s.length() + 1;
			bool check = false;
			for(int i = k,dk = c + k;i < dk;++i) {
				if(a[q[i]] == true) {
					check = true;
					break;
				}
			}
			if(c >= 0 && check == false) {
				q.erase(k,c);
				q.insert(k,"*");
			}
		}
		for(int i = 0;i < v.size();++i) {
			if(v[i] >= q.length()) break;
			if(a[q[v[i]]] == true) q[v[i]] = '?';
			else break;
		}
		if(q == s) OK = true;
		if(k != -1) {
			string zz = s;
			if(q == zz.erase(k,1)) OK = true;
		}
		if(OK == true) cout << "YES" << endl;
		else cout << "NO" << endl;
	}
}


#include <bits/stdc++.h>
using namespace std;
string q,n,s;
bool a[300];
vector<int> v;
int query,k = -1;
bool check(string ch) {
	for(int i = 0;i < ch.length();++i) if(a[ch[i]] == true) return false;
	return true;
}
int main() {
	cin >> n >> s;
	for(int i = 0;i < n.length();++i) a[n[i]] = true;
	for(int i = 0;i < s.length();++i) {
		if(s[i] == '?') v.push_back(i);
		else if(s[i] == '*') k = i,s.erase(i,1);
	}
	cin >> query;while(query--) {
		string q; cin >> q;
		if(q.length() < s.length()) {cout << "NO\n";continue;}
		if(check(q.substr(k,q.length() - s.length())) == true)
			q.erase(k,q.length() - s.length());
		for(int i = 0;i < v.size();++i)
			if(a[q[v[i]]] == true) q[v[i]] = '?';
		cout << (s == q ? "YES\n" : "NO\n");
	}
}


#include <bits/stdc++.h>
using namespace std;
string q,n,s;
bool a[300];
vector<int> v;
int tttt,k = -1;
int main() {
	cin >> n >> s;
	for(int i = 0;i < n.length();++i) a[n[i]] = true;
	for(int i = 0;i < s.length();++i) {
		if(s[i] == '?') v.push_back(i);
		else if(s[i] == '*') k = i,s.erase(i,1);
	}
	cin >> tttt;while(tttt--) {
		bool OK = false;
		cin >> q;
		if(k != -1 && k < q.length()) {
			int c = (int)q.length() - (int)s.length();
			bool check = false;
			for(int i = k,dk = c + k;i <= dk;++i) {
				if(a[q[i]] == true) {
					check = true;
					break;
				}
			}
			if(check == false && c >= 0) q.erase(k,c);
		}
		for(int i = 0;i < v.size();++i) {
			if(v[i] >= q.length()) break;
			if(a[q[v[i]]] == true) q[v[i]] = '?';
			else break;
		}
		if(q == s) OK = true;
		if(OK == true) cout << "YES" << endl;
		else cout << "NO" << endl;
	}
}


#include <bits/stdc++.h>
using namespace std;
string q,n,s;
bool a[300];
vector<int> v;
int tttt,k = -1;
int main() {
	cin >> n >> s;
	for(int i = 0;i < n.length();++i) a[n[i]] = true;
	for(int i = 0;i < s.length();++i) {
		if(s[i] == '?') v.push_back(i);
		else if(s[i] == '*') k = i,s.erase(i,1);
	}
	cin >> tttt;while(tttt--) {
		bool OK = false;
		cin >> q;
		if(k != -1 && k < q.length()) {
			int c = (int)q.length() - (int)s.length() + 1;
			bool check = false;
			for(int i = k,dk = c + k;i < dk;++i) {
				if(a[q[i]] == true) {
					check = true;
					break;
				}
			}
			if(check == false && c >= 1) q.erase(k,c);
		}
		for(int i = 0;i < v.size();++i) {
			if(v[i] >= q.length()) break;
			if(a[q[v[i]]] == true) q[v[i]] = '?';
			else break;
		}
		if(q == s) OK = true;
		if(OK == true) cout << "YES" << endl;
		else cout << "NO" << endl;
	}
}


#include <bits/stdc++.h>
using namespace std;
string q,n,s;
bool a[300];
vector<int> v;
int tttt,k = -1;
int main() {
	cin >> n >> s;
	for(int i = 0;i < n.length();++i) a[n[i]] = true;
	for(int i = 0;i < s.length();++i) {
		if(s[i] == '?') v.push_back(i);
		else if(s[i] == '*') k = i;
	}
	cin >> tttt;while(tttt--) {
		bool OK = false;
		cin >> q;
		if(k != -1) s[k] = q[k];
		for(int i = 0;i < v.size();++i) {
			if(v[i] >= q.length()) break;
			if(a[q[v[i]]] == true) q[v[i]] = '?';
			else break;
		}
		if(q == s) OK = true;
		if(k != -1) {
			string zz = s;
			if(q == zz.erase(k,1)) OK = true;
		}
		if(OK == true) cout << "YES" << endl;
		else cout << "NO" << endl;
	}
}


#include <bits/stdc++.h>
using namespace std;
string n,s;
bool a[300];
vector<int> v;
int query,k = -1;
bool check(string ch) {
	for(int i = 0;i < ch.length();++i) if(a[ch[i]] == true) return false;
	return true;
}
int main() {
	cin >> n >> s;
	for(int i = 0;i < n.length();++i) a[n[i]] = true;
	for(int i = 0;i < s.length();++i) {
		if(s[i] == '?') v.push_back(i);
		else if(s[i] == '*') k = i,s.erase(i,1);
	}
	cin >> query;while(query--) {
		string q; cin >> q;
		if(q.length() > s.length() && k != -1)
			if(check(q.substr(k,q.length() - s.length())) == true)
				q.erase(k,q.length() - s.length());
		for(int i = 0;i < v.size();++i)
			if(q.length() <= v[i]) break;
			else if(a[q[v[i]]] == true) q[v[i]] = '?';else break;
		cout << (s == q ? "YES\n" : "NO\n");
	}
}


#include <bits/stdc++.h>
using namespace std;
string n,s;
bool a[300];
vector<int> v;
int query,k = -1;
bool check(string ch) {
	for(int i = 0;i < ch.length();++i) if(a[ch[i]] == true) return false;
	return true;
}
int main() {
	cin >> n >> s;
	for(int i = 0;i < n.length();++i) a[n[i]] = true;
	for(int i = 0;i < s.length();++i) {
		if(s[i] == '?') v.push_back(i);
		else if(s[i] == '*') k = i,s.erase(i,1),i--;
	}
	cin >> query;while(query--) {
		string q; cin >> q;
		if(q.length() >= s.length() && k != -1)
			if(check(q.substr(k,q.length() - s.length())) == true)
				q.erase(k,q.length() - s.length());
		for(int i = 0,j = v[i++];i < v.size();)
			if(a[q[j]] == true) q[j] = '?';else break;
		cout << (q == s ? "YES\n" : "NO\n");
	}
}


#include <bits/stdc++.h>
using namespace std;
string q,n,s;
bool a[300];
vector<int> v;
int tttt,k = -1;
int main() {
	cin >> n >> s;
	for(int i = 0;i < n.length();++i) a[n[i]] = true;
	for(int i = 0;i < s.length();++i) {
		if(s[i] == '?') v.push_back(i);
		else if(s[i] == '*') k = i,s.erase(i,1);
	}
	cin >> tttt;while(tttt--) {
		bool OK = false;
		cin >> q;
		if(k != -1 && k < q.length()) {
			int c = (int)q.length() - (int)s.length();
			bool check = false;
			for(int i = k,dk = c + k;i < dk;++i) {
				if(a[q[i]] == true) {
					check = true;
					break;
				}
			}
			if(check == false && c >= 0) q.erase(k,c);
		}
		for(int i = 0;i < v.size();++i) {
			if(v[i] >= q.length()) break;
			if(a[q[v[i]]] == true) q[v[i]] = '?';
			else break;
		}
		if(q == s) OK = true;
		if(OK == true) cout << "YES" << endl;
		else cout << "NO" << endl;
	}
}


#include <bits/stdc++.h>
using namespace std;
string q,n,s;
bool a[300];
vector<int> v;
int tttt,k = -1;
int main() {
	cin >> n >> s;
	for(int i = 0;i < n.length();++i) a[n[i]] = true;
	for(int i = 0;i < s.length();++i) {
		if(s[i] == '?') v.push_back(i);
		else if(s[i] == '*') k = i;
	}
	cin >> tttt;while(tttt--) {
		bool OK = false;
		cin >> q;
		if(k != -1 && k < q.length()) s[k] = q[k];
		for(int i = 0;i < v.size();++i) {
			if(v[i] >= q.length()) break;
			if(a[q[v[i]]] == true) q[v[i]] = '?';
			else break;
		}
		if(q == s) OK = true;
		if(k != -1) {
			string zz = s;
			if(q == zz.erase(k,1)) OK = true;
		}
		if(OK == true) cout << "YES" << endl;
		else cout << "NO" << endl;
	}
}


#include <bits/stdc++.h>
using namespace std;
string q,n,s;
bool a[300];
vector<int> v;
int query,k = -1;
bool check(string ch) {
	for(int i = 0;i < ch.length();++i) if(a[ch[i]] == false) return false;
	return true;
}
int main() {
	cin >> n >> s;
	for(int i = 0;i < n.length();++i) a[n[i]] = true;
	for(int i = 0;i < s.length();++i) {
		if(s[i] == '?') v.push_back(i);
		else if(s[i] == '*') k = i,s.erase(i,1);
	}
	cin >> query;while(query--) {
		string q; cin >> q;
		if(q.length() > s.length()) {
			if(check(q.substr(k,q.length() - s.length())))
				q.erase(k,q.length() - s.length());
		}
		for(int i = 0;i < v.size();++i) {
			if(v[i] >= q.length()) break;
			else if(a[q[v[i]]] == true) q[v[i]] = '?';
		}
		cout << (s == q ? "YES\n" : "NO\n");
	}
}


#include <bits/stdc++.h>
using namespace std;
string q,n,s;
bool a[300];
vector<int> v;
int tttt,k = -1;
int main() {
	cin >> n >> s;
	for(int i = 0;i < n.length();++i) a[n[i]] = true;
	for(int i = 0;i < s.length();++i) {
		if(s[i] == '?') v.push_back(i);
		else if(s[i] == '*') k = i;
	}
	cin >> tttt;while(tttt--) {
		bool OK = false;
		cin >> q;
		if(k != -1 && k < q.length()) {
			int c = (int)q.length() - (int)s.length() + 1;
			bool check = false;
			for(int i = k,dk = c + k;i < dk;++i) {
				if(a[q[i]] == true) {
					check = true;
					break;
				}
			}
			if(c >= 1 && check == false) {
				q.erase(k,c);
				q.insert(k,"*");
			}
		}
		for(int i = 0;i < v.size();++i) {
			if(v[i] >= q.length()) break;
			if(a[q[v[i]]] == true) q[v[i]] = '?';
			else break;
		}
		if(q == s) OK = true;
		if(k != -1) {
			string zz = s;
			if(q == zz.erase(k,1)) OK = true;
		}
		if(OK == true) cout << "YES" << endl;
		else cout << "NO" << endl;
	}
}


#include <bits/stdc++.h>
using namespace std;
string q,n,s;
bool a[300];
vector<int> v;
int tttt,k = -1;
int main() {
	cin >> n >> s;
	for(int i = 0;i < n.length();++i) a[n[i]] = true;
	for(int i = 0;i < s.length();++i) {
		if(s[i] == '?') v.push_back(i);
		else if(s[i] == '*') k = i;
	}
	cin >> tttt;while(tttt--) {
		bool OK = false;
		cin >> q;
		if(k != -1) q[k] = '*';
		for(int i = 0;i < v.size();++i) {
			if(v[i] >= q.length()) break;
			if(a[q[v[i]]] == true) q[v[i]] = '?';
			else break;
		}
		if(q == s) OK = true;
		if(k != -1) {
			string zz = s;
			if(q == zz.erase(k,1)) OK = true;
		}
		if(OK == true) cout << "YES" << endl;
		else cout << "NO" << endl;
	}
}


#include <bits/stdc++.h>
using namespace std;
string n,s;
bool a[300];
vector<int> v;
int query,k = -1;
bool check(string ch) {
	for(int i = 0;i < ch.length();++i) if(a[ch[i]] == true) return false;
	return true;
}
int main() {
	cin >> n >> s;
	for(int i = 0;i < n.length();++i) a[n[i]] = true;
	for(int i = 0;i < s.length();++i) {
		if(s[i] == '?') v.push_back(i);
		else if(s[i] == '*') k = i,s.erase(i,1);
	}
	cin >> query;while(query--) {
		string q; cin >> q;
		if(q.length() > s.length() && k != -1)
			if(check(q.substr(k,min(q.length() - k,q.length() - s.length()))) == true)
				q.erase(k,q.length() - s.length());
		for(int i = 0;i < v.size();++i)
			if(q.length() <= v[i]) break;
			else if(a[q[v[i]]] == true) q[v[i]] = '?';
		cout << (q == s ? "YES\n" : "NO\n");
	}
}


#include <bits/stdc++.h>
using namespace std;
string q,n,s;
bool a[300];
vector<int> v;
int query,k = -1;
bool check(string ch) {
	for(int i = 0;i < ch.length();++i) if(a[ch[i]] == true) return false;
	return true;
}
int main() {
	cin >> n >> s;
	for(int i = 0;i < n.length();++i) a[n[i]] = true;
	for(int i = 0;i < s.length();++i) {
		if(s[i] == '?') v.push_back(i);
		else if(s[i] == '*') k = i,s.erase(i,1);
	}
	cin >> query;while(query--) {
		string q; cin >> q;
		if(q.length() < s.length()) {cout << "NO\n";continue;}
		if(q.length() > s.length())
			if(check(q.substr(k,q.length() - s.length())) == true)
				q.erase(k,q.length() - s.length());
		for(int i = 0;i < v.size();++i)
			if(v[i] >= q.length()) break;
			else if(a[q[v[i]]] == true) q[v[i]] = '?';
		cout << (s == q ? "YES\n" : "NO\n");
	}
}


#include <bits/stdc++.h>
using namespace std;
string q,n,s;
bool a[300];
vector<int> v;
int query,k = -1;
bool check(string ch) {
	for(int i = 0;i < ch.length();++i) if(a[ch[i]] == false) return false;
	return true;
}
int main() {
	cin >> n >> s;
	for(int i = 0;i < n.length();++i) a[n[i]] = true;
	for(int i = 0;i < s.length();++i) {
		if(s[i] == '?') v.push_back(i);
		else if(s[i] == '*') k = i,s.erase(i,1);
	}
	cin >> query;while(query--) {
		string q; cin >> q;
		if(q.length() > s.length()) {
			if(check(q.substr(k,q.length() - s.length())))
				q.erase(k,q.length() - s.length());
			else {cout << "NO\n";continue;}
		}
		for(int i = 0;i < v.size();++i) {
			if(v[i] >= q.length()) break;
			else if(a[q[v[i]]] == true) q[v[i]] = '?';
		}
		cout << (s == q ? "YES\n" : "NO\n");
	}
}


# include <bits/stdc++.h>
using namespace std;
# define ll long long int

int main()
{
	ll t;
	cin>>t;
	while (t--)
	{
		ll n,k,i,ma=INT_MIN,mi=INT_MAX;
		cin>>n>>k;
		ll a[n];
		for (i=0;i<n;i++)
		{
			cin>>a[i];
			ma=max(ma,a[i]);
			mi=min(mi,a[i]);
		}
		if (mi+k>=ma)
			cout<<mi+k<<endl;
		else
		{
			ll b,c,f=-1;
			for (i=n-1;i>=0;i--)
			{
				b=ma-a[i];
				c=mi-a[i];
				if (abs(c)<=k && abs(b)<=k)
				{
					f=max(f,a[i]);
				}
			}
			cout<<f<<endl;
		}
	}
}


#include <iostream>
using namespace std;

int main(void)
{
	int A[200] = { 0 };
	int T, N, K, B, i, j;
	int max, min;
	cin >> T;
	for(i = 0; i < T; i++)
	{
		scanf("%d %d", &N, &K);
		for(j = 0; j < N; j++)
		{
			scanf("%d", &A[j]);
			if(j == 0)
			{
				max = A[j] + K;
				min = A[j] - K;
			}
			else if(A[j] - K > max || A[j] + K < min)
			{
				printf("-1\n");
				break;
			}
			else
			{
				if(A[j] - K > min)
					min = A[j] - K;
				if(A[j] + K < max)
					max = A[j] + K;
			}
		}
		if(j == N)
			printf("%d\n", max);
	}
}


#include <iostream>
using namespace std;

int main(void)
{
	long long A[200] = { 0 };
	long long T, N, K, B, i, j;
	long long max, min;
	cin >> T;
	for(i = 0; i < T; i++)
	{
		scanf("%lld %lld", &N, &K);
		for(j = 0; j < N; j++)
		{
			scanf("%lld", &A[j]);
			if(j == 0)
			{
				max = A[j] + K;
				min = A[j] - K;
			}
			else if(A[j] - K > max || A[j] + K < min)
			{
				printf("-1\n");
				break;
			}
			else
			{
				if(A[j] - K > min)
					min = A[j] - K;
				if(A[j] + K < max)
					max = A[j] + K;
			}
		}
		if(j == N)
		{
			if(max < 1)
				printf("%lld\n", -1);
			else
				printf("%lld\n", max);
		}
	}
}


#include <iostream>
using namespace std;

int main(void)
{
	int A[200] = { 0 };
	int T, N, K, B, r;
	cin >> T;
	for(int i = 0; i < T; i++)
	{
		int Check[100000005] = { 0 };
		scanf("%d %d", &N, &K);
		for(int j = 0; j < N; j++)
		{
			scanf("%d", &A[j]);
			for(int s = A[j] - K; s <= A[j] + K; s++)
				Check[s]++;
		}
		for(r = 100000004; r >= 0; r--)
		{
			if(Check[r] == N)
			{
				printf("%d\n", r);
				break;
			}
		}
		if(r == -1)
			printf("%d\n", -1);
	}
}


#include <iostream>
using namespace std;

int main(void)
{
	int A[200] = { 0 };
	int T, N, K, B, i, j;
	int max, min;
	cin >> T;
	for(i = 0; i < T; i++)
	{
		scanf("%d %d", &N, &K);
		for(j = 0; j < N; j++)
		{
			scanf("%d", &A[j]);
			if(j == 0)
			{
				max = A[j] + K;
				min = A[j] - K;
				if(min <= 0)
					min = 1;
			}
			else if(A[j] - K > max || A[j] + K < min)
			{
				printf("-1\n");
				break;
			}
			else
			{
				if(A[j] - K > min)
					min = A[j] - K;
				if(A[j] + K < max)
					max = A[j] + K;
			}
		}
		if(j == N)
			printf("%d\n", max);
	}
}


#include <iostream>
using namespace std;

int main(void)
{
	long long A[200] = { 0 };
	long long T, N, K, B, i, j;
	long long max, min;
	cin >> T;
	for(i = 0; i < T; i++)
	{
		scanf("%lld %lld", &N, &K);
		for(j = 0; j < N; j++)
			scanf("%lld", &A[j]);
		for(j = 0; j < N; j++)
		{
			if(j == 0)
			{
				max = A[j] + K;
				min = A[j] - K;
			}
			else if(A[j] - K > max || A[j] + K < min)
			{
				printf("-1\n");
				break;
			}
			else
			{
				if(A[j] - K > min)
					min = A[j] - K;
				if(A[j] + K < max)
					max = A[j] + K;
			}
		}
		if(j == N)
			printf("%lld\n", max);
	}
}


#include <iostream>
using namespace std;

int main(void)
{
	long long A[200] = { 0 };
	long long T, N, K, B, i, j;
	long long max, min;
	cin >> T;
	for(i = 0; i < T; i++)
	{
		scanf("%lld %lld", &N, &K);
		for(j = 0; j < N; j++)
		{
			scanf("%lld", &A[j]);
			if(j == 0)
			{
				max = A[j] + K;
				min = A[j] - K;
			}
			else if(A[j] - K > max || A[j] + K < min)
			{
				printf("-1\n");
				break;
			}
			else
			{
				if(A[j] - K > min)
					min = A[j] - K;
				if(A[j] + K < max)
					max = A[j] + K;
			}
		}
		if(j == N)
			printf("%lld\n", max);
	}
}


#include <iostream>
using namespace std;

int main(void)
{
	long long A[200] = { 0 };
	long long T, N, K, B, i, j;
	long long max, min, a, b;
	cin >> T;
	for(i = 0; i < T; i++)
	{
		scanf("%lld %lld", &N, &K);
		for(j = 0; j < N; j++)
		{
			scanf("%lld", &A[j]);
			if(j == 0)
			{
				max = A[j] + K;
				min = A[j] - K;
				if(min < 1) min = 1;
				if(max > 100000000) max = 100000000;
			}
			else
			{
				a = A[j] - K; b = A[j] + K;
				if(a < 1) a = 1;
				if(b > 100000000) b = 100000000;
				if(a > max || b < min)
				{
					printf("-1\n");
					break;
				}
				else
				{
					if(a > min)
						min = a;
					if(b < max)
						max = b;
				}
			}
		}
		if(j == N)
		{
			if(max < 1)
				printf("%lld\n", -1);
			else
				printf("%lld\n", max);
		}
	}
}


#include <iostream>
using namespace std;

int main(void)
{
	int A[200] = { 0 };
	int T, N, K, B, r;
	cin >> T;
	for(int i = 0; i < T; i++)
	{
		int Check[300000005] = { 0 };
		scanf("%d %d", &N, &K);
		for(int j = 0; j < N; j++)
		{
			scanf("%d", &A[j]);
			for(int s = A[j] - K; s <= A[j] + K; s++)
			{
				if(s < 0) continue;
				Check[s]++;
			}
		}
		for(r = 300000004; r >= 0; r--)
		{
			if(Check[r] == N)
			{
				printf("%d\n", r);
				break;
			}
		}
		if(r == -1)
			printf("%d\n", -1);
	}
}


#include <iostream>
using namespace std;

int main(void)
{
	int A[200] = { 0 };
	int T, N, K, B, r;
	cin >> T;
	for(int i = 0; i < T; i++)
	{
		int max = -111;
		int min = 9999999999;
		int Check[300000005] = { 0 };
		scanf("%d %d", &N, &K);
		for(int j = 0; j < N; j++)
		{
			scanf("%d", &A[j]);
			if(max < A[j]) max = A[j];
			if(min > A[j]) min = A[j];
			for(int s = A[j] - K; s <= A[j] + K; s++)
			{
				if(s < 0) continue;
				Check[s]++;
			}
		}
		for(r = max + K; r >= min - K; r--)
		{
			if(Check[r] == N)
			{
				printf("%d\n", r);
				break;
			}
		}
		if(r == min - K - 1)
			printf("%d\n", -1);
	}
}


#include <iostream>
using namespace std;

int main(void)
{
	int A[200] = { 0 };
	int T, N, K, B, r;
	cin >> T;
	for(int i = 0; i < T; i++)
	{
		int Check[300000005] = { 0 };
		scanf("%d %d", &N, &K);
		for(int j = 0; j < N; j++)
		{
			scanf("%d", &A[j]);
			for(int s = A[j] - K; s <= A[j] + K; s++)
				Check[s]++;
		}
		for(r = 300000004; r >= 0; r--)
		{
			if(Check[r] == N)
			{
				printf("%d\n", r);
				break;
			}
		}
		if(r == -1)
			printf("%d\n", -1);
	}
}


#include <iostream>
using namespace std;

int main(void)
{
	long long A[200] = { 0 };
	long long T, N, K, B, i, j;
	long long max, min, a, b;
	cin >> T;
	for(i = 0; i < T; i++)
	{
		scanf("%lld %lld", &N, &K);
		for(j = 0; j < N; j++)
		{
			scanf("%lld", &A[j]);
			if(j == 0)
			{
				max = A[j] + K;
				min = A[j] - K;
			}
			else
			{
				a = A[j] - K; b = A[j] + K;
				if(a < 1) a = 0;
				if(b > 100000000) b = 100000000;
				if(a > max || b < min)
				{
					printf("-1\n");
					break;
				}
				else
				{
					if(a > min)
						min = a;
					if(b < max)
						max = b;
				}
			}
		}
		if(j == N)
		{
			if(max < 1)
				printf("%lld\n", -1);
			else
				printf("%lld\n", max);
		}
	}
}


/* winners never quit and quitters never win.
      #swap                                 */
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
#pragma GCC optimize ("-O3")
#define mod 1000000007
#define inf 1000000000000000000
#define rk 256
#define pb push_back
#define mp make_pair
#define vc vector
#define fs first
#define sec second
#define pq priority_queue
#define lb lower_bound
#define ub upper_bound
#define pll pair<ll,ll>
#define pls pair<ll,string>
#define psl pair<string,ll>
#define plc pair<ll,char>
#define pcl pair<char,ll>
#define pss pair<string,string>
#define all(v) v.begin(),v.end()
#define tol(s) transform(s.begin(),s.end(),s.begin(),::tolower);
#define tou(s) transform(s.begin(),s.end(),s.begin(),::toupper);
#define T ll t=0;cin>>t;for(ll test=0;test<t;test++)

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
	//isprime();
	//coeff();
	/*freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);*/
    T
    {
        ll n,k;
        cin>>n>>k;
        ll x[n];
        for(ll a=0;a<n;a++)
            cin>>x[a];
        sort(x,x+n);
        ll low=max(1LL,x[0]-k),high=x[n-1]+k;
        while(low<=high)
        {
            ll mid=(low+high)/2;
            bool flag=true;
            for(ll a=0;a<n;a++)
            {
                if(llabs(x[a]-mid)>k)
                    flag=false;
            }
            if(flag)
                low=mid+1;
            else
                high=mid-1;
        }
        for(ll a=0;a<n;a++)
            if(llabs(x[a]-high)>k)
                high=-1;
        cout<<high<<endl;
    }
	return 0;
}


/* winners never quit and quitters never win.
      #swap                                 */
#include <bits/stdc++.h>
using namespace std;
typedef int ll;
typedef long double ld;
#pragma GCC optimize ("-O3")
#define mod 1000000007
#define inf 1000000000000000000
#define rk 256
#define pb push_back
#define mp make_pair
#define vc vector
#define fs first
#define sec second
#define pq priority_queue
#define lb lower_bound
#define ub upper_bound
#define pll pair<ll,ll>
#define pls pair<ll,string>
#define psl pair<string,ll>
#define plc pair<ll,char>
#define pcl pair<char,ll>
#define pss pair<string,string>
#define all(v) v.begin(),v.end()
#define tol(s) transform(s.begin(),s.end(),s.begin(),::tolower);
#define tou(s) transform(s.begin(),s.end(),s.begin(),::toupper);
#define T ll t=0;cin>>t;for(ll test=0;test<t;test++)

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
	//isprime();
	//coeff();
	/*freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);*/
    T
    {
        ll n,k;
        cin>>n>>k;
        ll x[n];
        for(ll a=0;a<n;a++)
            cin>>x[a];
        sort(x,x+n);
        ll ans=-1;
        for(ll a=n-1;a>=0;a--)
        {
            bool flag=true;
            ll temp=x[a]+k;
            if(temp>x[0]+k)
                temp=x[0]+k;
            if(temp<x[n-1]-k)
                break;
            for(ll b=n-1;b>=0;b--)
            {
                if(llabs(temp-x[a])>k)
                    flag=false;
            }
            if(flag)
            {
                ans=temp;
                break;
            }
        }
        cout<<ans<<endl;
    }
	return 0;
}


/* winners never quit and quitters never win.
      #swap                                 */

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#pragma GCC optimize ("-O3")
using ll = long long;
using ld = long double;
const ll mod = 1000000007;
const ll inf = 1000000000000000000;
const ll rk = 256;
const ld PI = 3.141592653589793;

typedef tree<
int,
null_type,
less<int>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;

template <typename T>
void make_unique(vector<T>& vec) {
  sort(all(vec));
  vec.erase(unique(all(vec)), vec.end());
}

#define endl '\n'
#define pb push_back
#define mp make_pair
#define vc vector
#define fs first
#define sec second
#define pq priority_queue
#define lb lower_bound
#define ub upper_bound
#define pll pair<ll,ll>
#define pls pair<ll,string>
#define psl pair<string,ll>
#define plc pair<ll,char>
#define pcl pair<char,ll>
#define pss pair<string,string>
#define all(x) (x).begin(), (x).end()
#define tol(s) transform(s.begin(),s.end(),s.begin(),::tolower);
#define tou(s) transform(s.begin(),s.end(),s.begin(),::toupper);
#define printclock cerr<<"Time : "<<1000*(ld)clock()/(ld)CLOCKS_PER_SEC<<"ms\n";

#define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }

void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
	cerr << *it << " = " << a << endl;
	err(++it, args...);
}

#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define T ll t=0;cin>>t;for(ll test=0;test<t;test++)

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

ll nxt() {
    ll TemporaryVariable;
    cin >> TemporaryVariable;
    return TemporaryVariable;
}

struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        // http://xorshift.di.unimi.it/splitmix64.c
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};

int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
	//isprime();
	//coeff();
	/*freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);*/
    T
	{
		//cout<<"Case #"<<test+1<<": ";
		ll n=nxt(),k=nxt();
        vc<ll> v(n);
        generate(all(v),nxt);
        sort(all(v));
        if(v[0]+k<v[n-1]-k)
        {
            cout<<-1<<endl;
        }
        else
        {
            cout<<v[0]+k<<endl;
        }

	}
	printclock;
	return 0;
}


/* winners never quit and quitters never win.
      #swap                                 */
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
#pragma GCC optimize ("-O3")
#define mod 1000000007
#define inf 1000000000000000000
#define rk 256
#define pb push_back
#define mp make_pair
#define vc vector
#define fs first
#define sec second
#define pq priority_queue
#define lb lower_bound
#define ub upper_bound
#define pll pair<ll,ll>
#define pls pair<ll,string>
#define psl pair<string,ll>
#define plc pair<ll,char>
#define pcl pair<char,ll>
#define pss pair<string,string>
#define all(v) v.begin(),v.end()
#define tol(s) transform(s.begin(),s.end(),s.begin(),::tolower);
#define tou(s) transform(s.begin(),s.end(),s.begin(),::toupper);
#define T ll t=0;cin>>t;for(ll test=0;test<t;test++)

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
	//isprime();
	//coeff();
	/*freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);*/
    T
    {
        ll n,k;
        cin>>n>>k;
        ll x[n];
        for(ll a=0;a<n;a++)
            cin>>x[a];
        sort(x,x+n);
        ll low=max(0*1LL,x[0]-k),high=x[n-1]+k;
        while(low<=high)
        {
            ll mid=(low+high)/2;
            bool flag=true;
            for(ll a=0;a<n;a++)
            {
                if(llabs(x[a]-mid)>k)
                    flag=false;
            }
            if(flag)
                low=mid+1;
            else
                high=mid-1;
        }
        for(ll a=0;a<n;a++)
            if(llabs(x[a]-high)>k)
                high=-1;
        cout<<high<<endl;
    }
	return 0;
}


/* winners never quit and quitters never win.
      #swap                                 */
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
#pragma GCC optimize ("-O3")
#define mod 1000000007
#define inf 1000000000000000000
#define rk 256
#define pb push_back
#define mp make_pair
#define vc vector
#define fs first
#define sec second
#define pq priority_queue
#define lb lower_bound
#define ub upper_bound
#define pll pair<ll,ll>
#define pls pair<ll,string>
#define psl pair<string,ll>
#define plc pair<ll,char>
#define pcl pair<char,ll>
#define pss pair<string,string>
#define all(v) v.begin(),v.end()
#define tol(s) transform(s.begin(),s.end(),s.begin(),::tolower);
#define tou(s) transform(s.begin(),s.end(),s.begin(),::toupper);
#define T ll t=0;cin>>t;for(ll test=0;test<t;test++)

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
	//isprime();
	//coeff();
	/*freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);*/
    T
    {
        ll n,k;
        cin>>n>>k;
        ll x[n];
        for(ll a=0;a<n;a++)
            cin>>x[a];
        sort(x,x+n);
        ll low=max(0*1LL,x[0]-k),high=x[n-1]+k;
        while(low<=high)
        {
            ll mid=(low+high)/2;
            bool flag=true;
            for(ll a=0;a<n;a++)
            {
                if(llabs(x[a]-mid)>k)
                    flag=false;
            }
            if(flag)
                low=mid+1;
            else
                high=mid-1;
        }
        cout<<high<<endl;
    }
	return 0;
}


#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#pragma GCC optimize("Ofast")
//*********************************************DO IT NOW****************************************************************
#include<bits/stdc++.h>
#include<random>
#define int long long
#define pp pair<int,int>
#define ss second
#define ff first
#define pb push_back
#define mod 1000000007
#define pi 3.14159265359
#define mk(arr,n,type)  type *arr=new type[n];
#define sl s1.length();
#define yes cout<< "YES"<<endl
#define no  cout<< "NO"<<endl
#define all(v) (v).begin(),(v).end()
#define s(v)   sort(v,v+n)
#define  mt mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define read(a,n) for(int i=0;i<n;i++)cin>>a[i]
#define print(a,n) for(int i=0;i<n;i++)cout<<a[i]<<" "
#define for1(i,a,n)  for(int i=0;i<n;i++)
#define rep(i, c) for(int i = 0; i < (int)c; i++)
#define set(x) cout<<fixed<<setprecision(x);
#define cone(x) (__builtin_popcountll(x))        // zero indexed
#define fone(x) (__builtin_ffsll(x) -1LL)        //give wrong answer if x==0;(zero indexed)
#define mone(x) (63LL-__builtin_clzll(x))
using namespace std;
//**********************************************DO IT NOW****************************************************************
void fastio()
{
  #ifndef ONLINE_JUDGE
  freopen("INPUT.txt","r",stdin);
  freopen("OUTPUT.txt","w",stdout);
  #endif
  ios_base :: sync_with_stdio(false);
  cin.tie(NULL) ;
  cout.tie(NULL);
}
int dp[101][1001],ans1=1e9,dp1[1000001];
int num[10];
int a[1000000];
signed main()
{
    fastio();
    int t=1;
    cin>>t;
    while(t--)
    {
      int n,k;
      cin>>n>>k;
      int a[n];
      read(a,n);
      int x=*max_element(a,a+n);
      int xx=*min_element(a,a+n);
      int c=0;
      int aa=(x+xx),a1=(x+xx)/2;
      for(int i=aa;i>=xx;i--)
      {
        //  cout<<i<<"\n";
          if(abs(x-i)<=k && (abs(xx-i)<=k))
          {
              cout<<i<<"\n";
              c++;
              break;
          }
      }
     // cout<<aa<<"\n";
      if(c==0)
      cout<<"-1\n";

    }
}
// 🍪 🍪 🍪


#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#pragma GCC optimize("Ofast")
//*********************************************DO IT NOW****************************************************************
#include<bits/stdc++.h>
#include<random>
#define int long long
#define pp pair<int,int>
#define ss second
#define ff first
#define pb push_back
#define mod 1000000007
#define pi 3.14159265359
#define mk(arr,n,type)  type *arr=new type[n];
#define sl s1.length();
#define yes cout<< "YES"<<endl
#define no  cout<< "NO"<<endl
#define all(v) (v).begin(),(v).end()
#define s(v)   sort(v,v+n)
#define  mt mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define read(a,n) for(int i=0;i<n;i++)cin>>a[i]
#define print(a,n) for(int i=0;i<n;i++)cout<<a[i]<<" "
#define for1(i,a,n)  for(int i=0;i<n;i++)
#define rep(i, c) for(int i = 0; i < (int)c; i++)
#define set(x) cout<<fixed<<setprecision(x);
#define cone(x) (__builtin_popcountll(x))        // zero indexed
#define fone(x) (__builtin_ffsll(x) -1LL)        //give wrong answer if x==0;(zero indexed)
#define mone(x) (63LL-__builtin_clzll(x))
using namespace std;
//**********************************************DO IT NOW****************************************************************
void fastio()
{
  #ifndef ONLINE_JUDGE
  freopen("INPUT.txt","r",stdin);
  freopen("OUTPUT.txt","w",stdout);
  #endif
  ios_base :: sync_with_stdio(false);
  cin.tie(NULL) ;
  cout.tie(NULL);
}
int dp[101][1001],ans1=1e9,dp1[1000001];
int num[10];
int a[1000000];
signed main()
{
    fastio();
    int t=1;
    cin>>t;
    while(t--)
    {
      int n,k;
      cin>>n>>k;
      int a[n];
      read(a,n);
      int x=*max_element(a,a+n);
      int xx=*min_element(a,a+n);

      int aa=(x+xx),a1=(x+xx)/2;
       if(abs(a1-x)<=k && abs(a1-xx)<=k)
       {
      for(int i=aa;i>=xx;i--)
      {
        //  cout<<i<<"\n";
          if(abs(x-i)<=k && (abs(xx-i)<=k))
          {
              cout<<i<<"\n";
              break;
          }
      }
       }
     // cout<<aa<<"\n";
      else
      cout<<"-1\n";

    }
}
// 🍪 🍪 🍪


#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#pragma GCC optimize("Ofast")
//*********************************************DO IT NOW****************************************************************
#include<bits/stdc++.h>
#include<random>
#define int long long
#define pp pair<int,int>
#define ss second
#define ff first
#define pb push_back
#define mod 1000000007
#define pi 3.14159265359
#define mk(arr,n,type)  type *arr=new type[n];
#define sl s1.length();
#define yes cout<< "YES"<<endl
#define no  cout<< "NO"<<endl
#define all(v) (v).begin(),(v).end()
#define s(v)   sort(v,v+n)
#define  mt mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define read(a,n) for(int i=0;i<n;i++)cin>>a[i]
#define print(a,n) for(int i=0;i<n;i++)cout<<a[i]<<" "
#define for1(i,a,n)  for(int i=0;i<n;i++)
#define rep(i, c) for(int i = 0; i < (int)c; i++)
#define set(x) cout<<fixed<<setprecision(x);
#define cone(x) (__builtin_popcountll(x))        // zero indexed
#define fone(x) (__builtin_ffsll(x) -1LL)        //give wrong answer if x==0;(zero indexed)
#define mone(x) (63LL-__builtin_clzll(x))
using namespace std;
//**********************************************DO IT NOW****************************************************************
void fastio()
{
  #ifndef ONLINE_JUDGE
  freopen("INPUT.txt","r",stdin);
  freopen("OUTPUT.txt","w",stdout);
  #endif
  ios_base :: sync_with_stdio(false);
  cin.tie(NULL) ;
  cout.tie(NULL);
}
int dp[101][1001],ans1=1e9,dp1[1000001];
int num[10];
int a[1000000];
signed main()
{
    fastio();
    int t=1;
    cin>>t;
    while(t--)
    {
      int n,k;
      cin>>n>>k;
      int a[n];
      read(a,n);
      int x=*max_element(a,a+n);
      int xx=*min_element(a,a+n);
      int c=0;
      int aa=(x+xx),a1=(x+xx)/2;
      for(int i=aa;i>=0;i--)
      {
        //  cout<<i<<"\n";
          if(abs(x-i)<=k && (abs(xx-i)<=k))
          {
              cout<<i<<"\n";
              c++;
              break;
          }
      }
     // cout<<aa<<"\n";
      if(c==0)
      cout<<"-1\n";

    }
}
// 🍪 🍪 🍪


#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#pragma GCC optimize("Ofast")
//*********************************************DO IT NOW****************************************************************
#include<bits/stdc++.h>
#include<random>
#define int long long
#define pp pair<int,int>
#define ss second
#define ff first
#define pb push_back
#define mod 1000000007
#define pi 3.14159265359
#define mk(arr,n,type)  type *arr=new type[n];
#define sl s1.length();
#define yes cout<< "YES"<<endl
#define no  cout<< "NO"<<endl
#define all(v) (v).begin(),(v).end()
#define s(v)   sort(v,v+n)
#define  mt mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define read(a,n) for(int i=0;i<n;i++)cin>>a[i]
#define print(a,n) for(int i=0;i<n;i++)cout<<a[i]<<" "
#define for1(i,a,n)  for(int i=0;i<n;i++)
#define rep(i, c) for(int i = 0; i < (int)c; i++)
#define set(x) cout<<fixed<<setprecision(x);
#define cone(x) (__builtin_popcountll(x))        // zero indexed
#define fone(x) (__builtin_ffsll(x) -1LL)        //give wrong answer if x==0;(zero indexed)
#define mone(x) (63LL-__builtin_clzll(x))
using namespace std;
//**********************************************DO IT NOW****************************************************************
void fastio()
{
  #ifndef ONLINE_JUDGE
  freopen("INPUT.txt","r",stdin);
  freopen("OUTPUT.txt","w",stdout);
  #endif
  ios_base :: sync_with_stdio(false);
  cin.tie(NULL) ;
  cout.tie(NULL);
}
int dp[101][1001],ans1=1e9,dp1[1000001];
int num[10];
int a[1000000];
signed main()
{
    fastio();
    int t=1;
    cin>>t;
    while(t--)
    {
      int n,k;
      cin>>n>>k;
      int a[n];
      read(a,n);
      int x=*max_element(a,a+n);
      int xx=*min_element(a,a+n);
      int c=0;
      int aa=(x+xx),a1=(x+xx)/2;
      for(int i=xx+k;i>=abs(xx-k);i--)
      {
        //  cout<<i<<"\n";
          if(abs(x-i)<=k && (abs(xx-i)<=k))
          {
              cout<<i<<"\n";
              c++;
              break;
          }
      }
     // cout<<aa<<"\n";
      if(c==0)
      cout<<"-1\n";

    }
}
// 🍪 🍪 🍪


#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#pragma GCC optimize("Ofast")
//*********************************************DO IT NOW****************************************************************
#include<bits/stdc++.h>
#include<random>
#define int long long
#define pp pair<int,int>
#define ss second
#define ff first
#define pb push_back
#define mod 1000000007
#define pi 3.14159265359
#define mk(arr,n,type)  type *arr=new type[n];
#define sl s1.length();
#define yes cout<< "YES"<<endl
#define no  cout<< "NO"<<endl
#define all(v) (v).begin(),(v).end()
#define s(v)   sort(v,v+n)
#define  mt mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define read(a,n) for(int i=0;i<n;i++)cin>>a[i]
#define print(a,n) for(int i=0;i<n;i++)cout<<a[i]<<" "
#define for1(i,a,n)  for(int i=0;i<n;i++)
#define rep(i, c) for(int i = 0; i < (int)c; i++)
#define set(x) cout<<fixed<<setprecision(x);
#define cone(x) (__builtin_popcountll(x))        // zero indexed
#define fone(x) (__builtin_ffsll(x) -1LL)        //give wrong answer if x==0;(zero indexed)
#define mone(x) (63LL-__builtin_clzll(x))
using namespace std;
//**********************************************DO IT NOW****************************************************************
void fastio()
{
  #ifndef ONLINE_JUDGE
  freopen("INPUT.txt","r",stdin);
  freopen("OUTPUT.txt","w",stdout);
  #endif
  ios_base :: sync_with_stdio(false);
  cin.tie(NULL) ;
  cout.tie(NULL);
}
int dp[101][1001],ans1=1e9,dp1[1000001];
int num[10];
int a[1000000];
signed main()
{
    fastio();
    int t=1;
    cin>>t;
    while(t--)
    {
      int n,k;
      cin>>n>>k;
      int a[n];
      read(a,n);
      int x=*max_element(a,a+n);
      int xx=*min_element(a,a+n);
      int c=0;
      int aa=(x+xx),a1=(x+xx)/2;
      for(int i=10000000;i>=0;i--)
      {
        //  cout<<i<<"\n";
          if(abs(x-i)<=k && (abs(xx-i)<=k))
          {
              cout<<i<<"\n";
              c++;
              break;
          }
      }
     // cout<<aa<<"\n";
      if(c==0)
      cout<<"-1\n";

    }
}
// 🍪 🍪 🍪


#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#pragma GCC optimize("Ofast")
//*********************************************DO IT NOW****************************************************************
#include<bits/stdc++.h>
#include<random>
#define int long long
#define pp pair<int,int>
#define ss second
#define ff first
#define pb push_back
#define mod 1000000007
#define pi 3.14159265359
#define mk(arr,n,type)  type *arr=new type[n];
#define sl s1.length();
#define yes cout<< "YES"<<endl
#define no  cout<< "NO"<<endl
#define all(v) (v).begin(),(v).end()
#define s(v)   sort(v,v+n)
#define  mt mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define read(a,n) for(int i=0;i<n;i++)cin>>a[i]
#define print(a,n) for(int i=0;i<n;i++)cout<<a[i]<<" "
#define for1(i,a,n)  for(int i=0;i<n;i++)
#define rep(i, c) for(int i = 0; i < (int)c; i++)
#define set(x) cout<<fixed<<setprecision(x);
#define cone(x) (__builtin_popcountll(x))        // zero indexed
#define fone(x) (__builtin_ffsll(x) -1LL)        //give wrong answer if x==0;(zero indexed)
#define mone(x) (63LL-__builtin_clzll(x))
using namespace std;
//**********************************************DO IT NOW****************************************************************
void fastio()
{
  #ifndef ONLINE_JUDGE
  freopen("INPUT.txt","r",stdin);
  freopen("OUTPUT.txt","w",stdout);
  #endif
  ios_base :: sync_with_stdio(false);
  cin.tie(NULL) ;
  cout.tie(NULL);
}
int dp[101][1001],ans1=1e9,dp1[1000001];
int num[10];
int a[1000000];
signed main()
{
    fastio();
    int t=1;
    cin>>t;
    while(t--)
    {
      int n,k;
      cin>>n>>k;
      int a[n];
      read(a,n);
      int x=*max_element(a,a+n);
      int xx=*min_element(a,a+n);
      int c=0;
      int aa=(x+xx),a1=(x+xx)/2;
      for(int i=xx+k;i>=0;i--)
      {
        //  cout<<i<<"\n";
          if(abs(x-i)<=k && (abs(xx-i)<=k))
          {
              cout<<i<<"\n";
              c++;
              break;
          }
      }
     // cout<<aa<<"\n";
      if(c==0)
      cout<<"-1\n";

    }
}
// 🍪 🍪 🍪


#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#pragma GCC optimize("Ofast")
//*********************************************DO IT NOW****************************************************************
#include<bits/stdc++.h>
#include<random>
#define int long long
#define pp pair<int,int>
#define ss second
#define ff first
#define pb push_back
#define mod 1000000007
#define pi 3.14159265359
#define mk(arr,n,type)  type *arr=new type[n];
#define sl s1.length();
#define yes cout<< "YES"<<endl
#define no  cout<< "NO"<<endl
#define all(v) (v).begin(),(v).end()
#define s(v)   sort(v,v+n)
#define  mt mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define read(a,n) for(int i=0;i<n;i++)cin>>a[i]
#define print(a,n) for(int i=0;i<n;i++)cout<<a[i]<<" "
#define for1(i,a,n)  for(int i=0;i<n;i++)
#define rep(i, c) for(int i = 0; i < (int)c; i++)
#define set(x) cout<<fixed<<setprecision(x);
#define cone(x) (__builtin_popcountll(x))        // zero indexed
#define fone(x) (__builtin_ffsll(x) -1LL)        //give wrong answer if x==0;(zero indexed)
#define mone(x) (63LL-__builtin_clzll(x))
using namespace std;
//**********************************************DO IT NOW****************************************************************
void fastio()
{
  #ifndef ONLINE_JUDGE
  freopen("INPUT.txt","r",stdin);
  freopen("OUTPUT.txt","w",stdout);
  #endif
  ios_base :: sync_with_stdio(false);
  cin.tie(NULL) ;
  cout.tie(NULL);
}
int dp[101][1001],ans1=1e9,dp1[1000001];
int num[10];
int a[1000000];
signed main()
{
    fastio();
    int t=1;
    cin>>t;
    while(t--)
    {
      int n,k;
      cin>>n>>k;
      int a[n];
      read(a,n);
      int x=*max_element(a,a+n);
      int xx=*min_element(a,a+n);
      int c=0;
      int aa=(x+xx),a1=(x+xx)/2;
       if(abs(a1-x)<=k && abs(a1-xx)<=k)
       {
      for(int i=aa;i>=xx;i--)
      {
        //  cout<<i<<"\n";
          if(abs(x-i)<=k && (abs(xx-i)<=k))
          {
              cout<<i<<"\n";
              c++;
              break;
          }
      }
       }
     // cout<<aa<<"\n";
      if(c==0)
      cout<<"-1\n";

    }
}
// 🍪 🍪 🍪


#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#pragma GCC optimize("Ofast")
//*********************************************DO IT NOW****************************************************************
#include<bits/stdc++.h>
#include<random>
#define int long long
#define pp pair<int,int>
#define ss second
#define ff first
#define pb push_back
#define mod 1000000007
#define pi 3.14159265359
#define mk(arr,n,type)  type *arr=new type[n];
#define sl s1.length();
#define yes cout<< "YES"<<endl
#define no  cout<< "NO"<<endl
#define all(v) (v).begin(),(v).end()
#define s(v)   sort(v,v+n)
#define  mt mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define read(a,n) for(int i=0;i<n;i++)cin>>a[i]
#define print(a,n) for(int i=0;i<n;i++)cout<<a[i]<<" "
#define for1(i,a,n)  for(int i=0;i<n;i++)
#define rep(i, c) for(int i = 0; i < (int)c; i++)
#define set(x) cout<<fixed<<setprecision(x);
#define cone(x) (__builtin_popcountll(x))        // zero indexed
#define fone(x) (__builtin_ffsll(x) -1LL)        //give wrong answer if x==0;(zero indexed)
#define mone(x) (63LL-__builtin_clzll(x))
using namespace std;
//**********************************************DO IT NOW****************************************************************
void fastio()
{
  #ifndef ONLINE_JUDGE
  freopen("INPUT.txt","r",stdin);
  freopen("OUTPUT.txt","w",stdout);
  #endif
  ios_base :: sync_with_stdio(false);
  cin.tie(NULL) ;
  cout.tie(NULL);
}
int dp[101][1001],ans1=1e9,dp1[1000001];
int num[10];
int a[1000000];
signed main()
{
    fastio();
    int t=1;
    cin>>t;
    while(t--)
    {
      int n,k;
      cin>>n>>k;
      int a[n];
      read(a,n);
      int x=*max_element(a,a+n);
      int xx=*min_element(a,a+n);
      int c=0;
      int aa=(x+xx),a1=(x+xx)/2;
      for(int i=1000000;i>=0;i--)
      {
        //  cout<<i<<"\n";
          if(abs(x-i)<=k && (abs(xx-i)<=k))
          {
              cout<<i<<"\n";
              c++;
              break;
          }
      }
     // cout<<aa<<"\n";
      if(c==0)
      cout<<"-1\n";

    }
}
// 🍪 🍪 🍪


#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#pragma GCC optimize("Ofast")
//*********************************************DO IT NOW****************************************************************
#include<bits/stdc++.h>
#include<random>
#define int long long
#define pp pair<int,int>
#define ss second
#define ff first
#define pb push_back
#define mod 1000000007
#define pi 3.14159265359
#define mk(arr,n,type)  type *arr=new type[n];
#define sl s1.length();
#define yes cout<< "YES"<<endl
#define no  cout<< "NO"<<endl
#define all(v) (v).begin(),(v).end()
#define s(v)   sort(v,v+n)
#define  mt mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define read(a,n) for(int i=0;i<n;i++)cin>>a[i]
#define print(a,n) for(int i=0;i<n;i++)cout<<a[i]<<" "
#define for1(i,a,n)  for(int i=0;i<n;i++)
#define rep(i, c) for(int i = 0; i < (int)c; i++)
#define set(x) cout<<fixed<<setprecision(x);
#define cone(x) (__builtin_popcountll(x))        // zero indexed
#define fone(x) (__builtin_ffsll(x) -1LL)        //give wrong answer if x==0;(zero indexed)
#define mone(x) (63LL-__builtin_clzll(x))
using namespace std;
//**********************************************DO IT NOW****************************************************************
void fastio()
{
  #ifndef ONLINE_JUDGE
  freopen("INPUT.txt","r",stdin);
  freopen("OUTPUT.txt","w",stdout);
  #endif
  ios_base :: sync_with_stdio(false);
  cin.tie(NULL) ;
  cout.tie(NULL);
}
int dp[101][1001],ans1=1e9,dp1[1000001];
int num[10];
int a[1000000];
signed main()
{
    fastio();
    int t=1;
    cin>>t;
    while(t--)
    {
      int n,k;
      cin>>n>>k;
      int a[n];
      read(a,n);
      int x=*max_element(a,a+n);
      int xx=*min_element(a,a+n);
      int x1=x-xx;
      if((x1+1)/2<=k)
       cout<<xx+k<<"\n";
       else
       cout<<"-1\n";
    }
}
// 🍪 🍪 🍪


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

int n, k;

bool valid(ll v, int *vet){
    for(int i = 0; i < n; i++){
        ll aux = abs(vet[i] - v);
        if(aux > k) return false;
    }
    return true;
}

int main(){
    int t; cin >> t;
    while(t--){
        cin >> n >> k;
        int vet[n];
        for(int i = 0; i < n; i++) cin >> vet[i];
        ll l = 0, r = 1e9;
        ll res = -1;
        while(l <= r){
            ll mid = l + (r - l) / 2;
            if(valid(mid, vet)){
                res = max(res, mid);
                l = mid + 1;
            }
            else r = mid - 1;
        }
        cout << res << endl;
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

ll n, k;
ll vet[105];

int valid(ll v){
    for(int i = 0; i < n; i++){
        ll aux = vet[i] - v;
        if(abs(aux) > k){
            if(aux > 0) return -1;
            return 0;
        }
    }
    return 1;
}

int main(){
    int t; cin >> t;
    while(t--){
        cin >> n >> k;
        for(int i = 0; i < n; i++) cin >> vet[i];
        ll l = 1, r = 1e9;
        ll res = -1;
        while(l <= r){
            ll mid = (r + l) / 2LL;
            int aux = valid(mid);
            if(aux == 1){
                res = max(res, mid);
                l = mid + 1;
            } else if(aux == -1){
                l = mid + 1;
            } else r = mid - 1;
        }
        cout << res << endl;
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

int n, k;
ll vet[105];

bool valid(ll v){
    for(int i = 0; i < n; i++){
        ll aux = abs(vet[i] - v);
        if(aux > k) return false;
    }
    return true;
}

int main(){
    int t; cin >> t;
    while(t--){
        cin >> n >> k;
        for(int i = 0; i < n; i++) cin >> vet[i];
        ll l = 0, r = 1e15;
        ll res = -1;
        while(l <= r){
            ll mid = (r + l) / 2;
            if(valid(mid)){
                res = max(res, mid);
                l = mid + 1;
            }
            else r = mid - 1;
        }
        cout << res << endl;
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

int n, k;

bool valid(ll v, int *vet){
    for(int i = 0; i < n; i++){
        ll aux = abs(vet[i] - v);
        if(aux > k) return false;
    }
    return true;
}

int main(){
    int t; cin >> t;
    while(t--){
        cin >> n >> k;
        int vet[n];
        for(int i = 0; i < n; i++) cin >> vet[i];
        ll l = 0, r = 1e9;
        ll res = -1;
        while(l <= r){
            ll mid = l + (r - l) / 2;
            if(valid(mid, vet)) res = mid, l = mid + 1;
            else r = mid - 1;
        }
        cout << res << endl;
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

int n, k;
ll vet[105];

bool valid(ll v){
    for(int i = 0; i < n; i++){
        ll aux = abs(vet[i] - v);
        if(aux > k) return false;
    }
    return true;
}

int main(){
    int t; cin >> t;
    while(t--){
        cin >> n >> k;
        for(int i = 0; i < n; i++) cin >> vet[i];
        ll l = 1, r = 1e9;
        ll res = -1;
        while(l <= r){
            ll mid = (r + l) / 2;
            if(valid(mid)){
                res = max(res, mid);
                l = mid + 1;
            }
            else r = mid - 1;
        }
        cout << res << endl;
    }
    return 0;
}