#include <bits/stdc++.h>
using namespace std;
template <class T>
T _abs(T n) {
  return (n < 0 ? -n : n);
}
template <class T>
T _max(T a, T b) {
  return (!(a < b) ? a : b);
}
template <class T>
T _min(T a, T b) {
  return (a < b ? a : b);
}
template <class T>
T sq(T x) {
  return x * x;
}
template <class T>
T gcd(T a, T b) {
  return (b != 0 ? gcd<T>(b, a % b) : a);
}
template <class T>
T lcm(T a, T b) {
  return (a / gcd<T>(a, b) * b);
}
template <class T>
T power(T N, T P) {
  return (P == 0) ? 1 : N * power(N, P - 1);
}
template <class T>
std::string to_string(T const& value) {
  stringstream sstr;
  sstr << value;
  return sstr.str();
}
bool yes_no[10005];
long long prime[6 * 10000], sz = 0;
void seive() {
  long long i, j;
  yes_no[0] = yes_no[1] = true;
  yes_no[2] = false;
  prime[sz++] = 2;
  for (i = 4; i <= 10005; i += 2) yes_no[i] = true;
  for (i = 3; i <= sqrt(10005); i += 2)
    if (yes_no[i] == false)
      for (j = i * i; j <= 10005; j += (2 * i)) yes_no[j] = true;
  for (i = 3; i <= 10005; i += 2)
    if (yes_no[i] == false) prime[sz++] = i;
}
int main() {
  map<int, int> first_team, second_team;
  string home_team, away_team;
  int i, home_player[110], away_player[110], n, mint, jersy;
  vector<string> v;
  vector<pair<int, int> > time_mint;
  char team, card;
  memset(home_player, 0, sizeof home_player);
  memset(away_player, 0, sizeof away_player);
  cin >> home_team >> away_team;
  cin >> n;
  while (n--) {
    cin >> mint >> team >> jersy >> card;
    if (team == 'h') {
      if (card == 'r') {
        if (!home_player[jersy]) {
          home_player[jersy] = 1;
          v.push_back(home_team);
          time_mint.push_back(make_pair(jersy, mint));
        }
      } else {
        first_team[jersy]++;
        if (first_team[jersy] == 2 && !home_player[jersy]) {
          home_player[jersy] = 1;
          v.push_back(home_team);
          time_mint.push_back(make_pair(jersy, mint));
        }
      }
    } else {
      if (card == 'r') {
        if (!away_player[jersy]) {
          away_player[jersy] = 1;
          v.push_back(away_team);
          time_mint.push_back(make_pair(jersy, mint));
        }
      } else {
        second_team[jersy]++;
        if (second_team[jersy] == 2 && !away_player[jersy]) {
          away_player[jersy] = 1;
          v.push_back(away_team);
          time_mint.push_back(make_pair(jersy, mint));
        }
      }
    }
  }
  n = v.size();
  for (i = 0; i < n; i++) {
    cout << v[i] << " " << time_mint[i].first << " " << time_mint[i].second
         << endl;
  }
  return 0;
}
