#include<bits/stdc++.h>
using namespace std;
struct Res{
vector<string> op; //操作
vector<string> res_type;//资源种类
vector<string> res_name;//资源名称描述
};
map<string, vector<string> >group2role;
map<string, vector<string> >user2role;
map<string, Res >roles;
//判断role 能否提供操作类型op,资源种类tp,资源名称name的权限
bool check(string op,string tp,string name,string role) {
Res res=roles[role];
bool ok=false;
for(auto o:res.op) {
if(o=="*"||o==op) {ok=true;break;}
}
if(ok==false) return false;
ok=false;
for(auto t:res.res_type) {
if(t=="*"||t==tp) {ok=true;break;}
}
if(ok==false) return false;
ok=false;
if(res.res_name.size()==0) return true;
for(auto na:res.res_name) {
if(na==name) {ok=true;break;}
}
return ok;
}
int main() {
int n,m,q;
cin>>n>>m>>q;
for(int i=0;i<n;i++) {
string str,tmp;
Res r;
cin>>str;
int nv,no,nn;
cin>>nv;
for(int j=0;j<nv;j++) {
cin>>tmp;
r.op.push_back(tmp);
}
cin>>no;
for(int j=0;j<no;j++) {
cin>>tmp;
r.res_type.push_back(tmp);
}
cin>>nn;
for(int j=0;j<nn;j++) {
cin>>tmp;
r.res_name.push_back(tmp);
}
roles[str]=r;
}
for(int i=0;i<m;i++) {
string str,tmp,tp;
int ns;
cin>>str>>ns;
for(int j=0;j<ns;j++) {
cin>>tp>>tmp;
if(tp=="g") {
group2role[tmp].push_back(str);
} else {
user2role[tmp].push_back(str);
}
}
}
for(int i=0;i<q;i++) {
vector<string>groups;
string user;
int ng;
bool ok=false;
cin>>user>>ng;
for(int j=0;j<ng;j++) {
string tmp;
cin>>tmp;
groups.push_back(tmp);
}
string op,tp,name;
cin>>op>>tp>>name;
// 遍历关联user的所有角色
for(auto role:user2role[user]) {
if(check(op,tp,name,role)) {ok=true;break;}
}
for(auto group:groups) {
for(auto role:group2role[group]) {
if(check(op,tp,name,role)) {ok=true;break;}
}
}
if(ok) cout<<1<<endl;
else cout<<0<<endl;
}
return 0;
}