#include <bits/stdc++.h>
using namespace std;
int main() {
  int k, n, m, q;
  while (cin >> k >> n >> m >> q) {
    map<string, int> mop[200];
    map<string, int>::iterator it1;
    vector<string> basic;
    map<string, vector<pair<string, int> > > comp;
    map<string, vector<pair<string, int> > >::iterator it;
    for (int i = 0; i < n; i++) {
      string s;
      cin >> s;
      basic.push_back(s);
    }
    cin.ignore();
    for (int i = 0; i < m; i++) {
      string s, head, tail;
      vector<pair<string, int> > vec;
      int var = 0;
      getline(cin, s);
      int tmp = (int)s.find(':');
      head = s.substr(0, tmp);
      for (int j = tmp + 2; j < s.size(); j++) {
        if (s[j] != ',' && s[j] != ' ' && !isdigit(s[j])) {
          tail += s[j];
        }
        if (isdigit(s[j])) {
          var = var * 10 + s[j] - '0';
        }
        if (s[j] == ',' || j == s.size() - 1) {
          vec.push_back({tail, var});
          tail = "";
          var = 0;
        }
      }
      comp[head] = vec;
    }
    for (int i = 0; i < q; i++) {
      int f;
      string art;
      map<string, int> tmpmop;
      cin >> f >> art;
      mop[f][art]++;
      for (it = comp.begin(); it != comp.end(); it++) {
        int me = 0;
        tmpmop = mop[f];
        for (int j = 0; j < it->second.size(); j++) {
          if (mop[f][it->second[j].first] >= it->second[j].second) {
            mop[f][it->second[j].first] -= it->second[j].second;
            me++;
          } else
            break;
        }
        if (me != it->second.size())
          mop[f] = tmpmop;
        else
          mop[f][it->first]++;
      }
    }
    for (int i = 1; i <= k; i++) {
      vector<pair<string, int> > vec;
      for (it1 = mop[i].begin(); it1 != mop[i].end(); it1++) {
        if (it1->second > 0) vec.push_back({it1->first, it1->second});
      }
      cout << vec.size() << endl;
      for (int j = 0; j < vec.size(); j++)
        cout << vec[j].first << " " << vec[j].second << endl;
    }
  }
}
