#include <bits/stdc++.h>
using namespace std;
int main() {
  string hometeam, awayteam;
  cin >> hometeam;
  cin >> awayteam;
  int n;
  cin >> n;
  int time, num;
  char team, card;
  int arrhome[100][2];
  int arraway[100][2];
  for (int i = 0; i < 100; i++) {
    arrhome[i][0] = 0;
    arraway[i][0] = 0;
  }
  while (n--) {
    cin >> time >> team >> num >> card;
    if (team == 'h') {
      if (card == 'r' && arrhome[num][0] != -1) {
        arrhome[num][0] = 2;
      } else if (card == 'y' && arrhome[num][0] != -1) {
        arrhome[num][0]++;
      }
      if (arrhome[num][0] == 2) {
        cout << hometeam << " " << num << " " << time << endl;
        arrhome[num][0] = -1;
      }
      arrhome[num][1] = time;
    }
    if (team == 'a') {
      if (card == 'r' && arraway[num][0] != -1) {
        arraway[num][0] = 2;
      } else if (card == 'y' && arraway[num][0] != -1) {
        arraway[num][0]++;
      }
      if (arraway[num][0] == 2) {
        cout << awayteam << " " << num << " " << time << endl;
        arraway[num][0] = -1;
      }
      arrhome[num][1] = time;
    }
  }
  return 0;
}
