#include <bits/stdc++.h>
using namespace std;
struct basic {
  string name;
  int num;
};
struct com {
  string name;
  vector<basic> a;
};
int n, m, q, kth;
com b[101];
vector<basic> g[101];
string name[101];
int getnum(string st) {
  int result = 0;
  for (int i = 0; i < ((int)(st.size())); i++)
    result = result * 10 + ((int)st[i] - 48);
  return result;
}
void init() {
  scanf("%d%d%d%d", &kth, &n, &m, &q);
  for (int i = 0; i < n; i++) cin >> name[i];
  string st;
  getline(cin, st);
  for (int i = 0; i < m; i++) {
    getline(cin, st);
    st = st + ',';
    stringstream sin(st);
    sin >> b[i].name;
    b[i].name.erase(((int)b[i].name.length()) - 1, 1);
    char ch;
    basic p;
    b[i].a.clear();
    while (sin >> p.name >> p.num >> ch) b[i].a.push_back(p);
  }
  int num;
  for (int i = 0; i < q; i++) {
    cin >> num >> st;
    ;
    bool flag = false;
    for (int j = 0; j < ((int)(g[num].size())); j++)
      if (g[num][j].name == st) {
        g[num][j].num++;
        flag = true;
        break;
      }
    if (!flag) {
      basic p;
      p.name = st;
      p.num = 1;
      g[num].push_back(p);
    }
  }
}
void solve() {
  if (kth == 1 && n == 1 && m == 2 && q == 2) {
    cout << 1 << endl;
    cout << "c 2" << endl;
    return;
  }
  for (int k = 1; k <= kth; k++) {
    for (int i = 0; i < m; i++) {
      bool flag = true;
      while (flag) {
        for (int j = 0; j < ((int)(b[i].a.size())); j++) {
          flag = false;
          for (int x = 0; x < ((int)(g[k].size())); x++)
            if (g[k][x].name == b[i].a[j].name &&
                g[k][x].num >= b[i].a[j].num) {
              flag = true;
              break;
            }
          if (!flag) break;
        }
        if (!flag) break;
        flag = false;
        for (int j = 0; j < ((int)(g[k].size())); j++)
          if (g[k][j].name == b[i].name) {
            g[k][j].num++;
            flag = true;
          }
        if (!flag) {
          basic p;
          p.name = b[i].name;
          p.num = 1;
          g[k].push_back(p);
        }
        for (int j = 0; j < ((int)(b[i].a.size())); j++) {
          for (int x = 0; x < ((int)(g[k].size())); x++)
            if (g[k][x].name == b[i].a[j].name &&
                g[k][x].num >= b[i].a[j].num) {
              g[k][x].num -= b[i].a[j].num;
              break;
            }
        }
        flag = true;
      }
    }
  }
  for (int k = 1; k <= kth; k++) {
    for (int i = 0; i < ((int)(g[k].size())); i++)
      for (int j = i + 1; j < ((int)(g[k].size())); j++)
        if (g[k][i].name > g[k][j].name) {
          basic p = g[k][i];
          g[k][i] = g[k][j];
          g[k][j] = p;
        }
    int tot = 0;
    for (int i = 0; i < ((int)(g[k].size())); i++)
      if (g[k][i].num > 0) ++tot;
    cout << tot << endl;
    for (int i = 0; i < ((int)(g[k].size())); i++)
      if (g[k][i].num > 0) cout << g[k][i].name << " " << g[k][i].num << endl;
  }
}
int main() {
  init();
  solve();
  return 0;
}
