#include <bits/stdc++.h>
using namespace std;
const int oo = (int)1e9;
const int MAXN = 100;
int k, n, m, q;
map<string, int> dic;
map<int, string> dic_back;
map<int, vector<pair<int, int> > > comp;
map<int, int> player[MAXN];
void check(int p) {
  for (typeof((comp).begin()) it = (comp).begin(); it != (comp).end(); it++) {
    bool completed = true;
    for (typeof((comp[it->first]).begin()) obj = (comp[it->first]).begin();
         obj != (comp[it->first]).end(); obj++)
      if (player[p][obj->first] < obj->second) {
        completed = false;
        break;
      }
    if (completed) {
      for (typeof((comp[it->first]).begin()) obj = (comp[it->first]).begin();
           obj != (comp[it->first]).end(); obj++)
        player[p][obj->first] -= obj->second;
      player[p][it->first]++;
    }
  }
}
void print(int p) {
  map<string, int> objects;
  for (typeof((player[p]).begin()) it = (player[p]).begin();
       it != (player[p]).end(); it++)
    if (it->second) objects[dic_back[it->first]] = it->second;
  cout << objects.size() << endl;
  for (typeof((objects).begin()) it = (objects).begin(); it != (objects).end();
       it++)
    cout << it->first << ' ' << it->second << endl;
}
char line[1000];
int main() {
  scanf("%d%d%d%d", &k, &n, &m, &q);
  int idx = 0;
  string art;
  for (int i = 0; i < (n); i++) {
    scanf("%s", &line);
    art = string(line);
    dic[art] = idx;
    dic_back[idx] = art;
    idx++;
  }
  int a;
  for (int i = 0; i < (m); i++) {
    scanf("%s", &line);
    art = string(line);
    art.erase(art.size() - 1);
    dic[art] = idx;
    dic_back[idx] = art;
    idx++;
    while (1) {
      scanf("%s%d", &line, &a);
      art = string(line);
      comp[idx - 1].push_back(make_pair(dic[art], a));
      char c = getchar();
      if (c == '\n' || c == EOF) break;
    }
  }
  int p;
  for (int i = 0; i < (q); i++) {
    scanf("%d%s", &p, &line);
    p--;
    art = string(line);
    player[p][dic[art]]++;
    check(p);
  }
  for (int i = 0; i < (k); i++) print(i);
  return 0;
}
