#include <bits/stdc++.h>
using namespace std;
map<string, int> mp;
map<string, int>::iterator it;
int coll[100][100];
int req[100][100];
char s[120];
char comp[100][120];
int main() {
  int n, m, i, j, p, k, q, h, g;
  scanf("%d %d %d %d\n", &k, &n, &m, &q);
  for (i = (0); i < (n); i++) {
    gets(s);
    strcpy(comp[i], s);
    mp[s] = i;
  }
  for (i = (0); i < (m); i++) {
    scanf("%s:", s);
    s[strlen(s) - 1] = '\0';
    mp[s] = i + 50;
    strcpy(comp[i + 50], s);
    do {
      scanf("%s", s);
      j = mp[s];
      scanf("%d", &req[i][j]);
    } while (getchar() == ',');
  }
  for (i = (0); i < (q); i++) {
    scanf("%d %s", &p, &s);
    j = mp[s];
    p--;
    coll[p][j]++;
    for (g = (0); g < (m); g++) {
      for (h = (0); h < (n); h++) {
        if (coll[p][h] < req[g][h]) break;
      }
      if (h == n) {
        for (h = (0); h < (n); h++) coll[p][h] -= req[g][h];
        coll[p][50 + g]++;
      }
    }
  }
  for (i = (0); i < (k); i++) {
    g = 0;
    for (it = mp.begin(); it != mp.end(); ++it) {
      j = it->second;
      if (coll[i][j]) g++;
    }
    printf("%d\n", g);
    for (it = mp.begin(); it != mp.end(); ++it) {
      j = it->second;
      if (coll[i][j]) printf("%s %d\n", comp[j], coll[i][j]);
    }
  }
  return 0;
}
