#include <bits/stdc++.h>
using namespace std;
long long pw(long long b, long long p) {
  if (!p) return 1;
  long long sq = pw(b, p / 2);
  sq *= sq;
  if (p % 2) sq *= b;
  return sq;
}
int gcd(int a, int b) { return (b == 0 ? a : gcd(b, a % b)); }
int sd(long long x) { return x < 10 ? x : x % 10 + sd(x / 10); }
int lcm(int a, int b) { return ((a * b) / gcd(a, b)); }
int main() {
  int k, n, m, q, c, v;
  cin >> k >> n >> m >> q;
  string x, y, z, in;
  for (int i = 0; i < n; i++) cin >> x;
  map<string, map<string, int>> composite;
  getline(cin, x);
  for (int i = 0; i < m; i++) {
    y = "";
    getline(cin, x);
    int j = 0;
    while (x[j] != ':') {
      y += x[j];
      j++;
    }
    j += 2;
    while (j < x.length()) {
      z = in = "";
      while (x[j] != ' ') {
        z += x[j];
        j++;
      }
      j++;
      while (j < x.length() && isdigit(x[j])) {
        in += x[j];
        j++;
      }
      j += 2;
      composite[y][z] = stoll(in);
    }
  }
  vector<map<string, int>> allies(k + 1);
  for (int i = 0; i < q; i++) {
    cin >> v >> x;
    allies[v][x]++;
    for (map<string, map<string, int>>::iterator it = composite.begin();
         it != composite.end(); it++) {
      c = 0;
      for (map<string, int>::iterator it2 = composite[it->first].begin();
           it2 != composite[it->first].end(); it2++)
        if (allies[v][it2->first] >= it2->second) c++;
      if (c == composite[it->first].size()) {
        allies[v][it->first]++;
        for (map<string, int>::iterator it2 = composite[it->first].begin();
             it2 != composite[it->first].end(); it2++)
          allies[v][it2->first] -= it2->second;
        break;
      }
    }
  }
  vector<pair<string, int>> out;
  for (int i = 0; i < k; i++) {
    for (map<string, int>::iterator it = allies[i + 1].begin();
         it != allies[i + 1].end(); it++)
      if (it->second) out.push_back(*it);
    cout << out.size() << endl;
    for (int j = 0; j < out.size(); j++)
      cout << out[j].first << " " << out[j].second << endl;
    out.clear();
  }
}
