#include <bits/stdc++.h>
using namespace std;
using namespace std;
bool debug = 0;
void run();
int main() {
  int st = clock();
  run();
  int en = clock();
  return 0;
}
const int N = 1e6 + 7;
int n;
int a[2][100];
string s[2];
void run() {
  cin >> s[0] >> s[1];
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    int t, m;
    char c1, c2;
    scanf("%d %c %d %c", &t, &c1, &m, &c2);
    int k = (c1 == 'h') ? 0 : 1;
    int add = (c2 == 'y') ? 1 : 2;
    if (a[k][m] == 1) add = 1;
    a[k][m] += add;
    if (a[k][m] == 2) printf("%s %d %d \n", s[k].c_str(), m, t);
  }
  printf("\n");
}
