#include <bits/stdc++.h>
using namespace std;
map<string, int> names;
map<string, int> dg;
void extractAndRank(string in, string my) {
  dg["posted"] = 3;
  dg["commented"] = 2;
  dg["likes"] = 1;
  vector<string> vec;
  string tt = "";
  for (int i = 0; i < in.size(); i++)
    if (in[i] != ' ')
      tt += in[i];
    else {
      if (vec.size() != 2 || tt != "on") vec.push_back(tt);
      tt = "";
    }
  vec[2] = vec[2].substr(0, vec[2].size() - 2);
  if (vec[0] == my)
    names[vec[2]] += dg[vec[1]];
  else if (vec[2] == my)
    names[vec[0]] += dg[vec[1]];
  else {
    names[vec[0]] = names[vec[0]];
    names[vec[2]] = names[vec[2]];
  }
}
struct cmp {
  bool operator()(const pair<int, string> &a,
                  const pair<int, string> &b) const {
    if (a.first != b.first) return a.first > b.first;
    return a.second < b.second;
  }
};
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  string my;
  cin >> my;
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    string in;
    if (i == 0) getline(cin, in);
    getline(cin, in);
    extractAndRank(in, my);
  }
  vector<pair<int, string> > res;
  for (map<string, int>::iterator it = names.begin(); it != names.end(); it++)
    res.push_back(make_pair(it->second, it->first));
  sort(res.begin(), res.end(), cmp());
  for (int i = 0; i < res.size(); i++) cout << res[i].second << endl;
}
