#include <bits/stdc++.h>
using namespace std;
int main() {
  int k, n, m, q;
  cin >> k >> n >> m >> q;
  vector<map<string, int> > base(k);
  vector<map<string, int> > compaund(k);
  vector<string> compaundName(m);
  vector<vector<pair<string, int> > > base2Compaund(m);
  for (int i = (0); i < ((n)); ++i) {
    string b;
    cin >> b;
  }
  for (int i = (0); i < ((m)); ++i) {
    string comp;
    cin >> comp;
    comp.resize((int)comp.size() - 1);
    compaundName[i] = comp;
    string line;
    getline(cin, line);
    replace(line.begin(), line.end(), ',', ' ');
    istringstream iss(line);
    string b;
    int c;
    while (iss >> b >> c) {
      base2Compaund[i].push_back(make_pair(b, c));
    }
    sort(base2Compaund[i].begin(), base2Compaund[i].end());
  }
  for (int i = (0); i < ((q)); ++i) {
    int who;
    string what;
    cin >> who >> what;
    --who;
    ++base[who][what];
    for (int j = (0); j < ((m)); ++j) {
      bool ok = true;
      for (int k = (0); k < (((int)base2Compaund[j].size())); ++k) {
        string const& target = base2Compaund[j][k].first;
        int count = base2Compaund[j][k].second;
        ok &= base[who][target] >= count;
      }
      if (ok) {
        ++compaund[who][compaundName[j]];
        for (int k = (0); k < (((int)base2Compaund[j].size())); ++k) {
          string const& target = base2Compaund[j][k].first;
          int count = base2Compaund[j][k].second;
          base[who][target] -= count;
        }
        break;
      }
    }
  }
  for (int i = (0); i < ((k)); ++i) {
    vector<pair<string, int> > total;
    {
      map<string, int>::const_iterator it = base[i].begin();
      for (; it != base[i].end(); ++it)
        if (it->second) total.push_back(*it);
    }
    {
      map<string, int>::const_iterator it = compaund[i].begin();
      for (; it != compaund[i].end(); ++it)
        if (it->second) total.push_back(*it);
    }
    sort(total.begin(), total.end());
    cout << (int)total.size() << endl;
    for (int j = (0); j < (((int)total.size())); ++j)
      cout << total[j].first << " " << total[j].second << endl;
  }
  return 0;
}
