#include <bits/stdc++.h>
using namespace std;
unsigned long long summ(unsigned long long n) { return ((n * n) + n) / 2; }
unsigned long long gcd(unsigned long long a, unsigned long long b) {
  unsigned long long c;
  while (a != 0) {
    c = a;
    a = b % a;
    b = c;
  }
  return b;
}
double dist(long long x, long long y, long long xx, long long yy) {
  return sqrt((x - xx) * (x - xx) + (y - yy) * (y - yy));
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, m, k, q;
  while (cin >> k >> n >> m >> q) {
    map<string, int> fri[k];
    map<string, int> tm[m];
    string s, nam[m], tmp;
    for (int i = 0; i < n; i++) cin >> s;
    for (int i = 0; i < m; i++) {
      cin >> s;
      nam[i] = s.substr(0, s.size() - 1);
      do {
        cin >> s >> tmp;
        tm[i][s] +=
            stoi(tmp.substr(0, tmp.size() - (tmp[tmp.size() - 1] == ',')));
      } while (tmp[tmp.size() - 1] == ',');
    }
    int ha;
    for (int i = 0; i < q; i++) {
      cin >> ha >> s;
      ha--;
      fri[ha][s]++;
      for (int j = 0; j < m; j++) {
        bool ma = 1;
        for (auto p : tm[j]) {
          if (fri[ha][p.first] < p.second) {
            ma = 0;
            break;
          }
        }
        if (ma) {
          for (auto p : tm[j]) {
            fri[ha][p.first] -= p.second;
          }
          fri[ha][nam[j]]++;
          break;
        }
      }
    }
    vector<pair<string, int> > ss;
    for (int i = 0; i < k; i++) {
      ss.clear();
      for (auto p : fri[i]) {
        if (p.second) {
          ss.push_back(p);
        }
      }
      sort(ss.begin(), ss.end());
      cout << ss.size() << "\n";
      for (int j = 0; j < ss.size(); j++) {
        cout << ss[j].first << " " << ss[j].second << "\n";
      }
    }
  }
  return 0;
}
