#include <bits/stdc++.h>
char s[2][25], c1, c2;
int n;
struct node {
  int t;
} a[2][100];
int main(void) {
  int i, j, k, time, num;
  while (~scanf("%s%s", s[0], s[1])) {
    scanf("%d", &n);
    memset(a, 0, sizeof(a));
    while (n--) {
      scanf("%d %c %d %c", &time, &c1, &num, &c2);
      getchar();
      if (c1 == 'a')
        k = 1;
      else
        k = 0;
      if (a[k][num].t >= 0) {
        if (c2 == 'y')
          a[k][num].t++;
        else
          a[k][num].t += 2;
      }
      if (a[k][num].t >= 2) {
        a[k][num].t = -1;
        printf("%s %d %d\n", s[k], num, time);
      }
    }
  }
  return 0;
}
