#include <bits/stdc++.h>
using namespace std;
const int INF = (-1u) / 2;
const long long int INF2 = (-1ull) / 2;
int a, b, i, d[1011000], j, k, n, m, timer = 0, l, r, x, y;
int c[1011000], cnt = 0, fl = 0, a2, a3 = -1000000, ans = 0;
string s1, s2;
char rt, rt2;
int main() {
  cin >> s1 >> s2;
  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    cin >> l >> rt >> r >> rt2;
    if (rt == 'h') {
      if (rt2 == 'y')
        c[r]++;
      else
        c[r] += 2;
      if (c[r] >= 2) {
        cout << s1 << " " << r << " " << l << endl;
        c[r] = -100000;
      }
    }
    if (rt == 'a') {
      if (rt2 == 'y')
        d[r]++;
      else
        d[r] += 2;
      if (d[r] >= 2) {
        cout << s2 << " " << r << " " << l << endl;
        d[r] = -100000;
      }
    }
  }
  return 0;
}
