#include <bits/stdc++.h>
using namespace std;
char ba[52][102], ca[52][102], s[102];
int btc[52][52], co[102][52], r[102][52], M, n, m;
struct air {
  char na[102];
  int cao;
  bool operator<(const air& p) const { return strcmp(na, p.na) < 0; }
};
struct node {
  air A[102];
  int c;
} peo[102];
int J() {
  int i;
  for (i = 0; i < n; i++)
    if (strcmp(ba[i], s) == 0) return i;
}
int C(int a) {
  int i, j;
  for (i = 0; i < m; i++) {
    M = 0x3f3f3f3f;
    for (j = 0; j < n; j++) {
      if (!btc[i][j]) continue;
      if (btc[i][j] > co[a][j]) break;
      if (co[a][j] / btc[i][j] < M) M = co[a][j] / btc[i][j];
    }
    if (j == n) return i;
  }
  return -1;
}
int main() {
  int k, q, i, j, l, a, b;
  while (~scanf("%d%d%d%d", &k, &n, &m, &q)) {
    for (i = 0; i < n; i++) scanf("%s", ba[i]);
    getchar();
    memset(btc, 0, sizeof(btc));
    for (i = 0; i < m; i++) {
      bool f = 1;
      while (1) {
        if (f) {
          scanf("%s", s);
          l = strlen(s), s[l - 1] = '\0', strcpy(ca[i], s);
        } else {
          scanf("%s %d", s, &a);
          btc[i][J()] += a;
          if (getchar() == '\n') break;
        }
        f = 0;
      }
    }
    memset(co, 0, sizeof(co));
    memset(r, 0, sizeof(r));
    while (q--) {
      scanf("%d%s", &a, s);
      l = J();
      co[a][l]++;
      b = C(a);
      if (b != -1) {
        r[a][b] += M;
        for (i = 0; i < n; i++) co[a][i] -= M * btc[b][i];
      }
    }
    for (i = 1; i <= k; i++) {
      int t1 = 0;
      for (j = 0; j < n; j++)
        if (co[i][j])
          peo[i].A[t1].cao = co[i][j], strcpy(peo[i].A[t1].na, ba[j]), t1++;
      for (j = 0; j < m; j++)
        if (r[i][j])
          peo[i].A[t1].cao = r[i][j], strcpy(peo[i].A[t1].na, ca[j]), t1++;
      peo[i].c = t1;
      sort(peo[i].A, peo[i].A + t1);
    }
    for (i = 1; i <= k; i++) {
      printf("%d\n", peo[i].c);
      for (j = 0; j < peo[i].c; j++)
        printf("%s %d\n", peo[i].A[j].na, peo[i].A[j].cao);
    }
  }
  return 0;
}
