#include <bits/stdc++.h>
using namespace std;
int K, N, M, Q;
string s;
map<string, int> mp, F[110];
string ZuZh[110];
vector<pair<string, int> > ZuPart[110];
int main(void) {
  while (scanf("%d%d%d%d", &K, &N, &M, &Q) == 4) {
    mp.clear();
    for (int i = 1; i <= N; i++) {
      cin >> s;
      mp[s] = i;
    }
    for (int i = 0; i < M; i++) {
      ZuPart[i].clear();
      cin >> s;
      s = s.substr(0, s.size() - 1);
      ZuZh[i] = s;
      while (cin >> s) {
        int nt;
        cin >> nt;
        ZuPart[i].push_back(make_pair(s, nt));
        char c;
        scanf("%c", &c);
        if (c != ',') break;
      }
    }
    for (int i = 1; i <= N; i++) F[i].clear();
    for (int i = 0; i < Q; i++) {
      int a;
      cin >> a >> s;
      F[a][s]++;
      for (int j = 0; j < M; j++) {
        s = ZuZh[j];
        bool have = 1;
        for (int k = 0; k < ZuPart[j].size(); k++) {
          if (F[a][ZuPart[j][k].first] < ZuPart[j][k].second) have = 0;
        }
        if (have) {
          F[a][s]++;
          for (int k = 0; k < ZuPart[j].size(); k++) {
            F[a][ZuPart[j][k].first] -= ZuPart[j][k].second;
          }
        }
      }
    }
    for (int i = 1; i <= K; i++) {
      int cnt = 0;
      for (map<string, int>::iterator it = F[i].begin(); it != F[i].end();
           it++) {
        if (it->second > 0) cnt++;
      }
      cout << cnt << endl;
      for (map<string, int>::iterator it = F[i].begin(); it != F[i].end();
           it++) {
        if (it->second > 0) cout << it->first << " " << it->second << endl;
      }
    }
  }
  return 0;
}
