#include <bits/stdc++.h>
using namespace std;
int main() {
  int k, n, m, q;
  char s[200];
  scanf("%d%d%d%d", &k, &n, &m, &q);
  map<string, int> u[101];
  string lo[101], hi[101];
  map<string, map<string, int>> c;
  for (int i = 0; i < n; i++) {
    scanf("%s", s);
    lo[i] = s;
  }
  for (int i = 0; i < m; i++) {
    scanf("%s", s);
    s[strlen(s) - 1] = 0;
    hi[i] = s;
    do {
      scanf("%s", s);
      scanf("%d", &c[hi[i]][s]);
    } while (getchar() == ',');
  }
  for (int i = 0, o; i < q; i++) {
    scanf("%d%s", &o, s);
    u[o][s]++;
    for (auto& r : c) {
      int cnt = 9999999;
      for (auto& x : r.second) cnt = min(cnt, u[o][x.first] / x.second);
      u[o][r.first] += cnt;
      for (auto& x : r.second) u[o][x.first] -= x.second * cnt;
    }
  }
  for (int i = 1; i <= k; i++) {
    int ans = 0;
    for (auto& r : u[i])
      if (r.second) ans++;
    printf("%d\n", ans);
    for (auto& r : u[i])
      if (r.second) printf("%s %d\n", r.first.c_str(), r.second);
  }
}
