#include <bits/stdc++.h>
using namespace std;
int K, N, M, Q;
map<string, int> Basic, F[111];
string st, Comp_St[60];
vector<pair<string, int> > Comp[60];
int main() {
  cin >> K >> N >> M >> Q;
  for (int i = 1; i <= N; i++) {
    cin >> st;
    Basic[st] = i;
  }
  for (int i = 0, k; i < M; i++) {
    cin >> st;
    st = st.substr(0, st.size() - 1);
    Comp_St[i] = st;
    for (string stt; cin >> stt;) {
      cin >> k;
      Comp[i].push_back(make_pair(stt, k));
      char ch;
      scanf("%c", &ch);
      if (ch != ',') break;
    }
  }
  for (int i = 0, p; i < Q; i++) {
    cin >> p >> st;
    F[p][st]++;
    for (int j = 0; j < M; j++) {
      st = Comp_St[j];
      bool flag = true;
      for (int k = 0; k < Comp[j].size(); k++)
        if (F[p][Comp[j][k].first] < Comp[j][k].second) flag = false;
      if (flag) {
        F[p][st]++;
        for (int k = 0; k < Comp[j].size(); k++)
          F[p][Comp[j][k].first] -= Comp[j][k].second;
      }
    }
  }
  for (int i = 1; i <= K; i++) {
    int TOT = 0;
    for (map<string, int>::iterator it = F[i].begin(); it != F[i].end(); it++)
      if (it->second > 0) TOT++;
    printf("%d\n", TOT);
    for (map<string, int>::iterator it = F[i].begin(); it != F[i].end(); it++)
      if (it->second > 0) cout << it->first << " " << it->second << endl;
  }
  return 0;
}
