#include <bits/stdc++.h>
using namespace std;
map<string, int> ind_art;
string nm_comp[100], simp_art[100];
vector<vector<int> > comp_art;
vector<int> frnds[110];
vector<int> fr_comp[110];
int k, n, m, q;
int main() {
  cin >> k >> n >> m >> q;
  comp_art.resize(m);
  for (int i = 0; i < n; i++) {
    string nm_art;
    cin >> nm_art;
    ind_art[nm_art] = i;
    simp_art[i] = nm_art;
  }
  getchar();
  for (int i = 0; i < m; i++) {
    comp_art[i].resize(n);
    char w[2000];
    gets(w);
    stringstream in(w);
    string nm;
    in >> nm;
    nm.resize(nm.size() - 1);
    nm_comp[i] = nm;
    string art;
    string cnt;
    int d;
    while (in >> art >> cnt) {
      if (cnt[cnt.size() - 1] == ',') cnt.resize(cnt.size() - 1);
      sscanf(cnt.c_str(), "%d", &d);
      comp_art[i][ind_art[art]] = d;
    }
  }
  for (int i = 0; i < q; i++) {
    int d;
    string art;
    cin >> d >> art;
    if (frnds[d].size() == 0) {
      frnds[d].resize(n);
      fr_comp[d].resize(m);
    }
    frnds[d][ind_art[art]]++;
    for (int j = 0; j < m; j++) {
      bool can = true;
      for (int k = 0; k < n; k++)
        if (frnds[d][k] < comp_art[j][k]) can = false;
      if (can) {
        for (int k = 0; k < n; k++)
          if (comp_art[j][k]) frnds[d][k] -= comp_art[j][k];
        fr_comp[d][j]++;
      }
    }
  }
  vector<pair<string, int> > ans;
  for (int i = 1; i <= k; i++) {
    ans.clear();
    if (frnds[i].size() > 0) {
      for (int j = 0; j < n; j++) {
        if (frnds[i][j] > 0) ans.push_back(make_pair(simp_art[j], frnds[i][j]));
      }
    }
    if (fr_comp[i].size() > 0) {
      for (int k = 0; k < m; k++) {
        if (fr_comp[i][k] > 0)
          ans.push_back(make_pair(nm_comp[k], fr_comp[i][k]));
      }
    }
    printf("%d\n", ans.size());
    sort(ans.begin(), ans.end());
    for (int l = 0; l < ans.size(); l++)
      cout << ans[l].first << ' ' << ans[l].second << '\n';
  }
  return 0;
}
