#include <bits/stdc++.h>
using namespace std;
int main() {
  char str[1000];
  int k, n, m, q;
  char c;
  cin >> k >> n >> m >> q;
  string s;
  vector<string> v, v1, vvv;
  getline(cin, s);
  for (int i = 0; i < n; i++) {
    scanf("%s", str);
    s = string(str);
    v1.push_back(s);
    vvv.push_back(s);
  }
  getline(cin, s);
  map<string, vector<pair<string, int> > > mp;
  for (int i = 0; i < m; i++) {
    getline(cin, s);
    string s1 = "";
    int x = 0;
    while (s[x] != ':') {
      s1 += s[x];
      x++;
    }
    v.push_back(s1);
    vvv.push_back(s1);
    x++;
    x++;
    bool fl = true;
    while (x < s.length() && fl) {
      string s2 = "";
      while (s[x] != ' ') {
        s2 += s[x];
        x++;
      }
      int pp = 0;
      x++;
      while (s[x] != ',') {
        pp = pp * 10 + (s[x] - '0');
        x++;
        if (x == s.length()) {
          s[x] = ',';
          fl = false;
        }
      }
      x++;
      x++;
      mp[s1].push_back(make_pair(s2, pp));
    }
  }
  map<string, int> mmm[k + 1];
  for (int p = 0; p < q; p++) {
    int num;
    cin >> num;
    string s;
    scanf("%s", str);
    s = string(str);
    mmm[num][s]++;
    bool ff = true;
    for (int j = 0; j < m && ff; j++) {
      bool flag = true;
      for (int k = 0; k < mp[v[j]].size() && flag; k++)
        if (mmm[num][mp[v[j]][k].first] < mp[v[j]][k].second) flag = false;
      if (flag) {
        ff = false;
        mmm[num][v[j]]++;
        for (int k = 0; k < mp[v[j]].size() && flag; k++)
          mmm[num][mp[v[j]][k].first] -= mp[v[j]][k].second;
      }
    }
  }
  sort(vvv.begin(), vvv.end());
  vector<pair<string, int> > rrr;
  for (int i = 1; i <= k; i++) {
    rrr.clear();
    for (int j = 0; j < vvv.size(); j++)
      if (mmm[i][vvv[j]] > 0) rrr.push_back(make_pair(vvv[j], mmm[i][vvv[j]]));
    cout << rrr.size() << endl;
    for (int j = 0; j < rrr.size(); j++)
      cout << rrr[j].first << " " << rrr[j].second << endl;
  }
  return 0;
}
