#include <bits/stdc++.h>
using namespace std;
int main() {
  char s[100], t[100];
  int n, m[2][100], tm[100];
  char a[2], b[2];
  memset(s, 0, sizeof(s));
  memset(t, 0, sizeof(t));
  cin >> s >> t;
  cin >> n;
  pair<char, pair<int, int> > an[100];
  int l = 0, x;
  memset(m, 0, sizeof(m));
  for (long long i = 0; i < n; i++) {
    cin >> tm[i] >> a >> x >> b;
    if (a[0] == 'a') {
      if (m[0][x] == 1 || (b[0] == 'r' && m[0][x] < 2)) {
        an[l++] = make_pair('a', make_pair(x, tm[i]));
        m[0][x] = 2;
      }
      m[0][x]++;
    } else {
      if (m[1][x] == 1 || (b[0] == 'r' && m[1][x] < 2)) {
        an[l++] = make_pair('h', make_pair(x, tm[i]));
        m[1][x] = 2;
      }
      m[1][x]++;
    }
  }
  for (long long i = 0; i < l; i++)
    if (an[i].first == 'a')
      printf("%s %d %d\n", t, an[i].second.first, an[i].second.second);
    else
      printf("%s %d %d\n", s, an[i].second.first, an[i].second.second);
}
