#include <bits/stdc++.h>
using namespace std;
bool usedh[100];
bool useda[100];
int h[100];
int a[100];
int main() {
  ios::sync_with_stdio(false);
  string sh, sa;
  cin >> sh >> sa;
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int time, nom;
    char c1, c2;
    cin >> time >> c1 >> nom >> c2;
    if (c1 == 'h') {
      if (usedh[nom]) continue;
      if (c2 == 'r') {
        usedh[nom] = true;
        cout << sh << " " << nom << " " << time << endl;
      } else {
        h[nom]++;
        if (h[nom] == 2) {
          usedh[nom] = true;
          cout << sh << " " << nom << " " << time << endl;
        }
      }
    } else {
      if (useda[nom]) continue;
      if (c2 == 'r') {
        useda[nom] = true;
        cout << sa << " " << nom << " " << time << endl;
      } else {
        a[nom]++;
        if (a[nom] == 2) {
          useda[nom] = true;
          cout << sa << " " << nom << " " << time << endl;
        }
      }
    }
  }
  return 0;
}
