#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:1024000000,1024000000")
template <class T>
bool scanff(T &ret) {
  char c;
  int sgn;
  T bit = 0.1;
  if (c = getchar(), c == EOF) return 0;
  while (c != '-' && c != '.' && (c < '0' || c > '9')) c = getchar();
  sgn = (c == '-') ? -1 : 1;
  ret = (c == '-') ? 0 : (c - '0');
  while (c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c - '0');
  if (c == ' ' || c == '\n') {
    ret *= sgn;
    return 1;
  }
  while (c = getchar(), c >= '0' && c <= '9') ret += (c - '0') * bit, bit /= 10;
  ret *= sgn;
  return 1;
}
using namespace std;
int k, n, m, q;
string art, st;
map<string, int> mp;
map<string, int> fr[111];
struct com {
  char gl[111];
  char cc[111][111];
  int num[111];
  int sum, realsum;
  void init() { realsum = sum = 0; }
} cp[66];
bool cmp(com a, com b) { return a.realsum < b.realsum; }
void makecp(int n) {
  int len = st.size();
  int pos = 0, js = 0, tot = 0;
  for (; st[pos] != ':'; pos++) {
    cp[n].gl[pos] = st[pos];
  }
  pos += 2;
  while (pos < len) {
    js = 0;
    for (; st[pos] != ' '; pos++, js++) {
      cp[n].cc[tot][js] = st[pos];
    }
    pos++;
    char zh[111];
    for (int j = 0; pos < len; pos++, j++) {
      if (st[pos] == ',')
        break;
      else {
        zh[j] = st[pos];
      }
    }
    cp[n].num[tot] = atoi(zh);
    cp[n].realsum += cp[n].num[tot];
    cp[n].sum++;
    pos += 2;
    tot++;
  }
}
void getans(int n) {
  int ans = 0;
  for (int i = 1; i <= m; i++) {
    int js = 1073741823, flag = 1;
    for (int j = 0; j <= cp[i].sum - 1; j++) {
      int sm = fr[n][cp[i].cc[j]];
      if (!fr[n][cp[i].cc[j]]) {
        flag = 0;
        break;
      } else {
        js = min(js, sm / cp[i].num[j]);
      }
    }
    if (flag == 1 && js != 0) {
      for (int j = 0; j <= cp[i].sum - 1; j++) {
        int sm = fr[n][cp[i].cc[j]];
        if (fr[n][cp[i].cc[j]]) {
          fr[n][cp[i].cc[j]] -= js * cp[i].num[j];
        }
      }
      fr[n][cp[i].gl] = js;
    }
  }
  map<string, int>::iterator it;
  for (it = fr[n].begin(); it != fr[n].end(); it++) {
    if ((it->second) != 0) ans++;
  }
  cout << ans << endl;
  for (it = fr[n].begin(); it != fr[n].end(); it++) {
    if ((it->second) != 0) cout << it->first << ' ' << it->second << endl;
  }
}
int main() {
  cin >> k >> n >> m >> q;
  for (int i = 1; i <= n; i++) {
    cin >> art;
    mp[art]++;
  }
  getchar();
  for (int i = 1; i <= 65; i++) cp[i].init();
  for (int i = 1; i <= m; i++) {
    getline(cin, st);
    makecp(i);
  }
  sort(cp + 1, cp + 1 + m, cmp);
  for (int i = 1; i <= q; i++) {
    int x;
    cin >> x >> st;
    fr[x][st]++;
  }
  for (int i = 1; i <= k; i++) getans(i);
  return 0;
}
