#include <bits/stdc++.h>
int main() {
  std::string home_name, away_name;
  std::cin >> home_name >> away_name;
  int n;
  std::cin >> n;
  std::map<int, int> foul;
  int t, m;
  char h, c;
  for (int i = 0; i < n; ++i) {
    std::cin >> t >> h >> m >> c;
    int id = h * 100 + m;
    int foul_cnt = 0;
    if (c == 'y')
      foul_cnt = 1;
    else
      foul_cnt = 2;
    if (foul.count(id) == 0) foul[id] = 0;
    if (foul[id] < 2 && foul[id] + foul_cnt >= 2) {
      if (h == 'h')
        std::cout << home_name << " ";
      else
        std::cout << away_name << " ";
      std::cout << m << " " << t << "\n";
    }
    foul[id] += foul_cnt;
  }
  return 0;
}
