#include <bits/stdc++.h>
using namespace std;
const int MAXN = 105;
string n[2];
int r[2][MAXN], f[2][MAXN], m, t, p;
char pt, ct;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n[0] >> n[1] >> m;
  for (int i = 0; i < m; ++i) {
    cin >> t >> pt >> p >> ct;
    int idx = pt == 'h' ? 0 : 1;
    f[idx][p] += ct == 'y' ? 1 : 2;
    if (r[idx][p] == 0 && f[idx][p] > 1) {
      r[idx][p] = 1;
      cout << n[idx] << " " << p << " " << t << endl;
    }
  }
  return 0;
}
