#include <bits/stdc++.h>
using namespace std;
map<string, string> name;
map<string, map<int, int> > pena;
int main() {
  int n;
  string home, away;
  cin >> home;
  cin >> away;
  cin >> n;
  name["h"] = home;
  name["a"] = away;
  for (int i = 1; i <= n; i++) {
    int t, m;
    string team, color;
    cin >> t;
    cin >> team;
    cin >> m;
    cin >> color;
    if (pena[team][m] == 0) {
      if (color == "r") {
        pena[team][m] = 2;
        cout << name[team] << " " << m << " " << t << endl;
      } else {
        pena[team][m] = 1;
      }
    } else if (pena[team][m] == 1) {
      pena[team][m] = 2;
      cout << name[team] << " " << m << " " << t << endl;
    }
  }
  return 0;
}
