#include <bits/stdc++.h>
using namespace std;
string name1[200];
string name2[200];
vector<pair<int, int> > a[200];
map<string, int> ind1;
map<string, int> ind2;
vector<pair<int, int> > aa;
map<pair<int, int>, int> d[200];
char s[10000];
int main() {
  int i, j, k, m, n, q, o, p, b1, x;
  string s1, s2, ss;
  scanf("%d", &k);
  scanf("%d", &n);
  scanf("%d", &m);
  scanf("%d", &q);
  ind1.clear();
  for (i = 0; i < n; i++) {
    scanf("%s", s);
    name1[i] = s;
    ind1[s] = i;
  }
  gets(s);
  ind2.clear();
  for (i = 0; i < m; i++) {
    gets(s);
    ss = s;
    p = ss.find_first_of(':');
    name2[i] = ss.substr(0, p);
    ind2[name2[i]] = i;
    ss.erase(0, p + 2);
    ss += ", ";
    aa.clear();
    while (ss != "") {
      p = ss.find_first_of(' ');
      s1 = ss.substr(0, p);
      ss.erase(0, p + 1);
      p = ss.find_first_of(',');
      s2 = ss.substr(0, p);
      ss.erase(0, p + 2);
      aa.push_back(make_pair(ind1[s1], atoi(s2.c_str())));
    }
    a[i] = aa;
  }
  for (i = 0; i < k; i++) d[i].clear();
  for (i = 0; i < q; i++) {
    scanf("%d", &x);
    x--;
    scanf("%s", s);
    d[x][make_pair(0, ind1[s])]++;
    for (j = 0; j < m; j++) {
      b1 = 1;
      for (o = 0; o < a[j].size(); o++)
        if (d[x][make_pair(0, a[j][o].first)] < a[j][o].second) b1 = 0;
      if (b1 == 1) {
        for (o = 0; o < a[j].size(); o++)
          d[x][make_pair(0, a[j][o].first)] -= a[j][o].second;
        d[x][make_pair(1, j)]++;
      }
    }
  }
  for (i = 0; i < k; i++) {
    vector<pair<string, int> > ans;
    ans.clear();
    for (map<pair<int, int>, int>::iterator it = d[i].begin(); it != d[i].end();
         it++) {
      if (it->second != 0) {
        if (it->first.first == 0)
          ans.push_back(make_pair(name1[it->first.second], it->second));
        else
          ans.push_back(make_pair(name2[it->first.second], it->second));
      }
    }
    sort(ans.begin(), ans.end());
    printf("%d\n", ans.size());
    for (j = 0; j < ans.size(); j++)
      printf("%s %d\n", ans[j].first.c_str(), ans[j].second);
  }
  return 0;
}
