#include <bits/stdc++.h>
using namespace std;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T>
inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
const double EPS = 1e-10;
const double PI = acos(-1.0);
std::vector<std::string> split(const std::string &str,
                               const std::string &delim) {
  std::vector<std::string> res;
  std::size_t current = 0, found, delimlen = delim.size();
  while ((found = str.find(delim, current)) != std::string::npos) {
    res.push_back(string(str, current, found - current));
    current = found + delimlen;
  }
  res.push_back(std::string(str, current, str.size() - current));
  return res;
}
int main(int argc, char *argv[]) {
  ios::sync_with_stdio(false);
  int k, n, m, q;
  cin >> k >> n >> m >> q;
  string tmp;
  getline(cin, tmp);
  vector<string> simple;
  map<string, map<string, int> > composite;
  for (int i = (0); i < (n); ++i) {
    string str;
    getline(cin, str);
    simple.push_back(str);
  }
  for (int i = (0); i < (m); ++i) {
    string str;
    getline(cin, str);
    map<string, int> art;
    vector<string> splited = split(str, ": ");
    vector<string> arts = split(splited[1], ", ");
    for (int j = (0); j < (int((arts).size())); ++j) {
      vector<string> fields = split(arts[j], " ");
      art[fields[0]] = toInt(fields[1]);
    }
    composite[splited[0]] = art;
  }
  vector<vector<string> > v(k + 1);
  for (int i = (0); i < (q); ++i) {
    string str;
    getline(cin, str);
    vector<string> splited = split(str, " ");
    v[toInt(splited[0])].push_back(splited[1]);
  }
  vector<map<string, int> > res(k + 1);
  for (int i = (0); i < (int((v).size())); ++i) {
    map<string, int> jz;
    for (int j = (0); j < (int((v[i]).size())); ++j) {
      jz[v[i][j]]++;
      for (typeof((composite).begin()) l = (composite).begin();
           l != (composite).end(); ++l) {
        map<string, int> s = l->second;
        bool flg = true;
        for (typeof((s).begin()) p = (s).begin(); p != (s).end(); ++p) {
          if (jz[p->first] < p->second) {
            flg = false;
          }
        }
        if (flg) {
          for (typeof((s).begin()) p = (s).begin(); p != (s).end(); ++p) {
            jz[p->first] -= p->second;
          }
          jz[l->first]++;
        }
      }
    }
    res[i] = jz;
  }
  for (int i = (1); i < (int((res).size())); ++i) {
    map<string, int> s;
    for (typeof((res[i]).begin()) j = (res[i]).begin(); j != (res[i]).end();
         ++j) {
      if (j->second > 0) {
        s[j->first] = j->second;
      }
    }
    cout << s.size() << endl;
    for (typeof((s).begin()) j = (s).begin(); j != (s).end(); ++j) {
      cout << j->first << " " << j->second << endl;
    }
  }
  return 0;
}
