#include <bits/stdc++.h>
using namespace std;
int k, n, m, q, x;
string basic;
map<string, vector<pair<string, int>>> comp;
map<int, map<string, int>> mp;
int main() {
  cin >> k >> n >> m >> q;
  for (int i = 0; i < n; i++) cin >> basic;
  cin.ignore();
  for (int i = 0; i < m; i++) {
    bool c = 0;
    string temp = "", com = "";
    getline(cin, basic);
    for (int j = 0; j < basic.size(); j++) {
      if (basic[j] == ':') {
        c = 1;
        com = temp;
        temp.clear();
        j++;
        j++;
      }
      if (c) {
        if (basic[j] >= '0' && basic[j] <= '9') {
          temp.pop_back();
          comp[com].push_back({temp, basic[j] - '0'});
          j++;
          j++;
          temp.clear();
        } else
          temp += basic[j];
      } else
        temp += basic[j];
    }
  }
  for (int _ = 0; _ < q; _++) {
    cin >> x >> basic;
    mp[x][basic]++;
    for (auto it : comp) {
      string c = it.first;
      int cnt = 0;
      for (auto iter : it.second) {
        string basicneeded = iter.first;
        int needed = iter.second;
        if (mp[x][basicneeded] < needed)
          break;
        else
          cnt++;
      }
      if (cnt == it.second.size()) {
        mp[x][c]++;
        for (auto i : comp[c]) {
          mp[x][i.first] -= i.second;
          if (mp[x][i.first] <= 0) mp[x].erase(i.first);
        }
      }
    }
  }
  for (int i = 1; i <= k; i++) {
    map<int, map<string, int>>::iterator it = mp.find(i);
    if (it != mp.end()) {
      int cnt = 0;
      for (auto iter : it->second)
        if (iter.second > 0) cnt++;
      cout << cnt << '\n';
      for (auto iter : it->second)
        if (iter.second > 0) cout << iter.first << " " << iter.second << '\n';
    } else
      cout << 0 << '\n';
  }
  return 0;
}
