#include<iostream>
#include<string>
using namespace std;
struct SinhVien
{
	string msv;
	string name;
	string lop;
	string email;
};
void nhap(SinhVien& ds)
{
	getline(cin, ds.msv);
	getline(cin, ds.name);
	getline(cin, ds.lop);
	getline(cin, ds.email);
}
void find(SinhVien ds[], int n, string  find)
{
	if (find == "KE TOAN") find = "DCKT";
	if (find == "CONG NGHE THONG TIN") find = "DCCN";
	if (find == "AN TOAN THONG TIN") find = "DCAT";
	if (find == "VIEN THONG") find = "DCVT";
	if (find == "DIEN TU") find = "DCDT";
	for (int i = 0; i < n; i++)
	{
		string k = ds[i].msv.substr(3, 4);
		char key = ds[i].lop[0];
		bool logic = true;
		if (find == k)
		{
			if (find == "DCAT" || find == "DCCN" )
			{
				if (key == 'E')
				{
					logic = false;
				}
			}
			if(logic)
			cout << ds[i].msv << " " << ds[i].name << " " << ds[i].lop << " " << ds[i].email << endl;
		}
		
	}
}
void Upper(string& k)
{
	for (int i = 0; i < k.length(); i++)
	{
		k[i] = toupper(k[i]);
	}
}
int main()
{
	SinhVien ds[1000];
	int n;
	cin >> n;
	cin.ignore();
	for (int i = 0; i < n; i++)
	{
		nhap(ds[i]);
	}
	int sl;
	string k;
	cin >> sl;
	cin.ignore();
	while (sl--)
	{
		getline(cin, k);
		string title = k;
		Upper(title);
		cout << "DANH SACH SINH VIEN NGHANH " << title << ":" << endl;
		find(ds, n, title);
	}
	return 0;
}
	