#include <bits/stdc++.h>

using namespace std;



int main(){

    int n,q;
    cin >> n;

    char book[110][2][60];
    int day[110][3];

    for(int i = 0;i < n;i++){
        cin >> book[i][0] >> book[i][1];
        scanf("%d/%d/%d",&day[i][0],&day[i][1],&day[i][2]);
    }

    bool ch[110][4];
    cin >> q;

    for(int ii = 0;ii < q;ii++){
        for(int i = 0;i < n;i++){
            for(int j = 0;j < 4;j++){
                ch[i][j] = false;
            }
        }
        char a[2][60];
        for(int k = 0;k < 2;k++){
            cin >> a[k];
       //     cout << a[k] << " " ;
            if(a[k][0] != '*'){
                int alen = strlen(a[k]);
                for(int i = 0;i < n;i++){
                //    cout << "no." << i  << " ";
                    for(int st = 0;st < strlen(book[i][k]);st++){
                        int j = 0;
                        for(j = 0;j < alen;j++){
                            if(st+j >= strlen(book[i][k])){
                         //       cout << "a" ;
                                j = -1;
                                break;
                            }
                            if(book[i][k][st+j] != a[k][j]){
                         //       cout << "b" << book[i][k][st+j] << ":" << a[k][j] << endl;

                                j = -1;
                                break;
                            }
                        }
                        if(j == alen){
                            ch[i][k] = true;
                            break;
                        }
                    }
                }
            }else{
           //     cout << "pou";
                for(int i = 0;i < n;i++){
                    ch[i][k] = true;
                }
            }
        }
        
        int day1[3];
        char tmp[60];
        cin >> tmp;
    //    scanf("%d/%d/%d",&day1[0],&day1[1],&day1[2]);
        bool flag;
        if(tmp[0] != '*'){
            day1[0] = 1000 * (tmp[0] - '0') + 100 * (tmp[1] - '0') + 10 * (tmp[2] - '0') + (tmp[3] - '0');
            day1[1] = 10 * (tmp[5] - '0') + (tmp[6] - '0');
            day1[2] = 10 * (tmp[8] - '0') + (tmp[9] - '0');
            for(int i = 0;i < n;i++){
                flag = true;
                for(int j = 0;j < 3;j++){
                    if(day[i][j] < day1[j]){
                        flag = false;
                        break;
                    }else if(day[i][j] > day1[j]){
                        break;
                    }
                }
                if(flag == true){
                    ch[i][2] = true;
                }
            }
        }else{
            for(int i = 0;i < n;i++){
                ch[i][2] = true;
            }
        }
        // もう一回
        cin >> tmp;
        if(tmp[0] != '*'){
            day1[0] = 1000 * (tmp[0] - '0') + 100 * (tmp[1] - '0') + 10 * (tmp[2] - '0') + (tmp[3] - '0');
            day1[1] = 10 * (tmp[5] - '0') + (tmp[6] - '0');
            day1[2] = 10 * (tmp[8] - '0') + (tmp[9] - '0');
            for(int i = 0;i < n;i++){
                flag = true;
                for(int j = 0;j < 3;j++){
                    if(day[i][j] > day1[j]){
                        flag = false;
                        break;
                    }else if(day[i][j] < day1[j]){
                        break;
                    }
                }
                if(flag == true){
                    ch[i][3] = true;
                }
            }
        }else{
            for(int i = 0;i < n;i++){
                ch[i][3] = true;
            }
        }
        for(int i = 0;i < n;i++){
            bool ans = true;
            for(int j = 0; j < 4;j++){
                if(ch[i][j] == false){
                    ans = false;
                    break;
                }
            }
            if(ans == true){
                cout << book[i][0] << endl;
            }
        }
        for(int i = 0;i < n;i++){
   //         cout << ch[i][0] << " " ;
        }
        if(ii != q-1) cout << endl;

    }


}


Measured memory_bytes: 3216