#include <bits/stdc++.h>
using namespace std;
double const EPS = 2.22045e-016;
int gcd(int a, int b) { return (b == 0 ? a : gcd(b, a % b)); }
vector<vector<int> > frd;
map<string, int> mp;
vector<vector<int> > comp;
int main() {
  int k, n, m, q;
  scanf("%d %d %d %d", &k, &n, &m, &q);
  comp.assign(m, vector<int>(n + m, 0));
  frd.assign(k, vector<int>(n + m, 0));
  vector<string> bas_arr(n, ""), com_arr(m, "");
  string s;
  for (int i = 0; i < (int)(n); ++i) {
    cin >> s;
    bas_arr[i] = s;
    mp[s] = i + m;
  }
  string tet = " ";
  cin >> s;
  for (int i = 0; i < (int)(m); ++i) {
    if (i > 0) s = tet;
    s.erase(s.end() - 1);
    mp[s] = i;
    cin >> tet;
    while (find(((bas_arr).begin()), ((bas_arr).end()), tet) != bas_arr.end()) {
      int b;
      scanf("%d", &b);
      comp[mp[s]][mp[tet]] = b;
      cin.ignore();
      cin >> tet;
    }
  }
  stringstream ss;
  int b;
  ss << tet;
  ss >> b;
  ss.clear();
  for (int i = 0; i < (int)(q); ++i) {
    if (i > 0) scanf("%d", &b);
    b--;
    cin >> s;
    frd[b][mp[s]]++;
    for (int c = 0; c < (int)(m); ++c) {
      bool good = true;
      for (int v = 0; v < (int)(m + n); ++v) {
        if (frd[b][v] < comp[c][v]) {
          good = false;
          break;
        }
      }
      if (good) {
        frd[b][c]++;
        for (int v = 0; v < (int)(m + n); ++v) {
          frd[b][v] -= comp[c][v];
        }
        break;
      }
    }
  }
  for (int i = 0; i < (int)(k); ++i) {
    map<string, int> ans;
    vector<string> ans2;
    map<string, int>::iterator it = mp.begin();
    for (int v = 0; v < (int)(n + m); ++v) {
      if (frd[i][v] > 0) {
        map<string, int>::iterator it = mp.begin();
        for (int ttt = 0; ttt < (int)(n + m); ++ttt) {
          if (it->second == v) break;
          it++;
        }
        ans[(it)->first] = frd[i][v];
        ans2.push_back((it)->first);
      }
    }
    sort(((ans2).begin()), ((ans2).end()));
    printf("%d\n", ((int)((ans2).size())));
    for (int v = 0; v < (int)(((int)((ans2).size()))); ++v) {
      printf("%s %d\n", ans2[v].c_str(), ans[ans2[v]]);
    }
  }
  return 0;
}
