#include <bits/stdc++.h>
using namespace std;
struct p {
  string s;
  vector<pair<int, int>> a;
};
struct pp {
  string s;
  int n;
};
bool cmp(pp a, pp b) { return a.s < b.s; }
string a[200];
p b[200];
int to_int(string s) {
  int res = 0;
  for (int i = 0; i < s.length(); i++) {
    if (s[i] >= '0' && s[i] <= '9') {
      res *= 10;
      res += s[i] - '0';
    }
  }
  return res;
}
int c[200][200];
int main() {
  int k, n, m, q;
  cin >> k >> n >> m >> q;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < m; i++) {
    string s;
    cin >> s;
    s.erase(s.length() - 1, 1);
    b[i].s = s;
    while (1) {
      string s1, s2;
      cin >> s1 >> s2;
      int x = to_int(s2);
      int y;
      for (int j = 0; j < n; j++) {
        if (a[j] == s1) {
          y = j;
          break;
        }
      }
      b[i].a.push_back(make_pair(y, x));
      if (s2[s2.length() - 1] != ',') break;
    }
  }
  for (int i = 0; i < q; i++) {
    int x;
    string s;
    cin >> x >> s;
    int y;
    for (int j = 0; j < n; j++) {
      if (a[j] == s) {
        y = j;
        break;
      }
    }
    c[x][y]++;
    for (int j = 0; j < m; j++) {
      bool ok = 1;
      for (int k = 0; k < b[j].a.size(); k++) {
        if (c[x][b[j].a[k].first] < b[j].a[k].second) {
          ok = 0;
          break;
        }
      }
      if (!ok) continue;
      c[x][n + j]++;
      for (int k = 0; k < b[j].a.size(); k++) {
        c[x][b[j].a[k].first] -= b[j].a[k].second;
      }
    }
  }
  for (int i = 1; i <= k; i++) {
    int nn = 0;
    for (int j = 0; j < n + m; j++) {
      nn += c[i][j];
    }
    vector<pp> xx;
    xx.clear();
    for (int j = 0; j < n + m; j++) {
      if (c[i][j] == 0) continue;
      pp aa;
      aa.n = c[i][j];
      if (j < n)
        aa.s = a[j];
      else
        aa.s = b[j - n].s;
      xx.push_back(aa);
    }
    cout << xx.size() << endl;
    sort(xx.begin(), xx.end(), cmp);
    for (int j = 0; j < xx.size(); j++) {
      cout << xx[j].s << ' ' << xx[j].n << endl;
    }
  }
}
