#include <bits/stdc++.h>
using namespace std;
const double pi = 2 * acos(0.0);
const double eps = 1e-9;
const int MOD = (int)1e9 + 7;
long long pow(long long base, long long exp, long long mod) {
  long long result = 1;
  while (exp > 0) {
    if (exp & 1) result *= base;
    base *= base;
    if (base > mod) base %= mod;
    if (result > mod) result %= mod;
    exp /= 2;
  }
  return result;
}
long long mult(long long A, long long B) {
  if (B == 0) return 0;
  long long u = mult(A, B / 2);
  long long res;
  if (B % 2 == 0)
    res = u + u;
  else
    res = u + u + A;
  while (res >= MOD) res -= MOD;
  return res;
}
long long n, num;
vector<long long> nums;
void inp() {
  for (long long i = 0; i < n; i++) {
    cin >> num;
    nums.push_back(num);
  }
}
long long _sieve_size;
bitset<10000010> bs;
vector<int> primes;
void sieve(long long upperbound) {
  _sieve_size = upperbound + 1;
  bs.set();
  bs[0] = bs[1] = 0;
  for (long long i = 2; i <= _sieve_size; i++)
    if (bs[i]) {
      for (long long j = i * i; j <= _sieve_size; j += i) bs[j] = 0;
      primes.push_back((int)i);
    }
}
bool isprime(int a) {
  for (int i = 2; i * i <= a; i++)
    if (a % i == 0) return false;
  return true;
}
bool comp(pair<int, int> a, pair<int, int> b) { return a.second < b.second; }
int main() {
  std::ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  string ht, at;
  cin >> ht >> at;
  cin >> num;
  char s, cc;
  int timin, tno;
  vector<char> locs, cards;
  vector<long long> timez, teamnumz;
  set<int> t1, t2;
  for (long long i = 0; i < num; i++) {
    cin >> timin >> s >> tno >> cc;
    if (s == 'h')
      t1.insert(tno);
    else
      t2.insert(tno);
    teamnumz.push_back(tno);
    cards.push_back(cc);
    locs.push_back(s);
    timez.push_back(timin);
  }
  vector<pair<int, pair<string, int> > > ret;
  set<int>::iterator it;
  for (it = t1.begin(); it != t1.end(); it++) {
    int minr, min2y;
    minr = min2y = 200;
    for (long long i = 0; i < teamnumz.size(); i++)
      if (locs[i] == 'h' && teamnumz[i] == *it && cards[i] == 'r') {
        minr = timez[i];
        break;
      }
    int cnty = 0, locy;
    for (long long i = 0; i < teamnumz.size(); i++)
      if (locs[i] == 'h' && teamnumz[i] == *it && cards[i] == 'y') {
        cnty++;
        if (cnty == 2) {
          locy = timez[i];
          break;
        }
      }
    if (cnty == 2) min2y = locy;
    if (minr == 200 && min2y == 200) continue;
    if (minr < min2y) {
      ret.push_back(make_pair(minr, make_pair(ht, *it)));
    } else if (min2y < minr)
      ret.push_back(make_pair(min2y, make_pair(ht, *it)));
  }
  for (it = t2.begin(); it != t2.end(); it++) {
    int minr, min2y;
    minr = min2y = 200;
    for (long long i = 0; i < teamnumz.size(); i++)
      if (locs[i] == 'a' && teamnumz[i] == *it && cards[i] == 'r') {
        minr = timez[i];
        break;
      }
    int cnty = 0, locy;
    for (long long i = 0; i < teamnumz.size(); i++)
      if (locs[i] == 'a' && teamnumz[i] == *it && cards[i] == 'y') {
        cnty++;
        if (cnty == 2) {
          locy = timez[i];
          break;
        }
      }
    if (cnty == 2) min2y = locy;
    if (minr == 200 && min2y == 200) continue;
    if (minr < min2y) {
      ret.push_back(make_pair(minr, make_pair(at, *it)));
    } else if (min2y < minr)
      ret.push_back(make_pair(min2y, make_pair(at, *it)));
  }
  sort((ret).begin(), (ret).end());
  for (long long i = 0; i < ret.size(); i++)
    cout << ret[i].second.first << " " << ret[i].second.second << " "
         << ret[i].first << "\n";
  return 0;
}
