#include <bits/stdc++.h>
using namespace std;
void task();
int main() {
  task();
  return 0;
}
const int N = (int)1e5 + 10;
const int MODULO = (int)1e9 + 3;
const int INF = (int)1e9;
const int BUF_SIZE = (int)1e5 + 10;
char buf[110];
int k, n, m, q;
unordered_map<string, int> ind_ga, ind_sa;
vector<string> ga, sa;
int need[55][55];
int frg[110][55], frs[110][55];
void task() {
  scanf("%d %d %d %d\n", &k, &n, &m, &q);
  ga.resize(n);
  for (int i = 0; i < n; ++i) {
    scanf("%s", buf);
    ind_ga[buf] = i;
    ga[i] = buf;
  }
  sa.resize(m);
  for (int i = 0; i < m; ++i) {
    scanf("%s ", buf);
    buf[strlen(buf) - 1] = '\0';
    ind_sa[buf] = i;
    sa[i] = buf;
    do {
      int cnt;
      scanf("%s %d", buf, &cnt);
      need[i][ind_ga[buf]] = cnt;
    } while (getchar() == ',');
  }
  while (q--) {
    int who;
    scanf("%d %s", &who, buf);
    (void(1));
    ++frg[who][ind_ga[buf]];
    for (int i = 0; i < m; ++i) {
      bool ok = 1;
      for (int j = 0; j < n; ++j) {
        if (frg[who][j] < need[i][j]) {
          ok = 0;
          break;
        }
      }
      if (ok) {
        (void(1));
        ++frs[who][ind_sa[sa[i]]];
        (void(1));
        for (int j = 0; j < n; ++j) {
          frg[who][j] -= need[i][j];
        }
        break;
      }
    }
  }
  for (int i = 1; i <= k; ++i) {
    map<string, int> ans;
    for (int j = 0; j < n; ++j)
      if (frg[i][ind_ga[ga[j]]] != 0) ans[ga[j]] = frg[i][ind_ga[ga[j]]];
    for (int j = 0; j < m; ++j) {
      if (frs[i][ind_sa[sa[j]]] != 0) ans[sa[j]] = frs[i][ind_sa[sa[j]]];
    }
    printf("%d\n", ans.size());
    for (auto x : ans) {
      printf("%s %d\n", x.first.c_str(), x.second);
    }
  }
}
