#include <bits/stdc++.h>
using namespace std;
map<string, int> fr[105];
pair<string, map<string, int> > art[55];
vector<pair<string, int> > v[105];
bool cmp(pair<string, int> a, pair<string, int> b) { return a.first < b.first; }
int main() {
  int n, k, m, q;
  cin >> k >> n >> m >> q;
  string s;
  for (int i = 0; i < n; ++i) cin >> s;
  for (int i = 0; i < m; ++i) {
    if (!i) getline(cin, s);
    getline(cin, s);
    int c = s.find(':');
    string s1;
    s1 = s.substr(0, c);
    art[i].first = s1;
    s.erase(0, c + 2);
    c = s.find(' ');
    while (c != -1) {
      s1 = s.substr(0, c);
      int t = 0;
      string s2;
      s.erase(0, c + 1);
      c = s.find(' ');
      s2 = s.substr(0, c);
      s.erase(0, c + 1);
      if (s2[s2.length() - 1] == ',') s2.erase(s2.length() - 1, 1);
      int step = 1;
      for (int tt = s2.length() - 1; tt >= 0; tt--) {
        t += int(s2[tt] - '0') * step;
        step *= 10;
      }
      art[i].second.insert(make_pair(s1, t));
      c = s.find(' ');
    }
  }
  for (int i = 0; i < q; ++i) {
    int t;
    cin >> t >> s;
    t--;
    if (fr[t].find(s) != fr[t].end())
      fr[t][s]++;
    else
      fr[t].insert(make_pair(s, 1));
    for (int a = 0; a < m; ++a) {
      map<string, int>::iterator it;
      bool fl = true;
      for (it = art[a].second.begin(); it != art[a].second.end(); it++) {
        if (fr[t].find(it->first) == fr[t].end()) {
          fl = false;
          break;
        } else if (fr[t][it->first] != it->second) {
          fl = false;
          break;
        }
      }
      if (fl) {
        for (it = fr[t].begin(); it != fr[t].end(); it++) {
          if (art[a].second.find(it->first) != art[a].second.end()) {
            it->second -= art[a].second[it->first];
          }
        }
        if (fr[t].find(art[a].first) != fr[t].end())
          fr[t][art[a].first]++;
        else
          fr[t].insert(make_pair(art[a].first, 1));
        break;
      }
    }
  }
  for (int i = 0; i < k; ++i) {
    map<string, int>::iterator it;
    for (it = fr[i].begin(); it != fr[i].end(); it++) {
      if (it->second != 0) {
        v[i].push_back(make_pair(it->first, it->second));
      }
    }
  }
  for (int i = 0; i < k; ++i) sort(v[i].begin(), v[i].end(), cmp);
  for (int i = 0; i < k; ++i) {
    cout << v[i].size() << endl;
    for (int j = 0; j < v[i].size(); j++)
      cout << v[i][j].first << ' ' << v[i][j].second << endl;
  }
  return 0;
}
