#include <bits/stdc++.h>
using namespace std;
const int maxn = 256;
map<string, int> ma[maxn];
string all[maxn];
vector<string> need[maxn];
vector<int> cnt[maxn];
string name[maxn];
int n, m, k, q;
char s[1 << 20];
int main() {
  scanf("%d %d %d %d", &n, &m, &k, &q);
  gets(s);
  for (int i = 0; i < (m); ++i) {
    gets(s);
    all[i] = s;
  }
  for (int i = 0; i < (k); ++i) {
    gets(s);
    int len = strlen(s);
    for (int j = 0; j < (len); ++j)
      if (s[j] == ':' || s[j] == ',') s[j] = ' ';
    istringstream sin(s);
    sin >> name[i];
    string q;
    int w;
    for (; sin >> q >> w;) need[i].push_back(q), cnt[i].push_back(w);
  }
  while (q--) {
    gets(s);
    istringstream sin(s);
    int id;
    string w;
    sin >> id >> w;
    --id;
    ma[id][w]++;
    for (int i = 0; i < (k); ++i) {
      int ok = 1;
      for (int j = 0; j < (int((need[i]).size())); ++j)
        if (ma[id][need[i][j]] < cnt[i][j]) {
          ok = 0;
          break;
        }
      if (ok) {
        for (int j = 0; j < (int((need[i]).size())); ++j)
          ma[id][need[i][j]] -= cnt[i][j];
        ma[id][name[i]]++;
        break;
      }
    }
  }
  for (int i = 0; i < (n); ++i) {
    vector<pair<string, int> > ans;
    for (__typeof((ma[i]).begin()) it = (ma[i]).begin(); it != (ma[i]).end();
         ++it)
      if (it->second > 0) ans.push_back(*it);
    printf("%d\n", int((ans).size()));
    for (__typeof((ans).begin()) it = (ans).begin(); it != (ans).end(); ++it)
      printf("%s %d\n", it->first.c_str(), it->second);
  }
  return 0;
}
