#include <bits/stdc++.h>
using namespace std;
int n, m, k;
bool debug = false;
struct CA {
  string name;
  map<string, int> cc;
} ca[55];
map<string, int> mp[105];
void com(int i) {
  for (int j = 0; j < m; j++) {
    CA cart = ca[j];
    bool bl = true;
    for (map<string, int>::iterator it = cart.cc.begin(); it != cart.cc.end();
         it++) {
      if (mp[i][it->first] < it->second) {
        bl = false;
        break;
      }
    }
    if (bl) {
      for (map<string, int>::iterator it = cart.cc.begin(); it != cart.cc.end();
           it++) {
        mp[i][it->first] -= it->second;
      }
      mp[i][cart.name]++;
      return;
    }
  }
}
int main() {
  int q, num;
  char d;
  scanf("%d%d%d%d", &k, &n, &m, &q);
  string s;
  while (n--) cin >> s;
  for (int i = 0; i < m; i++) {
    cin >> ca[i].name;
    ca[i].name.pop_back();
    while (true) {
      cin >> s >> num;
      ca[i].cc[s] = num;
      scanf("%c", &d);
      if (d != ',') break;
    }
  }
  for (int i = 0; i < q; i++) {
    cin >> num >> s;
    mp[num][s]++;
    com(num);
  }
  for (int i = 1; i <= k; i++) {
    int ss = 0;
    for (map<string, int>::iterator it = mp[i].begin(); it != mp[i].end();
         it++) {
      if (it->second > 0) {
        ss++;
      }
    }
    printf("%d\n", ss);
    for (map<string, int>::iterator it = mp[i].begin(); it != mp[i].end();
         it++) {
      if (it->second > 0) {
        printf("%s %d\n", &(it->first)[0], it->second);
      }
    }
  }
  return 0;
}
