#include <iostream>
#include <cstdio>
#include <set>
using namespace std;
#define mp make_pair
const int SZ=100005;
set < pair<int,int> > s;
int x[SZ],y[SZ],z[SZ],rank[3*SZ],n,k,t;
bool do_[3*SZ];
int main()
{
  cin>>n;

  for(int i=1;i<=3*n;i++)
  {
    scanf("%d",&t);
    rank[t]=i;
    s.insert(mp(i,t));
  }
  for(int i=1;i<=n;i++)
    scanf("%d%d%d",&x[i],&y[i],&z[i]);
  cin>>k;
  int f1=-1,f2=-1;
  bool was=false,out1=false,out2=false;
  for(int i=1;i<=n;i++)
    if (x[i]!=k && y[i]!=k && z[i]!=k)
    {
      if (!was) do_[x[i]]=do_[y[i]]=do_[z[i]]=true;
      s.erase(mp(rank[x[i]],x[i]));
      s.erase(mp(rank[y[i]],y[i]));
      s.erase(mp(rank[z[i]],z[i]));
    } else
    {
      if (k==x[i] && (*s.begin()).second==x[i]) f1=min(y[i],z[i]), f2=max(y[i],z[i]),was=true;
      else if (k==y[i] && (*s.begin()).second==y[i]) f1=min(x[i],z[i]), f2=max(x[i],z[i]),was=true;
      else if (k==z[i] && (*s.begin()).second==z[i]) f1=min(x[i],y[i]), f2=max(x[i],y[i]),was=true;
      s.erase(mp(rank[x[i]],x[i]));
      s.erase(mp(rank[y[i]],y[i]));
      s.erase(mp(rank[z[i]],z[i]));
      if (f1!=-1)do_[f1]=do_[f2]=true;
    }
  if (f1!=-1)
  {
    int i;
    for(i=1;i<=3*n;i++)
    {
      if (do_[i])cout<<i<<" ";
      if (i==f2)
      {
        for(int j=1;j<i;j++)
          if (!do_[j] && j!=k)cout<<j<<" ";
        i++;
        for(;i<=3*n;i++)
          if (i!=k) cout<<i<<" ";
        break;
      }
    }
  } else
    for(int i=1;i<=3*n;i++)
      if (i==k)continue;
      else cout<<i<<" ";
  return 0;
}


#include <iostream>
#include <cstdio>
#include <set>
using namespace std;
#define mp make_pair
const int SZ=100005;
set < pair<int,int> > s;
int x[SZ],y[SZ],z[SZ],rank[3*SZ],n,k,t;
bool do_[3*SZ];
int main()
{
  cin>>n;
  for(int i=1;i<=3*n;i++)
  {
    scanf("%d",&t);
    rank[t]=i;
    s.insert(mp(i,t));
  }
  for(int i=1;i<=n;i++)
    scanf("%d%d%d",&x[i],&y[i],&z[i]);
  cin>>k;
  int f1=-1,f2=-1;
  bool was=false,out1=false,out2=false;
  for(int i=1;i<=n;i++)
    if (x[i]!=k && y[i]!=k && z[i]!=k)
    {
      if (!was) do_[x[i]]=do_[y[i]]=do_[z[i]]=true;
      s.erase(mp(rank[x[i]],x[i]));
      s.erase(mp(rank[y[i]],y[i]));
      s.erase(mp(rank[z[i]],z[i]));
    } else
    {
      if (k==x[i] && (*s.begin()).second==x[i]) f1=min(y[i],z[i]), f2=max(y[i],z[i]),was=true;
      else if (k==y[i] && (*s.begin()).second==y[i]) f1=min(x[i],z[i]), f2=max(x[i],z[i]),was=true;
      else if (k==z[i] && (*s.begin()).second==z[i]) f1=min(x[i],y[i]), f2=max(x[i],y[i]),was=true;
      s.erase(mp(rank[x[i]],x[i]));
      s.erase(mp(rank[y[i]],y[i]));
      s.erase(mp(rank[z[i]],z[i]));
      if (f1!=-1)do_[f1]=do_[f2]=true;
    }
  if (f1!=-1)
  {
    int i;
    for(i=1;i<=3*n;i++)
    {
      if (do_[i])cout<<i<<" ";
      if (i==f2)
      {
        for(int j=1;j<i;j++)
          if (!do_[j] && j!=k)cout<<j<<" ";
        break;
      }
    }
    i++;
    for(;i<=3*n;i++)
      if (i!=k) cout<<i<<" ";
  } else
    for(int i=1;i<=3*n;i++)
      if (i==k)continue;
      else cout<<i<<" ";
  return 0;
}


#include <iostream>
#include <cstdio>
#include <set>
using namespace std;
#define mp make_pair
const int SZ=100005;
set < pair<int,int> > s;
int x[SZ],y[SZ],z[SZ],rank[3*SZ],n,k,t;
bool do_[3*SZ];
int main()
{
  cin>>n;
  for(int i=1;i<=3*n;i++)
  {
    scanf("%d",&t);
    rank[t]=i;
    s.insert(mp(i,t));
  }
  for(int i=1;i<=n;i++)
    scanf("%d%d%d",&x[i],&y[i],&z[i]);
  cin>>k;
  int f1=-1,f2=-1;
  bool was=false,out1=false,out2=false;
  for(int i=1;i<=n;i++)
    if (x[i]!=k && y[i]!=k && z[i]!=k)
    {
      if (!was) do_[x[i]]=do_[y[i]]=do_[z[i]]=true;
      s.erase(mp(rank[x[i]],x[i]));
      s.erase(mp(rank[y[i]],y[i]));
      s.erase(mp(rank[z[i]],z[i]));
    } else
    {
      if (k==x[i] && (*s.begin()).second==x[i]) f1=y[i], f2=z[i],was=true;
      else if (k==y[i] && (*s.begin()).second==y[i]) f1=x[i], f2=z[i],was=true;
      else if (k==z[i] && (*s.begin()).second==z[i]) f1=x[i], f2=y[i],was=true;
      s.erase(mp(rank[x[i]],x[i]));
      s.erase(mp(rank[y[i]],y[i]));
      s.erase(mp(rank[z[i]],z[i]));
      if (f1!=-1)do_[f1]=do_[f2]=true;
    }
  if (f1!=-1)
  {
    for(int i=1;i<=3*n;i++)
      if (do_[i])cout<<i<<" ";
    for(int i=1;i<=3*n;i++)
      if (i!=k && !do_[i])cout<<i<<" ";
  } else
    for(int i=1;i<=3*n;i++)
      if (i==k)continue;
      else cout<<i<<" ";
  return 0;
}


#include <iostream>
#include <cstdio>
#include <set>
using namespace std;
#define mp make_pair
const int SZ=100005;
set < pair<int,int> > s;
int x[SZ],y[SZ],z[SZ],rank[3*SZ],n,k,t;
bool do_[3*SZ];
int main()
{
  cin>>n;
  for(int i=1;i<=3*n;i++)
  {
    scanf("%d",&t);
    rank[t]=i;
    s.insert(mp(i,t));
  }
  for(int i=1;i<=n;i++)
    scanf("%d%d%d",&x[i],&y[i],&z[i]);
  cin>>k;
  int f1=-1,f2=-1;
  bool was=false,out1=false,out2=false;
  for(int i=1;i<=n;i++)
    if (x[i]!=k && y[i]!=k && z[i]!=k)
    {
      if (!was) do_[x[i]]=do_[y[i]]=do_[z[i]]=true;
      s.erase(mp(rank[x[i]],x[i]));
      s.erase(mp(rank[y[i]],y[i]));
      s.erase(mp(rank[z[i]],z[i]));
    } else
    {
      if (k==x[i] && (*s.begin()).second==x[i]) f1=min(y[i],z[i]), f2=max(y[i],z[i]),was=true;
      else if (k==y[i] && (*s.begin()).second==y[i]) f1=min(x[i],z[i]), f2=max(x[i],z[i]),was=true;
      else if (k==z[i] && (*s.begin()).second==z[i]) f1=min(x[i],y[i]), f2=max(x[i],y[i]),was=true;
      s.erase(mp(rank[x[i]],x[i]));
      s.erase(mp(rank[y[i]],y[i]));
      s.erase(mp(rank[z[i]],z[i]));
      if (f1!=-1)do_[f1]=do_[f2]=true;
    }
  if (f1!=-1)
  {
    int i;
    for(i=1;i<=3*n;i++)
    {
      if (do_[i])cout<<i<<" ";
      if (i==f2)
      {
        for(int j=1;j<i;j++)
          if (!do_[j] && j!=k)cout<<j<<" ";
        i++;
        for(;i<=3*n;i++)
          if (i!=k) cout<<i<<" ";
        break;
      }
    }
  } else
    for(int i=1;i<=3*n;i++)
      if (i==k)continue;
      else cout<<i<<" ";

  return 0;
}


#include <iostream>
#include <cstdio>
#include <set>
using namespace std;
#define mp make_pair
const int SZ=100005;
set < pair<int,int> > s;
int x[SZ],y[SZ],z[SZ],rank[3*SZ],n,k,t;
bool do_[3*SZ];
int main()
{
  cin>>n;
  for(int i=1;i<=3*n;i++)
  {
    scanf("%d",&t);
    rank[t]=i;
    s.insert(mp(i,t));
  }
  for(int i=1;i<=n;i++)
    scanf("%d%d%d",&x[i],&y[i],&z[i]);
  cin>>k;
  int f1=-1,f2=-1;
  bool was=false,out1=false,out2=false;
  for(int i=1;i<=n;i++)
    if (x[i]!=k && y[i]!=k && z[i]!=k)
    {
      if (!was) do_[x[i]]=do_[y[i]]=do_[z[i]]=true;
      s.erase(mp(rank[x[i]],x[i]));
      s.erase(mp(rank[y[i]],y[i]));
      s.erase(mp(rank[z[i]],z[i]));
    } else
    {
      if (k==x[i] && (*s.begin()).second==x[i]) f1=min(y[i],z[i]), f2=max(y[i],z[i]),was=true;
      else if (k==y[i] && (*s.begin()).second==y[i]) f1=min(x[i],z[i]), f2=max(x[i],z[i]),was=true;
      else if (k==z[i] && (*s.begin()).second==z[i]) f1=min(x[i],y[i]), f2=max(x[i],y[i]),was=true;
      s.erase(mp(rank[x[i]],x[i]));
      s.erase(mp(rank[y[i]],y[i]));
      s.erase(mp(rank[z[i]],z[i]));
      if (f1!=-1)do_[f1]=do_[f2]=true;
    }
  if (f1!=-1)
  {
    int i;
    for(i=1;i<=3*n;i++)
    {
      if (do_[i])cout<<i<<" ";
      if (i==f2)
      {
        for(int j=1;j<i;j++)
          if (!do_[j] && j!=k)cout<<j<<" ";
        i++;
        for(;i<=3*n;i++)
          if (i!=k) cout<<i<<" ";
        break;
      }
    }
  } else
    for(int i=1;i<=3*n;i++)
      if (i==k)continue;
      else cout<<i<<" ";
  return 0;
}


#include <fstream>
#include <iostream>
#include <vector>
#include <string>
#include <list>
#include <map>
#include <algorithm>
#include <math.h>

using namespace std;

typedef long long ll;

ll n, i, j, res[300010], x, p, v[300010];
bool cap[300010], b;

int main()
{
    //ifstream f1("input.txt");
    //ofstream f2("output.txt");
	cin >> n;
	for(i = 1; i <= 3 * n; i++)
	{
		cin >> x;
		res[x] = i;
	}

	for(i = 1; i <= n; i++)
	{
		cin >> x >> j >> p;
		if((res[x] < res[j])&&(res[x] < res[p]))
		{
			cap[x] = 1;
		}
		if((res[j] < res[x])&&(res[j] < res[p]))
		{
			cap[j] = 1;
		}
		if((res[p] < res[j])&&(res[p] < res[x]))
		{
			cap[p] = 1;
		}

		v[x] = i;
		v[j] = i;
		v[p] = i;
	}

	cin >> j;
	if(cap[j] == 0)
	{
		for(i = 1; i <= 3 * n; i++)
			if(i != j)
		{
			if(i > 1)
				cout << " ";
			cout << i;
		}
		cout << endl;
	}
	else
	{
		b = 0;
		for(i = 1; i <= 3 * n; i++)
			if((i != j)&&(v[i] <= v[j]))
			{
				if(b)
					cout << " ";

				cout << i;
				b = 1;
			}

		for(i = 1; i <= 3 * n; i++)
			if((i != j)&&(v[i] > v[j]))
			{
				if(b)
					cout << " ";

				cout << i;
				b = 1;
			}

		cout << endl;

	}

    //f1.close();
    //f2.close();
     //system("PAUSE");
        return 0;
}


#include <fstream>
#include <iostream>
#include <vector>
#include <string>
#include <list>
#include <map>
#include <algorithm>
#include <math.h>

using namespace std;

typedef long long ll;

ll n, i, j, res[300010], x, p, v[300010];
bool cap[300010], b;

int main()
{
    //ifstream f1("input.txt");
    //ofstream f2("output.txt");
	cin >> n;
	for(i = 1; i <= 3 * n; i++)
	{
		cin >> x;
		res[x] = i;
	}

	for(i = 1; i <= n; i++)
	{
		cin >> x >> j >> p;
		if((res[x] < res[j])&&(res[x] < res[p]))
		{
			cap[x] = 1;
		}
		if((res[j] < res[x])&&(res[j] < res[p]))
		{
			cap[j] = 1;
		}
		if((res[p] < res[j])&&(res[p] < res[x]))
		{
			cap[p] = 1;
		}

		v[x] = i;
		v[j] = i;
		v[p] = i;
	}

	cin >> j;
	b = 0;
	if(cap[j] == 0)
	{
		for(i = 1; i <= 3 * n; i++)
			if(i != j)
		{
			if(b)
				cout << " ";
			cout << i;
			b = 1;
		}
		cout << endl;
	}
	else
	{
		b = 0;
		for(i = 1; i <= 3 * n; i++)
			if((i != j)&&(v[i] <= v[j]))
			{
				if(b)
					cout << " ";

				cout << i;
				b = 1;
			}

		for(i = 1; i <= 3 * n; i++)
			if((i != j)&&(v[i] > v[j]))
			{
				if(b)
					cout << " ";

				cout << i;
				b = 1;
			}

		cout << endl;

	}

    //f1.close();
    //f2.close();
     //system("PAUSE");
        return 0;
}


#include <fstream>
#include <iostream>
#include <vector>
#include <string>
#include <list>
#include <map>
#include <algorithm>
#include <math.h>

using namespace std;

typedef long long ll;

ll n, i, j, res[300010], x, p, v[300010];
bool cap[300010], b;

int main()
{
    //ifstream f1("input.txt");
    //ofstream f2("output.txt");
	cin >> n;
	for(i = 1; i <= 3 * n; i++)
	{
		cin >> x;
		res[x] = i;
	}

	for(i = 1; i <= n; i++)
	{
		cin >> x >> j >> p;
		if((res[x] < res[j])&&(res[x] < res[p]))
		{
			cap[x] = 1;
		}
		if((res[j] < res[x])&&(res[j] < res[p]))
		{
			cap[j] = 1;
		}
		if((res[p] < res[j])&&(res[p] < res[x]))
		{
			cap[p] = 1;
		}

		v[x] = i;
		v[j] = i;
		v[p] = i;
	}

	cin >> j;
	if(cap[j] == 0)
	{
		for(i = 1; i <= 3 * n; i++)
		{
			if(i > 1)
				cout << " ";
			cout << i;
		}
		cout << endl;
	}
	else
	{
		b = 0;
		for(i = 1; i <= 3 * n; i++)
			if((i != j)&&(v[i] <= v[j]))
			{
				if(b)
					cout << " ";

				cout << i;
				b = 1;
			}

		for(i = 1; i <= 3 * n; i++)
			if((i != j)&&(v[i] > v[j]))
			{
				if(b)
					cout << " ";

				cout << i;
				b = 1;
			}

		cout << endl;

	}

    //f1.close();
    //f2.close();
     //system("PAUSE");
        return 0;
}


#include <fstream>
#include <iostream>
#include <vector>
#include <string>
#include <list>
#include <map>
#include <algorithm>
#include <math.h>

using namespace std;

typedef long long ll;

ll n, i, j, res[300010], x, p, v[300010];
bool cap[300010], b;

int main()
{
    ifstream f1("input.txt");
    //ofstream f2("output.txt");
	f1 >> n;
	for(i = 1; i <= 3 * n; i++)
	{
		f1 >> x;
		res[x] = i;
	}

	for(i = 1; i <= n; i++)
	{
		f1 >> x >> j >> p;
		if((res[x] < res[j])&&(res[x] < res[p]))
		{
			cap[x] = 1;
		}
		if((res[j] < res[x])&&(res[j] < res[p]))
		{
			cap[j] = 1;
		}
		if((res[p] < res[j])&&(res[p] < res[x]))
		{
			cap[p] = 1;
		}

		v[x] = i;
		v[j] = i;
		v[p] = i;
	}

	f1 >> j;
	if(cap[j] == 0)
	{
		for(i = 1; i <= 3 * n; i++)
		{
			if(i > 1)
				cout << " ";
			cout << i;
		}
		cout << endl;
	}
	else
	{
		b = 0;
		for(i = 1; i <= 3 * n; i++)
			if((i != j)&&(v[i] <= v[j]))
			{
				if(b)
					cout << " ";

				cout << i;
				b = 1;
			}

		for(i = 1; i <= 3 * n; i++)
			if((i != j)&&(v[i] > v[j]))
			{
				if(b)
					cout << " ";

				cout << i;
				b = 1;
			}

		cout << endl;

	}

    f1.close();
    //f2.close();
     system("PAUSE");
        return 0;
}


#include <fstream>
#include <iostream>
#include <vector>
#include <string>
#include <list>
#include <map>
#include <algorithm>
#include <math.h>

using namespace std;

typedef long long ll;

ll n, i, j, res[300010], x, p, v[300010];
bool cap[300010], b;

ll min(ll a, ll b)
{
	if(a > b)
		return b;
	else
		return a;
}

ll max(ll a, ll b)
{
	if(a > b)
		return a;
	else
		return b;
}

int main()
{
    //ifstream f1("input.txt");
    //ofstream f2("output.txt");
	cin >> n;
	for(i = 1; i <= 3 * n; i++)
	{
		cin >> x;
		res[x] = i;
	}

	for(i = 1; i <= n; i++)
	{
		cin >> x >> j >> p;
		if((res[x] < res[j])&&(res[x] < res[p]))
		{
			cap[x] = 1;
		}
		if((res[j] < res[x])&&(res[j] < res[p]))
		{
			cap[j] = 1;
		}
		if((res[p] < res[j])&&(res[p] < res[x]))
		{
			cap[p] = 1;
		}

		v[x] = i;
		v[j] = i;
		v[p] = i;
	}

	cin >> j;
	b = 0;
	if(cap[j] == 0)
	{
		for(i = 1; i <= 3 * n; i++)
			if(i != j)
		{
			if(b)
				cout << " ";
			cout << i;
			b = 1;
		}
		cout << endl;
	}
	else
	{
		b = 0;
		p = 0;
		x = 0;
		for(i = 1; i <= 3 * n; i++)
			if((v[i] == v[j])&&(i != j))
			{
				if(x == 0)
					x = i;
				else
					p = i;
			}

			i = x;
			x = max(x, p);
			p = min(i, p);

			for(i = 1; i <= 3 * n; i++)
				if((v[i] < v[j])&&(i < p))
					cout << i << " ";

			cout << p << " ";
			for(i = 1; i <= 3 * n; i++)
				if((v[i] < v[j])&&(i > p)&&(i < x))
					cout << i << " ";

			cout << x << " ";

			for(i = 1; i <= 3 * n; i++)
				if(v[i] > v[j] || (i > x))
					cout << i << " ";

			cout << endl;
	}

    //f1.close();
    //f2.close();
     //system("PAUSE");
        return 0;
}


#include <fstream>
#include <iostream>
#include <vector>
#include <string>
#include <list>
#include <map>
#include <algorithm>
#include <math.h>

using namespace std;

typedef long long ll;

ll n, i, j, res[300010], x, p, v[300010];
bool cap[300010], b;

ll min(ll a, ll b)
{
	if(a > b)
		return b;
	else
		return a;
}

ll max(ll a, ll b)
{
	if(a > b)
		return a;
	else
		return b;
}

int main()
{
    //ifstream f1("input.txt");
    //ofstream f2("output.txt");
	cin >> n;
	for(i = 1; i <= 3 * n; i++)
	{
		cin >> x;
		res[x] = i;
	}

	for(i = 1; i <= n; i++)
	{
		cin >> x >> j >> p;
		if((res[x] < res[j])&&(res[x] < res[p]))
		{
			cap[x] = 1;
		}
		if((res[j] < res[x])&&(res[j] < res[p]))
		{
			cap[j] = 1;
		}
		if((res[p] < res[j])&&(res[p] < res[x]))
		{
			cap[p] = 1;
		}

		v[x] = i;
		v[j] = i;
		v[p] = i;
	}

	cin >> j;
	b = 0;
	if(cap[j] == 0)
	{
		for(i = 1; i <= 3 * n; i++)
			if(i != j)
		{
			if(b)
				cout << " ";
			cout << i;
			b = 1;
		}
		cout << endl;
	}
	else
	{
		b = 0;
		p = 0;
		x = 0;
		for(i = 1; i <= 3 * n; i++)
			if((v[i] == v[j])&&(i != j))
			{
				if(x == 0)
					x = i;
				else
					p = i;
			}

			i = x;
			x = max(x, p);
			p = min(i, p);

			for(i = 1; i <= 3 * n; i++)
				if((v[i] < v[j])&&(i < p))
					cout << i << " ";

			cout << p << " ";
			for(i = 1; i <= 3 * n; i++)
				if((v[i] < v[j])&&(i > p)&&(i < x))
					cout << i << " ";

			cout << x << " ";

			for(i = 1; i <= 3 * n; i++)
				if(v[i] > v[j])
					cout << i << " ";

			cout << endl;
	}

    //f1.close();
    //f2.close();
     //system("PAUSE");
        return 0;
}


#include <fstream>
#include <iostream>
#include <vector>
#include <string>
#include <list>
#include <map>
#include <algorithm>
#include <math.h>

using namespace std;

typedef long long ll;

ll n, i, j, res[300010], x, p, v[300010];
bool cap[300010], b;

ll min(ll a, ll b)
{
	if(a > b)
		return b;
	else
		return a;
}

ll max(ll a, ll b)
{
	if(a > b)
		return a;
	else
		return b;
}

int main()
{
    //ifstream f1("input.txt");
    //ofstream f2("output.txt");
	cin >> n;
	for(i = 1; i <= 3 * n; i++)
	{
		cin >> x;
		res[x] = i;
	}

	for(i = 1; i <= n; i++)
	{
		cin >> x >> j >> p;
		if((res[x] < res[j])&&(res[x] < res[p]))
		{
			cap[x] = 1;
		}
		if((res[j] < res[x])&&(res[j] < res[p]))
		{
			cap[j] = 1;
		}
		if((res[p] < res[j])&&(res[p] < res[x]))
		{
			cap[p] = 1;
		}

		v[x] = i;
		v[j] = i;
		v[p] = i;
	}

	cin >> j;
	b = 0;
	if(cap[j] == 0)
	{
		for(i = 1; i <= 3 * n; i++)
			if(i != j)
		{
			if(b)
				cout << " ";
			cout << i;
			b = 1;
		}
		cout << endl;
	}
	else
	{
		b = 0;
		p = 0;
		x = 0;
		for(i = 1; i <= 3 * n; i++)
			if((v[i] == v[j])&&(i != j))
			{
				if(x == 0)
					x = i;
				else
					p = i;
			}

			i = x;
			x = max(x, p);
			p = min(i, p);

			for(i = 1; i <= 3 * n; i++)
				if((v[i] < v[j])&&(i < p))
					cout << i << " ";

			cout << p << " ";
			for(i = 1; i <= 3 * n; i++)
				if((v[i] < v[j])&&(i > p)&&(i < x))
					cout << i << " ";

			cout << x << " ";

			for(i = 1; i <= 3 * n; i++)
				if((v[i] > v[j] || (i > x))&&(i != j))
					cout << i << " ";

			cout << endl;
	}

    //f1.close();
    //f2.close();
     //system("PAUSE");
        return 0;
}


#include <fstream>
#include <iostream>
#include <vector>
#include <string>
#include <list>
#include <map>
#include <algorithm>
#include <math.h>

using namespace std;

typedef long long ll;

ll n, i, j, res[300010], x, p, v[300010];
bool cap[300010];

int main()
{
    //ifstream f1("input.txt");
    //ofstream f2("output.txt");
	cin >> n;
	for(i = 1; i <= 3 * n; i++)
	{
		cin >> x;
		res[x] = i;
	}

	for(i = 1; i <= n; i++)
	{
		cin >> x >> j >> p;
		if((res[x] < res[j])&&(res[x] < res[p]))
		{
			cap[x] = 1;
		}
		if((res[j] < res[x])&&(res[j] < res[p]))
		{
			cap[j] = 1;
		}
		if((res[p] < res[j])&&(res[p] < res[x]))
		{
			cap[p] = 1;
		}

		v[x] = i;
		v[j] = i;
		v[p] = i;
	}

	cin >> j;
	if(cap[j] == 0)
	{
		for(i = 1; i <= 3 * n; i++)
		{
			if(i > 1)
				cout << " ";
			cout << i;
		}
		cout << endl;
	}
	else
	{
		for(i = 1; i <= 3 * n; i++)
			if((i != j)&&(v[i] <= v[j]))
				cout << i << " ";

		for(i = 1; i <= 3 * n; i++)
			if((i != j)&&(v[i] > v[j]))
				cout << i << " ";

		cout << endl;

	}

    //f1.close();
    //f2.close();
    // system("PAUSE");
        return 0;
}


#include <fstream>
#include <iostream>
#include <vector>
#include <string>
#include <list>
#include <map>
#include <algorithm>
#include <math.h>

using namespace std;

typedef long long ll;

ll n, i, j, res[300010], x, p, v[300010];
bool cap[300010], b;

int main()
{
    //ifstream f1("input.txt");
    //ofstream f2("output.txt");
	cin >> n;
	for(i = 1; i <= 3 * n; i++)
	{
		cin >> x;
		res[x] = i;
	}

	for(i = 1; i <= n; i++)
	{
		cin >> x >> j >> p;
		if((res[x] < res[j])&&(res[x] < res[p]))
		{
			cap[x] = 1;
		}
		if((res[j] < res[x])&&(res[j] < res[p]))
		{
			cap[j] = 1;
		}
		if((res[p] < res[j])&&(res[p] < res[x]))
		{
			cap[p] = 1;
		}

		v[x] = i;
		v[j] = i;
		v[p] = i;
	}

	cin >> j;
	if(cap[j] == 0)
	{
		for(i = 1; i <= 3 * n; i++)
		{
			if(i > 1)
				cout << " ";
			cout << i;
		}
		cout << endl;
	}
	else
	{
		b = 0;
		for(i = 1; i <= 3 * n; i++)
			if((i != j)&&(v[i] <= v[j]))
			{
				if(b)
					cout << " ";

				cout << i << " ";
				b = 1;
			}

		for(i = 1; i <= 3 * n; i++)
			if((i != j)&&(v[i] > v[j]))
			{
				if(b)
					cout << " ";

				cout << i << " ";
				b = 1;
			}

		cout << endl;

	}

    //f1.close();
    //f2.close();
     //system("PAUSE");
        return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <set>

using namespace std;

typedef struct node {
	int val,pos;
} node;

typedef struct team {
	int x,y,z;
} team;

bool operator<(node a, node b) {
	return a.pos < b.pos;
}

#define MAXN 400000

bool used[MAXN];
set<node> myset;
set<node>::iterator itnode;
set<int> USED,NOTUSED;
set<int>::iterator it1,it2;
team vet[MAXN];
int ans[MAXN],ok,help[MAXN],nh;

int main() {
	int n,k,t;
	node aux;
	scanf("%d",&n);

	for (int i=0; i<3*n; i++) {
		int d;
		scanf("%d",&d);
		aux.val = d;	aux.pos = i;
		myset.insert(aux);
		NOTUSED.insert(d);
		used[d] = false;
	}

	for (int i=0; i<n; i++)
		scanf("%d %d %d",&vet[i].x,&vet[i].y,&vet[i].z);

	scanf("%d",&k);
	ok = 0;
	for (int i=0; i<n; i++) {
		itnode = myset.begin();
		myset.erase(itnode);

		if (used[(*itnode).val]) {
			i--;
			continue;
		}

		if ((*itnode).val == k) {
			int a,b;
			if (k == vet[i].x) {	a = vet[i].y;	b = vet[i].z;	}
			else if (k == vet[i].y) {	a = vet[i].x;	b = vet[i].z;	}
			else { a = vet[i].x;	b = vet[i].y;	}

			if (a > b) {
				int tmp = a;
				a = b;
				b = tmp;
			}

			NOTUSED.erase(vet[i].x);
			NOTUSED.erase(vet[i].y);
			NOTUSED.erase(vet[i].z);

			t = 0;
			nh = 0;
			for (it1 = USED.begin(); it1 != USED.end(); it1++) {
				if (*it1 > a) break;
				ans[t++] = *it1;
				help[nh++] = *it1;
			}

			ans[t++] = a;

			for (;it1 != USED.end(); it1++) {
				if (*it1 > b) break;
				ans[t++] = *it1;
				help[nh++] = *it1;
			}

			for (int j=0; j<nh; j++)
				USED.erase(help[j]);

			ans[t++] = b;
			for (it1 = USED.begin(), it2 = NOTUSED.begin(); it1 != USED.end() && it2 != NOTUSED.end();) {
				if (*it1 < *it2) {
					ans[t++] = *it1;
					it1++;
				}

				else {
					ans[t++] = *it2;
					it2++;
				}
			}

			while (it1 != USED.end()) {	ans[t++] = *it1;	it1++;	}
			while (it2 != NOTUSED.end()) {	ans[t++] = *it2;	it2++;	}

			ok = 1;
			break;
		}

		else {
			USED.insert(vet[i].x);	NOTUSED.erase(vet[i].x);
			USED.insert(vet[i].y);	NOTUSED.erase(vet[i].y);
			USED.insert(vet[i].z);	NOTUSED.erase(vet[i].z);
			used[vet[i].x] = true;
			used[vet[i].y] = true;
			used[vet[i].z] = true;
		}
	}

	if (!ok) {
		t = 0;
		for (int i=1; i<=3*n; i++)
			if (i != k) ans[t++] = i;
	}

	for (int i=0; i<t; i++) {
		if (i) printf(" ");
		printf("%d",ans[i]);
	}
	printf("\n");

	return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <set>

using namespace std;

typedef struct node {
	int val,pos;
} node;

typedef struct team {
	int x,y,z;
} team;

bool operator<(node a, node b) {
	return a.pos < b.pos;
}

#define MAXN 400000

bool used[MAXN];
set<node> myset;
set<node>::iterator itnode;
set<int> USED,NOTUSED;
set<int>::iterator it1,it2;
team vet[MAXN];
int ans[MAXN],ok;

int main() {
	int n,k,t;
	node aux;
	scanf("%d",&n);

	for (int i=0; i<3*n; i++) {
		int d;
		scanf("%d",&d);
		aux.val = d;	aux.pos = i;
		myset.insert(aux);
		NOTUSED.insert(d);
		used[d] = false;
	}

	for (int i=0; i<n; i++)
		scanf("%d %d %d",&vet[i].x,&vet[i].y,&vet[i].z);

	scanf("%d",&k);
	ok = 0;
	for (int i=0; i<n; i++) {
		itnode = myset.begin();
		myset.erase(itnode);

		if (used[(*itnode).val]) continue;

		if ((*itnode).val == k) {
			int a,b;
			if (k == vet[i].x) {	a = vet[i].y;	b = vet[i].z;	}
			else if (k == vet[i].y) {	a = vet[i].x;	b = vet[i].z;	}
			else { a = vet[i].x;	b = vet[i].y;	}

			if (a > b) {
				int tmp = a;
				a = b;
				b = tmp;
			}

			NOTUSED.erase(vet[i].x);
			NOTUSED.erase(vet[i].y);
			NOTUSED.erase(vet[i].z);

			t = 0;
			for (it1 = USED.begin(); it1 != USED.end(); it1++) {
				if (*it1 > a) break;
				ans[t++] = *it1;
				USED.erase(*it1);
			}

			ans[t++] = a;

			for (it1 = USED.begin(); it1 != USED.end(); it1++) {
				if (*it1 > b) break;
				ans[t++] = *it1;
				USED.erase(*it1);
			}

			ans[t++] = b;
			for (it1 = USED.begin(), it2 = NOTUSED.begin(); it1 != USED.end() && it2 != NOTUSED.end();) {
				if (*it1 < *it2) {
					ans[t++] = *it1;
					it1++;
				}

				else {
					ans[t++] = *it2;
					it2++;
				}
			}

			while (it1 != USED.end()) {	ans[t++] = *it1;	it1++;	}
			while (it2 != NOTUSED.end()) {	ans[t++] = *it2;	it2++;	}

			ok = 1;
			break;
		}

		else {
			USED.insert(vet[i].x);	NOTUSED.erase(vet[i].x);
			USED.insert(vet[i].y);	NOTUSED.erase(vet[i].y);
			USED.insert(vet[i].z);	NOTUSED.erase(vet[i].z);
			used[vet[i].x] = true;
			used[vet[i].y] = true;
			used[vet[i].z] = true;
		}
	}

	if (!ok) {
		t = 0;
		for (int i=1; i<=3*n; i++)
			if (i != k) ans[t++] = i;
	}

	for (int i=0; i<t; i++) {
		if (i) printf(" ");
		printf("%d",ans[i]);
	}
	printf("\n");

	return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <set>

using namespace std;

typedef struct node {
	int val,pos;
} node;

typedef struct team {
	int x,y,z;
} team;

bool operator<(node a, node b) {
	return a.pos < b.pos;
}

#define MAXN 400000

bool used[MAXN];
set<node> myset;
set<node>::iterator itnode;
set<int> USED,NOTUSED;
set<int>::iterator it1,it2;
team vet[MAXN];
int ans[MAXN],ok;

int main() {
	int n,k,t;
	node aux;
	scanf("%d",&n);

	for (int i=0; i<3*n; i++) {
		int d;
		scanf("%d",&d);
		aux.val = d;	aux.pos = i;
		myset.insert(aux);
		NOTUSED.insert(d);
		used[d] = false;
	}

	for (int i=0; i<n; i++)
		scanf("%d %d %d",&vet[i].x,&vet[i].y,&vet[i].z);

	scanf("%d",&k);
	ok = 0;
	for (int i=0; i<n; i++) {
		itnode = myset.begin();
		myset.erase(itnode);

		if (used[(*itnode).val]) {
			i--;
			continue;
		}

		if ((*itnode).val == k) {
			int a,b;
			if (k == vet[i].x) {	a = vet[i].y;	b = vet[i].z;	}
			else if (k == vet[i].y) {	a = vet[i].x;	b = vet[i].z;	}
			else { a = vet[i].x;	b = vet[i].y;	}

			if (a > b) {
				int tmp = a;
				a = b;
				b = tmp;
			}

			NOTUSED.erase(vet[i].x);
			NOTUSED.erase(vet[i].y);
			NOTUSED.erase(vet[i].z);

			t = 0;
			for (it1 = USED.begin(); it1 != USED.end(); it1++) {
				if (*it1 > a) break;
				ans[t++] = *it1;
				USED.erase(*it1);
			}

			ans[t++] = a;

			for (it1 = USED.begin(); it1 != USED.end(); it1++) {
				if (*it1 > b) break;
				ans[t++] = *it1;
				USED.erase(*it1);
			}

			ans[t++] = b;
			for (it1 = USED.begin(), it2 = NOTUSED.begin(); it1 != USED.end() && it2 != NOTUSED.end();) {
				if (*it1 < *it2) {
					ans[t++] = *it1;
					it1++;
				}

				else {
					ans[t++] = *it2;
					it2++;
				}
			}

			while (it1 != USED.end()) {	ans[t++] = *it1;	it1++;	}
			while (it2 != NOTUSED.end()) {	ans[t++] = *it2;	it2++;	}

			ok = 1;
			break;
		}

		else {
			USED.insert(vet[i].x);	NOTUSED.erase(vet[i].x);
			USED.insert(vet[i].y);	NOTUSED.erase(vet[i].y);
			USED.insert(vet[i].z);	NOTUSED.erase(vet[i].z);
			used[vet[i].x] = true;
			used[vet[i].y] = true;
			used[vet[i].z] = true;
		}
	}

	if (!ok) {
		t = 0;
		for (int i=1; i<=3*n; i++)
			if (i != k) ans[t++] = i;
	}

	for (int i=0; i<t; i++) {
		if (i) printf(" ");
		printf("%d",ans[i]);
	}
	printf("\n");

	return 0;
}


#include <iomanip>
#include <algorithm>
#include <map>
#include <fstream>
#include <stack>
#include <queue>
#include <vector>
#include <cmath>
#include <iostream>
#include <string>
#include <set>

#include <time.h>
#include <sys/time.h>

using namespace std;

int q[100100];
int w[100100];
int e[100100];
int n;
int a[300100];
int b[300100];
bool was[300100];
int o = 0;

int main()
{
//	freopen("anarc05b.in","r",stdin);
//	freopen("anarc05b.out","w",stdout);
	scanf("%d",&n);

	for (int i = 0; i < 3*n; i++)
	{
		int x;
		scanf("%d",&x);
		x--;
		a[x] = i;
		was[i] = false;
	}

	for (int i = 0; i < n; i++)
	{
		scanf("%d%d%d",&q[i],&w[i],&e[i]);
		q[i]--; w[i]--; e[i]--;
	}

	int x;
	scanf("%d",&x);
	x--;

	for (int i = 0; i < n; i++)
	{
		if (q[i] == x || w[i] == x || e[i] == x)
		{
			if (q[i] != x) swap(q[i],w[i]);
			if (q[i] != x) swap(q[i],e[i]);
			int y = w[i],z = e[i];

			was[x] = true; was[y] = true;
			was[z] = true;

			if (a[x] > a[y] || a[x] > a[z])
			{
				for (int j = 0; j < 3*n; j++)
					if (x != j) printf("%d ",j+1);
				return 0;
			}
			b[o] = y;
			b[o+1] = z;

			o+=2;

			sort(b,b+o);

			int ns = 0;

			for (int j = o-1; j >= 0; j--)
				if (b[j] == y || b[j] == z) { ns = j+1; break; }

			for (int j = 0; j < 3*n; j++)
			{
				if (was[j]) continue;
				b[o] = j;
				o++;
			}

			sort(b+ns,b+o);

			for (int j = 0; j < o; j++)
				printf("%d ",b[j]+1);

			return 0;
		}
		else
		{
			b[o] = q[i];
			b[o+1] = w[i];
			b[o+2] = e[i];
			o += 3;
			was[q[i]] = true;
			was[w[i]] = true;
			was[e[i]] = true;
		}
	}

//	in.getline(s);

	return 0;
}


#include <iomanip>
#include <algorithm>
#include <map>
#include <fstream>
#include <stack>
#include <queue>
#include <vector>
#include <cmath>
#include <iostream>
#include <string>
#include <set>

#include <time.h>
#include <sys/time.h>

using namespace std;

int q[100100];
int w[100100];
int e[100100];
int n;
int a[300100];
int b[300100];
bool was[300100];
int o = 0;

int main()
{
//	freopen("anarc05b.in","r",stdin);
//	freopen("anarc05b.out","w",stdout);
	scanf("%d",&n);

	for (int i = 0; i < 3*n; i++)
	{
		int x;
		scanf("%d",&x);
		x--;
		a[x] = i;
		was[i] = false;
	}

	for (int i = 0; i < n; i++)
	{
		scanf("%d%d%d",&q[i],&w[i],&e[i]);
		q[i]--; w[i]--; e[i]--;
	}

	int x;
	scanf("%d",&x);
	x--;

	for (int i = 0; i < n; i++)
	{
		if (q[i] == x || w[i] == x || e[i] == x)
		{
			if (q[i] != x) swap(q[i],w[i]);
			if (q[i] != x) swap(q[i],e[i]);
			int y = w[i],z = e[i];

			was[x] = true; was[y] = true;
			was[z] = true;

			if (a[x] > a[y] || a[x] > a[z])
			{
				for (int j = 0; j < 3*n; j++)
					if (x != j) printf("%d ",j+1);
				return 0;
			}
			b[o] = y;
			b[o+1] = z;

			o+=2;

			sort(b,b+o);

			int ns = 0;

			for (int j = o-1; j >= 0; j--)
				if (b[j] == y || b[j] == z) { ns = j+1; break; }

			for (int j = 0; j < 3*n; j++)
			{
				if (was[j]) continue;
				b[o] = j;
				o++;
			}

//			cout << ns << " " << o << endl;

			sort(b+ns,b+o);

			for (int j = 0; j < o; j++)
				printf("%d ",b[j]+1);

			return 0;
		}
		else
		{
			b[o] = q[i];
			b[o+1] = w[i];
			b[o+2] = e[i];
			o += 3;
			was[q[i]] = true;
			was[w[i]] = true;
			was[e[i]] = true;
		}
	}

//	in.getline(s);

	return 0;
}


#include <iomanip>
#include <algorithm>
#include <map>
#include <fstream>
#include <stack>
#include <queue>
#include <vector>
#include <cmath>
#include <iostream>
#include <string>
#include <set>

#include <time.h>
#include <sys/time.h>

using namespace std;

int q[100100];
int w[100100];
int e[100100];
int n;
int a[300100];
int b[300100];
bool was[300100];
int o = 0;

int main()
{
//	freopen("anarc05b.in","r",stdin);
//	freopen("anarc05b.out","w",stdout);
	scanf("%d",&n);

	for (int i = 0; i < 3*n; i++)
	{
		int x;
		scanf("%d",&x);
		x--;
		a[x] = i;
		was[i] = false;
	}

	for (int i = 0; i < n; i++)
	{
		scanf("%d%d%d",&q[i],&w[i],&e[i]);
		q[i]--; w[i]--; e[i]--;
	}

	int x;
	scanf("%d",&x);
	x--;

	for (int i = 0; i < n; i++)
	{
		if (q[i] == x || w[i] == x || e[i] == x)
		{
			if (q[i] != x) swap(q[i],w[i]);
			if (q[i] != x) swap(q[i],e[i]);
			int y = w[i],z = e[i];

			was[x] = true; was[y] = true;
			was[z] = true;

			if (a[x] > a[y] || a[x] > a[z])
			{
				for (int j = 0; j < 3*n; j++)
					if (x != j) printf("%d ",j+1);
				return 0;
			}
			b[o] = y;
			b[o+1] = z;

			o+=2;

			sort(b,b+o);

			int ns = 0;

			for (int j = o; j >= 0; j--)
				if (b[j] == y || b[j] == z) { ns = j+1; break; }

			for (int j = 0; j < 3*n; j++)
			{
				if (was[j]) continue;
				b[o] = j;
				o++;
			}

			sort(b+ns,b+o);

			for (int j = 0; j < o; j++)
				printf("%d ",b[j]+1);

			return 0;
		}
		else
		{
			b[o] = q[i];
			b[o+1] = w[i];
			b[o+2] = e[i];
			o += 3;
			was[q[i]] = true;
			was[w[i]] = true;
			was[e[i]] = true;
		}
	}

//	in.getline(s);

	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <climits>
#include <queue>
#include <algorithm>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <cstring>
#include <cmath>
using namespace std;

//trocar para 0 para desabilitar output
#if 1
#define DEBUG(x) cout << x << endl
#define PAUSE() cin.get(); cin.get()
#else
#define DEBUG(x)
#define PAUSE()
#endif

#define TRACE(x) DEBUG(#x << " = " << x)
#define DEBUGS() DEBUG("***************************")
#define MAX 100001

int person_team[3*MAX];
int team_person[MAX][3];
int score[MAX];
bool mark[MAX];

vector<int> solve(int k, int n){
    vector<int> before;
    fill(mark, mark+n, false);
    for (int i = 0; i < n; ++i){
        if (!mark[score[i]]){
            int team = person_team[score[i]];
            for (int j = 0; j < 3; ++j){
                if (score[i] != team_person[team][j]) before.push_back(team_person[team][j]);
                mark[team_person[team][j]] = true;
            }
            if (score[i] == k){
                vector<int> after;
                for (int j = 0; j < n; ++j)
                    if (!mark[j]) after.push_back(j);
                sort(before.begin(), before.end());
                sort(after.begin(), after.end());
                before.insert(before.end(), after.begin(), after.end());
                return before;
            }
            before.push_back(score[i]);
        }
    }
    sort(before.begin(), before.end());
    return before;
}

int main(){
    int n, k;
    scanf("%d", &n);
    for (int i = 0; i < 3*n; ++i){
        scanf("%d", &score[i]);
        --score[i];
        mark[i] = false;
    }
    for (int i = 0; i < n; ++i){
        for (int j = 0; j < 3; ++j){
            scanf("%d", &team_person[i][j]);
            --team_person[i][j];
            person_team[team_person[i][j]] = i;
        }
    }
    scanf("%d", &k);
    vector<int> ans = solve(k-1, 3*n);
    for (int i = 0; i < ans.size(); ++i) printf("%d ", ans[i]+1);
    puts("");
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <climits>
#include <queue>
#include <algorithm>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <cstring>
#include <cmath>
using namespace std;

//trocar para 0 para desabilitar output
#if 1
#define DEBUG(x) cout << x << endl
#define PAUSE() cin.get(); cin.get()
#else
#define DEBUG(x)
#define PAUSE()
#endif

#define TRACE(x) DEBUG(#x << " = " << x)
#define DEBUGS() DEBUG("***************************")
#define MAX 100001

int person_team[3*MAX];
int team_person[MAX][3];
int score[3*MAX];
bool mark[3*MAX];

vector<int> solve(int k, int n){
    vector<int> before;
    fill(mark, mark+n, false);
    for (int i = 0; i < n; ++i){
        if (!mark[score[i]]){
            int team = person_team[score[i]];
            int major = -1;
            for (int j = 0; j < 3; ++j){
                if (score[i] != team_person[team][j]){
                    before.push_back(team_person[team][j]);
                    major = max(major, team_person[team][j]);
                }
                mark[team_person[team][j]] = true;
            }
            if (score[i] == k){
                vector<int> before2, after;
                for (int j = 0; j < before.size(); ++j){
                    if (before[j] <= major) before2.push_back(before[j]);
                    else after.push_back(before[j]);
                }
                for (int j = 0; j < n; ++j)
                    if (!mark[j]) after.push_back(j);
                sort(before2.begin(), before2.end());
                sort(after.begin(), after.end());
                before2.insert(before2.end(), after.begin(), after.end());
                return before2;
            }
            before.push_back(score[i]);
        }
    }
    sort(before.begin(), before.end());
    return before;
}

int main(){
    int n, k;
    scanf("%d", &n);
    for (int i = 0; i < 3*n; ++i){
        scanf("%d", &score[i]);
        --score[i];
        mark[i] = false;
    }
    for (int i = 0; i < n; ++i){
        for (int j = 0; j < 3; ++j){
            scanf("%d", &team_person[i][j]);
            --team_person[i][j];
            person_team[team_person[i][j]] = i;
        }
    }
    scanf("%d", &k);
    vector<int> ans = solve(k-1, 3*n);
    for (int i = 0; i < ans.size(); ++i) if (ans[i]+1 != k) printf("%d ", ans[i]+1);
    puts("");
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <climits>
#include <queue>
#include <algorithm>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <cstring>
#include <cmath>
using namespace std;

//trocar para 0 para desabilitar output
#if 1
#define DEBUG(x) cout << x << endl
#define PAUSE() cin.get(); cin.get()
#else
#define DEBUG(x)
#define PAUSE()
#endif

#define TRACE(x) DEBUG(#x << " = " << x)
#define DEBUGS() DEBUG("***************************")
#define MAX 100001

int person_team[3*MAX];
int team_person[MAX][3];
int score[MAX];
bool mark[MAX];

vector<int> solve(int k, int n){
    vector<int> before;
    fill(mark, mark+n, false);
    for (int i = 0; i < n; ++i){
        if (!mark[score[i]]){
            int team = person_team[score[i]];
            int major = -1;
            for (int j = 0; j < 3; ++j){
                if (score[i] != team_person[team][j]){
                    before.push_back(team_person[team][j]);
                    major = max(major, team_person[team][j]);
                }
                mark[team_person[team][j]] = true;
            }
            if (score[i] == k){
                vector<int> before2, after;
                for (int j = 0; j < before.size(); ++j){
                    if (before[j] <= major) before2.push_back(before[j]);
                    else after.push_back(before[j]);
                }
                for (int j = 0; j < n; ++j)
                    if (!mark[j]) after.push_back(j);
                sort(before2.begin(), before2.end());
                sort(after.begin(), after.end());
                before2.insert(before2.end(), after.begin(), after.end());
                return before2;
            }
            before.push_back(score[i]);
        }
    }
    sort(before.begin(), before.end());
    return before;
}

int main(){
    int n, k;
    scanf("%d", &n);
    for (int i = 0; i < 3*n; ++i){
        scanf("%d", &score[i]);
        --score[i];
        mark[i] = false;
    }
    for (int i = 0; i < n; ++i){
        for (int j = 0; j < 3; ++j){
            scanf("%d", &team_person[i][j]);
            --team_person[i][j];
            person_team[team_person[i][j]] = i;
        }
    }
    scanf("%d", &k);
    vector<int> ans = solve(k-1, 3*n);
    for (int i = 0; i < ans.size(); ++i) if (ans[i]+1 != k) printf("%d ", ans[i]+1);
    puts("");
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <climits>
#include <queue>
#include <algorithm>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <cstring>
#include <cmath>
using namespace std;

//trocar para 0 para desabilitar output
#if 1
#define DEBUG(x) cout << x << endl
#define PAUSE() cin.get(); cin.get()
#else
#define DEBUG(x)
#define PAUSE()
#endif

#define TRACE(x) DEBUG(#x << " = " << x)
#define DEBUGS() DEBUG("***************************")
#define MAX 100001

int person_team[3*MAX];
int team_person[MAX][3];
int score[MAX];
bool mark[MAX];

vector<int> solve(int k, int n){
    vector<int> before;
    fill(mark, mark+n, false);
    for (int i = 0; i < n; ++i){
        if (!mark[score[i]]){
            int team = person_team[score[i]];
            for (int j = 0; j < 3; ++j){
                if (score[i] != team_person[team][j]) before.push_back(team_person[team][j]);
                mark[team_person[team][j]] = true;
            }
            if (score[i] == k){
                vector<int> after;
                for (int j = 0; j < n; ++j)
                    if (!mark[j]) after.push_back(j);
                sort(before.begin(), before.end());
                sort(after.begin(), after.end());
                before.insert(before.end(), after.begin(), after.end());
                return before;
            }
            before.push_back(score[i]);
        }
    }
    sort(before.begin(), before.end());
    return before;
}

int main(){
    int n, k;
    scanf("%d", &n);
    for (int i = 0; i < 3*n; ++i){
        scanf("%d", &score[i]);
        --score[i];
        mark[i] = false;
    }
    for (int i = 0; i < n; ++i){
        for (int j = 0; j < 3; ++j){
            scanf("%d", &team_person[i][j]);
            --team_person[i][j];
            person_team[team_person[i][j]] = i;
        }
    }
    scanf("%d", &k);
    vector<int> ans = solve(k-1, 3*n);
    for (int i = 0; i < ans.size(); ++i) if (ans[i]+1 != k) printf("%d ", ans[i]+1);
    puts("");
    return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#define maxn 100001
using   namespace std;

bool    flag;
int     n,aim,now;
int     rank[maxn*3+1],a[maxn][3],b[maxn*3],c[maxn*3],ans[maxn*3];

int     find(int aim)
{
        for (int i=1; i<=n; i++)
                for (int j=0; j<3; j++)
                        if (a[i][j]==aim) return i;
}

void    swap(int &x, int &y)
{
        x=x+y;
        y=x-y;
        x=x-y;
}

void    init()
{
        cin>>n; int x;
        for (int i=1; i<=n*3; i++)
        {
                scanf("%d",&x);
                rank[x]=i;
        }
        for (int i=1; i<=n; i++)
                for (int j=0; j<3; j++) scanf("%d",&a[i][j]);
        cin>>aim;
        now=find(aim);
        if (a[now][1]==aim) swap(a[now][1],a[now][0]); else
        if (a[now][2]==aim) swap(a[now][2],a[now][0]);
        if (rank[a[now][0]]<rank[a[now][1]] && rank[a[now][0]]<rank[a[now][2]]) flag=true; else flag=false;
}

void    slove()
{
        int tot=0;
        if (!flag)
        {
                for (int i=1; i<=3*n; i++)
                        if (i!=aim)
                        {
                                tot++;
                                if (tot>1) printf(" ");
                                printf("%d",i);
                        }
                printf("\n");
        } else
        {
                int t1=-1 , t2=-1;
                for (int i=1; i<now; i++)
                        for (int j=0; j<3; j++)
                                b[++t1]=a[i][j];
                b[++t1]=a[now][1]; b[++t1]=a[now][2];
                for (int i=now+1; i<=n; i++)
                        for (int j=0; j<3; j++)
                                c[++t2]=a[i][j];
                sort(b,b+t1+1); sort(c,c+t2+1);
                int big=max(a[now][1],a[now][2]);
                int l1=0 , l2=0; tot=0;
                while (b[l1]!=big) ans[++tot]=b[l1++];
                ans[++tot]=b[l1++];
                while (l1<=t1 || l2<=t2)
                        if (l1<t1 && b[l1]<c[l2]) ans[++tot]=b[l1++]; else ans[++tot]=c[l2++];
                for (int i=1; i<3*n-1; i++) printf("%d ",ans[i]);
                printf("%d\n",ans[3*n-1]);
        }
}

int     main()
{
        init();
        slove();
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#define maxn 100002
using   namespace std;

bool    flag;
int     n,aim,now;
int     rank[maxn*3+1],a[maxn][3],b[maxn*3+1],c[maxn*3+1],ans[maxn*3+1];

int     find(int aim)
{
        for (int i=1; i<=n; i++)
                for (int j=0; j<3; j++)
                        if (a[i][j]==aim) return i;
}

void    swap(int &x, int &y)
{
        x=x+y;
        y=x-y;
        x=x-y;
}

void    init()
{
        cin>>n; int x;
        for (int i=1; i<=n*3; i++)
        {
                scanf("%d",&x);
                rank[x]=i;
        }
        for (int i=1; i<=n; i++)
                for (int j=0; j<3; j++) scanf("%d",&a[i][j]);
        cin>>aim;
        now=find(aim);
        if (a[now][1]==aim) swap(a[now][1],a[now][0]); else
        if (a[now][2]==aim) swap(a[now][2],a[now][0]);
        if (rank[a[now][0]]<rank[a[now][1]] && rank[a[now][0]]<rank[a[now][2]]) flag=true; else flag=false;
}

void    slove()
{
        int tot=0;
        if (!flag)
        {
                for (int i=1; i<=3*n; i++)
                        if (i!=aim)
                        {
                                tot++;
                                if (tot>1) printf(" ");
                                printf("%d",i);
                        }
                printf("\n");
        } else
        {
                int t1=-1 , t2=-1;
                for (int i=1; i<now; i++)
                        for (int j=0; j<3; j++)
                                b[++t1]=a[i][j];

                b[++t1]=a[now][1]; b[++t1]=a[now][2];
                for (int i=now+1; i<=n; i++)
                        for (int j=0; j<3; j++)
                                c[++t2]=a[i][j];

                if (t1>=0) sort(b,b+t1+1);
                if (t2>=0) sort(c,c+t2+1);
                int big=max(a[now][1],a[now][2]);
                int l1=0 , l2=0; tot=0;
                while (b[l1]!=big) ans[++tot]=b[l1++];
                ans[++tot]=b[l1++];
                while (l1<=t1 || l2<=t2)
                        if (l1<t1 && b[l1]<c[l2]) ans[++tot]=b[l1++]; else ans[++tot]=c[l2++];
                for (int i=1; i<3*n-1; i++) printf("%d ",ans[i]);
                printf("%d\n",ans[3*n-1]);
        }
}

int     main()
{
        init();
        slove();
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#define maxn 100002
using   namespace std;

bool    flag;
int     n,aim,now;
int     rank[maxn*3+1],a[maxn][3],b[maxn*3+1],c[maxn*3+1],ans[maxn*3+1];

int     find(int aim)
{
        for (int i=1; i<=n; i++)
                for (int j=0; j<3; j++)
                        if (a[i][j]==aim) return i;
}

void    swap(int &x, int &y)
{
        x=x+y;
        y=x-y;
        x=x-y;
}

void    init()
{
        cin>>n; int x;
        for (int i=1; i<=n*3; i++)
        {
                scanf("%d",&x);
                rank[x]=i;
        }
        for (int i=1; i<=n; i++)
                for (int j=0; j<3; j++) scanf("%d",&a[i][j]);
        cin>>aim;
        now=find(aim);
        if (a[now][1]==aim) swap(a[now][1],a[now][0]); else
        if (a[now][2]==aim) swap(a[now][2],a[now][0]);
        if (rank[a[now][0]]<rank[a[now][1]] && rank[a[now][0]]<rank[a[now][2]]) flag=true; else flag=false;
}

void    slove()
{
        int tot=0;
        if (!flag)
        {
                for (int i=1; i<=3*n; i++)
                        if (i!=aim)
                        {
                                tot++;
                                if (tot>1) printf(" ");
                                printf("%d",i);
                        }
                printf("\n");
        } else
        {
                int t1=-1 , t2=-1;
                for (int i=1; i<now; i++)
                        for (int j=0; j<3; j++)
                                b[++t1]=a[i][j];

                b[++t1]=a[now][1]; b[++t1]=a[now][2];
                for (int i=now+1; i<=n; i++)
                        for (int j=0; j<3; j++)
                                c[++t2]=a[i][j];

                if (t1>=0) sort(b,b+t1+1);
                if (t2>=0) sort(c,c+t2+1);
                c[t2+1]=2147483647;
                int big=max(a[now][1],a[now][2]);
                int l1=0 , l2=0; tot=0;
                while (b[l1]!=big) ans[++tot]=b[l1++];
                ans[++tot]=b[l1++];
                while (l1<=t1 || l2<=t2)
                        if (l1<=t1 && b[l1]<c[l2]) ans[++tot]=b[l1++]; else ans[++tot]=c[l2++];
                for (int i=1; i<3*n-1; i++) printf("%d ",ans[i]);
                printf("%d\n",ans[3*n-1]);
        }
}

int     main()
{
        init();
        slove();
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#define maxn 100002
using   namespace std;

bool    flag;
int     n,aim,now;
int     rank[maxn*3+1],a[maxn][3],b[maxn*3+1],c[maxn*3+1],ans[maxn*3+1];

int     find(int aim)
{
        for (int i=1; i<=n; i++)
                for (int j=0; j<3; j++)
                        if (a[i][j]==aim) return i;
}

void    swap(int &x, int &y)
{
        x=x+y;
        y=x-y;
        x=x-y;
}

void    init()
{
        cin>>n; int x;
        for (int i=1; i<=n*3; i++)
        {
                scanf("%d",&x);
                rank[x]=i;
        }
        for (int i=1; i<=n; i++)
                for (int j=0; j<3; j++) scanf("%d",&a[i][j]);
        cin>>aim;
        now=find(aim);
        if (a[now][1]==aim) swap(a[now][1],a[now][0]); else
        if (a[now][2]==aim) swap(a[now][2],a[now][0]);
        if (rank[a[now][0]]<rank[a[now][1]] && rank[a[now][0]]<rank[a[now][2]]) flag=true; else flag=false;
}

void    slove()
{
        int tot=0;
        if (!flag)
        {
                for (int i=1; i<=3*n; i++)
                        if (i!=aim)
                        {
                                tot++;
                                if (tot>1) printf(" ");
                                printf("%d",i);
                        }
                printf("\n");
        } else
        {
                int t1=-1 , t2=-1;
                for (int i=1; i<now; i++)
                        for (int j=0; j<3; j++)
                                b[++t1]=a[i][j];

                b[++t1]=a[now][1]; b[++t1]=a[now][2];
                for (int i=now+1; i<=n; i++)
                        for (int j=0; j<3; j++)
                                c[++t2]=a[i][j];

                if (t1>=0) sort(b,b+t1+1); c[l2+1]=2147483647;
                if (t2>=0) sort(c,c+t2+1);
                int big=max(a[now][1],a[now][2]);
                int l1=0 , l2=0; tot=0;
                while (b[l1]!=big) ans[++tot]=b[l1++];
                ans[++tot]=b[l1++];
                while (l1<=t1 || l2<=t2)
                        if (l1<=t1 && b[l1]<c[l2]) ans[++tot]=b[l1++]; else ans[++tot]=c[l2++];
                for (int i=1; i<3*n-1; i++) printf("%d ",ans[i]);
                printf("%d\n",ans[3*n-1]);
        }
}

int     main()
{
        init();
        slove();
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#define maxn 100002
using   namespace std;

bool    flag;
int     n,aim,now;
int     rank[maxn*3+1],a[maxn][3],b[maxn*3+1],c[maxn*3+1],ans[maxn*3+1];

int     find(int aim)
{
        for (int i=1; i<=n; i++)
                for (int j=0; j<3; j++)
                        if (a[i][j]==aim) return i;
}

void    swap(int &x, int &y)
{
        x=x+y;
        y=x-y;
        x=x-y;
}

void    init()
{
        cin>>n; int x;
        for (int i=1; i<=n*3; i++)
        {
                scanf("%d",&x);
                rank[x]=i;
        }
        for (int i=1; i<=n; i++)
                for (int j=0; j<3; j++) scanf("%d",&a[i][j]);
        cin>>aim;
        now=find(aim);
        if (a[now][1]==aim) swap(a[now][1],a[now][0]); else
        if (a[now][2]==aim) swap(a[now][2],a[now][0]);
        if (rank[a[now][0]]<rank[a[now][1]] && rank[a[now][0]]<rank[a[now][2]]) flag=true; else flag=false;
}

void    slove()
{
        int tot=0;
        if (!flag)
        {
                for (int i=1; i<=3*n; i++)
                        if (i!=aim)
                        {
                                tot++;
                                if (tot>1) printf(" ");
                                printf("%d",i);
                        }
                printf("\n");
        } else
        {
                int t1=-1 , t2=-1;
                for (int i=1; i<now; i++)
                        for (int j=0; j<3; j++)
                                b[++t1]=a[i][j];

                b[++t1]=a[now][1]; b[++t1]=a[now][2];
                for (int i=now+1; i<=n; i++)
                        for (int j=0; j<3; j++)
                                c[++t2]=a[i][j];

                if (t1>=0) sort(b,b+t1+1);
                if (t2>=0) sort(c,c+t2+1);
                int big=max(a[now][1],a[now][2]);
                int l1=0 , l2=0; tot=0;
                while (b[l1]!=big) ans[++tot]=b[l1++];
                ans[++tot]=b[l1++];
                while (l1<=t1 || l2<=t2)
                        if (l1<=t1 && b[l1]<c[l2]) ans[++tot]=b[l1++]; else ans[++tot]=c[l2++];
                for (int i=1; i<3*n-1; i++) printf("%d ",ans[i]);
                printf("%d\n",ans[3*n-1]);
        }
}

int     main()
{
        init();
        slove();
}


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

int N,P[300001],S[100000][3],t,t1,t2,L1,L2,a=0,b=0,cnt;
bool cap[300001],flag;
vector <int> A,B;

bool cmp(int a,int b){
    return P[a] < P[b];
}

int main(){
    scanf("%d",&N);
    for(int i=0;i<3*N;++i){
        scanf("%d",&t);
        P[t] = i;
    }
    for(int i=0;i<N;++i){
        scanf("%d%d%d",&S[i][0],&S[i][1],&S[i][2]);
        sort(S[i],S[i]+3,cmp);
        cap[S[i][0]] = 1;
    }
    scanf("%d",&t);
    if(cap[t] == 0){
        for(int i=1;i<=3*N;++i)
            if(i != t) printf("%d ",i);
        printf("\n");
    //  system("pause");
        return 0;
    }
    for(int i=0;i<N;++i){
        if(S[i][0] == t){
            flag = 1;
            t1 = S[i][1];
            t2 = S[i][2];
            A.push_back(t1);
            A.push_back(t2);
            continue;
        }
        for(int j=0;j<3;++j){
            if(!flag) A.push_back(S[i][j]);
            else B.push_back(S[i][j]);
        }
    }
    sort(A.begin(),A.end());
    sort(B.begin(),B.end());
    L1 = A.size();
    L2 = B.size();
    while(a < L1 || b < L2){
        if(b == L2) printf("%d ",A[a++]);
        else if(a == l2) printf("%d ",B[b++]);
        else if(cnt < 2){
            cnt += (A[a] == t1 || A[a] == t2);
            printf("%d ",A[a++]);
        }
        else if(A[a] <= B[b]) printf("%d ",A[a++]);
        else printf("%d ",B[b++]);
    }
    printf("\n");
//  system("pause");
}


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

int N,P[300001],S[100000][3],t,t1,t2,L1,L2,a=0,b=0,cnt;
bool cap[300001],flag;
vector <int> A,B;

bool cmp(int a,int b){
    return P[a] < P[b];
}

int main(){
    scanf("%d",&N);
    for(int i=0;i<3*N;++i){
        scanf("%d",&t);
        P[t] = i;
    }
    for(int i=0;i<N;++i){
        scanf("%d%d%d",&S[i][0],&S[i][1],&S[i][2]);
        sort(S[i],S[i]+3,cmp);
        cap[S[i][0]] = 1;
    }
    scanf("%d",&t);
    if(cap[t] == 0){
        for(int i=1;i<=3*N;++i)
            if(i != t) printf("%d ",i);
        printf("\n");
    //  system("pause");
        return 0;
    }
    for(int i=0;i<N;++i){
        if(S[i][0] == t){
            flag = 1;
            t1 = S[i][1];
            t2 = S[i][2];
            A.push_back(t1);
            A.push_back(t2);
            continue;
        }
        for(int j=0;j<3;++j){
            if(!flag) A.push_back(S[i][j]);
            else B.push_back(S[i][j]);
        }
    }
    sort(A.begin(),A.end());
    sort(B.begin(),B.end());
    L1 = A.size();
    L2 = B.size();
    while(a < L1 || b < L2){
    if(a < L1 && (A[a] == t1 || A[a] == t2)){
        printf("%d ",A[a++]);
        ++cnt;
        }
        else if(cnt < 2) printf("%d ",A[a++]);
    else{
        if(b == L2 || (a < L1 && A[a] <= B[b])) printf("%d ",A[a++]);
        else printf("%d ",B[b++]);
    }
  }
    printf("\n");
//  system("pause");
}


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

int N,P[300001],S[100000][3],t,t1,t2,L1,L2,a,b,cnt;
bool cap[300001],flag;
vector <int> A,B;

bool cmp(int a,int b){
	return P[a] < P[b];
}

int main(){
	scanf("%d",&N);
	for(int i=0;i<3*N;++i){
		scanf("%d",&t);
		P[t] = i;
	}
	for(int i=0;i<N;++i){
		scanf("%d%d%d",&S[i][0],&S[i][1],&S[i][2]);
		sort(S[i],S[i]+3,cmp);
		cap[S[i][0]] = 1;
	}
	scanf("%d",&t);
	if(cap[t] == 0){
		for(int i=1;i<=3*N;++i)
			if(i != t) printf("%d ",i);
		printf("\n");
	//	system("pause");
		return 0;
	}
	for(int i=0;i<N;++i){
		if(S[i][0] == t){
			flag = 1;
			t1 = S[i][1];
			t2 = S[i][2];
			A.push_back(S[i][1]);
			A.push_back(S[i][2]);
			continue;
		}
		for(int j=0;j<3;++j){
			if(!flag) A.push_back(S[i][j]);
			else B.push_back(S[i][j]);
		}
	}
	sort(A.begin(),A.end());
	sort(B.begin(),B.end());
	L1 = A.size();
	L2 = B.size();
	while(a < L1 || b < L2){
		if(a < L1 && (A[a] == t1 || A[a] == t2)){
			printf("%d ",A[a++]);
			++cnt;
		}
		else if(cnt < 2) printf("%d ",A[a++]);
		else{
			if(b == L2 || (A[a] < B[b])) printf("%d ",A[a++]);
			else printf("%d ",B[b++]);
		}
	}
	printf("\n");
//	system("pause");
}


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

int N,P[300001],S[100000][3],t,t1,t2,L1,L2,a,b,cnt;
bool cap[300001],flag;
vector <int> A,B;

bool cmp(int a,int b){
	return P[a] < P[b];
}

int main(){
	scanf("%d",&N);
	for(int i=0;i<3*N;++i){
		scanf("%d",&t);
		P[t] = i;
	}
	for(int i=0;i<N;++i){
		scanf("%d%d%d",&S[i][0],&S[i][1],&S[i][2]);
		sort(S[i],S[i]+3,cmp);
		cap[S[i][0]] = 1;
	}
	scanf("%d",&t);
	if(cap[t] == 0){
		for(int i=1;i<=3*N;++i)
			if(i != t) printf("%d ",i);
		printf("\n");
	//	system("pause");
		return 0;
	}
	for(int i=0;i<N;++i){
		if(S[i][0] == t){
			flag = 1;
			t1 = S[i][1];
			t2 = S[i][2];
			A.push_back(t1);
			A.push_back(t2);
			continue;
		}
		for(int j=0;j<3;++j){
			if(!flag) A.push_back(S[i][j]);
			else B.push_back(S[i][j]);
		}
	}
	sort(A.begin(),A.end());
	sort(B.begin(),B.end());
	L1 = A.size();
	L2 = B.size();
	while(a < L1 || b < L2){
		if(a < L1 && (A[a] == t1 || A[a] == t2)){
			printf("%d ",A[a++]);
			++cnt;
		}
		else if(cnt < 2) printf("%d ",A[a++]);
		else{
			if(b == L2 || (A[a] <= B[b])) printf("%d ",A[a++]);
			else printf("%d ",B[b++]);
		}
	}
	printf("\n");
//	system("pause");
}


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

int N,P[300001],S[100000][3],t,t1,t2,L1,L2,a=0,b=0,cnt;
bool cap[300001],flag;
vector <int> A,B;

bool cmp(int a,int b){
    return P[a] < P[b];
}

int main(){
    scanf("%d",&N);
    for(int i=0;i<3*N;++i){
        scanf("%d",&t);
        P[t] = i;
    }
    for(int i=0;i<N;++i){
        scanf("%d%d%d",&S[i][0],&S[i][1],&S[i][2]);
        sort(S[i],S[i]+3,cmp);
        cap[S[i][0]] = 1;
    }
    scanf("%d",&t);
    if(cap[t] == 0){
        for(int i=1;i<=3*N;++i)
            if(i != t) printf("%d ",i);
        printf("\n");
    //  system("pause");
        return 0;
    }
    for(int i=0;i<N;++i){
        if(S[i][0] == t){
            flag = 1;
            t1 = S[i][1];
            t2 = S[i][2];
            A.push_back(t1);
            A.push_back(t2);
            continue;
        }
        for(int j=0;j<3;++j){
            if(!flag) A.push_back(S[i][j]);
            else B.push_back(S[i][j]);
        }
    }
    sort(A.begin(),A.end());
    sort(B.begin(),B.end());
    L1 = A.size();
    L2 = B.size();
    while(a < L1 || b < L2){
        if(b == L2) printf("%d ",A[a++]);
        else if(a == L2) printf("%d ",B[b++]);
        else if(cnt < 2){
            cnt += (A[a] == t1 || A[a] == t2);
            printf("%d ",A[a++]);
        }
        else if(A[a] <= B[b]) printf("%d ",A[a++]);
        else printf("%d ",B[b++]);
    }
    printf("\n");
//  system("pause");
}


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

int N,P[300001],S[100000][3],t,t1,t2,L1,L2,a=0,b=0,cnt;
bool cap[300001],flag;
vector <int> A,B;

bool cmp(int a,int b){
    return P[a] < P[b];
}

int main(){
    scanf("%d",&N);
    for(int i=0;i<3*N;++i){
        scanf("%d",&t);
        P[t] = i;
    }
    for(int i=0;i<N;++i){
        scanf("%d%d%d",&S[i][0],&S[i][1],&S[i][2]);
        sort(S[i],S[i]+3,cmp);
        cap[S[i][0]] = 1;
    }
    scanf("%d",&t);
    if(cap[t] == 0){
        for(int i=1;i<=3*N;++i)
            if(i != t) printf("%d ",i);
        printf("\n");
    //  system("pause");
        return 0;
    }
    for(int i=0;i<N;++i){
        if(S[i][0] == t){
            flag = 1;
            t1 = S[i][1];
            t2 = S[i][2];
            A.push_back(t1);
            A.push_back(t2);
            continue;
        }
        for(int j=0;j<3;++j){
            if(!flag) A.push_back(S[i][j]);
            else B.push_back(S[i][j]);
        }
    }
    sort(A.begin(),A.end());
    sort(B.begin(),B.end());
    L1 = A.size();
    L2 = B.size();
    while(a < L1 || b < L2){
        if(b == L2) printf("%d ",A[a++]);
        else if(a == L1) printf("%d ",B[b++]);
        else if(cnt < 2){
            cnt += (A[a] == t1 || A[a] == t2);
            printf("%d ",A[a++]);
        }
        else if(A[a] <= B[b]) printf("%d ",A[a++]);
        else printf("%d ",B[b++]);
    }
    printf("\n");
//  system("pause");
}


#include <cstdio>
#include <algorithm>

#define N int(3e5 + 10)

using namespace std;

int a[N], team[N][3];
bool b[N];

int main()
{
    int n;
    scanf("%d ", &n);

    for (int i = 0; i < 3 * n; i++)
    {
        int num;
        scanf("%d ", &num);
        a[num] = i;
    }

    for (int i = 0; i < n; i++)
        for (int j = 0; j < 3; j++)
            scanf("%d ", &team[i][j]);

    int k;
    scanf("%d ", &k);

    int i, j;
    bool cont = true;

    for (i = 0; i < n; i++)
    {
        for (j = 0; j < 3; j++)
        {
            if (team[i][j] == k)
                cont = false;
            if (!cont) break;
        }
        if (!cont) break;
    }

    int k1, k2;
    if (j == 0) k1 = 1, k2 = 2;
    if (j == 1) k1 = 2, k2 = 0;
    if (j == 2) k1 = 0, k2 = 1;

    int p = team[i][j], p1 = team[i][k1], p2 = team[i][k2];

    if (a[p] > a[p1] || a[p] > a[p2])
    {
        for (int i = 1; i <= 3 * n; i++)
            if (i != k) printf("%d ", i);
        return 0;
    }

    for (int j = 0; j <= i; j++)
        for (int st = 0; st < 3; st++)
            b[ team[j][st] ] = true;

    for (int j = 1; j <= max(p1, p2); j++)
        if (b[j] && j != k) printf("%d ", j);

    for (int j = max(p1, p2) + 1; j <= 3 * n; j++)
        b[j] = false;

    for (int j = 1; j <= 3 * n; j++)
        if (!b[j] && j != k) printf("%d ", j);

    return 0;
}


#include <cstdio>
#include <algorithm>

#define N int(3e5 + 10)

using namespace std;

int a[N], team[N][3];
bool b[N];

int main()
{
    int n;
    scanf("%d ", &n);

    for (int i = 0; i < 3 * n; i++)
    {
        int num;
        scanf("%d ", &num);
        a[num] = i;
    }

    for (int i = 0; i < n; i++)
        for (int j = 0; j < 3; j++)
            scanf("%d ", &team[i][j]);

    int k;
    scanf("%d ", &k);

    int i, j;
    bool cont = true;

    for (i = 0; i < n; i++)
    {
        for (j = 0; j < 3; j++)
        {
            if (team[i][j] == k)
                cont = false;
            if (!cont) break;
        }
        if (!cont) break;
    }

    int k1, k2;
    if (j == 0) k1 = 1, k2 = 2;
    if (j == 1) k1 = 2, k2 = 0;
    if (j == 2) k1 = 0, k2 = 1;

    int p = team[i][j], p1 = team[i][k1], p2 = team[i][k2];

    if (a[p] > a[p1] || a[p] > a[p2])
    {
        for (int i = 1; i <= 3 * n; i++)
            if (i != k) printf("%d ", i);
        return 0;
    }

    for (int j = 0; j <= i; j++)
        for (int st = 0; st < 3; st++)
            b[ team[j][st] ] = true;

    for (int j = 1; j <= max(p1, p2); j++)
        if (b[j] && j != k) printf("%d ", j);

    for (int j = max(p1, p2) + 1; j <= 3 * n; j++)
        b[j] = false;

    for (int j = 1; j <= 3 * n; j++)
        if (!b[j]) printf("%d ", j);

    return 0;
}


#include <cstdio>
#include <algorithm>

#define N int(3e5 + 10)

using namespace std;

int a[N], team[N][3];
bool b[N];

int main()
{
    int n;
    scanf("%d ", &n);

    for (int i = 0; i < 3 * n; i++)
    {
        int num;
        scanf("%d ", &num);
        a[num] = i;
    }

    for (int i = 0; i < n; i++)
        for (int j = 0; j < 3; j++)
            scanf("%d ", &team[i][j]);

    int k;
    scanf("%d ", &k);

    int i, j;
    bool cont = true;

    for (i = 0; i < n; i++)
    {
        for (j = 0; j < 3; j++)
        {
            if (team[i][j] == k)
                cont = false;
            if (!cont) break;
        }
        if (!cont) break;
    }

    int k1, k2;
    if (j == 0) k1 = 1, k2 = 2;
    if (j == 1) k1 = 2, k2 = 0;
    if (j == 2) k1 = 0, k2 = 1;

    int p = team[i][j], p1 = team[i][k1], p2 = team[i][k2];

    if (a[p] > a[p1] || a[p] > a[p2])
    {
        for (int i = 1; i <= 3 * n; i++)
            if (i != k) printf("%d ", i);
        return 0;
    }

    for (int j = 0; j <= i; j++)
        for (int st = 0; st < 3; st++)
            b[ team[j][st] ] = true;

    for (int j = i + 1; j < n; j++)
        for (int st = 0; st < 3; st++)
            if (a[ team[j][st] ] > a[p1] && a[ team[j][st] ] > a[p2])
                b[ team[j][st] ] = true;

    for (int j = 1; j <= 3 * n; j++)
        if (b[j] && j != k) printf("%d ", j);

    for (int j = 1; j <= 3 * n; j++)
        if (!b[j]) printf("%d ", j);

    return 0;
}


#include <cstdio>
#include <algorithm>

#define N int(3e5 + 10)

using namespace std;

int a[N], team[N][3];
bool b[N];

int main()
{
    int n;
    scanf("%d ", &n);

    for (int i = 0; i < 3 * n; i++)
    {
        int num;
        scanf("%d ", &num);
        a[num] = i;
    }

    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < 3; j++)
            scanf("%d ", &team[i][j]);
    }

    int k;
    scanf("%d ", &k);

    int i, j;
    bool cont = true;

    for (i = 0; i < n; i++)
    {
        for (j = 0; j < 3; j++)
        {
            if (team[i][j] == k)
                cont = false;
            if (!cont) break;
        }
        if (!cont) break;
    }

    int k1, k2;
    if (j == 0) k1 = 1, k2 = 2;
    if (j == 1) k1 = 2, k2 = 0;
    if (j == 2) k1 = 0, k2 = 1;

    int p = team[i][j], p1 = team[i][k1], p2 = team[i][k2];

    if (a[p] > a[p1] || a[p] > a[p2])
    {
        for (int i = 1; i <= 3 * n; i++)
            if (i != k) printf("%d ", i);
        return 0;
    }

    for (int j = 0; j <= i; j++)
        for (int st = 0; st < 3; st++)
            b[ team[j][st] ] = true;

    for (int j = 1; j <= 3 * n; j++)
        if (b[j] && j != k) printf("%d ", j);

    for (int j = 1; j <= 3 * n; j++)
        if (!b[j]) printf("%d ", j);

    return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

#define pb push_back
#define mp make_pair
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define tr(c, i) for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define rtr(c, i) for (typeof((c).rbegin()) i = (c).rbegin(); i != (c).rend(); --i)
#define contains(c, x) ((c).find(x) != (c).end())
#define Contains(c, x) (find(all(c), x) != (c).end())
#define REP(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) REP(i, 0, (n))
#define To_String to_string< char,std::char_traits<char>,std::allocator<char> >

typedef long long ll;
typedef unsigned long long ull;
using namespace std;

int a[300010];
int b[100010][3];
int used[300010];
int main()
{
	int n;
	while (scanf("%d", &n) != EOF) {
		int m = 3 * n;
		int k;
		rep(i, m) scanf("%d", a+i);
		rep(i, n) rep(j, 3) scanf("%d", &b[i][j]);
		scanf("%d", &k);

		memset(used, 0, sizeof(used));
		int k0, k1, k2;
		int i0, i1, i2;
		rep(i, m) {
			bool f = false;
			rep(j, 3) { used[b[i][j]]++; if (k == b[i][j]) f = true; }
			if (f) { k0 = b[i][0]; k1 = b[i][1]; k2 = b[i][2]; break; }
		}
		bool leader = false;
		rep(i, m) {
			if (a[i] == k0) i0 = i;
			else if (a[i] == k1) i1 = i;
			else if (a[i] == k2) i2 = i;
		}
		if (k == k0 && i0 < i1 && i0 < i2) leader = true;
		else if (k == k1 && i1 < i0 && i1 < i2) leader = true;
		else if (k == k2 && i2 < i0 && i2 < i1) leader = true;

		ostringstream os;
		if (leader) {
			REP(i, 1, m+1) if (i != k && used[i]) os << ' ' << i;
			REP(i, 1, m+1) if (i != k && !used[i]) os << ' ' << i;
		}
		else {
			REP(i, 1, m+1) if (i != k) os << ' ' << i;
		}
		puts(os.str().substr(1).c_str());
	}
	return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

#define pb push_back
#define mp make_pair
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define tr(c, i) for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define rtr(c, i) for (typeof((c).rbegin()) i = (c).rbegin(); i != (c).rend(); --i)
#define contains(c, x) ((c).find(x) != (c).end())
#define Contains(c, x) (find(all(c), x) != (c).end())
#define REP(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) REP(i, 0, (n))
#define To_String to_string< char,std::char_traits<char>,std::allocator<char> >

typedef long long ll;
typedef unsigned long long ull;
using namespace std;

int a[300010];
int b[100010][3];
int used[300010];
int main()
{
	int n;
	while (scanf("%d", &n) != EOF) {
		int m = 3 * n;
		int k;
		rep(i, m) scanf("%d", a+i);
		rep(i, n) rep(j, 3) scanf("%d", &b[i][j]);
		scanf("%d", &k);

		memset(used, 0, sizeof(used));
		rep(i, m) {
			bool f = false;
			rep(j, 3) { used[b[i][j]]++; if (k == b[i][j]) f = true; }
			if (f) break;
		}
		ostringstream os;
		REP(i, 1, m+1) if (i != k && used[i]) os << ' ' << i;
		REP(i, 1, m+1) if (i != k && !used[i]) os << ' ' << i;
		puts(os.str().substr(1).c_str());
	}
	return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

#define pb push_back
#define mp make_pair
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define tr(c, i) for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define rtr(c, i) for (typeof((c).rbegin()) i = (c).rbegin(); i != (c).rend(); --i)
#define contains(c, x) ((c).find(x) != (c).end())
#define Contains(c, x) (find(all(c), x) != (c).end())
#define REP(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) REP(i, 0, (n))
#define To_String to_string< char,std::char_traits<char>,std::allocator<char> >

typedef long long ll;
typedef unsigned long long ull;
using namespace std;

int a[300010];
int b[100010][3];
int used[300010];
int main()
{
	int n;
	while (scanf("%d", &n) != EOF) {
		int m = 3 * n;
		int k;
		rep(i, m) scanf("%d", a+i);
		rep(i, n) rep(j, 3) scanf("%d", &b[i][j]);
		scanf("%d", &k);

		memset(used, 0, sizeof(used));
		bool leader = false;
		rep(i, m) {
			bool f = false;
			rep(j, 3) { used[b[i][j]]++; if (k == b[i][j]) f = true; }
			if (f) {
				int i0 = find(a, a+m, b[i][0]) - a;
				int i1 = find(a, a+m, b[i][1]) - a;
				int i2 = find(a, a+m, b[i][2]) - a;
				if (k == b[i][0] && i0 < i1 && i0 < i2) leader = true;
				else if (k == b[i][1] && i1 < i0 && i1 < i2) leader = true;
				else if (k == b[i][2] && i2 < i0 && i2 < i1) leader = true;
				break;
			}
		}
		ostringstream os;
		if (leader) {
			REP(i, 1, m+1) if (i != k && used[i]) os << ' ' << i;
			REP(i, 1, m+1) if (i != k && !used[i]) os << ' ' << i;
		}
		else {
			REP(i, 1, m+1) if (i != k) os << ' ' << i;
		}
		puts(os.str().substr(1).c_str());
	}
	return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

#define pb push_back
#define mp make_pair
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define tr(c, i) for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define rtr(c, i) for (typeof((c).rbegin()) i = (c).rbegin(); i != (c).rend(); --i)
#define contains(c, x) ((c).find(x) != (c).end())
#define Contains(c, x) (find(all(c), x) != (c).end())
#define REP(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) REP(i, 0, (n))
#define To_String to_string< char,std::char_traits<char>,std::allocator<char> >

typedef long long ll;
typedef unsigned long long ull;
using namespace std;

int a[300010];
int b[100010][3];
int used[300010];
int main()
{
	int n;
	while (scanf("%d", &n) != EOF) {
		int m = 3 * n;
		int k;
		rep(i, m) scanf("%d", a+i);
		rep(i, n) rep(j, 3) scanf("%d", &b[i][j]);
		scanf("%d", &k);

		memset(used, 0, sizeof(used));
		int k0, k1, k2;
		int i0, i1, i2;
		int ii;
		rep(i, m) {
			bool f = false;
			rep(j, 3) { used[b[i][j]]++; if (k == b[i][j]) f = true; }
			if (f) { ii = i; k0 = b[i][0]; k1 = b[i][1]; k2 = b[i][2]; break; }
		}
		bool leader = false;
		rep(i, m) {
			if (a[i] == k0) i0 = i;
			else if (a[i] == k1) i1 = i;
			else if (a[i] == k2) i2 = i;
		}
		if (k == k0 && i0 < i1 && i0 < i2) leader = true;
		else if (k == k1 && i1 < i0 && i1 < i2) leader = true;
		else if (k == k2 && i2 < i0 && i2 < i1) leader = true;

		ostringstream os;
		memset(a, 0, sizeof(a));
		if (leader) {
			sort(b[ii], b[ii]+3);
			int mm = (b[ii][2] == k) ? b[ii][1] : b[ii][2];
			REP(i, 1, m+1) if (i != k && used[i]) {
				os << ' ' << i;
				a[i]++;
				if (i == mm) break;
			}
			REP(i, 1, m+1) if (i != k && !a[i]) os << ' ' << i;
		}
		else {
			REP(i, 1, m+1) if (i != k) os << ' ' << i;
		}
		puts(os.str().substr(1).c_str());
	}
	return 0;
}


#include <cstdio>
#include <iostream>
#include <vector>
#include <set>
#include <cstring>
#include <string>
#include <map>
#include <cmath>
#include <ctime>
#include <algorithm>
#include <bitset>
#include <queue>
#include <sstream>

using namespace std;

#define mp make_pair
#define pb push_back
#define rep(i,n) for(int i = 0; i < n; i++)
#define re return
#define fi first
#define se second
#define sz(x) ((int) (x).size())
#define all(x) (x).begin(), (x).end()
#define sqr(x) ((x) * (x))
#define y0 y3487465
#define y1 y8687969

typedef vector<int> vi;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<string> vs;
typedef vector<vi> vvi;

template<class T> T abs(T x) {
    re x > 0 ? x : -x;
}

int n;
int m;
int p[300000], q[300000], use[300000], last[300000];
ii g[100000][3];
ii w[100000];

int main() {
    scanf ("%d", &n);

    for (int i = 0; i < 3 * n; i++) {
        scanf ("%d", &p[i]);
        p[i]--;
        q[p[i]] = i;
    }
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < 3; j++) {
            scanf ("%d", &g[i][j].se);
            g[i][j].se--;
            g[i][j].fi = q[g[i][j].se];
        }
        sort (g[i], g[i] + 3);
    }
    int f;
    scanf ("%d", &f);
    f--;
    for (int i = 0; i < n; i++) {
        if (g[i][0].se == f) {
            use[g[i][1].se] = use[g[i][2].se] = 1;
            for (int k = 0; k < 3 * n && (last[g[i][1].se] != i + 1 || last[g[i][2].se] != i + 1); k++)
                if (use[k]) {
                    last[k] = i + 1;
                    printf ("%d ", k + 1);
                }
            for (int k = 0; k < 3 * n; k++)
                if (last[k] != i + 1 && k != f) {
                    last[k] = i + 1;
                    printf ("%d ", k + 1);
                }
            printf ("\n");
            re 0;
        }
        use[g[i][0].se] = use[g[i][1].se] = use[g[i][2].se] = 1;
    }
    for (int i = 0; i < 3 * n; i++)
        if (i != f)
            printf ("%d ", i + 1);
    printf ("\n");
    return 0;
}


#include <cstdio>
#include <iostream>
#include <vector>
#include <set>
#include <cstring>
#include <string>
#include <map>
#include <cmath>
#include <ctime>
#include <algorithm>
#include <bitset>
#include <queue>
#include <sstream>

using namespace std;

#define mp make_pair
#define pb push_back
#define rep(i,n) for(int i = 0; i < n; i++)
#define re return
#define fi first
#define se second
#define sz(x) ((int) (x).size())
#define all(x) (x).begin(), (x).end()
#define sqr(x) ((x) * (x))
#define y0 y3487465
#define y1 y8687969

typedef vector<int> vi;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<string> vs;
typedef vector<vi> vvi;

template<class T> T abs(T x) {
    re x > 0 ? x : -x;
}

int n;
int m;
int p[300000], q[300000], use[300000];
ii g[100000][3];
ii w[100000];

int main() {
    scanf ("%d", &n);

    for (int i = 0; i < 3 * n; i++) {
        scanf ("%d", &p[i]);
        p[i]--;
        q[p[i]] = i;
    }
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < 3; j++) {
            scanf ("%d", &g[i][j].se);
            g[i][j].se--;
            g[i][j].fi = q[g[i][j].se];
        }
        sort (g[i], g[i] + 3);
        w[i] = mp (g[i][0].fi, i);
    }
    sort (w, w + n);
    int f;
    scanf ("%d", &f);
    f--;
    for (int i = 0; i < n; i++) {
        int j = w[i].se;
        if (g[j][0].se == f) {
            use[g[j][1].se] = use[g[j][2].se] = 1;
            for (int k = 0; k < 3 * n; k++)
                if (use[k])
                    printf ("%d ", k + 1);
            for (int k = 0; k < 3 * n; k++)
                if (!use[k] && k != f)
                    printf ("%d ", k + 1);
            printf ("\n");
            re 0;
        }
        use[g[j][0].se] = use[g[j][1].se] = use[g[j][2].se] = 1;
    }
    for (int i = 0; i < 3 * n; i++)
        if (i != f)
            printf ("%d ", i + 1);
    printf ("\n");
    return 0;
}


#include <cstdio>
#include <iostream>
#include <vector>
#include <set>
#include <cstring>
#include <string>
#include <map>
#include <cmath>
#include <ctime>
#include <algorithm>
#include <bitset>
#include <queue>
#include <sstream>

using namespace std;

#define mp make_pair
#define pb push_back
#define rep(i,n) for(int i = 0; i < n; i++)
#define re return
#define fi first
#define se second
#define sz(x) ((int) (x).size())
#define all(x) (x).begin(), (x).end()
#define sqr(x) ((x) * (x))
#define y0 y3487465
#define y1 y8687969

typedef vector<int> vi;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<string> vs;
typedef vector<vi> vvi;

template<class T> T abs(T x) {
    re x > 0 ? x : -x;
}

int n;
int m;
int p[300000], q[300000], use[300000];
ii g[100000][3];
ii w[100000];

int main() {
    scanf ("%d", &n);

    for (int i = 0; i < 3 * n; i++) {
        scanf ("%d", &p[i]);
        p[i]--;
        q[p[i]] = i;
    }
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < 3; j++) {
            scanf ("%d", &g[i][j].se);
            g[i][j].se--;
            g[i][j].fi = q[g[i][j].se];
        }
        sort (g[i], g[i] + 3);
    }
    int f;
    scanf ("%d", &f);
    f--;
    for (int i = 0; i < n; i++) {
        if (g[i][0].se == f) {
            use[g[i][1].se] = use[g[i][2].se] = 1;
            for (int k = 0; k < 3 * n; k++)
                if (use[k])
                    printf ("%d ", k + 1);
            for (int k = 0; k < 3 * n; k++)
                if (!use[k] && k != f)
                    printf ("%d ", k + 1);
            printf ("\n");
            re 0;
        }
        use[g[i][0].se] = use[g[i][1].se] = use[g[i][2].se] = 1;
    }
    for (int i = 0; i < 3 * n; i++)
        if (i != f)
            printf ("%d ", i + 1);
    printf ("\n");
    return 0;
}


#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>
#include <cassert>
using namespace std;

#define all(c) (c).begin(), (c).end()
#define iter(c) __typeof((c).begin())
#define cpresent(c, e) (find(all(c), (e)) != (c).end())
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
#define pb(e) push_back(e)
#define mp(a, b) make_pair(a, b)

int N, K;
int A[3000010];
int T[1000010][3];

int rev[3000010];
bool usd[3000010];

int main() {
  while (1 == scanf("%d", &N)) {
    int M = N * 3;
    rep (i, M) {
      scanf("%d", &A[i]);
      rev[A[i]] = i;
    }
    rep (i, N) rep (j, 3) {
      scanf("%d", &T[i][j]);
      // printf("paoijpoaij %d\n", T[i][j]);
    }
    scanf("%d", &K);

    memset(usd, 0, sizeof(usd));
    rep (i, N) {
      rep (j, 3) usd[T[i][j]] = true;

      // printf("i=%d, %d %d %d\n", i, T[i][0], T[i][1], T[i][2]);

      rep (j, 3) if (T[i][j] == K) goto now;
      continue;

   now:
      rep (j, 3) if (rev[T[i][j]] < rev[K]) goto lose;

      // printf("i=%d\n", i);
      // printf("%d %d %d\n", T[i][0], T[i][1], T[i][2]);

      int c = 1, x = 1;
      for (; x <= M; ++x) {
        // printf("x=%d, %d %d\n", x, usd[x], K);
        if (usd[x] && x != K) printf("%d ", x);
        rep (j, 3) if (T[i][j] == x && x != K) ++c;
        if (c == 3) break;
      }
      // printf("x=%d\n", x);
      for (int y = 1; y <= M; ++y) if ((!usd[y] || x < y) && y != K) printf("%d ", y);
      puts("");
      continue;
    }

    continue;
 lose:
    for (int i = 1; i <= M; ++i) if (i != K) printf("%d ", i);
    puts("");
  }

  return 0;
}


#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>
#include <cassert>
using namespace std;

#define all(c) (c).begin(), (c).end()
#define iter(c) __typeof((c).begin())
#define cpresent(c, e) (find(all(c), (e)) != (c).end())
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
#define pb(e) push_back(e)
#define mp(a, b) make_pair(a, b)

int N, K;
int A[3000010];
int T[1000010][3];

int rev[3000010];
bool usd[3000010];

int main() {
  while (1 == scanf("%d", &N)) {
    int M = N * 3;
    rep (i, M) {
      scanf("%d", &A[i]);
      rev[A[i]] = i;
    }
    rep (i, N) rep (j, 3) {
      scanf("%d", &T[i][j]);
      // printf("paoijpoaij %d\n", T[i][j]);
    }
    scanf("%d", &K);

    memset(usd, 0, sizeof(usd));
    rep (i, N) {
      rep (j, 3) usd[T[i][j]] = true;

      // printf("i=%d, %d %d %d\n", i, T[i][0], T[i][1], T[i][2]);

      rep (j, 3) if (T[i][j] == K) goto now;
      continue;

   now:
      rep (j, 3) if (rev[T[i][j]] < rev[K]) goto lose;

      // printf("i=%d\n", i);
      // printf("%d %d %d\n", T[i][0], T[i][1], T[i][2]);

      int c = 0, x = 1;
      for (; x <= M; ++x) {
        // printf("x=%d, %d %d\n", x, usd[x], K);
        if (usd[x] && x != K) printf("%d ", x);
        rep (j, 3) if (T[i][j] == x) ++c;
        if (c == 3) break;
      }
      // printf("x=%d\n", x);
      for (int y = 1; y <= M; ++y) if ((!usd[y] || x < y) && y != K) printf("%d ", y);
      puts("");
      continue;
    }

    continue;
 lose:
    for (int i = 1; i <= M; ++i) if (i != K) printf("%d ", i);
    puts("");
  }

  return 0;
}


#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>
#include <cassert>
using namespace std;

#define all(c) (c).begin(), (c).end()
#define iter(c) __typeof((c).begin())
#define cpresent(c, e) (find(all(c), (e)) != (c).end())
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
#define pb(e) push_back(e)
#define mp(a, b) make_pair(a, b)

int N, K;
int A[300010];
int T[100010][3];

int rev[300010];
bool usd[300010];

int main() {
  while (1 == scanf("%d", &N)) {
    int M = N * 3;
    rep (i, M) {
      scanf("%d", &A[i]);
      rev[A[i]] = i;
    }
    rep (i, N) rep (j, 3) scanf("%d", &T[i][j]);
    scanf("%d", &K);

    memset(usd, 0, sizeof(usd));
    rep (i, N) {
      rep (j, 3) usd[T[i][j]] = true;
      if (T[i][0] != K && T[i][1] != K && T[i][2] != K) {
        continue;
      }

      rep (j, 3) if (rev[T[i][j]] < rev[K]) goto lose;

      for (int i = 1; i <= M; ++i) if (usd[i]  && i != K) printf("%d ", i);
      for (int i = 1; i <= M; ++i) if (!usd[i] && i != K) printf("%d ", i);
      puts("");
      continue;
    }

    continue;
 lose:
    for (int i = 1; i <= M; ++i) if (i != K) printf("%d ", i);
    puts("");
  }

  return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#define eps 1e-9
#define FOR(x, s, e) for(int x = (s); x < (e); ++x)
#define FORc(x, s, e, c) for(int x = (s); x < (e) && (c); ++x)
#define STEP(x, s, e, d) for(int x = (s); x < (e); x+=(d))
#define ROF(x, s, e) for(int x = (s); x >= (e); --x)
#define ROFc(x, s, e, c) for(int x = (s); x >= (e) && (c); --x)
#define vb vector<bool>
#define vi vector<int>
#define vii vector<pair<int, int> >
#define vs vector<string>
#define pb push_back
#define mp make_pair
#define ALL(X) X.begin(), X.end()

using namespace std;

int main(int argc, char **argv){
	int M;
	cin >> M;
	int N = 3*M;
	bool s = 0;
	vi A, B, C, L(N), R(N+1);
	FOR(i, 0, N) cin >> L[i], R[L[i]] = i;
	FOR(i, 0, M){
		int x, y, z;
		cin >> x >> y >> z;
		C.pb(x), C.pb(y), C.pb(z);
	}
	int K;
	bool win = 0;
	cin >> K;
	int px, py, pz;
	FOR(i, 0, M){
		int x = C[3*i], y = C[3*i+1], z = C[3*i+2];
		if (x == K || y == K || z == K){
			px = x, py = y, pz = z;
			if (x != K) A.pb(x);
			if (y != K) A.pb(y);
			if (z != K) A.pb(z);
			s = 1;
		}else if (s){ B.pb(x), B.pb(y), B.pb(z);
		}else{ A.pb(x), A.pb(y), A.pb(z); }
	}
	int ptr = 0;
	bool del[N]; memset(del, 0, sizeof(del));
	FORc(i, 0, M, !win){
		int x = C[3*i], y = C[3*i+1], z = C[3*i+2];
		//printf("Winner %d: %d\n", i, L[ptr]);
		if (L[ptr] == x && x == K) win = 1;
		if (L[ptr] == y && y == K) win = 1;
		if (L[ptr] == z && z == K) win = 1;
		del[R[x]] = del[R[y]] = del[R[z]] = 1;
		while (ptr < N && del[ptr]) ptr++;
	}
	if (win){
		sort(ALL(A));
		vi res = A;
		//printf("Check for %d %d %d\n>>", px, py, pz);
		//FOR(i, 0, A.size()) printf("%d%c", A[i], ' '); puts("");
		//FOR(i, 0, B.size()) printf("%d%c", B[i], ' '); puts("");
		while (res.size()) if (res.back() == px || res.back() == py || res.back() == pz) break;
		else{
			//printf("discard %d\n", res.back());
			B.pb(res.back()), res.pop_back();
		}
		sort(ALL(B));
		FOR(i, 0, B.size()) res.pb(B[i]);
		FOR(i, 0, N-1) printf("%d%c", res[i], i==N-2?'\n':' ');
	}else{
		vi res;
		FOR(i, 1, N+1) if (K != i) res.pb(i);
		FOR(i, 0, N-1) printf("%d%c", res[i], i==N-2?'\n':' ');
	}
	return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#define eps 1e-9
#define FOR(x, s, e) for(int x = (s); x < (e); ++x)
#define FORc(x, s, e, c) for(int x = (s); x < (e) && (c); ++x)
#define STEP(x, s, e, d) for(int x = (s); x < (e); x+=(d))
#define ROF(x, s, e) for(int x = (s); x >= (e); --x)
#define ROFc(x, s, e, c) for(int x = (s); x >= (e) && (c); --x)
#define vb vector<bool>
#define vi vector<int>
#define vii vector<pair<int, int> >
#define vs vector<string>
#define pb push_back
#define mp make_pair
#define ALL(X) X.begin(), X.end()

using namespace std;

int main(int argc, char **argv){
	int M;
	cin >> M;
	int N = 3*M;
	int dummy;
	bool s = 0;
	vi A, B, C, L(N), R(N+1);
	FOR(i, 0, N) cin >> L[i], R[L[i]] = i;
	FOR(i, 0, M){
		int x, y, z;
		cin >> x >> y >> z;
		C.pb(x), C.pb(y), C.pb(z);
	}
	int K;
	bool win = 0;
	cin >> K;
	FOR(i, 0, M){
		int x = C[3*i], y = C[3*i+1], z = C[3*i+2];
		if (x == K || y == K || z == K){
			if (x != K) A.pb(x);
			if (y != K) A.pb(y);
			if (z != K) A.pb(z);
			s = 1;
		}else if (s){ B.pb(x), B.pb(y), B.pb(z);
		}else{ A.pb(x), A.pb(y), A.pb(z);
		}
	}
	int ptr = 0;
	bool del[N]; memset(del, 0, sizeof(del));
	FORc(i, 0, M, !win){
		int x = C[3*i], y = C[3*i+1], z = C[3*i+2];
		if (L[ptr] == x && x == K) win = 1;
		if (L[ptr] == y && y == K) win = 1;
		if (L[ptr] == z && z == K) win = 1;
		del[R[x]] = del[R[y]] = del[R[z]] = 1;
		while (ptr < N && del[ptr]) ptr++;
	}
	if (win){
		sort(ALL(A)), sort(ALL(B));
		vi res = A;
		FOR(i, 0, B.size()) res.pb(B[i]);
		FOR(i, 0, N-1) printf("%d%c", res[i], i==N-2?'\n':' ');
	}else{
		vi res;
		FOR(i, 1, N+1) if (K != i) res.pb(i);
		FOR(i, 0, N-1) printf("%d%c", res[i], i==N-2?'\n':' ');
	}
	return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#define eps 1e-9
#define FOR(x, s, e) for(int x = (s); x < (e); ++x)
#define FORc(x, s, e, c) for(int x = (s); x < (e) && (c); ++x)
#define STEP(x, s, e, d) for(int x = (s); x < (e); x+=(d))
#define ROF(x, s, e) for(int x = (s); x >= (e); --x)
#define ROFc(x, s, e, c) for(int x = (s); x >= (e) && (c); --x)
#define vb vector<bool>
#define vi vector<int>
#define vii vector<pair<int, int> >
#define vs vector<string>
#define pb push_back
#define mp make_pair
#define ALL(X) X.begin(), X.end()

using namespace std;

int main(int argc, char **argv){
	int M;
	cin >> M;
	int N = 3*M;
	int dummy;
	bool s = 0;
	vi A, B, C;
	FOR(i, 0, N) cin >> dummy;
	FOR(i, 0, M){
		int x, y, z;
		cin >> x >> y >> z;
		C.pb(x), C.pb(y), C.pb(z);
	}
	int K;
	cin >> K;
	FOR(i, 0, M){
		int x = C[3*i], y = C[3*i+1], z = C[3*i+2];
		if (x == K || y == K || z == K){
			if (x != K) A.pb(x);
			if (y != K) A.pb(y);
			if (z != K) A.pb(z);
			s = 1;
		}else if (s){ B.pb(x), B.pb(y), B.pb(z);
		}else{ A.pb(x), A.pb(y), A.pb(z);
		}
	}
	sort(ALL(A)), sort(ALL(B));
	vi res = A;
	FOR(i, 0, B.size()) res.pb(B[i]);
	FOR(i, 0, N-1) printf("%d%c", res[i], i==N-2?'\n':' ');
	return 0;
}


#define _USE_MATH_DEFINES

#include <stdio.h>
#include <iterator>
#include <memory.h>
#include <iostream>
#include <algorithm>
#include <limits>
#include <iomanip>
#include <string>
#include <vector>
#include <map>
#include <math.h>
#include <sstream>
#include <assert.h>
#include <list>
#include <valarray>
#include <queue>
#include <set>
#include <iomanip>
#include <stdlib.h>

#define pb push_back
#define mp make_pair
#define X first
#define Y second
#define rep(i,x) for(int i=0; i<x; ++i)
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define out(x,type) copy(all(x),ostream_iterator<type >(cout," ")); cout << endl

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int,int> pii;

template<class T1, class T2>
ostream& operator<<(ostream& os, const pair<T1,T2>& p) { os << "{" << p.X << " " << p.Y << "}"; return os; }

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

	n*=3;

	vi pl(n,0), r_pl(n,0);
	rep(i,n)
	{
		cin >> pl[i];
		pl[i]--;
		r_pl[pl[i]] = i;
	}

	vi a(n/3,0), b(n/3,0), c(n/3,0);

	rep(i,n/3)
	{
		cin >> a[i] >> b[i] >> c[i];
		a[i]--,b[i]--,c[i]--;
	}

	int k; cin >> k; k--;

	int ind = 0;
	rep(i,n/3)
	{
		while(pl[ind] == -1)
			ind++;

		assert(ind < n);

		int curr = pl[ind];
		pl[r_pl[a[i]]] = -1;
		pl[r_pl[b[i]]] = -1;
		pl[r_pl[c[i]]] = -1;

		if (k==a[i] || k==b[i] || k==c[i])
		{
			vi ret;
			if (k==curr)
			{
				rep(i,n)
					if (i!=k && pl[r_pl[i]] == -1)
						ret.pb(i);
				rep(i,n)
					if (i!=k && pl[r_pl[i]] != -1)
						ret.pb(i);
			}
			else
			{
				rep(i,n)
					if (i!=k)
						ret.pb(i);
			}

			rep(i,n-1)
				cout << ret[i]+1 << " ";
			cout << endl;

			return 0;
		}
	}

	return 0;
}


#define _USE_MATH_DEFINES

#include <stdio.h>
#include <iterator>
#include <memory.h>
#include <iostream>
#include <algorithm>
#include <limits>
#include <iomanip>
#include <string>
#include <vector>
#include <map>
#include <math.h>
#include <sstream>
#include <assert.h>
#include <list>
#include <valarray>
#include <queue>
#include <set>
#include <iomanip>
#include <stdlib.h>

#define pb push_back
#define mp make_pair
#define X first
#define Y second
#define rep(i,x) for(int i=0; i<x; ++i)
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define out(x,type) { copy(all(x),ostream_iterator<type >(cout," ")); cout << endl; }

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int,int> pii;

template<class T1, class T2>
ostream& operator<<(ostream& os, const pair<T1,T2>& p) { os << "{" << p.X << " " << p.Y << "}"; return os; }

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

	n*=3;

	vi pl(n,0), r_pl(n,0), orig(n,0);
	rep(i,n)
	{
		cin >> pl[i];
		pl[i]--;
		r_pl[pl[i]] = i;
	}
	orig = pl;

	vi a(n/3,0), b(n/3,0), c(n/3,0);

	rep(i,n/3)
	{
		cin >> a[i] >> b[i] >> c[i];
		a[i]--,b[i]--,c[i]--;
	}

	int k1; cin >> k1; k1--;

	int ind = 0;
	vector<vi> ret(n,vi());

	rep(i,n/3)
	{
		while(pl[ind] == -1)
			ind++;

		assert(ind < n);

		int curr = pl[ind];
		pl[r_pl[a[i]]] = -1;
		pl[r_pl[b[i]]] = -1;
		pl[r_pl[c[i]]] = -1;

		{
			int k = k1;

			vi ret;
			if (k==a[i] || k==b[i] || k==c[i])
			{
				int r1,r2;

				if (k==a[i])
					r1=b[i],r2=c[i];
				if (k==b[i])
					r1=a[i],r2=c[i];
				if (k==c[i])
					r1=b[i],r2=a[i];

				//vi ret;
				if (k==curr)
				{
					rep(i,n)
						if (i!=k && (pl[r_pl[i]] == -1 && i <= min(r1,r2)))
							ret.pb(i);
					rep(i,n)
						if (i!=k && !(pl[r_pl[i]] == -1 && i <= min(r1,r2)))
							ret.pb(i);
				}
				else
				{
					rep(i,n)
						if (i!=k)
							ret.pb(i);
				}

				{
					rep(i,n-1)
						cout << ret[i]+1 << " ";
					cout << endl;
				}

				return 0;
			}
		}
	}

	return 0;
}


#define _USE_MATH_DEFINES

#include <stdio.h>
#include <iterator>
#include <memory.h>
#include <iostream>
#include <algorithm>
#include <limits>
#include <iomanip>
#include <string>
#include <vector>
#include <map>
#include <math.h>
#include <sstream>
#include <assert.h>
#include <list>
#include <valarray>
#include <queue>
#include <set>
#include <iomanip>
#include <stdlib.h>

#define pb push_back
#define mp make_pair
#define X first
#define Y second
#define rep(i,x) for(int i=0; i<x; ++i)
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define out(x,type) { copy(all(x),ostream_iterator<type >(cout," ")); cout << endl; }

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int,int> pii;

template<class T1, class T2>
ostream& operator<<(ostream& os, const pair<T1,T2>& p) { os << "{" << p.X << " " << p.Y << "}"; return os; }

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

	n*=3;

	vi pl(n,0), r_pl(n,0), orig(n,0);
	rep(i,n)
	{
		cin >> pl[i];
		pl[i]--;
		r_pl[pl[i]] = i;
	}
	orig = pl;

	vi a(n/3,0), b(n/3,0), c(n/3,0);

	rep(i,n/3)
	{
		cin >> a[i] >> b[i] >> c[i];
		a[i]--,b[i]--,c[i]--;
	}

	int k1; cin >> k1; k1--;

	int ind = 0;
	vector<vi> ret(n,vi());

	rep(i,n/3)
	{
		while(pl[ind] == -1)
			ind++;

		assert(ind < n);

		int curr = pl[ind];
		pl[r_pl[a[i]]] = -1;
		pl[r_pl[b[i]]] = -1;
		pl[r_pl[c[i]]] = -1;

		vi ks(3,0); ks[0]=a[i],ks[1]=b[i],ks[2]=c[i];

		rep(j,3)
		{
			int k = ks[j];

			if (k==a[i] || k==b[i] || k==c[i])
			{
				//vi ret;
				if (k==curr)
				{
					rep(i,n)
						if (i!=k && pl[r_pl[i]] == -1)
							ret[k].pb(i);
					rep(i,n)
						if (i!=k && pl[r_pl[i]] != -1)
							ret[k].pb(i);
				}
				else
				{
					rep(i,n)
						if (i!=k)
							ret[k].pb(i);
				}

				if (0)
				{
					rep(i,n-1)
						cout << ret[k][i]+1 << " ";
					cout << endl;
				}

				//return 0;
			}
		}
	}

	rep(i,n-1)
		cout << ret[k1][i]+1 << " ";
	cout << endl;

	if (0)
	rep(i,n)
	{
		rep(j,n-1)
			cout << ret[i][j]+1 << " ";
		cout << endl;
	}

	vi used = orig;
	ind = 0;
	rep(i,n/3)
	{
		while(orig[ind] == -1)
			ind++;
		int curr = orig[ind];
		orig[ind] = -1;

		vi v1;
		//cout << curr+1 << " ";
		v1.pb(curr);

		int cnt = 0;
		rep(j,n-1)
		{
			if (orig[r_pl[ret[curr][j]]] != -1)
			{
				cnt++;
				orig[r_pl[ret[curr][j]]] = -1;
				v1.pb(ret[curr][j]);
				//cout << ret[curr][j]+1 << " ";
			}
			if (cnt == 2)
				break;
		}
		//cout << endl;

		vi v2;
		v2.pb(a[i]);
		v2.pb(b[i]);
		v2.pb(c[i]);

		sort(all(v2));
		sort(all(v1));

		assert(v1==v2);
	}

	return 0;
}


#define _USE_MATH_DEFINES

#include <stdio.h>
#include <iterator>
#include <memory.h>
#include <iostream>
#include <algorithm>
#include <limits>
#include <iomanip>
#include <string>
#include <vector>
#include <map>
#include <math.h>
#include <sstream>
#include <assert.h>
#include <list>
#include <valarray>
#include <queue>
#include <set>
#include <iomanip>
#include <stdlib.h>

#define pb push_back
#define mp make_pair
#define X first
#define Y second
#define rep(i,x) for(int i=0; i<x; ++i)
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define out(x,type) { copy(all(x),ostream_iterator<type >(cout," ")); cout << endl; }

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int,int> pii;

template<class T1, class T2>
ostream& operator<<(ostream& os, const pair<T1,T2>& p) { os << "{" << p.X << " " << p.Y << "}"; return os; }

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

	n*=3;

	vi pl(n,0), r_pl(n,0), orig(n,0);
	rep(i,n)
	{
		cin >> pl[i];
		pl[i]--;
		r_pl[pl[i]] = i;
	}
	orig = pl;

	vi a(n/3,0), b(n/3,0), c(n/3,0);

	rep(i,n/3)
	{
		cin >> a[i] >> b[i] >> c[i];
		a[i]--,b[i]--,c[i]--;
	}

	int k1; cin >> k1; k1--;

	int ind = 0;
	vector<vi> ret(n,vi());

	rep(i,n/3)
	{
		while(pl[ind] == -1)
			ind++;

		assert(ind < n);

		int curr = pl[ind];
		pl[r_pl[a[i]]] = -1;
		pl[r_pl[b[i]]] = -1;
		pl[r_pl[c[i]]] = -1;

		{
			int k = k1;

			vi ret;
			if (k==a[i] || k==b[i] || k==c[i])
			{
				int r1,r2;

				if (k==a[i])
					r1=b[i],r2=c[i];
				if (k==b[i])
					r1=a[i],r2=c[i];
				if (k==c[i])
					r1=b[i],r2=a[i];

				//vi ret;
				if (k==curr)
				{
					rep(i,n)
						if (i!=k && (pl[r_pl[i]] == -1 && i <= max(r1,r2)))
							ret.pb(i);
					rep(i,n)
						if (i!=k && !(pl[r_pl[i]] == -1 && i <= max(r1,r2)))
							ret.pb(i);
				}
				else
				{
					rep(i,n)
						if (i!=k)
							ret.pb(i);
				}

				{
					rep(i,n-1)
						cout << ret[i]+1 << " ";
					cout << endl;
				}

				return 0;
			}
		}
	}

	return 0;
}