#include <bits/stdc++.h>
using namespace std;
int main() {
  int f;
  char h[100];
  char g[100];
  int time, nom;
  char team, card;
  int a[111][2] = {0};
  cin >> h >> g;
  cin >> f;
  for (int i = 0; i < f; ++i) {
    cin >> time >> team >> nom >> card;
    if (card == 'y')
      a[nom][team == 'h']++;
    else
      a[nom][team == 'h'] += 2;
    if ((a[nom][team == 'h'] == 2) ||
        (a[nom][team == 'h'] == 3 && card == 'r')) {
      if (team == 'h')
        cout << h << " " << nom << " " << time << endl;
      else
        cout << g << " " << nom << " " << time << endl;
    }
  }
  return 0;
}
