#include <bits/stdc++.h>
using namespace std;
pair<pair<int, int>, pair<int, int> > event[100];
int n;
string name[2];
int a[200][3];
int main() {
  cin >> name[0] >> name[1];
  cin >> n;
  for (int i = 0; i < n; ++i) {
    int t, num;
    char s, y;
    cin >> t >> s >> num >> y;
    event[i] = make_pair(make_pair(t, num), make_pair(y == 'y', s == 'a'));
  }
  sort(event, event + n);
  for (int i = 0; i < n; ++i) {
    int num = event[i].first.second;
    int time = event[i].first.first;
    int side = event[i].second.second;
    int col = event[i].second.first;
    if (a[num][side] != -1) {
      if (col == 0) {
        cout << name[side] << " " << num << " " << time << "\n";
        a[num][side] = -1;
      } else {
        a[num][side]++;
        if (a[num][side] == 2) {
          cout << name[side] << " " << num << " " << time << "\n";
          a[num][side] = -1;
        }
      }
    }
  }
  return 0;
}
