#include <bits/stdc++.h>
using namespace std;
vector<map<int, int> > rec;
vector<map<int, int> > fr;
bool doArt(int frId, int artId) {
  map<int, int>& have = fr[frId];
  map<int, int>& need = rec[artId];
  bool can = true;
  for (map<int, int>::iterator i = need.begin(); i != need.end(); ++i)
    if (have.count(i->first) == 0 || have[i->first] < i->second) {
      can = false;
      break;
    }
  if (can) {
    for (map<int, int>::iterator i = need.begin(); i != need.end(); ++i) {
      have[i->first] -= i->second;
      if (have[i->first] <= 0) have.erase(i->first);
    }
    have[artId]++;
  }
  return can;
}
char buf[302];
int main() {
  for (string line; getline(cin, line);) {
    istringstream iss(line);
    int k, n, m, q;
    if (!(iss >> k >> n >> m >> q)) break;
    vector<string> names;
    map<string, int> ids;
    rec.assign(n + m, map<int, int>());
    fr.assign(k, map<int, int>());
    for (int i = 0; i < (n); ++i) {
      getline(cin, line);
      istringstream iss(line);
      iss >> line;
      names.push_back(line);
      ids[names.back()] = ((int)(names).size()) - 1;
    }
    for (int i = 0; i < (m); ++i) {
      getline(cin, line);
      istringstream iss(line);
      string name;
      iss >> name;
      assert(((int)(name).size()) > 1 && name[((int)(name).size()) - 1] == ':');
      name.resize(((int)(name).size()) - 1);
      names.push_back(name);
      int curId = ids[name] = ((int)(names).size()) - 1;
      for (int cnt; iss >> name >> cnt;) {
        rec[curId][ids[name]] += cnt;
        iss >> name;
      }
    }
    for (int u = 0; u < (q); ++u) {
      getline(cin, line);
      istringstream iss(line);
      int frId;
      string name;
      iss >> frId >> name;
      frId--;
      fr[frId][ids[name]] += 1;
      for (int i = (n); i < (n + m); ++i)
        if (doArt(frId, i)) break;
    }
    for (int u = 0; u < (k); ++u) {
      printf("%d\n", ((int)(fr[u]).size()));
      vector<pair<string, int> > ans;
      for (map<int, int>::iterator i = fr[u].begin(); i != fr[u].end(); ++i) {
        assert(i->second > 0);
        ans.push_back(make_pair(names[i->first], i->second));
      }
      sort((ans).begin(), (ans).end());
      for (int i = 0; i < (((int)(ans).size())); ++i)
        printf("%s %d\n", ans[i].first.c_str(), ans[i].second);
    }
  }
  return 0;
}
