#include <bits/stdc++.h>
using namespace std;
unsigned short int kn4m[110][50 + 50];
unsigned short int mn[50][50];
char comp[110], base[110];
unsigned short int numb;
int main() {
  int k, n, m, q;
  scanf("%d %d %d %d", &k, &n, &m, &q);
  vector<string> V(n + m);
  map<string, unsigned short int> M;
  for (int i = 0; i < n; i++) {
    scanf("\n%s", base);
    V[i] = string(base);
    M[V[i]] = i;
  }
  for (int i = 0; i < m; i++) {
    scanf("\n%[^:]s", comp);
    scanf(": %s %d", base, &numb);
    V[n + i] = string(comp);
    M[V[n + i]] = n + i;
    do {
      mn[i][M[string(base)]] += numb;
    } while (scanf(", %s %d", base, &numb) > 0);
  }
  for (int i = 0; i < q; i++) {
    int numb;
    char base[110];
    scanf("\n%d %s", &numb, base);
    kn4m[numb][M[string(base)]]++;
    for (int i = 0; i < m; i++) {
      bool f = true;
      for (int j = 0; j < n; j++)
        if (kn4m[numb][j] < mn[i][j]) {
          f = false;
          break;
        }
      if (f) {
        for (int j = 0; j < n; j++) kn4m[numb][j] -= mn[i][j];
        kn4m[numb][n + i]++;
        break;
      }
    }
  }
  for (int i = 1; i <= k; i++) {
    vector<pair<string, int>> res;
    for (int j = 0; j < n + m; j++)
      if (kn4m[i][j] > 0) res.push_back(make_pair(V[j], kn4m[i][j]));
    sort(res.begin(), res.end());
    cout << res.size() << endl;
    for (int i = 0; i < res.size(); i++)
      cout << res[i].first << " " << res[i].second << endl;
  }
  return 0;
}
