#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:64000000")
using namespace std;
template <typename T>
inline T SQR(T a) {
  return a * a;
}
template <typename T>
struct InfP {
  operator T() const {
    return std::numeric_limits<T>::has_infinity
               ? std::numeric_limits<T>::infinity()
               : std::numeric_limits<T>::max();
  }
};
struct Infinity : InfP<int>,
                  InfP<long long>,
                  InfP<double>,
                  InfP<long double>,
                  InfP<char>,
                  InfP<unsigned long long>,
                  InfP<unsigned int>,
                  InfP<float> {
} INF;
const double PI = 2 * asin(1.0);
void solve();
int main(int argc, char *argv[]) {
  ios::sync_with_stdio(false);
  solve();
  return 0;
}
const double eps = 1e-6;
const int maxn = 1e6;
void solve() {
  int k, n, m, q;
  cin >> k >> n >> m >> q;
  set<string> base;
  for (int i = 0; i < (int)(n); ++i) {
    string str;
    cin >> str;
    base.insert(str);
  }
  map<string, set<pair<string, int> > > msp;
  string str, tname;
  getline(cin, str);
  for (int i = 0; i < (int)(m); ++i) {
    getline(cin, str);
    stringstream ss(str);
    ss >> tname;
    tname = tname.substr(0, int((tname).size()) - 1);
    string nname;
    int cnt;
    char c;
    while (ss >> nname >> cnt) {
      msp[tname].insert(make_pair(nname, cnt));
      ss >> c;
    }
  }
  vector<map<string, int> > Pl(k);
  for (int i = 0; i < (int)(q); ++i) {
    int id;
    string name;
    cin >> id >> name;
    Pl[id - 1][name]++;
    map<string, int> &mk = Pl[id - 1];
    for (map<string, set<pair<string, int> > >::iterator it1 = msp.begin();
         it1 != msp.end(); ++it1) {
      set<pair<string, int> > &Set = it1->second;
      bool ok = true;
      for (set<pair<string, int> >::iterator it = Set.begin(); it != Set.end();
           ++it) {
        if (mk[it->first] < it->second) {
          ok = false;
          break;
        }
      }
      if (ok) {
        for (set<pair<string, int> >::iterator it = Set.begin();
             it != Set.end(); ++it) {
          mk[it->first] -= it->second;
        }
        mk[it1->first]++;
      }
    }
  }
  for (int i = 0; i < (int)(k); ++i) {
    map<string, int> &mkk = Pl[i];
    int p = 0;
    for (map<string, int>::iterator it = mkk.begin(); it != mkk.end(); ++it) {
      if (it->second > 0) p++;
    }
    cout << p << "\n";
    for (map<string, int>::iterator it = mkk.begin(); it != mkk.end(); ++it) {
      if (it->second > 0) cout << it->first << " " << it->second << "\n";
    }
  }
}
