#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <cstdio>

using namespace std;

#define PB push_back
#define MP make_pair

//#define DEBUG

struct str
{
       long long gruz;
       int num;
};

bool operator < ( const str & i, const str & j )
{
     return i.gruz < j.gruz;
}

int main()
{
#ifdef DEBUG
   freopen("input.txt", "r", stdin);
   freopen("output.txt", "w", stdout);
#endif
   long N, V, type, vv;
   cin >> N >> V;
   str st;
   vector < str > list_1, list_2;
   list_1.reserve(N);
   list_2.reserve(N);
   for ( long Q = 0; Q < N; ++Q )
   {
      cin >> type >> st.gruz;
      st.num = Q+1;
      if ( type == 1 )
         list_1.PB(st);
      else
         list_2.PB(st);
   }

   sort(list_1.begin(), list_1.end()), sort(list_2.begin(), list_2.end());
   vector < int > res;
   res.reserve(N);
   long long result = 0;

   while ( V > 0 && ( list_1.size() > 1 || list_2.size() > 1 ) )
   {
         if ( list_1.size () == 0 )
         {
              if ( V >= 2 )
                 result += list_2.back().gruz, res.PB(list_2.back().num), V -= 2;
              else
                 V = 0;
              list_2.pop_back();
              continue;
         }
         if ( list_2.size() == 0 )
         {
              if ( V >= 1 )
                 result += list_1.back().gruz, res.PB(list_1.back().num), V -= 1;
              else
                 V = 0;
              list_1.pop_back();
              continue;
         }
         if ( list_1.size() == 1 )
         {
              if ( V <= 1 )
              {
                   result += list_1.back().gruz;
                   res.PB(list_1.back().num);
                   V -= 1;
                   list_1.pop_back();
                   continue;
              }

              if ( list_1.back().gruz > list_2.back().gruz )
              {
                   result += list_1.back().gruz;
                   res.PB(list_1.back().num);
                   V -= 2;
                   list_1.pop_back();
                   continue;
              }
              else
              {
                   result += list_2.back().gruz;
                   res.PB(list_2.back().num);
                   V -= 2;
                   list_2.pop_back();
                   continue;
              }
         }
         else
         {
              if ( V == 1 )
              {
                   result += list_1.back().gruz;
                   res.PB(list_1.back().num);
                   V -= 1;
                   list_1.pop_back();
                   continue;
              }
              if ( list_1.back().gruz + list_1[list_1.size() - 2].gruz > list_2.back().gruz )
              {
                   result += list_1.back().gruz;
                   res.PB(list_1.back().num);
                   V -= 1;
                   list_1.pop_back();
                   continue;
              }
              else
              {
                   result += list_2.back().gruz;
                   res.PB(list_2.back().num);
                   list_2.pop_back();
                   V -= 2;
                   continue;
              }
         }
   }
   cout << result << endl;
   for ( long Q = 0; Q < res.size(); ++Q )
     cout << res[Q] << " ";
}


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <cstdio>

using namespace std;

#define PB push_back
#define MP make_pair

//#define DEBUG

struct str
{
       long long gruz;
       int num;
};

bool operator < ( const str & i, const str & j )
{
     return i.gruz < j.gruz;
}

int main()
{
#ifdef DEBUG
   freopen("input.txt", "r", stdin);
   freopen("output.txt", "w", stdout);
#endif
   long N, V, type, vv;
   cin >> N >> V;
   str st;
   vector < str > list_1, list_2;
   list_1.reserve(N);
   list_2.reserve(N);
   for ( long Q = 0; Q < N; ++Q )
   {
      cin >> type >> st.gruz;
      st.num = Q+1;
      if ( type == 1 )
         list_1.PB(st);
      else
         list_2.PB(st);
   }

   sort(list_1.begin(), list_1.end()), sort(list_2.begin(), list_2.end());
   vector < int > res;
   res.reserve(N);
   long long result = 0;

   while ( V > 0 && ( list_1.size() > 1 || list_2.size() > 1 ) )
   {
         if ( list_1.size () == 0 )
         {
              if ( V >= 2 )
                 result += list_2.back().gruz, res.PB(list_2.back().num), V -= 2;
              else
                 V = 0;
              list_2.pop_back();
              continue;
         }
         if ( list_2.size() == 0 )
         {
              if ( V >= 1 )
                 result += list_1.back().gruz, res.PB(list_1.back().num), V -= 1;
              else
                 V = 0;
              list_1.pop_back();
              continue;
         }
         if ( list_1.size() == 1 )
         {
              if ( V <= 1 )
              {
                   result += list_1.back().gruz;
                   res.PB(list_1.back().num);
                   V -= 1;
                   list_1.pop_back();
                   continue;
              }

              if ( list_1.back().gruz > list_2.back().gruz )
              {
                   result += list_1.back().gruz;
                   res.PB(list_1.back().num);
                   V -= 2;
                   list_1.pop_back();
                   continue;
              }
              else
              {
                   result += list_2.back().gruz;
                   res.PB(list_2.back().num);
                   V -= 2;
                   list_2.pop_back();
                   continue;
              }
         }
         else
         {
              if ( V == 1 )
              {
                   result += list_1.back().gruz;
                   res.PB(list_1.back().num);
                   V -= 1;
                   list_1.pop_back();
                   continue;
              }
              if ( list_1.back().gruz + list_1[list_1.size() - 2].gruz > list_2.back().gruz )
              {
                   result += list_1.back().gruz + list_1[list_1.size() - 2].gruz;
                   res.PB(list_1.back().num), res.PB(list_1[list_1.size() - 2].num);
                   V -= 2;
                   list_1.pop_back(), list_1.pop_back();
                   continue;
              }
              else
              {
                   result += list_2.back().gruz;
                   res.PB(list_2.back().num);
                   list_2.pop_back();
                   V -= 2;
                   continue;
              }
         }
   }
   cout << result << endl;
   for ( long Q = 0; Q < res.size(); ++Q )
     cout << res[Q] << " ";
}


#include<cstdio>
#include<algorithm>
using namespace std;
struct Data {
    int id;
    int value;
}D1[100002],D2[100002],*t;  //如数组名后面的数字所示，D1代表类型1货物的队列，D2代表类型2货物的队列，以下的变量均用数字表示是与何种类型相关
                            //t是指向当前输入数据的指针
int head1,head2;            //队列长度
bool Compare(const Data &a,const Data &b) {     //降序排列
    return a.value>b.value;
}
int main() {
    int n,v,mark1=0,mark2=0;    //mark标记的是货物能被放入的数量
    long long ans=0,temp;       //ans就是最大贡献，temp记录取出一个货物时的贡献
    scanf("%d%d",&n,&v);
    for(int i=1,j;i<=n;++i) {
        scanf("%d",&j);
        if(j==1)
            t=&D1[head1++];     //根据判断类型更新结构体指针
        else
            t=&D2[head2++];
        t->id=i;                //通过指针输入数据
        scanf("%d",&t->value);
    }
    for(sort(D2,D2+head2,Compare);v>1&&mark2<head2;v-=2)    //初始化是排序
        ans+=D2[mark2++].value;     //尽可能的放
    for(sort(D1,D1+head1,Compare);v>0&&mark1<head1;v-=1)
        ans+=D1[mark1++].value;
    temp=ans;
    for(int i=mark2-1,j;i>=0;--i) {     //在类型2中尝试取出
        temp-=D2[i].value;          //录取出一个货物时的贡献
        v+=2;
        for(j=mark1;j<head1&&v>0;++j) {     //在类型1中寻找
            temp+=D1[j].value;
            v-=1;
        }
        if(ans<temp) {      //检查是否更新最大贡献
            ans=temp;
            mark1=j;
            mark2-=1;
        }
        else
            break;
    }
    printf("%I64d\n",ans);
    for(int i=0;i<mark2;++i)    //输出数据标号
        printf("%d ",D2[i].id);
    for(int i=0;i<mark1;++i)
        printf("%d ",D1[i].id);
    return 0;
}


#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
using namespace std;
long long int a,b,c,d,e,f,ans,ll,t[510000],sl,bb,qq,zz,d1[3];
//string mm;
typedef struct
{
	long long int a,f;
	long long int z1,z2;
	double tz;
} fhs;
fhs p[512000];
bool cmp(fhs x,fhs y)
{
	return x.tz>y.tz;
}
bool cmp1(fhs x,fhs y)
{if(x.a!=y.a)
	return x.a<y.a;
	else
	return x.f>y.f;
}
int main() {
	scanf("%lld",&a);
	scanf("%lld",&f);
	c=f;
	e=a;
	for(int x=0; x<a; x++)
	{
		scanf("%lld%lld",&p[x].a,&p[x].f);
		p[x].z1=x+1;
		p[x].tz=(p[x].f*1.0)/(p[x].a*1.0);
		d1[p[x].a]++;
	}
	sort(p,p+a,cmp);
	for(int x=0;x<a;x++)
	{
		if(f>=p[x].a)
		{
			if(!(p[x].a==1&&f%2==0&&d1[1]==1))
			{
			ans+=p[x].f;
			d1[p[x].a]--;
			t[p[x].z1]=1;
			f-=p[x].a;
			sl++;
			}
		}
	}
	//if(a==1000&&f==854)
	//cout<<qq<<endl;
	/*if(qq*2<=f)
	{
		zz=f-qq*2;
	}
	else
	{
	if(f%2==1)
	{
		zz=1;
	}
	else
	{
		zz=0;
	}
	}
	/*for(int x=0; x<a; x++)
	{
		printf("%d %d %d %d\n",x,p[x].a,p[x].f,p[x].z1);
	}*/
	/*for(int x=zz;x<e;x+=2)
	{
		if(p[x].a+p[x+1].a==2)
		{
			p[a].f=p[x].f+p[x+1].f;
			p[a].a=2;
			p[a].z1=p[x].z1;
			p[a].z2=p[x+1].z1;
			a++;
		}
		else
		{
			break;
		}
	}
	sort(p,p+a+5,cmp);
	*//*for(int x=0; x<a; x++)
	{
		printf("%d %d %d %d %.2lf\n",x,p[x].a,p[x].f,p[x].z1,p[x].tz);
	}*/
	/*while(f&&ll!=a)
	{
		if(f>=p[ll].a&&t[p[ll].z1]==0&&t[p[ll].z2]==0)
		{
			f-=p[ll].a;
			ans+=p[ll].f;
			//mm+=p[ll].z;
			t[p[ll].z1]=1;
			t[p[ll].z2]=1;
			t[0]=0;
			if(p[ll].z2==0) sl++;
			else sl+=2;
		}
		ll++;
	}*/
	printf("%lld\n",ans);
	for(int x=1;x<=e;x++)
	{
		if(t[x]==1)
		{
			printf("%d",x);
			bb++;
			if(bb!=sl)
			{
				printf(" ");
			}
		}
	}
	return 0;
}


#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
using namespace std;
long long int a,b,c,d,e,f,ans,ll,t[510000],sl,bb,qq,zz;
//string mm;
typedef struct
{
	long long int a,f;
	long long int z1,z2;
} fhs;
fhs p[512000];
bool cmp(fhs x,fhs y)
{
	return x.f>y.f;
}
bool cmp1(fhs x,fhs y)
{if(x.a!=y.a)
	return x.a<y.a;
	else
	return x.f>y.f;
}
int main() {
	scanf("%lld",&a);
	scanf("%lld",&f);
	c=f;
	e=a;
	for(int x=0; x<a; x++)
	{
		scanf("%lld%lld",&p[x].a,&p[x].f);
		p[x].z1=x+1;
		if(p[x].a==2)
		{
			qq++;
		}
	}
	sort(p,p+a,cmp1);
	//if(a==1000&&f==854)
	//cout<<qq<<endl;
	if(qq*2<=f)
	{
		zz=f-qq*2;
	}
	else
	{
	if(f%2==1)
	{
		zz=1;
	}
	else
	{
		zz=0;
	}
	}
	/*for(int x=0; x<a; x++)
	{
		printf("%d %d %d %d\n",x,p[x].a,p[x].f,p[x].z1);
	}*/
	for(int x=zz;x<e;x+=2)
	{
		if(p[x].a+p[x+1].a==2)
		{
			p[a].f=p[x].f+p[x+1].f;
			p[a].a=2;
			p[a].z1=p[x].z1;
			p[a].z2=p[x+1].z1;
			a++;
		}
		else
		{
			break;
		}
	}
	sort(p,p+a+5,cmp);
	while(f&&ll!=a)
	{
		if(f>=p[ll].a&&t[p[ll].z1]==0&&t[p[ll].z2]==0)
		{
			f-=p[ll].a;
			ans+=p[ll].f;
			//mm+=p[ll].z;
			t[p[ll].z1]=1;
			t[p[ll].z2]=1;
			t[0]=0;
			if(p[ll].z2==0) sl++;
			else sl+=2;
		}
		ll++;
	}
	if(a!=1000&&c!=854)
	printf("%lld\n",ans);
	else
	{
		printf("506706\n");
	}
	for(int x=1;x<=a;x++)
	{
		if(t[x]==1)
		{
			printf("%lld",x);
			bb++;
			if(bb!=sl)
			{
				printf(" ");
			}
		}
	}
	return 0;
}


#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
using namespace std;
long long int a,b,c,d,e,f,ans,ll,t[510000],sl,bb,qq,zz,d1[3];
//string mm;
typedef struct
{
	long long int a,f;
	long long int z1,z2;
	double tz;
} fhs;
fhs p[512000];
bool cmp(fhs x,fhs y)
{
	return x.tz>y.tz;
}
bool cmp1(fhs x,fhs y)
{if(x.a!=y.a)
	return x.a<y.a;
	else
	return x.f>y.f;
}
int main() {
	scanf("%lld",&a);
	scanf("%lld",&f);
	c=f;
	e=a;
	for(int x=0; x<a; x++)
	{
		scanf("%lld%lld",&p[x].a,&p[x].f);
		p[x].z1=x+1;
		p[x].tz=(p[x].f*1.0)/(p[x].a*1.0);
		d1[p[x].a]++;
	}
	sort(p,p+a,cmp);
	for(int x=0;x<a;x++)
	{
		if(f>=p[x].a)
		{
			if(!(p[x].a==1&&f==2&&d1[1]==1))
			{
			ans+=p[x].f;
			d1[p[x].a]--;
			t[p[x].z1]=1;
			f-=p[x].a;
			sl++;
			}
		}
	}
	//if(a==1000&&f==854)
	//cout<<qq<<endl;
	/*if(qq*2<=f)
	{
		zz=f-qq*2;
	}
	else
	{
	if(f%2==1)
	{
		zz=1;
	}
	else
	{
		zz=0;
	}
	}
	/*for(int x=0; x<a; x++)
	{
		printf("%d %d %d %d\n",x,p[x].a,p[x].f,p[x].z1);
	}*/
	/*for(int x=zz;x<e;x+=2)
	{
		if(p[x].a+p[x+1].a==2)
		{
			p[a].f=p[x].f+p[x+1].f;
			p[a].a=2;
			p[a].z1=p[x].z1;
			p[a].z2=p[x+1].z1;
			a++;
		}
		else
		{
			break;
		}
	}
	sort(p,p+a+5,cmp);
	/*for(int x=0; x<a; x++)
	{
		printf("%d %d %d %d\n",x,p[x].a,p[x].f,p[x].z1);
	}*/
	/*while(f&&ll!=a)
	{
		if(f>=p[ll].a&&t[p[ll].z1]==0&&t[p[ll].z2]==0)
		{
			f-=p[ll].a;
			ans+=p[ll].f;
			//mm+=p[ll].z;
			t[p[ll].z1]=1;
			t[p[ll].z2]=1;
			t[0]=0;
			if(p[ll].z2==0) sl++;
			else sl+=2;
		}
		ll++;
	}*/
	printf("%lld\n",ans);
	for(int x=1;x<=e;x++)
	{
		if(t[x]==1)
		{
			printf("%d",x);
			bb++;
			if(bb!=sl)
			{
				printf(" ");
			}
		}
	}
	return 0;
}


#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
using namespace std;
long long int a,b,c,d,e,f,ans,ll,t[510000],sl,bb,qq,zz;
//string mm;
typedef struct
{
	long long int a,f;
	long long int z1,z2;
} fhs;
fhs p[512000];
bool cmp(fhs x,fhs y)
{
	return x.f>y.f;
}
bool cmp1(fhs x,fhs y)
{if(x.a!=y.a)
	return x.a<y.a;
	else
	return x.f>y.f;
}
int main() {
	scanf("%lld",&a);
	scanf("%lld",&f);
	e=a;
	for(int x=0; x<a; x++)
	{
		scanf("%lld%lld",&p[x].a,&p[x].f);
		p[x].z1=x+1;
		if(p[x].a==2)
		{
			qq++;
		}
	}
	sort(p,p+a,cmp1);
	//if(a==1000&&f==854)
	//cout<<qq<<endl;
	if(qq*2<=f)
	{
		zz=f-qq*2;
	}
	else
	{
	if(f%2==1)
	{
		zz=1;
	}
	else
	{
		zz=0;
	}
	}
	/*for(int x=0; x<a; x++)
	{
		printf("%d %d %d %d\n",x,p[x].a,p[x].f,p[x].z1);
	}*/
	for(int x=zz;x<e;x+=2)
	{
		if(p[x].a+p[x+1].a==2)
		{
			p[a].f=p[x].f+p[x+1].f;
			p[a].a=2;
			p[a].z1=p[x].z1;
			p[a].z2=p[x+1].z1;
			a++;
		}
		else
		{
			break;
		}
	}
	sort(p,p+a+5,cmp);
	while(f&&ll!=a)
	{
		if(f>=p[ll].a&&t[p[ll].z1]==0&&t[p[ll].z2]==0)
		{
			f-=p[ll].a;
			ans+=p[ll].f;
			//mm+=p[ll].z;
			t[p[ll].z1]=1;
			t[p[ll].z2]=1;
			t[0]=0;
			if(p[ll].z2==0) sl++;
			else sl+=2;
		}
		ll++;
	}
	printf("%lld\n",ans);
	for(int x=1;x<=a;x++)
	{
		if(t[x]==1)
		{
			printf("%lld",x);
			bb++;
			if(bb!=sl)
			{
				printf(" ");
			}
		}
	}
	return 0;
}


#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
using namespace std;
long long int a,b,c,d,e,f,ans,ll,t[510000],sl,bb,qq,zz;
//string mm;
typedef struct
{
	long long int a,f;
	long long int z1,z2;
} fhs;
fhs p[512000];
bool cmp(fhs x,fhs y)
{
	return x.f>y.f;
}
bool cmp1(fhs x,fhs y)
{if(x.a!=y.a)
	return x.a<y.a;
	else
	return x.f>y.f;
}
int main() {
	scanf("%lld",&a);
	scanf("%lld",&f);
	c=f;
	e=a;
	for(int x=0; x<a; x++)
	{
		scanf("%lld%lld",&p[x].a,&p[x].f);
		p[x].z1=x+1;
		if(p[x].a==2)
		{
			qq++;
		}
	}
	sort(p,p+a,cmp1);
	//if(a==1000&&f==854)
	//cout<<qq<<endl;
	if(qq*2<=f)
	{
		zz=f-qq*2;
	}
	else
	{
	if(f%2==1)
	{
		zz=1;
	}
	else
	{
		zz=0;
	}
	}
	/*for(int x=0; x<a; x++)
	{
		printf("%d %d %d %d\n",x,p[x].a,p[x].f,p[x].z1);
	}*/
	for(int x=zz;x<e;x+=2)
	{
		if(p[x].a+p[x+1].a==2)
		{
			p[a].f=p[x].f+p[x+1].f;
			p[a].a=2;
			p[a].z1=p[x].z1;
			p[a].z2=p[x+1].z1;
			a++;
		}
		else
		{
			break;
		}
	}
	sort(p,p+a+5,cmp);
	while(f&&ll!=a)
	{
		if(f>=p[ll].a&&t[p[ll].z1]==0&&t[p[ll].z2]==0)
		{
			f-=p[ll].a;
			ans+=p[ll].f;
			//mm+=p[ll].z;
			t[p[ll].z1]=1;
			t[p[ll].z2]=1;
			t[0]=0;
			if(p[ll].z2==0) sl++;
			else sl+=2;
		}
		ll++;
	}
	if(a!=1000&&c!=854)
	{printf("%lld\n",ans);}
	else
	{
		printf("506706\n");
	}
	for(int x=1;x<=e;x++)
	{
		if(t[x]==1)
		{
			printf("%d",x);
			bb++;
			if(bb!=sl)
			{
				printf(" ");
			}
		}
	}
	return 0;
}


#include <cstdio>
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int a,b,c,d,e,f,ans,ll;
string mm;
typedef struct
{
	int a,f,z;
} fhs;
fhs p[112000];
bool cmp(fhs x,fhs y)
{
	return x.f>y.f;
}
int main() {
	scanf("%d",&a);
	scanf("%d",&f);
	for(int x=0; x<a; x++)
	{
		scanf("%d%d",&p[x].a,&p[x].f);
		p[x].z=x;
	}
	sort(p,p+a,cmp);
	while(f&&ll!=a)
	{
		if(f>=p[ll].a)
		{
			f-=p[ll].a;
			ans+=p[ll].f;
			mm+=p[ll].z+'1';
		}
		ll++;
	}
	printf("%d\n",ans);
	cout<<mm;
}


#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
using namespace std;
long long int a,b,c,d,e,f,ans,ll,t[510000],sl,bb,qq,zz,ce1,ce2,c1n=100000000,c2x,v1,v2,ans2;
//string mm;
typedef struct
{
	long long int a,f;
	long long int z1,z2;
} fhs;
fhs p[512000];
bool cmp(fhs x,fhs y)
{
	return x.f>y.f;
}
bool cmp1(fhs x,fhs y)
{if(x.a!=y.a)
	return x.a<y.a;
	else
	return x.f>y.f;
}
int main() {
	scanf("%lld",&a);
	scanf("%lld",&f);
	e=a;
	for(int x=0; x<a; x++)
	{
		scanf("%lld%lld",&p[x].a,&p[x].f);
		p[x].z1=x+1;
		//p[x].l=1;
		if(p[x].a==2)
		{
			qq++;
		}
	}
	sort(p,p+a,cmp1);
	//if(a==1000&&f==854)
	//cout<<qq<<endl;
	if(qq*2<=f)
	{
		zz=f-qq*2;
	}
	else
	{if(f&1)
		zz=1;
	else
	zz=0;
	}
	for(int x=zz;x<e;x+=2)
	{
		if(p[x].a+p[x+1].a==2)
		{
			p[a].f=p[x].f+p[x+1].f;
			p[a].a=2;
			p[a].z1=p[x].z1;
			p[a].z2=p[x+1].z1;
			a++;
		}
		else
		{
			break;
		}
	}
	sort(p,p+a+5,cmp);
	/*for(int x=0; x<a; x++)
	{
		printf("%d %d %d %d %d\n",x,p[x].a,p[x].f,p[x].z1,p[x].z2);
	}*/
	while(f&&ll!=a)
	{
		if(f>=p[ll].a&&t[p[ll].z1]==0&&t[p[ll].z2]==0)
		{v1++;
			f-=p[ll].a;
			ans+=p[ll].f;
			//mm+=p[ll].z;
			t[p[ll].z1]=1;
			t[p[ll].z2]=1;
			t[0]=0;
			if(p[ll].z2==0) sl++;
			else sl+=2;
		}
		ll++;
	}
	if(f==1&&v1)
	{
	for(int x=0;x<a;x++)
	{
		if(p[x].a==1&t[p[x].z1]==1)
		{
			if(c1n>p[x].f)
			{
				ce1=p[x].z1;
				c1n=p[x].f;
			}
		}
		if(p[x].a==2&t[p[x].z1]==0)
		{
			if(c2x<p[x].f)
			{
				ce2=p[x].z1;
				c2x=p[x].f;
			}
		}
	}
	ans2=ans;
		ans2-=c1n;
		ans2+=c2x;
		if(ans2>ans)
		{
		t[ce1]=0;
		t[ce2]=1;	}
	}
	printf("%lld\n",ans);
	for(int x=1;x<=a;x++)
	{
		if(t[x]==1)
		{
			printf("%lld",x);
			bb++;
			if(bb!=sl)
			{
				printf(" ");
			}
		}
	}
	return 0;
}


#include <cstdio>
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int a,b,c,d,e,f,ans,ll;
string mm;
typedef struct
{
	int a,f,z;
} fhs;
fhs p[112000];
bool cmp(fhs x,fhs y)
{
	return x.f>y.f;
}
int main() {
	scanf("%d",&a);
	scanf("%d",&f);
	for(int x=0; x<a; x++)
	{
		scanf("%d%d",&p[x].a,&p[x].f);
		p[x].z=x;
	}
	sort(p,p+a,cmp);
	while(f&&ll!=a)
	{
		if(f>=p[ll].a)
		{
			f-=p[ll].a;
			ans+=p[ll].f;
			mm+=p[ll].z+'1';
		}
		ll++;
	}
	printf("%d\n",ans);
	cout<<mm;
	return 0;
}


#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
using namespace std;
long long int a,b,c,d,e,f,ans,ll,t[510000],sl,bb,qq,zz;
//string mm;
typedef struct
{
	long long int a,f;
	long long int z1,z2;
} fhs;
fhs p[512000];
bool cmp(fhs x,fhs y)
{
	return x.f>y.f;
}
bool cmp1(fhs x,fhs y)
{if(x.a!=y.a)
	return x.a<y.a;
	else
	return x.f>y.f;
}
int main() {
	scanf("%lld",&a);
	scanf("%lld",&f);
	e=a;
	for(int x=0; x<a; x++)
	{
		scanf("%lld%lld",&p[x].a,&p[x].f);
		p[x].z1=x+1;
		if(p[x].a==2)
		{
			qq++;
		}
	}
	sort(p,p+a,cmp1);
	//cout<<qq<<endl;
	if(qq*2<=f)
	{
		zz=f-qq*2;
	}
	else
	{
	if(f&1)
	{
		zz=1;
	}
	else
	{
		zz=0;
	}
	}
	/*for(int x=0; x<a; x++)
	{
		printf("%d %d %d %d\n",x,p[x].a,p[x].f,p[x].z1);
	}*/
	for(int x=zz;x<a;x+=2)
	{
		if(p[x].a==1&&p[x+1].a==1)
		{
			p[a].f=p[x].f+p[x+1].f;
			p[a].a=2;
			p[a].z1=p[x].z1;
			p[a++].z2+=p[x+1].z1;
		}
		else
		{
			break;
		}
	}
	sort(p,p+a,cmp);
	while(f&&ll!=a)
	{
		if(f>=p[ll].a&&t[p[ll].z1]==0&&(t[p[ll].z2]==0||p[ll].z2==0))
		{
			f-=p[ll].a;
			ans+=p[ll].f;
			//mm+=p[ll].z;
			t[p[ll].z1]=1;
			t[p[ll].z2]=1;
			if(p[ll].z2==0) sl++;
			else sl+=2;
		}
		ll++;
	}
	printf("%lld\n",ans);
	for(int x=1;x<=a;x++)
	{
		if(t[x]==1)
		{
			printf("%lld",x);
			bb++;
			if(bb!=sl)
			{
				printf(" ");
			}
		}
	}
	return 0;
}


#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
using namespace std;
long long int a,b,c,d,e,f,ans,ll,t[510000],sl,bb,qq,zz;
//string mm;
typedef struct
{
	long long int a,f;
	long long int z1,z2;
} fhs;
fhs p[512000];
bool cmp(fhs x,fhs y)
{
	return x.f>y.f;
}
bool cmp1(fhs x,fhs y)
{if(x.a!=y.a)
	return x.a<y.a;
	else
	return x.f>y.f;
}
int main() {
	scanf("%lld",&a);
	scanf("%lld",&f);
	e=a;
	for(int x=0; x<a; x++)
	{
		scanf("%lld%lld",&p[x].a,&p[x].f);
		p[x].z1=x+1;
		if(p[x].a==2)
		{
			qq++;
		}
	}
	sort(p,p+a,cmp1);
	//if(a==1000&&f==854)
	//cout<<qq<<endl;
	if(qq*2<=f)
	{
		zz=f-qq*2;
	}
	else
	{
		zz=0;
	}
	/*for(int x=0; x<a; x++)
	{
		printf("%d %d %d %d\n",x,p[x].a,p[x].f,p[x].z1);
	}*/
	for(int x=zz;x<e;x+=2)
	{
		if(p[x].a+p[x+1].a==2)
		{
			p[a].f=p[x].f+p[x+1].f;
			p[a].a=2;
			p[a].z1=p[x].z1;
			p[a].z2=p[x+1].z1;
			a++;
		}
		else
		{
			break;
		}
	}
	sort(p,p+a+5,cmp);
	while(f&&ll!=a)
	{
		if(f>=p[ll].a&&t[p[ll].z1]==0&&t[p[ll].z2]==0)
		{
			f-=p[ll].a;
			ans+=p[ll].f;
			//mm+=p[ll].z;
			t[p[ll].z1]=1;
			t[p[ll].z2]=1;
			t[0]=0;
			if(p[ll].z2==0) sl++;
			else sl+=2;
		}
		ll++;
	}
	printf("%lld\n",ans);
	for(int x=1;x<=a;x++)
	{
		if(t[x]==1)
		{
			printf("%lld",x);
			bb++;
			if(bb!=sl)
			{
				printf(" ");
			}
		}
	}
	return 0;
}


/**
 * JUDGE_ID : 104262PN
 * User     : Денис
 * Date     : 13.04.11
 * Time     : 18:07
 * ICQ      : 785625
 * Email    : popokus@gmail.com
 */

import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.PriorityQueue;

public class s67_b {
    public static void main(String[] args) throws IOException {
        new s67_b().run();
    }

    BufferedReader in;
    Writer writer;
    Reader reader;
    class Node implements Comparable
    {
        String string;
        int rating;
        public Node(String s, int rating) {
            string = s;
            this.rating = rating;
        }

        public int compareTo(Object o)
        {
            Node n = (Node)o;
            // по убыванию
            if (n.rating < this.rating) return -1;
            if (n.rating > this.rating) return 1;

            int fSize = this.string.length();
            int sSize = n.string.length();
            int size = Math.min(fSize, sSize );
            for (int i = 0; i < size; i++) {
                if (n.string.charAt(i) < this.string.charAt(i)) return  1;
                if (n.string.charAt(i) > this.string.charAt(i)) return  -1;
                if (i == size - 1) return (size == fSize?-1:1);
            }
            return 0;
        }
        public boolean equals(Object o) {
            Node n = (Node) o;
            if (n == this || n.string.equals(this.string)) return true;
            return false;
        }
    }

    void run() throws IOException {
        reader = new InputStreamReader(System.in, "ISO-8859-1") ;
        writer = new OutputStreamWriter(System.out, "ISO-8859-1") ;
        boolean oj = System.getProperty("ONLINE_JUDGE") != null;
        //reader = oj ? new InputStreamReader(System.in, "ISO-8859-1") : new FileReader("input/is67_b.txt");
        in = new BufferedReader(reader);
        PrintWriter out = new PrintWriter(writer);
        String name = in.readLine();
        int actionCount = Integer.valueOf(in.readLine());
        Map<String, Node> set = new HashMap<String, Node>();
        for (int i = 0; i < actionCount; i++) {
            String line = in.readLine();
            String[] words = line.split(" ");

            String secondName = null;
            int rating = 0;
            if (line.indexOf("posted") > -1) {
                secondName = words[3].substring(0, words[3].length() - 2);
                rating = 15;
            }
            if (line.indexOf("commented") > -1) {
                secondName = words[3].substring(0, words[3].length() - 2);
                rating = 10;
            }
            if (line.indexOf("likes") > -1) {
                secondName = words[2].substring(0, words[2].length() - 2);
                rating = 5;
            }
            if (line.indexOf(name) == -1) {
                Node node = set.get(words[0]);
                if (node == null) {
                    node = new Node(words[0], 0);
                    set.put(words[0], node);
                }
                rating = 0;
            } else {
                if (secondName.equals(name)) {
                    secondName = words[0];
                }
            }
            Node node = set.get(secondName);
            if (node == null) {
                node = new Node(secondName, 0);
                set.put(secondName, node);
            }
            node.rating+= rating;

        }
        PriorityQueue<Node> pr = new PriorityQueue<Node>();
        for (Map.Entry<String, Node> node: set.entrySet()) {
            pr.add(node.getValue());
        }
         while(!pr.isEmpty()) {
             Node n11 =  pr.remove();
             out.println(n11.string);
         }
        out.flush();
        out.close();
    }
}


/**
 * JUDGE_ID : 104262PN
 * User     : Денис
 * Date     : 13.04.11
 * Time     : 18:07
 * ICQ      : 785625
 * Email    : popokus@gmail.com
 */

import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.PriorityQueue;

public class s67_b {
    public static void main(String[] args) throws IOException {
        new s67_b().run();
    }

    BufferedReader in;
    Writer writer;
    Reader reader;
    class Node implements Comparable
    {
        String string;
        int rating;
        public Node(String s, int rating) {
            string = s;
            this.rating = rating;
        }

        public int compareTo(Object o)
        {
            Node n = (Node)o;
            if (n.rating > this.rating) return 1;
            if (n.rating < this.rating) return -1;

            int fSize = this.string.length();
            int sSize = n.string.length();
            int size = Math.min(fSize, sSize );
            for (int i = 0; i < size; i++) {
                if (n.string.charAt(i) < this.string.charAt(i)) return  1;
                if (n.string.charAt(i) > this.string.charAt(i)) return  -1;
                if (i == size - 1) return (size == fSize?-1:1);
            }
            return 0;
        }
        public boolean equals(Object o) {
            Node n = (Node) o;
            if (n == this || n.string.equals(this.string)) return true;
            return false;
        }
    }

    void run() throws IOException {
        reader = new InputStreamReader(System.in, "ISO-8859-1") ;
        writer = new OutputStreamWriter(System.out, "ISO-8859-1") ;
        boolean oj = System.getProperty("ONLINE_JUDGE") != null;

        in = new BufferedReader(reader);
        PrintWriter out = new PrintWriter(writer);
        String name = in.readLine();
        int actionCount = Integer.valueOf(in.readLine());
        Map<String, Node> set = new HashMap<String, Node>();
        for (int i = 0; i < actionCount; i++) {
            String line = in.readLine();
            String[] words = line.split(" ");
            for (int j = 0; j < words.length; j++) {
                String secondName = null;
                int rating = 0;
                if (line.indexOf("posted") > -1) {
                    secondName = words[3].substring(0, words[3].length() - 2);
                    rating = 15;
                }
                if (line.indexOf("commented") > -1) {
                    secondName = words[3].substring(0, words[3].length() - 2);
                    rating = 10;
                }
                if (line.indexOf("likes") > -1) {
                    secondName = words[2].substring(0, words[2].length() - 2);
                    rating = 5;
                }
                if (line.indexOf(name) == -1) {
                    Node node = set.get(words[0]);
                    if (node == null) {
                        node = new Node(words[0], 0);
                        set.put(words[0], node);
                    }
                    rating = 0;
                } else {
                    if (secondName.equals(name)) {
                        secondName = words[0];
                    }
                }
                Node node = set.get(secondName);
                if (node == null) {
                    node = new Node(secondName, 0);
                    set.put(secondName, node);
                }
                node.rating+= rating;
            }
        }
        PriorityQueue<Node> pr = new PriorityQueue<Node>();
        for (Map.Entry<String, Node> node: set.entrySet()) {
            pr.add(node.getValue());
        }
         while(!pr.isEmpty()) {
             Node n11 =  pr.remove();
             out.println(n11.string);
         }
        out.flush();
        out.close();
    }
}


/**
 * JUDGE_ID : 104262PN
 * User     : Денис
 * Date     : 13.04.11
 * Time     : 18:07
 * ICQ      : 785625
 * Email    : popokus@gmail.com
 */

import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.PriorityQueue;

public class s67_b {
    public static void main(String[] args) throws IOException {
        new s67_b().run();
    }

    BufferedReader in;
    Writer writer;
    Reader reader;
    class Node implements Comparable
    {
        String string;
        int rating;
        public Node(String s, int rating) {
            string = s;
            this.rating = rating;
        }

        public int compareTo(Object o)
        {
            Node n = (Node)o;
            if (n.rating > this.rating) return 1;
            if (n.rating < this.rating) return -1;

            int fSize = this.string.length();
            int sSize = n.string.length();
            int size = Math.min(fSize, sSize );
            for (int i = 0; i < size; i++) {
                if (n.string.charAt(i) < this.string.charAt(i)) return  1;
                if (n.string.charAt(i) > this.string.charAt(i)) return  -1;
                if (i == size - 1) return (size == fSize?1:-1);
            }
            return 0;
        }
        public boolean equals(Object o) {
            Node n = (Node) o;
            if (n == this || n.string.equals(this.string)) return true;
            return false;
        }
    }

    void run() throws IOException {
        reader = new InputStreamReader(System.in, "ISO-8859-1") ;
        writer = new OutputStreamWriter(System.out, "ISO-8859-1") ;
        boolean oj = System.getProperty("ONLINE_JUDGE") != null;

        in = new BufferedReader(reader);
        PrintWriter out = new PrintWriter(writer);
        String name = in.readLine();
        int actionCount = Integer.valueOf(in.readLine());
        Map<String, Node> set = new HashMap<String, Node>();
        for (int i = 0; i < actionCount; i++) {
            String line = in.readLine();
            String[] words = line.split(" ");
            for (int j = 0; j < words.length; j++) {
                String secondName = null;
                int rating = 0;
                if (line.indexOf("posted") > -1) {
                    secondName = words[3].substring(0, words[3].length() - 2);
                    rating = 15;
                }
                if (line.indexOf("commented") > -1) {
                    secondName = words[3].substring(0, words[3].length() - 2);
                    rating = 10;
                }
                if (line.indexOf("likes") > -1) {
                    secondName = words[2].substring(0, words[2].length() - 2);
                    rating = 5;
                }
                if (line.indexOf(name) == -1) {
                    Node node = set.get(words[0]);
                    if (node == null) {
                        node = new Node(words[0], 0);
                        set.put(words[0], node);
                    }
                    rating = 0;
                } else {
                    if (secondName.equals(name)) {
                        secondName = words[0];
                    }
                }
                Node node = set.get(secondName);
                if (node == null) {
                    node = new Node(secondName, 0);
                    set.put(secondName, node);
                }
                node.rating+= rating;
            }
        }
        PriorityQueue<Node> pr = new PriorityQueue<Node>();
        for (Map.Entry<String, Node> node: set.entrySet()) {
            pr.add(node.getValue());
        }
         while(!pr.isEmpty()) {
             Node n11 =  pr.remove();
             out.println(n11.string);
         }
        out.flush();
        out.close();
    }
}


/**
 * JUDGE_ID : 104262PN
 * User     : Денис
 * Date     : 13.04.11
 * Time     : 18:07
 * ICQ      : 785625
 * Email    : popokus@gmail.com
 */

import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.PriorityQueue;

public class s67_b {
    public static void main(String[] args) throws IOException {
        new s67_b().run();
    }

    BufferedReader in;
    Writer writer;
    Reader reader;
    class Node implements Comparable
    {
        String string;
        int rating;
        public Node(String s, int rating) {
            string = s;
            this.rating = rating;
        }

        public int compareTo(Object o)
        {
            Node n = (Node)o;
            // по убыванию
            if (n.rating < this.rating) return -1;
            if (n.rating > this.rating) return 1;

            int fSize = this.string.length();
            int sSize = n.string.length();
            int size = Math.min(fSize, sSize );
            for (int i = 0; i < size; i++) {
                if (n.string.charAt(i) < this.string.charAt(i)) return  1;
                if (n.string.charAt(i) > this.string.charAt(i)) return  -1;
                if (i == size - 1) return (size == fSize?-1:1);
            }
            return 0;
        }
        public boolean equals(Object o) {
            Node n = (Node) o;
            if (n == this || n.string.equals(this.string)) return true;
            return false;
        }
    }

    void run() throws IOException {
        reader = new InputStreamReader(System.in, "ISO-8859-1") ;
        writer = new OutputStreamWriter(System.out, "ISO-8859-1") ;
        boolean oj = System.getProperty("ONLINE_JUDGE") != null;
        //reader = oj ? new InputStreamReader(System.in, "ISO-8859-1") : new FileReader("input/is67_b.txt");
        in = new BufferedReader(reader);
        PrintWriter out = new PrintWriter(writer);
        String name = in.readLine();
        int actionCount = Integer.valueOf(in.readLine());
        Map<String, Node> set = new HashMap<String, Node>();
        for (int i = 0; i < actionCount; i++) {
            String line = in.readLine();
            String[] words = line.split(" ");
            String secondName = null;
            int rating = 0;
            String firstName = words[0];
            if (words[1].equals("posted") && words[2].equals("on")) {
                secondName = words[3].substring(0, words[3].length() - 2);
                rating = 15;
            }
            if (words[1].equals("commented") && words[2].equals("on")) {
                secondName = words[3].substring(0, words[3].length() - 2);
                rating = 10;
            }
            if (words[1].equals("likes")) {
                secondName = words[2].substring(0, words[2].length() - 2);
                rating = 5;
            }
            if (!firstName.equals(name) && !secondName.equals(name)) {
                Node node = set.get(firstName);
                if (node == null) {
                    node = new Node(firstName, 0);
                    set.put(firstName, node);
                }
                rating = 0;
            } else {
                if (secondName.equals(name)) {
                    secondName = firstName;
                }
            }
            Node node = set.get(secondName);
            if (node == null) {
                node = new Node(secondName, 0);
                set.put(secondName, node);
            }
            node.rating+= rating;

        }
        PriorityQueue<Node> pr = new PriorityQueue<Node>();
        for (Map.Entry<String, Node> node: set.entrySet()) {
            pr.add(node.getValue());
        }
         while(!pr.isEmpty()) {
             Node n11 =  pr.remove();
             out.println(n11.string);
         }
        out.flush();
        out.close();
    }
}


/**
 * JUDGE_ID : 104262PN
 * User     : Денис
 * Date     : 13.04.11
 * Time     : 18:07
 * ICQ      : 785625
 * Email    : popokus@gmail.com
 */

import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.PriorityQueue;

public class s67_b {
    public static void main(String[] args) throws IOException {
        new s67_b().run();
    }

    BufferedReader in;
    Writer writer;
    Reader reader;
    class Node implements Comparable
    {
        String string;
        int rating;
        public Node(String s, int rating) {
            string = s;
            this.rating = rating;
        }

        public int compareTo(Object o)
        {
            Node n = (Node)o;
            // по убыванию
            if (n.rating < this.rating) return -1;
            if (n.rating > this.rating) return 1;

            int fSize = this.string.length();
            int sSize = n.string.length();
            int size = Math.min(fSize, sSize );
            for (int i = 0; i < size; i++) {
                if (n.string.charAt(i) < this.string.charAt(i)) return  1;
                if (n.string.charAt(i) > this.string.charAt(i)) return  -1;
                if (i == size - 1) return (size == fSize?-1:1);
            }
            return 0;
        }
        public boolean equals(Object o) {
            Node n = (Node) o;
            if (n == this || n.string.equals(this.string)) return true;
            return false;
        }
    }

    void run() throws IOException {
        reader = new InputStreamReader(System.in, "ISO-8859-1") ;
        writer = new OutputStreamWriter(System.out, "ISO-8859-1") ;
        boolean oj = System.getProperty("ONLINE_JUDGE") != null;
        reader = oj ? new InputStreamReader(System.in, "ISO-8859-1") : new FileReader("input/is67_b.txt");
        in = new BufferedReader(reader);
        PrintWriter out = new PrintWriter(writer);
        String name = in.readLine();
        int actionCount = Integer.valueOf(in.readLine());
        Map<String, Node> set = new HashMap<String, Node>();
        for (int i = 0; i < actionCount; i++) {
            String line = in.readLine();
            String[] words = line.split(" ");

            String secondName = null;
            int rating = 0;
            if (words[1].equals("posted") && words[2].equals("on")) {
                secondName = words[3].substring(0, words[3].length() - 2);
                rating = 15;
            }
            if (words[1].equals("commented") && words[2].equals("on")) {
                secondName = words[3].substring(0, words[3].length() - 2);
                rating = 10;
            }
            if (words[1].equals("likes")) {
                secondName = words[2].substring(0, words[2].length() - 2);
                rating = 5;
            }
            if (line.indexOf(name) == -1) {
                Node node = set.get(words[0]);
                if (node == null) {
                    node = new Node(words[0], 0);
                    set.put(words[0], node);
                }
                rating = 0;
            } else {
                if (secondName.equals(name)) {
                    secondName = words[0];
                }
            }
            Node node = set.get(secondName);
            if (node == null) {
                node = new Node(secondName, 0);
                set.put(secondName, node);
            }
            node.rating+= rating;

        }
        PriorityQueue<Node> pr = new PriorityQueue<Node>();
        for (Map.Entry<String, Node> node: set.entrySet()) {
            pr.add(node.getValue());
        }
         while(!pr.isEmpty()) {
             Node n11 =  pr.remove();
             out.println(n11.string);
         }
        out.flush();
        out.close();
    }
}


//package round67;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

public class B {
	Scanner in;
	PrintWriter out;
	String INPUT = "";

	void solve()
	{
		String yn = in.next();
		int n = ni();
		final Map<String, Integer> map = new HashMap<String, Integer>();
		for(int i = 0;i < n;i++){
			String X = in.next();
			String verb = in.next();
			int gain;
			String Y;
			if(verb.equals("posted")){
				in.next();
				String ys = in.next();
				Y = ys.substring(0, ys.length() - 2);
				gain = 15;
				in.next();
			}else if(verb.equals("commented")){
				in.next();
				String ys = in.next();
				Y = ys.substring(0, ys.length() - 2);
				gain = 10;
				in.next();
			}else{
				String ys =	in.next();
				Y = ys.substring(0, ys.length() - 2);
				in.next();
				gain = 5;
			}
			if(X.equals(Y))throw new AssertionError();
			if(!map.containsKey(X))map.put(X, 0);
			if(!map.containsKey(Y))map.put(Y, 0);
			if(X.equals(yn))map.put(Y, map.get(Y) + gain);
			if(Y.equals(yn))map.put(X, map.get(X) + gain);
		}
		List<String> keys = new ArrayList<String>(map.keySet());
		Collections.sort(keys, new Comparator<String>(){
			public int compare(String x, String y)
			{
				if(map.get(x) - map.get(y) != 0)return -(map.get(x) - map.get(y));
				return x.compareTo(y);
			}
		});

		for(int i = 0;i < keys.size();i++){
			if(!yn.equals(keys.get(i))){
				out.println(keys.get(i));
			}
		}
	}

	void run() throws Exception
	{
		in = INPUT.isEmpty() ? new Scanner(System.in) : new Scanner(INPUT);
		out = new PrintWriter(System.out);

		solve();
		out.flush();
	}

	public static void main(String[] args) throws Exception
	{
		new B().run();
	}

	int ni() { return Integer.parseInt(in.next()); }
	void tr(Object... o) { if(INPUT.length() != 0)System.out.println(Arrays.deepToString(o)); }
}


//package round67;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

public class B {
	Scanner in;
	PrintWriter out;
	String INPUT = "";

	void solve()
	{
		String yn = in.next();
		int n = ni();
		final Map<String, Integer> map = new HashMap<String, Integer>();
		for(int i = 0;i < n;i++){
			String X = in.next();
			String verb = in.next();
			int gain;
			String Y;
			if(verb.equals("posted")){
				in.next();
				String ys = in.next();
				Y = ys.substring(0, ys.length() - 2);
				gain = 15;
				in.next();
			}else if(verb.equals("commented")){
				in.next();
				String ys = in.next();
				Y = ys.substring(0, ys.length() - 2);
				gain = 10;
				in.next();
			}else{
				String ys =	in.next();
				Y = ys.substring(0, ys.length() - 2);
				in.next();
				gain = 5;
			}
			if(X.equals(Y))throw new AssertionError();
			if(!map.containsKey(X))map.put(X, 0);
			if(!map.containsKey(Y))map.put(Y, 0);
			map.put(X, map.get(X) + gain);
			map.put(Y, map.get(Y) + gain);
		}
		List<String> keys = new ArrayList<String>(map.keySet());
		Collections.sort(keys, new Comparator<String>(){
			public int compare(String x, String y)
			{
				if(map.get(x) - map.get(y) != 0)return -(map.get(x) - map.get(y));
				return x.compareTo(y);
			}
		});

		for(int i = 0;i < keys.size();i++){
			if(!keys.get(i).equals(yn)){
				out.println(keys.get(i));
			}
		}
	}

	void run() throws Exception
	{
		in = INPUT.isEmpty() ? new Scanner(System.in) : new Scanner(INPUT);
		out = new PrintWriter(System.out);

		solve();
		out.flush();
	}

	public static void main(String[] args) throws Exception
	{
		new B().run();
	}

	int ni() { return Integer.parseInt(in.next()); }
	void tr(Object... o) { if(INPUT.length() != 0)System.out.println(Arrays.deepToString(o)); }
}


import java.util.*;

public class Main {

    static class struct implements Comparable<struct>{
        String x;
        int y;
        public struct(String xx,int yy) {
            x=xx;
            y=yy;
        }
        public int compareTo(struct r) {
            if(r.y>y) return 1;
            else if(r.y<y) return -1;
            else {
                return x.compareTo(r.x);
            }
        }
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String me = sc.nextLine();
        int actions = Integer.parseInt(sc.nextLine());
        HashMap<String, Integer> map = new HashMap<String, Integer>();
        HashSet<String> set=new HashSet<String>();
        //System.out.println(actions);
        for (int i = 0; i < actions; i++) {
            //System.out.println(sc.nextLine());
            String[] st = sc.nextLine().split(" ");
            //System.out.println(st[0]);
            String n1 = st[0];
            String n2 = st[st.length - 2].substring(0, st[st.length - 2]
                    .length() - 2);
            String ac = st[1];
            int add;
            if (n1.equals(me)) {
                if (!n2.equals(me)) {
                    if (ac.equals("posted")) {
                        add = 15;
                    } else if (ac.equals("commented")) {
                        add = 10;
                    } else
                        add = 5;
                    Integer tmp = map.get(n2);
                    if (tmp == null)
                        map.put(n2, add);
                    else
                        map.put(n2, tmp + add);
                    set.add(n2);
                }
            } else if (n2.equals(me)) {
                if (!n1.equals(me)) {
                    if (ac.equals("posted")) {
                        add = 15;
                    } else if (ac.equals("commented")) {
                        add = 10;
                    } else
                        add = 5;
                    Integer tmp = map.get(n1);
                    if (tmp == null)
                        map.put(n1, add);
                    else
                        map.put(n1, tmp + add);
                    set.add(n1);
                }
            } else {
                map.put(n1, 0);
                map.put(n2, 0);
                set.add(n1);
                set.add(n2);
            }
        }
        PriorityQueue<struct> pq=new PriorityQueue<struct>();
        for(String x:set) {
            pq.add(new struct(x,map.get(x)));
        }
        while(!pq.isEmpty()) {
            //System.out.println(pq.peek().y+" "+pq.peek().x);
            System.out.println(pq.remove().x);
        }
    }
}


import java.util.*;

public class Main {

    static class struct implements Comparable<struct>{
        String x;
        int y;
        public struct(String xx,int yy) {
            x=xx;
            y=yy;
        }
        public int compareTo(struct r) {
            if(r.y>y) return 1;
            else if(r.y<y) return -1;
            else {
                return x.compareTo(r.x);
            }
        }
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String me = sc.nextLine();
        int actions = Integer.parseInt(sc.nextLine());
        HashMap<String, Integer> map = new HashMap<String, Integer>();
        HashSet<String> set=new HashSet<String>();
        System.out.println(actions);
        for (int i = 0; i < actions; i++) {
            //System.out.println(sc.nextLine());
            String[] st = sc.nextLine().split(" ");
            //System.out.println(st[0]);
            String n1 = st[0];
            String n2 = st[st.length - 2].substring(0, st[st.length - 2]
                    .length() - 2);
            String ac = st[1];
            int add;
            if (n1.equals(me)) {
                if (!n2.equals(me)) {
                    if (ac.equals("posted")) {
                        add = 15;
                    } else if (ac.equals("commented")) {
                        add = 10;
                    } else
                        add = 5;
                    Integer tmp = map.get(n2);
                    if (tmp == null)
                        map.put(n2, add);
                    else
                        map.put(n2, tmp + add);
                    set.add(n2);
                }
            } else if (n2.equals(me)) {
                if (!n1.equals(me)) {
                    if (ac.equals("posted")) {
                        add = 15;
                    } else if (ac.equals("commented")) {
                        add = 10;
                    } else
                        add = 5;
                    Integer tmp = map.get(n1);
                    if (tmp == null)
                        map.put(n1, add);
                    else
                        map.put(n1, tmp + add);
                    set.add(n1);
                }
            } else {
                map.put(n1, 0);
                map.put(n2, 0);
                set.add(n1);
                set.add(n2);
            }
        }
        PriorityQueue<struct> pq=new PriorityQueue<struct>();
        for(String x:set) {
            pq.add(new struct(x,map.get(x)));
        }
        while(!pq.isEmpty()) {
            //System.out.println(pq.peek().y+" "+pq.peek().x);
            System.out.println(pq.remove().x);
        }
    }
}


import java.io.*;
import java.util.*;

public class FacetookPriorityWall {

    static class struct implements Comparable<struct> {
        String x;
        int y;

        public struct(String xx, int yy) {
            x = xx;
            y = yy;
        }

        public int compareTo(struct r) {
            if (r.y > y)
                return 1;
            else if (r.y < y)
                return -1;
            else {
                return x.compareTo(r.x);
            }
        }
    }

    public static void main(String[] args) throws NumberFormatException,
            IOException {
        BufferedReader sc = new BufferedReader(new InputStreamReader(System.in));
        String me = sc.readLine();
        int actions = Integer.parseInt(sc.readLine());
        HashMap<String, Integer> map = new HashMap<String, Integer>();
        HashSet<String> set = new HashSet<String>();
        // System.out.println(actions);
        for (int i = 0; i < actions; i++) {
            // System.out.println(sc.nextLine());
            String[] st = sc.readLine().split(" ");
            // System.out.println(st[0]);
            String n1 = st[0];
            String n2 = st[st.length - 2].substring(0, st[st.length - 2]
                    .length() - 2);
            String ac = st[1];
            int add;
            if (n1.equals(me)) {
                if (!n2.equals(me)) {
                    if (ac.equals("posted")) {
                        add = 15;
                    } else if (ac.equals("commented")) {
                        add = 10;
                    } else
                        add = 5;
                    Integer tmp = map.get(n2);
                    if (tmp == null)
                        map.put(n2, add);
                    else
                        map.put(n2, tmp + add);
                    set.add(n2);
                }
            } else if (n2.equals(me)) {
                if (!n1.equals(me)) {
                    if (ac.equals("posted")) {
                        add = 15;
                    } else if (ac.equals("commented")) {
                        add = 10;
                    } else
                        add = 5;
                    Integer tmp = map.get(n1);
                    if (tmp == null)
                        map.put(n1, add);
                    else
                        map.put(n1, tmp + add);
                    set.add(n1);
                }
            } else {
                Integer tmp = map.get(n1);
                if (tmp == null)
                    map.put(n1, 0);
                tmp = map.get(n2);
                if (tmp == null)
                    map.put(n2, 0);
                set.add(n1);
                set.add(n2);
            }
        }
        PriorityQueue<struct> pq = new PriorityQueue<struct>();
        for (String x : set) {
            pq.add(new struct(x, map.get(x)));
        }
        while (!pq.isEmpty()) {
            // System.out.println(pq.peek().y+" "+pq.peek().x);
            System.out.println(pq.remove().x);
        }
    }
}


import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

/**
 *
 * @author epiZend
 */
public class Facetook {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String myName = sc.next();
        int actions = sc.nextInt();
        Map<String, Integer> prio = new HashMap<String, Integer>();
        sc.nextLine();
        for (int i = 0; i < actions; i++) {
            String action = sc.nextLine();
            String nameA = action.substring(0, action.indexOf(' '));
            action = action.substring(nameA.length() + 1);
            String type = action.substring(0, action.indexOf(' '));
            action = action.substring(type.length() + 1);
            if (!type.equals("likes")) {
                action = action.substring(3);
            }
            String nameB = action.substring(0, action.indexOf('\''));
            if (nameA.equals(myName)) {
                prio.put(nameB, (prio.get(nameB) == null ? 0 : prio.get(nameB)) + points(type));
            } else if (nameB.equals(myName)) {
                prio.put(nameA, (prio.get(nameA) == null ? 0 : prio.get(nameA)) + points(type));
            } else {
                prio.put(nameB, (prio.get(nameB) == null ? 0 : prio.get(nameB)));
                prio.put(nameA, (prio.get(nameA) == null ? 0 : prio.get(nameA)));
            }
        }
        List<Friend> friends = new ArrayList<Friend>();
        for (Map.Entry<String, Integer> en : prio.entrySet()) {
            String key = en.getKey();
            Integer val = en.getValue();
            friends.add(new Friend(key, val));
        }
        Collections.sort(friends);
        for (Friend friend : friends) {
            System.out.println(friend.name);
        }
    }

    static class Friend implements Comparable<Friend> {

        String name;
        Integer val;

        Friend(String name, Integer val) {
            this.name = name;
            this.val = val;
        }

        @Override
        public int compareTo(Friend o) {
            int cmp = o.val.compareTo(val);
            if (cmp == 0) {
                return name.compareTo(o.name);
            }
            return cmp;
        }
    }

    private static Integer points(String type) {
        if (type.equals("likes")) {
            return 5;
        }
        if (type.equals("posted")) {
            return 15;
        }
        if (type.equals("commented")) {
            return 10;
        }
        throw new AssertionError("type " + type);
    }
}


import java.util.*;
public class Facetook {
	public static void main(String[] args){
		Scanner reader = new Scanner(System.in);

		ArrayList<Dude> f = new ArrayList<Dude>();
		HashMap<String, Integer> stoi = new HashMap<String, Integer>();
		f.add(new Dude(reader.next()));
		stoi.put(f.get(0).name, 0);

		int n = reader.nextInt();
		reader.nextLine();
		for(int i = 0; i < n; i++){
			String in = reader.nextLine();
			StringTokenizer st = new StringTokenizer(in);
			String a = st.nextToken();
			int s = getIndex(stoi, f, a);
			String c = st.nextToken();

			int p = 5;
			String b;
			if(c.equals("posted")){
				st.nextToken();
				p = 15;
			}else if(c.equals("commented")){
				st.nextToken();
				p = 10;
			}

			b = st.nextToken();
			b = b.substring(0, b.length()-2);
			int t = getIndex(stoi, f, b);

			f.get(s).r += p;
			f.get(t).r += p;
		}

		f.remove(0);
		Collections.sort(f);

		for(int i = 0; i < f.size(); i++)
			System.out.println(f.get(i).name);
	}

	public static int getIndex(HashMap<String, Integer> stoi, ArrayList<Dude> f, String n){
		if(stoi.get(n) == null){
			stoi.put(n, f.size());
			f.add(new Dude(n));
		}
		return stoi.get(n);
	}

	public static class Dude implements Comparable<Dude>{
		String name;
		int r;
		public Dude(String _name){
			name = _name;
		}
		public int compareTo(Dude d){
			if(r == d.r)
				return name.compareTo(d.name);
			return d.r-r;
		}
	}
}


import java.util.*;
public class Facetook {
	public static void main(String[] args){
		Scanner reader = new Scanner(System.in);

		ArrayList<Dude> f = new ArrayList<Dude>();
		HashMap<String, Integer> stoi = new HashMap<String, Integer>();
		f.add(new Dude(reader.next()));
		stoi.put(f.get(0).name, 0);

		int n = reader.nextInt();
		reader.nextLine();
		for(int i = 0; i < n; i++){
			String in = reader.nextLine();
			StringTokenizer st = new StringTokenizer(in);
			String a = st.nextToken();
			int s = getIndex(stoi, f, a);
			String c = st.nextToken();

			int p = 5;
			String b;
			if(c.equals("posted")){
				st.nextToken();
				p = 15;
			}else if(c.equals("commented")){
				st.nextToken();
				p = 10;
			}

			b = st.nextToken();
			b = b.substring(0, b.length()-2);
			int t = getIndex(stoi, f, b);

			if(s==0||t==0){
				f.get(s).r += p;
				f.get(t).r += p;
			}
		}

		f.remove(0);
		Collections.sort(f);

		for(int i = 0; i < f.size(); i++)
			System.out.println(f.get(i).name);
	}

	public static int getIndex(HashMap<String, Integer> stoi, ArrayList<Dude> f, String n){
		if(stoi.get(n) == null){
			stoi.put(n, f.size());
			f.add(new Dude(n));
		}
		return stoi.get(n);
	}

	public static class Dude implements Comparable<Dude>{
		String name;
		int r;
		public Dude(String _name){
			name = _name;
		}
		public int compareTo(Dude d){
			if(r == d.r)
				return name.compareTo(d.name);
			return d.r-r;
		}
	}
}


import static java.lang.Math.*;
import static java.util.Arrays.*;
import static java.util.Collections.*;
import java.io.*;
import java.math.*;
import java.util.*;
import java.util.Map.Entry;

public class CF67B {
    public static void main(String[] args) throws Exception {
        String me = next();
        int n = nextInt();
        int[] a = new int[100];
        HashMap<String, Integer> map = new HashMap<String, Integer>();
        int fs = 0;
        for (int i = 0; i < n; i++) {
            String s = nextLine();
            String x = first(s);
            String y = second(s);

            if (x.equals(me) || y.equals(me)) {
                String him = x.equals(me) ? y : x;
                if (map.get(him) == null) {
                    map.put(him, fs++);
                }
                String[] ss = s.split(" ");
                if (type15(ss))
                    a[map.get(him)] += 15;
                else if (type10(ss))
                    a[map.get(him)] += 10;
                else
                    a[map.get(him)] += 5;
            } else {
                if (map.get(x) == null) {
                    map.put(x, fs++);
                }
                if (map.get(y) == null) {
                    map.put(y, fs++);
                }
            }
        }
        ArrayList<Friend> all = new ArrayList<Friend>();
        for (Entry<String, Integer> e : map.entrySet()) {
            all.add(new Friend(e.getKey(), e.getValue()));
        }
        sort(all);
        for (Friend f : all) {
            System.out.println(f.name);
        }
    }

    private static boolean type15(String[] s) {
        return s[1].equals("posted");
    }

    private static boolean type10(String[] s) {
        return s[1].equals("commented");
    }

    private static String first(String s) {
        return s.substring(0, s.indexOf(' '));
    }

    private static String second(String s) {
        String r = "";
        for (int i = s.indexOf("'") - 1; i >= 0 && s.charAt(i) != ' '; i--) {
            r = s.charAt(i) + r;
        }
        return r;
    }

    static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    static StringTokenizer st = new StringTokenizer("");

    static String nextLine() throws Exception {
        return in.readLine();
    }

    static String next() throws Exception {
        while (!st.hasMoreTokens()) {
            String s = in.readLine();
            if (s == null)
                return null;
            st = new StringTokenizer(s);
        }
        return st.nextToken();
    }

    static int nextInt() throws Exception {
        return Integer.parseInt(next());
    }

    static long nextLong() throws Exception {
        return Long.parseLong(next());
    }

    static double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
}

class Friend implements Comparable<Friend> {
    String name;
    int score;

    public Friend(String name, int score) {
        super();
        this.name = name;
        this.score = score;
    }

    public int compareTo(Friend f) {
        if (score == f.score)
            return name.compareTo(f.name);
        return score - f.score;
    }
}


import static java.lang.Math.*;
import static java.util.Arrays.*;
import static java.util.Collections.*;
import java.io.*;
import java.math.*;
import java.util.*;
import java.util.Map.Entry;

public class CF67B {
    public static void main(String[] args) throws Exception {
        String me = next();
        int n = nextInt();
        int[] a = new int[100];
        HashMap<String, Integer> map = new HashMap<String, Integer>();
        int fs = 0;
        for (int i = 0; i < n; i++) {
            String s = nextLine();
            String x = first(s);
            String y = second(s);

            if (x.equals(me) || y.equals(me)) {
                String him = x.equals(me) ? y : x;
                if (map.get(him) == null) {
                    map.put(him, fs++);
                }
                String[] ss = s.split(" ");
                if (type15(ss))
                    a[map.get(him)] += 15;
                else if (type10(ss))
                    a[map.get(him)] += 10;
                else
                    a[map.get(him)] += 5;
            } else {
                if (map.get(x) == null) {
                    map.put(x, fs++);
                }
                if (map.get(y) == null) {
                    map.put(y, fs++);
                }
            }
        }
        ArrayList<Friend> all = new ArrayList<Friend>();
        for (Entry<String, Integer> e : map.entrySet()) {
            all.add(new Friend(e.getKey(), a[e.getValue()]));
        }
        sort(all);
        for (Friend f : all) {
            System.out.println(f.name);
        }
    }

    private static boolean type15(String[] s) {
        return s[1].equals("posted");
    }

    private static boolean type10(String[] s) {
        return s[1].equals("commented");
    }

    private static String first(String s) {
        return s.substring(0, s.indexOf(' '));
    }

    private static String second(String s) {
        String r = "";
        for (int i = s.indexOf("'") - 1; i >= 0 && s.charAt(i) != ' '; i--) {
            r = s.charAt(i) + r;
        }
        return r;
    }

    static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    static StringTokenizer st = new StringTokenizer("");

    static String nextLine() throws Exception {
        return in.readLine();
    }

    static String next() throws Exception {
        while (!st.hasMoreTokens()) {
            String s = in.readLine();
            if (s == null)
                return null;
            st = new StringTokenizer(s);
        }
        return st.nextToken();
    }

    static int nextInt() throws Exception {
        return Integer.parseInt(next());
    }

    static long nextLong() throws Exception {
        return Long.parseLong(next());
    }

    static double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
}

class Friend implements Comparable<Friend> {
    String name;
    int score;

    public Friend(String name, int score) {
        super();
        this.name = name;
        this.score = score;
    }

    public int compareTo(Friend f) {
        if (score == f.score)
            return name.compareTo(f.name);
        return - score + f.score;
    }

    public String toString() {
        return "Friend [name=" + name + ", score=" + score + "]";
    }

}


import static java.lang.Math.*;
import static java.util.Arrays.*;
import static java.util.Collections.*;
import java.io.*;
import java.math.*;
import java.util.*;
import java.util.Map.Entry;

public class CF67B {
    public static void main(String[] args) throws Exception {
        String me = next();
        int n = nextInt();
        int[] a = new int[100];
        HashMap<String, Integer> map = new HashMap<String, Integer>();
        int fs = 0;
        for (int i = 0; i < n; i++) {
            String s = nextLine();
            String x = first(s);
            String y = second(s);

            if (s.contains(me)) {
                String him = x.equals(me) ? y : x;
                if (map.get(him) == null) {
                    map.put(him, fs++);
                }
                if (s.contains("posted"))
                    a[map.get(him)] += 15;
                else if (s.contains("commented"))
                    a[map.get(him)] += 10;
                else
                    a[map.get(him)] += 5;
            } else {
                if (map.get(x) == null) {
                    map.put(x, fs++);
                }
                if (map.get(y) == null) {
                    map.put(y, fs++);
                }
            }
        }
        ArrayList<Friend> all = new ArrayList<Friend>();
        for (Entry<String, Integer> e : map.entrySet()) {
            all.add(new Friend(e.getKey(), e.getValue()));
        }
        sort(all);
        for (Friend f : all) {
            System.out.println(f.name);
        }
    }

    private static String first(String s) {
        return s.substring(0, s.indexOf(' '));
    }

    private static String second(String s) {
        String r = "";
        for (int i = s.indexOf("'") - 1; i >= 0 && s.charAt(i) != ' '; i--) {
            r = s.charAt(i) + r;
        }
        return r;
    }

    static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    static StringTokenizer st = new StringTokenizer("");

    static String nextLine() throws Exception {
        return in.readLine();
    }

    static String next() throws Exception {
        while (!st.hasMoreTokens()) {
            String s = in.readLine();
            if (s == null)
                return null;
            st = new StringTokenizer(s);
        }
        return st.nextToken();
    }

    static int nextInt() throws Exception {
        return Integer.parseInt(next());
    }

    static long nextLong() throws Exception {
        return Long.parseLong(next());
    }

    static double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
}

class Friend implements Comparable<Friend> {
    String name;
    int score;

    public Friend(String name, int score) {
        super();
        this.name = name;
        this.score = score;
    }

    public int compareTo(Friend f) {
        if (score == f.score)
            return name.compareTo(f.name);
        return score - f.score;
    }
}


import static java.lang.Math.*;
import static java.util.Arrays.*;
import static java.util.Collections.*;
import java.io.*;
import java.math.*;
import java.util.*;
import java.util.Map.Entry;

public class CF67B {
    public static void main(String[] args) throws Exception {
        String me = next();
        int n = nextInt();
        int[] a = new int[100];
        HashMap<String, Integer> map = new HashMap<String, Integer>();
        int fs = 0;
        for (int i = 0; i < n; i++) {
            String s = nextLine();
            String x = first(s);
            String y = second(s);

            if (s.contains(me)) {
                String him = x.equals(me) ? y : x;
                if (map.get(him) == null) {
                    map.put(him, fs++);
                }
                String[] ss = s.split(" ");
                if (type15(ss))
                    a[map.get(him)] += 15;
                else if (type10(ss))
                    a[map.get(him)] += 10;
                else
                    a[map.get(him)] += 5;
            } else {
                if (map.get(x) == null) {
                    map.put(x, fs++);
                }
                if (map.get(y) == null) {
                    map.put(y, fs++);
                }
            }
        }
        ArrayList<Friend> all = new ArrayList<Friend>();
        for (Entry<String, Integer> e : map.entrySet()) {
            all.add(new Friend(e.getKey(), e.getValue()));
        }
        sort(all);
        for (Friend f : all) {
            System.out.println(f.name);
        }
    }

    private static boolean type15(String[] s) {
        return s[1].equals("posted");
    }

    private static boolean type10(String[] s) {
        return s[1].equals("commented");
    }

    private static String first(String s) {
        return s.substring(0, s.indexOf(' '));
    }

    private static String second(String s) {
        String r = "";
        for (int i = s.indexOf("'") - 1; i >= 0 && s.charAt(i) != ' '; i--) {
            r = s.charAt(i) + r;
        }
        return r;
    }

    static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    static StringTokenizer st = new StringTokenizer("");

    static String nextLine() throws Exception {
        return in.readLine();
    }

    static String next() throws Exception {
        while (!st.hasMoreTokens()) {
            String s = in.readLine();
            if (s == null)
                return null;
            st = new StringTokenizer(s);
        }
        return st.nextToken();
    }

    static int nextInt() throws Exception {
        return Integer.parseInt(next());
    }

    static long nextLong() throws Exception {
        return Long.parseLong(next());
    }

    static double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
}

class Friend implements Comparable<Friend> {
    String name;
    int score;

    public Friend(String name, int score) {
        super();
        this.name = name;
        this.score = score;
    }

    public int compareTo(Friend f) {
        if (score == f.score)
            return name.compareTo(f.name);
        return score - f.score;
    }
}


import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Scanner;
import java.util.Set;
import java.util.StringTokenizer;

public class B67 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        HashMap<String, Integer> user = new HashMap<String, Integer>();

        String me = sc.nextLine();
        int n = Integer.parseInt(sc.nextLine());

        for(int i = 0 ; i < n ; ++i) {
            String s = sc.nextLine();
            String[] tok = s.split(" ");

            if(tok[1].equals("likes")) {
                tok[2] = tok[2].substring(0 , tok[2].length() - 2);
                if(!tok[0].equals(me) && !tok[2].equals(me)) {
                    if(user.get(tok[0]) == null) user.put(tok[0] , 0);
                    if(user.get(tok[2]) == null) user.put(tok[2] , 0);
                    continue;
                }

                String other = null;

                if(!tok[0].equals(me)) other = tok[0];
                else other = tok[2];

                Integer pt = user.get(other);
                if(pt != null) {
                    user.put(other , pt + 5);
                } else {
                    user.put(other , 5);
                }

            } else if(tok[1].equals("posted")) {
                tok[3] = tok[3].substring(0 , tok[3].length() - 2);
                if(!tok[0].equals(me) && !tok[3].equals(me)) {
                    if(user.get(tok[0]) == null) user.put(tok[0] , 0);
                    if(user.get(tok[3]) == null) user.put(tok[3] , 0);
                    continue;
                }

                String other = null;

                if(!tok[0].equals(me)) other = tok[0];
                else other = tok[3];

                Integer pt = user.get(other);
                if(pt != null) {
                    user.put(other , pt + 15);
                } else {
                    user.put(other , 15);
                }
            } else {
                tok[3] = tok[3].substring(0 , tok[3].length() - 2);
                if(!tok[0].equals(me) && !tok[3].equals(me)) {
                    if(user.get(tok[0]) == null) user.put(tok[0] , 0);
                    if(user.get(tok[3]) == null) user.put(tok[3] , 0);
                    continue;
                }

                String other = null;

                if(!tok[0].equals(me)) other = tok[0];
                else other = tok[3];

                Integer pt = user.get(other);
                if(pt != null) {
                    user.put(other , pt + 10);
                } else {
                    user.put(other , 10);
                }
            }

        } // for i

        Set<String> names = user.keySet();

        HashMap<Integer, ArrayList<String>> points = new HashMap<Integer, ArrayList<String>>();

        for(String name : names) {
            Integer pt = user.get(name);
            ArrayList<String> u = points.get(pt);
            if(u == null) {
                ArrayList<String> aa = new ArrayList<String>();
                aa.add(name);
                points.put(pt , aa);
            } else {
                u.add(name);
                points.put(pt , u);
            }
        }

        Set<Integer> p1 = points.keySet();
        ArrayList<Integer> p = new ArrayList<Integer>();

        for(Integer i : p1) {
            p.add(i);
        }

        Collections.sort(p);
        Collections.reverse(p);

        for(Integer i : p) {
            ArrayList<String> list = points.get(i);
            Collections.sort(list);
            for(String nam : list) {
                System.out.println(nam);
            }
        }
    }

}


import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Scanner;
import java.util.Set;
import java.util.StringTokenizer;

public class B67 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        HashMap<String, Integer> user = new HashMap<String, Integer>();

        String me = sc.nextLine();
        int n = Integer.parseInt(sc.nextLine());

        for(int i = 0 ; i < n ; ++i) {
            String s = sc.nextLine();
            String[] tok = s.split(" ");

            if(tok[1].equals("likes")) {
                tok[2] = tok[2].substring(0,tok[2].length()-2);
                if(!tok[0].equals(me) && !tok[2].equals(me)) {
                    if(user.get(tok[0]) == null) user.put(tok[0] , 0);
                    if(user.get(tok[2]) == null) user.put(tok[2] , 0);
                }

                String other = null;

                if(!tok[0].equals(me)) other = tok[0];
                else other = tok[2];

                Integer pt = user.get(other);
                if(pt != null) {
                    user.put(other , pt + 5);
                } else {
                    user.put(other , 5);
                }

            } else if(tok[1].equals("posted")) {
                tok[3] = tok[3].substring(0,tok[3].length()-2);
                if(!tok[0].equals(me) && !tok[3].equals(me)) {
                    if(user.get(tok[0]) == null) user.put(tok[0] , 0);
                    if(user.get(tok[3]) == null) user.put(tok[3] , 0);
                }

                String other = null;

                if(!tok[0].equals(me)) other = tok[0];
                else other = tok[3];

                Integer pt = user.get(other);
                if(pt != null) {
                    user.put(other , pt + 15);
                } else {
                    user.put(other , 15);
                }
            } else {
                tok[3] = tok[3].substring(0,tok[3].length()-2);
                if(!tok[0].equals(me) && !tok[3].equals(me)) {
                    if(user.get(tok[0]) == null) user.put(tok[0] , 0);
                    if(user.get(tok[3]) == null) user.put(tok[3] , 0);
                }

                String other = null;

                if(!tok[0].equals(me)) other = tok[0];
                else other = tok[3];

                Integer pt = user.get(other);
                if(pt != null) {
                    user.put(other , pt + 10);
                } else {
                    user.put(other , 10);
                }
            }

        } // for i

        Set<String> names = user.keySet();

        HashMap<Integer,ArrayList<String>> points = new HashMap<Integer, ArrayList<String>>();

        for(String name : names) {
            Integer pt = user.get(name);
            ArrayList<String> u = points.get(pt);
            if(u == null){
                ArrayList<String> aa = new ArrayList<String>();
                aa.add(name);
                points.put(pt , aa);
            } else {
                u.add(name);
                points.put(pt , u);
            }
        }

        Set<Integer> p1 = points.keySet();
        ArrayList<Integer> p = new ArrayList<Integer>();

        for(Integer i : p1){
            p.add(i);
        }

        Collections.sort(p);
        Collections.reverse(p);

        for(Integer i : p) {
            ArrayList<String> list = points.get(i);
            Collections.sort(list);
            for(String nam : list) {
                System.out.println(nam);
            }
        }
    }

}


//package round67;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.util.TreeMap;

public class B {

	static BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
	static StringTokenizer st;
	static PrintWriter out = new PrintWriter(System.out);

	static String nextToken() throws IOException{
		while (st==null || !st.hasMoreTokens()){
			String s = bf.readLine();
			if (s == null)
				return null;
			st = new StringTokenizer(s);
		}

		return st.nextToken();
	}

	static int nextInt() throws IOException{
		return Integer.parseInt(nextToken());
	}

	static long nextLong() throws IOException{
		return Long.parseLong(nextToken());
	}

	static String nextStr() throws IOException{
		return nextToken();
	}

	static double nextDouble() throws IOException{
		return Double.parseDouble(nextToken());
	}

	public static void main(String[] args) throws IOException{
		String me = nextStr();
		int n = nextInt();

		TreeMap<String, Integer> map = new TreeMap<String, Integer>();

		for (int i=0; i<n; i++){
			String name1 = nextStr();
			String _do = nextStr();

			if (_do.compareTo("likes") != 0)
				nextStr();

			String name2 = nextStr();
			name2 = name2.substring(0, name2.length()-2);
			nextStr();

			if (name1.compareTo(me)!=0 && map.get(name1)==null)
				map.put(name1, 0);
			if (name2.compareTo(me)!=0 && map.get(name2)==null)
				map.put(name2, 0);

			if (name1.compareTo(me)==0 && name2.compareTo(me)!=0){
				int score = 0;
				if (_do.compareTo("likes") == 0)
					score = 5;
				else if (_do.compareTo("posted") == 0)
					score = 15;
				else if (_do.compareTo("commented") == 0)
					score = 10;

				map.put(name2, score+map.get(name2));
			}

			if (name2.compareTo(me)==0 && name1.compareTo(me)!=0){
				int score = 0;
				if (_do.compareTo("likes") == 0)
					score = 5;
				else if (_do.compareTo("posted") == 0)
					score = 15;
				else if (_do.compareTo("commented") == 0)
					score = 10;

				map.put(name1, score+map.get(name1));
			}
		}

		String a[] = new String[100];
		int b[] = new int[100];
		int k = 0;
		for (String s: map.keySet()){
			a[k] = s;
			b[k] = map.get(s);
			k++;
		}

		for (int i=0; i<k; i++)
			for (int j=i+1; j<k; j++)
				if (b[i]<b[j] || b[i]==b[j] && a[i].compareTo(a[j])>0){
					String s = a[i]; a[i] = a[j]; a[j] = s;
					int p = b[i]; b[i] = b[j]; b[j] = p;
				}

		for (int i=0; i<k; i++)
			out.println(a[i]);
		out.flush();
	}

}


import java.io.*;
import java.util.*;

public class Main {
    static String my;
    static String[] name=new String[200];
    static int[] fr=new int[200];
    static int names=1,h;

    static void add(String s,int x) {
        for (int i=1;i<names;i++)
            if (name[i].equals(s)) {fr[i]+=x;return;}
        name[names]=s;
        fr[names]=x;
        names++;
    }

    static void heapify(int i) {
        int g;
        String s;

      do {
       if (2*i+1<=h)
        if ((fr[2*i+1]<fr[i]|(name[2*i+1].compareTo(name[i])>0&fr[i]==fr[2*i+1]))&fr[2*i+1]<fr[2*i]) {
         g=fr[i]; fr[i]=fr[2*i+1]; fr[2*i+1]=g;
         s=name[i]; name[i]=name[2*i+1]; name[2*i+1]=s;
         i=2*i+1;
         continue;
        }

       if (2*i<=h)
        if (fr[2*i]<fr[i]|(name[2*i].compareTo(name[i])>0&fr[i]==fr[2*i])) {
         g=fr[i]; fr[i]=fr[2*i]; fr[2*i]=g;
         s=name[i]; name[i]=name[2*i]; name[2*i]=s;
         i=2*i;
         continue;
        }

       break;
      }  while (1==1);

    }

    public static void main(String[] args) throws FileNotFoundException {
      Scanner in=new Scanner(new FileReader("input.txt"));
      my=in.nextLine();
      int n=in.nextInt();
      int j,j1,j2,x;
      String s=in.nextLine();
      String s1="",s2="",s3="";
      for (int i=1;i<=n;i++) {
          s=in.nextLine();
          s1="";s2="";s3="";

           for (j=1;j<=s.length();j++)
               if (s.charAt(j-1)!=' ') s1+=s.charAt(j-1); else break;

           for (j1=j+1;j1<=s.length();j1++)
               if (s.charAt(j1-1)!=' ') s2+=s.charAt(j1-1); else break;

           if (!s2.equals("likes")) j1+=3;

           for (j2=j1+1;j2<=s.length();j2++)
               if (s.charAt(j2-1)!=39) s3+=s.charAt(j2-1); else break;//считали строки

           if (!s1.equals(my)&!s3.equals(my)) {add(s1,0);add(s3,0);continue;}

           if (s2.equals("posted")) x=15; else if (s2.equals("commented")) x=10; else x=5;

          if (s1.equals(my)) add(s3,x); else add(s1,x);

      }//считали

      h=names-1;
      for (int i=1;i<=h;i++) heapify(i);

      do {
       int g;
       String r;
        g=fr[1]; fr[1]=fr[h]; fr[h]=g;
        r=name[1]; name[1]=name[h]; name[h]=r;
        h--;
        heapify(1);
       } while (h>=1);

      for (int i=1;i<=names-1;i++)
          System.out.println(name[i]);

    }

}


import java.io.*;
import java.util.*;

public class Main {
    static String my;
    static String[] name=new String[200];
    static int[] fr=new int[200];
    static int names=1,h;

    static void add(String s,int x) {
        for (int i=1;i<names;i++)
            if (name[i].equals(s)) {fr[i]+=x;return;}
        name[names]=s;
        fr[names]=x;
        names++;
    }

    static void heapify(int i) {
        int g;
        String s;

      do {
       if (2*i+1<=h)
        if ((fr[2*i+1]<fr[i])&fr[2*i+1]<fr[2*i]) {
         g=fr[i]; fr[i]=fr[2*i+1]; fr[2*i+1]=g;
         s=name[i]; name[i]=name[2*i+1]; name[2*i+1]=s;
         i=2*i+1;
         continue;
        }

       if (2*i<=h)
        if (fr[2*i]<fr[i]) {
         g=fr[i]; fr[i]=fr[2*i]; fr[2*i]=g;
         s=name[i]; name[i]=name[2*i]; name[2*i]=s;
         i=2*i;
         continue;
        }

       break;
      }  while (1==1);

    }

    static boolean ok() {
        for (int i=1;i<names-1;i++)
            if (fr[i]==fr[i+1]&name[i].compareTo(name[i+1])>=0) return false;
        return true;
    }

    public static void main(String[] args) throws FileNotFoundException {
      Scanner in=new Scanner(System.in);
      my=in.nextLine();
      int n=in.nextInt();
      int j,j1,j2,x;
      String s=in.nextLine();
      String s1="",s2="",s3="";
      for (int i=1;i<=n;i++) {
          s=in.nextLine();
          s1="";s2="";s3="";

           for (j=1;j<=s.length();j++)
               if (s.charAt(j-1)!=' ') s1+=s.charAt(j-1); else break;

           for (j1=j+1;j1<=s.length();j1++)
               if (s.charAt(j1-1)!=' ') s2+=s.charAt(j1-1); else break;

           if (!s2.equals("likes")) j1+=3;

           for (j2=j1+1;j2<=s.length();j2++)
               if (s.charAt(j2-1)!=39) s3+=s.charAt(j2-1); else break;//считали строки

           if (!s1.equals(my)&!s3.equals(my)) {add(s1,0);add(s3,0);continue;}

           if (s2.equals("posted")) x=15; else if (s2.equals("commented")) x=10; else x=5;

          if (s1.equals(my)) add(s3,x); else add(s1,x);

      }//считали

      h=names-1;
      if (my.equals("ou")) for (int i=1;i<=names-1;i++) System.out.println("0 "+name[i]+" "+fr[i]);
      for (int i=1;i<=h;i++) heapify(i);
      if (my.equals("ou")) for (int i=1;i<=names-1;i++) System.out.println("1 "+name[i]+" "+fr[i]);
      do {
       int g;
       String r;
        g=fr[1]; fr[1]=fr[h]; fr[h]=g;
        r=name[1]; name[1]=name[h]; name[h]=r;
        h--;
        heapify(1);
       } while (h>1);

      do {
          String r;
      for (int i=1;i<names-1;i++)
          if (fr[i]==fr[i+1]&name[i].compareTo(name[i+1])>0) {r=name[i]; name[i]=name[i+1]; name[i+1]=r;}
      } while (!ok());

      if (my.equals("ou")) for (int i=1;i<=names-1;i++) System.out.println(name[i]+" "+fr[i]); else
      for (int i=1;i<=names-1;i++)
          System.out.println(name[i]);

    }

}


import java.io.*;
import java.util.*;

public class Main {
    static String my;
    static String[] name=new String[200];
    static int[] fr=new int[200];
    static int names=1,h;

    static void add(String s,int x) {
        for (int i=1;i<names;i++)
            if (name[i].equals(s)) {fr[i]+=x;return;}
        name[names]=s;
        fr[names]=x;
        names++;
    }

    static void heapify(int i) {
        int g;
        String s;

      do {
       if (2*i+1<=h)
        if ((fr[2*i+1]<fr[i])&fr[2*i+1]<fr[2*i]) {
         g=fr[i]; fr[i]=fr[2*i+1]; fr[2*i+1]=g;
         s=name[i]; name[i]=name[2*i+1]; name[2*i+1]=s;
         i=2*i+1;
         continue;
        }

       if (2*i<=h)
        if (fr[2*i]<fr[i]) {
         g=fr[i]; fr[i]=fr[2*i]; fr[2*i]=g;
         s=name[i]; name[i]=name[2*i]; name[2*i]=s;
         i=2*i;
         continue;
        }

       break;
      }  while (1==1);

    }

    static boolean ok() {
        for (int i=1;i<names-1;i++)
            if (fr[i]==fr[i+1]&name[i].compareTo(name[i+1])>=0) return false;
        return true;
    }

    public static void main(String[] args) throws FileNotFoundException {
      Scanner in=new Scanner(System.in);
      my=in.nextLine();
      int n=in.nextInt();
      int j,j1,j2,x;
      String s=in.nextLine();
      String s1="",s2="",s3="";
      for (int i=1;i<=n;i++) {
          s=in.nextLine();
          s1="";s2="";s3="";

           for (j=1;j<=s.length();j++)
               if (s.charAt(j-1)!=' ') s1+=s.charAt(j-1); else break;

           for (j1=j+1;j1<=s.length();j1++)
               if (s.charAt(j1-1)!=' ') s2+=s.charAt(j1-1); else break;

           if (!s2.equals("likes")) j1+=3;

           for (j2=j1+1;j2<=s.length();j2++)
               if (s.charAt(j2-1)!=39) s3+=s.charAt(j2-1); else break;//считали строки

           if (!s1.equals(my)&!s3.equals(my)) {add(s1,0);add(s3,0);continue;}

           if (s2.equals("posted")) x=15; else if (s2.equals("commented")) x=10; else x=5;

          if (s1.equals(my)) add(s3,x); else add(s1,x);

      }//считали

      if (!my.equals("ou")) h=names-1; else {h=names-1;System.out.println("h="+h);};
      for (int i=1;i<=h;i++) heapify(i);

      do {
       int g;
       String r;
        g=fr[1]; fr[1]=fr[h]; fr[h]=g;
        r=name[1]; name[1]=name[h]; name[h]=r;
        h--;
        heapify(1);
       } while (h>1);

      do {
          String r;
      for (int i=1;i<names-1;i++)
          if (fr[i]==fr[i+1]&name[i].compareTo(name[i+1])>0) {r=name[i]; name[i]=name[i+1]; name[i+1]=r;}
      } while (!ok());

      if (my.equals("ou")) for (int i=1;i<=names-1;i++) System.out.println(name[i]+" "+fr[i]); else
      for (int i=1;i<=names-1;i++)
          System.out.println(name[i]);

    }

}


import java.io.*;
import java.util.*;

public class Main {
    static String my;
    static String[] name=new String[200];
    static int[] fr=new int[200];
    static int names=1,h;

    static void add(String s,int x) {
        for (int i=1;i<names;i++)
            if (name[i].equals(s)) {fr[i]+=x;return;}
        name[names]=s;
        fr[names]=x;
        names++;
    }

    static void heapify(int i) {
        int g;
        String s;

      do {
       if (2*i+1<=h)
        if ((fr[2*i+1]<fr[i])&fr[2*i+1]<fr[2*i]) {
         g=fr[i]; fr[i]=fr[2*i+1]; fr[2*i+1]=g;
         s=name[i]; name[i]=name[2*i+1]; name[2*i+1]=s;
         i=2*i+1;
         continue;
        }

       if (2*i<=h)
        if (fr[2*i]<fr[i]) {
         g=fr[i]; fr[i]=fr[2*i]; fr[2*i]=g;
         s=name[i]; name[i]=name[2*i]; name[2*i]=s;
         i=2*i;
         continue;
        }

       break;
      }  while (1==1);

    }

    static boolean ok() {
        for (int i=1;i<names-1;i++)
            if (fr[i]==fr[i+1]&name[i].compareTo(name[i+1])>=0) return false;
        return true;
    }

    public static void main(String[] args) throws FileNotFoundException {
      Scanner in=new Scanner(System.in);
      my=in.nextLine();
      int n=in.nextInt();
      int j,j1,j2,x;
      String s=in.nextLine();
      String s1="",s2="",s3="";
      for (int i=1;i<=n;i++) {
          s=in.nextLine();
          s1="";s2="";s3="";

           for (j=1;j<=s.length();j++)
               if (s.charAt(j-1)!=' ') s1+=s.charAt(j-1); else break;

           for (j1=j+1;j1<=s.length();j1++)
               if (s.charAt(j1-1)!=' ') s2+=s.charAt(j1-1); else break;

           if (!s2.equals("likes")) j1+=3;

           for (j2=j1+1;j2<=s.length();j2++)
               if (s.charAt(j2-1)!=39) s3+=s.charAt(j2-1); else break;//считали строки

           if (!s1.equals(my)&!s3.equals(my)) {add(s1,0);add(s3,0);continue;}

           if (s2.equals("posted")) x=15; else if (s2.equals("commented")) x=10; else x=5;

          if (s1.equals(my)) add(s3,x); else add(s1,x);

      }//считали

      h=names-1;
      for (int i=1;i<=h;i++) heapify(i);

      do {
       int g;
       String r;
        g=fr[1]; fr[1]=fr[h]; fr[h]=g;
        r=name[1]; name[1]=name[h]; name[h]=r;
        h--;
        heapify(1);
       } while (h>=1);

      do {
          String r;
      for (int i=1;i<names-1;i++)
          if (fr[i]==fr[i+1]&name[i].compareTo(name[i+1])>0) {r=name[i]; name[i]=name[i+1]; name[i+1]=r;}
      } while (!ok());

      if (my.equals("ou")) for (int i=1;i<=names-1;i++) System.out.println(name[i]+" "+fr[i]); else
      for (int i=1;i<=names-1;i++)
          System.out.println(name[i]);

    }

}


import java.io.*;
import java.util.*;

public class Main {
    static String my;
    static String[] name=new String[200];
    static int[] fr=new int[200];
    static int names=1,h;

    static void add(String s,int x) {
        for (int i=1;i<names;i++)
            if (name[i].equals(s)) {fr[i]+=x;return;}
        name[names]=s;
        fr[names]=x;
        names++;
    }

    static void heapify(int i) {
        int g;
        String s;

      do {
       if (2*i+1<=h)
        if ((fr[2*i+1]<fr[i]|(name[2*i+1].compareTo(name[i])>0&fr[i]==fr[2*i+1]))&fr[2*i+1]<fr[2*i]) {
         g=fr[i]; fr[i]=fr[2*i+1]; fr[2*i+1]=g;
         s=name[i]; name[i]=name[2*i+1]; name[2*i+1]=s;
         i=2*i+1;
         continue;
        }

       if (2*i<=h)
        if (fr[2*i]<fr[i]|(name[2*i].compareTo(name[i])>0&fr[i]==fr[2*i])) {
         g=fr[i]; fr[i]=fr[2*i]; fr[2*i]=g;
         s=name[i]; name[i]=name[2*i]; name[2*i]=s;
         i=2*i;
         continue;
        }

       break;
      }  while (1==1);

    }

    public static void main(String[] args) throws FileNotFoundException {
      Scanner in=new Scanner(System.in);
      my=in.nextLine();
      int n=in.nextInt();
      int j,j1,j2,x;
      String s=in.nextLine();
      String s1="",s2="",s3="";
      for (int i=1;i<=n;i++) {
          s=in.nextLine();
          s1="";s2="";s3="";

           for (j=1;j<=s.length();j++)
               if (s.charAt(j-1)!=' ') s1+=s.charAt(j-1); else break;

           for (j1=j+1;j1<=s.length();j1++)
               if (s.charAt(j1-1)!=' ') s2+=s.charAt(j1-1); else break;

           if (!s2.equals("likes")) j1+=3;

           for (j2=j1+1;j2<=s.length();j2++)
               if (s.charAt(j2-1)!=39) s3+=s.charAt(j2-1); else break;//считали строки

           if (!s1.equals(my)&!s3.equals(my)) {add(s1,0);add(s3,0);continue;}

           if (s2.equals("posted")) x=15; else if (s2.equals("commented")) x=10; else x=5;

          if (s1.equals(my)) add(s3,x); else add(s1,x);

      }//считали

      h=names-1;
      for (int i=1;i<=h;i++) heapify(i);

      do {
       int g;
       String r;
        g=fr[1]; fr[1]=fr[h]; fr[h]=g;
        r=name[1]; name[1]=name[h]; name[h]=r;
        h--;
        heapify(1);
       } while (h>=1);

      for (int i=1;i<=names-1;i++)
          System.out.println(name[i]);

    }

}


import java.io.*;
import java.util.*;

public class Main {
    static String my;
    static String[] name=new String[200];
    static int[] fr=new int[200];
    static int names=1,h;

    static void add(String s,int x) {
        for (int i=1;i<names;i++)
            if (name[i].equals(s)) {fr[i]+=x;return;}
        name[names]=s;
        fr[names]=x;
        names++;
    }

    static void heapify(int i) {
        int g;
        String s;

      do {
       if (2*i+1<=h)
        if ((fr[2*i+1]<fr[i])&fr[2*i+1]<fr[2*i]) {
         g=fr[i]; fr[i]=fr[2*i+1]; fr[2*i+1]=g;
         s=name[i]; name[i]=name[2*i+1]; name[2*i+1]=s;
         i=2*i+1;
         continue;
        }

       if (2*i<=h)
        if (fr[2*i]<fr[i]) {
         g=fr[i]; fr[i]=fr[2*i]; fr[2*i]=g;
         s=name[i]; name[i]=name[2*i]; name[2*i]=s;
         i=2*i;
         continue;
        }

       break;
      }  while (1==1);

    }

    static boolean ok() {
        for (int i=1;i<names-1;i++)
            if (fr[i]==fr[i+1]&name[i].compareTo(name[i+1])>0) return false;
        return true;
    }

    public static void main(String[] args) throws FileNotFoundException {
      Scanner in=new Scanner(System.in);
      my=in.nextLine();
      int n=in.nextInt();
      int j,j1,j2,x;
      String s=in.nextLine();
      String s1="",s2="",s3="";
      for (int i=1;i<=n;i++) {
          s=in.nextLine();
          s1="";s2="";s3="";

           for (j=1;j<=s.length();j++)
               if (s.charAt(j-1)!=' ') s1+=s.charAt(j-1); else break;

           for (j1=j+1;j1<=s.length();j1++)
               if (s.charAt(j1-1)!=' ') s2+=s.charAt(j1-1); else break;

           if (!s2.equals("likes")) j1+=3;

           for (j2=j1+1;j2<=s.length();j2++)
               if (s.charAt(j2-1)!=39) s3+=s.charAt(j2-1); else break;//считали строки

           if (!s1.equals(my)&!s3.equals(my)) {add(s1,0);add(s3,0);continue;}

           if (s2.equals("posted")) x=15; else if (s2.equals("commented")) x=10; else x=5;

          if (s1.equals(my)) add(s3,x); else add(s1,x);

      }//считали

      h=names-1;
      for (int i=1;i<=h;i++) heapify(i);

      do {
       int g;
       String r;
        g=fr[1]; fr[1]=fr[h]; fr[h]=g;
        r=name[1]; name[1]=name[h]; name[h]=r;
        h--;
        heapify(1);
       } while (h>=1);

      do {
          String r;
      for (int i=1;i<names-1;i++)
          if (fr[i]==fr[i+1]&name[i].compareTo(name[i+1])>0) {r=name[i]; name[i]=name[i+1]; name[i+1]=r;}
      } while (!ok());

      for (int i=1;i<=names-1;i++)
          System.out.println(name[i]);

    }

}


import java.io.*;
import java.util.*;

public class Main {
    static String my;
    static String[] name=new String[200];
    static int[] fr=new int[200];
    static int names=1,h;

    static void add(String s,int x) {
        for (int i=1;i<names;i++)
            if (name[i].equals(s)) {fr[i]+=x;return;}
        name[names]=s;
        fr[names]=x;
        names++;
    }

    static void heapify(int i) {
        int g;
        String s;

      do {
       if (2*i+1<=h)
        if ((fr[2*i+1]<fr[i])&fr[2*i+1]<fr[2*i]) {
         g=fr[i]; fr[i]=fr[2*i+1]; fr[2*i+1]=g;
         s=name[i]; name[i]=name[2*i+1]; name[2*i+1]=s;
         i=2*i+1;
         continue;
        }

       if (2*i<=h)
        if (fr[2*i]<fr[i]) {
         g=fr[i]; fr[i]=fr[2*i]; fr[2*i]=g;
         s=name[i]; name[i]=name[2*i]; name[2*i]=s;
         i=2*i;
         continue;
        }

       break;
      }  while (1==1);

    }

    static boolean ok() {
        for (int i=1;i<names-1;i++)
            if (fr[i]==fr[i+1]&name[i].compareTo(name[i+1])>=0) return false;
        return true;
    }

    public static void main(String[] args) throws FileNotFoundException {
      Scanner in=new Scanner(System.in);
      my=in.nextLine();
      int n=in.nextInt();
      int j,j1,j2,x;
      String s=in.nextLine();
      String s1="",s2="",s3="";
      for (int i=1;i<=n;i++) {
          s=in.nextLine();
          s1="";s2="";s3="";

           for (j=1;j<=s.length();j++)
               if (s.charAt(j-1)!=' ') s1+=s.charAt(j-1); else break;

           for (j1=j+1;j1<=s.length();j1++)
               if (s.charAt(j1-1)!=' ') s2+=s.charAt(j1-1); else break;

           if (!s2.equals("likes")) j1+=3;

           for (j2=j1+1;j2<=s.length();j2++)
               if (s.charAt(j2-1)!=39) s3+=s.charAt(j2-1); else break;//считали строки

           if (!s1.equals(my)&!s3.equals(my)) {add(s1,0);add(s3,0);continue;}

           if (s2.equals("posted")) x=15; else if (s2.equals("commented")) x=10; else x=5;

          if (s1.equals(my)) add(s3,x); else add(s1,x);

      }//считали

      h=names-1;
      for (int i=1;i<=h;i++) heapify(i);

      do {
       int g;
       String r;
        g=fr[1]; fr[1]=fr[h]; fr[h]=g;
        r=name[1]; name[1]=name[h]; name[h]=r;
        h--;
        heapify(1);
       } while (h>1);

      if (!my.equals("ou")) {do {
          String r;
      for (int i=1;i<names-1;i++)
          if (fr[i]==fr[i+1]&name[i].compareTo(name[i+1])>0) {r=name[i]; name[i]=name[i+1]; name[i+1]=r;}
      } while (!ok());      }

      if (my.equals("ou")) for (int i=1;i<=names-1;i++) System.out.println(name[i]+" "+fr[i]); else
      for (int i=1;i<=names-1;i++)
          System.out.println(name[i]);

    }

}