#include <algorithm>
#include <iostream>
#include <cstdio>
#include <vector>
using namespace std;
typedef long long llong;
const int maxN = 100'001;
const long long mod = 1000'000'007;

int dp[maxN][301];
int h[maxN];
bool is[maxN];
int dist[maxN];

int l[maxN], r[maxN];
vector<int> nei[maxN];
const int lg = 16;
int par[maxN][lg + 1];
void DFS(int v, int p)
{
    static int time = 0;
    l[v] = ++time;
    par[v][0] = p;
    for (int i = 1; i <= lg; ++i)
        if (par[v][i - 1] != -1)
            par[v][i] = par[par[v][i - 1]][i - 1];
        else
            par[v][i] = -1;
    for (int to: nei[v])
        if (to != p)
            DFS(to, v);
    r[v] = time;
}
inline bool Upper(int u, int v)
{
    return l[u] <= l[v] && r[u] >= r[v];
}
int LCA(int u, int v)
{
    if (Upper(u, v)) return u;
    if (Upper(v, u)) return v;
    for (int i = lg; i >= 0; --i)
        if (par[u][i] != -1 && !Upper(par[u][i], v))
            u = par[u][i];
    return par[u][0];
}
int Dist(int u, int v)
{
    int res = 0;
    for (int i = lg; i >= 0; --i)
        if (par[u][i] != -1 && !Upper(par[u][i], v))
        {
            res += (1 << i);
            u = par[u][i];
        }
    if (!Upper(u, v))
    {
        ++res;
        u = par[u][0];
    }
    for (int i = lg; i >= 0; --i)
        if (par[v][i] != -1 && !Upper(par[v][i], u))
        {
            res += (1 << i);
            v = par[v][i];
        }
    if (!Upper(v, u))
        ++res;
    return res;
}

vector<int> chng;
int lazy[4 * maxN];
void Add(int v, int vl, int vr, int l, int r, int val)
{
    if (r < vl || vr < l)
        return;
    if (l <= vl && vr <= r)
    {
        if (!lazy[v])
            chng.push_back(v);
        lazy[v] += val;
        return;
    }
    int vm = (vl + vr) / 2;
    Add(v * 2, vl, vm, l, r, val);
    Add(v * 2 + 1, vm + 1, vr, l, r, val);
}
int Get(int v, int vl, int vr, int i)
{
    if (vl == vr)
        return lazy[v];
    int vm = (vl + vr) / 2;
    if (i <= vm)
        return Get(v * 2, vl, vm, i) + lazy[v];
    return Get(v * 2 + 1, vm + 1, vr, i) + lazy[v];
}
int main()
{
    /*ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);*/
    int n, q;
    // cin >> n >> q;
    scanf("%d%d", &n, &q);
    for (int i = 1; i < n; ++i)
    {
        int u, v;
        // cin >> u >> v;
        scanf("%d%d", &u, &v);
        nei[u].push_back(v);
        nei[v].push_back(u);
    }
    DFS(1, -1);
    while (q--)
    {
        int k, m, root;
        // cin >> k >> m >> root;
        scanf("%d%d%d", &k, &m, &root);
        vector<int> a(k);
        for (int& v: a)
        {
            // cin >> v;
            scanf("%d", &v);
            is[v] = true;
            Add(1, 1, n, l[v], r[v], 1);
        }
        for (int v: a)
            dist[v] = Dist(root, v);
        sort(a.begin(), a.end(), [](int u, int v) {
             return dist[u] < dist[v];
        });
        int h_root = Get(1, 1, n, l[root]);
        for (int v: a)
        {
            int lca = LCA(v, root);
            h[v] = Get(1, 1, n, l[v]) - 1 + h_root - 2 * Get(1, 1, n, l[lca]) + is[lca];
        }
        dp[0][1] = 1;
        for (int i = 1; i < k; ++i)
            for (int j = 1; j <= m; ++j)
                dp[i][j] = (dp[i - 1][j - 1] + (long long)dp[i - 1][j] * (j - h[a[i]])) % mod;
        int ans = 0;
        for (int j = 1; j <= m; ++j)
            ans = (ans + dp[k - 1][j]) % mod;
        // cout << ans << '\n';
        printf("%d\n", ans);
        for (int v: a)
            is[v] = false;
        for (int i = 0; i < k; ++i)
            for (int j = 1; j <= m; ++j)
                dp[i][j] = 0;
        for (int v: chng)
            lazy[v] = 0;
        chng.clear();
    }
}


#include <algorithm>
#include <iostream>
#include <cstdio>
#include <vector>
using namespace std;
typedef long long llong;
const int maxN = 100'001;
const long long mod = 1000'000'007;

int dp[maxN][301];
int h[maxN];
bool is[maxN];
int dist[maxN];

int l[maxN], r[maxN];
vector<int> nei[maxN];
const int lg = 16;
int par[maxN][lg + 1];
void DFS(int v, int p)
{
    static int time = 0;
    l[v] = ++time;
    par[v][0] = p;
    for (int i = 1; i <= lg; ++i)
        if (par[v][i - 1] != -1)
            par[v][i] = par[par[v][i - 1]][i - 1];
        else
            par[v][i] = -1;
    for (int to: nei[v])
        if (to != p)
            DFS(to, v);
    r[v] = time;
}
inline bool Upper(int u, int v)
{
    return l[u] <= l[v] && r[u] >= r[v];
}
int LCA(int u, int v)
{
    if (Upper(u, v)) return u;
    if (Upper(v, u)) return v;
    for (int i = lg; i >= 0; --i)
        if (par[u][i] != -1 && !Upper(par[u][i], v))
            u = par[u][i];
    return par[u][0];
}
int Dist(int u, int v)
{
    int p = LCA(u, v);
    int res = 0;
    for (int i = lg; i >= 0; --i)
        if (par[u][i] != -1 && (!Upper(par[u][i], p) || par[u][i] == p))
        {
            res += (1 << i);
            u = par[u][i];
        }
    for (int i = lg; i >= 0; --i)
        if (par[v][i] != -1 && (!Upper(par[v][i], p) || par[v][i] == p))
        {
            res += (1 << i);
            v = par[v][i];
        }
    return res;
}

vector<int> chng;
int lazy[4 * maxN];
void Add(int v, int vl, int vr, int l, int r, int val)
{
    if (r < vl || vr < l)
        return;
    if (l <= vl && vr <= r)
    {
        chng.push_back(v);
        lazy[v] += val;
        return;
    }
    int vm = (vl + vr) / 2;
    Add(v * 2, vl, vm, l, r, val);
    Add(v * 2 + 1, vm + 1, vr, l, r, val);
}
int Get(int v, int vl, int vr, int i)
{
    if (vl == vr)
        return lazy[v];
    int vm = (vl + vr) / 2;
    if (i <= vm)
        return Get(v * 2, vl, vm, i) + lazy[v];
    return Get(v * 2 + 1, vm + 1, vr, i) + lazy[v];
}
int main()
{
    /*ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);*/
    int n, q;
    // cin >> n >> q;
    scanf("%d%d", &n, &q);
    for (int i = 1; i < n; ++i)
    {
        int u, v;
        // cin >> u >> v;
        scanf("%d%d", &u, &v);
        nei[u].push_back(v);
        nei[v].push_back(u);
    }
    DFS(1, -1);
    while (q--)
    {
        int k, m, root;
        // cin >> k >> m >> root;
        scanf("%d%d%d", &k, &m, &root);
        vector<int> a(k);
        for (int& v: a)
        {
            // cin >> v;
            scanf("%d", &v);
            is[v] = true;
            Add(1, 1, n, l[v], r[v], 1);
        }
        for (int v: a)
            dist[v] = Dist(root, v);
        sort(a.begin(), a.end(), [](int u, int v) {
             return dist[u] < dist[v];
        });
        for (int v: a)
        {
            int lca = LCA(v, root);
            h[v] = Get(1, 1, n, l[v]) - 1 + Get(1, 1, n, l[root]) - 2 * Get(1, 1, n, l[lca]) + is[lca];
        }
        dp[0][1] = 1;
        for (int i = 1; i < k; ++i)
            for (int j = 1; j <= m; ++j)
                dp[i][j] = (dp[i - 1][j - 1] + (long long)dp[i - 1][j] * (j - h[a[i]])) % mod;
        int ans = 0;
        for (int j = 1; j <= m; ++j)
            ans = (ans + dp[k - 1][j]) % mod;
        // cout << ans << '\n';
        printf("%d\n", ans);
        for (int v: a)
            is[v] = false;
        for (int i = 0; i < k; ++i)
            for (int j = 1; j <= m; ++j)
                dp[i][j] = 0;
        for (int v: chng)
            lazy[v] = 0;
    }
}


#include <algorithm>
#include <iostream>
#include <cstdio>
#include <vector>
using namespace std;
typedef long long llong;
const int maxN = 100'001;
const long long mod = 1000'000'007;

int dp[maxN][301];
int h[maxN];
bool is[maxN];
int dist[maxN];

int l[maxN], r[maxN];
vector<int> nei[maxN];
const int lg = 16;
int par[maxN][lg + 1];
void DFS(int v, int p)
{
    static int time = 0;
    l[v] = ++time;
    par[v][0] = p;
    for (int i = 1; i <= lg; ++i)
        if (par[v][i - 1] != -1)
            par[v][i] = par[par[v][i - 1]][i - 1];
        else
            par[v][i] = -1;
    for (int to: nei[v])
        if (to != p)
            DFS(to, v);
    r[v] = time;
}
inline bool Upper(int u, int v)
{
    return l[u] <= l[v] && r[u] >= r[v];
}
int LCA(int u, int v)
{
    if (Upper(u, v)) return u;
    if (Upper(v, u)) return v;
    for (int i = lg; i >= 0; --i)
        if (par[u][i] != -1 && !Upper(par[u][i], v))
            u = par[u][i];
    return par[u][0];
}
int Dist(int u, int v)
{
    int res = 0;
    for (int i = lg; i >= 0; --i)
        if (par[u][i] != -1 && (!Upper(par[u][i], v) || par[u][i] == v))
        {
            res += (1 << i);
            u = par[u][i];
        }
    for (int i = lg; i >= 0; --i)
        if (par[v][i] != -1 && (!Upper(par[v][i], u) || par[v][i] == u))
        {
            res += (1 << i);
            v = par[v][i];
        }
    return res;
}

vector<int> chng;
int lazy[4 * maxN];
void Add(int v, int vl, int vr, int l, int r, int val)
{
    if (r < vl || vr < l)
        return;
    if (l <= vl && vr <= r)
    {
        chng.push_back(v);
        lazy[v] += val;
        return;
    }
    int vm = (vl + vr) / 2;
    Add(v * 2, vl, vm, l, r, val);
    Add(v * 2 + 1, vm + 1, vr, l, r, val);
}
int Get(int v, int vl, int vr, int i)
{
    if (vl == vr)
        return lazy[v];
    int vm = (vl + vr) / 2;
    if (i <= vm)
        return Get(v * 2, vl, vm, i) + lazy[v];
    return Get(v * 2 + 1, vm + 1, vr, i) + lazy[v];
}
int main()
{
    /*ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);*/
    int n, q;
    // cin >> n >> q;
    scanf("%d%d", &n, &q);
    for (int i = 1; i < n; ++i)
    {
        int u, v;
        // cin >> u >> v;
        scanf("%d%d", &u, &v);
        nei[u].push_back(v);
        nei[v].push_back(u);
    }
    DFS(1, -1);
    while (q--)
    {
        int k, m, root;
        // cin >> k >> m >> root;
        scanf("%d%d%d", &k, &m, &root);
        vector<int> a(k);
        for (int& v: a)
        {
            // cin >> v;
            scanf("%d", &v);
            is[v] = true;
            Add(1, 1, n, l[v], r[v], 1);
        }
        for (int v: a)
            dist[v] = Dist(root, v);
        sort(a.begin(), a.end(), [](int u, int v) {
             return dist[u] < dist[v];
        });
        for (int v: a)
        {
            int lca = LCA(v, root);
            h[v] = Get(1, 1, n, l[v]) - 1 + Get(1, 1, n, l[root]) - 2 * Get(1, 1, n, l[lca]) + is[lca];
        }
        dp[0][1] = 1;
        for (int i = 1; i < k; ++i)
            for (int j = 1; j <= m; ++j)
                dp[i][j] = (dp[i - 1][j - 1] + (long long)dp[i - 1][j] * (j - h[a[i]])) % mod;
        int ans = 0;
        for (int j = 1; j <= m; ++j)
            ans = (ans + dp[k - 1][j]) % mod;
        // cout << ans << '\n';
        printf("%d\n", ans);
        for (int v: a)
            is[v] = false;
        for (int i = 0; i < k; ++i)
            for (int j = 1; j <= m; ++j)
                dp[i][j] = 0;
        for (int v: chng)
            lazy[v] = 0;
    }
}


// COPIED FROM EDITORIAL JUST A TRIAL

#include<bits/stdc++.h>
using namespace std;
#define FastRead ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define ll long long int
#define FOR(i,a,n) for (int i=(a);i<=(n);++i)
#define RFOR(i,a,n) for (int i=(n);i>=(a);--i)
#define FI(i,n) for (int i=0; i<(n); ++i)
#define ZERO(a) memset((a),0,sizeof((a)))
#define f first
// #define s second
#define pb push_back
#define mk make_pair
#define all(g) g.begin(),g.end()
int fastMax(int x, int y) { return (((y-x)>>(32-1))&(x^y))^y; }
int fastMin(int x, int y) { return (((y-x)>>(32-1))&(x^y))^x; }

// #include <ext/pb_ds/assoc_container.hpp> // Common file
// #include <ext/pb_ds/tree_policy.hpp>     // Including tree_order_statistics_node_updat
// using namespace __gnu_pbds;
// typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;

// I am questioning life and universe and
// everything else after looking at this

const ll MAXN = 200010;
const ll LG = 20;
const ll MOD = 1e9+7;

ll power(ll x,ll y){
    ll res = 1;
    while(y){
        if(y&1) res = (res*x)%MOD;
        x = (x*x)%MOD;
        y >>= 1;
    }
    return res;
}

ll ver[2*MAXN]; // GIVES ID OF VERTEX IN EULER ARRAY
ll bit[2*MAXN];
ll st[MAXN],en[MAXN],A[MAXN]; // st and en are the start and end time of node i
ll f[MAXN];
ll dp[MAXN];
ll par[MAXN][LG];
ll mark[MAXN],lev[MAXN];

vector<ll> graph[MAXN];
ll tim = 0,n;

void dfs(ll u,ll p){
    if(p+1){
        lev[u] = lev[p] + 1;
        par[u][0] = p;
        FOR(j,1,LG-1) if(par[u][j-1] + 1) par[u][j] = par[par[u][j-1]][j-1];
    }

    st[u] = ++tim;
    ver[tim] = u;
    for(ll v:graph[u]){
        if(p-v) dfs(v,u);
    }
    en[u] = ++tim;
    ver[tim] = u;
}

ll lca(ll a,ll b){
    if(lev[a] < lev[b]) swap(a,b);

    for(ll j=LG-1;j>=0;j--)
        if(par[a][j] + 1 && lev[par[a][j]] >= lev[b]) a = par[a][j];

    if(a == b) return a;

    for(ll j = LG-1;j>=0;j--){
        if(par[a][j] + 1 && par[a][j] != par[b][j]){
           a = par[a][j];
           b = par[b][j];
        }
    }

    return par[a][0];
}

void update(ll idx,ll val){
    for(;idx<=2*n;idx+=(idx&(-idx))) bit[idx] += val;
}

ll query(ll idx){
    ll res = 0;
    for(;idx > 0;idx -= (idx&(-idx))) res += bit[idx];
    return res;
}

void solve(){
    ll i,j,m,q,u,v,w,typ,e,k,x;
    memset(par,-1,sizeof(par));

    cin>>n>>q;
    FOR(i,1,n-1){
        cin>>u>>v;
        graph[u].push_back(v);
        graph[v].push_back(u);
    }

    dfs(1,-1);
    ll root;

    while(q--){
        cin>>k>>x>>root;

        FOR(i,1,k){
            cin>>A[i];
            mark[A[i]] = 1;
            update(st[A[i]],1);
            update(en[A[i]],-1);
        }

        ll ans_root = query(st[root]);

        FOR(i,1,k){
            ll lp = lca(A[i],root);
            f[i] = query(st[A[i]]) + ans_root - 2*query(st[lp]) + (mark[lp] == 1) - 1;
        }

        sort(f+1,f+k+1);
        FOR(i,1,k){
            update(st[A[i]],-1);
            update(en[A[i]],1);
            mark[A[i]] = 0;
        }

        FOR(i,0,x) dp[i] = 0;

        ll fl = 0;
        FOR(i,1,k) if(f[i] >= x) fl = 1;
        if(fl) cout<<"0\n";
        else {
            dp[0] = 1;
            FOR(i,1,k){
                for(ll j=min((ll)i,x);j>=0;j--){
                    if(j<=f[i]) dp[j] = 0;
                    else dp[j] = ((dp[j]*(j-f[i]))%MOD + dp[j-1])%MOD;
                }
            }

            ll ans = 0;
            FOR(i,1,x) ans = (ans+dp[i])%MOD;

            cout<<ans<<endl;
        }
    }
}

signed main(){

#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif

    FastRead;

    ll t;
    t = 1;
    // cin>>t;

    while(t--) solve();

}


#include <bits/stdc++.h>
using namespace std;
const int maxn=1e5+10,mo=1e9+7;

long long Pow(long long x,int p) {long long res=1;while (p) {if (p&1) (res*=x)%=mo;(x*=x)%=mo;p>>=1;}return res;}
long long inv(long long x) {return Pow(x,mo-2);}

struct HLD // Heavy_Light_Decomposition
{
	struct Segment_Tree
	{
		int sum[maxn<<2];
		int n;

		#define ls (o<<1)
		#define rs ((o<<1)|1)
		#define mid ((l+r)>>1)

		void Update(int o)
		{
			sum[o]=sum[ls]+sum[rs];
		}

		void Change(int o,int l,int r,int a,int b,int t_inc)
		{
			if (a<=l && r<=b)
			{
				sum[o]+=t_inc;
				return;
			}
			if (a<=mid) Change(ls,l,mid,a,b,t_inc);
			if (b> mid) Change(rs,mid+1,r,a,b,t_inc);
			Update(o);
		}

		int Query(int o,int l,int r,int a,int b)
		{
			if (a<=l && r<=b)
				return sum[o];
			int res=0;
			if (a<=mid) res+=Query(ls,l,mid,a,b);
			if (b> mid) res+=Query(rs,mid+1,r,a,b);
			Update(o);
			return res;
		}

		void Build_Tree(int o,int l,int r)
		{
			sum[o]=0;
			if (l==r) return;
			Build_Tree(ls,l,mid);
			Build_Tree(rs,mid+1,r);
		}

		#undef ls
		#undef rs
		#undef mid

		void Init(int _n) {n=_n;Build_Tree(1,1,n);}
		void Change(int x,int y,int t_inc) {Change(1,1,n,x,y,t_inc);}
		int Query(int x,int y) {return Query(1,1,n,x,y);}
	}ST;

	int wson[maxn];
	int sons[maxn];
	int top[maxn];
	int dep[maxn];
	int fa[maxn];
	int dfq[maxn],order[maxn],Index;
	int *head,*nxt,*E;
	int n;

	void DFS(int x,int father,int depth)
	{
		dep[x]=depth;
		fa[x]=father;
		sons[x]=1;
		for (int i=head[x];i;i=nxt[i]) if (E[i]!=father)
		{
			DFS(E[i],x,depth+1),sons[x]+=sons[E[i]];
			if (sons[wson[x]]<sons[E[i]]) wson[x]=E[i];
		}
	}

	void DFS2(int x,int father,int is_wson)
	{
		top[x]=(is_wson)?top[father]:x;
		dfq[++Index]=x;
		order[x]=Index;
		if (wson[x]) DFS2(wson[x],x,1);
		for (int i=head[x];i;i=nxt[i]) if (E[i]!=father && E[i]!=wson[x])
			DFS2(E[i],x,0);
	}

	void Build_HLD(int _n,int *_head,int *_nxt,int *_E)
	{
		n=_n,head=_head,nxt=_nxt,E=_E;
		Index=0;
		memset(wson,0,(n+1)*sizeof(int));
		DFS(1,0,1);
		DFS2(1,0,0);
		ST.Init(n);
	}

	int Query(int x,int y)
	{
		int res=0;
		while (top[x]!=top[y])
			(dep[top[x]]>dep[top[y]]) ? (res+=ST.Query(order[top[x]],order[x]),x=fa[top[x]])
									  : (res+=ST.Query(order[top[y]],order[y]),y=fa[top[y]]);
		if (order[x]>order[y]) swap(x,y);
		res+=ST.Query(order[x],order[y]);
		return res;
	}

	void Change(int x,int y,int t_inc)
	{
		while (top[x]!=top[y])
			(dep[top[x]]>dep[top[y]]) ? (ST.Change(order[top[x]],order[x],t_inc),x=fa[top[x]])
									  : (ST.Change(order[top[y]],order[y],t_inc),y=fa[top[y]]);
		if (order[x]>order[y]) swap(x,y);
		ST.Change(order[x],order[y],t_inc);
	}

	void Change_Inc(int x,int y,int t_inc) {Change(x,y,t_inc);}
}T;

int head[maxn],nxt[maxn<<1],E[maxn<<1],Ecnt;
long long C[310][310],InvFactor[310],f[310];
int a[maxn],b[maxn];
int n,q;

void Add_Edge(int x,int y)
{
	nxt[++Ecnt]=head[x];head[x]=Ecnt;E[Ecnt]=y;
	nxt[++Ecnt]=head[y];head[y]=Ecnt;E[Ecnt]=x;
}

void Init()
{
	for (int i=0;i<=300;i++)
	{
		C[i][0]=1;
		for (int j=1;j<=i;j++)
			C[i][j]=(C[i-1][j-1]+C[i-1][j])%mo;
	}
	InvFactor[0]=1;
	for (int i=1;i<=300;i++)
		InvFactor[i]=InvFactor[i-1]*inv(i)%mo;
}

int main()
{
	Init();
	scanf("%d%d",&n,&q);
	for(int i=1,x,y;i<n;i++)
		scanf("%d%d",&x,&y),Add_Edge(x,y);
	T.Build_HLD(n,head,nxt,E);
	while(q--)
	{
		int k,m,r;
		scanf("%d%d%d",&k,&m,&r);
		for (int i=1;i<=k;i++) scanf("%d",&a[i]);
		for (int i=1;i<=k;i++) T.Change_Inc(a[i],a[i],1);
		for (int i=1;i<=k;i++) b[i]=T.Query(a[i],r)-1;
		for (int i=1;i<=k;i++) T.Change_Inc(a[i],a[i],-1);
		long long res=0;
		for (int i=1;i<=m;i++)
		{
			f[i]=1;
			for (int j=1;j<=k;j++) if (b[j]>=i) f[i]=0;
			if (f[i]==0) continue;
			for (int j=1;j<=k;j++) f[i]=f[i]*(i-b[j])%mo;
			for (int j=1;j<i;j++) f[i]=(f[i]-C[i][j]*f[i-j]%mo+mo)%mo;
			res=(res+f[i]*InvFactor[i]%mo)%mo;
		}
		printf("%lld\n",res);
	}
	return 0;
}


/*
5 2
3 5
4 5
4 2
1 4
2 2 3 1 2
2 2 4 5 4
*/
#include <bits/stdc++.h>
using namespace std; using ll = long long; using ld = long double; using pii = pair<int, int>; const int N = 1e5 + 5, M = 1e9 + 7, INF = 0x3f3f3f3f; const ll LNF = 0x3f3f3f3f3f3f3f3f; void Main();
void debug() { cerr << endl; } template <typename T, typename... Ts> void debug(T x, Ts... y) { cerr << "\033[31m" << x << "\033[0m"; if (sizeof...(y) > 0) cerr << ' '; debug(y...); } template <typename T> void debug(const T& a, int l, int r, char c) { for (int i = l; i <= r; ++i) cerr << "\033[31m" << a[i] << "\033[0m" << (i == r ? '\n' : c); }
void print() { cout << '\n'; } template <typename T, typename... Ts> void print(T x, Ts... y) { cout << x; if (sizeof...(y) > 0) cout << ' '; print(y...); } template <typename T> void print(const T& a, int l, int r, char c) { for (int i = l; i <= r; ++i) cout << a[i] << (i == r ? '\n' : c); }
void input() {} template <typename T, typename... Ts> void input(T& x, Ts&... y) { cin >> x; input(y...); } template <typename T> void input(T* a, int l, int r) { for (int i = l; i <= r; ++i) cin >> a[i]; } template <typename T> void input(vector<T>& a, int l, int r) { for (int i = l; i <= r; ++i) cin >> a[i]; }
template <typename T> void Max(T& a, T b) { if (a < b) a = b; } template <typename T> void Min(T& a, T b) { if (a > b) a = b; } template <typename T> void Add(T& a, T b) { a += b; if (a > M) a -= M; if (a < 0) a += M; } template <typename T> void Mul(T& a, T b) { a = a * b % M; } template <typename T> T Mod(T a) { return (a % M + M) % M; }
int main(int argc, char** argv) {
    ios::sync_with_stdio(0); cin.tie(0); /* cout << setprecision(10) << fixed; */
#ifdef Zmm
    if (argc) { freopen((string(*(argv)).substr(2, string(*(argv)).size() - 6) + string(".cpp")).c_str(), "r", stdin); string x; cin >> x; }
#endif
    Main();
}
template<int N>
struct BIT {
  int a[N], n, tag[N], TAG;
  void add(int p, int x = 1) { for (; p <= n; p += p & -p) a[p] += (tag[p] == TAG ? x : x - a[p]), tag[p] = TAG; }
  int getsum(int p) {
    int sum = 0;
    for ( ; p >= 1; p -= p & -p) sum += (tag[p] == TAG ? a[p] : 0);
    return sum;
  }
  void memset() { TAG++; }
};
template<int N>
struct LCA {
    vector<int> G[N];
    int dep[N], fa[N], top[N], siz[N], son[N], dfn[N], cnt;
    void add(int u, int v) {
        G[u].push_back(v);
        G[v].push_back(u);
    }
    void dfs1(int u, int f) {
        dep[u] = dep[f] + 1;
        siz[u] = 1;
        fa[u] = f;
        for (int v : G[u]) if (v != fa[u]) {
            dfs1(v, u);
            siz[u] += siz[v];
            if (!son[u] || siz[son[u]] < siz[v]) son[u] = v;
        }
    }
    void dfs2(int u, int t) {
        dfn[u] = ++cnt;
        top[u] = t;
        if (son[u]) dfs2(son[u], t);
        for (int v : G[u])
            if (v != son[u] && v != fa[u]) dfs2(v, v);
    }
    int lca(int u, int v) {
        while (top[u] != top[v]) {
            if (dep[top[u]] > dep[top[v]]) u = fa[top[u]];
            else v = fa[top[v]];
        }
        return dep[u] > dep[v] ? v : u;
    }
};
BIT<N> t;
LCA<N> L;
int Q(int u, int v) {
    int ret = 0;
    while (L.top[u] != L.top[v]) {
        if (L.dep[L.top[u]] > L.dep[L.top[v]]) swap(u, v);
        ret += t.getsum(L.dfn[v]) - t.getsum(L.dfn[L.top[v]] - 1);
        v = L.fa[L.top[v]];
    }
    if (L.dep[u] > L.dep[v]) swap(u, v);
    return ret + t.getsum(L.dfn[v]) - t.getsum(L.dfn[u] - 1);
}
int a[N], f[N];
ll dp[N][305];
void Main() {
    int n, q;
    input(n, q);
    for (int i = 1; i < n; ++i) {
        int u, v;
        input(u, v);
        L.add(u, v);
    }
    L.dfs1(1, 1);
    L.dfs2(1, 1);
    t.n = n;
    while (q--) {
        int k, m, r;
        input(k, m, r);
        t.memset();
        for (int i = 1; i <= k; ++i) {
            input(a[i]);
            t.add(L.dfn[a[i]]);
        }
        for (int i = 1; i <= k; ++i) {
            f[a[i]] = Q(a[i], r) - 1;
            // debug(a[i], f[a[i]]);
        }
        sort(a + 1, a + k + 1, [](int x, int y) { return f[x] < f[y]; });
        dp[0][0] = 1;
        for (int i = 1; i <= k; ++i) for (int j = 1; j <= min(m, i); ++j) {
            dp[i][j] = (dp[i - 1][j - 1] + max(0ll, (j - f[a[i]]) * dp[i - 1][j])) % M;
            // print(a[i], i, j, dp[i][j]);
        }
        ll ans = 0;
        for (int i = 1; i <= m; ++i) {
            Add(ans, dp[k][i]);
        }
        print(ans);
    }
}


#include <bits/stdc++.h>
using namespace std;
using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define dbg(x) cerr<<#x": "<<(x)<<endl
#define dbg_p(x) cerr<<#x": "<<(x).first<<' '<<(x).second<<endl
#define dbg_v(x, n) {cerr<<#x"[]: ";for(long long _=0;_<n;++_)cerr<<(x)[_]<<' ';cerr<<endl;}
#define all(v) v.begin(), v.end()
#define fi first
#define se second

template<typename T1, typename T2>
ostream& operator <<(ostream &out, const pair<T1, T2> &item) {
	out << '(' << item.first << ", " << item.second << ')';
	return out;
}

template<typename T>
ostream& operator <<(ostream &out, const vector<T> &v) {
	for(const auto &item : v) out << item << ' ';
	return out;
}

const int KMAX = 310;
const int NMAX = 100010;
const int LOGN = 18;
const int MOD = 1000000007;

int k, v[NMAX], mark[NMAX];
int lg[2 * NMAX];
int h[NMAX], tin[NMAX], tout[NMAX];
int rmq[LOGN][2 * NMAX];
int st[2 * NMAX];
int dp[NMAX][KMAX];
std::vector<int> adj[NMAX];

int lcaMax(int d, int a, int b) {
	return h[rmq[d][a]] < h[rmq[d][b]] ? rmq[d][a] : rmq[d][b];
}

int getLca(int v, int u) {
	v = tin[v];
	u = tin[u];
	if(v > u) swap(v, u);

	int d = lg[u - v + 1];
	return lcaMax(d, v, u - (1 << d) + 1);
}

int dist(int v, int u) {
	return h[v] + h[u] - 2 * h[getLca(v, u)];
}

void update(int l, int r, int val) {
	// cerr << l << ' ' << r << '\n';
	for(l += k, r += k + 1; l < r; l >>= 1, r >>= 1) {
		if(l & 1) st[l++] += val;
		if(r & 1) st[--r] += val;
	}
}

int query(int p) {
	int res;
	for(res = 0, p += k; p; p >>= 1) res += st[p];

	return res;
}

int queryNode(int v) {
	return query(tin[v]);
}

void dfs(int v, int p = -1) {
	rmq[0][k] = v;
	tin[v] = k++;

	for(auto u : adj[v]) {
		if(u == p) continue;

		h[u] = 1 + h[v];
		dfs(u, v);
		rmq[0][k++] = v;
	}

	tout[v] = k - 1;
}

int main()
{
	ios_base::sync_with_stdio(false);

	int i, j, n, q, a, b, nr, m, r, ans;

	for(i = 2; i < 2 * NMAX; ++i) lg[i] = 1 + lg[i >> 1];

	cin >> n >> q;
	for(i = 1; i < n; ++i) {
		cin >> a >> b;
		adj[a].push_back(b);
		adj[b].push_back(a);
	}

	dfs(1);
	for(i = 1; i < LOGN; ++i)
		for(j = 0; j + (1 << i) <= k; ++j)
			rmq[i][j] = lcaMax(i - 1, j, j + (1 << (i - 1)));

	while(q--) {
		cin >> nr >> m >> r;

		for(i = 1; i <= nr; ++i) cin >> v[i];
		sort(v + 1, v + nr + 1, [&r](int a, int b) { return dist(a, r) < dist(b, r); });

		// dbg_v(v + 1, nr);

		// if(v[1] == r) {
		// 	cout << (nr == 1 ? 1 : 0) << '\n';
		// 	continue;
		// }

		dp[0][0] = 1;
		for(i = 1; i <= nr; ++i) {
			int lca = getLca(r, v[i]);
			int nrAnc = queryNode(r) + queryNode(v[i]) - 2 * queryNode(lca) + mark[lca];
			// dbg(lca);
			// dbg(nrAnc);
			// dbg(queryNode(r));
			// dbg(queryNode(v[i]));
			// dbg(queryNode(lca));

			for(j = 1; j <= m; ++j) {
				dp[i][j] = (1LL * dp[i - 1][j] * max(0, j - nrAnc) + dp[i - 1][j - 1]) % MOD;
			}

			update(tin[v[i]], tout[v[i]], 1);
			mark[v[i]] = 1;
		}

		ans = 0;
		for(j = 1; j <= m; ++j) if((ans += dp[nr][j]) >= MOD) ans -= MOD;

		cout << ans << '\n';

		for(i = 1; i <= nr; ++i) {
			mark[v[i]] = 0;
			update(tin[v[i]], tout[v[i]], -1);
		}
	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define dbg(x) cerr<<#x": "<<(x)<<endl
#define dbg_p(x) cerr<<#x": "<<(x).first<<' '<<(x).second<<endl
#define dbg_v(x, n) {cerr<<#x"[]: ";for(long long _=0;_<n;++_)cerr<<(x)[_]<<' ';cerr<<endl;}
#define all(v) v.begin(), v.end()
#define fi first
#define se second

template<typename T1, typename T2>
ostream& operator <<(ostream &out, const pair<T1, T2> &item) {
	out << '(' << item.first << ", " << item.second << ')';
	return out;
}

template<typename T>
ostream& operator <<(ostream &out, const vector<T> &v) {
	for(const auto &item : v) out << item << ' ';
	return out;
}

const int KMAX = 310;
const int NMAX = 100010;
const int LOGN = 18;
const int MOD = 1000000007;

int k, v[NMAX], mark[NMAX];
int lg[2 * NMAX];
int h[NMAX], tin[NMAX], tout[NMAX];
int rmq[LOGN][2 * NMAX];
int st[4 * NMAX];
int dp[NMAX][KMAX];
std::vector<int> adj[NMAX];

int lcaMax(int d, int a, int b) {
	return h[rmq[d][a]] < h[rmq[d][b]] ? rmq[d][a] : rmq[d][b];
}

int getLca(int v, int u) {
	v = tin[v];
	u = tin[u];
	if(v > u) swap(v, u);

	int d = lg[u - v + 1];
	return lcaMax(d, v, u - (1 << d) + 1);
}

int dist(int v, int u) {
	return h[v] + h[u] - 2 * h[getLca(v, u)];
}

void update(int l, int r, int val) {
	// cerr << l << ' ' << r << '\n';
	for(l += k, r += k + 1; l < r; l >>= 1, r >>= 1) {
		if(l & 1) st[l++] += val;
		if(r & 1) st[--r] += val;
	}
}

int query(int p) {
	int res;
	for(res = 0, p += k; p; p >>= 1) res += st[p];

	return res;
}

int queryNode(int v) {
	return query(tin[v]);
}

void dfs(int v, int p = -1) {
	rmq[0][k] = v;
	tin[v] = k++;

	for(auto u : adj[v]) {
		if(u == p) continue;

		h[u] = 1 + h[v];
		dfs(u, v);
		rmq[0][k++] = v;
	}

	tout[v] = k - 1;
}

int main()
{
	ios_base::sync_with_stdio(false);

	int i, j, n, q, a, b, nr, m, r, ans;

	for(i = 2; i < 2 * NMAX; ++i) lg[i] = 1 + lg[i >> 1];

	cin >> n >> q;
	for(i = 1; i < n; ++i) {
		cin >> a >> b;
		adj[a].push_back(b);
		adj[b].push_back(a);
	}

	dfs(1);
	for(i = 1; i < LOGN; ++i)
		for(j = 0; j + (1 << i) <= k; ++j)
			rmq[i][j] = lcaMax(i - 1, j, j + (1 << (i - 1)));

	while(q--) {
		cin >> nr >> m >> r;

		for(i = 1; i <= nr; ++i) cin >> v[i];
		sort(v + 1, v + nr + 1, [&r](int a, int b) { return dist(a, r) < dist(b, r); });

		// dbg_v(v + 1, nr);

		// if(v[1] == r) {
		// 	cout << (nr == 1 ? 1 : 0) << '\n';
		// 	continue;
		// }

		dp[0][0] = 1;
		for(i = 1; i <= nr; ++i) {
			int lca = getLca(r, v[i]);
			int nrAnc = queryNode(r) + queryNode(v[i]) - 2 * queryNode(lca) + mark[lca];
			// dbg(lca);
			// dbg(nrAnc);
			// dbg(queryNode(r));
			// dbg(queryNode(v[i]));
			// dbg(queryNode(lca));

			for(j = 1; j <= m; ++j) {
				dp[i][j] = (1LL * dp[i - 1][j] * max(0, j - nrAnc) + dp[i - 1][j - 1]) % MOD;
			}

			update(tin[v[i]], tout[v[i]], 1);
			mark[v[i]] = 1;
		}

		ans = 0;
		for(j = 1; j <= m; ++j) if((ans += dp[nr][j]) >= MOD) ans -= MOD;

		cout << ans << '\n';

		for(i = 1; i <= nr; ++i) {
			mark[v[i]] = 0;
			update(tin[v[i]], tout[v[i]], -1);
		}
	}

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair <int,int> pii;

const int N = 100100;
const int Mod = 1e9 + 7;

int n;
int BIT[N];
int Size[N];
int Head[N];
int Heavy[N];
int Depth[N];
int Where[N];
int Parent[N];
vector <int> Adj[N];
int CurrentPosition = 0;

void add_self(int& x, int y)
{
    if((x += y) >= Mod) x -= Mod;
}

int mul(int x, int y)
{
    return (ll) x * y % Mod;
}

void Update(int i, int x)
{
    for( ; i <= n; i += i & -i) BIT[i] += x;
}

int GetSum(int i)
{
    int Res = 0;
    for( ; i > 0; i -= i & -i)  Res += BIT[i];
    return Res;
}

int GetSum(int L, int R)
{
    if(L > R)   return 0;
    return GetSum(R) - GetSum(L - 1);
}

void DFS(int x = 1, int p = 0)
{
    Size[x] = 1;
    Heavy[x] = 0;
    Parent[x] = p;
    Depth[x] = Depth[p] + 1;

    for(int y : Adj[x])
    {
        if(y ^ p)
        {
            DFS(y, x);
            Size[x] += Size[y];

            if(Size[Heavy[x]] < Size[y])
                Heavy[x] = y;
        }
    }
}

void Decompose(int x = 1, int h = 1)
{
    Head[x] = h;
    Where[x] = ++CurrentPosition;

    if(Heavy[x])    Decompose(Heavy[x], h);

    for(int y : Adj[x])
        if((y ^ Parent[x]) && (y ^ Heavy[x]))
            Decompose(y, y);
}

int LCA(int x, int y)
{
    for( ; Head[x] ^ Head[y]; x = Parent[Head[x]])
        if(Depth[Head[x]] < Depth[Head[y]])
            swap(x, y);

    if(Depth[x] > Depth[y]) swap(x, y);

    return x;
}

int Dist(int x, int y)
{
    return Depth[x] + Depth[y] - 2 * Depth[LCA(x, y)];
}

int Query(int x, int y)
{
    int Res = 0;

    for( ; Head[x] ^ Head[y]; x = Parent[Head[x]])
    {
        if(Depth[Head[x]] < Depth[Head[y]])
            swap(x, y);

        Res += GetSum(Where[Head[x]], Where[x]);
    }

    if(Depth[x] > Depth[y]) swap(x, y);

    Res += GetSum(Where[x], Where[y]);

    return Res;
}

int main()
{
    int q;  cin >> n >> q;

    for(int i = 1; i < n; i++)
    {
        int x, y;   scanf("%d%d", &x, &y);

        Adj[x].push_back(y);
        Adj[y].push_back(x);
    }

    DFS();
    Decompose();

    while(q--)
    {
        int k, m, r;    scanf("%d%d%d", &k, &m, &r);

        vector <pii> a(k);
        for(pii& p : a) scanf("%d", &p.second), p.first = Dist(r, p.second);
        sort(a.begin(), a.end());

        for(pii& p : a)
        {
            p.first = Query(p.second, r);
            Update(Where[p.second], +1);
        }

        vector <int> Dp(m + 1, 1);

        for(int i = k - 1; ~i; --i)
        {
            int h = a[i].first;

            for(int j = 0; j <= m; j++)
            {
                Dp[j] = (j > h ? mul(j - h, Dp[j]) : 0);
                if(j < m)   add_self(Dp[j], Dp[j + 1]);
            }
        }

        printf("%d\n", Dp[0]);

        for(pii& p : a) Update(Where[p.second], -1);
    }
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define sf(a) scanf("%d",&a)
const int N = 100005;
struct node
{
	int l,r,sum;
}tree[N<<2];
//vector<int > e[N];
struct Edge{
    int v,nxt;
    Edge(){}
    Edge(int v,int nxt):v(v),nxt(nxt){}
}edge[N<<1];
int head[N],son[N],fa[N],top[N],siz[N],dep[N],num,cnt,pos[N],f[N];
void add(int u,int v){
    edge[cnt]=Edge(v,head[u]);
    head[u]=cnt++;
    edge[cnt]=Edge(u,head[v]);
    head[v]=cnt++;
}
void dfs1(int u,int Fa,int d)
{
	siz[u]=1;son[u]=0;dep[u]=d;
	fa[u]=Fa;
	for(int i=head[u];i!=-1;i=edge[i].nxt)
	{
	  int v=edge[i].v;
	  if(v==Fa) continue;
	  dfs1(v,u,d+1); siz[u]+=siz[v];
	  if(siz[v]>siz[son[u]])  son[u]=v;
	}
}
void dfs2(int u,int tp)
{
   top[u]=tp;
   pos[u]=++num;
   if(son[u]) dfs2(son[u],tp);
   for(int i=head[u];i!=-1;i=edge[i].nxt)
   {
	 int v=edge[i].v;
   	 if(v!=fa[u]&&v!=son[u]) dfs2(v,v);
   }

}
void build(int rt,int l,int r)
{
   tree[rt].l=l,tree[rt].r=r;tree[rt].sum=0;
   if(l==r)
   {
   	 return ;
   }
   int mid=(l+r)>>1;
   build(rt<<1,l,mid);
   build(rt<<1|1,mid+1,r);
}
void updata(int rt,int p, int val)
{
	if(tree[rt].l==tree[rt].r)
	{
	  tree[rt].sum=val;
	  return ;
	}
	int mid=(tree[rt].l+tree[rt].r)>>1;
	if(p<=mid) updata(rt<<1,p,val);
    if(p>mid ) updata(rt<<1|1,p,val);
    tree[rt].sum=tree[rt<<1].sum+tree[rt<<1|1].sum;
}
int query(int rt,int l,int r)
{
	if(l<=tree[rt].l&&tree[rt].r<=r)
	{
		return tree[rt].sum;
	}
	int mid=(tree[rt].l+tree[rt].r)>>1;
	int ans=0;
	if(l<=mid) ans+=query(rt<<1,l,r);
	if(r>mid ) ans+=query(rt<<1|1,l,r);
	return ans;
}
int cal(int u,int v)
{
	int tp1=top[u];int tp2=top[v];
	int ans=0;
	while(tp1!=tp2)
	{
	  if(dep[tp1]<dep[tp2]) swap(tp1,tp2),swap(u,v);
	  ans+=query(1,pos[tp1],pos[u]);
	  u=fa[u];
	  tp1=top[u];

	}
	if(dep[u]>dep[v]) swap(u,v);
	return ans+query(1,pos[u],pos[v]);
}
ll dp[301];
const int mod = 1e9+7;
struct edg
{
  int x,f;
}id[N];
int cmp(edg a,edg b){return a.f<b.f;}
int main()
{
	ios::sync_with_stdio(false);
   memset(head,-1,sizeof(head));
   int n,q;num=cnt=0;
   int u,v;
   sf(n),sf(q);
   for(int i=1;i<n;i++)
   {
   	 sf(u),sf(v);
   	 add(u,v);
   }
   dfs1(1,0,0);dfs2(1,1);
   build(1,1,num);
   int k,m,r;
   while(q--)
   {
   	 sf(k),sf(m),sf(r);
   	 for(int i=1;i<=k;i++)
   	 {
   	 	sf(id[i].x);
   	 	updata(1,pos[id[i].x],1);
	 }
	 for(int i=1;i<=k;i++)
	 {
	 	id[i].f=cal(id[i].x,r)-1;
	 	//cout<<id[i].x<<' '<<id[i].f<<endl;
	 }
	 sort(id+1,id+k+1,cmp);
   	 for(int i=1;i<=k;i++)
   	 {
   	 	updata(1,pos[id[i].x],0);
	 }memset(dp,0,sizeof(dp));
	 if(id[k].f>=m) {cout<<0<<endl;}
	 else
	 {

	   dp[0]=1;
	   ll ans=0;
	   for(int i=1;i<=k;i++)
	   {
	     for(int j=min(m,i);~j;j--)
		 {
		   if(j<=id[i].f) dp[j]=0;
		   else dp[j]=(dp[j]*(j-id[i].f)%mod+dp[j-1])%mod;
	     }
	   }
	   for(int i=1;i<=m;i++) ans=(ans+dp[i])%mod;
	   cout<<ans<<endl;
	 }

   }
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define sf(a) scanf("%d",&a)
const int N = 100005;
struct node
{
	int l,r,sum;
}tree[N<<2];
//vector<int > e[N];
struct Edge{
    int v,nxt;
    Edge(){}
    Edge(int v,int nxt):v(v),nxt(nxt){}
}edge[N<<1];
int head[N],son[N],fa[N],top[N],siz[N],dep[N],num,cnt,pos[N],f[N];
void add(int u,int v){
    edge[cnt]=Edge(v,head[u]);
    head[u]=cnt++;
    edge[cnt]=Edge(u,head[v]);
    head[v]=cnt++;
}
void dfs1(int u,int Fa,int d)
{
	siz[u]=1;son[u]=0;dep[u]=d;
	fa[u]=Fa;
	for(int i=head[u];i!=-1;i=edge[i].nxt)
	{
	  int v=edge[i].v;
	  if(v==Fa) continue;
	  dfs1(v,u,d+1); siz[u]+=siz[v];
	  if(siz[v]>siz[son[u]])  son[u]=v;
	}
}
void dfs2(int u,int tp)
{
   top[u]=tp;
   pos[u]=++num;
   if(son[u]) dfs2(son[u],tp);
   for(int i=head[u];i!=-1;i=edge[i].nxt)
   {
	 int v=edge[i].v;
   	 if(v!=fa[u]&&v!=son[u]) dfs2(v,v);
   }

}
void build(int rt,int l,int r)
{
   tree[rt].l=l,tree[rt].r=r;tree[rt].sum=0;
   if(l==r)
   {
   	 return ;
   }
   int mid=(l+r)>>1;
   build(rt<<1,l,mid);
   build(rt<<1|1,mid+1,r);
}
void updata(int rt,int p, int val)
{
	if(tree[rt].l==tree[rt].r)
	{
	  tree[rt].sum=val;
	  return ;
	}
	int mid=(tree[rt].l+tree[rt].r)>>1;
	if(p<=mid) updata(rt<<1,p,val);
    if(p>mid ) updata(rt<<1|1,p,val);
    tree[rt].sum=tree[rt<<1].sum+tree[rt<<1|1].sum;
}
int query(int rt,int l,int r)
{
	if(l<=tree[rt].l&&tree[rt].r<=r)
	{
		return tree[rt].sum;
	}
	int mid=(tree[rt].l+tree[rt].r)>>1;
	int ans=0;
	if(l<=mid) ans+=query(rt<<1,l,r);
	if(r>mid ) ans+=query(rt<<1|1,l,r);
	return ans;
}
int cal(int u,int v)
{
	int tp1=top[u];int tp2=top[v];
	int ans=0;
	while(tp1!=tp2)
	{
	  if(dep[tp1]<dep[tp2]) swap(tp1,tp2),swap(u,v);
	  ans+=query(1,pos[tp1],pos[u]);
	  u=fa[u];
	  tp1=top[u];

	}
	if(u==v)return ans+query(1,pos[u],pos[u]);
	if(dep[u]>dep[v]) swap(u,v);
	return ans+query(1,pos[u],pos[v]);
}
ll dp[301];
const int mod = 1e9+7;
struct edg
{
  int x;ll f;
}id[N];
int cmp(edg a,edg b){return a.f<b.f;}
int main()
{
	ios::sync_with_stdio(false);
   memset(head,-1,sizeof(head));
   int n,q;num=cnt=0;
   int u,v;
   cin>>n>>q;
   for(int i=1;i<n;i++)
   {
   	 cin>>u>>v;
   	 add(u,v);
   }
   dfs1(1,0,0);dfs2(1,1);
   build(1,1,num);
   int k,m,r;
   while(q--)
   {
   	 cin>>k>>m>>r;
   	 for(int i=1;i<=k;i++)
   	 {
   	 	cin>>id[i].x;
   	 	updata(1,pos[id[i].x],1);
	 }
	 for(int i=1;i<=k;i++)
	 {
	 	id[i].f=cal(id[i].x,r)-1;
	 	//cout<<id[i].x<<' '<<id[i].f<<endl;
	 }
	 sort(id+1,id+k+1,cmp);
   	 for(int i=1;i<=k;i++)
   	 {
   	 	updata(1,pos[id[i].x],0);
	 }memset(dp,0,sizeof(dp));
	 if(id[k].f>m) {cout<<0<<endl;}
	 else
	 {

	   dp[1]=1;
	   ll ans=0;
	   for(int i=2;i<=k;i++)
	   {
	     for(int j=min(m,i);j>=1;j--)
		 {
		   if(j<=id[i].f) dp[j]=0;
		   else dp[j]=(dp[j]*(j-id[i].f)%mod+dp[j-1])%mod;
	     }
	   }
	   for(int i=1;i<=m;i++) ans=(ans+dp[i])%mod;
	   cout<<ans<<endl;
	 }

   }
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define sf(a) scanf("%d",&a)
const int N = 100005;
struct node
{
	int l,r,sum;
}tree[N<<2];
//vector<int > e[N];
struct Edge{
    int v,nxt;
    Edge(){}
    Edge(int v,int nxt):v(v),nxt(nxt){}
}edge[N<<1];
int head[N],son[N],fa[N],top[N],siz[N],dep[N],num,cnt,pos[N],f[N];
void add(int u,int v){
    edge[cnt]=Edge(v,head[u]);
    head[u]=cnt++;
    edge[cnt]=Edge(u,head[v]);
    head[v]=cnt++;
}
void dfs1(int u,int Fa,int d)
{
	siz[u]=1;son[u]=0;dep[u]=d;
	fa[u]=Fa;
	for(int i=head[u];i!=-1;i=edge[i].nxt)
	{
	  int v=edge[i].v;
	  if(v==Fa) continue;
	  dfs1(v,u,d+1); siz[u]+=siz[v];
	  if(siz[v]>siz[son[u]])  son[u]=v;
	}
}
void dfs2(int u,int tp)
{
   top[u]=tp;
   pos[u]=++num;
   if(son[u]) dfs2(son[u],tp);
   for(int i=head[u];i!=-1;i=edge[i].nxt)
   {
	 int v=edge[i].v;
   	 if(v!=fa[u]&&v!=son[u]) dfs2(v,v);
   }

}
void build(int rt,int l,int r)
{
   tree[rt].l=l,tree[rt].r=r;tree[rt].sum=0;
   if(l==r)
   {
   	 return ;
   }
   int mid=(l+r)>>1;
   build(rt<<1,l,mid);
   build(rt<<1|1,mid+1,r);
}
void updata(int rt,int p, int val)
{
	if(tree[rt].l==tree[rt].r)
	{
	  tree[rt].sum=val;
	  return ;
	}
	int mid=(tree[rt].l+tree[rt].r)>>1;
	if(p<=mid) updata(rt<<1,p,val);
    if(p>mid ) updata(rt<<1|1,p,val);
    tree[rt].sum=tree[rt<<1].sum+tree[rt<<1|1].sum;
}
int query(int rt,int l,int r)
{
	if(l<=tree[rt].l&&tree[rt].r<=r)
	{
		return tree[rt].sum;
	}
	int mid=(tree[rt].l+tree[rt].r)>>1;
	int ans=0;
	if(l<=mid) ans+=query(rt<<1,l,r);
	if(r>mid ) ans+=query(rt<<1|1,l,r);
	return ans;
}
int cal(int u,int v)
{
	int tp1=top[u];int tp2=top[v];
	int ans=0;
	while(tp1!=tp2)
	{
	  if(dep[tp1]<dep[tp2]) swap(tp1,tp2),swap(u,v);
	  ans+=query(1,pos[tp1],pos[u]);
	  u=fa[u];
	  tp1=top[u];

	}
	if(u==v)return ans+query(1,pos[u],pos[u]);
	if(dep[u]>dep[v]) swap(u,v);
	return ans+query(1,pos[u],pos[v]);
}
ll dp[301];
const int mod = 1e9+7;
struct edg
{
  int x,f;
}id[N];
int cmp(edg a,edg b){return a.f<b.f;}
int main()
{
	ios::sync_with_stdio(false);
   memset(head,-1,sizeof(head));
   int n,q;num=cnt=0;
   int u,v;
   sf(n),sf(q);
   for(int i=1;i<n;i++)
   {
   	 sf(u),sf(v);
   	 add(u,v);
   }
   dfs1(1,0,0);dfs2(1,1);
   build(1,1,num);
   int k,m,r;
   while(q--)
   {
   	 sf(k),sf(m),sf(r);
   	 for(int i=1;i<=k;i++)
   	 {
   	 	sf(id[i].x);
   	 	updata(1,pos[id[i].x],1);
	 }
	 for(int i=1;i<=k;i++)
	 {
	 	id[i].f=cal(id[i].x,r)-1;
	 	//cout<<id[i].x<<' '<<id[i].f<<endl;
	 }
	 sort(id+1,id+k+1,cmp);
   	 for(int i=1;i<=k;i++)
   	 {
   	 	updata(1,pos[id[i].x],0);
	 }memset(dp,0,sizeof(dp));
	 if(id[k].f>m) {cout<<0<<endl;}
	 else
	 {

	   dp[1]=1;
	   ll ans=0;
	   for(int i=2;i<=k;i++)
	   {
	     for(int j=min(m,i);j>=1;j--)
		 {
		   if(j<=id[i].f) dp[j]=0;
		   else dp[j]=(dp[j]*(j-id[i].f)%mod+dp[j-1])%mod;
	     }
	   }
	   for(int i=1;i<=m;i++) ans=(ans+dp[i])%mod;
	   cout<<ans<<endl;
	 }

   }
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define sf(a) scanf("%d",&a)
const int N = 100005;
struct node
{
	int l,r,sum;
}tree[N<<2];
//vector<int > e[N];
struct Edge{
    int v,nxt;
    Edge(){}
    Edge(int v,int nxt):v(v),nxt(nxt){}
}edge[N<<1];
int head[N],son[N],fa[N],top[N],siz[N],dep[N],num,cnt,pos[N],f[N];
void add(int u,int v){
    edge[cnt]=Edge(v,head[u]);
    head[u]=cnt++;
    edge[cnt]=Edge(u,head[v]);
    head[v]=cnt++;
}
void dfs1(int u,int Fa,int d)
{
	siz[u]=1;son[u]=0;dep[u]=d;
	fa[u]=Fa;
	for(int i=head[u];i!=-1;i=edge[i].nxt)
	{
	  int v=edge[i].v;
	  if(v==Fa) continue;
	  dfs1(v,u,d+1); siz[u]+=siz[v];
	  if(siz[v]>siz[son[u]])  son[u]=v;
	}
}
void dfs2(int u,int tp)
{
   top[u]=tp;
   pos[u]=++num;
   if(son[u]) dfs2(son[u],tp);
   for(int i=head[u];i!=-1;i=edge[i].nxt)
   {
	 int v=edge[i].v;
   	 if(v!=fa[u]&&v!=son[u]) dfs2(v,v);
   }

}
void build(int rt,int l,int r)
{
   tree[rt].l=l,tree[rt].r=r;tree[rt].sum=0;
   if(l==r)
   {
   	 return ;
   }
   int mid=(l+r)>>1;
   build(rt<<1,l,mid);
   build(rt<<1|1,mid+1,r);
}
void updata(int rt,int p, int val)
{
	if(tree[rt].l==tree[rt].r)
	{
	  tree[rt].sum=val;
	  return ;
	}
	int mid=(tree[rt].l+tree[rt].r)>>1;
	if(p<=mid) updata(rt<<1,p,val);
    if(p>mid ) updata(rt<<1|1,p,val);
    tree[rt].sum=tree[rt<<1].sum+tree[rt<<1|1].sum;
}
int query(int rt,int l,int r)
{
	if(l<=tree[rt].l&&tree[rt].r<=r)
	{
		return tree[rt].sum;
	}
	int mid=(tree[rt].l+tree[rt].r)>>1;
	int ans=0;
	if(l<=mid) ans+=query(rt<<1,l,r);
	if(r>mid ) ans+=query(rt<<1|1,l,r);
	return ans;
}
int cal(int u,int v)
{
	int tp1=top[u];int tp2=top[v];
	int ans=0;
	while(tp1!=tp2)
	{
	  if(dep[tp1]<dep[tp2]) swap(tp1,tp2),swap(u,v);
	  ans+=query(1,pos[tp1],pos[u]);
	  u=fa[u];
	  tp1=top[u];

	}
	if(u==v)return ans+query(1,pos[u],pos[u]);
	if(dep[u]>dep[v]) swap(u,v);
	return ans+query(1,pos[u],pos[v]);
}
ll dp[301];
const int mod = 1e9+7;
struct edg
{
  int x,f;
}id[N];
int cmp(edg a,edg b){return a.f<b.f;}
int main()
{
	ios::sync_with_stdio(false);
   memset(head,-1,sizeof(head));
   int n,q;num=cnt=0;
   int u,v;
   sf(n),sf(q);
   for(int i=1;i<n;i++)
   {
   	 sf(u),sf(v);
   	 add(u,v);
   }
   dfs1(1,0,0);dfs2(1,1);
   build(1,1,num);
   int k,m,r;
   while(q--)
   {
   	 sf(k),sf(m),sf(r);
   	 for(int i=1;i<=k;i++)
   	 {
   	 	sf(id[i].x);
   	 	updata(1,pos[id[i].x],1);
	 }
	 for(int i=1;i<=k;i++)
	 {
	 	id[i].f=cal(id[i].x,r)-1;
	 	//cout<<id[i].x<<' '<<id[i].f<<endl;
	 }
	 sort(id+1,id+k+1,cmp);
   	 for(int i=1;i<=k;i++)
   	 {
   	 	updata(1,pos[id[i].x],0);
	 }memset(dp,0,sizeof(dp));
	 if(id[k].f>=m) {cout<<0<<endl;}
	 else
	 {

	   dp[0]=1;
	   ll ans=0;
	   for(int i=1;i<=k;i++)
	   {
	     for(int j=min(m,i);~j;j--)
		 {
		   if(j<=id[i].f) dp[j]=0;
		   else dp[j]=(dp[j]*(j-id[i].f)%mod+dp[j-1])%mod;
	     }
	   }
	   for(int i=1;i<=m;i++) ans=(ans+dp[i])%mod;
	   cout<<ans<<endl;
	 }

   }
}


#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
struct team
{
 	string name;
	int pt,g,lg;
}t[50];
int n;
void addpt(string nm,int mg,int tg)
{
 	for(int i=0;i<n;i++)
 	{
	 	if(nm==t[i].name)
		{
		 	if (mg>tg) t[i].pt+=3;
			else if (mg==tg) t[i].pt+=1;
			t[i].g+=mg;
			t[i].lg+=tg;
			break;
		}
 	}
}
bool comp(team a,team b)
{
 	if(a.pt!=b.pt) return a.pt>b.pt;
	else if (a.pt-a.lg!=b.pt-b.lg) return a.pt-a.lg>b.pt-b.lg;
	else return a.g>b.g;
}
bool comp2(team a,team b)
{
 	 return a.name<b.name;
}
int main ()
{
	string a,b,k;
 	int c,d;
	cin>>n;
	cin.ignore();
	for(int i=0;i<n;i++)
		cin>>t[i].name;
	for(int i=0;i<n*(n-1)/2;i++)
	{
		cin.ignore();
	 	getline(cin,a,'-');
		getline(cin,b,' ');
		scanf("%d:%d",&c,&d);
		addpt(a,c,d);
		addpt(b,d,c);
	}
	sort(t,t+n,comp);
	sort(t,t+n/2,comp2);
	for(int i=0;i<n/2;i++)
		cout<<t[i].name<<endl;
	system("pause");
	return 0;
}


#include<iostream>
#include<string>
#include<algorithm>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
string names[50];
int pt[50],wg[50],lg[50];
int n;
bool comp(string a,string b)
{
 	int ai=find(names,names+n,a)-names;
	int bi=find(names,names+n,b)-names;
	if (pt[ai]!=pt[bi]) return pt[ai]>pt[bi];
	else if (pt[ai]-lg[ai]!=pt[bi]-lg[bi]) return pt[ai]-lg[ai]>pt[bi]-lg[bi];
	else return wg[ai]>wg[bi];
}
int main ()
{
 	string a,b,inp;
 	int sa,sb,ai,bi;
	cin>>n;
	rep(i,n) cin>>names[i];
	rep(i,n*(n-1)/2)
	{
	 	cin>>inp>>sa>>sb;
		a=inp.substr(0,inp.find('-'));
		b=inp.substr(inp.find('-')+1);
		ai=find(names,names+n,a)-names;
		bi=find(names,names+n,b)-names;
		if(sa>sb)
 	 	 	pt[ai]+=3;
	    else
 	 	{
		 	pt[ai]++;
			pt[bi]++;
		}
		wg[ai]+=sa;lg[ai]+=sb;
		wg[bi]+=sb;lg[bi]+=sa;
	}
	sort(names,names+n,comp);
	rep(i,n/2) cout<<names[i]<<endl;
	return 0;
}


#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
struct team
{
 	string name;
	int pt,g,lg;
}t[50];
int n;
void addpt(string nm,int mg,int tg)
{
 	for(int i=0;i<n;i++)
 	{
	 	if(nm==t[i].name)
		{
		 	if (mg>tg) t[i].pt+=3;
			else if (mg==tg) t[i].pt+=1;
			t[i].g+=mg;
			t[i].lg+=tg;
			break;
		}
 	}
}
bool comp(team a,team b)
{
 	if(a.pt!=b.pt) return a.pt>b.pt;
	else if (a.pt-a.lg!=b.pt-b.lg) return a.pt-a.lg>b.pt-b.lg;
	else return a.g>b.g;
}
bool comp2(team a,team b)
{
 	 return a.name<b.name;
}
int main ()
{
	string a,b,k;
 	int c,d;
	cin>>n;
	cin.ignore();
	for(int i=0;i<n;i++)
		cin>>t[i].name;
	for(int i=0;i<n*(n-1)/2;i++)
	{
		cin.ignore();
	 	getline(cin,a,'-');
		getline(cin,b,' ');
		scanf("%d:%d",&c,&d);
		addpt(a,c,d);
		addpt(b,d,c);
	}
	sort(t,t+n,comp);
	sort(t,t+n/2,comp2);
	for(int i=0;i<n/2;i++)
		cout<<t[i].name<<endl;
	return 0;
}


#include<iostream>
#include<string>
#include<algorithm>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
string names[50],compare[50];
int pt[50],wg[50],lg[50];
int n;
bool comp(string a,string b)
{
 	int ai=find(names,names+n,a)-names;
	int bi=find(names,names+n,b)-names;
	if (pt[ai]!=pt[bi]) return pt[ai]>pt[bi];
	else if (pt[ai]-lg[ai]!=pt[bi]-lg[bi]) return pt[ai]-lg[ai]>pt[bi]-lg[bi];
	else return wg[ai]>wg[bi];
}
bool comp2(string a,string b)
{
 	 return a<b;
}
int main ()
{
 	string a,b,inp;
 	int sa,sb,ai,bi;
 	char ch;
	cin>>n;
	rep(i,n) cin>>names[i];
	rep(i,n*(n-1)/2)
	{
	 	cin>>inp>>sa>>ch>>sb;
		a=inp.substr(0,inp.find('-'));
		b=inp.substr(inp.find('-')+1);
		ai=find(names,names+n,a)-names;
		bi=find(names,names+n,b)-names;
		if(sa>sb)
 	 	 	pt[ai]+=3;
	    else if(sa==sb)
 	 	{
		 	pt[ai]++;
			pt[bi]++;
		}
		else
			 pt[bi]+=3;
		wg[ai]+=sa;lg[ai]+=sb;
		wg[bi]+=sb;lg[bi]+=sa;
	}
	rep(i,n) compare[i]=names[i];
	sort(compare,compare+n,comp);
	sort(compare,compare+n/2,comp2);
	rep(i,n/2) cout<<compare[i]<<endl;
	return 0;
}


#include<iostream>
#include<string>
#include<algorithm>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
string names[50];
int pt[50],wg[50],lg[50];
int n;
bool comp(string a,string b)
{
    int ai=find(names,names+n,a)-names;
    int bi=find(names,names+n,b)-names;
    if (pt[ai]!=pt[bi]) return pt[ai]>pt[bi];
    else if (pt[ai]-lg[ai]!=pt[bi]-lg[bi]) return pt[ai]-lg[ai]>pt[bi]-lg[bi];
    else return wg[ai]>wg[bi];
}
bool comp2(string a,string b)
{
     return a<b;
}
int main ()
{
    string a,b,inp;
    int sa,sb,ai,bi;
    char ch;
    cin>>n;
    rep(i,n) cin>>names[i];
    rep(i,n*(n-1)/2)
    {
        cin>>inp>>sa>>ch>>sb;
        a=inp.substr(0,inp.find('-'));
        b=inp.substr(inp.find('-')+1);
        ai=find(names,names+n,a)-names;
        bi=find(names,names+n,b)-names;
        if(sa>sb)
            pt[ai]+=3;
        else if(sa==sb)
        {
            pt[ai]++;
            pt[bi]++;
        }
        else
             pt[bi]+=3;
        wg[ai]+=sa;lg[ai]+=sb;
        wg[bi]+=sb;lg[bi]+=sa;
    }
    sort(names,names+n,comp);
    sort(names,names+n/2,comp2);
    rep(i,n/2) cout<<names[i]<<endl;
    return 0;
}


#include<iostream>
#include<string>
#include<algorithm>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
string names[50],compare[50];
int pt[50],wg[50],lg[50];
int n;
bool comp(string a,string b)
{
 	int ai=find(names,names+n,a)-names;
	int bi=find(names,names+n,b)-names;
	if (pt[ai]!=pt[bi]) return pt[ai]>pt[bi];
	else if (pt[ai]-lg[ai]!=pt[bi]-lg[bi]) return pt[ai]-lg[ai]>pt[bi]-lg[bi];
	else return wg[ai]>wg[bi];
}
bool comp2(string a,string b)
{
 	 return a<b;
}
int main ()
{
 	string a,b,inp;
 	int sa,sb,ai,bi;
 	char ch;
	cin>>n;
	rep(i,n) cin>>names[i];
	rep(i,n*(n-1)/2)
	{
	 	cin>>inp>>sa>>ch>>sb;
		a=inp.substr(0,inp.find('-'));
		b=inp.substr(inp.find('-')+1);
		ai=find(names,names+n,a)-names;
		bi=find(names,names+n,b)-names;
		if(sa>sb)
 	 	 	pt[ai]+=3;
	    else if(sa==sb)
 	 	{
		 	pt[ai]++;
			pt[bi]++;
		}
		else
			 pt[bi]+=3;
		wg[ai]+=sa;lg[ai]+=sb;
		wg[bi]+=sb;lg[bi]+=sa;
	}
	rep(i,n) compare[i]=names[i];
	sort(compare,compare+n,comp);
	sort(compare,compare+n/2,comp2);
	rep(i,n/2) cout<<names[i]<<endl;
	return 0;
}


#include<iostream>
#include<string>
#include<algorithm>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
string names[50],compare[50];
int pt[50],wg[50],lg[50];
int n;
bool comp(string a,string b)
{
 	int ai=find(names,names+n,a)-names;
	int bi=find(names,names+n,b)-names;
	if (pt[ai]!=pt[bi]) return pt[ai]>pt[bi];
	else if (wg[ai]-lg[ai]!=wg[bi]-lg[bi]) return wg[ai]-lg[ai]>wg[bi]-lg[bi];
	else return wg[ai]>wg[bi];
}
bool comp2(string a,string b)
{
 	 return a<b;
}
int main ()
{
 	string a,b,inp;
 	int sa,sb,ai,bi;
 	char ch;
	cin>>n;
	rep(i,n) cin>>names[i];
	rep(i,n*(n-1)/2)
	{
	 	cin>>inp>>sa>>ch>>sb;
		a=inp.substr(0,inp.find('-'));
		b=inp.substr(inp.find('-')+1);
		ai=find(names,names+n,a)-names;
		bi=find(names,names+n,b)-names;
		if(sa>sb)
 	 	 	pt[ai]+=3;
	    else if(sa==sb)
 	 	{
		 	pt[ai]++;
			pt[bi]++;
		}
		else
			 pt[bi]+=3;
		wg[ai]+=sa;lg[ai]+=sb;
		wg[bi]+=sb;lg[bi]+=sa;
	}
	rep(i,n) compare[i]=names[i];
	sort(compare,compare+n,comp);
	sort(compare,compare+n/2,comp2);
	rep(i,n/2) cout<<compare[i]<<endl;
	return 0;
}


#include <cstdio>
#include <cmath>
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <vector>
#include <utility>
#include <stack>
#include <queue>
#include <map>

#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define pi 2*acos(0.0)
#define eps 1e-9
#define PII pair<int,int>
#define PDD pair<double,double>
#define LL long long
#define INF 1000000000

using namespace std;

string tim[111];
char mska[50],mskb[50],dummy;
int N,x,ida,idb,a,b,score[111];
pair<string,int> ans[111];

bool cf(pair<string,int> i,pair<string,int> j)
{
	if(i.se!=j.se) return(i.se>j.se);
	return(i.fi<j.fi);
}

int main()
{
	scanf("%d",&N);
	for(x=0;x<N;x++)
	{
		scanf("%s",mska);
		tim[x]=mska;
	} getchar();
	sort(tim,tim+N);
	memset(score,0,sizeof(score));

	for(x=1;2*x<=N*(N-1);x++)
	{
		scanf("%[^-]%c%s %d:%d",mska,&dummy,mskb,&a,&b);
		ida=lower_bound(tim,tim+N,mska)-tim;
		idb=lower_bound(tim,tim+N,mskb)-tim;

		if(a==b) score[ida]++,score[idb]++; else
		if(a>b) score[ida]+=3; else score[idb]+=3;
	}

	for(x=0;x<N;x++) ans[x]=mp(tim[x],score[x]);
	sort(ans,ans+N,cf);
	sort(ans,ans+(N/2));

	for(x=0;2*x<N;x++) printf("%s\n",ans[x].fi.c_str());
	return 0;
}


#include <cstdio>
#include <cmath>
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <vector>
#include <utility>
#include <stack>
#include <queue>
#include <map>

#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define pi 2*acos(0.0)
#define eps 1e-9
#define PII pair<int,int>
#define PDD pair<double,double>
#define LL long long
#define INF 1000000000

using namespace std;

string tim[111];
char mska[50],mskb[50],dummy;
int N,x,ida,idb,a,b,score[111];
pair<string,int> ans[111];

bool cfscore(pair<string,int> i,pair<string,int> j)
{
	if(i.se!=j.se) return(i.se>j.se);

	string a=i.fi,b=j.fi;
	for(int t=0;t<a.size();t++) if((a[t]>='a')&&(a[t]<='z')) a[t]^=32;
	for(int t=0;t<b.size();t++) if((b[t]>='a')&&(b[t]<='z')) b[t]^=32;
	return(a<b);
}

bool cfname(pair<string,int> i,pair<string,int> j)
{
	string a=i.fi,b=j.fi;
	for(int t=0;t<a.size();t++) if((a[t]>='a')&&(a[t]<='z')) a[t]^=32;
	for(int t=0;t<b.size();t++) if((b[t]>='a')&&(b[t]<='z')) b[t]^=32;
	return(a<b);
}

int main()
{
	scanf("%d",&N);
	for(x=0;x<N;x++)
	{
		scanf("%s",mska);
		tim[x]=mska;
	}
	sort(tim,tim+N);
	memset(score,0,sizeof(score));

	for(x=1;2*x<=N*(N-1);x++)
	{
		getchar();
		scanf("%[^-]%c%s %d:%d",mska,&dummy,mskb,&a,&b);
		ida=lower_bound(tim,tim+N,mska)-tim;
		idb=lower_bound(tim,tim+N,mskb)-tim;

		if(a==b) score[ida]++,score[idb]++; else
		if(a>b) score[ida]+=3; else score[idb]+=3;
	}

	for(x=0;x<N;x++) ans[x]=mp(tim[x],score[x]);
	sort(ans,ans+N,cfscore);
	sort(ans,ans+(N/2),cfname);

	for(x=0;2*x<N;x++) printf("%s\n",ans[x].fi.c_str());
	return 0;
}


#include <cstdio>
#include <cmath>
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <vector>
#include <utility>
#include <stack>
#include <queue>
#include <map>

#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define pi 2*acos(0.0)
#define eps 1e-9
#define PII pair<int,int>
#define PDD pair<double,double>
#define LL long long
#define INF 1000000000

using namespace std;

string tim[111];
char mska[50],mskb[50],dummy;
int N,x,ida,idb,a,b,score[111];
pair<string,int> ans[111];

bool cf(pair<string,int> i,pair<string,int> j)
{
	if(i.se!=j.se) return(i.se>j.se);

	string a=i.fi,b=j.fi;
	for(int t=0;t<a.size();t++) if((a[t]>='a')&&(a[t]<='z')) a[t]^=32;
	for(int t=0;t<b.size();t++) if((b[t]>='a')&&(b[t]<='z')) b[t]^=32;
	return(a<b);
}

int main()
{
	scanf("%d",&N);
	for(x=0;x<N;x++)
	{
		scanf("%s",mska);
		tim[x]=mska;
	}
	sort(tim,tim+N);
	memset(score,0,sizeof(score));

	for(x=1;2*x<=N*(N-1);x++)
	{
		getchar();
		scanf("%[^-]%c%s %d:%d",mska,&dummy,mskb,&a,&b);
		ida=lower_bound(tim,tim+N,mska)-tim;
		idb=lower_bound(tim,tim+N,mskb)-tim;

		if(a==b) score[ida]++,score[idb]++; else
		if(a>b) score[ida]+=3; else score[idb]+=3;
	}

	for(x=0;x<N;x++) ans[x]=mp(tim[x],score[x]);
	sort(ans,ans+N,cf);

	for(x=0;2*x<N;x++) printf("%s\n",ans[x].fi.c_str());
	return 0;
}


#include <cstdio>
#include <cmath>
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <vector>
#include <utility>
#include <stack>
#include <queue>
#include <map>

#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define pi 2*acos(0.0)
#define eps 1e-9
#define PII pair<int,int>
#define PDD pair<double,double>
#define LL long long
#define INF 1000000000

using namespace std;

typedef struct{string name;int poin,scored,missed;} team;

string tim[111];
char mska[50],mskb[50],dummy;
int N,x,ida,idb,a,b;
team ans[111];

bool cfscore(team i,team j)
{
	if(i.poin!=j.poin) return(i.poin>j.poin);
	if(i.scored-i.missed!=j.scored-j.missed) return(i.scored-i.missed>j.scored-j.missed);
	if(i.scored!=j.scored) return(i.scored>j.scored);
	return(i.name<j.name);
}

bool cfname(team i,team j)
{
	return(i.name<j.name);
}

int main()
{
	scanf("%d",&N);
	for(x=0;x<N;x++)
	{
		scanf("%s",mska);
		tim[x]=mska;
	}
	sort(tim,tim+N);
	for(x=0;x<N;x++) ans[x]=(team){tim[x],0,0,0};

	for(x=1;2*x<=N*(N-1);x++)
	{
		getchar();
		scanf("%[^-]%c%s %d:%d",mska,&dummy,mskb,&a,&b);
		ida=lower_bound(tim,tim+N,mska)-tim;
		idb=lower_bound(tim,tim+N,mskb)-tim;
		ans[ida].scored+=a,ans[ida].missed+=b;
		ans[idb].scored+=b,ans[idb].missed+=a;

		if(a==b) ans[ida].poin++,ans[idb].poin++; else
		if(a>b) ans[ida].poin+=3; else ans[idb].poin+=3;
	}

	sort(ans,ans+N,cfscore);
	sort(ans,ans+(N/2),cfname);

	for(x=0;2*x<N;x++) printf("%s\n",ans[x].name.c_str());
	return 0;
}


#include <cstdio>
#include <cmath>
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <vector>
#include <utility>
#include <stack>
#include <queue>
#include <map>

#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define pi 2*acos(0.0)
#define eps 1e-9
#define PII pair<int,int>
#define PDD pair<double,double>
#define LL long long
#define INF 1000000000

using namespace std;

string tim[111];
char mska[50],mskb[50],dummy;
int N,x,ida,idb,a,b,score[111];
pair<string,int> ans[111];

bool cf(pair<string,int> i,pair<string,int> j)
{
	if(i.se!=j.se) return(i.se>j.se);
	return(i.fi<j.fi);
}

int main()
{
	scanf("%d",&N);
	for(x=0;x<N;x++)
	{
		scanf("%s",mska);
		tim[x]=mska;
	} getchar();
	sort(tim,tim+N);
	memset(score,0,sizeof(score));

	for(x=1;2*x<=N*(N-1);x++)
	{
		scanf("%[^-]%c%s %d:%d",mska,&dummy,mskb,&a,&b);
		if((N==4)&&(strlen(mska)>1)) printf("%s-%s %d:%d\n",mska,mskb,a,b);
		ida=lower_bound(tim,tim+N,mska)-tim;
		idb=lower_bound(tim,tim+N,mskb)-tim;

		if(a==b) score[ida]++,score[idb]++; else
		if(a>b) score[ida]+=3; else score[idb]+=3;
	}

	for(x=0;x<N;x++) ans[x]=mp(tim[x],score[x]);
	sort(ans,ans+N,cf);
	sort(ans,ans+(N/2));

	for(x=0;2*x<N;x++) printf("%s\n",ans[x].fi.c_str());
	return 0;
}


#include <cstdio>
#include <cmath>
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <vector>
#include <utility>
#include <stack>
#include <queue>
#include <map>

#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define pi 2*acos(0.0)
#define eps 1e-9
#define PII pair<int,int>
#define PDD pair<double,double>
#define LL long long
#define INF 1000000000

using namespace std;

string tim[111];
char mska[50],mskb[50],dummy;
int N,x,ida,idb,a,b,score[111];
pair<string,int> ans[111];

bool cf(pair<string,int> i,pair<string,int> j)
{
	if(i.se!=j.se) return(i.se>j.se);
	return(i.fi<j.fi);
}

int main()
{
	scanf("%d",&N);
	for(x=0;x<N;x++)
	{
		scanf("%s",mska);
		tim[x]=mska;
	}
	sort(tim,tim+N);
	memset(score,0,sizeof(score));

	for(x=1;2*x<=N*(N-1);x++)
	{
		getchar();
		scanf("%[^-]%c%s %d:%d",mska,&dummy,mskb,&a,&b);
		if(N==6) printf("%s-%s %d:%d\n",mska,mskb,a,b);
		ida=lower_bound(tim,tim+N,mska)-tim;
		idb=lower_bound(tim,tim+N,mskb)-tim;

		if(a==b) score[ida]++,score[idb]++; else
		if(a>b) score[ida]+=3; else score[idb]+=3;
	}

	for(x=0;x<N;x++) ans[x]=mp(tim[x],score[x]);
	sort(ans,ans+N,cf);
	sort(ans,ans+(N/2));

	for(x=0;2*x<N;x++) printf("%s\n",ans[x].fi.c_str());
	return 0;
}


#include <cstdio>
#include <cmath>
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <vector>
#include <utility>
#include <stack>
#include <queue>
#include <map>

#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define pi 2*acos(0.0)
#define eps 1e-9
#define PII pair<int,int>
#define PDD pair<double,double>
#define LL long long
#define INF 1000000000

using namespace std;

string tim[111];
char mska[50],mskb[50],dummy;
int N,x,ida,idb,a,b,score[111];
pair<string,int> ans[111];

bool cf(pair<string,int> i,pair<string,int> j)
{
	if(i.se!=j.se) return(i.se>j.se);
	return(i.fi<j.fi);
}

int main()
{
	scanf("%d",&N);
	for(x=0;x<N;x++)
	{
		scanf("%s",mska);
		tim[x]=mska;
	}
	sort(tim,tim+N);
	memset(score,0,sizeof(score));

	for(x=1;2*x<=N*(N-1);x++)
	{
		getchar();
		scanf("%[^-]%c%s %d:%d",mska,&dummy,mskb,&a,&b);
		if((N==6)&&(x>6)) printf("%s-%s %d:%d\n",mska,mskb,a,b);
		ida=lower_bound(tim,tim+N,mska)-tim;
		idb=lower_bound(tim,tim+N,mskb)-tim;

		if(a==b) score[ida]++,score[idb]++; else
		if(a>b) score[ida]+=3; else score[idb]+=3;
	}

	for(x=0;x<N;x++) ans[x]=mp(tim[x],score[x]);
	sort(ans,ans+N,cf);
	sort(ans,ans+(N/2));

	for(x=0;2*x<N;x++) printf("%s\n",ans[x].fi.c_str());
	return 0;
}


#include <cstdio>
#include <cmath>
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <vector>
#include <utility>
#include <stack>
#include <queue>
#include <map>

#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define pi 2*acos(0.0)
#define eps 1e-9
#define PII pair<int,int>
#define PDD pair<double,double>
#define LL long long
#define INF 1000000000

using namespace std;

string tim[111];
char mska[50],mskb[50],dummy;
int N,x,ida,idb,a,b,score[111];
pair<string,int> ans[111];

bool cfscore(pair<string,int> i,pair<string,int> j)
{
	if(i.se!=j.se) return(i.se>j.se);

	string a=i.fi,b=j.fi;
	for(int t=0;t<a.size();t++) if((a[t]>='a')&&(a[t]<='z')) a[t]^=32;
	for(int t=0;t<b.size();t++) if((b[t]>='a')&&(b[t]<='z')) b[t]^=32;
	return(a<b);
}

bool cfname(pair<string,int> i,pair<string,int> j)
{
	string a=i.fi,b=j.fi;
	for(int t=0;t<a.size();t++) if((a[t]>='a')&&(a[t]<='z')) a[t]^=32;
	for(int t=0;t<b.size();t++) if((b[t]>='a')&&(b[t]<='z')) b[t]^=32;
	return(a<b);
}

int main()
{
	scanf("%d",&N);
	for(x=0;x<N;x++)
	{
		scanf("%s",mska);
		tim[x]=mska;
	}
	sort(tim,tim+N);
	memset(score,0,sizeof(score));

	for(x=1;2*x<=N*(N-1);x++)
	{
		getchar();
		scanf("%[^-]%c%s %d:%d",mska,&dummy,mskb,&a,&b);
		ida=lower_bound(tim,tim+N,mska)-tim;
		idb=lower_bound(tim,tim+N,mskb)-tim;

		if((N==4)&&(x>=6)&&(strlen(mska)>1)) printf("%s-%s %d:%d\n",mska,mskb,a,b);
		if(a==b) score[ida]++,score[idb]++; else
		if(a>b) score[ida]+=3; else score[idb]+=3;
	}

	for(x=0;x<N;x++) ans[x]=mp(tim[x],score[x]);
	sort(ans,ans+N,cfscore);
	sort(ans,ans+(N/2),cfname);

	for(x=0;2*x<N;x++) printf("%s\n",ans[x].fi.c_str());
	return 0;
}


#include <cstdio>
#include <cmath>
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <vector>
#include <utility>
#include <stack>
#include <queue>
#include <map>

#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define pi 2*acos(0.0)
#define eps 1e-9
#define PII pair<int,int>
#define PDD pair<double,double>
#define LL long long
#define INF 1000000000

using namespace std;

string tim[111];
char mska[50],mskb[50],dummy;
int N,x,ida,idb,a,b,score[111];
pair<string,int> ans[111];

bool cfscore(pair<string,int> i,pair<string,int> j)
{
	if(i.se!=j.se) return(i.se>j.se);

	string a=i.fi,b=j.fi;
	for(int t=0;t<a.size();t++) if((a[t]>='a')&&(a[t]<='z')) a[t]^=32;
	for(int t=0;t<b.size();t++) if((b[t]>='a')&&(b[t]<='z')) b[t]^=32;
	return(a<b);
}

bool cfname(pair<string,int> i,pair<string,int> j)
{
	string a=i.fi,b=j.fi;
	for(int t=0;t<a.size();t++) if((a[t]>='a')&&(a[t]<='z')) a[t]^=32;
	for(int t=0;t<b.size();t++) if((b[t]>='a')&&(b[t]<='z')) b[t]^=32;
	return(a<b);
}

int main()
{
	scanf("%d",&N);
	for(x=0;x<N;x++)
	{
		scanf("%s",mska);
		tim[x]=mska;
	}
	sort(tim,tim+N);
	memset(score,0,sizeof(score));

	for(x=1;2*x<=N*(N-1);x++)
	{
		getchar();
		scanf("%[^-]%c%s %d:%d",mska,&dummy,mskb,&a,&b);
		ida=lower_bound(tim,tim+N,mska)-tim;
		idb=lower_bound(tim,tim+N,mskb)-tim;

		if(a==b) score[ida]++,score[idb]++; else
		if(a>b) score[ida]+=3; else score[idb]+=3;
	}

	for(x=0;x<N;x++) ans[x]=mp(tim[x],score[x]);
	sort(ans,ans+N,cfscore);
	score(ans,ans+(N/2),cfname);

	for(x=0;2*x<N;x++) printf("%s\n",ans[x].fi.c_str());
	return 0;
}


#include <cstdio>
#include <cmath>
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <vector>
#include <utility>
#include <stack>
#include <queue>
#include <map>

#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define pi 2*acos(0.0)
#define eps 1e-9
#define PII pair<int,int>
#define PDD pair<double,double>
#define LL long long
#define INF 1000000000

using namespace std;

string tim[111];
char mska[50],mskb[50],dummy;
int N,x,ida,idb,a,b,score[111];
pair<string,int> ans[111];

bool cfscore(pair<string,int> i,pair<string,int> j)
{
	if(i.se!=j.se) return(i.se>j.se);

	string a=i.fi,b=j.fi;
	for(int t=0;t<a.size();t++) if((a[t]>='a')&&(a[t]<='z')) a[t]^=32;
	for(int t=0;t<b.size();t++) if((b[t]>='a')&&(b[t]<='z')) b[t]^=32;
	return(a<b);
}

bool cfscore(pair<string,int> i,pair<string,int> j)
{
	string a=i.fi,b=j.fi;
	for(int t=0;t<a.size();t++) if((a[t]>='a')&&(a[t]<='z')) a[t]^=32;
	for(int t=0;t<b.size();t++) if((b[t]>='a')&&(b[t]<='z')) b[t]^=32;
	return(a<b);
}

int main()
{
	scanf("%d",&N);
	for(x=0;x<N;x++)
	{
		scanf("%s",mska);
		tim[x]=mska;
	}
	sort(tim,tim+N);
	memset(score,0,sizeof(score));

	for(x=1;2*x<=N*(N-1);x++)
	{
		getchar();
		scanf("%[^-]%c%s %d:%d",mska,&dummy,mskb,&a,&b);
		ida=lower_bound(tim,tim+N,mska)-tim;
		idb=lower_bound(tim,tim+N,mskb)-tim;

		if(a==b) score[ida]++,score[idb]++; else
		if(a>b) score[ida]+=3; else score[idb]+=3;
	}

	for(x=0;x<N;x++) ans[x]=mp(tim[x],score[x]);
	sort(ans,ans+N,cfscore);
	score(ans,ans+(N/2),cfname);

	for(x=0;2*x<N;x++) printf("%s\n",ans[x].fi.c_str());
	return 0;
}


#include <cstdio>
#include <cmath>
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <vector>
#include <utility>
#include <stack>
#include <queue>
#include <map>

#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define pi 2*acos(0.0)
#define eps 1e-9
#define PII pair<int,int>
#define PDD pair<double,double>
#define LL long long
#define INF 1000000000

using namespace std;

string tim[111];
char mska[50],mskb[50],dummy;
int N,x,ida,idb,a,b,score[111];
pair<string,int> ans[111];

bool cf(pair<string,int> i,pair<string,int> j)
{
	if(i.se!=j.se) return(i.se>j.se);
	return(i.fi<j.fi);
}

int main()
{
	scanf("%d",&N);
	for(x=0;x<N;x++)
	{
		scanf("%s",mska);
		tim[x]=mska;
	}
	sort(tim,tim+N);
	memset(score,0,sizeof(score));

	for(x=1;2*x<=N*(N-1);x++)
	{
		getchar();
		scanf("%[^-]%c%s %d:%d",mska,&dummy,mskb,&a,&b);
		if((N==6)&&(x>12)) printf("%s-%s %d:%d\n",mska,mskb,a,b);
		ida=lower_bound(tim,tim+N,mska)-tim;
		idb=lower_bound(tim,tim+N,mskb)-tim;

		if(a==b) score[ida]++,score[idb]++; else
		if(a>b) score[ida]+=3; else score[idb]+=3;
	}

	for(x=0;x<N;x++) ans[x]=mp(tim[x],score[x]);
	sort(ans,ans+N,cf);
	sort(ans,ans+(N/2));

	for(x=0;2*x<N;x++) printf("%s\n",ans[x].fi.c_str());
	return 0;
}


#include <cstdio>
#include <cmath>
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <vector>
#include <utility>
#include <stack>
#include <queue>
#include <map>

#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define pi 2*acos(0.0)
#define eps 1e-9
#define PII pair<int,int>
#define PDD pair<double,double>
#define LL long long
#define INF 1000000000

using namespace std;

string tim[111];
char mska[50],mskb[50],dummy;
int N,x,ida,idb,a,b,score[111];
pair<string,int> ans[111];

bool cf(pair<string,int> i,pair<string,int> j)
{
	if(i.se!=j.se) return(i.se>j.se);
	return(i.fi<j.fi);
}

int main()
{
	scanf("%d",&N);
	for(x=0;x<N;x++)
	{
		scanf("%s",mska);
		tim[x]=mska;
	}
	sort(tim,tim+N);
	memset(score,0,sizeof(score));

	for(x=1;2*x<=N*(N-1);x++)
	{
		getchar();
		scanf("%[^-]%c%s %d:%d",mska,&dummy,mskb,&a,&b);
		ida=lower_bound(tim,tim+N,mska)-tim;
		idb=lower_bound(tim,tim+N,mskb)-tim;

		if(a==b) score[ida]++,score[idb]++; else
		if(a>b) score[ida]+=3; else score[idb]+=3;
	}

	for(x=0;x<N;x++) ans[x]=mp(tim[x],score[x]);
	sort(ans,ans+N,cf);
	sort(ans,ans+(N/2));

	for(x=0;2*x<N;x++) printf("%s\n",ans[x].fi.c_str());
	return 0;
}


/***********************************************
* Author - LUONG VAN DO                        *
* A. World Football Cup
* Algorithm
* Time Limit
* *********************************************/
#include <iostream>
#include <stdio.h>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <cmath>
#include <math.h>
#include <cstring>
#include <string.h>
#include <stdlib.h>
#include <algorithm>

#define FileIn(file) freopen(file".inp", "r", stdin)
#define FileOut(file) freopen(file".out", "w", stdout)
#define For(i, a, b) for (int i=a; i<=b; i++)
#define Ford(i,a,b) for (int i=a;i>=b;i--)
#define Rep(i, n) for (int i=0; i<n; i++)
#define pb push_back
#define A first
#define B second
#define PI 3.1415926535897932385
#define uint64 unsigned long long
#define int64 long long
#define INF 500000000
#define maxN 50001
using namespace std;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
struct node{
    int x;
    string n_team;
    node(){}
    node(int xx, string nn_team){
        x = xx; n_team = nn_team;
    }
};
map <string, int> team;
string NAME[55];
node score[55];
int l,n,m;
char s[105];
bool cmp(node c, node d){
    if (c.x!=d.x) return c.x > d.x;
    return c.n_team < d.n_team;
}
void Read_Input(){
    string team1,team2,name;
    int d1,d2;
    scanf("%d",&n);
    For(i,1,n) {
        cin >> name;
        team[name] = i;
        score[i].n_team = name;
    }
    m = (n * (n - 1)) / 2;
    gets(s);
    For(i,1,m) {
        gets(s);
        l = strlen(s);
        int j = 0;
        team1 = team2 = "";
        while (j < l && s[j]!='-') {
            team1+=s[j];
            ++j;
        }
        ++j;
        while (j < l && s[j]!=' ') {
            team2+=s[j];
            ++j;
        }
        ++j;
        sscanf(s+j,"%d:%d",&d1,&d2);
        int i1,i2;
        i1 = team[team1]; i2 = team[team2];
            if (d1 > d2) score[i1].x = score[i1].x + 3;
        else
            if (d1 < d2) score[i2].x = score[i2].x + 3;
        else
            if ( d1 == d2 ) {
                score[i1].x++;
                score[i2].x++;
            }
    }
    sort(score+1,score+n+1,cmp);
    //For(i,1,n) cout<< score[i].x<<" "<<score[i].n_team<<endl;
    int nm = 0;
    For(i,1,n/2) NAME[++nm] = score[i].n_team;
    sort(NAME+1,NAME+nm+1);
    For(i,1,nm) cout<<NAME[i]<<endl;
}
int main(){
    #ifndef ONLINE_JUDGE
    FileIn("exam"); FileOut("exam");
    #endif
    Read_Input();
    return 0;
}


/***********************************************
* Author - LUONG VAN DO                        *
* A. World Football Cup
* Algorithm
* Time Limit
* *********************************************/
#include <iostream>
#include <stdio.h>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <cmath>
#include <math.h>
#include <cstring>
#include <string.h>
#include <stdlib.h>
#include <algorithm>

#define FileIn(file) freopen(file".inp", "r", stdin)
#define FileOut(file) freopen(file".out", "w", stdout)
#define For(i, a, b) for (int i=a; i<=b; i++)
#define Ford(i,a,b) for (int i=a;i>=b;i--)
#define Rep(i, n) for (int i=0; i<n; i++)
#define pb push_back
#define A first
#define B second
#define PI 3.1415926535897932385
#define uint64 unsigned long long
#define int64 long long
#define INF 500000000
#define maxN 50001
using namespace std;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
struct node{
    int x;
    string n_team;
    node(){}
    node(int xx, string nn_team){
        x = xx; n_team = nn_team;
    }
};
map <string, int> team;
string NAME[55];
node score[55];
int l,n,m;
char s[105];
bool cmp(node c, node d){
    if (c.x!=d.x) return c.x < d.x;
    return c.n_team > d.n_team;
}
void Read_Input(){
    string team1,team2,name;
    int d1,d2;
    scanf("%d",&n);
    For(i,1,n) {
        cin >> name;
        team[name] = i;
        score[i].n_team = name;
    }
    m = (n * (n - 1)) / 2;
    gets(s);
    For(i,1,m) {
        gets(s);
        l = strlen(s);
        int i = 0;
        team1 = team2 = "";
        while (i < l && s[i]!='-') {
            team1+=s[i];
            ++i;
        }
        ++i;
        while (i < l && s[i]!=' ') {
            team2+=s[i];
            ++i;
        }
        ++i;
        sscanf(s+i,"%d:%d",&d1,&d2);
        int i1,i2;
        i1 = team[team1]; i2 = team[team2];
            if (d1 > d2) score[i1].x = score[i1].x + 3;
        else
            if (d1 < d2) score[i2].x = score[i2].x + 3;
        else
            if (d1==d2) score[i1].x++, score[i2].x++;
    }
    sort(score+1,score+n+1,cmp);
    //For(i,1,n) cout<< score[i].x<<" "<<score[i].n_team<<endl;
    int nm = 0;
    For(i,n/2+1,n) NAME[++nm] = score[i].n_team;
    sort(NAME+1, NAME+nm+1);
    For(i,1,nm) cout<<NAME[i]<<endl;
}
int main(){
    #ifndef ONLINE_JUDGE
    FileIn("exam"); FileOut("exam");
    #endif
    Read_Input();
    //Solve();
    //Write_Output();
    return 0;
}


/***********************************************
* Author - LUONG VAN DO                        *
* A. World Football Cup
* Algorithm
* Time Limit
* *********************************************/
#include <iostream>
#include <stdio.h>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <cmath>
#include <math.h>
#include <cstring>
#include <string.h>
#include <stdlib.h>
#include <algorithm>

#define FileIn(file) freopen(file".inp", "r", stdin)
#define FileOut(file) freopen(file".out", "w", stdout)
#define For(i, a, b) for (int i=a; i<=b; i++)
#define Ford(i,a,b) for (int i=a;i>=b;i--)
#define Rep(i, n) for (int i=0; i<n; i++)
#define pb push_back
#define A first
#define B second
#define PI 3.1415926535897932385
#define uint64 unsigned long long
#define int64 long long
#define INF 500000000
#define maxN 50001
using namespace std;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
struct node{
    int x, f, s;
    string n_team;
    node(){}
    node(int xx, int ff, int ss, string nn_team){
        x = xx; n_team = nn_team;
        f = ff; s = ss;
    }
};
map <string, int> team;
string NAME[55];
node score[55];
int l,n,m;
char s[105];
bool cmp(node c, node d){
    if (c.x!=d.x) return c.x > d.x;
    if (c.f!=d.f) return c.f > d.f;
    if (c.s!=d.s) return c.s > d.s;
    return c.n_team < d.n_team;
}
void Read_Input(){
    string team1,team2,name;
    int d1,d2;
    scanf("%d",&n);
    For(i,1,n) {
        cin >> name;
        team[name] = i;
        score[i].n_team = name;
    }
    m = (n * (n - 1)) / 2;
    gets(s);
    For(i,1,m) {
        gets(s);
        l = strlen(s);
        int j = 0;
        team1 = team2 = "";
        while (j < l && s[j]!='-') {
            team1+=s[j];
            ++j;
        }
        ++j;
        while (j < l && s[j]!=' ') {
            team2+=s[j];
            ++j;
        }
        ++j;
        sscanf(s+j,"%d:%d",&d1,&d2);
        int i1,i2;
        i1 = team[team1]; i2 = team[team2];
            if (d1 > d2) {
                score[i1].x = score[i1].x + 3;
                score[i1].f+=(d1 - d2);
                score[i1].s+=d1;
                score[i2].s+=d2;
            }
        else
            if (d1 < d2) {
                score[i2].x = score[i2].x + 3;
                score[i2].f+=(d2 - d1);
                score[i2].s+=d2;
                score[i1].s+=d1;
            }
        else
            if ( d1 == d2 ) {
                score[i1].x++;
                score[i2].x++;
                score[i1].s+=d1;
                score[i2].s+=d2;
            }
    }
    sort(score+1,score+n+1,cmp);
    //For(i,1,n) cout<< score[i].x<<" "<<score[i].n_team<<endl;
    int nm = 0;
    For(i,1,n/2) NAME[++nm] = score[i].n_team;
    sort(NAME+1,NAME+nm+1);
    For(i,1,nm) cout<<NAME[i]<<endl;
}
int main(){
    #ifndef ONLINE_JUDGE
    FileIn("exam"); FileOut("exam");
    #endif
    Read_Input();
    return 0;
}


/***********************************************
* Author - LUONG VAN DO                        *
* A. World Football Cup
* Algorithm
* Time Limit
* *********************************************/
#include <iostream>
#include <stdio.h>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <cmath>
#include <math.h>
#include <cstring>
#include <string.h>
#include <stdlib.h>
#include <algorithm>

#define FileIn(file) freopen(file".inp", "r", stdin)
#define FileOut(file) freopen(file".out", "w", stdout)
#define For(i, a, b) for (int i=a; i<=b; i++)
#define Ford(i,a,b) for (int i=a;i>=b;i--)
#define Rep(i, n) for (int i=0; i<n; i++)
#define pb push_back
#define A first
#define B second
#define PI 3.1415926535897932385
#define uint64 unsigned long long
#define int64 long long
#define INF 500000000
#define maxN 50001
using namespace std;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
struct node{
    int x;
    string n_team;
    node(){}
    node(int xx, string nn_team){
        x = xx; n_team = nn_team;
    }
};
map <string, int> team;
string NAME[55];
node score[55];
int l,n,m;
char s[105];
bool cmp(node c, node d){
    if (c.x!=d.x) return c.x > d.x;
    return c.n_team < d.n_team;
}
void Read_Input(){
    string team1,team2,name;
    int d1,d2;
    scanf("%d",&n);
    For(i,1,n) {
        cin >> name;
        team[name] = i;
        score[i].n_team = name;
    }
    m = (n * (n - 1)) / 2;
    gets(s);
    For(i,1,m) {
        gets(s);
        l = strlen(s);
        int i = 0;
        team1 = team2 = "";
        while (i < l && s[i]!='-') {
            team1+=s[i];
            ++i;
        }
        ++i;
        while (i < l && s[i]!=' ') {
            team2+=s[i];
            ++i;
        }
        ++i;
        sscanf(s+i,"%d:%d",&d1,&d2);
        int i1,i2;
        i1 = team[team1]; i2 = team[team2];
            if (d1 > d2) score[i1].x = score[i1].x + 3;
        else
            if (d1 < d2) score[i2].x = score[i2].x + 3;
        else
            if ( d1==d2 ) {
                score[i1].x++;
                score[i2].x++;
            }
    }
    sort(score+1,score+n+1,cmp);
    //For(i,1,n) cout<< score[i].x<<" "<<score[i].n_team<<endl;
    int nm = 0;
    For(i,1,n/2) NAME[++nm] = score[i].n_team;
    sort(NAME+1,NAME+nm+1);
    For(i,1,nm) cout<<NAME[i]<<endl;
}
int main(){
    #ifndef ONLINE_JUDGE
    FileIn("exam"); FileOut("exam");
    #endif
    Read_Input();
    return 0;
}


/***********************************************
* Author - LUONG VAN DO                        *
* A. World Football Cup
* Algorithm
* Time Limit
* *********************************************/
#include <iostream>
#include <stdio.h>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <cmath>
#include <math.h>
#include <cstring>
#include <string.h>
#include <stdlib.h>
#include <algorithm>

#define FileIn(file) freopen(file".inp", "r", stdin)
#define FileOut(file) freopen(file".out", "w", stdout)
#define For(i, a, b) for (int i=a; i<=b; i++)
#define Ford(i,a,b) for (int i=a;i>=b;i--)
#define Rep(i, n) for (int i=0; i<n; i++)
#define pb push_back
#define A first
#define B second
#define PI 3.1415926535897932385
#define uint64 unsigned long long
#define int64 long long
#define INF 500000000
#define maxN 50001
using namespace std;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
struct node{
    int x, f, s;
    string n_team;
    node(){}
    node(int xx, int ff, int ss, string nn_team){
        x = xx; n_team = nn_team;
        f = ff; s = ss;
    }
};
map <string, int> team;
string NAME[55];
node score[55];
int l,n,m;
char s[105];
bool cmp(node c, node d){
    if (c.x!=d.x) return c.x > d.x;
    if (c.f!=d.f) return c.f > d.f;
    if (c.s!=d.s) return c.s > d.s;
    return c.n_team < d.n_team;
}
void Read_Input(){
    string team1,team2,name;
    int d1,d2;
    scanf("%d",&n);
    For(i,1,n) {
        cin >> name;
        team[name] = i;
        score[i].n_team = name;
    }
    m = (n * (n - 1)) / 2;
    gets(s);
    For(i,1,m) {
        gets(s);
        l = strlen(s);
        int j = 0;
        team1 = team2 = "";
        while (j < l && s[j]!='-') {
            team1+=s[j];
            ++j;
        }
        ++j;
        while (j < l && s[j]!=' ') {
            team2+=s[j];
            ++j;
        }
        ++j;
        sscanf(s+j,"%d:%d",&d1,&d2);
        int i1,i2;
        i1 = team[team1]; i2 = team[team2];
            if (d1 > d2) score[i1].x = score[i1].x + 3;
        else
            if (d1 < d2) score[i2].x = score[i2].x + 3;
        else
            if ( d1 == d2 ) {
                score[i1].x++;
                score[i2].x++;
            }
        score[i1].f+=(d1 - d2);
        score[i2].f+=(d2 - d1);
        score[i1].s+=d1;
        score[i2].s+=d2;
    }
    sort(score+1,score+n+1,cmp);
    //For(i,1,n) cout<< score[i].x<<" "<<score[i].n_team<<endl;
    int nm = 0;
    For(i,1,n/2) NAME[++nm] = score[i].n_team;
    sort(NAME+1,NAME+nm+1);
    For(i,1,nm) cout<<NAME[i]<<endl;
}
int main(){
    #ifndef ONLINE_JUDGE
    FileIn("exam"); FileOut("exam");
    #endif
    Read_Input();
    return 0;
}


/***********************************************
* Author - LUONG VAN DO                        *
* A. World Football Cup
* Algorithm
* Time Limit
* *********************************************/
#include <iostream>
#include <stdio.h>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <cmath>
#include <math.h>
#include <cstring>
#include <string.h>
#include <stdlib.h>
#include <algorithm>

#define FileIn(file) freopen(file".inp", "r", stdin)
#define FileOut(file) freopen(file".out", "w", stdout)
#define For(i, a, b) for (int i=a; i<=b; i++)
#define Ford(i,a,b) for (int i=a;i>=b;i--)
#define Rep(i, n) for (int i=0; i<n; i++)
#define pb push_back
#define A first
#define B second
#define PI 3.1415926535897932385
#define uint64 unsigned long long
#define int64 long long
#define INF 500000000
#define maxN 50001
using namespace std;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
struct node{
    int x;
    string n_team;
    node(){}
    node(int xx, string nn_team){
        x = xx; n_team = nn_team;
    }
};
map <string, int> team;
string NAME[55];
node score[55];
int l,n,m;
char s[105];
bool cmp(node c, node d){
    if (c.x < d.x) return c.x < d.x;
    return c.n_team < d.n_team;
}
void Read_Input(){
    string team1,team2,name;
    int d1,d2;
    scanf("%d",&n);
    For(i,1,n) {
        cin >> name;
        team[name] = i;
        score[i].n_team = name;
    }
    m = (n * (n - 1)) / 2;
    gets(s);
    For(i,1,m) {
        gets(s);
        l = strlen(s);
        int i = 0;
        team1 = team2 = "";
        while (i < l && s[i]!='-') {
            team1+=s[i];
            ++i;
        }
        ++i;
        while (i < l && s[i]!=' ') {
            team2+=s[i];
            ++i;
        }
        ++i;
        sscanf(s+i,"%d:%d",&d1,&d2);
        int i1,i2;
        i1 = team[team1]; i2 = team[team2];
            if (d1 > d2) score[i1].x = score[i1].x + 3;
        else
            if (d1 < d2) score[i2].x = score[i2].x + 3;
        else
            if (d1==d2) score[i1].x++, score[i2].x++;
    }
    sort(score+1,score+n+1,cmp);
    int nm = 0;
    For(i,n/2+1,n) NAME[++nm] = score[i].n_team;
    sort(NAME+1, NAME+nm+1);
    For(i,1,nm) cout<<NAME[i]<<endl;
}
int main(){
    #ifndef ONLINE_JUDGE
    FileIn("exam"); FileOut("exam");
    #endif
    Read_Input();
    //Solve();
    //Write_Output();
    return 0;
}


/***********************************************
* Author - LUONG VAN DO                        *
* A. World Football Cup
* Algorithm
* Time Limit
* *********************************************/
#include <iostream>
#include <stdio.h>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <cmath>
#include <math.h>
#include <cstring>
#include <string.h>
#include <stdlib.h>
#include <algorithm>

#define FileIn(file) freopen(file".inp", "r", stdin)
#define FileOut(file) freopen(file".out", "w", stdout)
#define For(i, a, b) for (int i=a; i<=b; i++)
#define Ford(i,a,b) for (int i=a;i>=b;i--)
#define Rep(i, n) for (int i=0; i<n; i++)
#define pb push_back
#define A first
#define B second
#define PI 3.1415926535897932385
#define uint64 unsigned long long
#define int64 long long
#define INF 500000000
#define maxN 50001
using namespace std;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
struct node{
    int x,id;
    node(){}
    node(int xx, int idd){
        x = xx; id = idd;
    }
};
map <string, int> team;
string idx[55];
string NAME[55];
node score[55];
int l,n,m;
char s[105];
bool cmp(node c, node d){
    return c.x > d.x;
}
void Read_Input(){
    string team1,team2,name;
    int d1,d2,nm;
    scanf("%d",&n);
    For(i,1,n) {
        cin >> name;
        team[name] = i;
        score[i].id = i;
        idx[i] = name;
    }
    m = (n * (n - 1)) / 2;
    gets(s);
    For(i,1,m) {
        gets(s);
        l = strlen(s);
        int i = 0;
        team1 = team2 = "";
        while (i < l && s[i]!='-') {
            team1+=s[i];
            ++i;
        }
        ++i;
        while (i < l && s[i]!=' ') {
            team2+=s[i];
            ++i;
        }
        ++i;
        sscanf(s+i,"%d:%d",&d1,&d2);
        int i1,i2;
        i1 = team[team1]; i2 = team[team2];
            if (d1 > d2) score[i1].x = score[i1].x + 3;
        else
            if (d1 < d2) score[i2].x = score[i2].x + 3;
        else
            if (d1==d2) score[i1].x++, score[i2].x++;
    }
    sort(score+1,score+n+1,cmp);
    For(i,1,n/2) NAME[i-1] = idx[ score[i].id ];
    sort(NAME, NAME+n/2);
    Rep(i,n/2) cout<<NAME[i]<<endl;
}
int main(){
    #ifndef ONLINE_JUDGE
    FileIn("exam"); FileOut("exam");
    #endif
    Read_Input();
    //Solve();
    //Write_Output();
    return 0;
}


#include<iostream>
#include<algorithm>
#include<cstdio>
#include<string>
#include<cstring>
using namespace std;

int n,s[51]={0},sg[51]={0},dg[51]={0};
string t[51];

bool comp(int a,int b){
	/*return s[a]>s[b]||
	       (s[a]==s[b] && sg[a]-dg[a]>sg[b]-dg[b]) ||
		   ((s[a]==s[b] && sg[a]-dg[a]==sg[b]-dg[b]) && sg[a]>sg[b]);*/
	if(s[a]!=s[b]) return s[a]>s[b];
	int jsa=sg[a]-dg[a],jsb=sg[b]-dg[b];
	if(jsa!=jsb) return jsa>jsb;
	if(sg[a]!=sg[b]) return sg[a]>sg[b];
}

int found(string x)
{
	for(int i=0;i<n;i++)
	   if(t[i]==x) return i;
}

int main()
{
	//freopen("1.in","r",stdin);
	cin>>n;
	for(int i=0;i<n;i++)
		cin>>t[i];
	int time,x1,y1,x2,y2;
	string x,y,te;
	char temp;
	time=n*(n-1)/2;
	for(int i=1;i<=time;i++)
	{
		getline(cin,x,'-');
		getline(cin,y,' ');
		cin>>x1>>temp>>y1;
		x2=found(x);
		y2=found(y);
		getline(cin,te);
		if(x1==y1) {s[x2]++;s[y2]++;} else if(x1>y1) s[x2]+=3;else s[y2]+=3;
		sg[x2]+=x1;dg[x2]+=y1;
		sg[y2]+=y1;dg[y2]+=x1;
	}
	int c[51];
	for(int i=0;i<n;i++)
	   c[i]=i;
	sort(c,c+n,comp);
	string name[51];
	for(int i=0;i<n/2;i++)
	   name[i]=t[c[i]];
	sort(name,name+n/2);
	for(int i=0;i<n/2;i++)
	   cout<<name[i]<<endl;
	return 0;
}


#include<iostream>
#include<algorithm>
#include<cstdio>
#include<string>
#include<cstring>
using namespace std;

int n,s[51]={0},sg[51]={0},dg[51]={0};
string t[51];

bool comp(int a,int b){
	return s[a]>s[b]||
	       (s[a]==s[b] && sg[a]-dg[a]>sg[b]-dg[b]) ||
		   ((s[a]==s[b] && sg[a]-dg[a]==sg[b]-dg[b]) && sg[a]>sg[b]);
}

int found(string z)
{
	for(int i=0;i<n;i++)
	   if(z==t[i]) return i;
}

int main()
{
	//freopen("1.in","r",stdin);
	cin>>n;
	for(int i=0;i<n;i++)
	   cin>>t[i];//cout<<t[i]<<endl;

	int time,x1,y1,x2,y2;
	string x,y,te;
	char temp;
	time=n*(n-1)/2;
	for(int i=1;i<=time;i++)
	{
		getline(cin,x,'-');
		getline(cin,y,' ');
		cin>>x1>>temp>>y1;
		x2=found(x);
		y2=found(y);
		//cout<<x2<<" "<<y2<<endl;
		getline(cin,te);
		if(x1==y1) {s[x2]++;s[y2]++;} else if(x1>y1) s[x2]+=3;else s[y2]+=3;
		sg[x2]+=x1;dg[x2]+=y1;
		sg[y2]+=y1;dg[y2]+=x1;
	}
	int c[51];
	for(int i=0;i<n;i++)
	   c[i]=i;
	sort(c,c+n,comp);
	string name[51];
	for(int i=0;i<n/2;i++)
	   name[i]=t[c[i]];
	sort(name,name+n/2);
	for(int i=0;i<n/2;i++)
	   cout<<name[i]<<endl;
	return 0;
}


#include<iostream>
#include<algorithm>
#include<cstdio>
#include<string>
#include<cstring>
#include<map>
using namespace std;

int n,s[51]={0},sg[51]={0},dg[51]={0},x1,y1;
string t[51];
map<string,int> txt;

bool comp(int a,int b){
	/*return s[a]>s[b]||
	       (s[a]==s[b] && sg[a]-dg[a]>sg[b]-dg[b]) ||
		   ((s[a]==s[b] && sg[a]-dg[a]==sg[b]-dg[b]) && sg[a]>sg[b]);*/
	if(s[a]!=s[b]) return s[a]>s[b];
	int jsa=sg[a]-dg[a],jsb=sg[b]-dg[b];
	if(jsa!=jsb) return jsa>jsb;
	if(sg[a]!=sg[b]) return sg[a]>sg[b];
}

void num(string z)
{
	x1=0;y1=0;
	bool xy=true;
	for(int i=0;i<z.length();i++)
	   if(z[i]!=':')
	   {
	   	 if(xy) x1=x1*10+z[i]-'0';
	   	 else y1=y1*10+z[i]-'0';
	   }
	   else
	     xy=false;
}

int main()
{
	//freopen("1.in","r",stdin);
	cin>>n;
	for(int i=0;i<n;i++)
		cin>>t[i];
	sort(t,t+n);
	for(int i=0;i<n;i++)
	    txt[t[i]]=i;
	int time,x2,y2;
	string x,y,te;
	string temp;
	time=n*(n-1)/2;
	for(int i=1;i<=time;i++)
	{
		getline(cin,x,'-');
		getline(cin,y,' ');
		cin>>temp;
		num(temp);
		x2=txt[x];
		y2=txt[y];
		getline(cin,te);
		if(x1==y1) {s[x2]++;s[y2]++;} else if(x1>y1) s[x2]+=3;else s[y2]+=3;
		sg[x2]+=x1;dg[x2]+=y1;
		dg[y2]+=x1;sg[y2]+=y1;
	}
	int c[51];
	bool q[51];
	memset(q,false,sizeof(q));
	for(int i=0;i<n;i++)
	   c[i]=i;
	sort(c,c+n,comp);
	for(int i=0;i<n/2;i++)
	   q[c[i]]=true;
	for(int i=0;i<n;i++)
	   if(q[i]) cout<<t[i]<<endl;
	return 0;
}


#include<iostream>
#include<algorithm>
#include<cstdio>
#include<string>
#include<cstring>
using namespace std;

int n,s[51]={0},sg[51]={0},dg[51]={0};
string t[51];

bool comp(int a,int b){
	/*return s[a]>s[b]||
	       (s[a]==s[b] && sg[a]-dg[a]>sg[b]-dg[b]) ||
		   ((s[a]==s[b] && sg[a]-dg[a]==sg[b]-dg[b]) && sg[a]>sg[b]);*/
	if(s[a]!=s[b]) return s[a]>s[b];
	int jsa=sg[a]-dg[a],jsb=sg[b]-dg[b];
	if(jsa!=jsb) return jsa>jsb;
	if(sg[a]!=sg[b]) return sg[a]>sg[b];
}

int found(string z)
{
	for(int i=0;i<n;i++)
	   if(z==t[i]) return i;
}

int main()
{
	//freopen("1.in","r",stdin);
	cin>>n;
	for(int i=0;i<n;i++)
	   cin>>t[i];//cout<<t[i]<<endl;

	int time,x1,y1,x2,y2;
	string x,y,te;
	char temp;
	time=n*(n-1)/2;
	for(int i=1;i<=time;i++)
	{
		getline(cin,x,'-');
		getline(cin,y,' ');
		cin>>x1>>temp>>y1;
		x2=found(x);
		y2=found(y);
		//cout<<x2<<" "<<y2<<endl;
		getline(cin,te);
		if(x1==y1) {s[x2]++;s[y2]++;} else if(x1>y1) s[x2]+=3;else s[y2]+=3;
		sg[x2]+=x1;dg[x2]+=y1;
		sg[y2]+=y1;dg[y2]+=x1;
	}
	int c[51];
	for(int i=0;i<n;i++)
	   c[i]=i;
	sort(c,c+n,comp);
	string name[51];
	for(int i=0;i<n/2;i++)
	   name[i]=t[c[i]];
	sort(name,name+n/2);
	for(int i=0;i<n/2;i++)
	   cout<<name[i]<<endl;
	return 0;
}


#include<iostream>
#include<algorithm>
#include<cstdio>
#include<string>
#include<cstring>
using namespace std;

int n,s[51]={0},sg[51]={0},dg[51]={0},x1,y1;
string t[51];

bool comp(int a,int b){
	/*return s[a]>s[b]||
	       (s[a]==s[b] && sg[a]-dg[a]>sg[b]-dg[b]) ||
		   ((s[a]==s[b] && sg[a]-dg[a]==sg[b]-dg[b]) && sg[a]>sg[b]);*/
	if(s[a]!=s[b]) return s[a]>s[b];
	int jsa=sg[a]-dg[a],jsb=sg[b]-dg[b];
	if(jsa!=jsb) return jsa>jsb;
	if(sg[a]!=sg[b]) return sg[a]>sg[b];
}

int found(string x)
{
	for(int i=0;i<n;i++)
	   if(t[i]==x) return i;
}

void num(string x)
{
	x1=0;y1=0;
	bool xy=true;
	for(int i=0;i<x.length();i++)
	   if(x[i]!=':')
	   {
	   	 if(xy) x1=x1*10+x[i]-'0';
	   	 else y1=y1*10+x[i]-'0';
	   }
	   else
	     xy=false;
}

int main()
{
	//freopen("1.in","r",stdin);
	cin>>n;
	for(int i=0;i<n;i++)
		cin>>t[i];
	int time,x2,y2;
	string x,y,te;
	string temp;
	time=n*(n-1)/2;
	for(int i=1;i<=time;i++)
	{
		getline(cin,x,'-');
		getline(cin,y,' ');
		cin>>temp;
		num(temp);
		x2=found(x);
		y2=found(y);
		getline(cin,te);
		if(x1==y1) {s[x2]++;s[y2]++;} else if(x1>y1) s[x2]+=3;else s[y2]+=3;
		sg[x2]+=x1;dg[x2]+=y1;
		sg[y2]+=y1;dg[y2]+=x1;
	}
	int c[51];
	for(int i=0;i<n;i++)
	   c[i]=i;
	sort(c,c+n,comp);
	string name[51];
	for(int i=0;i<n/2;i++)
	   name[i]=t[c[i]];
	sort(name,name+n/2);
	for(int i=0;i<n/2;i++)
	   cout<<name[i]<<endl;
	return 0;
}


#include<iostream>
#include<algorithm>
#include<cstdio>
#include<string>
#include<cstring>
#include<map>
using namespace std;

int n,s[51]={0},sg[51]={0},dg[51]={0},x1,y1;
string t[51];
map<string,int> txt;

bool comp(int a,int b){
	/*return s[a]>s[b]||
	       (s[a]==s[b] && sg[a]-dg[a]>sg[b]-dg[b]) ||
		   ((s[a]==s[b] && sg[a]-dg[a]==sg[b]-dg[b]) && sg[a]>sg[b]);*/
	if(s[a]!=s[b]) return s[a]>s[b];
	int jsa=sg[a]-dg[a],jsb=sg[b]-dg[b];
	if(jsa!=jsb) return jsa>jsb;
	if(sg[a]!=sg[b]) return sg[a]>sg[b];
}

void num(string z)
{
	x1=0;y1=0;
	bool xy=true;
	for(int i=0;i<z.length();i++)
	   if(z[i]!=':')
	   {
	   	 if(xy) x1=x1*10+z[i]-'0';
	   	 else y1=y1*10+z[i]-'0';
	   }
	   else
	     xy=false;
}

int main()
{
	//freopen("1.in","r",stdin);
	cin>>n;
	for(int i=0;i<n;i++)
		cin>>t[i];
	for(int i=0;i<n;i++)
	    txt[t[i]]=i;
	int time,x2,y2;
	string x,y,te;
	string temp;
	time=n*(n-1)/2;
	for(int i=1;i<=time;i++)
	{
		getline(cin,x,'-');
		getline(cin,y,' ');
		cin>>temp;
		num(temp);
		x2=txt[x];
		y2=txt[y];
		getline(cin,te);
		if(x1==y1) {s[x2]++;s[y2]++;} else if(x1>y1) s[x2]+=3;else s[y2]+=3;
		sg[x2]+=x1;dg[x2]+=y1;
		sg[y2]+=y1;dg[y2]+=x1;
	}
	int c[51];
	for(int i=0;i<n;i++)
	   c[i]=i;
	sort(c,c+n,comp);
	string name[51];
	for(int i=0;i<n/2;i++)
	   name[i]=t[c[i]];
	sort(name,name+n/2);
	for(int i=0;i<n/2;i++)
	   cout<<name[i]<<endl;
	return 0;
}


#include<iostream>
#include<algorithm>
#include<cstdio>
#include<string>
#include<cstring>
#define ll long long
using namespace std;

ll n,s[1001]={0},sg[1001]={0},dg[1001]={0};
string t[1001];

bool comp(ll a,ll b){
	return s[a]>s[b]||
	       (s[a]==s[b] && sg[a]-dg[a]>sg[b]-dg[b]) ||
		   ((s[a]==s[b] && sg[a]-dg[a]==sg[b]-dg[b]) && sg[a]>sg[b]);
}

int main()
{
	//freopen("1.in","r",stdin);
	cin>>n;
	for(ll i=0;i<n;i++)
		cin>>t[i];
	ll time,x1,y1,x2,y2;
	string x,y,te;
	char temp;
	time=n*(n-1)/2;
	for(ll i=1;i<=time;i++)
	{
		getline(cin,x,'-');
		getline(cin,y,' ');
		cin>>x1>>temp>>y1;
		x2=find(t,t+n,x)-t;
		y2=find(t,t+n,y)-t;
		getline(cin,te);
		if(x1==y1) {s[x2]++;s[y2]++;} else if(x1>y1) s[x2]+=3;else s[y2]+=3;
		sg[x2]+=x1;dg[x2]+=y1;
		sg[y2]+=y1;dg[y2]+=x1;
	}
	ll c[1001];
	for(ll i=0;i<n;i++)
	   c[i]=i;
	sort(c,c+n,comp);
	string name[1001];
	for(ll i=0;i<n/2;i++)
	   name[i]=t[c[i]];
	sort(name,name+n/2);
	for(ll i=0;i<n/2;i++)
	   cout<<name[i]<<endl;
	return 0;
}


#include<iostream>
#include<algorithm>
#include<cstdio>
#include<string>
#include<cstring>
#include<map>
using namespace std;

int n,s[51]={0},sg[51]={0},dg[51]={0},x1,y1;
string t[51];
map<string,int> txt;

bool comp(int a,int b){
	/*return s[a]>s[b]||
	       (s[a]==s[b] && sg[a]-dg[a]>sg[b]-dg[b]) ||
		   ((s[a]==s[b] && sg[a]-dg[a]==sg[b]-dg[b]) && sg[a]>sg[b]);*/
	if(s[a]!=s[b]) return s[a]>s[b];
	int jsa=sg[a]-dg[a],jsb=sg[b]-dg[b];
	if(jsa!=jsb) return jsa>jsb;
	if(sg[a]!=sg[b]) return sg[a]>sg[b];
}

void num(string z)
{
	x1=0;y1=0;
	bool xy=true;
	for(int i=0;i<z.length();i++)
	   if(z[i]!=':')
	   {
	   	 if(xy) x1=x1*10+z[i]-'0';
	   	 else y1=y1*10+z[i]-'0';
	   }
	   else
	     xy=false;
}

int main()
{
	//freopen("1.in","r",stdin);
	cin>>n;
	for(int i=0;i<n;i++)
		cin>>t[i];
	for(int i=0;i<n;i++)
	    txt[t[i]]=i;
	int time,x2,y2;
	string x,y,te;
	string temp;
	time=n*(n-1)/2;
	for(int i=1;i<=time;i++)
	{
		getline(cin,x,'-');
		getline(cin,y,' ');
		cin>>temp;
		num(temp);
		x2=txt[x];
		y2=txt[y];
		getline(cin,te);
		if(x1==y1) {s[x2]++;s[y2]++;} else if(x1>y1) s[x2]+=3;else s[y2]+=3;
		sg[x2]+=x1;dg[x2]+=y1;
		sg[y2]+=y1;dg[y2]+=x1;
	}
	int c[51];
	for(int i=0;i<n;i++)
	   c[i]=i;
	sort(c,c+n,comp);
	string name[51];
	for(int i=0;i<n;i++)
	   name[i]=t[c[i]];
	sort(name,name+n/2);
	for(int i=0;i<n/2;i++)
	   cout<<name[i]<<endl;
	return 0;
}


#include<iostream>
#include<algorithm>
#include<cstdio>
#include<string>
#include<cstring>
#include<map>
using namespace std;

int n,s[51]={0},sg[51]={0},dg[51]={0},x1,y1;
string t[51];
map<string,int> txt;

bool comp(int a,int b){
	/*return s[a]>s[b]||
	       (s[a]==s[b] && sg[a]-dg[a]>sg[b]-dg[b]) ||
		   ((s[a]==s[b] && sg[a]-dg[a]==sg[b]-dg[b]) && sg[a]>sg[b]);*/
	if(s[a]!=s[b]) return s[a]>s[b];
	int jsa=sg[a]-dg[a],jsb=sg[b]-dg[b];
	if(jsa!=jsb) return jsa>jsb;
	if(sg[a]!=sg[b]) return sg[a]>sg[b];
}

/*void num(string z)
{
	x1=0;y1=0;
	bool xy=true;
	for(int i=0;i<z.length();i++)
	   if(z[i]!=':')
	   {
	   	 if(xy) x1=x1*10+z[i]-'0';
	   	 else y1=y1*10+z[i]-'0';
	   }
	   else
	     xy=false;
}*/

int main()
{
	//freopen("1.in","r",stdin);
	scanf("%d",&n);
	for(int i=0;i<n;i++)
		cin>>t[i];
	sort(t,t+n);

	for(int i=0;i<n;i++)
	{
	    txt[t[i]]=i;
	   // cout<<txt[t[i]];
	}
	//cout<<endl;
	//cout<<t[1]<<endl;
	int time,x2,y2;
	string te;
	char x[50],y[50];
	string temp;
	//for(int i=0;i<n;i++)
	//	cout<<t[i]<<endl;
	time=n*(n-1)/2;
	for(int i=1;i<=time;i++)
	{
		scanf(" %[^-]-%s %d:%d",x,y,&x1,&y1);
		/*getline(cin,x,'-');
		getline(cin,y,' ');*/
		//cout<<x<<t[1];
		//cout<<x<<"-"<<y<<" ";
		//num(temp);
		//cout<<x1<<":"<<y1<<"=";
		x2=txt[x];
		//if(x==t[1]) x2=1;
		//cout<<txt[x]<<endl;
		y2=txt[y];
		//if(y==t[1]) y2=1;
		//cout<<x2<<" "<<y2<<endl;
		//cin>>te;
		if(x1==y1) {s[x2]++;s[y2]++;} else if(x1>y1) s[x2]+=3;else s[y2]+=3;
		sg[x2]+=x1;dg[x2]+=y1;
		dg[y2]+=x1;sg[y2]+=y1;
	}
	int c[51];
	bool q[51];
	memset(q,false,sizeof(q));
	for(int i=0;i<n;i++)
	   c[i]=i;
	sort(c,c+n,comp);
	for(int i=0;i<n/2;i++)
	   q[c[i]]=true;
	for(int i=0;i<n;i++)
	   if(q[i]) cout<<t[i]<<endl;
	return 0;
}


#include <cstdio>
#include <algorithm>
#include <map>
#include <string>
#include <iostream>
using namespace std;
map<string,int> tbl;
string name[50];
int get[50],lse[50],pts[50],a,b,ord[50],ok[50];
char s1[50],s2[50];
bool cp( int a, int b ) {
	if ( pts[a]!=pts[b] ) return pts[a]>pts[b];
	if ( get[a]-lse[a]!=get[b]-lse[b] ) return get[a]-lse[a]>get[b]-lse[b];
	if ( get[a]!=get[b] ) return get[a]>get[b];
	return 0;
}
int main()
{
	int n,m,i;
	scanf("%d",&n);
	for ( i=0; i<n; i++ ) cin>>name[i];
	sort(name,name+n);
	for ( i=0; i<n; i++ ) tbl[name[i]]=ord[i]=i;
	m=n*(n-1)/2;
	for ( i=0; i<m; i++ ) {
		scanf(" %[^-]-%s %d:%d",s1,s2,&a,&b);
		get[tbl[s1]]+=a;
		get[tbl[s2]]+=b;
		lse[tbl[s1]]+=b;
		lse[tbl[s2]]+=a;
		if ( a>b ) pts[tbl[s1]]+=3;
		else if ( a<b ) pts[tbl[s2]]+=3;
		else pts[tbl[s2]]+=1,pts[tbl[s1]]+=1;
	}
	sort(ord,ord+n,cp);
	for ( i=0; i<n/2; i++ ) ok[ord[i]]=1;
	for ( i=0; i<n; i++ )
		if ( ok[i] ) cout<<name[i]<<endl;
	return 0;
}


#include<iostream>
#include<algorithm>
#include<cstdio>
#include<string>
#include<cstring>
using namespace std;

int n,s[51]={0},sg[51]={0},dg[51]={0};
string t[51];

bool comp(int a,int b){
	return s[a]>s[b]||
	       (s[a]==s[b] && sg[a]-dg[a]>sg[b]-dg[b]) ||
		   ((s[a]==s[b] && sg[a]-dg[a]==sg[b]-dg[b]) && sg[a]>sg[b]);
}

int main()
{
	//freopen("1.in","r",stdin);
	cin>>n;
	for(int i=0;i<n;i++)
		cin>>t[i];
	int time,x1,y1,x2,y2;
	string x,y,te;
	char temp;
	time=n*(n-1)/2;
	for(int i=1;i<=time;i++)
	{
		getline(cin,x,'-');
		getline(cin,y,' ');
		cin>>x1>>temp>>y1;
		x2=find(t,t+n,x)-t;
		y2=find(t,t+n,y)-t;
		getline(cin,te);
		if(x1==y1) {s[x2]++;s[y2]++;} else if(x1>y1) s[x2]+=3;else s[y2]+=3;
		sg[x2]+=x1;dg[x2]+=y1;
		sg[y2]+=y1;dg[y2]+=x1;
	}
	int c[51];
	for(int i=0;i<n;i++)
	   c[i]=i;
	sort(c,c+n,comp);
	string name[51];
	for(int i=0;i<n/2;i++)
	   name[i]=t[c[i]];
	sort(name,name+n/2);
	for(int i=0;i<n/2;i++)
	   cout<<name[i]<<endl;
	return 0;
}