#include <bits/stdc++.h>
using namespace std;
const int INF = 1000000000;
const int dre_4[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
const int dre_8[8][2] = {{1, 0},  {1, 1},   {0, 1},  {-1, 1},
                         {-1, 0}, {-1, -1}, {0, -1}, {1, -1}};
const int kaijou[10] = {1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880};
int main() {
  string s, t;
  int n;
  int a[102] = {};
  int h[102] = {};
  cin >> t >> s;
  cin >> n;
  for (int ppp = 0; ppp < n; ppp++) {
    int b, c;
    char d, e;
    scanf("%d %c%d %c", &b, &d, &c, &e);
    if (d == 'a') {
      if (e == 'y') {
        a[c]++;
        if (a[c] == 2) {
          cout << s;
          printf(" %d %d\n", c, b);
        }
      } else {
        a[c] += 2;
        if (a[c] < 4) {
          cout << s;
          printf(" %d %d\n", c, b);
        }
      }
    } else {
      if (e == 'y') {
        h[c]++;
        if (h[c] == 2) {
          cout << t;
          printf(" %d %d\n", c, b);
        }
      } else {
        h[c] += 2;
        if (h[c] < 4) {
          cout << t;
          printf(" %d %d\n", c, b);
        }
      }
    }
  }
}
