#include <bits/stdc++.h>
const double eps = 1e-9;
const double pi = acos(-1);
using namespace std;
struct P {
  string name;
  int prio;
  P() { prio = 0; }
} a[110];
map<string, int> mp;
int cnt;
int get_id(string& s) {
  if (!mp.count(s)) {
    mp[s] = cnt++;
    a[cnt - 1].prio = 0;
    a[cnt - 1].name = s;
  }
  return mp[s];
}
int get_op(string& s) {
  if (s[0] == 'p')
    return 1;
  else if (s[0] == 'c')
    return 2;
  else if (s[0] == 'l')
    return 3;
}
bool cmp(const P& a, const P& b) {
  if (a.prio == b.prio)
    return a.name < b.name;
  else
    return a.prio > b.prio;
}
int main() {
  string me;
  cin >> me;
  int n;
  cin >> n;
  getchar();
  cnt = 0;
  string s;
  for (int i = 1; i <= n; i++) {
    getline(cin, s);
    stringstream ss(s);
    string str[5];
    int tcnt = 0;
    string tmp;
    while (ss >> tmp) str[tcnt++] = tmp;
    int op = get_op(str[1]);
    if (op == 1 || op == 2) {
      int id_1 = get_id(str[0]);
      int j = 0;
      string tmp;
      while (isalpha(str[3][j])) tmp += str[3][j++];
      int id_2 = get_id(tmp);
      if (str[0] == me)
        a[id_2].prio += (op == 1 ? 15 : 10);
      else if (tmp == me)
        a[id_1].prio += (op == 1 ? 15 : 10);
    } else {
      int id_1 = get_id(str[0]);
      int j = 0;
      string tmp;
      while (isalpha(str[2][j])) tmp += str[2][j++];
      int id_2 = get_id(tmp);
      if (str[0] == me)
        a[id_2].prio += 5;
      else if (tmp == me)
        a[id_1].prio += 5;
    }
  }
  sort(a, a + cnt, cmp);
  for (int i = 0; i < cnt; i++)
    if (a[i].name != me) cout << a[i].name << endl;
  return 0;
}
