#include <bits/stdc++.h>
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
struct art {
  string name;
  vector<pair<string, long long int>> need;
  art(string name = "") {
    this->name = name;
    this->need = {};
  }
  art(string& name, vector<pair<string, long long int>>& need) {
    this->name = name;
    this->need = need;
  }
};
signed main() {
  long long int k, n, m, q;
  cin >> k >> n >> m >> q;
  vector<unordered_map<string, long long int>> pcnt(k);
  for (long long int i = 0; i < n; i++) {
    string s;
    cin >> s;
  }
  vector<art> arts;
  string s;
  getline(cin, s);
  for (long long int i = 0; i < m; i++) {
    getline(cin, s);
    istringstream ss(s);
    string name;
    ss >> name;
    name.pop_back();
    vector<pair<string, long long int>> need;
    while (!ss.eof()) {
      string pname;
      string cnt;
      ss >> pname;
      ss >> cnt;
      if (cnt.back() == ',') cnt.pop_back();
      need.push_back({pname, stoi(cnt)});
    }
    arts.push_back(art(name, need));
  }
  for (long long int i = 0; i < q; i++) {
    long long int ind;
    cin >> ind >> s;
    ind--;
    pcnt[ind][s]++;
    for (auto& j : arts) {
      bool ok = true;
      for (auto& el : j.need) {
        if (pcnt[ind][el.first] < el.second) {
          ok = false;
          break;
        }
      }
      if (ok) {
        pcnt[ind][j.name]++;
        for (auto& el : j.need) pcnt[ind][el.first] -= el.second;
        break;
      }
    }
  }
  for (long long int i = 0; i < k; i++) {
    vector<pair<string, long long int>> vals;
    for (auto& j : pcnt[i]) {
      if (j.second > 0) vals.push_back(j);
    }
    cout << vals.size() << "\n";
    sort(vals.begin(), vals.end());
    for (auto& j : vals) cout << j.first << " " << j.second << "\n";
  }
  return 0;
}
