#include <bits/stdc++.h>
using namespace std;
int a1[227], b[227];
int main() {
  string s1, s2;
  cin >> s1 >> s2;
  int n;
  cin >> n;
  while (n--) {
    int t, m;
    char c, car;
    cin >> t >> c >> m >> car;
    int w = 2;
    if (car == 'y') w = 1;
    if (c == 'h') {
      a1[m] += w;
      if (a1[m] >= 2) {
        a1[m] = -100000;
        cout << s1 << " " << m << " " << t << endl;
      }
    } else {
      b[m] += w;
      if (b[m] >= 2) {
        b[m] = -100000;
        cout << s2 << " " << m << " " << t << endl;
      }
    }
  }
  return 0;
}
