#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, i, x, t;
  char card, ha;
  map<int, pair<int, int> > mh;
  map<int, pair<int, int> > ma;
  map<int, pair<int, int> >::iterator it;
  string h, a;
  cin >> h >> a;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> t >> ha;
    getchar();
    cin >> x >> card;
    getchar();
    if (ha == 'a') {
      for (it = ma.begin(); it != ma.end(); it++) {
        if ((it->second).first == x) break;
      }
      if (it == ma.end() && card == 'y')
        ma[t] = make_pair(x, 1);
      else if (it == ma.end() && card == 'r')
        ma[t] = make_pair(x, 2);
      else if (it != ma.end() && (it->second).second == 1) {
        ma[t] = make_pair(x, 2);
        ma.erase(it);
      }
    }
    if (ha == 'h') {
      for (it = mh.begin(); it != mh.end(); it++) {
        if ((it->second).first == x) break;
      }
      if (it == mh.end() && card == 'y')
        mh[t] = make_pair(x, 1);
      else if (it == mh.end() && card == 'r')
        mh[t] = make_pair(x, 2);
      else if (it != ma.end() && (it->second).second == 1) {
        mh[t] = make_pair(x, 2);
        mh.erase(it);
      }
    }
  }
  for (i = 0; i <= 90; i++) {
    if (ma[i].second == 2) cout << a << " " << ma[i].first << " " << i << endl;
    if (mh[i].second == 2) cout << h << " " << mh[i].first << " " << i << endl;
  }
  return 0;
}
