#include <bits/stdc++.h>
using namespace std;
int cnt[100][2];
int main() {
  ios::sync_with_stdio(false);
  string s[2];
  cin >> s[0] >> s[1];
  int n;
  cin >> n;
  while (n--) {
    int t;
    cin >> t;
    char c;
    cin >> c;
    int num;
    cin >> num;
    char p;
    cin >> p;
    if (c == 'h')
      c = 0;
    else
      c = 1;
    if (p == 'y')
      p = 1;
    else
      p = 2;
    if (cnt[num][c] < 2) {
      cnt[num][c] += p;
      if (cnt[num][c] >= 2) cout << s[c] << " " << num << " " << t << endl;
    }
  }
  return 0;
}
