#include <bits/stdc++.h>
using namespace std;
int P, N, M, Q, id, cnt;
string dummy, line, name;
string NAME[1100];
map<string, int> C[1100], R[1100];
vector<pair<string, int> > sol;
int num(string s) {
  int ret = 0;
  stringstream ss(s);
  ss >> ret;
  return ret;
}
int main() {
  cin >> P >> N >> M >> Q;
  for (int I = 0; I < N; I++) {
    cin >> dummy;
  }
  getline(cin, dummy);
  for (int I = 0; I < M; I++) {
    getline(cin, line);
    int pos = line.find(":");
    name = line.substr(0, pos);
    line = line.substr(pos + 2);
    NAME[I] = name;
    bool ok = true;
    while (ok) {
      pos = line.find(" ");
      name = line.substr(0, pos);
      line = line.substr(pos + 1);
      pos = int(line.find(","));
      if (pos < 0) {
        cnt = num(line);
        ok = false;
      } else {
        cnt = num(line.substr(0, pos));
        line = line.substr(pos + 2);
      }
      R[I][name] = cnt;
    }
  }
  while (Q--) {
    cin >> id;
    cin >> name;
    C[id][name]++;
    for (int a = 0; a < M; a++) {
      bool ok = true;
      for (typeof((R[a]).begin()) it = (R[a]).begin(); it != (R[a]).end();
           it++) {
        name = it->first;
        if (C[id][name] < it->second) {
          ok = false;
          break;
        }
      }
      if (ok) {
        for (typeof((R[a]).begin()) it = (R[a]).begin(); it != (R[a]).end();
             it++) {
          name = it->first;
          C[id][name] -= it->second;
        }
        C[id][NAME[a]]++;
      }
    }
  }
  for (int id = 1; id <= P; id++) {
    sol.clear();
    for (typeof((C[id]).begin()) it = (C[id]).begin(); it != (C[id]).end();
         it++)
      if (it->second > 0)
        sol.push_back(pair<string, int>(it->first, it->second));
    cout << int((sol).size()) << endl;
    for (typeof((sol).begin()) it = (sol).begin(); it != (sol).end(); it++)
      cout << it->first << " " << it->second << endl;
  }
}
