#include <bits/stdc++.h>
using namespace std;
const int MAXN = 101;
int ally[MAXN][MAXN], rel[MAXN][MAXN];
map<string, int> mp;
vector<string> v;
int mpn, compstart;
int StringToNum(string st) {
  int ret = 0;
  while (!st.empty()) ret = ret * 10 + st[0] - '0', st = st.substr(1);
  return ret;
}
void AddComposite(string st) {
  mp[st.substr(0, st.find(":"))] = mpn++;
  v.push_back(st.substr(0, st.find(":")));
  st = st.substr(st.find(" ") + 1);
  while (!st.empty()) {
    int pos = st.find(",");
    string sst;
    sst = st.substr(0, pos);
    if (pos == string::npos)
      st = "";
    else
      st = st.substr(pos + 2);
    rel[mpn - 1][mp[sst.substr(0, sst.find(" "))]] =
        StringToNum(sst.substr(sst.find(" ") + 1));
  }
  return;
}
void AddToAlly(int allynum, int itemnum) {
  ally[allynum][itemnum]++;
  for (int compi = compstart; compi < mpn; compi++) {
    bool okay = true;
    for (int i = 0; i < compstart; i++)
      if (ally[allynum][i] < rel[compi][i]) {
        okay = false;
        break;
      }
    if (okay) {
      for (int i = 0; i < compstart; i++) ally[allynum][i] -= rel[compi][i];
      ally[allynum][compi]++;
      break;
    }
  }
  return;
}
int main() {
  int k, n, m, q, tem;
  string st;
  cin >> k >> n >> m >> q;
  while (n--) cin >> st, mp[st] = mpn++, v.push_back(st);
  getline(cin, st);
  compstart = mpn;
  while (m--) {
    getline(cin, st);
    AddComposite(st);
  }
  while (q--) {
    cin >> tem >> st;
    AddToAlly(tem - 1, mp[st]);
  }
  sort(v.begin(), v.end());
  for (int i = 0; i < k; i++) {
    int cnt = 0;
    for (int j = 0; j < mpn; j++)
      if (ally[i][j] > 0) cnt++;
    cout << cnt << endl;
    for (vector<string>::iterator iter = v.begin(); iter != v.end(); iter++)
      if (ally[i][mp[*iter]] > 0)
        cout << *iter << " " << ally[i][mp[*iter]] << endl;
  }
  return 0;
}
