#include <bits/stdc++.h>
const long long N = 1e6 + 10;
const long long M = 100000;
const long long mod = 1e9 + 7;
using namespace std;
long long dpx[1005][1005];
long long ax[2005];
long long bx[2005];
long long add(long long a, long long b) { return ((a % mod + b % mod) % mod); }
long long sub(long long a, long long b) {
  return ((a % mod - b % mod + mod) % mod);
}
long long mul(long long a, long long b) {
  return (((a % mod) * (b % mod)) % mod);
}
map<string, long long> mp;
string second;
vector<long long> v;
int32_t main() {
  long long k, n, m, q;
  cin >> k >> n >> m >> q;
  long long cnt = 0;
  vector<string> name;
  for (long long i = 0; i < n; i++) {
    string second;
    cin >> second;
    mp[second] = cnt++;
    name.push_back(second);
  }
  vector<vector<pair<long long, long long>>> b(m + 1);
  cin.ignore();
  for (long long i = 0; i < m; i++) {
    string second;
    getline(cin, second, ':');
    mp[second] = cnt++;
    name.push_back(second);
    getline(cin, second);
    stringstream sin(second);
    long long u;
    while (sin >> second >> u) {
      char null;
      sin >> null;
      b[i].push_back({mp[second], u});
    }
  }
  vector<vector<long long>> ax(k + 1, vector<long long>(n + m + 1));
  for (long long i = 0; i < q; i++) {
    long long u;
    string second;
    cin >> u >> second;
    ax[u][mp[second]]++;
    for (long long j = 0; j < m; j++) {
      long long flag = 1;
      for (long long k = 0; k < b[j].size(); k++)
        if (ax[u][b[j][k].first] < b[j][k].second) {
          flag = 0;
          break;
        }
      if (flag) {
        for (long long k = 0; k < b[j].size(); k++)
          ax[u][b[j][k].first] -= b[j][k].second;
        ax[u][j + n]++;
        break;
      }
    }
  }
  vector<pair<string, long long>> ans;
  for (long long i = 1; i <= k; i++) {
    ans.clear();
    for (long long j = 0; j < n + m; j++) {
      if (ax[i][j]) ans.push_back({name[j], ax[i][j]});
    }
    sort(ans.begin(), ans.end());
    cout << ans.size() << "\n";
    for (auto i : ans) cout << i.first << " " << i.second << "\n";
  }
}
