#include <bits/stdc++.h>
using namespace std;
char s[51][105];
char s2[51][105];
char ch[6000];
char ch2[105];
int cost[51][51];
int has[101][51];
int ans[101][51];
struct node {
  int count;
  char st[105];
} N[101];
bool cmp(const node& a, const node& b) { return strcmp(a.st, b.st) < 0; }
int main() {
  int k, n, m, q;
  scanf("%d%d%d%d", &k, &n, &m, &q);
  for (int i = 1; i <= n; i++) {
    scanf("%s", s[i]);
  }
  getchar();
  memset(has, 0, sizeof(has));
  memset(cost, 0, sizeof(cost));
  memset(ans, 0, sizeof(ans));
  for (int i = 1; i <= m; i++) {
    gets(ch);
    int id;
    for (int j = 0; ch[j] != '\0'; j++) {
      if (ch[j] == ':') {
        id = j;
        break;
      }
    }
    for (int j = 0; j < id; j++) {
      s2[i][j] = ch[j];
    }
    s2[i][id] = '\0';
    int cnt = -1;
    for (int j = id + 2; ch[j] != '\0'; j++) {
      if (ch[j] == ' ') {
        ch2[++cnt] = '\0';
        cnt = -1;
        int idd;
        for (int f = 1; f <= n; f++) {
          if (strcmp(s[f], ch2) == 0) {
            idd = f;
            break;
          }
        }
        int index = j + 1;
        int num = 0;
        while (ch[index] != ',' && ch[index] != '\0') {
          num = num * 10 + ch[index] - '0';
          index++;
        }
        cost[i][idd] = num;
        if (ch[index] == '\0')
          break;
        else
          j = index + 1;
      } else {
        ch2[++cnt] = ch[j];
      }
    }
  }
  int a;
  for (int i = 1; i <= q; i++) {
    scanf("%d%s", &a, ch);
    for (int j = 1; j <= n; j++) {
      if (strcmp(s[j], ch) == 0) {
        has[a][j]++;
        break;
      }
    }
    for (int f = 1; f <= m; f++) {
      bool flag = true;
      for (int g = 1; g <= n; g++) {
        if (has[a][g] < cost[f][g]) {
          flag = false;
          break;
        }
      }
      if (flag) {
        for (int g = 1; g <= n; g++) {
          has[a][g] -= cost[f][g];
        }
        ans[a][f]++;
        break;
      }
    }
  }
  for (int i = 1; i <= k; i++) {
    int t = 0;
    for (int j = 1; j <= m; j++) {
      if (ans[i][j] > 0) {
        N[++t].count = ans[i][j];
        strcpy(N[t].st, s2[j]);
      }
    }
    for (int j = 1; j <= n; j++) {
      if (has[i][j] > 0) {
        N[++t].count = has[i][j];
        strcpy(N[t].st, s[j]);
      }
    }
    sort(N + 1, N + t + 1, cmp);
    printf("%d\n", t);
    for (int kk = 1; kk <= t; kk++) {
      printf("%s %d\n", N[kk].st, N[kk].count);
    }
  }
}
