#include <bits/stdc++.h>
using namespace std;
template <typename _type>
void outArray(_type a[], long long n) {
  for (long long i = 1; i <= n; i++) cout << a[i] << " ";
  cout << endl;
}
template <typename _type>
void outMatrix(_type a[][100005], long long n, long long m) {
  for (long long i = 1; i <= n; i++) {
    for (long long j = 1; j <= m; j++) cout << a[i][j] << " ";
    cout << endl;
  }
}
long long n, p;
char h[1000];
char a[1000];
long long b[100005] = {0}, c[100005] = {0};
int main() {
  long long i, j, ii, jj, le, ri;
  long long t, x, y, z, m, f;
  char team, card;
  cin >> h;
  cin >> a;
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> t >> team >> m >> card;
    f = 0;
    if (card == 'r') {
      if (team == 'h') {
        if (b[m] <= 1) f = 1;
        b[m] = 2;
      } else {
        if (c[m] <= 1) f = 1;
        c[m] = 2;
      }
    } else if (card == 'y') {
      if (team == 'h') {
        if (b[m] == 1) f = 1;
        b[m]++;
      } else {
        if (c[m] == 1) f = 1;
        c[m]++;
      }
    }
    if (f) {
      if (team == 'h')
        cout << h;
      else
        cout << a;
      cout << " " << m << " " << t << endl;
    }
  }
  return 0;
}
