#include <bits/stdc++.h>
using namespace std;
map<string, int> mp;
struct node {
  string nn;
  int g;
} p[1000];
int f(string cc) {
  if (cc[0] == 'p') return 15;
  if (cc[0] == 'c') return 10;
  return 5;
}
bool cmp(node aa, node bb) {
  if (aa.g == bb.g) return aa.nn < bb.nn;
  return aa.g > bb.g;
}
int main() {
  int i, j, n, t, k, h, fen;
  string c[5], cc, c2;
  while (cin >> cc) {
    mp.clear();
    scanf("%d", &n);
    t = 1;
    for (i = 0; i < n; i++) {
      for (j = 0; j < 4; j++) cin >> c[j];
      fen = f(c[1]);
      if (mp[c[0]] == 0) {
        mp[c[0]] = t++;
        p[mp[c[0]]].nn = c[0];
        p[mp[c[0]]].g = 0;
      }
      c2.clear();
      if (fen == 5)
        h = 2;
      else
        h = 3, cin >> c[4];
      c2 = c[h].substr(0, c[h].size() - 2);
      if (mp[c2] == 0) {
        mp[c2] = t++;
        p[mp[c2]].nn = c2;
        p[mp[c2]].g = 0;
      }
      if (c[0] == cc) p[mp[c2]].g += fen;
      if (c2 == cc) p[mp[c[0]]].g += fen;
    }
    sort(p + 1, p + t, cmp);
    for (i = 1; i < t; i++)
      if (p[i].nn != cc) cout << p[i].nn << endl;
  }
}
