#include <bits/stdc++.h>
using namespace std;
template <typename T>
void read(T &x) {
  x = 0;
  char ch = getchar();
  int fh = 1;
  while (ch < '0' || ch > '9') {
    if (ch == '-') fh = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
  x *= fh;
}
template <typename T>
void write(T x) {
  if (x < 0) x = -x, putchar('-');
  if (x > 9) write(x / 10);
  putchar(x % 10 + '0');
}
template <typename T>
void writeln(T x) {
  write(x);
  puts("");
}
int k, n, m, q;
string bs[55];
map<string, int> bsk;
string cp[55];
map<string, int> cpk;
vector<pair<int, int> > cpn[55];
int cnt[105][55];
int ans[105][55];
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> k >> n >> m >> q;
  for (int i = 1; i <= n; i++) {
    cin >> bs[i];
    bsk[bs[i]] = i;
  }
  cin.get();
  for (int i = 1; i <= m; i++) {
    string getl;
    getline(cin, getl);
    stringstream ss;
    ss << getl;
    ss >> cp[i];
    cp[i].erase(cp[i].size() - 1, 1);
    string str;
    int num;
    while (ss >> str) {
      ss >> num;
      char tmp;
      ss >> tmp;
      cpn[i].push_back(make_pair(bsk[str], num));
    }
  }
  for (int i = 1; i <= q; i++) {
    int id;
    string itm;
    cin >> id >> itm;
    cnt[id][bsk[itm]]++;
    for (int j = 1; j <= m; j++) {
      bool flag = true;
      for (pair<int, int> p : cpn[j])
        if (cnt[id][p.first] < p.second) {
          flag = false;
          break;
        }
      if (flag) {
        ans[id][j]++;
        for (pair<int, int> p : cpn[j]) cnt[id][p.first] -= p.second;
      }
    }
  }
  for (int i = 1; i <= k; i++) {
    vector<pair<string, int> > res;
    for (int j = 1; j <= n; j++)
      if (cnt[i][j] > 0) res.push_back(make_pair(bs[j], cnt[i][j]));
    for (int j = 1; j <= m; j++)
      if (ans[i][j] > 0) res.push_back(make_pair(cp[j], ans[i][j]));
    sort(res.begin(), res.end());
    cout << res.size() << endl;
    for (pair<string, int> p : res) cout << p.first << ' ' << p.second << endl;
  }
  return 0;
}
