#include "bits/stdc++.h"
using namespace std;
const int N = 1e4 + 4;
const int M = 1e4 + 4;
const int MAX = 1 << 22;
int m1 , m2 , m3;
string arr1[N];
string arr2[N];
string arr3[N];
int str1[N];
int str2[N];
int str3[N];
vector < pair < int , int > > v[MAX];
set < pair < pair < int , int > , int > > used;
int n;
map < string , int > ma1;
map < string , int > ma2;
map < string , int > ma3;
int main(){
cin >> m1;
for(int i = 1 ; i <= m1 ; ++i){
cin >> arr1[i] >> str1[i];
ma1[arr1[i]] = i;
}
cin >> m2;
for(int i = 1 ; i <= m2 ; ++i){
cin >> arr2[i] >> str2[i];
ma2[arr2[i]] = i;
}
cin >> m3;
for(int i = 1 ; i <= m3 ; ++i){
cin >> arr3[i] >> str3[i];
ma3[arr3[i]] = i;
}
for(int i = 1 ; i <= m1 ; ++i){
for(int j = 1 ; j <= m2 ; ++j){
v[str1[i] + str2[j]].emplace_back(make_pair(i , j));
}
}
cin >> n;
while(n--){
string a , b , c;
cin >> a >> b >> c;
int i = ma1[a];
int j = ma2[b];
int k = ma3[c];
int strong = str1[i] + str2[j] + str3[k] + 1;
for(int i = 1 ; i <= m3 ; ++i){
if(str3[i] <= strong){
for(auto it : v[strong - str3[i]]){
pair < pair < int , int > , int > temp = {it , i};
if(used.find(temp) == used.end()){
used.insert(temp);
cout << arr1[it.first] << " " << arr2[it.second] << " " << arr3[i] << endl;
i = m3 + 1;
break;
}
}
}
}
}
}
I2luY2x1ZGUgImJpdHMvc3RkYysrLmgiCnVzaW5nIG5hbWVzcGFjZSBzdGQ7CmNvbnN0IGludCBOID0gMWU0ICsgNDsKY29uc3QgaW50IE0gPSAxZTQgKyA0Owpjb25zdCBpbnQgTUFYID0gMSA8PCAyMjsKaW50IG0xICwgbTIgLCBtMzsKc3RyaW5nIGFycjFbTl07CnN0cmluZyBhcnIyW05dOwpzdHJpbmcgYXJyM1tOXTsKaW50IHN0cjFbTl07CmludCBzdHIyW05dOwppbnQgc3RyM1tOXTsKdmVjdG9yIDwgcGFpciA8IGludCAsIGludCA+ID4gdltNQVhdOwpzZXQgPCBwYWlyIDwgcGFpciA8IGludCAsIGludCA+ICwgaW50ID4gPiB1c2VkOwppbnQgbjsKbWFwIDwgc3RyaW5nICwgaW50ID4gbWExOwptYXAgPCBzdHJpbmcgLCBpbnQgPiBtYTI7Cm1hcCA8IHN0cmluZyAsIGludCA+IG1hMzsKaW50IG1haW4oKXsKICAgIGNpbiA+PiBtMTsKICAgIGZvcihpbnQgaSA9IDEgOyBpIDw9IG0xIDsgKytpKXsKICAgICAgICBjaW4gPj4gYXJyMVtpXSA+PiBzdHIxW2ldOwogICAgICAgIG1hMVthcnIxW2ldXSA9IGk7CiAgICB9CiAgICBjaW4gPj4gbTI7CiAgICBmb3IoaW50IGkgPSAxIDsgaSA8PSBtMiA7ICsraSl7CiAgICAgICAgY2luID4+IGFycjJbaV0gPj4gc3RyMltpXTsKICAgICAgICBtYTJbYXJyMltpXV0gPSBpOwogICAgfQogICAgY2luID4+IG0zOwogICAgZm9yKGludCBpID0gMSA7IGkgPD0gbTMgOyArK2kpewogICAgICAgIGNpbiA+PiBhcnIzW2ldID4+IHN0cjNbaV07CiAgICAgICAgbWEzW2FycjNbaV1dID0gaTsKICAgIH0KICAgIGZvcihpbnQgaSA9IDEgOyBpIDw9IG0xIDsgKytpKXsKICAgICAgICBmb3IoaW50IGogPSAxIDsgaiA8PSBtMiA7ICsrail7CiAgICAgICAgICAgIHZbc3RyMVtpXSArIHN0cjJbal1dLmVtcGxhY2VfYmFjayhtYWtlX3BhaXIoaSAsIGopKTsKICAgICAgICB9CiAgICB9CiAgICBjaW4gPj4gbjsKICAgIHdoaWxlKG4tLSl7CiAgICAgICAgc3RyaW5nIGEgLCBiICwgYzsKICAgICAgICBjaW4gPj4gYSA+PiBiID4+IGM7CiAgICAgICAgaW50IGkgPSBtYTFbYV07CiAgICAgICAgaW50IGogPSBtYTJbYl07CiAgICAgICAgaW50IGsgPSBtYTNbY107CiAgICAgICAgaW50IHN0cm9uZyA9IHN0cjFbaV0gKyBzdHIyW2pdICsgc3RyM1trXSArIDE7CiAgICAgICAgZm9yKGludCBpID0gMSA7IGkgPD0gbTMgOyArK2kpewogICAgICAgICAgICBpZihzdHIzW2ldIDw9IHN0cm9uZyl7CiAgICAgICAgICAgICAgICBmb3IoYXV0byBpdCA6IHZbc3Ryb25nIC0gc3RyM1tpXV0pewogICAgICAgICAgICAgICAgICAgIHBhaXIgPCBwYWlyIDwgaW50ICwgaW50ID4gLCBpbnQgPiB0ZW1wID0ge2l0ICwgaX07CiAgICAgICAgICAgICAgICAgICAgaWYodXNlZC5maW5kKHRlbXApID09IHVzZWQuZW5kKCkpewogICAgICAgICAgICAgICAgICAgICAgICB1c2VkLmluc2VydCh0ZW1wKTsKICAgICAgICAgICAgICAgICAgICAgICAgY291dCA8PCBhcnIxW2l0LmZpcnN0XSA8PCAiICIgPDwgYXJyMltpdC5zZWNvbmRdIDw8ICIgIiA8PCBhcnIzW2ldIDw8IGVuZGw7CiAgICAgICAgICAgICAgICAgICAgICAgIGkgPSBtMyArIDE7CiAgICAgICAgICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgIH0KICAgIH0KfQ==