#include <bits/stdc++.h>
const int oo = 0x3f3f3f3f, mod = 1e9 + 7;
using namespace std;
int dx[]{1, -1, 0, 0, 1, 1, -1, -1};
int dy[]{0, 0, 1, -1, 1, -1, 1, -1};
long long OO = (1LL * oo * oo);
void K_K() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
long long gcd(long long a, long long b) {
  return b == 0 ? abs(a) : gcd(b, a % b);
}
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
int sToL(string s) {
  int x = 0;
  for (int i = 0; i < (int)(s.size()); i++) {
    x *= 10;
    x += s[i] - '0';
  }
  return x;
}
int main() {
  K_K();
  int k, n, m, q, d = 1;
  cin >> k >> n >> m >> q;
  map<pair<int, string>, vector<pair<string, int>>> mp;
  string s;
  for (int i = 0; i < n; i++) cin >> s;
  cin.ignore();
  string r = "";
  for (int i = 0; i < m; i++) {
    getline(cin, s);
    vector<string> v;
    for (int i = 0; i < (int)(s.size()); i++) {
      if (s[i] == ':')
        v.push_back(r), i++, r = "";
      else if (s[i] == ' ' || s[i] == ',') {
        if ((int)(r.size())) v.push_back(r);
        r = "";
      } else
        r += s[i];
    }
    if (r != "") v.push_back(r), r = "";
    for (int i = 1; i + 1 < (int)(v.size()); i += 2)
      mp[{d, v[0]}].push_back({v[i], sToL(v[i + 1])});
    d++;
  }
  vector<pair<int, string>> v(q);
  for (int i = 0; i < q; i++) cin >> v[i].first >> v[i].second;
  sort(v.begin(), v.end());
  unordered_map<int, set<pair<string, int>>> mpst;
  int c = 1;
  for (int i = 0; i < q;) {
    map<string, int> mp1;
    set<pair<string, int>> st;
    pair<string, int> ans{"", 0};
    vector<pair<string, int>> ans1;
    while (i < q && c == v[i].first) mp1[v[i].second]++, i++;
    for (auto it : mp) {
      int cnt = oo;
      for (int i = 0; i < (int)(it.second.size()); i++) {
        cnt = min(cnt, mp1[it.second[i].first] / it.second[i].second);
      }
      if (cnt > ans.second) {
        ans.first = it.first.second;
        ans.second = cnt;
        ans1.clear();
        ans1 = it.second;
      }
    }
    if (ans.second) {
      for (int i = 0; i < (int)(ans1.size()); i++)
        mp1[ans1[i].first] -= ans1[i].second * ans.second;
      st.insert({ans.first, ans.second});
    }
    for (auto it : mp1) {
      if (it.second) st.insert({it.first, it.second});
    }
    if ((int)(st.size())) {
      mpst[c] = st;
    }
    if (i < q) c = v[i].first;
  }
  for (int i = 1; i <= k; i++) {
    cout << (int)(mpst[i].size()) << "\n";
    for (auto itt : mpst[i]) cout << itt.first << " " << itt.second << "\n";
  }
}
