#include <bits/stdc++.h>
using namespace std;
const int maxn = 100 + 5;
int n, chek1[maxn][2], chek2[maxn][2];
vector<pair<int, pair<int, string> > > v;
string a, b;
int main() {
  int t, s;
  char x, y;
  cin >> b >> a >> n;
  for (int i = 0; i < n; i++) {
    cin >> t >> x >> s >> y;
    if (y == 'r') {
      if (x == 'a' && chek1[s][0] < 2) {
        chek1[s][0] = 2;
        chek1[s][1] = t;
      } else if (x == 'h' && chek2[s][0] < 2) {
        chek2[s][0] = 2;
        chek2[s][1] = t;
      }
    } else {
      if (x == 'a' && chek1[s][0] < 2) {
        chek1[s][0]++;
        chek1[s][1] = t;
      } else if (x == 'h' && chek2[s][0] < 2) {
        chek2[s][0]++;
        chek2[s][1] = t;
      }
    }
  }
  for (int i = 0; i < maxn; i++) {
    if (chek1[i][0] == 2) v.push_back(make_pair(chek1[i][1], make_pair(i, a)));
    if (chek2[i][0] == 2) v.push_back(make_pair(chek2[i][1], make_pair(i, b)));
  }
  sort(v.begin(), v.end());
  for (int i = 0; i < (int)v.size(); i++)
    cout << v[i].second.second << "  " << v[i].second.first << " " << v[i].first
         << endl;
  return 0;
}
