#include <bits/stdc++.h>
using namespace std;
int main() {
  char home[30], away[30];
  scanf("%s", home);
  scanf("%s", away);
  int n, a[101], h[101], i, time, id;
  scanf("%d\n", &n);
  char data[100];
  for (i = 0; i < 101; i++) a[i] = h[i] = 0;
  while (n--) {
    gets(data);
    i = 0;
    time = 0;
    while (data[i] != ' ') time = time * 10 + (data[i++] - '0');
    i++;
    if (data[i] == 'a') {
      i += 2;
      id = 0;
      while (data[i] != ' ') id = id * 10 + (data[i++] - '0');
      i++;
      if (data[i] == 'y')
        a[id]++;
      else if (data[i] == 'r')
        a[id] += 2;
      for (i = 0; i < 101; i++)
        if (a[i] >= 2) {
          a[i] = -100000000;
          printf("%s %d %d\n", away, i, time);
        }
    } else if (data[i] == 'h') {
      i += 2;
      id = 0;
      while (data[i] != ' ') id = id * 10 + (data[i++] - '0');
      i++;
      if (data[i] == 'y')
        h[id]++;
      else if (data[i] == 'r')
        h[id] += 2;
      for (i = 0; i < 101; i++)
        if (h[i] >= 2) {
          h[i] = -100000000;
          printf("%s %d %d\n", home, i, time);
        }
    }
  }
  return 0;
}
