#include <bits/stdc++.h>
using namespace std;
string str1;
string str2;
int n;
int a, c;
char d, b;
map<int, int> mm1;
map<int, int> mm2;
set<int> s1, s2;
int main() {
  cin >> str1;
  cin >> str2;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d %c %d %c\n", &a, &b, &c, &d);
    if ((b == 'h' && s1.count(c)) || (b == 'a' && s2.count(c))) continue;
    if (b == 'h') {
      if (d == 'r') {
        cout << str1 << ' ' << c << ' ' << a << endl;
        s1.insert(c);
      } else {
        if (!mm1.count(c)) {
          mm1[c] = 1;
        } else {
          mm1[c]++;
          cout << str1 << ' ' << c << ' ' << a << endl;
          s1.insert(c);
        }
      }
    } else {
      if (d == 'r') {
        cout << str2 << ' ' << c << ' ' << a << endl;
        s2.insert(c);
      } else {
        if (!mm2.count(c)) {
          mm2[c] = 1;
        } else {
          mm2[c]++;
          cout << str2 << ' ' << c << ' ' << a << endl;
          s2.insert(c);
        }
      }
    }
  }
  return 0;
}
