#include <bits/stdc++.h>
using namespace std;
map<string, int> basis;
map<string, map<string, int> > comp;
map<int, map<string, int> > peop;
int main() {
  int k, n, m, p;
  cin >> k >> n >> m >> p;
  for (int i = 0; i < n; ++i) {
    string s;
    cin >> s;
    basis[s] = i + 1;
  }
  for (int i = 0; i < m; ++i) {
    string name, s, t;
    cin >> name;
    name = name.substr(0, name.length() - 1);
    bool list = true;
    map<string, int> vals;
    while (list) {
      cin >> s >> t;
      if (t.back() != ',') list = false;
      if (list) t = t.substr(0, t.length() - 1);
      int val = atoi(t.c_str());
      vals[s] = val;
    }
    comp[name] = vals;
  }
  int c;
  string name;
  for (int i = 0; i < p; ++i) {
    cin >> c >> name;
    peop[c][name]++;
    bool loop = true;
    while (loop) {
      loop = false;
      for (auto y : comp) {
        bool canbe = true;
        for (auto x : y.second) {
          if (peop[c].find(x.first) == peop[c].end() ||
              peop[c][x.first] < x.second) {
            canbe = false;
          }
        }
        if (canbe) {
          for (auto x : y.second) {
            peop[c][x.first] -= x.second;
            if (peop[c][x.first] == 0) {
              peop[c].erase(x.first);
            }
          }
          peop[c][y.first]++;
          loop = true;
          break;
        }
      }
    }
  }
  for (int i = 1; i <= k; ++i) {
    cout << peop[i].size() << endl;
    for (auto x : peop[i]) {
      cout << x.first << ' ' << x.second << endl;
    }
  }
  return 0;
}
