#include <bits/stdc++.h>
using namespace std;
class node {
 public:
  string s;
  int x;
};
bool operator<(const node &a, const node &b) {
  if (a.s != b.s) return a.s < b.s;
  return a.x < b.x;
}
vector<string> b;
vector<string> comp;
vector<vector<string> > manu;
vector<vector<int> > num;
map<pair<int, string>, int> mm;
pair<int, string> p;
set<node> ss;
node tmp;
int con(string s) {
  int x;
  sscanf(s.c_str(), "%d", &x);
  return x;
}
int main() {
  int k, n, m, q;
  scanf("%d %d %d %d", &k, &n, &m, &q);
  manu.resize(m + 2);
  num.resize(m + 2);
  for (int i = 0; i < n; i++) {
    char s[105];
    scanf("%s", s);
    b.push_back(s);
  }
  scanf("\n");
  for (int i = 0; i < m; i++) {
    char c;
    string s = "";
    while (scanf("%c", &c)) {
      if (c == ':') {
        comp.push_back(s);
        s = "";
      } else if (c == ' ') {
        if (s.size() == 0) continue;
        manu[i].push_back(s);
        s = "";
      } else if (c == ',') {
        num[i].push_back(con(s));
        s = "";
      } else {
        if (c != '\n')
          s += c;
        else
          break;
      }
    }
    num[i].push_back(con(s));
    s = "";
  }
  for (int i = 0; i < q; i++) {
    int id;
    char sd[105];
    scanf("%d %s", &id, sd);
    id--;
    p = make_pair(id, sd);
    mm[p]++;
    for (int j = 0; j < m; j++) {
      int z;
      for (z = 0; z < manu[j].size(); z++) {
        p = make_pair(id, manu[j][z]);
        if (mm[p] < num[j][z]) break;
      }
      if (z == manu[j].size()) {
        for (z = 0; z < manu[j].size(); z++) {
          p = make_pair(id, manu[j][z]);
          mm[p] -= num[j][z];
        }
        p = make_pair(id, comp[j]);
        mm[p]++;
      }
    }
  }
  for (int i = 0; i < k; i++) {
    for (int j = 0; j < n; j++) {
      p = make_pair(i, b[j]);
      if (mm[p]) {
        tmp.s = b[j];
        tmp.x = mm[p];
        ss.insert(tmp);
      }
    }
    for (int j = 0; j < m; j++) {
      p = make_pair(i, comp[j]);
      if (mm[p]) {
        tmp.s = comp[j];
        tmp.x = mm[p];
        ss.insert(tmp);
      }
    }
    int sz = ss.size();
    printf("%d\n", sz);
    for (set<node>::iterator it = ss.begin(); it != ss.end(); it++) {
      tmp = *it;
      printf("%s %d\n", tmp.s.c_str(), tmp.x);
    }
    ss.clear();
  }
}
