#include <bits/stdc++.h>
using std::abs;
using std::bitset;
using std::cerr;
using std::cin;
using std::copy;
using std::cout;
using std::deque;
using std::endl;
using std::fill;
using std::fixed;
using std::greater;
using std::iota;
using std::istream;
using std::lower_bound;
using std::make_pair;
using std::make_tuple;
using std::map;
using std::max;
using std::max_element;
using std::min;
using std::min_element;
using std::multiset;
using std::next_permutation;
using std::nth_element;
using std::ostream;
using std::pair;
using std::priority_queue;
using std::queue;
using std::reverse;
using std::rotate;
using std::set;
using std::sort;
using std::string;
using std::swap;
using std::tie;
using std::tuple;
using std::unique;
using std::unordered_map;
using std::unordered_set;
using std::upper_bound;
using std::vector;
long long rnd(long long x, long long y) {
  static auto gen = std::bind(std::uniform_int_distribution<long long>(),
                              std::mt19937(960172));
  return gen() % (y - x + 1) + x;
}
long long gcd(long long a, long long b) {
  while (b > 0) {
    long long t = a % b;
    a = b;
    b = t;
  }
  return a;
}
template <typename T>
T sqr(T const& a) {
  return a * a;
}
int const INF = 100 + (int)1e9;
long long const INFL = 100 + (long long)1e18;
long double const PI = 3.1415926535897932384626433832795028;
void solve() {
  int k, n, m, q;
  cin >> k >> n >> m >> q;
  for (int i = 0; i < n; ++i) {
    string b;
    cin >> b;
  }
  {
    string temp;
    getline(cin, temp);
  }
  map<string, vector<pair<string, int>>> comps;
  for (int it = 0; it < m; ++it) {
    string s;
    getline(cin, s);
    s += ',';
    int i = 0;
    while (s[i] != ':') ++i;
    string art(s.begin(), s.begin() + i);
    i += 2;
    while (i < (int)s.size()) {
      int j = i;
      while (s[j] != ' ') ++j;
      string base(s.begin() + i, s.begin() + j);
      ++j;
      i = j;
      while (s[j] != ',') ++j;
      int cnt = atoi(string(s.begin() + i, s.begin() + j).data());
      i = j + 2;
      comps[art].emplace_back(base, cnt);
    }
  }
  vector<map<string, int>> cnts(k);
  for (int it = 0; it < q; ++it) {
    int fr;
    string base;
    cin >> fr >> base;
    --fr;
    auto& cnt = cnts[fr];
    ++cnt[base];
    string comp;
    for (auto p : comps) {
      bool can = true;
      for (auto q : p.second) {
        can &= cnt[q.first] >= q.second;
      }
      if (can) {
        comp = p.first;
        break;
      }
    }
    if (!comp.empty()) {
      ++cnt[comp];
      for (auto q : comps[comp]) {
        cnt[q.first] -= q.second;
      }
    }
  }
  for (int i = 0; i < k; ++i) {
    vector<pair<string, int>> cur;
    for (auto p : cnts[i]) {
      if (p.second) {
        cur.push_back(p);
      }
    }
    printf("%d\n", cur.size());
    for (auto p : cur) {
      printf("%s %d\n", p.first.data(), p.second);
    }
  }
}
int main() {
  cout.precision(15);
  cout << fixed;
  cerr.precision(6);
  cerr << fixed;
  solve();
}
