#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  int ar1[100], ar2[100];
  memset(ar1, 0, sizeof(ar1));
  memset(ar2, 0, sizeof(ar2));
  string s1, s2;
  cin >> s1 >> s2;
  int t;
  cin >> t;
  vector<pair<pair<int, char>, pair<int, char> > > v(t);
  for (int i = 0; i < t; i++)
    cin >> v[i].first.first >> v[i].first.second >> v[i].second.first >>
        v[i].second.second;
  sort(v.begin(), v.end());
  for (int i = 0; i < t; i++) {
    if (v[i].first.second == 'a') {
      if (ar2[v[i].second.first] < 2) {
        ar2[v[i].second.first] += (v[i].second.second == 'y') ? 1 : 2;
        if (ar2[v[i].second.first] >= 2)
          cout << s2 << " " << v[i].second.first << " " << v[i].first.first
               << endl;
      }
    } else if (v[i].first.second == 'h') {
      if (ar1[v[i].second.first] < 2) {
        ar1[v[i].second.first] += (v[i].second.second == 'y') ? 1 : 2;
        if (ar1[v[i].second.first] >= 2)
          cout << s1 << " "
               << " " << v[i].second.first << " " << v[i].first.first << endl;
      }
    }
  }
  return 0;
}
