#include <bits/stdc++.h>
using namespace std;
string basic[110];
map<string, int> ind;
struct comp {
  string nome;
  vector<pair<int, int> > p;
};
comp comps[110];
map<int, int> allies[110];
vector<string> resalies[1000];
char buf[10000];
char a[300];
int main() {
  int k, n, m, q;
  scanf("%d %d %d %d ", &k, &n, &m, &q);
  for (int i = 0; i < n; i++) cin >> basic[i];
  sort(basic, basic + n);
  for (int i = 0; i < n; i++) ind[basic[i]] = i;
  scanf(" ");
  for (int i = 0; i < m; i++) {
    int pos = 0, it, qtd;
    scanf(" %[^:]: ", &a);
    comps[i].nome = a;
    char lixo = 0;
    ind[comps[i].nome] = n + i;
    basic[n + i] = a;
    while (1) {
      scanf("%s %d%c", &a, &qtd, &lixo);
      string tmp = a;
      comps[i].p.push_back(make_pair(ind[tmp], qtd));
      if (lixo == '\n') break;
    }
    sort(comps[i].p.begin(), comps[i].p.end());
  }
  for (int i = 0; i < q; i++) {
    int ai;
    scanf("%d %s ", &ai, a);
    string tmp = a;
    if (allies[ai].find(ind[tmp]) != allies[ai].end())
      allies[ai][ind[tmp]] = allies[ai][ind[tmp]] + 1;
    else
      allies[ai][ind[tmp]] = 1;
    for (int j = 0; j < m; j++) {
      int passou = 1;
      for (int a = 0; a < comps[j].p.size(); a++) {
        int componente_atual = comps[j].p[a].first,
            quant = comps[j].p[a].second;
        if (allies[ai].find(componente_atual) == allies[ai].end() ||
            quant > allies[ai][componente_atual]) {
          passou = 0;
          break;
        }
      }
      if (passou) {
        resalies[ai].push_back(comps[j].nome);
        for (int a = 0; a < comps[j].p.size(); a++) {
          int componente_atual = comps[j].p[a].first,
              quant = comps[j].p[a].second;
          allies[ai][componente_atual] = allies[ai][componente_atual] - quant;
        }
        if (allies[ai].find(ind[comps[j].nome]) == allies[ai].end())
          allies[ai][ind[comps[j].nome]] = 1;
        else
          allies[ai][ind[comps[j].nome]] = allies[ai][ind[comps[j].nome]] + 1;
        break;
      }
    }
  }
  vector<string> aa;
  for (int i = 1; i <= k; i++) {
    for (map<int, int>::iterator it = allies[i].begin(); it != allies[i].end();
         it++)
      if (it->second > 0) {
        resalies[i].push_back(basic[it->first]);
      }
    aa.clear();
    sort(resalies[i].begin(), resalies[i].end());
    unique_copy(resalies[i].begin(), resalies[i].end(),
                inserter(aa, aa.begin()));
    printf("%d\n", aa.size());
    for (int a = 0; a < aa.size(); a++) {
      printf("%s %d\n", aa[a].c_str(), allies[i][ind[aa[a]]]);
    }
  }
  return 0;
}
