#include <bits/stdc++.h>
using namespace std;
int x, y, u[5000], A[50000], n;
string s, S;
char a, b;
int main() {
  cin >> s >> S;
  scanf("%d", &n);
  while (n--) {
    cin >> x >> a >> y >> b;
    if (a == 'h') {
      if (u[y] == 1 || (b == 'r' && u[y] <= 1))
        u[y] = 2, cout << s << ' ' << y << ' ' << x << endl;
      u[y]++;
    } else {
      if (A[y] == 1 || (b == 'r' && A[y] <= 1))
        A[y] = 2, cout << S << ' ' << y << ' ' << x << endl;
      A[y]++;
    }
  }
  return 0;
}
