#include <bits/stdc++.h>
using namespace std;
struct student {
  string str;
  int a, b;
};
map<int, int> mp1, flag, flag1, mp2;
vector<student> vec;
int main() {
  int i, j, k, n, num, aa, bb;
  string s1, s2, s3;
  char ch, x;
  cin >> s1;
  cin >> s2;
  cin >> n;
  while (n--) {
    cin >> k >> ch >> num >> x;
    if (x == 'r') {
      if (ch == 'h') {
        if (flag[num] == 0) {
          s3 = s1;
          student ant;
          ant.str = s3;
          ant.a = num;
          ant.b = k;
          vec.push_back(ant);
          flag[num] = 1;
        }
      } else if (ch == 'a') {
        if (flag1[num] == 0) {
          s3 = s2;
          student ant;
          ant.str = s3;
          ant.a = num;
          ant.b = k;
          vec.push_back(ant);
          flag1[num] = 1;
        }
      }
    } else if (x == 'y') {
      if (ch == 'h')
        mp1[num]++;
      else
        mp2[num]++;
    }
    if (mp1[num] == 2) {
      if (flag[num] == 0) {
        s3 = s1;
        student ant;
        ant.str = s3;
        ant.a = num;
        ant.b = k;
        vec.push_back(ant);
        flag[num] = 1;
      }
    } else if (mp2[num] == 2) {
      if (flag1[num] == 0) {
        s3 = s2;
        student ant;
        ant.str = s3;
        ant.a = num;
        ant.b = k;
        vec.push_back(ant);
        flag1[num] = 1;
      }
    }
  }
  for (i = 0; i < vec.size(); i++) {
    cout << vec[i].str << " " << vec[i].a << " " << vec[i].b << endl;
  }
  vec.clear();
  mp1.clear();
  flag.clear();
  flag1.clear();
  mp2.clear();
  return 0;
}
