#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:64000000")
using namespace std;
map<string, int> fr[110];
map<string, vector<pair<string, int> > > help;
string base[100];
string s2[110];
int main() {
  int k, n, m, q;
  cin >> k >> n >> m >> q;
  scanf("\n");
  string s;
  for (int i = 0; i < n; i++) getline(cin, base[i]);
  for (int i = 0; i < m; i++) {
    vector<pair<string, int> > helper;
    char c;
    scanf("%c", &c);
    s.clear();
    while (c != ':') {
      s += c;
      scanf("%c", &c);
    }
    s2[i] = s;
    scanf("%c", &c);
    while (true) {
      scanf("%c", &c);
      string s1;
      int t1;
      while (c != ' ') {
        s1 += c;
        scanf("%c", &c);
      }
      scanf("%d", &t1);
      helper.push_back(make_pair(s1, t1));
      scanf("%c", &c);
      if (c != ',') break;
      scanf("%c", &c);
    }
    help.insert(make_pair(s, helper));
  }
  for (int i = 0; i < k; i++)
    for (int j = 0; j < n; j++) fr[i].insert(make_pair(base[j], 0));
  for (int i = 0; i < q; i++) {
    int t1;
    scanf("%d ", &t1);
    string s1;
    getline(cin, s1);
    t1--;
    fr[t1][s1]++;
    for (int j = 0; j < m; j++) {
      bool q1 = true;
      vector<pair<string, int> > help1 = help[s2[j]];
      for (int z = 0; z < help1.size(); z++)
        if (fr[t1][help1[z].first] < help1[z].second) {
          q1 = false;
          break;
        }
      if (q1) {
        for (int z = 0; z < help1.size(); z++)
          fr[t1][help1[z].first] -= help1[z].second;
        if (fr[t1].find(s2[j]) == fr[t1].end())
          fr[t1].insert(make_pair(s2[j], 1));
        else
          fr[t1][s2[j]]++;
        break;
      }
    }
  }
  for (int i = 0; i < k; i++) {
    map<string, int>::iterator it = fr[i].begin();
    int kol = 0;
    for (; it != fr[i].end(); it++)
      if (it->second > 0) kol++;
    it = fr[i].begin();
    printf("%d\n", kol);
    for (; it != fr[i].end(); it++)
      if (it->second > 0) cout << it->first << ' ' << it->second << endl;
  }
  return 0;
}
