#include <bits/stdc++.h>
using namespace std;
map<string, int> number;
string name[146];
int need[146][146];
int cnt[146][146];
vector<pair<string, int> > v;
int toint(string s) {
  stringstream ss(s);
  int n;
  ss >> n;
  return n;
}
int main() {
  int k, n, m, q;
  cin >> k >> n >> m >> q;
  for (int i = 0; i < n; i++) {
    cin >> name[i];
    number[name[i]] = i;
  }
  memset(need, 0, sizeof(need));
  for (int j = 0; j < m; j++) {
    string s;
    cin >> s;
    name[n + j] = s.substr(0, s.length() - 1);
    number[name[n + j]] = n + j;
    while (1) {
      string cnt;
      cin >> s >> cnt;
      bool flag = false;
      if (cnt[cnt.length() - 1] == ',') {
        flag = true;
        cnt = cnt.substr(0, cnt.length() - 1);
      }
      need[j][number[s]] = toint(cnt);
      if (!flag) break;
    }
  }
  memset(cnt, 0, sizeof(cnt));
  for (int i = 0; i < q; i++) {
    int fr;
    string s;
    cin >> fr >> s;
    fr--;
    cnt[fr][number[s]]++;
    for (int j = 0; j < m; j++) {
      bool good = true;
      for (int k = 0; k < n; k++)
        if (cnt[fr][k] < need[j][k]) good = false;
      if (good) {
        for (int k = 0; k < n; k++) cnt[fr][k] -= need[j][k];
        cnt[fr][n + j]++;
      }
    }
  }
  for (int i = 0; i < k; i++) {
    v.clear();
    for (int j = 0; j < n + m; j++)
      if (cnt[i][j] > 0) v.push_back(make_pair(name[j], cnt[i][j]));
    sort(v.begin(), v.end());
    cout << v.size() << endl;
    for (int i = 0; i < v.size(); i++)
      cout << v[i].first << " " << v[i].second << endl;
  }
  return 0;
}
