#include <bits/stdc++.h>
using namespace std;
int n, k, m;
map<string, int> ma;
vector<int> v[400], vf[400];
string bart[500], cart[500];
string find(string s, int &n) {
  string t = "";
  for (n; n < s.length() && s[n] != ' '; n++) t += s[n];
  return t;
}
int find2(string s, int &n) {
  int k = 0, j = n;
  while (s[n] >= '0' && s[n] <= '9' && n < s.length()) n++;
  for (int i = n - 1; i >= j; i--) {
    k *= 10;
    k += s[i] - '0';
  }
  return k;
}
void check(int o) {
  for (int i = 0; i < m; i++) {
    bool ch = 1;
    for (int j = 0; j < n; j++)
      if (v[i][j] > vf[o][j]) ch = 0;
    if (ch) {
      for (int j = 0; j < n; j++) vf[o][j] -= v[i][j];
      vf[o][n + i]++;
    }
  }
}
int main() {
  int q;
  cin >> k >> n >> m >> q;
  string s;
  for (int i = 0; i < n; i++) {
    cin >> s;
    bart[i] = s;
    ma[s] = i;
  }
  getline(cin, s);
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < n; j++) v[i].push_back(0);
    getline(cin, s);
    string t = "";
    int j;
    for (j = 0; j < s.length() && s[j] != ':'; j++) t += s[j];
    cart[i] = t;
    ma[t] = n + i;
    j += 2;
    while (j < s.length()) {
      string t = find(s, j);
      j++;
      int ta = find2(s, j);
      j += 2;
      v[i][ma[t]] += ta;
    }
  }
  for (int i = 0; i < k; i++)
    for (int j = 0; j < n + m; j++) vf[i].push_back(0);
  for (int i = 0; i < q; i++) {
    int k;
    cin >> k >> s;
    k--;
    vf[k][ma[s]]++;
    check(k);
  }
  for (int i = 0; i < k; i++) {
    vector<string> ans;
    for (int j = 0; j < n + m; j++)
      if (vf[i][j]) {
        if (j >= n)
          ans.push_back(cart[j - n]);
        else
          ans.push_back(bart[j]);
      }
    sort(ans.begin(), ans.end());
    cout << ans.size() << endl;
    for (int j = 0; j < ans.size(); j++)
      cout << ans[j] << " " << vf[i][ma[ans[j]]] << endl;
  }
  return 0;
}
