#include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>	// require sort next_permutation count __gcd reverse etc.
#include <cstdlib>	// require abs exit
#include <cstdio>	// require scanf printf
#include <functional>
#include <numeric>	// require accumulate
#include <cmath>
#include <climits>
#include <limits>
#include <cfloat>
#include <iomanip>	// require setw
#include <sstream>	// require stringstream 
#include <cstring>	// require memset
#include <cctype>	// require tolower, toupper
#include <fstream>	// require freopen
#include <ctime>
#define rep(i,n) for(int i=0;i<(n);i++)
#define ALL(A) A.begin(), A.end()
#define DEBUG 0
#define EPS 1e-8

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
string FILE_NAME = "testcase.B";
string NAME;
string itos (int n )
{
	stringstream ss;
	ss << n;

	return ss.str();
}

struct BOOK{
	string title;
	string auther;
	string date;
	ll datenum;
};

const int MAX_N = 105;
BOOK book[MAX_N];
		//	1,   2,   3,  4,   5,   6,  7,  8,   9, 10,  11, 12
const int days[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
const int leap[] = { 1972, 1976, 1980, 1984, 1988, 1992, 1996, 2000, 2004, 2008, 2012, 2016, 2020, 2024, 2028, 2032, 2036 };

// 1970/01/01 を1日目と勘定する経過日時
ll calc_date (string d ){

	rep (i, d.length() ) if (!isdigit(d[i] ) )  d[i] = ' ';
	stringstream ss(d );

	int year, month, date;
	ss >> year >> month >> date;

	ll res = 0LL;
	rep (i, sizeof(leap)/sizeof(leap[0] ) ){
		if (leap[i] < year ) res++;
		else
		if (leap[i] == year && month >= 3 ) res++;
	} 
	year -= 1970;
	month -= 1;
	res += (ll)365LL*year;
	rep (i, month ) res += (ll)days[i];
	res += (ll)date;

	return res;
}

void disp_book (BOOK b ){
	cerr << b.title << endl;
	cerr << b.auther << endl;
	cerr << b.date << endl;
	cerr << b.datenum << endl;
}

int main()
{
//	cut here before submit 
#if DEBUG
	NAME = FILE_NAME;
	int CNT = 1;
	NAME += itos (CNT );
	while (freopen (NAME.c_str() , "r", stdin ) != NULL ) {
#endif
	rep (i, MAX_N ) book[i].title = "", book[i].auther = "", book[i].date = "", book[i].datenum = 0LL;
	ios_base::sync_with_stdio(0);
	int n; cin >> n;
	rep (i, n ){
		cin >> book[i].title >> book[i].auther >> book[i].date;
		book[i].datenum = calc_date (book[i].date);
//		disp_book (book[i] );
	} // end rep

	int q; cin >> q;
	while (q-- ){
		string q_title = "", q_auther = "", q_date_from = "1970/01/01", q_date_to = "2037/12/31";
		string q_t, q_a, q_d_f, q_d_t;
		vector<BOOK> search[4];
		rep (k, 4 ) search[k].clear();

		cin >> q_t >> q_a >> q_d_f >> q_d_t;
		if (q_t != "*" )   q_title     = q_t;
		if (q_a != "*" )   q_auther    = q_a;
		if (q_d_f != "*" ) q_date_from = q_d_f;
		if (q_d_t != "*" ) q_date_to   = q_d_t;

		rep (i, n ) search[0].push_back (book[i] );

		if (!q_title.empty() ){
			rep (i, search[0].size() ){
				string curr = search[0][i].title;
				if (curr.find (q_title ) != string::npos ) search[1].push_back (search[0][i] );
			} // end rep
		}else{
			search[1] = search[0];
		} // end if


		if (!q_auther.empty() ){
			rep (i, search[1].size() ){
				string curr = search[1][i].auther;
				if (curr.find (q_auther ) != string::npos ) search[2].push_back (search[1][i] );
			} // end rep
		}else{
			search[2] = search[1];
		} // end if

		ll from = calc_date (q_date_from );
		vector<string> res; res.clear();
		rep (i, search[2].size() ){
			ll curr = search[2][i].datenum;
			if (from <= curr  ) search[3].push_back (search[2][i] );
		} // end rep

		ll to   = calc_date (q_date_to );
		rep (i, search[3].size() ){
			ll curr = search[3][i].datenum;
			if (curr <= to  ) res.push_back (search[3][i].title );
		} // end rep

		if (!res.empty() ){
			rep (i, res.size() ){
				cout << res[i] << endl;
			} // end rep
		} // end if
		if (q != 0 ) cout << endl;
	} // end while	
		
		
#if DEBUG
	CNT++;	// cut here before submit
	NAME = FILE_NAME;
	NAME += itos (CNT );
	} // end loop; cut here before submit
#endif			
	return 0;
}

Measured memory_bytes: 1280