#include <bits/stdc++.h>
using namespace std;
vector<pair<string, int> > comb[51];
map<string, int> mp[101];
string name[51];
int main() {
  int t, n, m, q, a;
  while (scanf("%d%d%d%d", &t, &n, &m, &q) != EOF) {
    char str[101];
    for (int i = 0; i <= t; i++) mp[i].clear();
    for (int i = 0; i < n; i++) scanf("%*s");
    for (int i = 0; i < m; i++) {
      comb[i].clear();
      scanf("%s", str);
      str[strlen(str) - 1] = '\0';
      name[i] = str;
      while (1) {
        scanf("%s", str);
        scanf("%d", &a);
        comb[i].push_back(make_pair((string)str, a));
        scanf("%c", &str[0]);
        if (str[0] != ',') break;
      }
    }
    for (int i = 0; i < q; i++) {
      scanf("%d", &a);
      scanf("%s", str);
      mp[a][(string)str]++;
      for (int j = 0; j < m; j++) {
        int f = 1;
        for (int k = 0; k < comb[j].size() && f; k++)
          if (mp[a].find(comb[j][k].first) == mp[a].end() ||
              mp[a][comb[j][k].first] < comb[j][k].second)
            f = 0;
        if (!f) continue;
        for (int k = 0; k < comb[j].size(); k++) {
          mp[a][comb[j][k].first] -= comb[j][k].second;
          if (mp[a][comb[j][k].first] == 0) mp[a].erase(comb[j][k].first);
        }
        mp[a][name[j]]++;
      }
    }
    for (int i = 1; i <= t; i++) {
      printf("%d\n", mp[i].size());
      for (map<string, int>::iterator it = mp[i].begin(); it != mp[i].end();
           ++it) {
        printf("%s %d\n", (it->first).c_str(), it->second);
      }
    }
  }
}
