#include <bits/stdc++.h>
using namespace std;
int main() {
  int k, n, m, q;
  map<string, int> basic;
  map<string, int> comp;
  map<int, string> basic_name;
  map<int, string> comp_name;
  int comp_detail[100][100] = {};
  int a[110][200] = {};
  cin >> k >> n >> m >> q;
  for (int i = 0; i < n; i++) {
    char str[1024];
    scanf("%s", str);
    basic_name[i] = string(str);
    basic[basic_name[i]] = i;
  }
  for (int i = 0; i < m; i++) {
    char str[1024];
    scanf("%s", str);
    str[strlen(str) - 1] = 0;
    comp_name[i] = string(str);
    comp[comp_name[i]] = i;
    while (true) {
      scanf("%s", str);
      int c;
      scanf("%d", &c);
      comp_detail[i][basic[string(str)]] += c;
      if (getchar() != ',') break;
    }
  }
  for (int i = 0; i < q; i++) {
    int id;
    scanf("%d", &id);
    id--;
    char str[1024];
    scanf("%s", str);
    int p = basic[string(str)];
    a[id][p]++;
    for (int j = 0; j < m; j++) {
      bool flag = true;
      for (int k = 0; k < n; k++)
        if (a[id][k] < comp_detail[j][k]) {
          flag = false;
          break;
        }
      if (flag) {
        for (int k = 0; k < n; k++) a[id][k] -= comp_detail[j][k];
        a[id][j + n]++;
        break;
      }
    }
  }
  for (int i = 0; i < k; i++) {
    int b = 0;
    for (int j = 0; j < n + m; j++)
      if (a[i][j] > 0) b++;
    printf("%d\n", b);
    map<string, int> ans;
    for (int j = 0; j < n; j++)
      if (a[i][j] > 0) ans[basic_name[j]] = a[i][j];
    for (int j = 0; j < m; j++)
      if (a[i][j + n] > 0) ans[comp_name[j]] = a[i][j + n];
    for (map<string, int>::iterator j = ans.begin(); j != ans.end(); j++) {
      printf("%s %d\n", j->first.c_str(), j->second);
    }
  }
  return 0;
}
