#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000009;
int main() {
  int k, n, m, q;
  vector<map<string, int> > ally;
  map<string, vector<pair<string, int> > > comp;
  string cur, curn;
  int curcoun;
  char s;
  cin >> k >> n >> m >> q;
  ally = vector<map<string, int> >(k);
  while (n--) cin >> cur;
  while (m--) {
    cin >> cur;
    cur = cur.substr(0, cur.size() - 1);
    do {
      cin >> curn >> curcoun;
      comp[cur].push_back({curn, curcoun});
      s = getchar();
    } while (s != '\n');
  }
  while (q--) {
    cin >> n >> cur;
    ally[n - 1][cur]++;
    for (auto& i : comp) {
      bool can = true;
      for (__typeof(i.second.size()) j = (0) - ((0) > (i.second.size()));
           j != (i.second.size()) - ((0) > (i.second.size()));
           j += 1 - 2 * ((0) > (i.second.size()))) {
        if (ally[n - 1][i.second[j].first] != i.second[j].second) {
          can = false;
          break;
        }
      }
      if (can) {
        for (__typeof(i.second.size()) j = (0) - ((0) > (i.second.size()));
             j != (i.second.size()) - ((0) > (i.second.size()));
             j += 1 - 2 * ((0) > (i.second.size()))) {
          ally[n - 1][i.second[j].first] = 0;
        }
        ally[n - 1][i.first]++;
        break;
      }
    }
  }
  for (__typeof(k) i = (0) - ((0) > (k)); i != (k) - ((0) > (k));
       i += 1 - 2 * ((0) > (k))) {
    int x = 0;
    for (auto& j : ally[i]) {
      if (j.second > 0) x++;
    }
    cout << x << endl;
    for (auto& j : ally[i]) {
      if (j.second > 0) {
        cout << j.first << ' ' << j.second << endl;
      }
    }
  }
}
