#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, i;
  string away, home;
  cin >> home >> away >> n;
  long long a[100] = {0}, h[100] = {0}, x, y;
  char c1, c2;
  for (i = 0; i < n; i++) {
    cin >> x >> c1 >> y >> c2;
    if (c1 == 'a') {
      if (c2 == 'y') {
        if (a[y] == 0)
          a[y] = y;
        else {
          if (a[y] != -1) {
            cout << away << " " << y << " " << x << "\n";
            a[y] = -1;
          }
        }
      } else {
        if (a[y] != -1) {
          cout << away << " " << y << " " << x << "\n";
          a[y] = -1;
        }
      }
    } else {
      if (c2 == 'y') {
        if (h[y] == 0)
          h[y] = y;
        else {
          if (h[y] != -1) {
            cout << home << " " << y << " " << x << "\n";
            h[y] = -1;
          }
        }
      } else {
        if (h[y] != -1) {
          cout << home << " " << y << " " << x << "\n";
          h[y] = -1;
        }
      }
    }
  }
}
