#include <bits/stdc++.h>
using namespace std;
map<pair<string, int>, int> m;
set<pair<string, int>> s;
int main() {
  string a, h;
  cin >> a >> h;
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    string l;
    int t;
    int p;
    char q;
    cin >> t >> l >> p >> q;
    if (l == "a")
      l = h;
    else
      l = a;
    if (m.count(make_pair(l, p)) == 0)
      if (q == 'r' || s.count(make_pair(l, p)) != 0) {
        m[make_pair(l, p)] = t;
        cout << l << " " << p << " " << t << endl;
      } else
        s.insert(make_pair(l, p));
  }
  return 0;
}
