#include <bits/stdc++.h>
using namespace std;
struct node {
  int t, node_id;
  char from, card;
};
bool cmp(const node& n1, const node& n2) { return n1.t < n2.t; }
int n;
node a[100];
map<int, int> mp;
string name1, name2;
void show(node x) {
  if (x.from == 'h')
    cout << name1 << " ";
  else
    cout << name2 << " ";
  cout << x.node_id << " " << x.t << endl;
}
int main() {
  int i, j, k;
  cin >> name1 >> name2;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> a[i].t >> a[i].from >> a[i].node_id >> a[i].card;
  }
  sort(a, a + n, cmp);
  for (i = 0; i < n; i++) {
    int now;
    now = a[i].node_id;
    if (a[i].from == 'a') now += 100;
    if (a[i].card == 'r' && mp[now] >= 0) {
      mp[now] = -10000;
      show(a[i]);
    } else {
      mp[now]++;
      if (mp[now] == 2) {
        show(a[i]);
        mp[now] = -10000;
      }
    }
  }
  return 0;
}
