#include <bits/stdc++.h>
using namespace std;
const int INF = (int)2e9;
int main() {
  int k, n, m, q;
  cin >> k >> n >> m >> q;
  map<string, map<string, int>> rec;
  vector<string> art(n);
  map<int, map<string, int>> b;
  for (int(i) = (int)(0); (i) < (int)n; ++(i)) cin >> art[i];
  sort(art.begin(), art.end());
  string s;
  getline(cin, s);
  for (int(i) = (int)(0); (i) < (int)m; ++(i)) {
    getline(cin, s);
    int p = s.find(':');
    string ar = s.substr(0, p);
    s = s.substr(p + 2, s.size());
    s += ", ";
    while (s != "") {
      p = s.find(' ');
      string t = s.substr(0, p);
      s = s.substr(p + 1, s.size());
      p = s.find(',');
      string t2 = s.substr(0, p);
      s = s.substr(p + 2, s.size());
      int col = atoi(t2.c_str());
      rec[ar][t] = col;
    }
  }
  for (int(i) = (int)(0); (i) < (int)q; ++(i)) {
    int t;
    cin >> t;
    string st;
    cin >> st;
    b[t][st]++;
    for (map<string, map<string, int>>::iterator it1 = rec.begin();
         it1 != rec.end(); ++it1) {
      bool ok = true;
      for (map<string, int>::iterator it2 = it1->second.begin();
           it2 != it1->second.end(); ++it2) {
        if (b[t].find(it2->first) == b[t].end() ||
            b[t][it2->first] < it2->second) {
          ok = false;
          break;
        }
      }
      if (ok) {
        for (map<string, int>::iterator it2 = it1->second.begin();
             it2 != it1->second.end(); ++it2) {
          b[t][it2->first] -= it2->second;
          if (b[t][it2->first] == 0) b[t].erase(it2->first);
        }
        b[t][it1->first]++;
        break;
      }
    }
  }
  for (int(i) = (int)(1); (i) < (int)k + 1; ++(i)) {
    cout << b[i].size() << '\n';
    for (map<string, int>::iterator it = b[i].begin(); it != b[i].end(); ++it)
      if (it->second) cout << it->first << ' ' << it->second << '\n';
  }
  return 0;
}
