#include <bits/stdc++.h>
using namespace std;
int main() {
  string teams[2]{"", ""};
  map<char, int> mapp;
  char cards[2][100] = {' '};
  int n, num, minute;
  char team, card;
  mapp['h'] = 0;
  mapp['a'] = 1;
  cin >> teams[0] >> teams[1] >> n;
  while (n--) {
    cin >> minute >> team >> num >> card;
    if ((cards[mapp[team]][num] == 'y' || card == 'r') &&
        !(cards[mapp[team]][num] == 'r' && card == 'r')) {
      cout << teams[mapp[team]] << " " << num << " " << minute << '\n';
      cards[mapp[team]][num] = 'r';
    } else if (cards[mapp[team]][num] != 'r')
      cards[mapp[team]][num] = 'y';
  }
  return 0;
}
