/**************************************************
filename :j.cpp
author :maksyuki
created time :2018/1/24 18:08:06
last modified :2018/1/24 21:50:52
file location :C:\Users\abcd\Desktop\TheEternalPoet
***************************************************/
#pragma comment(linker, "/STACK:102400000,102400000")
#include <set>
#include <bitset>
#include <list>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <string>
#include <vector>
#include <ios>
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <utility>
#include <complex>
#include <numeric>
#include <functional>
#include <cmath>
#include <ctime>
#include <climits>
#include <cstdarg>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cassert>
using namespace std;
#define CFF freopen ("in", "r", stdin)
#define CFO freopen ("out", "w", stdout)
#define CPPFF ifstream cin ("in")
#define CPPFO ofstream cout ("out")
#define DB(ccc) cout << #ccc << " = " << ccc << endl
#define DBT printf("time used: %.2lfs\n", (double) clock() / CLOCKS_PER_SEC)
#define PB push_back
#define MP(A, B) make_pair(A, B)
typedef long long LL;
typedef unsigned long long ULL;
typedef double DB;
typedef pair <int, int> PII;
typedef pair <int, bool> PIB;
const int INF = 0x7F7F7F7F;
const int MOD = 1e9 + 7;
const double EPS = 1e-10;
const double PI = 2 * acos (0.0);
const int maxn = 1e2 + 66;
struct node {
int va, vb;
node () {}
node (int aa, int bb): va(aa), vb(bb) {}
bool operator < (const node &a) const {
if(va != a.va) return va < a.va;
return vb < a.vb;
}
bool operator == (const node &a) const {
if(va == a.va && vb == a.vb) return true;
return false;
}
};
map<string, vector<node>> dict;
vector<string> word[maxn];
vector<node> ans;
bool is_find;
void output() {
int aalen = ans.size();
if(!aalen) is_find = false;
else {
sort(ans.begin(), ans.end());
//DB(aalen);
aalen = unique(ans.begin(), ans.end()) - ans.begin();
//DB(aalen);
bool is_first = true;
int vvv = -1;
for(int i = 0; i < aalen; i++) {
if(is_first) {
is_first = false;
vvv = ans[i].va;
}
else {
if(vvv != ans[i].va) {
vvv = ans[i].va;
cout << "----------" << endl;
}
}
cout << word[ans[i].va][ans[i].vb-1] << endl;
//DB(i);
}
}
}
int main()
{
#ifdef LOCAL
CFF;
CFO;
#endif
int n;
cin >> n;
string s;
stringstream ss;
getline(cin, s);
for(int i = 1; i <= n; i++) {
int row = 0;
while(getline(cin, s)) {
if(s == "**********") break;
word[i].emplace_back(s);
row++;
for(int j = 0; s[j]; j++) {
if(!isalpha(s[j])) s[j] = ' ';
else s[j] = tolower(s[j]);
}
ss.clear();
ss << s;
while(ss >> s) dict[s].emplace_back(node(i, row));
}
}
int m;
cin >> m;
getline(cin, s);
for(int i = 1; i <= m; i++) {
getline(cin, s);
//DB(s);
is_find = true;
if(s.find("OR") != string::npos) {
ss.clear();
ss << s;
string sa, sb;
ss >> sa >> s >> sb;
for(int j = 0; sa[j]; j++) sa[j] = tolower(sa[j]);
for(int j = 0; sb[j]; j++) sb[j] = tolower(sb[j]);
ans.clear();
int alen = dict[sa].size(), blen = dict[sb].size();
for(int j = 0; j < alen; j++) ans.emplace_back(dict[sa][j]);
for(int j = 0; j < blen; j++) ans.emplace_back(dict[sb][j]);
output();
}
else if(s.find("AND") != string::npos) {
ss.clear();
ss << s;
string sa, sb;
ss >> sa >> s >> sb;
for(int j = 0; sa[j]; j++) sa[j] = tolower(sa[j]);
for(int j = 0; sb[j]; j++) sb[j] = tolower(sb[j]);
ans.clear();
int alen = dict[sa].size(), blen = dict[sb].size();
for(int j = 0; j < alen; j++) {
for(int k = 0; k < blen; k++)
if(dict[sa][j].va == dict[sb][k].va) {
ans.emplace_back(dict[sa][j]);
ans.emplace_back(dict[sb][k]);
}
}
output();
//DB("hello");
}
else if(s.find("NOT") != string::npos) {
ss.clear();
ss << s;
string sa;
ss >> s >> sa;
for(int j = 0; sa[j]; j++) sa[j] = tolower(sa[j]);
int cnt[maxn];
for(int j = 0; j < maxn; j++) cnt[j] = 1;
int alen = dict[sa].size();
for(int j = 0; j < alen; j++)
cnt[dict[sa][j].va] = 0;
bool is_output = false, is_first = true;
for(int j = 1; j <= n; j++) {
if(cnt[j]) {
is_output = true;
if(is_first) is_first = false;
else cout << "----------" << endl;
int wlen = word[j].size();
for(int k = 0; k < wlen; k++)
cout << word[j][k] << endl;
}
}
if(!is_output) is_find = false;
}
else {
for(int j = 0; s[j]; j++) s[j] = tolower(s[j]);
int len = dict[s].size();
if(!len) is_find = false;
else {
bool is_first = true;
int vvv = -1;
ans.clear();
for(int j = 0; j < len; j++) ans.emplace_back(dict[s][j]);
int alen = unique(ans.begin(), ans.end()) - ans.begin();
for(int j = 0; j < alen; j++) {
if(is_first) {
is_first = false;
vvv = ans[j].va;
}
else {
if(vvv != ans[j].va) {
vvv = ans[j].va;
cout << "----------" << endl;
}
}
cout << word[ans[j].va][ans[j].vb-1] << endl;
}
}
}
if(!is_find) cout << "Sorry, I found nothing." << endl;
cout << "==========" << endl;
}
return 0;
}