#include <bits/stdc++.h>
using namespace std;
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
int main() {
  ios::sync_with_stdio(false);
  int k, n, m, q, idx = 0, x;
  cin >> k >> n >> m >> q;
  string s, nm, ss;
  map<string, int> basicToNum, comToNum;
  map<int, string> numToBasic, numToCom;
  for (int i = (0); i < (n); i++) {
    cin >> s;
    numToBasic[idx] = s;
    basicToNum[s] = idx++;
  }
  idx = 0;
  getline(cin, ss);
  int comp[60][60];
  memset(comp, 0, sizeof(comp));
  for (int i = (0); i < (m); i++) {
    getline(cin, ss);
    stringstream str(ss);
    str >> nm;
    nm.erase((int)nm.size() - 1, 1);
    numToCom[idx] = nm;
    comToNum[nm] = idx++;
    while (str >> ss >> x) {
      comp[comToNum[nm]][basicToNum[ss]] += x;
      str >> ss;
    }
  }
  int haveBasic[110][60], haveCom[110][60];
  memset(haveBasic, 0, sizeof(haveBasic)), memset(haveCom, 0, sizeof(haveCom));
  for (int qq = (0); qq < (q); qq++) {
    cin >> x >> nm;
    haveBasic[x][basicToNum[nm]]++;
    for (int i = (0); i < (60); i++) {
      bool gd = true, zero = true;
      for (int j = (0); j < (60); j++) {
        if (haveBasic[x][j] < comp[i][j]) gd = false;
        if (comp[i][j]) zero = false;
      }
      if (gd && !zero) {
        for (int j = (0); j < (60); j++) {
          if (haveBasic[x][j]) {
            haveBasic[x][j] -= comp[i][j];
          }
        }
        haveCom[x][i]++;
        break;
      }
    }
  }
  for (int i = (1); i < (k + 1); i++) {
    vector<pair<string, int> > res;
    for (int j = (0); j < (60); j++) {
      if (haveBasic[i][j]) {
        res.push_back(make_pair(numToBasic[j], haveBasic[i][j]));
      }
    }
    for (int j = (0); j < (60); j++) {
      if (haveCom[i][j]) {
        res.push_back(make_pair(numToCom[j], haveCom[i][j]));
      }
    }
    sort((res).begin(), (res).end());
    cout << (int)res.size() << endl;
    for (int j = (0); j < ((int)res.size()); j++)
      cout << res[j].first << " " << res[j].second << endl;
  }
  return 0;
}
