#include <bits/stdc++.h>
using namespace std;
void solve() {
  vector<string> a(2);
  cin >> a[0] >> a[1];
  string team = "ha";
  ;
  vector<vector<int>> cnt(2, vector<int>(120));
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    int t;
    char c;
    int first;
    char second;
    cin >> t >> c >> first >> second;
    int ix = team.find(c);
    ;
    if ((second == 'r' && cnt[ix][first] != -1) || cnt[ix][first] > 0) {
      cout << a[ix] << " " << first << " " << t << "\n";
      cnt[ix][first] = -1;
    }
    if (second == 'y' && cnt[ix][first] != -1) {
      cnt[ix][first] = t;
    }
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  solve();
  return 0;
}
