#include <bits/stdc++.h>
using namespace std;
struct pt {
  string name;
  int val;
};
int ase[110][110];
string base_name[110], com_name[110];
map<string, int> mp_base;
map<string, int> mp_com;
map<string, int> ans[110];
int com[110][110];
char in[1010000];
void ini() {
  memset(com, 0, sizeof(com));
  mp_base.clear();
  mp_com.clear();
  int i;
  for (i = 0; i < 102; i++) {
    ans[i].clear();
  }
  memset(ase, 0, sizeof(ase));
}
int cmp(pt a, pt b) { return a.name < b.name; }
int main() {
  int n, m, x, k, q, i, person, item_no, j, l;
  string s1, s2;
  while (scanf("%d %d %d %d", &k, &n, &m, &q) == 4) {
    ini();
    for (i = 1; i <= n; i++) {
      cin >> base_name[i];
      mp_base[base_name[i]] = i;
    }
    gets(in);
    for (i = 1; i <= m; i++) {
      gets(in);
      j = 0;
      while (in[j]) {
        if (in[j] == ',') in[j] = ' ';
        if (in[j] == ':') in[j] = ' ';
        j++;
      }
      istringstream iss(in);
      iss >> s1;
      com_name[i] = s1;
      mp_com[s1] = i;
      while (iss >> s1) {
        iss >> x;
        com[i][mp_base[s1]] = x;
      }
    }
    for (i = 0; i < q; i++) {
      cin >> person >> s1;
      item_no = mp_base[s1];
      ase[person][item_no]++;
      for (j = 1; j <= m; j++) {
        for (l = 1; l <= n; l++) {
          if (ase[person][l] < com[j][l]) break;
        }
        if (l == (n + 1)) {
          for (l = 1; l <= n; l++) {
            ase[person][l] -= com[j][l];
          }
          ans[person][com_name[j]]++;
          break;
        }
      }
    }
    for (i = 1; i <= k; i++) {
      int total = 0;
      vector<pt> v;
      pt p;
      for (map<string, int>::iterator it = ans[i].begin(); it != ans[i].end();
           it++) {
        total++;
        p.name = it->first;
        p.val = it->second;
        v.push_back(p);
      }
      for (j = 1; j <= n; j++) {
        if (ase[i][j]) {
          total++;
          p.name = base_name[j];
          p.val = ase[i][j];
          v.push_back(p);
        }
      }
      sort(v.begin(), v.end(), cmp);
      printf("%d\n", total);
      for (j = 0; j < v.size(); j++) {
        cout << v[j].name << " " << v[j].val << endl;
      }
    }
  }
  return 0;
}
