#include <bits/stdc++.h>
using namespace std;
int have[105][105];
int cnt[105];
int main() {
  int k, n, m, q;
  cin >> k >> n >> m >> q;
  string s;
  int x;
  map<string, int> art;
  vector<string> artv;
  for (int i = 1; i <= n; i++) {
    cin >> s;
    art[s] = i;
    artv.push_back(s);
  }
  vector<pair<int, int> > need[55];
  cin.ignore();
  for (int i = 1; i <= m; i++) {
    stringstream ss;
    getline(cin, s);
    ss << s;
    ss >> s;
    s = s.substr(0, s.length() - 1);
    art[s] = n + i;
    artv.push_back(s);
    while (!ss.eof()) {
      ss >> s >> x;
      int id = art[s];
      need[i].push_back(make_pair(id, x));
      char c;
      ss >> c;
    }
  }
  for (int x = 0; x < q; x++) {
    int i;
    cin >> i >> s;
    int id = art[s];
    have[i][id]++;
    cnt[i]++;
    int f = 0;
    for (int j = 1; j <= m; j++) {
      bool ok = true;
      for (int k = 0; k < need[j].size(); k++) {
        int z = need[j][k].first;
        int c = need[j][k].second;
        ok &= (have[i][z] >= c);
      }
      if (ok) {
        f = j;
        break;
      }
    }
    if (f) {
      for (int k = 0; k < need[f].size(); k++) {
        int z = need[f][k].first;
        int c = need[f][k].second;
        have[i][z] -= c;
        cnt[i] -= c;
      }
      have[i][n + f]++;
      cnt[i]++;
    }
  }
  for (int i = 1; i <= k; i++) {
    set<pair<string, int> > st;
    for (int j = 1; j <= n + m; j++) {
      if (have[i][j]) {
        st.insert(make_pair(artv[j - 1], have[i][j]));
      }
    }
    cout << st.size() << "\n";
    for (set<pair<string, int> >::iterator si = st.begin(); si != st.end();
         si++)
      cout << si->first << " " << si->second << "\n";
  }
  return 0;
}
