#include <bits/stdc++.h>
using namespace std;
int main() {
  string home, guest;
  cin >> home >> guest;
  int n;
  cin >> n;
  int a[100] = {0}, b[100] = {0};
  bool visa[100] = {0}, visb[100] = {0};
  while (n--) {
    int time, num;
    char c, p;
    cin >> time >> c >> num >> p;
    if (c == 'h') {
      if (p == 'r')
        a[num] += 2;
      else
        a[num]++;
      if (!visa[num] && a[num] >= 2)
        cout << home << ' ' << num << ' ' << time << endl, visa[num] = 1;
    } else {
      if (p == 'r')
        b[num] += 2;
      else
        b[num]++;
      if (!visb[num] && b[num] >= 2)
        cout << guest << ' ' << num << ' ' << time << endl, visb[num] = 1;
    }
  }
  return 0;
}
