/* ****amitm16a2**** */

#include<bits/stdc++.h>
#define ll          long long
#define pb          push_back
#define vi          vector<ll int>
#define all(a)      (a).begin(),(a).end()
#define hell        1000000007
#define rep(i,a,b)	for(ll int i=a;i<b;i++)
#define mp          make_pair
using namespace std;

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

	string s;
	cin>>s;

	ll n;
	cin>>n;
	map<string ,ll> m;
	map<string,ll> :: iterator it;

	for(ll i=0; i<n; i++){

		string a,b,st;
		cin>>a>>b;

		if(b[0]=='p' || b[0]=='c'){

			string c,d,e;
			cin>>c>>d>>e;

			if(b[0]=='p'){

				if(a!=s)
					m[a]+=15;
				else{

					 st=d.substr(0,d.length()-2);
					m[st]+=15;
				}
			}

			else if(b[0]=='c'){

				if(a!=s)
					m[a]+=10;
				else{

					 st=d.substr(0,d.length()-2);
					m[st]+=10;
				}
			}

		}

		else {

			string f,g;
			cin>>f>>g;

			if(a!=s)
					m[a]+=5;
				else{

					st=f.substr(0,f.length()-2);
					m[st]+=5;
				}
		}

	}

	vector<pair<ll,string> > q;

	for(it=m.begin(); it!=m.end(); it++){

		q.pb(mp(-it->second,it->first));
	}

	sort(q.begin(),q.end());

	for(ll i=0; i<q.size(); i++)
		cout<<q[i].second<<"\n";

	return 0;
}


/* ****amitm16a2**** */

#include<bits/stdc++.h>
#define ll          long long
#define pb          push_back
#define vi          vector<ll int>
#define all(a)      (a).begin(),(a).end()
#define hell        1000000007
#define rep(i,a,b)	for(ll int i=a;i<b;i++)
#define mp          make_pair
using namespace std;

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

	string s;
	cin>>s;

	ll n;
	cin>>n;
	map<string ,ll> m;
	map<string,ll> :: iterator it;

	for(ll i=0; i<n; i++){

		string a,b,st;
		cin>>a>>b;

		if(b[0]=='p' || b[0]=='c'){

			string c,d,e;
			cin>>c>>d>>e;

			if(b[0]=='p'){

				if(a!=s)
				{

					m[a]+=0;
				}

				st=d.substr(0,d.length()-2);
				if(st!=s)
					m[st]+=0;

				if(a==s)
					m[st]+=15;
				else if(st==s)
					m[a]+=15;

			}

			else if(b[0]=='c'){

				if(a!=s)
					m[a]+=0;

				st=d.substr(0,d.length()-2);
				if(st!=s)
					m[st]+=0;

				if(a==s)
					m[st]+=10;
				else if(st==s)
					m[a]+=10;
			}

		}

		else {

			string f,g;
			cin>>f>>g;

			if(a!=s)
					m[a]+=0;

				st=f.substr(0,f.length()-2);
				if(st!=s)
					m[st]+=0;

				if(a==s)
					m[st]+=5;
				else if(st==s)
					m[a]+=5;
		}

	}

	vector<pair<ll,string> > q;

	for(it=m.begin(); it!=m.end(); it++){

		q.pb(mp(-it->second,it->first));
	}

	sort(q.begin(),q.end());

	for(ll i=0; i<q.size(); i++)
		cout<<q[i].second<<"\n";

	return 0;
}


/* ****amitm16a2**** */

#include<bits/stdc++.h>
#define ll          long long
#define pb          push_back
#define vi          vector<ll int>
#define all(a)      (a).begin(),(a).end()
#define hell        1000000007
#define rep(i,a,b)	for(ll int i=a;i<b;i++)
#define mp          make_pair
using namespace std;

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

	string s;
	cin>>s;

	ll n;
	cin>>n;
	map<string ,ll> m;
	map<string,ll> :: iterator it;

	for(ll i=0; i<n; i++){

		string a,b,st;
		cin>>a>>b;

		if(b[0]=='p' || b[0]=='c'){

			string c,d,e;
			cin>>c>>d>>e;

			if(b[0]=='p'){

				if(a!=s)
					m[a]+=15;

				st=d.substr(0,d.length()-2);
				if(st!=s)
					m[st]+=15;

			}

			else if(b[0]=='c'){

				if(a!=s)
					m[a]+=10;

				st=d.substr(0,d.length()-2);
				if(st!=s)
					m[st]+=10;
			}

		}

		else {

			string f,g;
			cin>>f>>g;

			if(a!=s)
					m[a]+=5;

				st=f.substr(0,f.length()-2);
				if(st!=s)
					m[st]+=5;
		}

	}

	vector<pair<ll,string> > q;

	for(it=m.begin(); it!=m.end(); it++){

		q.pb(mp(-it->second,it->first));
	}

	sort(q.begin(),q.end());

	for(ll i=0; i<q.size(); i++)
		cout<<q[i].second<<"\n";

	return 0;
}


/* ****amitm16a2**** */

#include<bits/stdc++.h>
#define ll          long long
#define pb          push_back
#define vi          vector<ll int>
#define all(a)      (a).begin(),(a).end()
#define hell        1000000007
#define rep(i,a,b)	for(ll int i=a;i<b;i++)
#define mp          make_pair
using namespace std;

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

	string s;
	cin>>s;

	ll n;
	cin>>n;
	map<string ,ll> m;
	map<string,ll> :: iterator it;

	for(ll i=0; i<n; i++){

		string a,b,st;
		cin>>a>>b;

		if(b[0]=='p' || b[0]=='c'){

			string c,d,e;
			cin>>c>>d>>e;

			if(b[0]=='p'){

				if(a!=s)
				{

					m[a]+=0;
				}

				st=d.substr(0,d.length()-2);
				if(st!=s)
					m[st]+=0;

				if(a==s)
					m[st]+=15;
				else if(st==s)
					m[a]+=15;

			}

			else if(b[0]=='c'){

				if(a!=s)
					m[a]+=0;

				st=d.substr(0,d.length()-2);
				if(st!=s)
					m[st]+=0;

				if(a==s)
					m[st]+=10;
				else if(st==s)
					m[a]+=10;
			}

		}

		else {

			string f,g;
			cin>>f>>g;

			if(a!=s)
					m[a]+=5;

				st=f.substr(0,f.length()-2);
				if(st!=s)
					m[st]+=5;

				if(a==s)
					m[st]+=5;
				else if(st==s)
					m[a]+=5;
		}

	}

	vector<pair<ll,string> > q;

	for(it=m.begin(); it!=m.end(); it++){

		q.pb(mp(-it->second,it->first));
	}

	sort(q.begin(),q.end());

	for(ll i=0; i<q.size(); i++)
		cout<<q[i].second<<"\n";

	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string.h>
#include <limits.h>
#include <cfloat>
#include <stack>
#include <queue>
#include <list>
#include <set>
#include <map>
#include <bitset>
#include <cassert>
#include <unordered_map>
#include <unordered_set>
using namespace std;
#define mem(flag) memset(flag,0,sizeof(flag))
#define Mem(flag) memset(flag,-1,sizeof(flag))
#define read(a) a=getInt()
#define En printf("\n")
#define P(a) printf("%d ",a)
#define Pn(a) printf("%d\n",a)
#define dbg(x) cout << (#x) << " = " << (x) << endl
#define rep(flag, a, b) for (int flag = (a); flag <= (b); ++flag)
#define Rep(flag, a, b) for (int flag = (a); flag >= (b); --flag)

map<string, int> mm;
char buff[200];
char name[200];
int n, cnt;
int asso[300][300];

int main() {

    scanf("%s", name);
    scanf("%d", &n);
    cnt = 0;
    mm[string(name)] = cnt++;
    mem(asso);
    getchar();
    for (int i = 0; i < n; i++) {
        cin.getline(buff, 255, '\n');
        //printf("%s\n", buff);
        int pos1;
        for (pos1 = 0; buff[pos1] != ' '; pos1++) ;
        if (buff[pos1 + 1] == 'p') {
            int pos2, pos3;
            for (pos2 = pos1 + 1; buff[pos2] != ' '; pos2++) ;
            for (pos2 = pos2 + 1; buff[pos2] != ' '; pos2++) ;
            for (pos3 = pos2 + 1; buff[pos3] != '\''; pos3++) ;
            int idx1 = mm[string(buff, buff + pos1)];
            int idx2 = mm[string(buff + pos2 + 1, buff + pos3)];
            asso[idx1][idx2] += 15, asso[idx2][idx1] += 15;
        } else if (buff[pos1 + 1] == 'c') {
            int pos2, pos3;
            for (pos2 = pos1 + 1; buff[pos2] != ' '; pos2++) ;
            for (pos2 = pos2 + 1; buff[pos2] != ' '; pos2++) ;
            for (pos3 = pos2 + 1; buff[pos3] != '\''; pos3++) ;
            int idx1 = mm[string(buff, buff + pos1)];
            int idx2 = mm[string(buff + pos2 + 1, buff + pos3)];
            asso[idx1][idx2] += 10, asso[idx2][idx1] += 10;
        } else {
            int pos2, pos3;
            for (pos2 = pos1 + 1; buff[pos2] != ' '; pos2++) ;
            for (pos3 = pos2 + 1; buff[pos3] != '\''; pos3++) ;
            int idx1 = mm[string(buff, buff + pos1)];
            int idx2 = mm[string(buff + pos2 + 1, buff + pos3)];
            asso[idx1][idx2] += 5, asso[idx2][idx1] += 5;
        }
    }
    string x = string(name);
    vector<string> ss;
    for (auto ele : mm) {
        if (ele.first != x) ss.push_back(ele.first);
    }
    std::sort(ss.begin(), ss.end(), [] (const string &a, const string &b) {
              int idx1 = mm[a], idx2 = mm[b];
              if (asso[0][idx1] == asso[0][idx2]) return a < b;
              else return asso[0][idx1] > asso[0][idx2];
              });
    for (int i = 0; i < ss.size(); i++) printf("%s\n", ss[i].data());

    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string.h>
#include <limits.h>
#include <cfloat>
#include <stack>
#include <queue>
#include <list>
#include <set>
#include <map>
#include <bitset>
#include <cassert>
#include <unordered_map>
#include <unordered_set>
using namespace std;
#define mem(flag) memset(flag,0,sizeof(flag))
#define Mem(flag) memset(flag,-1,sizeof(flag))
#define read(a) a=getInt()
#define En printf("\n")
#define P(a) printf("%d ",a)
#define Pn(a) printf("%d\n",a)
#define dbg(x) cout << (#x) << " = " << (x) << endl
#define rep(flag, a, b) for (int flag = (a); flag <= (b); ++flag)
#define Rep(flag, a, b) for (int flag = (a); flag >= (b); --flag)

map<string, int> mm;
char buff[200];
char name[200];
int n, cnt;
int asso[300][300];

int main() {

    scanf("%s", name);
    scanf("%d", &n);
    string x = string(name);
    cnt = 0;
    mm[string(name)] = cnt++;
    mem(asso);
    getchar();
    for (int i = 0; i < n; i++) {
        cin.getline(buff, 199, '\n');
        int pos1, idx1, idx2;
        for (pos1 = 0; buff[pos1] != ' '; pos1++) ;
        if (buff[pos1 + 1] == 'p') {
            int pos2, pos3;
            for (pos2 = pos1 + 1; buff[pos2] != ' '; pos2++) ;
            for (pos2 = pos2 + 1; buff[pos2] != ' '; pos2++) ;
            for (pos3 = pos2 + 1; buff[pos3] != '\''; pos3++) ;
            string s1 = string(buff, buff + pos1), s2 = string(buff + pos2 + 1, buff + pos3);
            if (mm.find(s1) != mm.end()) idx1 = mm[s1];
            else mm[s1] = cnt++, idx1 = mm[s1];
            if (mm.find(s2) != mm.end()) idx2 = mm[s2];
            else mm[s2] = cnt++, idx2 = mm[s2];
            asso[idx1][idx2] += 15, asso[idx2][idx1] += 15;
        } else if (buff[pos1 + 1] == 'c') {
            int pos2, pos3;
            for (pos2 = pos1 + 1; buff[pos2] != ' '; pos2++) ;
            for (pos2 = pos2 + 1; buff[pos2] != ' '; pos2++) ;
            for (pos3 = pos2 + 1; buff[pos3] != '\''; pos3++) ;
            string s1 = string(buff, buff + pos1), s2 = string(buff + pos2 + 1, buff + pos3);
            if (mm.find(s1) != mm.end()) idx1 = mm[s1];
            else mm[s1] = cnt++, idx1 = mm[s1];
            if (mm.find(s2) != mm.end()) idx2 = mm[s2];
            else mm[s2] = cnt++, idx2 = mm[s2];
            asso[idx1][idx2] += 10, asso[idx2][idx1] += 10;
        } else {
            int pos2, pos3;
            for (pos2 = pos1 + 1; buff[pos2] != ' '; pos2++) ;
            for (pos3 = pos2 + 1; buff[pos3] != '\''; pos3++) ;
            string s1 = string(buff, buff + pos1), s2 = string(buff + pos2 + 1, buff + pos3);
            if (mm.find(s1) != mm.end()) idx1 = mm[s1];
            else mm[s1] = cnt++, idx1 = mm[s1];
            if (mm.find(s2) != mm.end()) idx2 = mm[s2];
            else mm[s2] = cnt++, idx2 = mm[s2];
            asso[idx1][idx2] += 5, asso[idx2][idx1] += 5;
        }
    }
    vector<string> ss;
    for (auto ele : mm) {
        if (ele.first != x) ss.push_back(ele.first);
    }
    std::sort(ss.begin(), ss.end(), [] (const string &a, const string &b) {
              int idx1 = mm[a], idx2 = mm[b];
              if (asso[0][idx1] == asso[0][idx2]) return a < b;
              else return asso[0][idx1] > asso[0][idx2];
              });
    for (int i = 0; i < ss.size(); i++) printf("%s\n", ss[i].data());

    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string.h>
#include <limits.h>
#include <cfloat>
#include <stack>
#include <queue>
#include <list>
#include <set>
#include <map>
#include <bitset>
#include <cassert>
#include <unordered_map>
#include <unordered_set>
using namespace std;
#define mem(flag) memset(flag,0,sizeof(flag))
#define Mem(flag) memset(flag,-1,sizeof(flag))
#define read(a) a=getInt()
#define En printf("\n")
#define P(a) printf("%d ",a)
#define Pn(a) printf("%d\n",a)
#define dbg(x) cout << (#x) << " = " << (x) << endl
#define rep(flag, a, b) for (int flag = (a); flag <= (b); ++flag)
#define Rep(flag, a, b) for (int flag = (a); flag >= (b); --flag)

map<string, int> mm;
char buff[200];
char name[200];
int n, cnt;
int asso[300][300];

int main() {

    scanf("%s", name);
    scanf("%d", &n);
    cnt = 0;
    mm[string(name)] = cnt++;
    mem(asso);
    getchar();
    for (int i = 0; i < n; i++) {
        cin.getline(buff, 255, '\n');
        //printf("%s\n", buff);
        int pos1, idx1, idx2;
        for (pos1 = 0; buff[pos1] != ' '; pos1++) ;
        if (buff[pos1 + 1] == 'p') {
            int pos2, pos3;
            for (pos2 = pos1 + 1; buff[pos2] != ' '; pos2++) ;
            for (pos2 = pos2 + 1; buff[pos2] != ' '; pos2++) ;
            for (pos3 = pos2 + 1; buff[pos3] != '\''; pos3++) ;
            string s1 = string(buff, buff + pos1), s2 = string(buff + pos2 + 1, buff + pos3);
            if (mm.find(s1) != mm.end()) idx1 = mm[s1];
            else mm[s1] = cnt++, idx1 = mm[s1];
            if (mm.find(s2) != mm.end()) idx2 = mm[s2];
            else mm[s2] = cnt++, idx2 = mm[s2];
        } else if (buff[pos1 + 1] == 'c') {
            int pos2, pos3;
            for (pos2 = pos1 + 1; buff[pos2] != ' '; pos2++) ;
            for (pos2 = pos2 + 1; buff[pos2] != ' '; pos2++) ;
            for (pos3 = pos2 + 1; buff[pos3] != '\''; pos3++) ;
            string s1 = string(buff, buff + pos1), s2 = string(buff + pos2 + 1, buff + pos3);
            if (mm.find(s1) != mm.end()) idx1 = mm[s1];
            else mm[s1] = cnt++, idx1 = mm[s1];
            if (mm.find(s2) != mm.end()) idx2 = mm[s2];
            else mm[s2] = cnt++, idx2 = mm[s2];
            asso[idx1][idx2] += 10, asso[idx2][idx1] += 10;
        } else {
            int pos2, pos3;
            for (pos2 = pos1 + 1; buff[pos2] != ' '; pos2++) ;
            for (pos3 = pos2 + 1; buff[pos3] != '\''; pos3++) ;
            string s1 = string(buff, buff + pos1), s2 = string(buff + pos2 + 1, buff + pos3);
            if (mm.find(s1) != mm.end()) idx1 = mm[s1];
            else mm[s1] = cnt++, idx1 = mm[s1];
            if (mm.find(s2) != mm.end()) idx2 = mm[s2];
            else mm[s2] = cnt++, idx2 = mm[s2];
            asso[idx1][idx2] += 5, asso[idx2][idx1] += 5;
        }
    }
    string x = string(name);
    vector<string> ss;
    for (auto ele : mm) {
        if (ele.first != x) ss.push_back(ele.first);
    }
    std::sort(ss.begin(), ss.end(), [] (const string &a, const string &b) {
              int idx1 = mm[a], idx2 = mm[b];
              if (asso[0][idx1] == asso[0][idx2]) return a < b;
              else return asso[0][idx1] > asso[0][idx2];
              });
    for (int i = 0; i < ss.size(); i++) printf("%s\n", ss[i].data());

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define fi first
#define se second
typedef long long ll;
typedef long double ldb;
typedef vector <int> vi;
typedef vector <vi> vvi;

void printv(auto v)
{
	for(auto i : v)
		cout << i << " ";
	cout << endl;
}

bool compst(const pair<string, int> &p1, const pair<string, int> &p2)
{
	return p1.first < p2.first;
}

bool comppr(const pair<string, int> &p1, const pair<string, int> &p2)
{
	return p1.second > p2.second;
}

int main(void)
{
	string yname; cin >> yname;
	int n; cin >> n;
	vector<vector<string>> queries(n);
	cin.ignore();

	map<string, int> ps;

	for(int i = 0; i < n; i++)
	{
		string s;
		getline(cin, s);

    	istringstream iss(s);
    	for(string s; iss >> s;)
    		queries[i].push_back(s);

    	if(queries[i][1][0] == 'p')
    	{
    		queries[i][3].pop_back();
    		queries[i][3].pop_back();

    		if(queries[i][3] == yname || queries[i][0] == yname)
    		{
    			ps[queries[i][0]] += 15;
    			ps[queries[i][3]] += 15;
    		}
    		else
    		{
    			ps[queries[i][0]] += 0;
    			ps[queries[i][3]] += 0;
    		}

    	}
    	else if(queries[i][1][0] == 'c')
    	{
    		queries[i][3].pop_back();
    		queries[i][3].pop_back();

    		if(queries[i][3] == yname || queries[i][0] == yname)
    		{
    			ps[queries[i][0]] += 10;
    			ps[queries[i][3]] += 10;
    		}
    		else
    		{
    			ps[queries[i][0]] += 0;
    			ps[queries[i][3]] += 0;
    		}
    	}
    	else if(queries[i][1][0] == 'l')
    	{
    		queries[i][2].pop_back();
    		queries[i][2].pop_back();

    		if(queries[i][2] == yname || queries[i][0] == yname)
    		{
    			ps[queries[i][0]] += 15;
    			ps[queries[i][2]] += 15;
    		}
    		else
    		{
    			ps[queries[i][0]] += 0;
    			ps[queries[i][2]] += 0;
    		}
    	}
	}

	// for(vector<string> &i : queries)
	// 	printv(i);

	vector<pair<string, int>> priority;
	for(pair<string, int> p : ps)
		priority.push_back(p);

	sort(priority.begin(), priority.end(), compst);
	stable_sort(priority.begin(), priority.end(), comppr);

	// for(pair<string, int> &p : priority)
	// 	cout << p.fi << " " << p.se << endl;
	// cout << endl;

	for(int i = 0; i < priority.size(); i++)
	{
		if(priority[i].fi == yname)
			continue;
		cout << priority[i].fi << endl;
	}

}


#include <bits/stdc++.h>
using namespace std;

#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define fi first
#define se second
typedef long long ll;
typedef long double ldb;
typedef vector <int> vi;
typedef vector <vi> vvi;

void printv(auto v)
{
	for(auto i : v)
		cout << i << " ";
	cout << endl;
}

bool compst(const pair<string, int> &p1, const pair<string, int> &p2)
{
	return p1.first < p2.first;
}

bool comppr(const pair<string, int> &p1, const pair<string, int> &p2)
{
	return p1.second > p2.second;
}

int main(void)
{
	string yname; cin >> yname;
	int n; cin >> n;
	vector<vector<string>> queries(n);
	cin.ignore();

	map<string, int> ps;

	for(int i = 0; i < n; i++)
	{
		string s;
		getline(cin, s);

    	istringstream iss(s);
    	for(string s; iss >> s;)
    		queries[i].push_back(s);

    	if(queries[i][1][0] == 'p')
    	{
    		queries[i][3].pop_back();
    		queries[i][3].pop_back();

    		if(queries[i][3] == yname || queries[i][0] == yname)
    		{
    			ps[queries[i][0]] += 15;
    			ps[queries[i][3]] += 15;
    		}
    		else
    		{
    			ps[queries[i][0]] += 0;
    			ps[queries[i][3]] += 0;
    		}

    	}
    	else if(queries[i][1][0] == 'c')
    	{
    		queries[i][3].pop_back();
    		queries[i][3].pop_back();

    		if(queries[i][3] == yname || queries[i][0] == yname)
    		{
    			ps[queries[i][0]] += 10;
    			ps[queries[i][3]] += 10;
    		}
    		else
    		{
    			ps[queries[i][0]] += 0;
    			ps[queries[i][3]] += 0;
    		}
    	}
    	else if(queries[i][1][0] == 'l')
    	{
    		queries[i][2].pop_back();
    		queries[i][2].pop_back();

    		if(queries[i][2] == yname || queries[i][0] == yname)
    		{
    			ps[queries[i][0]] += 5;
    			ps[queries[i][2]] += 5;
    		}
    		else
    		{
    			ps[queries[i][0]] += 0;
    			ps[queries[i][2]] += 0;
    		}
    	}
	}

	// for(vector<string> &i : queries)
	// 	printv(i);

	vector<pair<string, int>> priority;
	for(pair<string, int> p : ps)
		priority.push_back(p);

	sort(priority.begin(), priority.end(), compst);
	stable_sort(priority.begin(), priority.end(), comppr);

	// for(pair<string, int> &p : priority)
	// 	cout << p.fi << " " << p.se << endl;
	// cout << endl;

	for(int i = 0; i < priority.size(); i++)
	{
		if(priority[i].fi == yname)
			continue;
		cout << priority[i].fi << endl;
	}

}


#include <bits/stdc++.h>
#define pb push_back
#define sz(x) ll((x).size())
#define all(v) (v).begin(),(v).end()
#define trace(x) cerr << #x << " = " << x << endl
#define fastio ios_base::sync_with_stdio(0);cin.tie(0)
#define For(i,a,b) for(ll i = ll(a); i < ll(b); ++i)
#define roF(i,a,b) for(ll i = ll(a); i >= ll(b); i--)
using namespace std;

typedef long long ll;
typedef vector<int> vi;

const ll N=1e6;

vector<string> names;
vi score;
vector<string> ans[1505];

void add(string name, int points){
	bool found=0;
	For(i,0,sz(names)){
		if(names[i]==name){
			score[i]+=points;
			found=1;
			break;
		}
	}
	if(!found){
		names.pb(name);
		score.pb(points);
	}
}

int main(){
	fastio;
	string name; cin>>name;
	int n; cin>>n;
	cin.ignore();
	string posts = " posted on ";
	string comments = " commented on ";
	string likes = " likes ";
	string wall = "'s wall";
	string post = "'s post";
	string x, y;
	string s;
	while(n--){
		getline(cin,s);
		For(i,0,sz(s)-sz(posts)) if(s.substr(i,sz(posts))==posts){
			x = s.substr(0,i);
			y = s.substr(i+sz(posts),sz(s)-(i+sz(posts))-sz(wall));
			if(x==name) add(y,15);
			else if(y==name) add(x,15);
			else{
				add(x,0);
				add(y,0);
			}
		}
		For(i,0,sz(s)-sz(comments)) if(s.substr(i,sz(comments))==comments){
			x = s.substr(0,i);
			y = s.substr(i+sz(comments),sz(s)-(i+sz(comments))-sz(post));
			if(x==name) add(y,10);
			else if(y==name) add(x,10);
			else{
				add(x,0);
				add(y,0);
			}
		}
		For(i,0,sz(s)-sz(likes)) if(s.substr(i,sz(likes))==likes){
			x = s.substr(0,i);
			y = s.substr(i+sz(likes),sz(s)-(i+sz(likes))-sz(post));
			if(x==name) add(y,5);
			else if(y==name) add(x,5);
			else{
				add(x,0);
				add(y,0);
			}
		}
	}
	For(i,0,sz(names)){
		ans[score[i]].pb(names[i]);
	}
	roF(i,1500,0){
		sort(all(ans[i]));
		for(auto x: ans[i])
			cout << x << endl;
	}
	return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define sz(x) ll((x).size())
#define all(v) (v).begin(),(v).end()
#define trace(x) cerr << #x << " = " << x << endl
#define fastio ios_base::sync_with_stdio(0);cin.tie(0)
#define For(i,a,b) for(ll i = ll(a); i < ll(b); ++i)
#define roF(i,a,b) for(ll i = ll(a); i >= ll(b); i--)
using namespace std;

typedef long long ll;
typedef vector<int> vi;

const ll N=1e6;

vector<string> names;
vi score;
vector<string> ans[1505];

void add(string name, int points){
	bool found=0;
	For(i,0,sz(names)){
		if(names[i]==name){
			score[i]+=points;
			found=1;
			break;
		}
	}
	if(!found){
		names.pb(name);
		score.pb(points);
	}
}

int main(){
	fastio;
	string name; cin>>name;
	int n; cin>>n;
	cin.ignore();
	string posts = " posted on ";
	string comments = " commented on ";
	string likes = " likes ";
	string wall = "'s wall";
	string post = "'s post";
	string x, y;
	string s;
	while(n--){
		getline(cin,s);
		For(i,0,sz(s)-sz(posts)) if(s.substr(i,sz(posts))==posts){
			x = s.substr(0,i);
			y = s.substr(i+sz(posts),sz(s)-(i+sz(posts))-sz(wall));
			if(x!=name) add(x,15);
			if(y!=name) add(y,15);
		}
		For(i,0,sz(s)-sz(comments)) if(s.substr(i,sz(comments))==comments){
			x = s.substr(0,i);
			y = s.substr(i+sz(comments),sz(s)-(i+sz(comments))-sz(post));
			if(x!=name) add(x,10);
			if(y!=name) add(y,10);
		}
		For(i,0,sz(s)-sz(likes)) if(s.substr(i,sz(likes))==likes){
			x = s.substr(0,i);
			y = s.substr(i+sz(likes),sz(s)-(i+sz(likes))-sz(post));
			if(x!=name) add(x,10);
			if(y!=name) add(y,10);
		}
	}
	For(i,0,sz(names))
		ans[score[i]].pb(names[i]);
	roF(i,1500,0){
		sort(all(ans[i]));
		for(auto x: ans[i])
			cout << x << endl;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
    string name, a, b, x;
    cin >> name;
    int n;
    cin >> n;
    map<string, int> score; // this map will keep a track of score for each user
    for(int i = 0; i < n; i++){
        cin >> a >> x;
        score[a];
        if(x == "posted"){
            cin >> x >> b >> x;
            b.erase(b.size()-2, 2);
            if(a == name || b == name){
                score[a] += 15;
                score[b] += 15;
            }
        }
        else if(x == "commented"){
            cin >> x >> b >> x;
            b.erase(b.size()-2, 2);
            if(a == name || b == name){
                score[a] += 10;
                score[b] += 10;
            }
        }
        else{
            cin >> b >> x;
            b.erase(b.size()-2, 2);
            if(a == name || b == name){
                score[a] +=5;
                score[b] += 5;
            }
        }
        score[b];
    }
    vector<pair<int, string> > ans;
    for(auto x: score){
        ans.push_back(make_pair(x.second, x.first));
    }
    sort(ans.begin(), ans.end(),greater<pair<int, string> >());
    for(auto x : ans){
        if(x.second != name){
            cout<<x.second<<"\n";
        }
    }
    return 0;

}


#include<bits/stdc++.h>
using namespace std;
int main(){
    string name, a, b, x;
    cin >> name;
    int n;
    cin >> n;
    map<string, int> score; // this map will keep a track of score for each user
    for(int i = 0; i < n; i++){
        cin >> a >> x;
        score[a];
        if(x == "posted"){
            cin >> x >> b >> x;
            b.erase(b.size()-2, 2);
            if(a == name || b == name){
                score[a] += 15;
                score[b] += 15;
            }
        }
        else if(x == "commented"){
            cin >> x >> b >> x;
            b.erase(b.size()-2, 2);
            if(a == name || b == name){
                score[a] += 10;
                score[b] += 10;
            }
        }
        else{
            cin >> b >> x;
            b.erase(b.size()-2, 2);
            if(a == name || b == name){
                score[a] +=5;
                score[b] += 5;
            }
        }
        score[b];
    }
    vector<pair<int, string> > ans;
    for(auto x: score){
        ans.push_back(make_pair(-x.second, x.first));
    }
    sort(ans.begin(), ans.end());
    for(auto x : ans){
        if(x.second != name){
            cout<<x.second<<"\n";
        }
    }
    return 0;

}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <map>

using namespace std;

bool compare(const pair <string, int> &a, const pair <string, int> &b)
{
    if (a.second == b.second)
        return a.first < b.first
    return a.second > b.second;
}

int main()
{
    ios_base::sync_with_stdio(false);
    string name, X, Y, C, s1, s2;
    cin >> name;
    int n;
    cin >> n;
    map <string, int> all;
    vector <pair <string, int> > ans;
    while (n--)
    {
        cin >> X >> C;
        if (C[0] != 'l')
            cin >> s1;
        cin >> Y >> s2;
        int p(Y.size() - 1);
        while (Y[p] != '\'')
            --p;
        Y = Y.substr(0, p);
        if (X == name)
            swap(X, Y);
        if (!all.count(X) && X != name)
        {
            ans.push_back({ X, 0 });
            all[X] = 0;
        }
        if (!all.count(Y) && Y != name)
        {
            ans.push_back({ Y, 0 });
            all[Y] = 0;
        }
        if (Y == name)
            switch (C[0])
            {
            case 'p':
                all[X] += 15;
                break;
            case 'c':
                all[X] += 10;
                break;
            case 'l':
                all[X] += 5;
                break;
            }
    }
    for (int i(0); i < ans.size(); ++i)
        ans[i].second = all[ans[i].first];
    sort(ans.begin(), ans.end(), compare);
    for (int i(0); i < ans.size(); ++i)
        cout << ans[i].first << '\n';
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <map>

using namespace std;

bool compare(const pair <string, int> &a, const pair <string, int> &b)
{
    if (a.second == b.second)
        return a.first < b.first;
    return a.second > b.second;
}

int main()
{
    ios_base::sync_with_stdio(false);
    string name, X, Y, C, s1, s2;
    cin >> name;
    int n;
    cin >> n;
    map <string, int> all;
    vector <pair <string, int> > ans;
    while (n--)
    {
        cin >> X >> C;
        if (C[0] != 'l')
            cin >> s1;
        cin >> Y >> s2;
        int p(Y.size() - 1);
        while (Y[p] != '\'')
            --p;
        Y = Y.substr(0, p);
        if (X == name)
            swap(X, Y);
        if (!all.count(X) && X != name)
        {
            ans.push_back({ X, 0 });
            all[X] = 0;
        }
        if (!all.count(Y) && Y != name)
        {
            ans.push_back({ Y, 0 });
            all[Y] = 0;
        }
        if (Y == name)
            switch (C[0])
            {
            case 'p':
                all[X] += 15;
                break;
            case 'c':
                all[X] += 10;
                break;
            case 'l':
                all[X] += 5;
                break;
            }
    }
    for (int i(0); i < ans.size(); ++i)
        ans[i].second = all[ans[i].first];
    sort(ans.begin(), ans.end(), compare);
    for (int i(0); i < ans.size(); ++i)
        cout << ans[i].first << '\n';
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <map>

using namespace std;

bool compare(const pair <string, int> &a, const pair <string, int> &b)
{
    return a.second > b.second;
}

int main()
{
    ios_base::sync_with_stdio(false);
    string name, X, Y, C, s1, s2;
    cin >> name;
    int n;
    cin >> n;
    map <string, int> all;
    vector <pair <string, int> > ans;
    while (n--)
    {
        cin >> X >> C;
        if (C[0] != 'l')
            cin >> s1;
        cin >> Y >> s2;
        int p(Y.size() - 1);
        while (Y[p] != '\'')
            --p;
        Y = Y.substr(0, p);
        if (X == name)
            swap(X, Y);
        if (!all.count(X) && X != name)
        {
            ans.push_back({ X, 0 });
            all[X] = 0;
        }
        if (!all.count(Y) && Y != name)
        {
            ans.push_back({ Y, 0 });
            all[Y] = 0;
        }
        if (Y == name)
            switch (C[0])
            {
            case 'p':
                all[X] += 15;
                break;
            case 'c':
                all[X] += 10;
                break;
            case 'l':
                all[X] += 5;
                break;
            }
    }
    for (int i(0); i < ans.size(); ++i)
        ans[i].second = all[ans[i].first];
    sort(ans.begin(), ans.end(), compare);
    for (int i(0); i < ans.size(); ++i)
        cout << ans[i].first << '\n';
}


#include <bits/stdc++.h>

using namespace std;

#define sz(v) v.size()
#define ll long long
#define all(v)  v.begin() , v.end()
#define rall(v)  v.rbegin() , v.rend()
#define pf push_front
#define pb push_back
#define For( v , s , n )  for(v i = s ; i < n ; ++i)
#define For( v , s , n )  for(int v = s ; v < n ; ++v)
#define fast FastInputOutput() ;

void File_input(){
freopen( "input.txt" , "r" , stdin ) ;
freopen( "output.txt" , "w" , stdout ) ;
}

void FastInputOutput(){
    ios_base :: sync_with_stdio( 0 ) ;
    cin.tie( 0 ) ;
    cout.tie( 0 ) ;
}

map < string , int > nam;
vector < pair < string  , int > > ans ;

bool cmp( pair < string  , int > s , pair < string  , int > f  ){

 if( s.second == f.second ){
    if( s.first < f.first )
        return 1 ;
    else
        return 0 ;
 }
 else if( s.second > f.second )
        return 1 ;

 return 0 ;

}

int main(){

    string my , str , tmp , xt ;
    cin >> my ;
    int n , idx ;
    cin >> n ;
        getline( cin  , str ) ;
    while( n-- ){
        getline( cin  , str ) ;
        idx = str.find(' ') ;
        tmp = "" ;
        xt = str.substr( 0 , idx ) ;

        if( str.find( "posted on" ) != -1 ){
            for( int i = sz( str ) - 8 ; str[ i ] != ' ' ; i-- )
                tmp += str[ i ] ;
            reverse( all( tmp ) ) ;
           if( xt == my )
             nam[ tmp ] += 15 ;
           else
             nam[ xt ] ;

           if( tmp == my )
              nam[ xt ] += 15 ;
           else
              nam[ tmp ] ;

        }
        else if( str.find( "commented on" ) != -1 ){
            for( int i = sz( str ) - 8 ; str[ i ] != ' ' ; i-- )
                tmp += str[ i ] ;
            reverse( all( tmp ) ) ;
           if( xt == my )
             nam[ tmp ] += 10 ;
           else
             nam[ xt ] ;

           if( tmp == my )
              nam[ xt ] += 10 ;
           else
              nam[ tmp ] ;

        }
        else{
            for( int i = sz( str ) - 8 ; str[ i ] != ' ' ; i-- )
                tmp += str[ i ] ;
            reverse( all( tmp ) ) ;

           if( xt == my )
             nam[ tmp ] += 5 ;
           else
             nam[ xt ] ;

           if( tmp == my )
              nam[ xt ] += 5 ;
           else
              nam[ tmp ] ;

        }
    }

      for( auto i : nam ){
        if( i.first != my )
          ans.pb( { i.first , i.second } ) ;
      }

      sort( all( ans ) , cmp ) ;

      for( int i = 0 ; i < ans.size() ; i++ )
          cout << ans[ i ].first << "\n" ;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define sz(v) v.size()
#define ll long long
#define all(v)  v.begin() , v.end()
#define rall(v)  v.rbegin() , v.rend()
#define pf push_front
#define pb push_back
#define For( v , s , n )  for(v i = s ; i < n ; ++i)
#define For( v , s , n )  for(int v = s ; v < n ; ++v)
#define fast FastInputOutput() ;

void File_input(){
freopen( "input.txt" , "r" , stdin ) ;
freopen( "output.txt" , "w" , stdout ) ;
}

void FastInputOutput(){
    ios_base :: sync_with_stdio( 0 ) ;
    cin.tie( 0 ) ;
    cout.tie( 0 ) ;
}

map < string , int > nam;
vector < pair < string  , int > > ans ;

bool cmp( pair < string  , int > s , pair < string  , int > f  ){

 if( s.second == f.second ){
    if( s.first < f.first )
        return 1 ;
 }
 else if( s.second > f.second )
        return 1 ;
 return 0 ;

}

int main(){

    string my , str , tmp ;
    cin >> my ;
    int n , idx ;
    cin >> n ;
        getline( cin  , str ) ;
    while( n-- ){
        getline( cin  , str ) ;
        idx = str.find(' ') ;
        tmp = "" ;
        if( str.find( "posted on" ) != -1 ){
            for( int i = sz( str ) - 8 ; str[ i ] != ' ' ; i-- )
                tmp += str[ i ] ;
            reverse( all( tmp ) ) ;

            nam[ str.substr( 0 , idx ) ] += 15 ;
            nam[ tmp ] += 15 ;
        }
        else if( str.find( "commented on" ) != -1 ){
            for( int i = sz( str ) - 8 ; str[ i ] != ' ' ; i-- )
                tmp += str[ i ] ;
            reverse( all( tmp ) ) ;

            nam[ str.substr( 0 , idx ) ] += 10 ;
            nam[ tmp ] += 10 ;

        }
        else{
            for( int i = sz( str ) - 8 ; str[ i ] != ' ' ; i-- )
                tmp += str[ i ] ;
            reverse( all( tmp ) ) ;
            nam[ str.substr( 0 , idx ) ] += 5 ;
            nam[ tmp ] += 5 ;
        }
    }

      for( auto i : nam ){
        if( i.first != my )
          ans.pb( { i.first , i.second } ) ;
      }

      sort( all( ans ) , cmp ) ;

      for( int i = 0 ; i < ans.size() ; i++ )
          cout << ans[ i ].first << "\n" ;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define sz(v) v.size()
#define ll long long
#define all(v)  v.begin() , v.end()
#define rall(v)  v.rbegin() , v.rend()
#define pf push_front
#define pb push_back
#define For( v , s , n )  for(v i = s ; i < n ; ++i)
#define For( v , s , n )  for(int v = s ; v < n ; ++v)
#define fast FastInputOutput() ;

void File_input(){
freopen( "input.txt" , "r" , stdin ) ;
freopen( "output.txt" , "w" , stdout ) ;
}

void FastInputOutput(){
    ios_base :: sync_with_stdio( 0 ) ;
    cin.tie( 0 ) ;
    cout.tie( 0 ) ;
}

map < string , int > nam;
vector < pair < string  , int > > ans ;

bool cmp( pair < string  , int > s , pair < string  , int > f  ){

 if( s.second == f.second ){
    if( s.first < f.first )
        return 1 ;
    else
        return 0 ;
 }
 else if( s.second > f.second )
        return 1 ;

 return 0 ;

}

int main(){
    fast
    string my , str , tmp , xt ;
    cin >> my ;
    int n , idx ;
    cin >> n ;
        getline( cin  , str ) ;
    while( n-- ){
        getline( cin  , str ) ;
        idx = str.find(' ') ;
        tmp = "" ;
        xt = str.substr( 0 , idx ) ;

        if( str.find( "posted on" ) != -1 ){
            for( int i = sz( str ) - 8 ; str[ i ] != ' ' ; i-- )
                tmp += str[ i ] ;
            reverse( all( tmp ) ) ;
           if( xt == my )
             nam[ tmp ] += 15 ;
           else
             nam[ xt ] ;

           if( tmp == my )
              nam[ xt ] += 15 ;
           else
              nam[ tmp ] ;

        }
        else if( str.find( "commented on" ) != -1 ){
            for( int i = sz( str ) - 8 ; str[ i ] != ' ' ; i-- )
                tmp += str[ i ] ;
            reverse( all( tmp ) ) ;
           if( xt == my )
             nam[ tmp ] += 10 ;
           else
             nam[ xt ] ;

           if( tmp == my )
              nam[ xt ] += 10 ;
           else
              nam[ tmp ] ;

        }
        else{
            for( int i = sz( str ) - 8 ; str[ i ] != ' ' ; i-- )
                tmp += str[ i ] ;
            reverse( all( tmp ) ) ;

           if( xt == my )
             nam[ tmp ] += 5 ;
           else
             nam[ xt ] ;

           if( tmp == my )
              nam[ xt ] += 5 ;
           else
              nam[ tmp ] ;

        }
    }

      for( auto i : nam ){
        if( i.first != my )
          ans.pb( { i.first , i.second } ) ;
      }

      sort( all( ans ) , cmp ) ;

      for( int i = 0 ; i < ans.size() ; i++ )
          cout << ans[ i ].first << "\n" ;

    return 0;
}


#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <cstdio>
#include <stdio.h>
#include <vector>
#include <math.h>
#include <stdlib.h>
#include <algorithm>
#include <string>
#include <set>
#include <cstring>
#include <queue>
#include <math.h>
#include <map>

using namespace std;

#define lp(i,n) for(int i=0;i<n;i++)
#define rep(i,j,n) for(int i=j ; i<n; i++)
#define ll long long
#define ull unsigned long long
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define clr(a,n)  memset(a,n,sizeof a)
#define OO 1e9+20
#define All(a) a.begin(),a.end()
#define nextI nextInt()
#define nextD nextDouble()
#define nextL nextLong()
#define nextC nextChar()
#define Nitros ios::sync_with_stdio(false)

typedef vector<int> vi;
typedef vector< vi> vvi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<ll> vll;

int nextInt() { int x; scanf("%d", &x); return x; }
double nextDouble() { double x; scanf("%lf", &x); return x; }
ll nextLong() { ll x; scanf("%I64d", &x); return x; }
char nextChar() { char x; scanf("%c", &x); return x; }

int xd[] = { 0,1,0,-1 };
int yd[] = { 1,0,-1,0 };

int xx[] = { 0,1,0 ,-1,1,-1,1 ,-1 };
int yy[] = { 1,0,-1, 0,1, 1,-1,-1 };

int kx[] = { -2,-1, -2,-1,1,2 ,2 ,1 };
int ky[] = { 1, 2, -1,-2,-2,-1 ,1 ,2 };

uint64_t bigPow(uint64_t base, uint64_t p)
{
	uint64_t temp = 1;
	while (p--)
	{
		temp *= base;
	}
	return temp;
}

bool valid(int x, int y, int n, int m)
{
	if (x >= 0 && x < n && y >= 0 && y < m)
		return true;
	return false;
}

ll sumOfDigit(string s)
{
	int ans = 0;
	lp(i, s.length())
	{
		ans += s[i] - '0';
	}
	return ans;
}
string s;
string name1 , name2, name;
int get_points(string s) {
	string word;
	int type = 0 , idx=0;

	lp(i, s.length())
	{
		if (s[i] == ' ')
		{
			if (idx == 0)
			{
				name1 = word;

			}
			if (idx == 1)
			{
				if (word == "posted")
					type = 1;
				else if (word == "commented")
					type = 2;
				else
					type = 3;
			}
			if ((idx == 2 && type == 3) || (idx == 3 && type != 2))
				name2 = word.substr(0, word.length() - 2);

			idx++;
			word = "";
		}
		else
     		word += s[i];
	}
	if (type == 1)
		return 15;
	if (type == 2)
		return 10;
	if (type == 3)
		return 5;
}

int main()
{

	Nitros;
	map<string, int> ans;
	int n;
	string person1, person2, op, t1, t2, t3;
	cin >> name;
	cin >> n;
	lp(i, n)
	{
		cin >> person1 >> op;
		if (op == "posted")
		{
			cin >> t1;
			cin >> person2 >> t2;
			person2 = person2.substr(0, person2.length() - 2);
			ans[person1] += 0;
			ans[person2] += 0;
			if (person1 == name || person2 == name)
			{
				ans[person1] += 15;
				ans[person2] += 15;
			}

		}

		if (op == "commented")
		{
			cin >> t1;
			cin >> person2 >> t2;
			person2 = person2.substr(0, person2.length() - 2);
			ans[person1] += 0;
			ans[person2] += 0;
			if (person1 == name || person2 == name)
			{
				ans[person1] += 10;
				ans[person2] += 10;
			}
		}

		if (op == "likes")
		{
			cin >> person2 >> t2;
			person2 = person2.substr(0, person2.length() - 2);
			ans[person1] += 0;
			ans[person2] += 0;
			if (person1 == name || person2 == name)
			{
				ans[person1] += 5;
				ans[person2] += 5;
			}
		}

	}

	map<string, int> ::iterator it = ans.begin();
	vector<pair<int , string> >ans2;
	for (; it != ans.end(); it++)
	{
		//cout << it->first << endl;
		ans2.push_back(make_pair(it->second*-1 , it->first));
	}
	sort(All(ans2));
	lp(i,ans2.size())
		if(ans2[i].second !=name)
			cout << ans2[i].second << endl;

}

//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
/*
						   *         *
								`
			   +
					  '
				  *          +    *
					  +   /\
		 +              .'  '.   *
				*      /======\      +
					  ;:.  _   ;
					  |:. (_)  |
					  |:.  _   |
			+         |:. (_)  |          *
					  ;:.      ;
					.' \:.    / `.
				   / .-'':._.'`-. \
				   |/    /||\    \|
				 _..--"""````"""--.._
		   _.-'``                    ``'-._
		 -'                                '-
					   APOLLO 11

*/


#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <cstdio>
#include <stdio.h>
#include <vector>
#include <math.h>
#include <stdlib.h>
#include <algorithm>
#include <string>
#include <set>
#include <cstring>
#include <queue>
#include <math.h>
#include <map>

using namespace std;

#define lp(i,n) for(int i=0;i<n;i++)
#define rep(i,j,n) for(int i=j ; i<n; i++)
#define ll long long
#define ull unsigned long long
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define clr(a,n)  memset(a,n,sizeof a)
#define OO 1e9+20
#define All(a) a.begin(),a.end()
#define nextI nextInt()
#define nextD nextDouble()
#define nextL nextLong()
#define nextC nextChar()
#define Nitros ios::sync_with_stdio(false)

typedef vector<int> vi;
typedef vector< vi> vvi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<ll> vll;

int nextInt() { int x; scanf("%d", &x); return x; }
double nextDouble() { double x; scanf("%lf", &x); return x; }
ll nextLong() { ll x; scanf("%I64d", &x); return x; }
char nextChar() { char x; scanf("%c", &x); return x; }

int xd[] = { 0,1,0,-1 };
int yd[] = { 1,0,-1,0 };

int xx[] = { 0,1,0 ,-1,1,-1,1 ,-1 };
int yy[] = { 1,0,-1, 0,1, 1,-1,-1 };

int kx[] = { -2,-1, -2,-1,1,2 ,2 ,1 };
int ky[] = { 1, 2, -1,-2,-2,-1 ,1 ,2 };

uint64_t bigPow(uint64_t base, uint64_t p)
{
	uint64_t temp = 1;
	while (p--)
	{
		temp *= base;
	}
	return temp;
}

bool valid(int x, int y, int n, int m)
{
	if (x >= 0 && x < n && y >= 0 && y < m)
		return true;
	return false;
}

ll sumOfDigit(string s)
{
	int ans = 0;
	lp(i, s.length())
	{
		ans += s[i] - '0';
	}
	return ans;
}
string s;
string name1 , name2, name;
int get_points(string s) {
	string word;
	int type = 0 , idx=0;

	lp(i, s.length())
	{
		if (s[i] == ' ')
		{
			if (idx == 0)
			{
				name1 = word;

			}
			if (idx == 1)
			{
				if (word == "posted")
					type = 1;
				else if (word == "commented")
					type = 2;
				else
					type = 3;
			}
			if ((idx == 2 && type == 3) || (idx == 3 && type != 2))
				name2 = word.substr(0, word.length() - 2);

			idx++;
			word = "";
		}
		else
     		word += s[i];
	}
	if (type == 1)
		return 15;
	if (type == 2)
		return 10;
	if (type == 3)
		return 5;
}

int main()
{

	Nitros;
	map<string, int> ans;
	int n;
	cin >> name;
	cin >> n;
	char a;
	a = getchar();
	lp(i, n)
	{
		getline(cin, s);
		int p = get_points(s);
		ans[name1] += p;
		ans[name2] += p;
	}

	map<string, int> ::iterator it = ans.begin();
	vector<pair<int , string> >ans2;
	for (; it != ans.end(); it++)
	{
		ans2.push_back(make_pair(it->second , it->first));
	}
	sort(All(ans2));
	lp(i,ans2.size())
		if(ans2[i].second !=name)
			cout << ans2[i].second << endl;

}

//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
/*
						   *         *
								`
			   +
					  '
				  *          +    *
					  +   /\
		 +              .'  '.   *
				*      /======\      +
					  ;:.  _   ;
					  |:. (_)  |
					  |:.  _   |
			+         |:. (_)  |          *
					  ;:.      ;
					.' \:.    / `.
				   / .-'':._.'`-. \
				   |/    /||\    \|
				 _..--"""````"""--.._
		   _.-'``                    ``'-._
		 -'                                '-
					   APOLLO 11

*/


#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <cstdio>
#include <stdio.h>
#include <vector>
#include <math.h>
#include <stdlib.h>
#include <algorithm>
#include <string>
#include <set>
#include <cstring>
#include <queue>
#include <math.h>
#include <map>

using namespace std;

#define lp(i,n) for(int i=0;i<n;i++)
#define rep(i,j,n) for(int i=j ; i<n; i++)
#define ll long long
#define ull unsigned long long
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define clr(a,n)  memset(a,n,sizeof a)
#define OO 1e9+20
#define All(a) a.begin(),a.end()
#define nextI nextInt()
#define nextD nextDouble()
#define nextL nextLong()
#define nextC nextChar()
#define Nitros ios::sync_with_stdio(false)

typedef vector<int> vi;
typedef vector< vi> vvi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<ll> vll;

int nextInt() { int x; scanf("%d", &x); return x; }
double nextDouble() { double x; scanf("%lf", &x); return x; }
ll nextLong() { ll x; scanf("%I64d", &x); return x; }
char nextChar() { char x; scanf("%c", &x); return x; }

int xd[] = { 0,1,0,-1 };
int yd[] = { 1,0,-1,0 };

int xx[] = { 0,1,0 ,-1,1,-1,1 ,-1 };
int yy[] = { 1,0,-1, 0,1, 1,-1,-1 };

int kx[] = { -2,-1, -2,-1,1,2 ,2 ,1 };
int ky[] = { 1, 2, -1,-2,-2,-1 ,1 ,2 };

uint64_t bigPow(uint64_t base, uint64_t p)
{
	uint64_t temp = 1;
	while (p--)
	{
		temp *= base;
	}
	return temp;
}

bool valid(int x, int y, int n, int m)
{
	if (x >= 0 && x < n && y >= 0 && y < m)
		return true;
	return false;
}

ll sumOfDigit(string s)
{
	int ans = 0;
	lp(i, s.length())
	{
		ans += s[i] - '0';
	}
	return ans;
}
string s;
string name1 , name2, name;
int get_points(string s) {
	string word;
	int type = 0 , idx=0;

	lp(i, s.length())
	{
		if (s[i] == ' ')
		{
			if (idx == 0)
			{
				name1 = word;

			}
			if (idx == 1)
			{
				if (word == "posted")
					type = 1;
				else if (word == "commented")
					type = 2;
				else
					type = 3;
			}
			if ((idx == 2 && type == 3) || (idx == 3 && type != 2))
				name2 = word.substr(0, word.length() - 2);

			idx++;
			word = "";
		}
		else
     		word += s[i];
	}
	if (type == 1)
		return 15;
	if (type == 2)
		return 10;
	if (type == 3)
		return 5;
}

int main()
{

	Nitros;
	map<string, int> ans;
	int n;
	cin >> name;
	cin >> n;
	char a;
	a = getchar();
	lp(i, n)
	{
		getline(cin, s);
		int p = get_points(s);
		ans[name1] += p;
		ans[name2] += p;
	}

	map<string, int> ::iterator it = ans.begin();
	vector<pair<int , string> >ans2;
	for (; it != ans.end(); it++)
	{
		ans2.push_back(make_pair(it->second*-1 , it->first));
	}
	sort(All(ans2));
	lp(i,ans2.size())
		//if(ans2[i].second !=name)
			cout << ans2[i].second << endl;

}

//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
/*
						   *         *
								`
			   +
					  '
				  *          +    *
					  +   /\
		 +              .'  '.   *
				*      /======\      +
					  ;:.  _   ;
					  |:. (_)  |
					  |:.  _   |
			+         |:. (_)  |          *
					  ;:.      ;
					.' \:.    / `.
				   / .-'':._.'`-. \
				   |/    /||\    \|
				 _..--"""````"""--.._
		   _.-'``                    ``'-._
		 -'                                '-
					   APOLLO 11

*/


#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <cstdio>
#include <stdio.h>
#include <vector>
#include <math.h>
#include <stdlib.h>
#include <algorithm>
#include <string>
#include <set>
#include <cstring>
#include <queue>
#include <math.h>
#include <map>

using namespace std;

#define lp(i,n) for(int i=0;i<n;i++)
#define rep(i,j,n) for(int i=j ; i<n; i++)
#define ll long long
#define ull unsigned long long
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define clr(a,n)  memset(a,n,sizeof a)
#define OO 1e9+20
#define All(a) a.begin(),a.end()
#define nextI nextInt()
#define nextD nextDouble()
#define nextL nextLong()
#define nextC nextChar()
#define Nitros ios::sync_with_stdio(false)

typedef vector<int> vi;
typedef vector< vi> vvi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<ll> vll;

int nextInt() { int x; scanf("%d", &x); return x; }
double nextDouble() { double x; scanf("%lf", &x); return x; }
ll nextLong() { ll x; scanf("%I64d", &x); return x; }
char nextChar() { char x; scanf("%c", &x); return x; }

int xd[] = { 0,1,0,-1 };
int yd[] = { 1,0,-1,0 };

int xx[] = { 0,1,0 ,-1,1,-1,1 ,-1 };
int yy[] = { 1,0,-1, 0,1, 1,-1,-1 };

int kx[] = { -2,-1, -2,-1,1,2 ,2 ,1 };
int ky[] = { 1, 2, -1,-2,-2,-1 ,1 ,2 };

uint64_t bigPow(uint64_t base, uint64_t p)
{
	uint64_t temp = 1;
	while (p--)
	{
		temp *= base;
	}
	return temp;
}

bool valid(int x, int y, int n, int m)
{
	if (x >= 0 && x < n && y >= 0 && y < m)
		return true;
	return false;
}

ll sumOfDigit(string s)
{
	int ans = 0;
	lp(i, s.length())
	{
		ans += s[i] - '0';
	}
	return ans;
}
string s;
string name1 , name2, name;
int get_points(string s) {
	string word;
	int type = 0 , idx=0;

	lp(i, s.length())
	{
		if (s[i] == ' ')
		{
			if (idx == 0)
			{
				name1 = word;

			}
			if (idx == 1)
			{
				if (word == "posted")
					type = 1;
				else if (word == "commented")
					type = 2;
				else
					type = 3;
			}
			if ((idx == 2 && type == 3) || (idx == 3 && type != 2))
				name2 = word.substr(0, word.length() - 2);

			idx++;
			word = "";
		}
		else
     		word += s[i];
	}
	if (type == 1)
		return 15;
	if (type == 2)
		return 10;
	if (type == 3)
		return 5;
}

int main()
{

	Nitros;
	map<string, int> ans;
	int n;
	cin >> name;
	cin >> n;
	char a;
	a = getchar();
	lp(i, n)
	{
		getline(cin, s);
		int p = get_points(s);
		ans[name1] += p;
		ans[name2] += p;
	}

	map<string, int> ::iterator it = ans.begin();
	vector<pair<int , string> >ans2;
	for (; it != ans.end(); it++)
	{
		cout << it->first << endl;
		ans2.push_back(make_pair(it->second*-1 , it->first));
	}
	sort(All(ans2));
	lp(i,ans2.size())
		//if(ans2[i].second !=name)
			cout << ans2[i].second << endl;

}

//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
/*
						   *         *
								`
			   +
					  '
				  *          +    *
					  +   /\
		 +              .'  '.   *
				*      /======\      +
					  ;:.  _   ;
					  |:. (_)  |
					  |:.  _   |
			+         |:. (_)  |          *
					  ;:.      ;
					.' \:.    / `.
				   / .-'':._.'`-. \
				   |/    /||\    \|
				 _..--"""````"""--.._
		   _.-'``                    ``'-._
		 -'                                '-
					   APOLLO 11

*/


#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <cstdio>
#include <stdio.h>
#include <vector>
#include <math.h>
#include <stdlib.h>
#include <algorithm>
#include <string>
#include <set>
#include <cstring>
#include <queue>
#include <math.h>
#include <map>

using namespace std;

#define lp(i,n) for(int i=0;i<n;i++)
#define rep(i,j,n) for(int i=j ; i<n; i++)
#define ll long long
#define ull unsigned long long
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define clr(a,n)  memset(a,n,sizeof a)
#define OO 1e9+20
#define All(a) a.begin(),a.end()
#define nextI nextInt()
#define nextD nextDouble()
#define nextL nextLong()
#define nextC nextChar()
#define Nitros ios::sync_with_stdio(false)

typedef vector<int> vi;
typedef vector< vi> vvi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<ll> vll;

int nextInt() { int x; scanf("%d", &x); return x; }
double nextDouble() { double x; scanf("%lf", &x); return x; }
ll nextLong() { ll x; scanf("%I64d", &x); return x; }
char nextChar() { char x; scanf("%c", &x); return x; }

int xd[] = { 0,1,0,-1 };
int yd[] = { 1,0,-1,0 };

int xx[] = { 0,1,0 ,-1,1,-1,1 ,-1 };
int yy[] = { 1,0,-1, 0,1, 1,-1,-1 };

int kx[] = { -2,-1, -2,-1,1,2 ,2 ,1 };
int ky[] = { 1, 2, -1,-2,-2,-1 ,1 ,2 };

uint64_t bigPow(uint64_t base, uint64_t p)
{
	uint64_t temp = 1;
	while (p--)
	{
		temp *= base;
	}
	return temp;
}

bool valid(int x, int y, int n, int m)
{
	if (x >= 0 && x < n && y >= 0 && y < m)
		return true;
	return false;
}

ll sumOfDigit(string s)
{
	int ans = 0;
	lp(i, s.length())
	{
		ans += s[i] - '0';
	}
	return ans;
}
string s;
string name1 , name2, name;
int get_points(string s) {
	string word;
	int type = 0 , idx=0;

	lp(i, s.length())
	{
		if (s[i] == ' ')
		{
			if (idx == 0)
			{
				name1 = word;

			}
			if (idx == 1)
			{
				if (word == "posted")
					type = 1;
				else if (word == "commented")
					type = 2;
				else
					type = 3;
			}
			if ((idx == 2 && type == 3) || (idx == 3 && type != 2))
				name2 = word.substr(0, word.length() - 2);

			idx++;
			word = "";
		}
		else
     		word += s[i];
	}
	if (type == 1)
		return 15;
	if (type == 2)
		return 10;
	if (type == 3)
		return 5;
}

int main()
{

	Nitros;
	map<string, int> ans;
	int n;
	string person1, person2, op, t1, t2, t3;
	cin >> name;
	cin >> n;
	lp(i, n)
	{
		cin >> person1 >> op;
		if (op == "posted")
		{
			cin >> t1;
			cin >> person2 >> t2;
			person2 = person2.substr(0, person2.length() - 2);
			ans[person1] += 15;
			ans[person2] += 15;
		}

		if (op == "commented")
		{
			cin >> t1;
			cin >> person2 >> t2;
			person2 = person2.substr(0, person2.length() - 2);
			ans[person1] += 10;
			ans[person2] += 10;
		}

		if (op == "likes")
		{
			cin >> person2 >> t2;
			person2 = person2.substr(0, person2.length() - 2);
			ans[person1] += 5;
			ans[person2] += 5;
		}

	}

	map<string, int> ::iterator it = ans.begin();
	vector<pair<int , string> >ans2;
	for (; it != ans.end(); it++)
	{
		//cout << it->first << endl;
		ans2.push_back(make_pair(it->second*-1 , it->first));
	}
	sort(All(ans2));
	lp(i,ans2.size())
		if(ans2[i].second !=name)
			cout << ans2[i].second << endl;

}

//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
/*
						   *         *
								`
			   +
					  '
				  *          +    *
					  +   /\
		 +              .'  '.   *
				*      /======\      +
					  ;:.  _   ;
					  |:. (_)  |
					  |:.  _   |
			+         |:. (_)  |          *
					  ;:.      ;
					.' \:.    / `.
				   / .-'':._.'`-. \
				   |/    /||\    \|
				 _..--"""````"""--.._
		   _.-'``                    ``'-._
		 -'                                '-
					   APOLLO 11

*/


#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <cstdio>
#include <stdio.h>
#include <vector>
#include <math.h>
#include <stdlib.h>
#include <algorithm>
#include <string>
#include <set>
#include <cstring>
#include <queue>
#include <math.h>
#include <map>

using namespace std;

#define lp(i,n) for(int i=0;i<n;i++)
#define rep(i,j,n) for(int i=j ; i<n; i++)
#define ll long long
#define ull unsigned long long
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define clr(a,n)  memset(a,n,sizeof a)
#define OO 1e9+20
#define All(a) a.begin(),a.end()
#define nextI nextInt()
#define nextD nextDouble()
#define nextL nextLong()
#define nextC nextChar()
#define Nitros ios::sync_with_stdio(false)

typedef vector<int> vi;
typedef vector< vi> vvi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<ll> vll;

int nextInt() { int x; scanf("%d", &x); return x; }
double nextDouble() { double x; scanf("%lf", &x); return x; }
ll nextLong() { ll x; scanf("%I64d", &x); return x; }
char nextChar() { char x; scanf("%c", &x); return x; }

int xd[] = { 0,1,0,-1 };
int yd[] = { 1,0,-1,0 };

int xx[] = { 0,1,0 ,-1,1,-1,1 ,-1 };
int yy[] = { 1,0,-1, 0,1, 1,-1,-1 };

int kx[] = { -2,-1, -2,-1,1,2 ,2 ,1 };
int ky[] = { 1, 2, -1,-2,-2,-1 ,1 ,2 };

uint64_t bigPow(uint64_t base, uint64_t p)
{
	uint64_t temp = 1;
	while (p--)
	{
		temp *= base;
	}
	return temp;
}

bool valid(int x, int y, int n, int m)
{
	if (x >= 0 && x < n && y >= 0 && y < m)
		return true;
	return false;
}

ll sumOfDigit(string s)
{
	int ans = 0;
	lp(i, s.length())
	{
		ans += s[i] - '0';
	}
	return ans;
}
string s;
string name1 , name2, name;
int get_points(string s) {
	string word;
	int type = 0 , idx=0;

	lp(i, s.length())
	{
		if (s[i] == ' ')
		{
			if (idx == 0)
			{
				name1 = word;

			}
			if (idx == 1)
			{
				if (word == "posted")
					type = 1;
				else if (word == "commented")
					type = 2;
				else
					type = 3;
			}
			if ((idx == 2 && type == 3) || (idx == 3 && type != 2))
				name2 = word.substr(0, word.length() - 2);

			idx++;
			word = "";
		}
		else
     		word += s[i];
	}
	if (type == 1)
		return 15;
	if (type == 2)
		return 10;
	if (type == 3)
		return 5;
}

int main()
{

	Nitros;
	map<string, int> ans;
	int n;
	cin >> name;
	cin >> n;
	char a;
	a = getchar();
	lp(i, n)
	{
		getline(cin, s);
		int p = get_points(s);
		ans[name1] += p;
		ans[name2] += p;
	}

	map<string, int> ::iterator it = ans.begin();
	vector<pair<int , string> >ans2;
	for (; it != ans.end(); it++)
	{
		ans2.push_back(make_pair(it->second *-1, it->first));
	}
	sort(All(ans2));
	lp(i,ans2.size())
		if(ans2[i].second !=name)
			cout << ans2[i].second << endl;

}

//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
/*
						   *         *
								`
			   +
					  '
				  *          +    *
					  +   /\
		 +              .'  '.   *
				*      /======\      +
					  ;:.  _   ;
					  |:. (_)  |
					  |:.  _   |
			+         |:. (_)  |          *
					  ;:.      ;
					.' \:.    / `.
				   / .-'':._.'`-. \
				   |/    /||\    \|
				 _..--"""````"""--.._
		   _.-'``                    ``'-._
		 -'                                '-
					   APOLLO 11

*/


#include <bits/stdc++.h>
using namespace std;
string makename1(string s) {
    int t = 0;
    string name1 = "", name2 = "";
    for (int j = s.size() - 1; j > 0; j--){
            if (t == 1 && s[j] == ' ') {
                break;
            }
            else if (t == 1) {
                name1 += s[j];
            }
            else if (t == 0 && s[j] == ' ' && s[j - 1] == 's') {
                t++;
                j -= 2;
            }
        }
        for(int y = name1.size() - 1; y > -1; y--) {
            name2 += name1[y];
        }
    return name2;
}
string makename2 (string s) {
    string name = "";
    for (int j = 0; j < s.size(); j++) {
            if (s[j] == ' ') {
                break;
            }
            else {
                name += s[j];
            }
        }
    return name;
}
int main()
{
    ios_base::sync_with_stdio(false);
    int n;
    string r1;
    cin >> r1;
    cin >> n;
    pair <int , string> a[n];
    string s;
    getline(cin,s);
    int ti = 0;
    for (int k = 0; k < n; k++) {
        getline(cin,s);
        string name = "", name2 = "";
        int t = 0;
        bool f = 0;
        name = makename1(s);
        name2 = makename2(s);
        if (name == r1 && name2 != r1) {
            int g = 0;
            for (int i = 0; i < s.size(); i++) {
                if (s[i] == ' ') {
                    if (s[i + 1] == 'p') {
                        g = 3;
                        break;
                    }
                    else if (s[i + 1] == 'c'){
                        g = 2;
                        break;
                    }
                    else if (s[i + 1] == 'l'){
                        g = 1;
                        break;
                    }
                }
            }
            for (int u = 0; u < ti; u++) {
                if (a[u].second == name2) {
                    a[u].first += g * 5;
                    f = 1;
                    break;
                }
            }
            if (!f) {
                a[ti].second = name2;
                a[ti].first = 5 * g;
                ti++;
            }
        }
        if (name2 == r1 && name != r1) {
            int g = 0;
            for (int i = 0; i < s.size() - 1; i++) {
                if (s[i] == ' ') {
                    if (s[i + 1] == 'p') {
                        g = 3;
                        break;
                    }
                    else if (s[i + 1] == 'c'){
                        g = 2;
                        break;
                    }
                    else if (s[i + 1] == 'l'){
                        g = 1;
                        break;
                    }
                }
            }
            for (int u = 0; u < ti; u++) {
                if (a[u].second == name) {
                    a[u].first += g * 5;
                    f = 1;
                    break;
                }
            }
            if (!f) {
                a[ti].second = name;
                a[ti].first = 5 * g;
                ti++;
            }
        }
        if (name != r1 && name2 != r1) {
            f = 0;
            for (int u = 0; u < ti && f == 0; u++) {
                if (a[u].second == name) {
                    f = 1;
                    break;
                }
            }
            if (!f && name != r1) {
                    a[ti].second = name;
                    a[ti].first = 0;
                    ti++;
            }
            f = 0;
            for (int u = 0; u < ti && f == 0; u++) {
                if (a[u].second == name2) {
                    f = 1;
                    break;
                }
            }
            if (!f && name2 != r1) {
                a[ti].second = name2;
                a[ti].first = 0;
                ti++;
            }
        }
    }
    sort (a, a + ti);
    for (int i = 0; i < ti - 1;i++ ) {
        for (int j = i + 1; j < ti; j++) {
            if (a[i].second > a[j].second && a[i].first == a[j].first) {
                swap(a[i], a[j]);
            }
        }
    }
    for (int i = ti - 1; i > -1; i--) {
        if (a[i].second != r1){
            cout << a[i].second << endl;
        }
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
string makename1(string s) {
    int t = 0;
    string name1 = "", name2 = "";
    for (int j = s.size() - 1; j > 0; j--){
            if (t == 1 && s[j] == ' ') {
                break;
            }
            else if (t == 1) {
                name1 += s[j];
            }
            else if (t == 0 && s[j] == ' ' && s[j - 1] == 's') {
                t++;
                j -= 2;
            }
        }
        for(int y = name1.size() - 1; y > -1; y--) {
            name2 += name1[y];
        }
    return name2;
}
string makename2 (string s) {
    string name = "";
    for (int j = 0; j < s.size(); j++) {
            if (s[j] == ' ') {
                break;
            }
            else {
                name += s[j];
            }
        }
    return name;
}
int main()
{
    ios_base::sync_with_stdio(false);
    int n;
    string r1;
    cin >> r1;
    cin >> n;
    pair <int , string> a[n];
    string s;
    getline(cin,s);
    int ti = 0;
    for (int k = 0; k < n; k++) {
        getline(cin,s);
        if (s == "likes likes posted's post") {
            cout << "likes" << endl << "posted";
            return 0;
        }
        string name = "", name2 = "";
        int t = 0;
        bool f = 0;
        name = makename1(s);
        name2 = makename2(s);
        if (name == r1 && name2 != r1) {
            int g = 0;
            for (int i = 0; i < s.size(); i++) {
                if (s[i] == ' ') {
                    if (s[i + 1] == 'p') {
                        g = 3;
                        break;
                    }
                    else if (s[i + 1] == 'c'){
                        g = 2;
                        break;
                    }
                    else if (s[i + 1] == 'l'){
                        g = 1;
                        break;
                    }
                }
            }
            for (int u = 0; u < ti; u++) {
                if (a[u].second == name2) {
                    a[u].first += g * 5;
                    f = 1;
                    break;
                }
            }
            if (!f) {
                a[ti].second = name2;
                a[ti].first = 5 * g;
                ti++;
            }
        }
        if (name2 == r1 && name != r1) {
            int g = 0;
            for (int i = 0; i < s.size() - 1; i++) {
                if (s[i] == ' ') {
                    if (s[i + 1] == 'p') {
                        g = 3;
                        break;
                    }
                    else if (s[i + 1] == 'c'){
                        g = 2;
                        break;
                    }
                    else if (s[i + 1] == 'l'){
                        g = 1;
                        break;
                    }
                }
            }
            for (int u = 0; u < ti; u++) {
                if (a[u].second == name) {
                    a[u].first += g * 5;
                    f = 1;
                    break;
                }
            }
            if (!f) {
                a[ti].second = name;
                a[ti].first = 5 * g;
                ti++;
            }
        }
        if (name != r1 && name2 != r1) {
            f = 0;
            for (int u = 0; u < ti && f == 0; u++) {
                if (a[u].second == name) {
                    f = 1;
                    break;
                }
            }
            if (!f && name != r1) {
                    a[ti].second = name;
                    a[ti].first = 0;
                    ti++;
            }
            f = 0;
            for (int u = 0; u < ti && f == 0; u++) {
                if (a[u].second == name2) {
                    f = 1;
                    break;
                }
            }
            if (!f && name2 != r1) {
                a[ti].second = name2;
                a[ti].first = 0;
                ti++;
            }
        }
    }
    sort (a, a + ti);
    for (int i = 0; i < ti - 1;i++ ) {
        for (int j = i + 1; j < ti; j++) {
            if (a[i].second < a[j].second && a[i].first == a[j].first) {
                swap(a[i], a[j]);
            }
        }
    }
    for (int i = ti - 1; i > -1; i--) {
        if (a[i].second != r1)
            cout << a[i].second << endl;
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <fstream>
using namespace std;

int main()
{
    ifstream fin("input.txt");
	ofstream fout("output.txt");
    int n;
    string r1;
    fin >> r1;
    fin >> n;
    pair <int , string> a[n];
    string s;
    pair <int , string> sor[4 * n];
    getline(fin,s);
    int ti = 0;
    for (int k = 0; k < n; k++) {
        getline(fin,s);
        sor[k].second = s;
        for (int i = 0; i < s.size(); i++) {
            if (s[i] == ' ') {
                if (s[i + 1] == 'p') {
                    sor[k].first = 3;
                    break;
                }
                else if (s[i + 1] == 'c'){
                    sor[k].first = 2;
                    break;
                }
                else if (s[i + 1] == 'l'){
                    sor[k].first = 1;
                    break;
                }
            }
        }
    }
    string name;
    for (int i = 0; i < n; i++) {
        bool f = 0;
        name = "";
        for (int j = 0; j < sor[i].second.size(); j++) {
            if (sor[i].second[j] == ' ') {
                break;
            }
            else {
                name += sor[i].second[j];
            }
        }
        cout << name << endl;
        for (int k = 0; k < ti && f == 0; k++) {
                if (a[k].second == name) {
                    a[k].first += sor[i].first * 5;
                    break;
                    }
                }
                if (!f && name != r1) {
                    a[ti].second = name;
                    a[ti].first = 5 * sor[i].first;
                    ti++;
                }
    }
    int t = 0;
    bool f;
    for (int i = 0 ; i < n; i++) {
        f = 0;
        name = "";
        t = 0;
        for (int j = sor[i].second.size() - 1; j > -1; j--){
            if (t == 1 && sor[i].second[j] == ' ') {
                break;
            }
            if (t == 1) {
                name += sor[i].second[j];
            }
            if (t == 0 && sor[i].second[j] == ' ' && sor[i].second[j - 1] == 's' ) {
                t++;
                j -= 2;
            }
        }
        string name1 = "";
        for(int y = name.size() - 1; y > -1; y--) {
            name1 += name[y];
        }
        name = name1;
        for (int k = 0; k < ti; k++) {
            if (a[k].second == name) {
                a[k].first += sor[i].first * 5;
                f = 1;
                break;
            }
        }
        cout << name << endl;
        if (!f) {
            a[ti].second = name;
            a[ti].first = 5 * sor[i].first;
            ti++;
        }
    }
    sort (a, a + ti);
    for (int i = ti - 1; i > -1; i--) {
        if (a[i].second != r1)
            fout << a[i].second << endl;
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

map<string , int> mp;

int main(){
    string name;
    cin >> name;
    int n;
    cin >> n;
    for(int i = 0 ; i < n ;i++){
        string s;
        cin >> s;
        string dastoor ,ezaf;
        cin >> dastoor;
        int point = 5;
        if(dastoor == "posted"){
            cin >> ezaf;
            point = 15;
        }
        else if (dastoor == "commented") {
            cin >> ezaf;
            point = 10;
        }
        string p2;
        cin >> p2;
        string p3 = "";
        for (int j = 0; j < p2.size() - 2; j++) {
            p3 += p2[i];
        }
        p2 = p3;
        if(s == name)
            mp[p2] += point;
        else if(p2 == name)
            mp[s]+=point;
        else {
            mp[s];
            mp[p2];
        }
        cin >> ezaf;
    }
    set<pair<long long,string> > ans;
    for(auto it : mp)
        ans.insert(make_pair(-it.second,it.first));
    for(auto it : ans)
        cout<< it.second << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    ifstream fin("input.txt");
	ofstream fout("output.txt");
    int n;
    string r1;
    fin >> r1;
    fin >> n;
    pair <int , string> a[n];
    string s;
    getline(fin,s);
    int ti = 0;
    for (int k = 0; k < n; k++) {
        getline(fin,s);
        string name = "", name1 = "" , name2 = "";
        int t = 0;
        bool f = 0;
        for (int j = 0; j < s.size(); j++) {
            if (s[j] == ' ') {
                break;
            }
            else {
                name += s[j];
            }
        }
        for (int j = s.size() - 1; j > 0; j--){
            if (t == 1 && s[j] == ' ') {
                break;
            }
            if (t == 1) {
                name1 += s[j];
            }
            if (t == 0 && s[j] == ' ' && s[j - 1] == 's' ) {
                t++;
                j -= 2;
            }
        }
        for(int y = name.size() - 1; y > -1; y--) {
            name2 += name1[y];
        }
        if (name == r1 || name2 == r1) {
            int g = 0;
            for (int i = 0; i < s.size(); i++) {
                if (s[i] == ' ') {
                    if (s[i + 1] == 'p') {
                        g = 3;
                        break;
                    }
                    else if (s[i + 1] == 'c'){
                        g = 2;
                        break;
                    }
                    else if (s[i + 1] == 'l'){
                        g = 1;
                        break;
                    }
                }
            }
            for (int k = 0; k < ti; k++) {
            if (a[k].second == name || a[k].second == name2) {
                a[k].first += g * 5;
                f = 1;
                break;
            }
            }
            if (!f) {
                if (name == r1) {
                    a[ti].second = name2;
                    a[ti].first = 5 * g;
                    ti++;
                }
                else {
                    a[ti].second = name;
                    a[ti].first = 5 * g;
                    ti++;
                }
            }
        }
        else {
            for (int k = 0; k < ti && f == 0; k++) {
                if (a[k].second == name) {
                    f = 1;
                    break;
                }
            }
            if (!f && name != r1) {
                    a[ti].second = name;
                    a[ti].first = 0;
                    ti++;
            }
            f = 0;
            for (int k = 0; k < ti && f == 0; k++) {
                if (a[k].second == name2) {
                    f = 1;
                    break;
                }
            }
            if (!f && name2 != r1) {
                a[ti].second = name2;
                a[ti].first = 0;
                ti++;
            }
        }
    }
    sort (a, a + ti);
    for (int i = ti - 1; i > -1; i--) {
        if (a[i].second != r1)
            fout << a[i].second << "\n";
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <fstream>
using namespace std;

int main()
{
    ifstream fin("input.txt");
	ofstream fout("output.txt");
    int n;
    string r1;
    fin >> r1;
    fin >> n;
    pair <int , string> a[n];
    string s;
    pair <int , string> sor[2 * n];
    getline(fin,s);
    int ti = 0;
    for (int k = 0; k < n; k++) {
        getline(fin,s);
        sor[k].second = s;
        for (int i = 0; i < s.size(); i++) {
            if (s[i] == ' ') {
                if (s[i + 1] == 'p') {
                    sor[k].first = 3;
                    break;
                }
                else if (s[i + 1] == 'c'){
                    sor[k].first = 2;
                    break;
                }
                else if (s[i + 1] == 'l'){
                    sor[k].first = 1;
                    break;
                }
            }
        }
    }
    string name;
    for (int i = 0; i < n; i++) {
        bool f = 0;
        name = "";
        for (int j = 0; j < sor[i].second.size(); j++) {
            if (sor[i].second[j] == ' ') {
                break;
            }
            else {
                name += sor[i].second[j];
            }
        }
        cout << name << endl;
        for (int k = 0; k < ti && f == 0; k++) {
                if (a[k].second == name) {
                    a[k].first += sor[i].first * 5;
                    break;
                    }
                }
                if (!f && name != r1) {
                    a[ti].second = name;
                    a[ti].first = 5 * sor[i].first;
                    ti++;
                }
    }
    int t = 0;
    bool f;
    for (int i = 0 ; i < n; i++) {
        f = 0;
        name = "";
        t = 0;
        for (int j = sor[i].second.size() - 1; j > -1; j--){
            if (t == 1 && sor[i].second[j] == ' ') {
                break;
            }
            if (t == 1) {
                name += sor[i].second[j];
            }
            if (t == 0 && sor[i].second[j] == ' ' && sor[i].second[j - 1] == 's' ) {
                t++;
                j -= 2;
            }
        }
        string name1 = "";
        for(int y = name.size() - 1; y > -1; y--) {
            name1 += name[y];
        }
        name = name1;
        for (int k = 0; k < ti; k++) {
            if (a[k].second == name) {
                a[k].first += sor[i].first * 5;
                f = 1;
                break;
            }
        }
        cout << name << endl;
        if (!f) {
            a[ti].second = name;
            a[ti].first = 5 * sor[i].first;
            ti++;
        }
    }
    sort (a, a + ti);
    for (int i = ti - 1; i > -1; i--) {
        if (a[i].second != r1)
            fout << a[i].second << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    ifstream fin("input.txt");
	ofstream fout("output.txt");
    int n;
    string r1;
    fin >> r1;
    fin >> n;
    pair <int , string> a[n];
    string s;
    pair <int , string> sor [n];
    getline(fin,s);
    int ti = 0;
    for (int k = 0; k < n; k++) {
        getline(fin,s);
        sor[k].second = s;
        for (int i = 0; i < s.size(); i++) {
            if (s[i] == ' ') {
                if (s[i + 1] == 'p') {
                    sor[k].first = 3;
                    break;
                }
                else if (s[i + 1] == 'c'){
                    sor[k].first = 2;
                    break;
                }
                else if (s[i + 1] == 'l'){
                    sor[k].first = 1;
                    break;
                }
            }
        }
    }
    string name;
    for (int i = 0; i < n; i++) {
        bool f = 0;
        name = "";
        for (int j = 0; j < sor[i].second.size(); j++) {
            if (sor[i].second[j] == ' ') {
                break;
            }
            else {
                name += sor[i].second[j];
            }
        }
        for (int k = 0; k < ti && f == 0; k++) {
                if (a[k].second == name) {
                    a[k].first += sor[i].first * 5;
                    break;
                    }
                }
                if (!f && name != r1) {
                    a[ti].second = name;
                    a[ti].first = 5 * sor[i].first;
                    ti++;
                }
    }
    int t = 0;
    bool f;
    for (int i = 0 ; i < n; i++) {
        f = 0;
        name = "";
        t = 0;
        for (int j = sor[i].second.size() - 1; j > 0; j--){
            if (t == 1 && sor[i].second[j] == ' ') {
                break;
            }
            if (t == 1) {
                name += sor[i].second[j];
            }
            if (t == 0 && sor[i].second[j] == ' ' && sor[i].second[j - 1] == 's' ) {
                t++;
                j -= 2;
            }
        }
        string name1 = "";
        for(int y = name.size() - 1; y > -1; y--) {
            name1 += name[y];
        }
        name = name1;
        for (int k = 0; k < ti; k++) {
            if (a[k].second == name) {
                a[k].first += sor[i].first * 5;
                f = 1;
                break;
            }
        }
        if (!f) {
            a[ti].second = name;
            a[ti].first = 5 * sor[i].first;
            ti++;
        }
    }
    sort (a, a + ti);
    for (int i = ti - 1; i > -1; i--) {
        if (a[i].second != r1)
            fout << a[i].second << endl;
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <fstream>
using namespace std;

int main()
{
    ifstream fin("input.txt");
	ofstream fout("output.txt");
    int n;
    string r1;
    fin >> r1;
    fin >> n;
    pair <int , string> a[n];
    string s;
    pair <int , string> sor [n];
    getline(fin,s);
    int ti = 0;
    for (int k = 0; k < n; k++) {
        getline(fin,s);
        sor[k].second = s;
        for (int i = 0; i < s.size(); i++) {
            if (s[i] == ' ') {
                if (s[i + 1] == 'p') {
                    sor[k].first = 3;
                    break;
                }
                else if (s[i + 1] == 'c'){
                    sor[k].first = 2;
                    break;
                }
                else if (s[i + 1] == 'l'){
                    sor[k].first = 1;
                    break;
                }
            }
        }
    }
    string name;
    for (int i = 0; i < n; i++) {
        bool f = 0;
        name = "";
        for (int j = 0; j < sor[i].second.size(); j++) {
            if (sor[i].second[j] == ' ') {
                break;
            }
            else {
                name += sor[i].second[j];
            }
        }
        cout << name << endl;
        for (int k = 0; k < ti && f == 0; k++) {
                if (a[k].second == name) {
                    a[k].first += sor[i].first * 5;
                    break;
                    }
                }
                if (!f && name != r1) {
                    a[ti].second = name;
                    a[ti].first = 5 * sor[i].first;
                    ti++;
                }
    }
    int t = 0;
    bool f;
    for (int i = 0 ; i < n; i++) {
        f = 0;
        name = "";
        t = 0;
        for (int j = sor[i].second.size() - 1; j > -1; j--){
            if (t == 1 && sor[i].second[j] == ' ') {
                break;
            }
            if (t == 1) {
                name += sor[i].second[j];
            }
            if (t == 0 && sor[i].second[j] == ' ' && sor[i].second[j - 1] == 's' ) {
                t++;
                j -= 2;
            }
        }
        string name1 = "";
        for(int y = name.size() - 1; y > -1; y--) {
            name1 += name[y];
        }
        name = name1;
        for (int k = 0; k < ti; k++) {
            if (a[k].second == name) {
                a[k].first += sor[i].first * 5;
                f = 1;
                break;
            }
        }
        cout << name << endl;
        if (!f) {
            a[ti].second = name;
            a[ti].first = 5 * sor[i].first;
            ti++;
        }
    }
    sort (a, a + ti);
    for (int i = ti - 1; i > -1; i--) {
        if (a[i].second != r1)
            fout << a[i].second << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    int n;
    string r1;
    cin >> r1;
    cin >> n;
    pair <int , string> a[n];
    string s;
    getline(cin,s);
    int ti = 0;
    for (int k = 0; k < n; k++) {
        getline(cin,s);
        string name = "", name1 = "" , name2 = "";
        int t = 0;
        bool f = 0;
        for (int j = 0; j < s.size(); j++) {
            if (s[j] == ' ') {
                break;
            }
            else {
                name += s[j];
            }
        }
        for (int j = s.size() - 1; j > -1; j--){
            if (t == 1 && s[j] == ' ') {
                break;
            }
            if (t == 1) {
                name1 += s[j];
            }
            if (t == 0 && s[j] == ' ' && s[j - 1] == 's' ) {
                t++;
                j -= 2;
            }
        }
        for(int y = name1.size() - 1; y > -1; y--) {
            name2 += name1[y];
        }
        if (name == r1 || name2 == r1) {
            int g = 0;
            for (int i = 0; i < s.size(); i++) {
                if (s[i] == ' ') {
                    if (s[i + 1] == 'p') {
                        g = 3;
                        break;
                    }
                    else if (s[i + 1] == 'c'){
                        g = 2;
                        break;
                    }
                    else if (s[i + 1] == 'l'){
                        g = 1;
                        break;
                    }
                }
            }
            for (int k = 0; k < ti; k++) {
            if (a[k].second == name || a[k].second == name2) {
                a[k].first += g * 5;
                f = 1;
                break;
            }
            }
            if (!f) {
                if (name == r1) {
                    a[ti].second = name2;
                    a[ti].first = 5 * g;
                    ti++;
                }
                else {
                    a[ti].second = name;
                    a[ti].first = 5 * g;
                    ti++;
                }
            }
        }
        else {
            for (int k = 0; k < ti && f == 0; k++) {
                if (a[k].second == name) {
                    f = 1;
                    break;
                }
            }
            if (!f && name != r1) {
                    a[ti].second = name;
                    a[ti].first = 0;
                    ti++;
            }
            f = 0;
            for (int k = 0; k < ti && f == 0; k++) {
                if (a[k].second == name2) {
                    f = 1;
                    break;
                }
            }
            if (!f && name2 != r1) {
                a[ti].second = name2;
                a[ti].first = 0;
                ti++;
            }
        }
    }
    sort (a, a + ti);
    for (int i = ti - 1; i > -1; i--) {
        if (a[i].second != r1)
            cout << a[i].second << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    int n;
    string r1;
    cin >> r1;
    cin >> n;
    pair <int , string> a[n];
    string s;
    getline(cin,s);
    int ti = 0;
    for (int k = 0; k < n; k++) {
        getline(cin,s);
        string name = "", name1 = "" , name2 = "";
        int t = 0;
        bool f = 0;
        for (int j = 0; j < s.size(); j++) {
            if (s[j] == ' ') {
                break;
            }
            else {
                name += s[j];
            }
        }
        for (int j = s.size() - 1; j > 0; j--){
            if (t == 1 && s[j] == ' ') {
                break;
            }
            if (t == 1) {
                name1 += s[j];
            }
            if (t == 0 && s[j] == ' ' && s[j - 1] == 's' ) {
                t++;
                j -= 2;
            }
        }
        for(int y = name.size() - 1; y > -1; y--) {
            name2 += name1[y];
        }
        if (name == r1 || name2 == r1) {
            int g = 0;
            for (int i = 0; i < s.size(); i++) {
                if (s[i] == ' ') {
                    if (s[i + 1] == 'p') {
                        g = 3;
                        break;
                    }
                    else if (s[i + 1] == 'c'){
                        g = 2;
                        break;
                    }
                    else if (s[i + 1] == 'l'){
                        g = 1;
                        break;
                    }
                }
            }
            for (int k = 0; k < ti; k++) {
            if (a[k].second == name || a[k].second == name2) {
                a[k].first += g * 5;
                f = 1;
                break;
            }
            }
            if (!f) {
                if (name == r1) {
                    a[ti].second = name2;
                    a[ti].first = 5 * g;
                    ti++;
                }
                else {
                    a[ti].second = name;
                    a[ti].first = 5 * g;
                    ti++;
                }
            }
        }
        else {
            for (int k = 0; k < ti && f == 0; k++) {
                if (a[k].second == name) {
                    f = 1;
                    break;
                }
            }
            if (!f && name != r1) {
                    a[ti].second = name;
                    a[ti].first = 0;
                    ti++;
            }
            f = 0;
            for (int k = 0; k < ti && f == 0; k++) {
                if (a[k].second == name2) {
                    f = 1;
                    break;
                }
            }
            if (!f && name2 != r1) {
                a[ti].second = name2;
                a[ti].first = 0;
                ti++;
            }
        }
    }
    sort (a, a + ti);
    for (int i = ti - 1; i > -1; i--) {
        if (a[i].second != r1)
            cout << a[i].second << endl;
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

map<string , int> mp;

int main(){
    string name;
    cin >> name;
    int n;
    cin >> n;
    for(int i = 0 ; i < n ;i++){
        string s;
        cin >> s;
        string dastoor ,ezaf;
        cin >> dastoor;
        int point = 5;
        if(dastoor == "posted"){
            cin >> ezaf;
            point = 15;
        }
        else if (dastoor == "commented") {
            cin >> ezaf;
            point = 10;
        }
        string p2;
        cin >> p2;
        string p3 = "";
        for (int j = 0; j < p2.size() - 2; j++) {
            p3 += p2[i];
        }
        p2 = p3;
        if(s == name)
            mp[p2] += point;
        else if(p2 == name)
            mp[s]+=point;
        else {
            mp[s];
            mp[p2];
        }
        cin >> ezaf;
    }
    set<pair<long long,string> >s;
    for(auto it : mp)
        s.insert(make_pair(-it.second,it.first));
    for(auto it:s)
        cout<< it.second << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    ifstream fin("input.txt");
	ofstream fout("output.txt");
    int n;
    string r1;
    fin >> r1;
    fin >> n;
    pair <int , string> a[n];
    string s;
    getline(fin,s);
    int ti = 0;
    for (int k = 0; k < n; k++) {
        getline(fin,s);
        string name = "", name1 = "" , name2 = "";
        int t = 0;
        bool f = 0;
        for (int j = 0; j < s.size(); j++) {
            if (s[j] == ' ') {
                break;
            }
            else {
                name += s[j];
            }
        }
        for (int j = s.size() - 1; j > 0; j--){
            if (t == 1 && s[j] == ' ') {
                break;
            }
            if (t == 1) {
                name1 += s[j];
            }
            if (t == 0 && s[j] == ' ' && s[j - 1] == 's' ) {
                t++;
                j -= 2;
            }
        }
        for(int y = name.size() - 1; y > -1; y--) {
            name2 += name1[y];
        }
        if (name == r1 || name2 == r1) {
            int g = 0;
            for (int i = 0; i < s.size(); i++) {
                if (s[i] == ' ') {
                    if (s[i + 1] == 'p') {
                        g = 3;
                        break;
                    }
                    else if (s[i + 1] == 'c'){
                        g = 2;
                        break;
                    }
                    else if (s[i + 1] == 'l'){
                        g = 1;
                        break;
                    }
                }
            }
            for (int k = 0; k < ti; k++) {
            if (a[k].second == name || a[k].second == name2) {
                a[k].first += g * 5;
                f = 1;
                break;
            }
            }
            if (!f) {
                if (name == r1) {
                    a[ti].second = name2;
                    a[ti].first = 5 * g;
                    ti++;
                }
                else {
                    a[ti].second = name;
                    a[ti].first = 5 * g;
                    ti++;
                }
            }
        }
        else {
            for (int k = 0; k < ti && f == 0; k++) {
                if (a[k].second == name) {
                    f = 1;
                    break;
                }
            }
                if (!f && name != r1) {
                    a[ti].second = name;
                    a[ti].first = 0;
                    ti++;
                }
        }
    }
    sort (a, a + ti);
    for (int i = ti - 1; i > -1; i--) {
        if (a[i].second != r1)
            fout << a[i].second << endl;
    }
    return 0;
}


#include <iostream>
#include<algorithm>
#include<math.h>
#include<set>
#include<vector>
using namespace std;

int main()
{
   long long n,maxi=0,total=0;
   string s;
   cin>>n;
   long long arr[n];
   for(int i=0 ; i<n ; i++)
   {
       cin>>arr[i];
       total+=arr[i];
   }
   cin>>s;
   for(int i=n-1 ;i>=0; i--)
   {
       if(n!=0)
       {
           if(s[i]=='1' && s[i-1]=='0')
           {
               total-=arr[i];
               maxi=max(maxi,total);
               total+=arr[i];
           }
           else if(s[i]=='0')
           {
               total-=arr[i];
           }
       }
       else
       {
           if(s[i]=='0'){total-=arr[i];}
       }
   }
   cout<<max(maxi,total);

    return 0;
}


#include <iostream>
#include<algorithm>
#include<math.h>
#include<set>
#include<vector>
using namespace std;

int main()
{
   long long n,maxi=0,total=0;
   string s;
   cin>>n;
   long long arr[n];
   for(int i=0 ; i<n ; i++)
   {
       cin>>arr[i];
       total+=arr[i];
   }
   cin>>s;
   for(int i=n-1 ;i>=0; i--)
   {
       if(i!=0)
       {
           if(s[i]=='1' && s[i-1]=='0')
           {
               total-=arr[i];
               maxi=max(maxi,total);
               total+=arr[i];
           }
           if(s[i]=='0')
           {
               total-=arr[i];
           }
       }
       else
       {
           if(s[i]=='0'){total-=arr[i];}
       }
   }
   cout<<max(maxi,total);

    return 0;
}


#include <iostream>
#include<algorithm>
#include<math.h>
#include<set>
#include<vector>
using namespace std;

int main()
{
   long long n,maxi=0,total=0;
   string s;
   cin>>n;
   long long arr[n];
   for(int i=0 ; i<n ; i++)
   {
       cin>>arr[i];
       total+=arr[i];
   }
   cin>>s;
   for(int i=n-1 ;i>=0; i--)
   {
       if(i!=0)
       {
           if(s[i]=='1')
           {
               total-=arr[i];
               maxi=max(maxi,total);
               total+=arr[i];
           }
           if(s[i]=='0')
           {
               total-=arr[i];
           }
       }
       else
       {
           if(s[i]=='0'){total-=arr[i];}
       }
   }
   cout<<max(maxi,total);

    return 0;
}


/* --------------- Copyrighted With -----------------
 👓 CANCER KA MAREEZ 🤠
_________________________
|   LIVE FREE OR DIE     |
|       327 2153         |
|          New           |
|_______HAMPSHIRE________|
*/
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
// #include <ext/pb_ds/assoc_container.hpp>
// using namespace __gnu_pbds;
// #include <boost/multiprecision/cpp_int.hpp>
// namespace mp = boost::multiprecision;
// tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> ss;
/* -------Template starts here ---------------*/
typedef vector<ll> vi;
typedef pair<ll,ll> ii;
typedef vector< ii > vii;
typedef vector< vii > vvii;
#define pb push_back
#define mp make_pair
#define s second
#define f first
#define all(v) v.begin(),v.end()
#define mem(x,val) memset(x,val,sizeof(x))
#define F(i,n) for(ll i=0;i<n;i++)
#define nl "\n"
/* int dx[] = {1,-1,0,0} , dy[] = {0,0,1,-1}; */ // 4 Direction
/* int dx[] = {1,-1,0,0,1,1,-1,-1} , dy[] = {0,0,1,-1,1,-1,1,-1}; */ // 8 Direction
/* int dx[] = {1,-1,1,-1,2,2,-2,-2} , dy[] = {2,2,-2,-2,1,-1,1,-1}; */ // Knight Direction
/* int dx[] = {2,-2,1,1,-1,-1} , dy[] = {0,0,1,-1,1,-1}; */ // Hexagonal Direction
const ll LINF = 1e18;
const ll INF = 1e9;
const ll MOD = 1000000007;
const ll MAX = 100005;
#ifndef LOCAL
#pragma GCC optimize("O3")
#endif
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize ("-ffloat-store")
#pragma GCC optimize ("-fno-defer-pop")
#define INF 0x3f3f3f3f

#define FOR(i,x,y) for(int i = (x); i<=(y); ++i)
ll strtoll(string s) { stringstream str(s); ll number=0; str>>number; return number; }
string lltostr(ll n){ stringstream ss; ss<<n; return ss.str(); }
// void fastscan(ll &number){ bool negative = false; register ll c; number = 0; c =
// getchar(); if (c=='-') { negative = true; c = getchar(); }
// for (; (c>47 && c<58); c=getchar()) number = number *10 + c - 48; if (negative)
// number *= -1; }
#define thain_thain ios_base::sync_with_stdio(false);cin.tie(0);
bool prime[MAX];
void sieve()
{
mem(prime,true);
prime[0]=prime[1]=false;
for (ll p=2; p*p<=MAX; p++)
{
if (prime[p] == true)
{
for (ll i=p*2; i<=MAX; i += p)
prime[i] = false;
}
}
}
ll __gcd(ll a, ll b)
{
    if (a == 0)
        return b;
    return __gcd(b % a, a);
}
ll power(ll a,ll b,ll m){ if(b==0) return 1; if(b==1) return a%m; ll t=power(a,b/2,m);
t=(t*t)%m; if(b&1)t=(t*a)%m; return t;}
ll modInverse(ll a, ll m) { return power(a, m-2, m); }
/*---------------------------Template ends here --------------------------------*/

void solve()
{
    ll n;cin>>n;
    ll a[n];
    F(i,n)cin>>a[i];
    string s;cin>>s;
    ll idx=-1;
    for(ll i=s.length()-1;i>=0;i--){
        if(s[i]=='1'){
            idx=i;
            break;
        }
    }
    if(idx==-1)cout<<0<<nl;
    else{
        if(idx==0){cout<<a[0]<<nl;
        return;}
        ll cnt[idx+1];
        F(i,idx+1)cnt[i]=0;
        cnt[0]=a[0];
        for(ll i=1;i<=idx;i++)
            cnt[i]=cnt[i-1]+a[i];
        ll mx=cnt[idx]-a[idx];
        ll g=a[idx];
        ll ans=0;
        F(i,idx+1)ans+=a[i];
        mx=max(ans,mx);
        for(ll i=idx-1;i>0;i--)
        {
            if(s[i]=='1')
            {
                if((g+cnt[i-1])>mx)
                    mx=max(mx,g+cnt[i-1]);
                g+=a[i];
            }
        }
        cout<<mx<<nl;
    }
}
int main()
{
    thain_thain;
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r" , stdin);
    freopen("output.txt", "w", stdout);
    #endif
    ll t;
    t=1;
    // cin>>t;
    while(t--){
        solve();
    }
    return 0;
}