#include <iostream>
#include <string>
#include <algorithm>
#include <functional>
#include <vector>
#include <utility>
#include <cstring>
#include <iomanip>
#include <numeric>
#include <cmath>
#include <cassert>
#include<cstdio>
#define repi(i,x,n) for(int i = x; i < n; i++)
#define rep(i,n) repi(i,0,n)
#define int long long
using namespace std;
const int INF = 1<<30;
const int MOD = 1e9 + 7;

struct info{
    string title, author;
    pair<int,pair<int,int>> from, to;
    
};
bool check(string a, string b){
    for(int i = 0; i <(int)b.size() - (int)a.size()+1; i++){
        bool flag = true;
        // cout<<"i = "<<i<<" "<<b[i]<<"  {}{} "<<b.size() - a.size() +1<<endl;
        for(int j = 0; j < a.size(); j++){
            if(b[i+j] != a[j]){
                // cout<<i<<" "<<j<<" "<<b[i+j]<<" "<<a[j]<<endl;
                flag = false;
                break;
            }
        }
        if(flag) return true;
    }

    return false;
}

signed main()
{
    // cin.tie(0);
    //ios::sync_with_stdio(false);

    int N, Q;

    vector<info> in;

    cin>>N;

    in.resize(N);

    for(int i = 0; i < N; i++){
        string a;
        cin>>in[i].title>>in[i].author>>a;
        // if(a != "*"){
            in[i].to.first = stoi(a.substr(0,4));
            in[i].to.second.first =  stoi(a.substr(5,2));
            in[i].to.second.second =  stoi(a.substr(8,2));
            // } else in[i].from.first = -INF;
            //if(b != "*"){
            //cout<<in[i].from.first<<" "<<in[i].from.second.first<<" "<<in[i].from.second.second<<endl;
            //in[i].to.first = stoi(b.substr(0,4));
            //in[i].to.second.first =  stoi(b.substr(5,2));
            //in[i].to.second.second =  stoi(b.substr(8,2));
            // } else in[i].to.first = INF;
        //scanf("%ld %ld %ld", &in[i].from.first, &in[i].from.second.first, &in[i].from.second.second);
        //scanf("%ld/%ld/%ld", &in[i].from.first, &in[i].from.second.first, &in[i].from.second.second);
    }

    cin>>Q;

    for(int i = 0; i < Q; i++){
        info q;
        string a, b;
        //cout<<"i = "<<i<<endl;
        cin>>q.title>>q.author>>a>>b;
        
         if(a != "*"){
            q.from.first = stoi(a.substr(0,4));
            q.from.second.first =  stoi(a.substr(5,2));
            q.from.second.second =  stoi(a.substr(8,2));
            
        } else q.from.first = -INF;
         
        if(b != "*"){
            q.to.first = stoi(b.substr(0,4));
            q.to.second.first =  stoi(b.substr(5,2));
            q.to.second.second =  stoi(b.substr(8,2));
        } else q.to.first = INF;
        
        for(int j = 0; j < N; j++){
            //  cout<<"j = "<<j<<endl;
            if(q.title != "*" && !check(q.title,in[j].title)) continue;
            // cout<<"w"<<" "<<q.title<<" "<<in[j].title<<endl;

            if(q.author != "*" && !check(q.author,in[j].author)) continue;
            

            // cout<<"x "<<q.author<<" "<<in[j].author<<endl;
            //if(q.from > in[j].to) continue;
            // cout<<"Y"<<endl;
            //if(q.to < in[j].to) continue;

            if(!(q.from <= in[j].to && in[j].to <= q.to)) continue;

            cout<<in[j].title<<endl;
        }
        if(i != Q-1) cout<<endl;
    }
    return 0;
}



Measured memory_bytes: 3212