#include <bits/stdc++.h>
using namespace std;
int n, a[100], b[100];
char c[33], d[33];
void inmake() {
  scanf("%s %s", &c, &d);
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    int num, minute;
    char pl, card;
    scanf("%d %c %d %c", &minute, &pl, &num, &card);
    if (pl == 'h') {
      if (card == 'r' && a[num] != -1) {
        a[num] = -1;
        printf("%s %d %d\n", c, num, minute);
      } else if (a[num] != -1) {
        a[num]++;
        if (a[num] == 2) {
          a[num] = -1;
          printf("%s %d %d\n", c, num, minute);
        }
      }
    } else {
      if (card == 'r' && b[num] != -1) {
        b[num] = -1;
        printf("%s %d %d\n", d, num, minute);
      } else if (b[num] != -1) {
        b[num]++;
        if (b[num] == 2) {
          b[num] = -1;
          printf("%s %d %d\n", d, num, minute);
        }
      }
    }
  }
}
int main() {
  inmake();
  return 0;
}
