#include <bits/stdc++.h>
using namespace std;
string teams[2];
int n;
int a[2][100];
int get_team(char c) {
  if (c == 'a') return 0;
  return 1;
}
int get_plus(char c) {
  if (c == 'r') return 2;
  return 1;
}
int main() {
  cin >> teams[1] >> teams[0];
  cin >> n;
  for (int i = 0; i < n; ++i) {
    int t, m;
    char c1, c2;
    cin >> t >> c1 >> m >> c2;
    int team = get_team(c1), pls = get_plus(c2);
    if (a[team][m] == -1) continue;
    a[team][m] += pls;
    if (a[team][m] >= 2) {
      a[team][m] = -1;
      cout << teams[team] << ' ' << m << ' ' << t << endl;
    }
  }
  return 0;
}
