#include <bits/stdc++.h>
using namespace std;
struct DATA {
  string Name;
  long C;
  DATA(string s, long c) {
    Name = s;
    C = c;
  }
};
bool operator<(const DATA &a, const DATA &b) { return a.Name < b.Name; }
long K, N, M, Q;
string Name[77 + 77 + 7];
map<string, long> Ind;
vector<long> Comp[77 + 77 + 7];
vector<long> Tot[77 + 77 + 7];
long Count[777 + 7][77 + 77 + 7];
char Str[777777];
void UpDate(long f) {
  long i, j;
  for (i = N + 1; i <= N + M; i++) {
    for (j = 0; j < Tot[i].size(); j++) {
      if (Count[f][Comp[i][j]] < Tot[i][j]) break;
    }
    if (j < Tot[i].size()) continue;
    Count[f][i]++;
    for (j = 0; j < Tot[i].size(); j++) {
      Count[f][Comp[i][j]] -= Tot[i][j];
    }
  }
}
int main(void) {
  long i, j, f, v;
  char Ch;
  scanf("%ld%ld%ld%ld", &K, &N, &M, &Q);
  for (i = 1; i <= N; i++) {
    cin >> Name[i];
    Ind[Name[i]] = i;
  }
  for (i = N + 1; i <= N + M; i++) {
    cin >> Str;
    Str[strlen(Str) - 1] = '\0';
    Name[i] = Str;
    Ind[Str] = i;
    gets(Str);
    char *p = strtok(Str, ",");
    while (p) {
      sscanf(p, "%s%ld", Str, &v);
      Comp[i].push_back(Ind[Str]);
      Tot[i].push_back(v);
      p = strtok(NULL, ",");
    }
  }
  for (i = 1; i <= Q; i++) {
    scanf("%ld%s", &f, Str);
    long Elem = Ind[Str];
    Count[f][Elem]++;
    UpDate(f);
  }
  for (i = 1; i <= K; i++) {
    vector<DATA> Vec;
    for (j = 1; j <= N + M; j++) {
      if (Count[i][j]) Vec.push_back(DATA(Name[j], Count[i][j]));
    }
    sort(Vec.begin(), Vec.end());
    printf("%ld\n", Vec.size());
    for (j = 0; j < Vec.size(); j++) {
      cout << Vec[j].Name << " " << Vec[j].C << endl;
    }
  }
  return 0;
}
