#include <bits/stdc++.h>
using namespace std;
const int maxN = 10500;
string nameByNum[maxN];
map<string, int> numByName;
char buff[100500];
vector<pair<int, int> > recipe[maxN];
map<int, int> human[maxN];
int main() {
  int k, n, m, q;
  scanf("%d%d%d%d", &k, &n, &m, &q);
  for (int i = 0; i < n; i++) {
    scanf("%s\n", buff);
    string name(buff);
    numByName[name] = i;
    nameByNum[i] = name;
  }
  int len = n;
  for (int i = 0; i < m; i++) {
    gets(buff);
    string S(buff);
    istringstream iss(S);
    string s;
    iss >> s;
    string name = s.substr(0, s.length() - 1);
    int Num = len++;
    numByName[name] = Num;
    nameByNum[Num] = name;
    while (iss >> name) {
      string d;
      iss >> d;
      int val;
      sscanf(d.c_str(), "%d", &val);
      int num = numByName[name];
      recipe[Num].push_back(make_pair(num, val));
    }
  }
  for (int i = 0; i < q; i++) {
    int hum;
    scanf("%d %s", &hum, buff);
    hum--;
    string name(buff);
    int num = numByName[name];
    map<int, int>& Human = human[hum];
    Human[num]++;
    for (int j = 0; j < m; j++) {
      int num = j + n;
      vector<pair<int, int> >& V = recipe[j + n];
      bool good = true;
      for (int t = 0; t < V.size(); t++) {
        if (Human.count(V[t].first) == 0 || Human[V[t].first] < V[t].second) {
          good = false;
          break;
        }
      }
      if (!good) continue;
      for (int t = 0; t < V.size(); t++) {
        Human[V[t].first] -= V[t].second;
        if (Human[V[t].first] == 0) {
          Human.erase(V[t].first);
        }
      }
      Human[num]++;
      break;
    }
  }
  for (int i = 0; i < k; i++) {
    printf("%d\n", human[i].size());
    vector<pair<string, int> > answer;
    answer.clear();
    for (map<int, int>::iterator it = human[i].begin(); it != human[i].end();
         it++) {
      answer.push_back(make_pair(nameByNum[it->first].c_str(), it->second));
    }
    sort(answer.begin(), answer.end());
    for (int i = 0; i < answer.size(); i++) {
      printf("%s %d\n", answer[i].first.c_str(), answer[i].second);
    }
  }
  return 0;
}
