#include <bits/stdc++.h>
const double eps = (1e-10);
using namespace std;
int dcmp(long double a, long double b) {
  return fabsl(a - b) <= eps ? 0 : (a > b) ? 1 : -1;
}
int getBit(int num, int idx) { return ((num >> idx) & 1) == 1; }
int setBit1(int num, int idx) { return num | (1 << idx); }
long long setBit0(long long num, int idx) { return num & ~(1ll << idx); }
long long flipBit(long long num, int idx) { return num ^ (1ll << idx); }
void M() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
int countNumBit1(int mask) {
  int ret = 0;
  while (mask) {
    mask &= (mask - 1);
    ++ret;
  }
  return ret;
}
int k, m, n, q, arr[109][109];
map<string, int> mb;
vector<pair<int, string> > ans[109];
vector<pair<string, int> > ans2[109];
map<string, vector<pair<string, string> > > v;
vector<string> comp, bc;
int main() {
  cin >> k >> n >> m >> q;
  string st;
  for (int i = 0; i < n; i++) {
    cin >> st;
    bc.push_back(st);
    mb[st] = i + 1;
  }
  string line, sst;
  cin.ignore();
  for (int i = 0; i < m; i++) {
    getline(cin, line);
    stringstream ss(line);
    string art;
    ss >> art;
    art.erase(art.end() - 1);
    string st, no;
    while (ss >> st >> no) {
      if (no[((int)(no).size()) - 1] == ',') no.erase(no.end() - 1);
      v[art].push_back(make_pair(st, no));
    }
    comp.push_back(art);
  }
  int num;
  while (q--) {
    cin >> num >> st;
    if (mb[st] == 0) continue;
    arr[num][mb[st]]++;
  }
  for (int i = 1; i <= k; i++) {
    for (int j = 0; j < m; j++) {
      string com = comp[j];
      int mi = INT_MAX;
      for (int l = 0; l < ((int)(v[com]).size()); l++) {
        string basic = v[com][l].first;
        int num = atoi(v[com][l].second.c_str());
        mi = min(mi, arr[i][mb[basic]] / num);
      }
      if (mi != 0) ans[i].push_back(make_pair(mi, com));
    }
  }
  for (int i = 1; i <= k; i++) {
    sort(((ans[i]).rbegin()), ((ans[i]).rend()));
    for (int j = 0; j < ((int)(ans[i]).size()); j++) {
      string com = ans[i][j].second;
      int mi = INT_MAX;
      for (int l = 0; l < ((int)(v[com]).size()); l++) {
        string basic = v[com][l].first;
        int num = atoi(v[com][l].second.c_str());
        mi = min(mi, arr[i][mb[basic]] / num);
      }
      for (int l = 0; l < ((int)(v[com]).size()); l++) {
        string basic = v[com][l].first;
        int num = atoi(v[com][l].second.c_str());
        arr[i][mb[basic]] -= mi * num;
      }
      if (mi != 0) ans2[i].push_back(make_pair(com, mi));
    }
  }
  for (int i = 1; i <= k; i++) {
    for (int j = 0; j < n; j++) {
      if (arr[i][mb[bc[j]]] > 0)
        ans2[i].push_back(make_pair(bc[j], arr[i][mb[bc[j]]]));
    }
  }
  for (int i = 1; i <= k; i++) sort(((ans2[i]).begin()), ((ans2[i]).end()));
  for (int i = 1; i <= k; i++) {
    cout << ((int)(ans2[i]).size()) << endl;
    for (int j = 0; j < ((int)(ans2[i]).size()); j++) {
      cout << ans2[i][j].first << " " << ans2[i][j].second << endl;
    }
  }
}
