#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
struct all {
  int t, num;
  char x, type;
} a[N];
bool cmp(all a, all b) { return a.t < b.t; }
int redhome[N], redaway[N];
int yellhome[N], yellaway[N];
string home, away;
int n;
int main() {
  cin >> home >> away;
  cin >> n;
  for (int i = 1; i <= n; ++i) cin >> a[i].t >> a[i].x >> a[i].num >> a[i].type;
  sort(a + 1, a + n + 1, &cmp);
  for (int i = 1; i <= n; ++i) {
    if (a[i].x == 'h') {
      if (a[i].type == 'r') {
        if (redhome[a[i].num] == 0) {
          redhome[a[i].num] = a[i].t;
          cout << home << " " << a[i].num << " " << a[i].t << endl;
        }
      } else {
        if (yellhome[a[i].num] == 0)
          yellhome[a[i].num] = a[i].t;
        else if (redhome[a[i].num] == 0) {
          redhome[a[i].num] = a[i].t;
          cout << home << " " << a[i].num << " " << a[i].t << endl;
        }
      }
    } else {
      if (a[i].type == 'r') {
        if (redaway[a[i].num] == 0) {
          redaway[a[i].num] = a[i].t;
          cout << away << " " << a[i].num << " " << a[i].t << endl;
        }
      } else {
        if (yellaway[a[i].num] == 0)
          yellaway[a[i].num] = a[i].t;
        else if (redaway[a[i].num] == 0) {
          redaway[a[i].num] = a[i].t;
          cout << away << " " << a[i].num << " " << a[i].t << endl;
        }
      }
    }
  }
  return 0;
}
