#include <bits/stdc++.h>
using namespace std;
char *fmt[] = {"%s posted on %s wall", "%s commented on %s post",
               "%s likes %s post"},
     *pred[] = {" posted ", " commented ", " likes "};
map<string, int> m;
bool mless(const pair<string, int> &a, const pair<string, int> &b) {
  return (a.second > b.second) ||
         ((a.second == b.second) && (a.first < b.first));
}
int main(int argc, char *argv[]) {
  char name[20];
  scanf("%s", name);
  string sn(name);
  int n;
  scanf("%d\n", &n);
  m.clear();
  for (int i = 0; i < n; i++) {
    char str[100];
    fgets(str, 100, stdin);
    char name1[20], name2[20];
    int t;
    for (int i = 0; i < 3; i++)
      if (strstr(str, pred[i])) {
        sscanf(str, fmt[i], name1, name2);
        int l = strlen(name2);
        for (int j = 0; j < l; j++)
          if (name2[j] == '\'') name2[j] = 0;
        t = 5 * (3 - i);
      }
    string sn1(name1), sn2(name2);
    if (strcmp(name1, name) == 0)
      m[sn2] = m[sn2] + t;
    else if (strcmp(name2, name) == 0)
      m[sn1] = m[sn1] + t;
    else {
      m[sn1] = m[sn1];
      m[sn2] = m[sn2];
    }
  }
  vector<pair<string, int> > l;
  l.clear();
  for (map<string, int>::iterator i = m.begin(); i != m.end(); i++)
    l.push_back(*i);
  sort(l.begin(), l.end(), mless);
  for (vector<pair<string, int> >::iterator i = l.begin(); i != l.end(); i++)
    if (i->first != sn) printf("%s\n", i->first.c_str());
  return 0;
}
