#include <bits/stdc++.h>
using namespace std;
bool r[1000];
bool arr[1000][1000];
int ar[1000][1000];
int rr[1000];
long long a, b, c, z, k, f;
long long poww(long long a, long long b) {
  long long res = 1;
  for (int i = 1; i <= b; ++i) res *= a;
  return res;
}
int main() {
  map<int, pair<char, int> > m;
  string H, AW;
  char l, o;
  cin >> H >> AW;
  cin >> a;
  while (a--) {
    cin >> b >> l >> c >> o;
    if (o == 'y') ar[(int)l][c]++;
    if (o == 'r') ar[(int)l][c] += 2;
    if (ar[(int)l][c] == 2 || ar[(int)l][c] == 3)
      m.insert(make_pair(b, make_pair(l, c)));
    if (ar[(int)l][c] == 2 || ar[(int)l][c] == 3) ar[(int)l][c] += 5;
  }
  map<int, pair<char, int> >::iterator it;
  for (it = m.begin(); it != m.end(); it++) {
    if (it->second.first == 'a')
      cout << AW << " " << it->second.second << " " << it->first << endl;
    else if (it->second.first == 'h')
      cout << H << " " << it->second.second << " " << it->first << endl;
  }
  return 0;
}
