#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1.0);
const int INF = 1000000000;
const int MOD = 1000000007;
int main() {
  int k, n, m, q;
  cin >> k >> n >> m >> q;
  vector<string> base(n);
  for (int i = 0; i < n; i++) {
    cin >> base[i];
  }
  vector<pair<string, vector<pair<string, int> > > > sos(m);
  string str1;
  int e;
  for (int i = 0; i < m; i++) {
    scanf("\n ");
    getline(cin, str1);
    for (int j = 0; j < str1.size(); j++) {
      if (str1[j] == ',' || str1[j] == ':') {
        str1[j] = ' ';
      }
    }
    stringstream ss(str1);
    ss >> sos[i].first;
    while (ss >> str1 >> e) {
      sos[i].second.push_back(make_pair(str1, e));
    }
  }
  vector<pair<int, string> > buy(q);
  for (int i = 0; i < q; i++) {
    cin >> buy[i].first >> buy[i].second;
  }
  vector<vector<pair<int, string> > > have(k);
  for (int i = 0; i < k; i++) {
    for (int j = 0; j < q; j++) {
      if (buy[j].first - 1 == i) {
        for (int f = 0; f < have[i].size(); f++) {
          if (buy[j].second == have[i][f].second) {
            have[i][f].first++;
            goto a;
          }
        }
        have[i].push_back(make_pair(1, buy[j].second));
      a:;
        for (int l = 0; l < m; l++) {
          vector<int> ingr(sos[l].second.size());
          for (int p = 0; p < sos[l].second.size(); p++) {
            for (int y = 0; y < have[i].size(); y++) {
              if (sos[l].second[p].first == have[i][y].second) {
                if (sos[l].second[p].second > have[i][y].first) {
                  goto b;
                } else {
                  ingr[p] = 1;
                }
              }
            }
          }
          for (int z = 0; z < ingr.size(); z++) {
            if (ingr[z] != 1) {
              goto b;
            }
          }
          for (int p = 0; p < sos[l].second.size(); p++) {
            for (int y = 0; y < have[i].size(); y++) {
              if (sos[l].second[p].first == have[i][y].second) {
                have[i][y].first -= sos[l].second[p].second;
              }
            }
          }
          for (int f = 0; f < have[i].size(); f++) {
            if (sos[l].first == have[i][f].second) {
              have[i][f].first++;
              goto b;
            }
          }
          have[i].push_back(make_pair(1, sos[l].first));
        b:;
        }
      }
    }
  }
  vector<vector<pair<string, int> > > have2(k);
  for (int i = 0; i < k; i++) {
    have2[i].resize(have[i].size());
    for (int j = 0; j < have[i].size(); j++) {
      have2[i][j].first = have[i][j].second;
      have2[i][j].second = have[i][j].first;
    }
    sort(have2[i].begin(), have2[i].end());
  }
  int S;
  for (int i = 0; i < k; i++) {
    S = 0;
    for (int j = 0; j < have2[i].size(); j++) {
      if (have2[i][j].second != 0) {
        S++;
      }
    }
    cout << S << endl;
    for (int j = 0; j < have2[i].size(); j++) {
      if (have2[i][j].second != 0) {
        cout << have2[i][j].first << " " << have2[i][j].second << endl;
      }
    }
  }
  return 0;
}
