#include <bits/stdc++.h>
using namespace std;
string b[55], c[55];
vector<pair<string, int> > cv[55];
map<string, int> mp[105];
inline int func(string &s) {
  int ret = 0;
  for (auto ch : s) {
    ret = ret * 10 + ch - '0';
  }
  return ret;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout << setprecision(32);
  int k, n, m, q;
  cin >> k >> n >> m >> q;
  for (int i = 0; i < n; i++) {
    cin >> b[i];
  }
  for (int j = 0; j < m; j++) {
    cin >> c[j];
    c[j].pop_back();
    while (true) {
      string x, y;
      cin >> x >> y;
      if (y.back() == ',') {
        y.pop_back();
        cv[j].emplace_back(x, func(y));
      } else {
        cv[j].emplace_back(x, func(y));
        break;
      }
    }
  }
  for (int i = 0; i < q; i++) {
    int p;
    string s;
    cin >> p >> s;
    mp[p][s]++;
    for (int j = 0; j < m; j++) {
      bool flg = true;
      for (auto x : cv[j]) {
        if (!mp[p].count(x.first) || mp[p][x.first] < x.second) {
          flg = false;
          break;
        }
      }
      if (flg) {
        for (auto x : cv[j]) {
          mp[p][x.first] -= x.second;
          if (!mp[p][x.first]) mp[p].erase(x.first);
        }
        mp[p][c[j]]++;
        break;
      }
    }
  }
  for (int i = 1; i <= k; i++) {
    cout << mp[i].size() << '\n';
    for (auto x : mp[i]) {
      cout << x.first << " " << x.second << '\n';
    }
  }
  return 0;
}
