#include <bits/stdc++.h>
using namespace std;
long double const EPS = 1e-9;
long double const PI = acos(-1.0);
int const INF = 1e9;
int const LINF = 1e18;
int k, n, m, q;
int str2int(string temp) {
  istringstream stream(temp);
  int res;
  stream >> res;
  return res;
}
struct recept {
  string title;
  vector<pair<string, int> > source;
};
vector<recept> recepts;
vector<string> e;
vector<map<string, int> > players;
int main() {
  cin >> k >> n >> m >> q;
  recepts.resize(m);
  players.resize(k);
  string data, temp;
  for (int i = 0; i < (int)(n); i++) {
    cin >> data;
    e.push_back(data);
  }
  scanf("\n");
  for (int i = 0; i < (int)(m); i++) {
    getline(cin, data);
    istringstream stream(data);
    stream >> recepts[i].title;
    recepts[i].title = recepts[i].title.substr(0, recepts[i].title.size() - 1);
    e.push_back(recepts[i].title);
    while (stream >> data) {
      int cnt;
      stream >> cnt >> temp;
      recepts[i].source.push_back(make_pair(data, cnt));
    }
  }
  for (int i = 0; i < (int)(q); i++) {
    int player;
    cin >> player >> data;
    player--;
    players[player][data]++;
    for (int j = 0; j < (int)(recepts.size()); j++) {
      bool flag = true;
      for (int q = 0; q < (int)(recepts[j].source.size()); q++)
        if (players[player][recepts[j].source[q].first] <
            recepts[j].source[q].second)
          flag = false;
      if (flag) {
        for (int q = 0; q < (int)(recepts[j].source.size()); q++)
          players[player][recepts[j].source[q].first] -=
              recepts[j].source[q].second;
        players[player][recepts[j].title]++;
      }
    }
  }
  sort(e.begin(), e.end());
  for (int i = 0; i < (int)(k); i++) {
    int cnt = 0;
    for (int j = 0; j < (int)(e.size()); j++)
      if (players[i][e[j]] != 0) cnt++;
    cout << cnt << endl;
    for (int j = 0; j < (int)(e.size()); j++)
      if (players[i][e[j]] != 0)
        cout << e[j] << ' ' << players[i][e[j]] << endl;
  }
  return 0;
}
