#include <bits/stdc++.h>
using namespace std;
int n, t, m, k, x[105][2];
char p, c, z[2][25];
int main() {
  cin >> z[0] >> z[1] >> n;
  while (n--) {
    cin >> t >> p >> m >> c;
    k = (p == 'a');
    if (x[m][k] < 2)
      if (!x[m][k] && c == 'y')
        x[m][k]++;
      else
        x[m][k] = 2, printf("%s %d %d\n", z[k], m, t);
  }
}
