#include <bits/stdc++.h>
using namespace std;
void Fastio() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
long long fact[1000006], inv[1000006];
long long fp(long long base, long long exp) {
  if (exp == 0) return 1;
  long long ans = fp(base, exp / 2);
  ans = (ans * ans) % 998244353;
  if (exp % 2 != 0) ans = (ans * (base % 998244353)) % 998244353;
  return ans;
}
void calcFacAndInv(long long n) {
  fact[0] = inv[0] = 1;
  for (long long i = 1; i <= n; i++) {
    fact[i] = (i * fact[i - 1]);
    inv[i] = fp(fact[i], 998244353 - 2);
  }
}
long long ncr(long long n, long long r) {
  return ((fact[n] * inv[r]) % 998244353 * inv[n - r]) % 998244353;
}
long long npr(long long n, long long r) {
  return (fact[n] * inv[n - r]) % 998244353;
}
long double NCR(int n, int m) {
  long double res = 1;
  m = min(m, n - m);
  for (int i = 0; i < m; i++) {
    res = res * (n - i) / (i + 1);
  }
  return res;
}
long long tol(string a) {
  long long res = 0;
  while (a[0] == '0') {
    a.erase(a.begin());
  }
  if (a == "") a = "0";
  for (int i = 0; i < a.size(); i++) {
    res *= 10;
    res += a[i] - '0';
  }
  return res;
}
int check(string a, string b) {
  for (int i = 0; i < a.size(); i++) {
    if (a[i] > b[i]) return 1;
    if (b[i] > a[i]) return -1;
  }
  return 0;
}
class data {
 public:
  long long v, d, p;
  data() {}
  data(long long vv, long long dd, long long pp) {
    v = vv;
    d = dd;
    p = pp;
  }
};
int primeFactors(int n) {
  long long c = 0;
  while (n % 2 == 0) {
    c++;
    n = n / 2;
  }
  for (int i = 3; i * i <= n; i = i + 2) {
    while (n % i == 0) {
      c++;
      n = n / i;
    }
  }
  if (n > 2) c++;
  return c;
}
pair<string, vector<pair<string, int> > > split(string a) {
  int i;
  pair<string, vector<pair<string, int> > > res;
  string s = "";
  for (i = 0; i < a.size(); i++) {
    if (a[i] == ':') break;
    s += a[i];
  }
  res.first = s;
  s = "";
  i += 2;
  while (i < a.size()) {
    for (i; i < a.size(); i++) {
      if (a[i] <= '9' && a[i] >= '0') break;
      s += a[i];
    }
    s.erase(s.end() - 1);
    int num = 0;
    for (i; i < a.size(); i++) {
      if (a[i] == ',') break;
      num *= 10;
      num += a[i] - '0';
    }
    i += 2;
    res.second.push_back({s, num});
    s = "";
  }
  return res;
}
int main() {
  Fastio();
  int n, p, s, pu;
  cin >> n >> p >> s >> pu;
  map<string, int> pp;
  string names[p];
  for (int i = 0; i < p; i++) {
    string a;
    cin >> a;
    names[i] = a;
    pp[a] = i;
  }
  cin.ignore();
  vector<pair<string, vector<pair<string, int> > > > se;
  for (int i = 0; i < s; i++) {
    string a;
    getline(cin, a);
    pair<string, vector<pair<string, int> > > res;
    res = split(a);
    se.push_back(res);
  }
  int arr[n][p + s];
  memset(arr, 0, sizeof(arr));
  for (int j = 0; j < pu; j++) {
    int pos;
    cin >> pos;
    pos--;
    string a;
    cin >> a;
    arr[pos][pp[a]]++;
    int c = 0;
    for (auto i : se) {
      bool st = 1;
      c++;
      for (int y = 0; y < i.second.size(); y++) {
        string pr = i.second[y].first;
        int ac = i.second[y].second;
        if (arr[pos][pp[pr]] < ac) {
          st = 0;
          break;
        }
      }
      if (!st) continue;
      for (int y = 0; y < i.second.size(); y++) {
        string pr = i.second[y].first;
        int ac = i.second[y].second;
        arr[pos][pp[pr]] -= ac;
      }
      arr[pos][p + c - 1]++;
    }
  }
  for (int i = 0; i < n; i++) {
    vector<pair<string, int> > v;
    for (int y = 0; y < p; y++) {
      if (arr[i][y]) v.push_back({names[y], arr[i][y]});
    }
    for (int y = 0; y < s; y++) {
      if (arr[i][p + y]) v.push_back({se[y].first, arr[i][p + y]});
    }
    cout << v.size() << '\n';
    sort(v.begin(), v.end());
    for (int y = 0; y < v.size(); y++) {
      cout << v[y].first << " " << v[y].second << '\n';
    }
  }
  return 0;
}
