#include <bits/stdc++.h>
using namespace std;
int ri() {
  int r;
  cin >> r;
  return r;
}
unsigned int rui() {
  unsigned int r;
  cin >> r;
  return r;
}
long long rl() {
  long long r;
  cin >> r;
  return r;
}
unsigned long long rul() {
  unsigned long long r;
  cin >> r;
  return r;
}
double rd() {
  double r;
  cin >> r;
  return r;
}
string rss() {
  string r;
  cin >> r;
  return r;
}
string rs() {
  string r;
  getline(cin, r);
  return r;
}
vector<string> rvs(int n) {
  assert(n);
  vector<string> r;
  for (int i = 0; i < n; ++i) {
    string s = rs();
    while (s.empty()) s = rs();
    r.push_back(s);
  }
  return r;
}
vector<int> rvi(int n) {
  assert(n);
  vector<int> r;
  for (int i = 0; i < n; ++i) r.push_back(ri());
  return r;
}
vector<unsigned int> rvui(int n) {
  assert(n);
  vector<unsigned int> r;
  for (int i = 0; i < n; ++i) r.push_back(rui());
  return r;
}
vector<long long> rvl(int n) {
  assert(n);
  vector<long long> r;
  for (int i = 0; i < n; ++i) r.push_back(rl());
  return r;
}
vector<unsigned long long> rvul(int n) {
  assert(n);
  vector<unsigned long long> r;
  for (int i = 0; i < n; ++i) r.push_back(rul());
  return r;
}
vector<double> rvd(int n) {
  assert(n);
  vector<double> r;
  for (int i = 0; i < n; ++i) r.push_back(rd());
  return r;
}
map<string, int> parse(string s) {
  map<string, int> r;
  while (!s.empty()) {
    auto p = s.find(',');
    auto cur = s.substr(0, p);
    if (p == s.npos)
      s.clear();
    else
      s.erase(0, p + 1);
    istringstream strm(cur);
    string n;
    int v;
    strm >> n >> v;
    r[n] = v;
  }
  return r;
}
void check(map<string, int>& fr, const map<string, map<string, int>>& comp) {
  for (auto it = comp.begin(); it != comp.end(); ++it) {
    bool g = true;
    for (auto it2 = it->second.begin(); it2 != it->second.end(); ++it2) {
      auto it3 = fr.find(it2->first);
      if (it3 == fr.end() || it3->second < it2->second) {
        g = false;
        break;
      }
    }
    if (g) {
      for (auto it2 = it->second.begin(); it2 != it->second.end(); ++it2) {
        fr[it2->first] -= it2->second;
        if (fr[it2->first] == 0) fr.erase(it2->first);
      }
      ++fr[it->first];
      break;
    }
  }
}
int main() {
  int k = ri(), n = ri(), m = ri(), q = ri();
  rs();
  set<string> bas;
  for (int i = 0; i < n; ++i) bas.insert(rs());
  map<string, map<string, int>> comp;
  for (int i = 0; i < m; ++i) {
    string n;
    getline(cin, n, ':');
    auto r = rs();
    comp[n] = parse(r);
  }
  vector<map<string, int>> v(k);
  for (int i = 0; i < q; ++i) {
    int f = ri() - 1;
    auto a = rss();
    ++v[f][a];
    check(v[f], comp);
  }
  for (int i = 0; i < v.size(); ++i) {
    cout << v[i].size() << endl;
    for (auto it = v[i].begin(); it != v[i].end(); ++it)
      cout << it->first << " " << it->second << endl;
  }
  return 0;
}
