#include <bits/stdc++.h>
using namespace std;
int n, p, q, m;
vector < int > c;
vector < vector < int > > b;
vector < int > a;
vector < int > f;
vector < vector < int > > r;
vector < vector < int > > ans;
int main() {
cin >> n >> p >> q;
c.resize(n);
b.resize(n);
for (int i = 0; i < n; ++i) {
cin >> c[i];
b[i].resize(p);
for (int j = 0; j < p; ++j) {
int cnt;
cin >> cnt;
for (int k = 0; k < cnt; ++k) {
int x;
cin >> x;
b[i][j] |= 1 << x;
}
}
}
cin >> m;
a.resize(m);
f.resize(m);
r.resize(m);
for (int i = 0; i < m; ++i) {
cin >> a[i] >> f[i];
r[i].resize(p);
for (int j = 0; j < p; ++j) {
int cnt;
cin >> cnt;
for (int k = 0; k < cnt; ++k) {
int x;
cin >> x;
r[i][j] |= 1 << x;
}
}
}
ans.resize(m, vector < int > (n));
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
bool ok = true;
for (int k = 0; k < p; ++k) {
if ((r[j][k] & b[i][k]) == 0) {
ok = false;
break;
}
}
if (ok) {
int cnt = min(c[i], a[j]);
if (f[j] != 0) {
cnt = min(cnt, f[j]);
}
ans[j][i] += cnt;
c[i] -= cnt;
a[j] -= cnt;
}
}
}
for (int i = 0; i < m; ++i) {
if (a[i] != 0) {
for (int j = 0; j < n; ++j) {
ans[i][j] = 0;
}
}
for (int j = 0; j < n; ++j) {
cout << ans[i][j] << ' ';
}
cout << endl;
}
return 0;
}