#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:64000000")
using namespace std;
void __never(int a) { printf("\nOPS %d", a); }
int k, n, m, q;
map<string, int> base;
vector<string> base_name;
vector<vector<int> > comp;
vector<string> comp_name;
vector<int> bag[128];
char S[1000000];
void sol() {
  for (int a = (1); a <= (k); a++) bag[a].resize(128);
  for (int a = (1); a <= (q); a++) {
    int hero;
    string art;
    cin >> hero >> art;
    bag[hero][base[art]]++;
    for (int b = (0); b <= (m - 1); b++) {
      bool flag = true;
      for (int c = (0); c <= (n - 1); c++)
        if (bag[hero][c] < comp[b][c]) {
          flag = false;
          break;
        }
      if (flag) {
        for (int c = (0); c <= (n - 1); c++) bag[hero][c] -= comp[b][c];
        bag[hero][b + n]++;
        break;
      }
    }
  }
  for (int a = (1); a <= (k); a++) {
    vector<pair<string, int> > vec;
    for (int b = (0); b <= (n + m); b++)
      if (bag[a][b])
        vec.push_back(
            make_pair(b < n ? base_name[b] : comp_name[b - n], bag[a][b]));
    sort(vec.begin(), vec.end());
    printf("%d\n", (int)((vec).size()));
    for (int b = (0); b <= ((int)((vec).size()) - 1); b++)
      printf("%s %d\n", vec[b].first.c_str(), vec[b].second);
  }
}
int main() {
  cin >> k >> n >> m >> q;
  gets(S);
  for (int a = (0); a <= (n - 1); a++) {
    gets(S);
    string str = string(S);
    int tmp = (int)((base).size());
    base[str] = tmp;
    base_name.push_back(str);
  }
  for (int a = (0); a <= (m - 1); a++) {
    gets(S);
    stringstream ss;
    ss << S;
    string str;
    ss >> str;
    str = str.substr(0, (int)((str).size()) - 1);
    comp_name.push_back(str);
    vector<int> vec(n);
    while (ss >> str) {
      int i = base[str], j;
      ss >> str;
      sscanf(str.c_str(), "%d", &j);
      vec[i] = j;
    }
    comp.push_back(vec);
  }
  sol();
  return 0;
}
