#include <bits/stdc++.h>
using namespace std;
map<string, int> mp;
string name[110];
int a[110][110];
int n, m, k, kk;
int b[110][110];
inline int sti(string s) {
  int t = 0;
  for (int i = 0; i < s.size(); i++) t = t * 10 + (s[i] - '0');
  return t;
}
int main() {
  cin >> n >> m >> k >> kk;
  for (int i = 0; i < m; i++) {
    string s;
    cin >> s;
    mp[s] = i;
    name[i] = s;
  }
  cin.ignore();
  for (int i = 0; i < k; i++) {
    string s;
    getline(cin, s);
    int t = 0;
    for (int j = 0; j < s.size(); j++) {
      if (s[j] == ':') {
        string nam = s.substr(0, j);
        mp[nam] = m + i;
        name[m + i] = nam;
        t = j;
        break;
      }
    }
    s += ',';
    string ss = "";
    int tt;
    for (int j = t + 2; j < s.size(); j++) {
      if (s[j] == ',') {
        b[m + i][tt] = sti(ss);
        ss = "";
        j++;
      } else if (s[j] == ' ') {
        tt = mp[ss];
        ss = "";
      } else
        ss += s[j];
    }
  }
  for (int tr = 0; tr < kk; tr++) {
    string s;
    int x;
    cin >> x >> s;
    x--;
    a[x][mp[s]]++;
    for (int i = m; i < m + k; i++) {
      bool fl = 1;
      for (int j = 0; j < m; j++) {
        if (a[x][j] < b[i][j]) {
          fl = 0;
          break;
        }
      }
      if (fl) {
        for (int j = 0; j < m; j++) a[x][j] -= b[i][j];
        a[x][i]++;
        break;
      }
    }
  }
  for (int i = 0; i < n; i++) {
    int t = 0;
    for (int j = 0; j < m + k; j++)
      if (a[i][j]) t++;
    cout << t << "\n";
    for (map<string, int>::iterator it = mp.begin(); it != mp.end(); it++) {
      int j = it->second;
      if (a[i][j]) cout << it->first << " " << a[i][j] << "\n";
    }
  }
  return 0;
}
