#include <bits/stdc++.h>
using namespace std;
map<string, int> mps;
map<int, string> mpi;
int cnt[120][120];
vector<pair<int, int> > v[120];
int main() {
  int k, n, m, q, idx = 0, cn;
  cin >> k >> n >> m >> q;
  memset(cnt, 0, sizeof cnt);
  string s, nm;
  for (int i = 0; i < n; i++) {
    cin >> s;
    mpi[idx] = s;
    mps[s] = idx++;
  }
  for (int i = 0; i < m; i++) {
    cin >> s;
    s = s.substr(0, s.size() - 1);
    mpi[idx] = s;
    mps[s] = idx++;
    while (1) {
      cin >> s >> nm;
      if (nm[nm.size() - 1] == ',') {
        nm = nm.substr(0, nm.size() - 1);
        v[i].push_back({mps[s], stoi(nm)});
      } else {
        v[i].push_back({mps[s], stoi(nm)});
        break;
      }
    }
  }
  for (int i = 0; i < q; i++) {
    cin >> cn >> s;
    cn--;
    cnt[cn][mps[s]]++;
    while (1) {
      bool found = 0;
      for (int j = 0; j < m; j++) {
        bool ck = 1;
        for (int f = 0; f < v[j].size(); f++)
          ck &= (cnt[cn][v[j][f].first] >= v[j][f].second);
        if (ck) {
          for (int f = 0; f < v[j].size(); f++)
            cnt[cn][v[j][f].first] -= v[j][f].second;
          cnt[cn][j + n]++;
          found = 1;
          break;
        }
      }
      if (!found) break;
    }
  }
  for (int i = 0; i < k; i++) {
    vector<pair<string, int> > ans;
    for (int j = 0; j < 120; j++)
      if (cnt[i][j]) ans.push_back({mpi[j], cnt[i][j]});
    sort(ans.begin(), ans.end());
    cout << ans.size() << "\n";
    for (int j = 0; j < ans.size(); j++)
      cout << ans[j].first << " " << ans[j].second << "\n";
  }
}
