#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  string sa, sb, s, ss;
  int n, t, id, i, j, k;
  cin >> sa >> sb;
  map<pair<string, int>, int> yellow, red;
  map<string, string> team;
  team["h"] = sa;
  team["a"] = sb;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> t >> s >> id >> ss;
    if (ss == "r") {
      if (red[{s, id}]++ == 0) cout << team[s] << " " << id << " " << t << "\n";
    } else {
      if (++yellow[{s, id}] >= 2) {
        if (red[{s, id}]++ == 0)
          cout << team[s] << " " << id << " " << t << "\n";
      }
    }
  }
  return 0;
}
