#include <bits/stdc++.h>
using namespace std;
int k, n, m, q;
string base[222], adv[222];
int need[222][222], have[222][222];
int ans[222][222];
int main() {
  cin >> k >> n >> m >> q;
  for (int i = 0; i < n; i++) cin >> base[i];
  for (int i = 0; i < m; i++) {
    cin >> adv[i];
    if (adv[i][adv[i].size() - 1] == ':')
      adv[i] = adv[i].substr(0, adv[i].size() - 1);
    while (true) {
      string str;
      int num;
      cin >> str >> num;
      for (int j = 0; j < n; j++)
        if (str == base[j]) {
          need[i][j] += num;
          break;
        }
      char c;
      while (c = getchar()) {
        if (c == ',' || c == '\n' || c == -1) break;
      }
      if (c == ',')
        continue;
      else
        break;
    }
  }
  for (int i = 0; i < q; i++) {
    string str;
    int num;
    cin >> num >> str;
    num--;
    int idx = -1;
    for (int j = 0; j < n; j++)
      if (str == base[j]) {
        idx = j;
        break;
      }
    have[num][idx]++;
    for (int j = 0; j < m; j++) {
      bool flag = true;
      for (int k = 0; k < n; k++)
        if (have[num][k] < need[j][k]) flag = false;
      if (flag) {
        for (int k = 0; k < n; k++) have[num][k] -= need[j][k];
        ans[num][j]++;
        break;
      }
    }
  }
  for (int i = 0; i < k; i++) {
    vector<pair<string, int> > v;
    int num = 0;
    for (int j = 0; j < m; j++)
      if (ans[i][j] > 0) {
        v.push_back(make_pair(adv[j], ans[i][j]));
      }
    for (int j = 0; j < n; j++)
      if (have[i][j] > 0) {
        v.push_back(make_pair(base[j], have[i][j]));
      }
    printf("%d\n", (int)v.size());
    sort(v.begin(), v.end());
    for (int i = 0; i < v.size(); i++)
      printf("%s %d\n", v[i].first.c_str(), v[i].second);
  }
  return 0;
}
