#include <bits/stdc++.h>
using namespace std;
long long qwe[2][105];
long long tim[2][105];
int main() {
  string s1, s2;
  cin >> s1 >> s2;
  long long n;
  cin >> n;
  for (long long i = 0; i < n; i++) {
    long long t;
    char c;
    long long num;
    char y;
    cin >> t >> c >> num >> y;
    if (qwe[(c == 'h' ? 0 : 1)][num] < 0) continue;
    qwe[(c == 'h' ? 0 : 1)][num] += (y == 'y' ? 1 : 2);
    if (qwe[(c == 'h' ? 0 : 1)][num] >= 2) {
      cout << (c == 'h' ? s1 : s2) << " " << num << " " << t << endl;
      qwe[(c == 'h' ? 0 : 1)][num] = -1;
    }
  }
  return 0;
}
