#include <bits/stdc++.h>
using namespace std;
int main() {
  string teams[2];
  cin >> teams[0] >> teams[1];
  int n;
  cin >> n;
  map<int, char> players[2];
  for (int i = 0; i < n; i++) {
    int times, number;
    char team, card;
    cin >> times >> team >> number >> card;
    int index = team == 'a' ? 1 : 0;
    if (players[index].find(number) == players[index].end()) {
      if (card == 'r')
        cout << teams[index] << " " << number << " " << times << endl;
      players[index][number] = card;
    } else if (players[index][number] == 'y') {
      cout << teams[index] << " " << number << " " << times << endl;
      players[index][number] = 'r';
    }
  }
  return 0;
}
