#include <bits/stdc++.h>
using namespace std;
const double eps = 1.0e-11;
const double pi = acos(-1.0);
const int N = 110;
int k, n, m, q;
string base[N];
struct art {
  string name;
  map<string, int> contains;
  void read() {
    cin >> name;
    name = name.substr(0, (int)(name).size() - 1);
    string a, num;
    while (cin >> a >> num) {
      int c;
      if (num[(int)(num).size() - 1] == ',')
        c = atoi(num.substr(0, (int)(num).size() - 1).c_str());
      else
        c = atoi(num.substr(0, (int)(num).size()).c_str());
      contains[a] = c;
      if (num[(int)(num).size() - 1] != ',') break;
    }
  }
} mixed[N];
struct player {
  map<string, int> contains;
  void add(string name) {
    if (contains.find(name) == contains.end()) contains[name] = 0;
    ++contains[name];
    for (long long i = 0; i < m; i++) {
      bool ok = true;
      for (auto it = mixed[i].contains.begin(); it != mixed[i].contains.end();
           ++it) {
        if (contains.find(it->first) == contains.end() ||
            contains[it->first] < it->second)
          ok = false;
      }
      if (ok) {
        if (contains.find(mixed[i].name) == contains.end())
          contains[mixed[i].name] = 0;
        ++contains[mixed[i].name];
        for (auto it = mixed[i].contains.begin(); it != mixed[i].contains.end();
             ++it) {
          contains[it->first] -= it->second;
        }
        break;
      }
    }
  }
  void print() {
    int cnt = 0;
    for (auto it = contains.begin(); it != contains.end(); ++it)
      if (it->second) ++cnt;
    cout << cnt << endl;
    for (auto it = contains.begin(); it != contains.end(); ++it)
      if (it->second) cout << it->first << " " << it->second << endl;
  }
};
player p[N];
int main() {
  cin >> k >> n >> m >> q;
  for (long long i = 0; i < n; i++) cin >> base[i];
  for (long long i = 0; i < m; i++) mixed[i].read();
  for (long long i = 0; i < q; i++) {
    int c;
    string name;
    cin >> c >> name;
    p[c - 1].add(name);
  }
  for (long long i = 0; i < k; i++) p[i].print();
  return 0;
}
