#include <bits/stdc++.h>
using namespace std;
bool compare(map<string, int>::iterator a, map<string, int>::iterator b) {
  if (a->second == b->second) return b->first.compare(a->first) > 0;
  return a->second > b->second;
}
int main() {
  string myname;
  char line[1000];
  cin >> myname;
  int i;
  int n;
  scanf(" %d ", &n);
  map<string, int> names;
  for (; n > 0; n--) {
    fgets(line, 1000, stdin);
    string name;
    for (i = 0; line[i] != ' '; i++) name += line[i];
    i++;
    char t = line[i];
    int k = -1;
    for (; line[i] != '\''; i++)
      if (line[i] == ' ') k = i;
    k++;
    string name2;
    for (; line[k] != '\''; k++) name2 += line[k];
    names[name] += 0;
    names[name2] += 0;
    if (!name.compare(myname) || !name2.compare(myname)) {
      if (t == 'p') {
        names[name] += 15;
        names[name2] += 15;
      } else if (t == 'c') {
        names[name] += 10;
        names[name2] += 10;
      } else {
        names[name] += 5;
        names[name2] += 5;
      }
    }
  }
  vector<map<string, int>::iterator> s;
  for (map<string, int>::iterator it = names.begin(); it != names.end(); it++)
    s.push_back(it);
  sort(s.begin(), s.end(), compare);
  for (i = 0; i < s.size(); i++)
    if (myname.compare(s[i]->first)) cout << s[i]->first << endl;
  return 0;
}
