#include <bits/stdc++.h>
using namespace std;
int main() {
  string home, guest;
  cin >> home >> guest;
  int n;
  cin >> n;
  map<pair<char, int>, int> m;
  for (int i = 0; i < n; i++) {
    int mi;
    char team;
    int number;
    char card;
    cin >> mi >> team >> number >> card;
    m[{team, number}] += card == 'r' ? 2 : 1;
    if (m[{team, number}] > 1 && m[{team, number}] < 10) {
      cout << (team == 'h' ? home : guest) << " " << number << " " << mi
           << endl;
      m[{team, number}] = 10;
    }
  }
}
