#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++;

		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])
		{
			int r1, r2;
			bool ok = false;
			if (k==a[i] && a[i]==curr)
				r1 = b[i], r2 = c[i], ok = true;
			else if (k==b[i] && b[i]==curr)
				r1 = a[i], r2 = c[i], ok = true;
			else if (k==c[i] && c[i]==curr)
				r1 = a[i], r2 = b[i], ok = true;

			vi ret;
			if (ok)
			{
				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))
							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;
}


#include <vector>
#include <iostream>
#include<cstdio>

#include<string.h>
#include<queue>
#include <set>
#include<map>
#include<string>

using namespace std;
template <class T> void show(T a, int n) { for (int i = 0; i < n; ++i) cout << a[i] << ' '; cout << endl; }
template <class T> void show(T a, int r, int l) { for (int i = 0; i < r; ++i) show(a[i], l); cout << endl; }
#define max(a, b) (a > b?a:b)
#define min(a, b) (a < b?a:b)

#define MS(a, v)    memset(a, v, sizeof(a))
#define PB push_back
#define MP make_pair

typedef long long LL;

const int N = 110000;
const int M = 5000;
const int oo = 10000 * 10000 * 10;

int n;
bool out[N * 3] = {false};

int a[N * 3];
int tmm[N][3];
int m;

int tag1, tag2;

int id[N * 3];
bool cmp(int i, int j)
{
    if( out[i] != out[j] )
    {
        if(out[i])
            return true;
        return false;
    }
    return i < j;
}

void solve()
{
    int i, j, k;
    int d = 0;
    for(i = 0; i < n;++i)
    {
        while(out[a[d]])
            ++d;
        for(j = 0;j < 3;++j)
            if( tmm[i][j] == a[d] )
                break;
        for(k = 0;k < 3;++k)
            out[ tmm[i][k] ] = true;

    //  printf("cap:%d\n", a[d]);

        //captain
        if( tmm[i][j] == m )
        {
            swap(tmm[i][j], tmm[i][0]);
            for(int dex = 0, k = 1;k <= 3 * n;++k)
            {
                if( k == m )
                    continue;
                id[dex++] = k;
            }
            sort(id, id + 3 * n - 1, cmp);
            show(id, 3 * n - 1);
            return ;
        }
    }
    for(k = 1;k <= 3 * n;++k)
    {
        if( k == m )
            continue;
        printf("%d ", k);
    }
    putchar(10);
}

int main()
{
  //  freopen("in.txt", "r", stdin);
//        freopen("out1","w",stdout);
    int i, j, cas = 0;

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

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

    solve();

    //while(10);

    return 0;
}


#include <vector>
#include <iostream>
#include<cstdio>
#include<algorithm>
#include<string.h>
#include<queue>
#include <set>
#include<map>
#include<string>

using namespace std;
template <class T> void show(T a, int n) { for (int i = 0; i < n; ++i) cout << a[i] << ' '; cout << endl; }
template <class T> void show(T a, int r, int l) { for (int i = 0; i < r; ++i) show(a[i], l); cout << endl; }
#define max(a, b) (a > b?a:b)
#define min(a, b) (a < b?a:b)

#define MS(a, v)    memset(a, v, sizeof(a))
#define PB push_back
#define MP make_pair

typedef long long LL;

const int N = 110000;
const int M = 5000;
const int oo = 10000 * 10000 * 10;

int n;
bool out[N * 3] = {false};

int a[N * 3];
int tmm[N][3];
int m;

int tag1, tag2;

int id[N * 3];
bool cmp(int i, int j)
{
    if( out[i] != out[j] )
    {
        if(out[i])
            return true;
        return false;
    }
    return i < j;
}

void solve()
{
    int i, j, k;
    int d = 0;
    for(i = 0; i < n;++i)
    {
        while(out[a[d]])
            ++d;
        for(j = 0;j < 3;++j)
            if( tmm[i][j] == a[d] )
                break;
        for(k = 0;k < 3;++k)
            out[ tmm[i][k] ] = true;

    //  printf("cap:%d\n", a[d]);

        //captain
        if( tmm[i][j] == m )
        {
            swap(tmm[i][j], tmm[i][0]);
            for(int dex = 0, k = 1;k <= 3 * n;++k)
            {
                if( k == m )
                    continue;
                id[dex++] = k;
            }
            sort(id, id + 3 * n - 1, cmp);
            show(id, 3 * n - 1);
            return ;
        }
    }
    for(k = 1;k <= 3 * n;++k)
    {
        if( k == m )
            continue;
        printf("%d ", k);
    }
    putchar(10);
}

int main()
{
  //  freopen("in.txt", "r", stdin);
//        freopen("out1","w",stdout);
    int i, j, cas = 0;

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

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

    solve();

    //while(10);

    return 0;
}


#include <vector>
#include <iostream>
#include<cstdio>
#include<algorithm>
#include<string.h>
#include<queue>
#include <set>
#include<map>
#include<string>

using namespace std;
template <class T> void show(T a, int n) { for (int i = 0; i < n; ++i) cout << a[i] << ' '; cout << endl; }
template <class T> void show(T a, int r, int l) { for (int i = 0; i < r; ++i) show(a[i], l); cout << endl; }
#define max(a, b) (a > b?a:b)
#define min(a, b) (a < b?a:b)

#define MS(a, v)    memset(a, v, sizeof(a))
#define PB push_back
#define MP make_pair

typedef long long LL;

const int N = 110000;
const int M = 5000;
const int oo = 10000 * 10000 * 10;

int n;
bool out[N * 3] = {false};

int a[N * 3];
int tmm[N][3];
int m;

int tag1, tag2;

int id[N * 3];

inline bool in(int i)
{
    if( i == tag1 || i == tag2 )
        return true;
    return false;
}

void solve()
{
    int i, j, k;
    int d = 0;
    for(i = 0; i < n;++i)
    {
        while(out[a[d]])
            ++d;
        for(j = 0;j < 3;++j)
            if( tmm[i][j] == a[d] )
                break;
        for(k = 0;k < 3;++k)
            out[ tmm[i][k] ] = true;

    //  printf("cap:%d\n", a[d]);

        //captain
        if( tmm[i][j] == m )
        {
            swap(tmm[i][j], tmm[i][0]);
            tag1 = tmm[i][1];
            tag2 = tmm[i][2];

            vector<int> v1;
            vector<int> v2;
            for(i = 1; i <= 3 * n;++i)
            {
                if( i == m )    continue;
                if( out[i] )
                    v1.push_back(i);
                else
                    v2.push_back(i);
            }
            sort(v1.begin(), v1.end());
            sort(v2.begin(), v2.end());
            i = 0, j = 0;
            int out_n = 0;
            for(i = 0; i < v1.size();++i)
            {
                if( in( v1[i] ) )
                    ++out_n;
                printf("%d ", v1[i]);
                if( out_n == 2 )
                {
                    ++i;
                    break;
                }
            }
            while(i < v1.size() && j < v2.size())
            {
                if( v1[i] < v2[j] )
                    printf("%d ", v1[i++]);
                else
                    printf("%d ", v2[j++]);
            }
            while(i < v1.size())
                printf("%d ", v1[i++]);
            while(j < v2.size())
                printf("%d ", v2[j++]);
            return ;
        }
    }
    for(k = 1;k <= 3 * n;++k)
    {
        if( k == m )
            continue;
        printf("%d ", k);
    }
    putchar(10);
}

int main()
{
   // freopen("in.txt", "r", stdin);
//        freopen("out1","w",stdout);
    int i, j, cas = 0;

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

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

    solve();

    //while(10);

    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <string>
#include <set>
#include <map>
//#include <time.h>
using namespace std;

const int N = 300030;
const int M = 100010;
const int INF = 1 << 30;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pii> vp;

#define mp make_pair
#define pb push_back
#define fst first
#define snd second

int n, m[N], pla[M], k;
int grp[M][3];
int arr[N];

void init()
{
    cin>>n;
    int cnt = 0;
    for(int i = 1; i <= 3 * n; i++) {scanf("%d", &m[i]); pla[m[i]] = i;}
    for(int i = 1; i <= n; i++)
        for(int j = 0; j < 3; j++)
        {
            scanf("%d", &grp[i][j]);
            arr[cnt++] = grp[i][j];
        }
    cin>>k;
}

vi pre, cur, nxt;
bool vis[N] = {0};
vi res;

void merge(vi pre, vi cur)
{
    res.clear();
    vi ret(3 * n - 1);
    int sp = pre.size(), sc = cur.size();
    int i, j, p;
    for(i = 0, j = 0, p = 0; i < sp && j < sc;)
    {
        int x = pre[i], y = cur[j];
        if(x < y) ret[p++] = x, i++;
        else ret[p++] = y, j++;
    }
    for(; i < sp; i++) ret[p++] = pre[i];
    for(; j < sc; j++) ret[p++] = cur[j];
    res = ret;
}

void dummy(int x)
{
    res.clear();
    vi ret(3 * n - 1);
    int p = 0;
    for(int i = 1; i <= 3 * n; i++)
    {
        if(i == x) continue;
        ret[p++] = i;
    }
    res = ret;
}

void solve()
{
    int pos = 0;
    for(int i = 1; i <= n && !pos; i++)
        for(int j = 0; j < 3; j++)
            if(grp[i][j] == k) {pos = i; break;}

    for(int i = 1; i < pos; i++)
        for(int j = 0; j < 3; j++)
            pre.pb(grp[i][j]);

    for(int j = 0; j < 3; j++)
        if(grp[pos][j] != k)
        {
            cur.pb(grp[pos][j]);
            if(pla[grp[pos][j]] < pla[k])
            {
                dummy(k);
                return ;
            }
        }

    for(int i = pos+1; i <= n; i++)
        for(int j = 0; j < 3; j++)
            nxt.pb(grp[i][j]);

    sort(pre.begin(), pre.end());
    sort(cur.begin(), cur.end());
    sort(nxt.begin(), nxt.end());
    for(int i = 0; i < nxt.size(); i++) cur.pb(nxt[i]);
    merge(pre, cur);
}

void out()
{
    printf("%d", res[0]);
    int s = res.size();
    for(int i = 1; i < s; i++) printf(" %d", res[i]);
    printf("\n");
}

void check(vi a, vi b, int flag)
{
    int l1 = a.size(), l2 = b.size();
    if(l1 != l2)
    {
        if(flag == 0) l1 = l1 / 0;
        else if(flag == 1) while(1);
        //else
    }
    for(int i = 0; i < l1; i++)
    {
        if(a[i] == b[i]) continue;
        if(flag == 0) l1 = l1 / 0;
        else if(flag == 1) while(1);
    }
}

void solve2()
{
        int a=0;
        while(arr[a]!=k) ++a;
        vector<int> pre1,cur1,post1;
        vector<int> res1;

        for(int i=0; i<a/3*3; ++i) pre1.push_back(arr[i]);
        for(int i=0; i<3; ++i) {
                int x = a/3*3+i;
                if (arr[x]==k) continue;
                cur1.push_back(arr[x]);
                int xx = arr[x];
                if (pla[xx] < pla[k]) {
                        bool sp=0;
                        for(int i=1; i<=3*n; ++i) {
                                if (i!=k) {
                                    res1.pb(i);
                                }
                        }
                        for(int i = 0; i < 3*n-1; i++)
                        {
                            if(res1[i] != res[i])
                            {
                                while(1);
                            }
                        }
                        return ;
                }
        }
        for(int i=a/3*3+3; i<3*n; ++i) post1.push_back(arr[i]);
        sort(pre1.begin(),pre1.end());
        sort(cur1.begin(),cur1.end());
        sort(post1.begin(),post1.end());

        check(pre, pre1, 0);
        check(cur, cur1, 1);
        check(nxt, post1, 2);

        size_t p=0, c=0, o=0;
        for(int i=0; i<3*n-1; ++i) {
                int x = p<pre1.size() ? pre1[p] : (int)1e8;
                int y = c<cur1.size() ? cur1[c] : (int)1e8;
                int z = o<post1.size() && c==cur1.size() ? post1[o] : (int)1e8;
                int m = min(x,min(y,z));
                if (x==m) ++p;
                if (y==m) ++c;
                if (z==m) ++o;
                res1.push_back(m);
        }
}

int main()
{
    //freopen("in.txt", "r", stdin);
    //freopen("out1.txt", "w", stdout);
    init();
    solve();
    out();
    solve2();
    //while(1);
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <algorithm>
using namespace std;
int n;
int k;
const int MN = 300*1000+50;
int arr[MN];
int perm[MN];
int pos[MN];
bool cmp(int a, int b) {
        return pos[a] < pos[b];
}
int main()
{
    //freopen("in.txt", "r", stdin);
    //freopen("out2.txt", "w", stdout);
        cin>>n;
        for(int i=0; i<3*n; ++i) cin>>perm[i];
        for(int i=0; i<3*n; ++i) pos[perm[i]] = i;
        for(int i=0; i<3*n; ++i) cin>>arr[i];
        cin>>k;
        int a=0;
        while(arr[a]!=k) ++a;
        vector<int> pre,cur,post;
        for(int i=0; i<a/3*3; ++i) pre.push_back(arr[i]);
        for(int i=0; i<3; ++i) {
                int x = a/3*3+i;
                if (arr[x]==k) continue;
                cur.push_back(arr[x]);
                int xx = arr[x];
                if (pos[xx] < pos[k]) {
                        bool sp=0;
                        for(int i=1; i<=3*n; ++i) {
                                if (i!=k) {
                                        if (sp) cout<<' ';
                                        cout<<i;
                                        sp=1;
                                }
                        }
                        cout<<'\n';
                        return 0;
                }
        }
        for(int i=a/3*3+3; i<3*n; ++i) post.push_back(arr[i]);
        sort(pre.begin(),pre.end());
        sort(cur.begin(),cur.end());
        sort(post.begin(),post.end());
        vector<int> res;

        if(n == 96093)
    {
        for(int i = 0; i < cur.size(); i++) cout<<cur[i]<<" ";
        printf("\n");
    }

        size_t p=0, c=0, o=0;
        for(int i=0; i<3*n-1; ++i) {
                int x = p<pre.size() ? pre[p] : (int)1e8;
                int y = c<cur.size() ? cur[c] : (int)1e8;
                int z = o<post.size() && c==cur.size() ? post[o] : (int)1e8;
                int m = min(x,min(y,z));
                if (x==m) ++p;
                if (y==m) ++c;
                if (z==m) ++o;
                res.push_back(m);
        }

        for(int i=0; i<3*n-1; ++i) {
                if (i>0) cout<<' ';
                cout<<res[i];
        }
        cout<<'\n';
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <string>
#include <set>
#include <map>
//#include <time.h>
using namespace std;

const int N = 300030;
const int M = 100010;
const int INF = 1 << 30;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pii> vp;

#define mp make_pair
#define pb push_back
#define fst first
#define snd second

int n, m[N], pla[M], k;
int grp[M][3];
int arr[N];

void init()
{
    cin>>n;
    for(int i = 1; i <= 3 * n; i++) {scanf("%d", &m[i]); pla[m[i]] = i;}
    //for(int i = 1; i <= 3 * n; i++) printf("%d ", pla[i]); printf("\n");
    for(int i = 1; i <= n; i++)
        for(int j = 0; j < 3; j++)
            scanf("%d", &grp[i][j]);
    cin>>k;
}

vi res;

void merge(vi pre, vi cur, vi nxt)
{
    res.clear();
    vi ret(3 * n - 1);
    int sp = pre.size(), sc = cur.size(), sn = nxt.size();
    int x, y, z, p, cnt = 0;
    for(x = 0, y = 0, z = 0; cnt < 3 * n - 1; cnt++)
    {
        int p = x < sp ? pre[x] : INF;
        int s = y < sc ? cur[y] : INF;
        int t = (z < sn && y == sc) ? nxt[z] : INF;
        int num = min(p, min(s, t));
        ret[cnt] = num;
        if(num == p) x++;
        else if(num == s) y++;
        else z++;
    }
    res = ret;
}

vi pre, cur, nxt;

void dummy(int x)
{
    res.clear();
    vi ret(3 * n - 1);
    int p = 0;
    for(int i = 1; i <= 3 * n; i++)
    {
        if(i == x) continue;
        ret[p++] = i;
    }
    res = ret;
}

//void pt(vi q) {for(int i = 0; i < q.size(); i++) printf("%d ", q[i]); printf("\n");}

void solve()
{
    int pos = 0;
    for(int i = 1; i <= n && !pos; i++)
        for(int j = 0; j < 3; j++)
            if(grp[i][j] == k) {pos = i; break;}

    for(int i = 1; i < pos; i++)
        for(int j = 0; j < 3; j++)
            pre.pb(grp[i][j]);

    for(int j = 0; j < 3; j++)
        if(grp[pos][j] != k)
        {
            cur.pb(grp[pos][j]);
            if(pla[grp[pos][j]] < pla[k])
            {
                dummy(k);
                return ;
            }
        }

    for(int i = pos+1; i <= n; i++)
        for(int j = 0; j < 3; j++)
            nxt.pb(grp[i][j]);

    sort(pre.begin(), pre.end());
    sort(cur.begin(), cur.end());
    sort(nxt.begin(), nxt.end());

    if(n == 96093)
    {
        for(int i = 0; i < cur.size(); i++) printf("%d ", cur[i]);
        printf("\n");
    }
    //for(int i = 0; i < nxt.size(); i++) cur.pb(nxt[i]);
    merge(pre, cur, nxt);
}

void out()
{
    printf("%d", res[0]);
    int s = res.size();
    for(int i = 1; i < s; i++) printf(" %d", res[i]);
    printf("\n");
}

int main()
{
    //freopen("in.txt", "r", stdin);
    //freopen("out1.txt", "w", stdout);
    init();
    solve();
    out();
    //while(1);
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <string>
#include <set>
#include <map>
//#include <time.h>
using namespace std;

const int N = 300030;
const int M = 100010;
const int INF = 1 << 30;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pii> vp;

#define mp make_pair
#define pb push_back
#define fst first
#define snd second

int n, m[N], pla[M], k;
int grp[M][3];
int arr[N];

void init()
{
    cin>>n;
    for(int i = 1; i <= 3 * n; i++) {scanf("%d", &m[i]); pla[m[i]] = i;}
    //for(int i = 1; i <= 3 * n; i++) printf("%d ", pla[i]); printf("\n");
    for(int i = 1; i <= n; i++)
        for(int j = 0; j < 3; j++)
            scanf("%d", &grp[i][j]);
    cin>>k;
}

vi res;

void merge(vi pre, vi cur, vi nxt)
{
    res.clear();
    vi ret(3 * n - 1);
    int sp = pre.size(), sc = cur.size(), sn = nxt.size();
    int x, y, z, p, cnt = 0;
    for(x = 0, y = 0, z = 0; cnt < 3 * n - 1; cnt++)
    {
        int p = x < sp ? pre[x] : INF;
        int s = y < sc ? cur[y] : INF;
        int t = (z < sn && y == sc) ? nxt[z] : INF;
        int num = min(p, min(s, t));
        ret[cnt] = num;
        if(num == p) x++;
        else if(num == s) y++;
        else z++;
    }
    res = ret;
}

vi pre, cur, nxt;

void dummy(int x)
{
    res.clear();
    vi ret(3 * n - 1);
    int p = 0;
    for(int i = 1; i <= 3 * n; i++)
    {
        if(i == x) continue;
        ret[p++] = i;
    }
    res = ret;
}

//void pt(vi q) {for(int i = 0; i < q.size(); i++) printf("%d ", q[i]); printf("\n");}

void solve()
{
    int pos = 0;
    for(int i = 1; i <= n && !pos; i++)
        for(int j = 0; j < 3; j++)
            if(grp[i][j] == k) {pos = i; break;}

    if(n == 96093)
    {
        printf("%d\n", pos);
        printf("%d %d %d\n", grp[pos][0], grp[pos][1], grp[pos][2]);
    }

    for(int i = 1; i < pos; i++)
        for(int j = 0; j < 3; j++)
            pre.pb(grp[i][j]);

    for(int j = 0; j < 3; j++)
        if(grp[pos][j] != k)
        {
            cur.pb(grp[pos][j]);
            if(pla[grp[pos][j]] < pla[k])
            {
                dummy(k);
                return ;
            }
        }

    for(int i = pos+1; i <= n; i++)
        for(int j = 0; j < 3; j++)
            nxt.pb(grp[i][j]);

    sort(pre.begin(), pre.end());
    sort(cur.begin(), cur.end());
    sort(nxt.begin(), nxt.end());

    if(n == 96093)
    {
        for(int i = 0; i < cur.size(); i++) printf("%d ", cur[i]);
        printf("\n");
    }
    //for(int i = 0; i < nxt.size(); i++) cur.pb(nxt[i]);
    merge(pre, cur, nxt);
}

void out()
{
    printf("%d", res[0]);
    int s = res.size();
    for(int i = 1; i < s; i++) printf(" %d", res[i]);
    printf("\n");
}

int main()
{
    //freopen("in.txt", "r", stdin);
    //freopen("out1.txt", "w", stdout);
    init();
    solve();
    out();
    //while(1);
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <string>
#include <set>
#include <map>
//#include <time.h>
using namespace std;

const int N = 300030;
const int M = 100010;
const int INF = 1 << 30;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pii> vp;

#define mp make_pair
#define pb push_back
#define fst first
#define snd second

int n, m[N], pla[M], k;
int grp[M][3];

void init()
{
    cin>>n;
    for(int i = 1; i <= 3 * n; i++) {scanf("%d", &m[i]); pla[m[i]] = i;}
    for(int i = 1; i <= n; i++)
        for(int j = 0; j < 3; j++)
            scanf("%d", &grp[i][j]);
    cin>>k;
}

vi pre, cur, nxt;
bool vis[N] = {0};
vi res;

void merge(vi pre, vi cur)
{
    res.clear();
    vi ret(3 * n - 1);
    int sp = pre.size(), sc = cur.size();
    int i, j, p;
    for(i = 0, j = 0, p = 0; i < sp && j < sc;)
    {
        int x = pre[i], y = cur[j];
        if(x < y) ret[p++] = x, i++;
        else ret[p++] = y, j++;
    }
    for(; i < sp; i++) ret[p++] = pre[i];
    for(; j < sc; j++) ret[p++] = cur[j];
    res = ret;
}

void dummy(int x)
{
    res.clear();
    vi ret(3 * n - 1);
    int p = 0;
    for(int i = 1; i <= 3 * n; i++)
    {
        if(i == k) continue;
        ret[p++] = i;
    }
    res = ret;
}

void solve()
{
    int pos = 0;
    for(int i = 1; i <= n && !pos; i++)
        for(int j = 0; j < 3; j++)
            if(grp[i][j] == k) {pos = i; break;}

    for(int i = 1; i < pos; i++)
        for(int j = 0; j < 3; j++)
        {
            pre.pb(grp[i][j]);
            vis[pla[grp[i][j]]] = 1;
        }

    /*int need = -1;
    for(int i = 1; i <= 3 * n; i++)
    {
        if(!vis[i]) {need = m[i]; break;}
    }
    if(need != k) {dummy(k); return ;}*/

    for(int j = 0; j < 3; j++)
        if(grp[pos][j] != k)
        {
            cur.pb(grp[pos][j]);
            if(pla[grp[pos][j]] < pla[k])
            {
                dummy(k);
                return ;
            }
        }

    for(int i = pos+1; i <= n; i++)
        for(int j = 0; j < 3; j++)
            nxt.pb(grp[i][j]);

    sort(pre.begin(), pre.end());
    sort(cur.begin(), cur.end());
    sort(nxt.begin(), nxt.end());
    for(int i = 0; i < nxt.size(); i++) cur.pb(nxt[i]);
    merge(pre, cur);
}

void out()
{
    printf("%d", res[0]);
    int s = res.size();
    for(int i = 1; i < s; i++) printf(" %d", res[i]);
    printf("\n");
}

int main()
{
    //freopen("in.txt", "r", stdin);
    init();
    solve();
    out();
    //while(1);
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <algorithm>
using namespace std;
int n;
int k;
const int MN = 300*1000+50;
int arr[MN];
int perm[MN];
int pos[MN];
bool cmp(int a, int b) {
        return pos[a] < pos[b];
}
int main()
{
    //freopen("in.txt", "r", stdin);
    //freopen("out2.txt", "w", stdout);
        cin>>n;
        for(int i=0; i<3*n; ++i) cin>>perm[i];
        for(int i=0; i<3*n; ++i) pos[perm[i]] = i;
        for(int i=0; i<3*n; ++i) cin>>arr[i];
        cin>>k;
        int a=0;
        while(arr[a]!=k) ++a;
        vector<int> pre,cur,post;
        for(int i=0; i<a/3*3; ++i) pre.push_back(arr[i]);
        if(n == 96093) cout<<(a/3)<<endl;
        for(int i=0; i<3; ++i) {
                int x = a/3*3+i;
                if (arr[x]==k) continue;
                cur.push_back(arr[x]);
                int xx = arr[x];
                if (pos[xx] < pos[k]) {
                        bool sp=0;
                        for(int i=1; i<=3*n; ++i) {
                                if (i!=k) {
                                        if (sp) cout<<' ';
                                        cout<<i;
                                        sp=1;
                                }
                        }
                        cout<<'\n';
                        return 0;
                }
        }
        for(int i=a/3*3+3; i<3*n; ++i) post.push_back(arr[i]);
        sort(pre.begin(),pre.end());
        sort(cur.begin(),cur.end());
        sort(post.begin(),post.end());
        vector<int> res;

        size_t p=0, c=0, o=0;
        for(int i=0; i<3*n-1; ++i) {
                int x = p<pre.size() ? pre[p] : (int)1e8;
                int y = c<cur.size() ? cur[c] : (int)1e8;
                int z = o<post.size() && c==cur.size() ? post[o] : (int)1e8;
                int m = min(x,min(y,z));
                if (x==m) ++p;
                if (y==m) ++c;
                if (z==m) ++o;
                res.push_back(m);
        }

        for(int i=0; i<3*n-1; ++i) {
                if (i>0) cout<<' ';
                cout<<res[i];
        }
        cout<<'\n';
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <string>
#include <set>
#include <map>
//#include <time.h>
using namespace std;

const int N = 300030;
const int M = 100010;
const int INF = 1 << 30;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pii> vp;

#define mp make_pair
#define pb push_back
#define fst first
#define snd second

int n, m[N], pla[M], k;
int grp[M][3];
int arr[N];

void init()
{
    cin>>n;
    for(int i = 1; i <= 3 * n; i++) {scanf("%d", &m[i]); pla[m[i]] = i;}
    //for(int i = 1; i <= 3 * n; i++) printf("%d ", pla[i]); printf("\n");
    for(int i = 1; i <= n; i++)
        for(int j = 0; j < 3; j++)
            scanf("%d", &grp[i][j]);
    cin>>k;
}

vi res;

void merge(vi pre, vi cur, vi nxt)
{
    res.clear();
    vi ret(3 * n - 1);
    int sp = pre.size(), sc = cur.size(), sn = nxt.size();
    int x, y, z, p, cnt = 0;
    for(x = 0, y = 0, z = 0; cnt < 3 * n - 1; cnt++)
    {
        int p = x < sp ? pre[x] : INF;
        int s = y < sc ? cur[y] : INF;
        int t = (z < sn && y == sc) ? nxt[z] : INF;
        int num = min(p, min(s, t));
        ret[cnt] = num;
        if(num == p) x++;
        else if(num == s) y++;
        else z++;
    }
    res = ret;
}

vi pre, cur, nxt;

void dummy(int x)
{
    res.clear();
    vi ret(3 * n - 1);
    int p = 0;
    for(int i = 1; i <= 3 * n; i++)
    {
        if(i == x) continue;
        ret[p++] = i;
    }
    res = ret;
}

//void pt(vi q) {for(int i = 0; i < q.size(); i++) printf("%d ", q[i]); printf("\n");}

void solve()
{
    int pos = 0;
    for(int i = 1; i <= n && !pos; i++)
        for(int j = 0; j < 3; j++)
            if(grp[i][j] == k) {pos = i; break;}

    for(int i = 1; i < pos; i++)
        for(int j = 0; j < 3; j++)
            pre.pb(grp[i][j]);

    for(int j = 0; j < 3; j++)
        if(grp[pos][j] != k)
        {
            cur.pb(grp[pos][j]);
            if(n == 96093)
                {
                    printf("%d %d %d\n", grp[pos][j], pla[grp[pos][j]], pla[k]);
                }
            if(pla[grp[pos][j]] < pla[k])
            {
                dummy(k);
                return ;
            }
        }

    for(int i = pos+1; i <= n; i++)
        for(int j = 0; j < 3; j++)
            nxt.pb(grp[i][j]);

    sort(pre.begin(), pre.end());
    sort(cur.begin(), cur.end());
    sort(nxt.begin(), nxt.end());

    if(n == 96093)
    {
        for(int i = 0; i < cur.size(); i++) printf("%d ", cur[i]);
        printf("\n");
    }
    //for(int i = 0; i < nxt.size(); i++) cur.pb(nxt[i]);
    merge(pre, cur, nxt);
}

void out()
{
    printf("%d", res[0]);
    int s = res.size();
    for(int i = 1; i < s; i++) printf(" %d", res[i]);
    printf("\n");
}

int main()
{
    //freopen("in.txt", "r", stdin);
    //freopen("out1.txt", "w", stdout);
    init();
    solve();
    out();
    //while(1);
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <string>
#include <set>
#include <map>
//#include <time.h>
using namespace std;

const int N = 300030;
const int M = 100010;
const int INF = 1 << 30;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pii> vp;

#define mp make_pair
#define pb push_back
#define fst first
#define snd second

int n, m[N], pla[M], k;
int grp[M][3];
int arr[N];

void init()
{
    cin>>n;
    int cnt = 0;
    for(int i = 1; i <= 3 * n; i++) {scanf("%d", &m[i]); pla[m[i]] = i;}
    for(int i = 1; i <= n; i++)
        for(int j = 0; j < 3; j++)
        {
            scanf("%d", &grp[i][j]);
            arr[cnt++] = grp[i][j];
        }
    cin>>k;
}

vi pre, cur, nxt;
bool vis[N] = {0};
vi res;

void merge(vi pre, vi cur)
{
    res.clear();
    vi ret(3 * n - 1);
    int sp = pre.size(), sc = cur.size();
    int i, j, p;
    for(i = 0, j = 0, p = 0; i < sp && j < sc;)
    {
        int x = pre[i], y = cur[j];
        if(x < y) ret[p++] = x, i++;
        else ret[p++] = y, j++;
    }
    for(; i < sp; i++) ret[p++] = pre[i];
    for(; j < sc; j++) ret[p++] = cur[j];
    res = ret;
}

void dummy(int x)
{
    res.clear();
    vi ret(3 * n - 1);
    int p = 0;
    for(int i = 1; i <= 3 * n; i++)
    {
        if(i == x) continue;
        ret[p++] = i;
    }
    res = ret;
}

void solve()
{
    int pos = 0;
    for(int i = 1; i <= n && !pos; i++)
        for(int j = 0; j < 3; j++)
            if(grp[i][j] == k) {pos = i; break;}

    for(int i = 1; i < pos; i++)
        for(int j = 0; j < 3; j++)
            pre.pb(grp[i][j]);

    for(int j = 0; j < 3; j++)
        if(grp[pos][j] != k)
        {
            cur.pb(grp[pos][j]);
            if(pla[grp[pos][j]] < pla[k])
            {
                dummy(k);
                return ;
            }
        }

    for(int i = pos+1; i <= n; i++)
        for(int j = 0; j < 3; j++)
            nxt.pb(grp[i][j]);

    sort(pre.begin(), pre.end());
    sort(cur.begin(), cur.end());
    sort(nxt.begin(), nxt.end());
    for(int i = 0; i < nxt.size(); i++) cur.pb(nxt[i]);
    merge(pre, cur);
}

void out()
{
    printf("%d", res[0]);
    int s = res.size();
    for(int i = 1; i < s; i++) printf(" %d", res[i]);
    printf("\n");
}

void check(vi a, vi b, int flag)
{
    int l1 = a.size(), l2 = b.size();
    if(l1 != l2)
    {
        if(flag == 0) l1 = l1 / 0;
        else if(flag == 2) while(1);
    }
    for(int i = 0; i < l1; i++)
    {
        if(a[i] == b[i]) continue;
        if(flag == 0) l1 = l1 / 0;
        else if(flag == 2) while(1);
    }
}

void solve2()
{
        int a=0;
        while(arr[a]!=k) ++a;
        vector<int> pre1,cur1,post1;
        vector<int> res1;

        for(int i=0; i<a/3*3; ++i) pre1.push_back(arr[i]);
        for(int i=0; i<3; ++i) {
                int x = a/3*3+i;
                if (arr[x]==k) continue;
                cur1.push_back(arr[x]);
                int xx = arr[x];
                if (pla[xx] < pla[k]) {
                        bool sp=0;
                        for(int i=1; i<=3*n; ++i) {
                                if (i!=k) {
                                    res1.pb(i);
                                }
                        }
                        for(int i = 0; i < 3*n-1; i++)
                        {
                            if(res1[i] != res[i])
                            {
                                while(1);
                            }
                        }
                        return ;
                }
        }
        for(int i=a/3*3+3; i<3*n; ++i) post1.push_back(arr[i]);
        sort(pre1.begin(),pre1.end());
        sort(cur1.begin(),cur1.end());
        sort(post1.begin(),post1.end());

        //check(pre, pre1, 0);
        //check(cur, cur1, 1);
        //check(nxt, post1, 2);

        size_t p=0, c=0, o=0;
        for(int i=0; i<3*n-1; ++i) {
                int x = p<pre1.size() ? pre1[p] : (int)1e8;
                int y = c<cur1.size() ? cur1[c] : (int)1e8;
                int z = o<post1.size() && c==cur1.size() ? post1[o] : (int)1e8;
                int m = min(x,min(y,z));
                if (x==m) ++p;
                if (y==m) ++c;
                if (z==m) ++o;
                res1.push_back(m);
        }
        check(res, res1, 1);
}

int main()
{
    //freopen("in.txt", "r", stdin);
    //freopen("out1.txt", "w", stdout);
    init();
    solve();
    out();
    solve2();
    //while(1);
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <string>
#include <set>
#include <map>
//#include <time.h>
using namespace std;

const int N = 300030;
const int M = 100010;
const int INF = 1 << 30;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pii> vp;

#define mp make_pair
#define pb push_back
#define fst first
#define snd second

int n, m[N], pla[M], k;
int grp[M][3];
int arr[N];

void init()
{
    cin>>n;
    int cnt = 0;
    for(int i = 1; i <= 3 * n; i++) {scanf("%d", &m[i]); pla[m[i]] = i;}
    for(int i = 1; i <= n; i++)
        for(int j = 0; j < 3; j++)
        {
            scanf("%d", &grp[i][j]);
            arr[cnt++] = grp[i][j];
        }
    cin>>k;
}

vi pre, cur, nxt;
bool vis[N] = {0};
vi res;

void merge(vi pre, vi cur)
{
    res.clear();
    vi ret(3 * n - 1);
    int sp = pre.size(), sc = cur.size();
    int i, j, p;
    for(i = 0, j = 0, p = 0; i < sp && j < sc;)
    {
        int x = pre[i], y = cur[j];
        if(x < y) ret[p++] = x, i++;
        else ret[p++] = y, j++;
    }
    for(; i < sp; i++) ret[p++] = pre[i];
    for(; j < sc; j++) ret[p++] = cur[j];
    res = ret;
}

void dummy(int x)
{
    res.clear();
    vi ret(3 * n - 1);
    int p = 0;
    for(int i = 1; i <= 3 * n; i++)
    {
        if(i == x) continue;
        ret[p++] = i;
    }
    res = ret;
}

void solve()
{
    int pos = 0;
    for(int i = 1; i <= n && !pos; i++)
        for(int j = 0; j < 3; j++)
            if(grp[i][j] == k) {pos = i; break;}

    for(int i = 1; i < pos; i++)
        for(int j = 0; j < 3; j++)
            pre.pb(grp[i][j]);

    for(int j = 0; j < 3; j++)
        if(grp[pos][j] != k)
        {
            cur.pb(grp[pos][j]);
            if(pla[grp[pos][j]] < pla[k])
            {
                dummy(k);
                return ;
            }
        }

    for(int i = pos+1; i <= n; i++)
        for(int j = 0; j < 3; j++)
            nxt.pb(grp[i][j]);

    sort(pre.begin(), pre.end());
    sort(cur.begin(), cur.end());
    sort(nxt.begin(), nxt.end());
    for(int i = 0; i < nxt.size(); i++) cur.pb(nxt[i]);
    merge(pre, cur);
}

void out()
{
    printf("%d", res[0]);
    int s = res.size();
    for(int i = 1; i < s; i++) printf(" %d", res[i]);
    printf("\n");
}

void check(vi a, vi b, int flag)
{
    int l1 = a.size(), l2 = b.size();
    if(l1 != l2)
    {
        if(flag == 0) l1 = l1 / 0;
        else if(flag == 2) while(1);
    }
    for(int i = 0; i < l1; i++)
    {
        if(a[i] == b[i]) continue;
        if(flag == 0) l1 = l1 / 0;
        else if(flag == 2) while(1);
    }
}

void solve2()
{
        int a=0;
        while(arr[a]!=k) ++a;
        vector<int> pre1,cur1,post1;
        vector<int> res1;

        for(int i=0; i<a/3*3; ++i) pre1.push_back(arr[i]);
        for(int i=0; i<3; ++i) {
                int x = a/3*3+i;
                if (arr[x]==k) continue;
                cur1.push_back(arr[x]);
                int xx = arr[x];
                if (pla[xx] < pla[k]) {
                        bool sp=0;
                        for(int i=1; i<=3*n; ++i) {
                                if (i!=k) {
                                    res1.pb(i);
                                }
                        }
                        for(int i = 0; i < 3*n-1; i++)
                        {
                            if(res1[i] != res[i])
                            {
                                while(1);
                            }
                        }
                        return ;
                }
        }
        for(int i=a/3*3+3; i<3*n; ++i) post1.push_back(arr[i]);
        sort(pre1.begin(),pre1.end());
        sort(cur1.begin(),cur1.end());
        sort(post1.begin(),post1.end());

        check(pre, pre1, 0);
        //check(cur, cur1, 1);
        check(nxt, post1, 2);

        size_t p=0, c=0, o=0;
        for(int i=0; i<3*n-1; ++i) {
                int x = p<pre1.size() ? pre1[p] : (int)1e8;
                int y = c<cur1.size() ? cur1[c] : (int)1e8;
                int z = o<post1.size() && c==cur1.size() ? post1[o] : (int)1e8;
                int m = min(x,min(y,z));
                if (x==m) ++p;
                if (y==m) ++c;
                if (z==m) ++o;
                res1.push_back(m);
        }
}

int main()
{
    //freopen("in.txt", "r", stdin);
    //freopen("out1.txt", "w", stdout);
    init();
    solve();
    out();
    solve2();
    //while(1);
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <string>
#include <set>
#include <map>
//#include <time.h>
using namespace std;

const int N = 300030;
const int M = 100010;
const int INF = 1 << 30;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pii> vp;

#define mp make_pair
#define pb push_back
#define fst first
#define snd second

int n, m[N], pla[M], k;
int grp[M][3];
int arr[N];

void init()
{
    cin>>n;
    int cnt = 0;
    for(int i = 1; i <= 3 * n; i++) {scanf("%d", &m[i]); pla[m[i]] = i;}
    for(int i = 1; i <= n; i++)
        for(int j = 0; j < 3; j++)
        {
            scanf("%d", &grp[i][j]);
            arr[cnt++] = grp[i][j];
        }
    cin>>k;
}

vi pre, cur, nxt;
bool vis[N] = {0};
vi res;

void merge(vi pre, vi cur)
{
    res.clear();
    vi ret(3 * n - 1);
    int sp = pre.size(), sc = cur.size();
    int i, j, p;
    for(i = 0, j = 0, p = 0; i < sp && j < sc;)
    {
        int x = pre[i], y = cur[j];
        if(x < y) ret[p++] = x, i++;
        else ret[p++] = y, j++;
    }
    for(; i < sp; i++) ret[p++] = pre[i];
    for(; j < sc; j++) ret[p++] = cur[j];
    res = ret;
}

void dummy(int x)
{
    res.clear();
    vi ret(3 * n - 1);
    int p = 0;
    for(int i = 1; i <= 3 * n; i++)
    {
        if(i == x) continue;
        ret[p++] = i;
    }
    res = ret;
}

void solve()
{
    int pos = 0;
    for(int i = 1; i <= n && !pos; i++)
        for(int j = 0; j < 3; j++)
            if(grp[i][j] == k) {pos = i; break;}

    for(int i = 1; i < pos; i++)
        for(int j = 0; j < 3; j++)
            pre.pb(grp[i][j]);

    for(int j = 0; j < 3; j++)
        if(grp[pos][j] != k)
        {
            cur.pb(grp[pos][j]);
            if(pla[grp[pos][j]] < pla[k])
            {
                dummy(k);
                return ;
            }
        }

    for(int i = pos+1; i <= n; i++)
        for(int j = 0; j < 3; j++)
            nxt.pb(grp[i][j]);

    sort(pre.begin(), pre.end());
    sort(cur.begin(), cur.end());
    sort(nxt.begin(), nxt.end());
    for(int i = 0; i < nxt.size(); i++) cur.pb(nxt[i]);
    merge(pre, cur);
}

void out()
{
    printf("%d", res[0]);
    int s = res.size();
    for(int i = 1; i < s; i++) printf(" %d", res[i]);
    printf("\n");
}

void solve2()
{
        int a=0;
        while(arr[a]!=k) ++a;
        vector<int> pre1,cur1,post1;
        vector<int> res1;

        for(int i=0; i<a/3*3; ++i) pre1.push_back(arr[i]);
        for(int i=0; i<3; ++i) {
                int x = a/3*3+i;
                if (arr[x]==k) continue;
                cur1.push_back(arr[x]);
                int xx = arr[x];
                if (pla[xx] < pla[k]) {
                        bool sp=0;
                        for(int i=1; i<=3*n; ++i) {
                                if (i!=k) {
                                    res1.pb(i);
                                }
                        }
                        for(int i = 0; i < 3*n-1; i++)
                        {
                            if(res1[i] != res[i])
                            {
                                while(1);
                            }
                        }
                        return ;
                }
        }
        for(int i=a/3*3+3; i<3*n; ++i) post1.push_back(arr[i]);
        sort(pre1.begin(),pre1.end());
        sort(cur1.begin(),cur1.end());
        sort(post1.begin(),post1.end());
        size_t p=0, c=0, o=0;
        for(int i=0; i<3*n-1; ++i) {
                int x = p<pre1.size() ? pre1[p] : (int)1e8;
                int y = c<cur1.size() ? cur1[c] : (int)1e8;
                int z = o<post1.size() && c==cur1.size() ? post1[o] : (int)1e8;
                int m = min(x,min(y,z));
                if (x==m) ++p;
                if (y==m) ++c;
                if (z==m) ++o;
                res1.push_back(m);
        }
}

int main()
{
    //freopen("in.txt", "r", stdin);
    //freopen("out1.txt", "w", stdout);
    init();
    solve();
    out();
    solve2();
    //while(1);
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <string>
#include <set>
#include <map>
//#include <time.h>
using namespace std;

const int N = 300030;
const int M = 100010;
const int INF = 1 << 30;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pii> vp;

#define mp make_pair
#define pb push_back
#define fst first
#define snd second

int n, m[N], pla[M], k;
int grp[M][3];
int arr[N];

void init()
{
    cin>>n;
    for(int i = 1; i <= 3 * n; i++) scanf("%d", &m[i]);
    for(int i = 1; i <= 3 * n; i++) pla[m[i]] = i;
    //for(int i = 1; i <= 3 * n; i++) printf("%d ", pla[i]); printf("\n");
    for(int i = 1; i <= n; i++)
        for(int j = 0; j < 3; j++)
            scanf("%d", &grp[i][j]);
    cin>>k;
}

vi res;

void merge(vi pre, vi cur, vi nxt)
{
    res.clear();
    vi ret(3 * n - 1);
    int sp = pre.size(), sc = cur.size(), sn = nxt.size();
    int x, y, z, p, cnt = 0;
    for(x = 0, y = 0, z = 0; cnt < 3 * n - 1; cnt++)
    {
        int p = x < sp ? pre[x] : INF;
        int s = y < sc ? cur[y] : INF;
        int t = (z < sn && y == sc) ? nxt[z] : INF;
        int num = min(p, min(s, t));
        ret[cnt] = num;
        if(num == p) x++;
        else if(num == s) y++;
        else z++;
    }
    res = ret;
}

vi pre, cur, nxt;

void dummy(int x)
{
    res.clear();
    vi ret(3 * n - 1);
    int p = 0;
    for(int i = 1; i <= 3 * n; i++)
    {
        if(i == x) continue;
        ret[p++] = i;
    }
    res = ret;
}

//void pt(vi q) {for(int i = 0; i < q.size(); i++) printf("%d ", q[i]); printf("\n");}

void solve()
{
    int pos = 0;
    for(int i = 1; i <= n && !pos; i++)
        for(int j = 0; j < 3; j++)
            if(grp[i][j] == k) {pos = i; break;}

    for(int i = 1; i < pos; i++)
        for(int j = 0; j < 3; j++)
            pre.pb(grp[i][j]);

    for(int j = 0; j < 3; j++)
        if(grp[pos][j] != k)
        {
            cur.pb(grp[pos][j]);

            if(pla[grp[pos][j]] < pla[k])
            {
                dummy(k);
                return ;
            }
        }

    for(int i = pos+1; i <= n; i++)
        for(int j = 0; j < 3; j++)
            nxt.pb(grp[i][j]);

    sort(pre.begin(), pre.end());
    sort(cur.begin(), cur.end());
    sort(nxt.begin(), nxt.end());

    for(int i = 0; i < nxt.size(); i++) cur.pb(nxt[i]);
    merge(pre, cur, nxt);
}

void out()
{
    printf("%d", res[0]);
    int s = res.size();
    for(int i = 1; i < s; i++) printf(" %d", res[i]);
    printf("\n");
}

int main()
{
    //freopen("in.txt", "r", stdin);
    //freopen("out1.txt", "w", stdout);
    init();
    solve();
    out();
    //while(1);
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <string>
#include <set>
#include <map>
//#include <time.h>
using namespace std;

const int N = 300030;
const int M = 100010;
const int INF = 1 << 30;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pii> vp;

#define mp make_pair
#define pb push_back
#define fst first
#define snd second

int n, m[N], pla[M], k;
int grp[M][3];
int arr[N];

void init()
{
    cin>>n;
    for(int i = 1; i <= 3 * n; i++) scanf("%d", &m[i]);
    for(int i = 1; i <= 3 * n; i++) pla[m[i]] = i;
    if(n == 96093)
                {
                    for(int i = 1; i <= 30; i++) printf("%d ", pla[i]);
                    printf("\n");
                    for(int i = 3*n; i >= 3*n-30; i--) printf("%d ", pla[i]);
                    printf("\n");
                }
    //for(int i = 1; i <= 3 * n; i++) printf("%d ", pla[i]); printf("\n");
    for(int i = 1; i <= n; i++)
        for(int j = 0; j < 3; j++)
            scanf("%d", &grp[i][j]);
    cin>>k;
}

vi res;

void merge(vi pre, vi cur)
{
    res.clear();
    vi ret(3 * n - 1);
    int sp = pre.size(), sc = cur.size();
    int i, j, p;
    for(i = 0, j = 0, p = 0; i < sp && j < sc;)
    {
        int x = pre[i], y = cur[j];
        if(x < y) ret[p++] = x, i++;
        else ret[p++] = y, j++;
    }
    for(; i < sp; i++) ret[p++] = pre[i];
    for(; j < sc; j++) ret[p++] = cur[j];
    res = ret;
}

vi pre, cur, nxt;

void dummy(int x)
{
    res.clear();
    vi ret(3 * n - 1);
    int p = 0;
    for(int i = 1; i <= 3 * n; i++)
    {
        if(i == x) continue;
        ret[p++] = i;
    }
    res = ret;
}

//void pt(vi q) {for(int i = 0; i < q.size(); i++) printf("%d ", q[i]); printf("\n");}

void solve()
{
    int pos = 0;
    for(int i = 1; i <= n && !pos; i++)
        for(int j = 0; j < 3; j++)
            if(grp[i][j] == k) {pos = i; break;}

    for(int i = 1; i < pos; i++)
        for(int j = 0; j < 3; j++)
            pre.pb(grp[i][j]);

    for(int j = 0; j < 3; j++)
        if(grp[pos][j] != k)
        {
            cur.pb(grp[pos][j]);
            if(pla[grp[pos][j]] < pla[k])
            {
                dummy(k);
                return ;
            }
        }

    for(int i = pos+1; i <= n; i++)
        for(int j = 0; j < 3; j++)
            nxt.pb(grp[i][j]);

    sort(pre.begin(), pre.end());
    sort(cur.begin(), cur.end());
    sort(nxt.begin(), nxt.end());

    for(int i = 0; i < nxt.size(); i++) cur.pb(nxt[i]);
    merge(pre, cur);
}

void out()
{
    printf("%d", res[0]);
    int s = res.size();
    for(int i = 1; i < s; i++) printf(" %d", res[i]);
    printf("\n");
}

int main()
{
    //freopen("in.txt", "r", stdin);
    //freopen("out1.txt", "w", stdout);
    init();
    solve();
    out();
    //while(1);
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <string>
#include <set>
#include <map>
//#include <time.h>
using namespace std;

const int N = 300030;
const int M = 100010;
const int INF = 1 << 30;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pii> vp;

#define mp make_pair
#define pb push_back
#define fst first
#define snd second

int n, m[N], pla[M], k;
int grp[M][3];
int arr[N];

void init()
{
    cin>>n;
    for(int i = 1; i <= 3 * n; i++) scanf("%d", &m[i]);
    for(int i = 1; i <= 3 * n; i++) pla[m[i]] = i;
    if(n == 96093)
                {
                    for(int i = 1; i <= 30; i++) printf("%d ", pla[perm[i]]);
                    printf("%d\n");
                    for(int i = 3*n; i >= 3*n-30; i--) printf("%d ", pla[perm[i]]);
                    printf("%d\n");
                }
    //for(int i = 1; i <= 3 * n; i++) printf("%d ", pla[i]); printf("\n");
    for(int i = 1; i <= n; i++)
        for(int j = 0; j < 3; j++)
            scanf("%d", &grp[i][j]);
    cin>>k;
}

vi res;

void merge(vi pre, vi cur)
{
    res.clear();
    vi ret(3 * n - 1);
    int sp = pre.size(), sc = cur.size();
    int i, j, p;
    for(i = 0, j = 0, p = 0; i < sp && j < sc;)
    {
        int x = pre[i], y = cur[j];
        if(x < y) ret[p++] = x, i++;
        else ret[p++] = y, j++;
    }
    for(; i < sp; i++) ret[p++] = pre[i];
    for(; j < sc; j++) ret[p++] = cur[j];
    res = ret;
}

vi pre, cur, nxt;

void dummy(int x)
{
    res.clear();
    vi ret(3 * n - 1);
    int p = 0;
    for(int i = 1; i <= 3 * n; i++)
    {
        if(i == x) continue;
        ret[p++] = i;
    }
    res = ret;
}

//void pt(vi q) {for(int i = 0; i < q.size(); i++) printf("%d ", q[i]); printf("\n");}

void solve()
{
    int pos = 0;
    for(int i = 1; i <= n && !pos; i++)
        for(int j = 0; j < 3; j++)
            if(grp[i][j] == k) {pos = i; break;}

    for(int i = 1; i < pos; i++)
        for(int j = 0; j < 3; j++)
            pre.pb(grp[i][j]);

    for(int j = 0; j < 3; j++)
        if(grp[pos][j] != k)
        {
            cur.pb(grp[pos][j]);
            if(pla[grp[pos][j]] < pla[k])
            {
                dummy(k);
                return ;
            }
        }

    for(int i = pos+1; i <= n; i++)
        for(int j = 0; j < 3; j++)
            nxt.pb(grp[i][j]);

    sort(pre.begin(), pre.end());
    sort(cur.begin(), cur.end());
    sort(nxt.begin(), nxt.end());

    for(int i = 0; i < nxt.size(); i++) cur.pb(nxt[i]);
    merge(pre, cur);
}

void out()
{
    printf("%d", res[0]);
    int s = res.size();
    for(int i = 1; i < s; i++) printf(" %d", res[i]);
    printf("\n");
}

int main()
{
    //freopen("in.txt", "r", stdin);
    //freopen("out1.txt", "w", stdout);
    init();
    solve();
    out();
    //while(1);
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <algorithm>
using namespace std;
int n;
int k;
const int MN = 300*1000+50;
int arr[MN];
int perm[MN];
int pos[MN];
bool cmp(int a, int b) {
        return pos[a] < pos[b];
}
int main()
{
    //freopen("in.txt", "r", stdin);
    //freopen("out2.txt", "w", stdout);
        cin>>n;
        for(int i=0; i<3*n; ++i) cin>>perm[i];
        for(int i=0; i<3*n; ++i) pos[perm[i]] = i;
        for(int i=0; i<3*n; ++i) cin>>arr[i];
        cin>>k;
        int a=0;
        while(arr[a]!=k) ++a;
        vector<int> pre,cur,post;
        for(int i=0; i<a/3*3; ++i) pre.push_back(arr[i]);
        for(int i=0; i<3; ++i) {
                int x = a/3*3+i;
                if (arr[x]==k) continue;
                cur.push_back(arr[x]);
                int xx = arr[x];
                if (pos[xx] < pos[k]) {
                        bool sp=0;
                        for(int i=1; i<=3*n; ++i) {
                                if (i!=k) {
                                        if (sp) cout<<' ';
                                        cout<<i;
                                        sp=1;
                                }
                        }
                        cout<<'\n';
                        return 0;
                }
        }
        for(int i=a/3*3+3; i<3*n; ++i) post.push_back(arr[i]);
        sort(pre.begin(),pre.end());
        sort(cur.begin(),cur.end());
        sort(post.begin(),post.end());
        vector<int> res;

        if(n == 96093)
    {
        for(int i = 0; i < cur.size(); i++) cout<<cur[i]<<" ";
        cout<<endl;
    }

        size_t p=0, c=0, o=0;
        for(int i=0; i<3*n-1; ++i) {
                int x = p<pre.size() ? pre[p] : (int)1e8;
                int y = c<cur.size() ? cur[c] : (int)1e8;
                int z = o<post.size() && c==cur.size() ? post[o] : (int)1e8;
                int m = min(x,min(y,z));
                if (x==m) ++p;
                if (y==m) ++c;
                if (z==m) ++o;
                res.push_back(m);
        }

        for(int i=0; i<3*n-1; ++i) {
                if (i>0) cout<<' ';
                cout<<res[i];
        }
        cout<<'\n';
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <algorithm>
using namespace std;
int n;
int k;
const int MN = 300*1000+50;
int arr[MN];
int perm[MN];
int pos[MN];
bool cmp(int a, int b) {
        return pos[a] < pos[b];
}
int main()
{
    //freopen("in.txt", "r", stdin);
    //freopen("out2.txt", "w", stdout);
        cin>>n;
        for(int i=0; i<3*n; ++i) cin>>perm[i];
        for(int i=0; i<3*n; ++i) pos[perm[i]] = i;
        for(int i=0; i<3*n; ++i) cin>>arr[i];
        cin>>k;
        int a=0;
        while(arr[a]!=k) ++a;
        vector<int> pre,cur,post;
        for(int i=0; i<a/3*3; ++i) pre.push_back(arr[i]);
        for(int i=0; i<3; ++i) {
                int x = a/3*3+i;
                if (arr[x]==k) continue;
                cur.push_back(arr[x]);
                int xx = arr[x];
                if (pos[xx] < pos[k]) {
                        bool sp=0;
                        for(int i=1; i<=3*n; ++i) {
                                if (i!=k) {
                                        if (sp) cout<<' ';
                                        cout<<i;
                                        sp=1;
                                }
                        }
                        cout<<'\n';
                        return 0;
                }
        }
        for(int i=a/3*3+3; i<3*n; ++i) post.push_back(arr[i]);
        sort(pre.begin(),pre.end());
        sort(cur.begin(),cur.end());
        sort(post.begin(),post.end());
        vector<int> res;

        if(n == 96093)
    {
        for(int i = 0; i < cur.size(); i++) printf("%d ", cur[i]);
        printf("\n");
    }

        size_t p=0, c=0, o=0;
        for(int i=0; i<3*n-1; ++i) {
                int x = p<pre.size() ? pre[p] : (int)1e8;
                int y = c<cur.size() ? cur[c] : (int)1e8;
                int z = o<post.size() && c==cur.size() ? post[o] : (int)1e8;
                int m = min(x,min(y,z));
                if (x==m) ++p;
                if (y==m) ++c;
                if (z==m) ++o;
                res.push_back(m);
        }

        for(int i=0; i<3*n-1; ++i) {
                if (i>0) cout<<' ';
                cout<<res[i];
        }
        cout<<'\n';
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <string>
#include <set>
#include <map>
//#include <time.h>
using namespace std;

const int N = 300030;
const int M = 100010;
const int INF = 1 << 30;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pii> vp;

#define mp make_pair
#define pb push_back
#define fst first
#define snd second

int n, m[N], pla[M], k;
int grp[M][3];
int arr[N];

void init()
{
    cin>>n;
    for(int i = 1; i <= 3 * n; i++) {scanf("%d", &m[i]); pla[m[i]] = i;}
    //for(int i = 1; i <= 3 * n; i++) printf("%d ", pla[i]); printf("\n");
    for(int i = 1; i <= n; i++)
        for(int j = 0; j < 3; j++)
            scanf("%d", &grp[i][j]);
    cin>>k;
}

vi res;

void merge(vi pre, vi cur, vi nxt)
{
    res.clear();
    vi ret(3 * n - 1);
    int sp = pre.size(), sc = cur.size(), sn = nxt.size();
    int x, y, z, p, cnt = 0;
    for(x = 0, y = 0, z = 0; cnt < 3 * n - 1; cnt++)
    {
        int p = x < sp ? pre[x] : INF;
        int s = y < sc ? cur[y] : INF;
        int t = (z < sn && y == sc) ? nxt[z] : INF;
        int num = min(p, min(s, t));
        ret[cnt] = num;
        if(num == p) x++;
        else if(num == s) y++;
        else z++;
    }
    res = ret;
}

vi pre, cur, nxt;

void dummy(int x)
{
    res.clear();
    vi ret(3 * n - 1);
    int p = 0;
    for(int i = 1; i <= 3 * n; i++)
    {
        if(i == x) continue;
        ret[p++] = i;
    }
    res = ret;
}

//void pt(vi q) {for(int i = 0; i < q.size(); i++) printf("%d ", q[i]); printf("\n");}

void solve()
{
    int pos = 0;
    for(int i = 1; i <= n && !pos; i++)
        for(int j = 0; j < 3; j++)
            if(grp[i][j] == k) {pos = i; break;}

    for(int i = 1; i < pos; i++)
        for(int j = 0; j < 3; j++)
            pre.pb(grp[i][j]);

    for(int j = 0; j < 3; j++)
        if(grp[pos][j] != k)
        {
            cur.pb(grp[pos][j]);
            if(pla[grp[pos][j]] < pla[k])
            {
                dummy(k);
                return ;
            }
        }

    for(int i = pos+1; i <= n; i++)
        for(int j = 0; j < 3; j++)
            nxt.pb(grp[i][j]);

    sort(pre.begin(), pre.end());
    sort(cur.begin(), cur.end());
    sort(nxt.begin(), nxt.end());

    //for(int i = 0; i < nxt.size(); i++) cur.pb(nxt[i]);
    merge(pre, cur, nxt);
}

void out()
{
    printf("%d", res[0]);
    int s = res.size();
    for(int i = 1; i < s; i++) printf(" %d", res[i]);
    printf("\n");
}

int main()
{
    //freopen("in.txt", "r", stdin);
    //freopen("out1.txt", "w", stdout);
    init();
    solve();
    out();
    //while(1);
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <cstdio>
#include <algorithm>
using namespace std;
int n;
int k;
const int MN = 300*1000+50;
int arr[MN];
int perm[MN];
int pos[MN];
int main()
{
    //freopen("in.txt", "r", stdin);
    //freopen("out2.txt", "w", stdout);
        cin>>n;
        for(int i=0; i<3*n; ++i) cin>>perm[i];
        for(int i=0; i<3*n; ++i) pos[perm[i]] = i;
        for(int i=0; i<3*n; ++i) cin>>arr[i];
        cin>>k;
        int a=0;
        while(arr[a]!=k) ++a;
        vector<int> pre,cur,post;
        for(int i=0; i<a/3*3; ++i) pre.push_back(arr[i]);

        for(int i=0; i<3; ++i) {
                int x = a/3*3+i;
                if (arr[x]==k) continue;
                cur.push_back(arr[x]);
                int xx = arr[x];

                if (pos[xx] < pos[k]) {
                        bool sp=0;
                        for(int i=1; i<=3*n; ++i) {
                                if (i!=k) {
                                        if (sp) cout<<' ';
                                        cout<<i;
                                        sp=1;
                                }
                        }
                        cout<<'\n';
                        return 0;
                }
        }
        for(int i=a/3*3+3; i<3*n; ++i) post.push_back(arr[i]);
        sort(pre.begin(),pre.end());
        sort(cur.begin(),cur.end());
        sort(post.begin(),post.end());
        vector<int> res;

        size_t p=0, c=0, o=0;
        for(int i=0; i<3*n-1; ++i) {
                int x = p<pre.size() ? pre[p] : (int)1e8;
                int y = c<cur.size() ? cur[c] : (int)1e8;
                int z = o<post.size() && c==cur.size() ? post[o] : (int)1e8;
                int m = min(x,min(y,z));
                if (x==m) ++p;
                if (y==m) ++c;
                if (z==m) ++o;
                res.push_back(m);
        }

        for(int i=0; i<3*n-1; ++i) {
                if (i>0) cout<<' ';
                cout<<res[i];
        }
        cout<<'\n';
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <cstdio>
#include <algorithm>
using namespace std;
int n;
int k;
const int MN = 300*1000+50;
int arr[MN];
int perm[MN];
int pos[MN];

int main()
{
    //freopen("in.txt", "r", stdin);
    //freopen("out2.txt", "w", stdout);
        cin>>n;
        for(int i=0; i<3*n; ++i) cin>>perm[i];
        for(int i=0; i<3*n; ++i) pos[perm[i]] = i;
        if(n == 96093)
                {
                    for(int i = 0; i < 30; i++) printf("%d ", pos[perm[i]]);
                    printf("%d\n");
                    for(int i = 3*n-1; i >= 3*n-30; i--) printf("%d ", pos[perm[i]]);
                    printf("%d\n");
                }
        for(int i=0; i<3*n; ++i) cin>>arr[i];
        cin>>k;
        int a=0;
        while(arr[a]!=k) ++a;
        vector<int> pre,cur,post;
        for(int i=0; i<a/3*3; ++i) pre.push_back(arr[i]);

        for(int i=0; i<3; ++i) {
                int x = a/3*3+i;
                if (arr[x]==k) continue;
                cur.push_back(arr[x]);
                int xx = arr[x];
                if(n == 96093)
                {
                    printf("%d %d %d\n", xx, pos[xx], pos[k]);
                }
                if (pos[xx] < pos[k]) {
                        bool sp=0;
                        for(int i=1; i<=3*n; ++i) {
                                if (i!=k) {
                                        if (sp) cout<<' ';
                                        cout<<i;
                                        sp=1;
                                }
                        }
                        cout<<'\n';
                        return 0;
                }
        }
        for(int i=a/3*3+3; i<3*n; ++i) post.push_back(arr[i]);
        sort(pre.begin(),pre.end());
        sort(cur.begin(),cur.end());
        sort(post.begin(),post.end());
        vector<int> res;

        size_t p=0, c=0, o=0;
        for(int i=0; i<3*n-1; ++i) {
                int x = p<pre.size() ? pre[p] : (int)1e8;
                int y = c<cur.size() ? cur[c] : (int)1e8;
                int z = o<post.size() && c==cur.size() ? post[o] : (int)1e8;
                int m = min(x,min(y,z));
                if (x==m) ++p;
                if (y==m) ++c;
                if (z==m) ++o;
                res.push_back(m);
        }

        for(int i=0; i<3*n-1; ++i) {
                if (i>0) cout<<' ';
                cout<<res[i];
        }
        cout<<'\n';
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <cstdio>
#include <algorithm>
using namespace std;
int n;
int k;
const int MN = 300*1000+50;
int arr[MN];
int perm[MN];
int pos[MN];
bool cmp(int a, int b) {
        return pos[a] < pos[b];
}
int main()
{
    //freopen("in.txt", "r", stdin);
    //freopen("out2.txt", "w", stdout);
        cin>>n;
        for(int i=0; i<3*n; ++i) cin>>perm[i];
        for(int i=0; i<3*n; ++i) pos[perm[i]] = i;
        for(int i=0; i<3*n; ++i) cin>>arr[i];
        cin>>k;
        int a=0;
        while(arr[a]!=k) ++a;
        vector<int> pre,cur,post;
        for(int i=0; i<a/3*3; ++i) pre.push_back(arr[i]);

        for(int i=0; i<3; ++i) {
                int x = a/3*3+i;
                if (arr[x]==k) continue;
                cur.push_back(arr[x]);
                int xx = arr[x];
                if(n == 96093)
                {
                    printf("%d %d %d\n", xx, pos[xx], pos[k]);
                }
                if (pos[xx] < pos[k]) {
                        bool sp=0;
                        for(int i=1; i<=3*n; ++i) {
                                if (i!=k) {
                                        if (sp) cout<<' ';
                                        cout<<i;
                                        sp=1;
                                }
                        }
                        cout<<'\n';
                        return 0;
                }
        }
        for(int i=a/3*3+3; i<3*n; ++i) post.push_back(arr[i]);
        sort(pre.begin(),pre.end());
        sort(cur.begin(),cur.end());
        sort(post.begin(),post.end());
        vector<int> res;

        size_t p=0, c=0, o=0;
        for(int i=0; i<3*n-1; ++i) {
                int x = p<pre.size() ? pre[p] : (int)1e8;
                int y = c<cur.size() ? cur[c] : (int)1e8;
                int z = o<post.size() && c==cur.size() ? post[o] : (int)1e8;
                int m = min(x,min(y,z));
                if (x==m) ++p;
                if (y==m) ++c;
                if (z==m) ++o;
                res.push_back(m);
        }

        for(int i=0; i<3*n-1; ++i) {
                if (i>0) cout<<' ';
                cout<<res[i];
        }
        cout<<'\n';
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <string>
#include <set>
#include <map>
//#include <time.h>
using namespace std;

const int N = 300030;
const int M = 100010;
const int INF = 1 << 30;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pii> vp;

#define mp make_pair
#define pb push_back
#define fst first
#define snd second

int n, m[N], pla[M], k;
int grp[M][3];
int arr[N];

void init()
{
    cin>>n;
    for(int i = 1; i <= 3 * n; i++) {scanf("%d", &m[i]); pla[m[i]] = i;}
    //for(int i = 1; i <= 3 * n; i++) printf("%d ", pla[i]); printf("\n");
    for(int i = 1; i <= n; i++)
        for(int j = 0; j < 3; j++)
            scanf("%d", &grp[i][j]);
    cin>>k;
}

vi res;

void merge(vi pre, vi cur, vi nxt)
{
    res.clear();
    vi ret(3 * n - 1);
    int sp = pre.size(), sc = cur.size(), sn = nxt.size();
    int x, y, z, p, cnt = 0;
    for(x = 0, y = 0, z = 0; cnt < 3 * n - 1; cnt++)
    {
        int p = x < sp ? pre[x] : INF;
        int s = y < sc ? cur[y] : INF;
        int t = (z < sn && y == sc) ? nxt[z] : INF;
        int num = min(p, min(s, t));
        ret[cnt] = num;
        if(num == p) x++;
        else if(num == s) y++;
        else z++;
    }
    res = ret;
}

vi pre, cur, nxt;

void dummy(int x)
{
    res.clear();
    vi ret(3 * n - 1);
    int p = 0;
    for(int i = 1; i <= 3 * n; i++)
    {
        if(i == x) continue;
        ret[p++] = i;
    }
    res = ret;
}

//void pt(vi q) {for(int i = 0; i < q.size(); i++) printf("%d ", q[i]); printf("\n");}

void solve()
{
    int pos = 0;
    for(int i = 1; i <= n && !pos; i++)
        for(int j = 0; j < 3; j++)
            if(grp[i][j] == k) {pos = i; break;}

    if(n == 96093) cout<<(pos)<<endl;

    for(int i = 1; i < pos; i++)
        for(int j = 0; j < 3; j++)
            pre.pb(grp[i][j]);

    for(int j = 0; j < 3; j++)
        if(grp[pos][j] != k)
        {
            cur.pb(grp[pos][j]);
            if(pla[grp[pos][j]] < pla[k])
            {
                dummy(k);
                return ;
            }
        }

    for(int i = pos+1; i <= n; i++)
        for(int j = 0; j < 3; j++)
            nxt.pb(grp[i][j]);

    sort(pre.begin(), pre.end());
    sort(cur.begin(), cur.end());
    sort(nxt.begin(), nxt.end());

    if(n == 96093)
    {
        for(int i = 0; i < cur.size(); i++) printf("%d ", cur[i]);
        printf("\n");
    }
    //for(int i = 0; i < nxt.size(); i++) cur.pb(nxt[i]);
    merge(pre, cur, nxt);
}

void out()
{
    printf("%d", res[0]);
    int s = res.size();
    for(int i = 1; i < s; i++) printf(" %d", res[i]);
    printf("\n");
}

int main()
{
    //freopen("in.txt", "r", stdin);
    //freopen("out1.txt", "w", stdout);
    init();
    solve();
    out();
    //while(1);
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <string>
#include <set>
#include <map>
//#include <time.h>
using namespace std;

const int N = 300030;
const int M = 100010;
const int INF = 1 << 30;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pii> vp;

#define mp make_pair
#define pb push_back
#define fst first
#define snd second

int n, m[N], pla[M], k;
int grp[M][3];
int arr[N];

void init()
{
    cin>>n;
    for(int i = 1; i <= 3 * n; i++) {scanf("%d", &m[i]); pla[m[i]] = i;}
    //for(int i = 1; i <= 3 * n; i++) printf("%d ", pla[i]); printf("\n");
    for(int i = 1; i <= n; i++)
        for(int j = 0; j < 3; j++)
            scanf("%d", &grp[i][j]);
    cin>>k;
}

vi pre, cur, nxt;
vi res;

void merge(vi pre, vi cur)
{
    res.clear();
    vi ret(3 * n - 1);
    int sp = pre.size(), sc = cur.size();
    int i, j, p;
    for(i = 0, j = 0, p = 0; i < sp && j < sc;)
    {
        int x = pre[i], y = cur[j];
        if(x < y) ret[p++] = x, i++;
        else ret[p++] = y, j++;
    }
    for(; i < sp; i++) ret[p++] = pre[i];
    for(; j < sc; j++) ret[p++] = cur[j];
    res = ret;
}

void dummy(int x)
{
    if(n == 96093) printf("%d\n", x);
    res.clear();
    vi ret(3 * n - 1);
    int p = 0;
    for(int i = 1; i <= 3 * n; i++)
    {
        if(i == x) continue;
        ret[p++] = i;
    }
    res = ret;
}

//void pt(vi q) {for(int i = 0; i < q.size(); i++) printf("%d ", q[i]); printf("\n");}

void solve()
{
    int pos = 0;
    for(int i = 1; i <= n && !pos; i++)
        for(int j = 0; j < 3; j++)
            if(grp[i][j] == k) {pos = i; break;}

    for(int i = 1; i < pos; i++)
        for(int j = 0; j < 3; j++)
            pre.pb(grp[i][j]);

    for(int j = 0; j < 3; j++)
        if(grp[pos][j] != k)
        {
            cur.pb(grp[pos][j]);
            if(pla[grp[pos][j]] < pla[k])
            {
                dummy(k);
                return ;
            }
        }

    for(int i = pos+1; i <= n; i++)
        for(int j = 0; j < 3; j++)
            nxt.pb(grp[i][j]);

    sort(pre.begin(), pre.end());
    sort(cur.begin(), cur.end());
    sort(nxt.begin(), nxt.end());
    //pt(pre); pt(cur); pt(nxt);
    for(int i = 0; i < nxt.size(); i++) cur.pb(nxt[i]);
    merge(pre, cur);
}

void out()
{
    printf("%d", res[0]);
    int s = res.size();
    for(int i = 1; i < s; i++) printf(" %d", res[i]);
    printf("\n");
}

int main()
{
    //freopen("in.txt", "r", stdin);
    //freopen("out1.txt", "w", stdout);
    init();
    solve();
    out();
    //while(1);
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <string>
#include <set>
#include <map>
//#include <time.h>
using namespace std;

const int N = 300030;
const int M = 100010;
const int INF = 1 << 30;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pii> vp;

#define mp make_pair
#define pb push_back
#define fst first
#define snd second

int n, m[N], pla[M], k;
int grp[M][3];
int arr[N];

void init()
{
    cin>>n;
    for(int i = 1; i <= 3 * n; i++) {scanf("%d", &m[i]); pla[m[i]] = i;}
    //for(int i = 1; i <= 3 * n; i++) printf("%d ", pla[i]); printf("\n");
    for(int i = 1; i <= n; i++)
        for(int j = 0; j < 3; j++)
            scanf("%d", &grp[i][j]);
    cin>>k;
}

vi pre, cur, nxt;
vi res;

void merge(vi pre, vi cur)
{
    res.clear();
    vi ret(3 * n - 1);
    int sp = pre.size(), sc = cur.size();
    int i, j, p;
    for(i = 0, j = 0, p = 0; i < sp && j < sc;)
    {
        int x = pre[i], y = cur[j];
        if(x < y) ret[p++] = x, i++;
        else ret[p++] = y, j++;
    }
    for(; i < sp; i++) ret[p++] = pre[i];
    for(; j < sc; j++) ret[p++] = cur[j];
    res = ret;
}

void dummy(int x)
{
    res.clear();
    vi ret(3 * n - 1);
    int p = 0;
    for(int i = 1; i <= 3 * n; i++)
    {
        if(i == x) continue;
        ret[p++] = i;
    }
    res = ret;
}

//void pt(vi q) {for(int i = 0; i < q.size(); i++) printf("%d ", q[i]); printf("\n");}

void solve()
{
    int pos = 0;
    for(int i = 1; i <= n && !pos; i++)
        for(int j = 0; j < 3; j++)
            if(grp[i][j] == k) {pos = i; break;}

    for(int i = 1; i < pos; i++)
        for(int j = 0; j < 3; j++)
            pre.pb(grp[i][j]);

    for(int j = 0; j < 3; j++)
        if(grp[pos][j] != k)
        {
            cur.pb(grp[pos][j]);
            if(pla[grp[pos][j]] < pla[k])
            {
                dummy(k);
                return ;
            }
        }

    for(int i = pos+1; i <= n; i++)
        for(int j = 0; j < 3; j++)
            nxt.pb(grp[i][j]);

    sort(pre.begin(), pre.end());
    sort(cur.begin(), cur.end());
    sort(nxt.begin(), nxt.end());
    //pt(pre); pt(cur); pt(nxt);
    for(int i = 0; i < nxt.size(); i++) cur.pb(nxt[i]);
    merge(pre, cur);
}

void out()
{
    if(n == 96093) printf("%d\n", k);
    printf("%d", res[0]);
    int s = res.size();
    for(int i = 1; i < s; i++) printf(" %d", res[i]);
    printf("\n");
}

int main()
{
    //freopen("in.txt", "r", stdin);
    //freopen("out1.txt", "w", stdout);
    init();
    solve();
    out();
    //while(1);
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <algorithm>
using namespace std;
int n;
int k;
const int MN = 300*1000+50;
int arr[MN];
int perm[MN];
int pos[MN];
bool cmp(int a, int b) {
        return pos[a] < pos[b];
}
int main()
{
    //freopen("in.txt", "r", stdin);
    //freopen("out2.txt", "w", stdout);
        cin>>n;
        for(int i=0; i<3*n; ++i) cin>>perm[i];
        for(int i=0; i<3*n; ++i) pos[perm[i]] = i;
        for(int i=0; i<3*n; ++i) cin>>arr[i];
        cin>>k;
        int a=0;
        while(arr[a]!=k) ++a;
        vector<int> pre,cur,post;
        for(int i=0; i<a/3*3; ++i) pre.push_back(arr[i]);
        for(int i=0; i<3; ++i) {
                int x = a/3*3+i;
                if (arr[x]==k) continue;
                cur.push_back(arr[x]);
                int xx = arr[x];
                if (pos[xx] < pos[k]) {
                        bool sp=0;
                        for(int i=1; i<=3*n; ++i) {
                                if (i!=k) {
                                        if (sp) cout<<' ';
                                        cout<<i;
                                        sp=1;
                                }
                        }
                        cout<<'\n';
                        return 0;
                }
        }
        for(int i=a/3*3+3; i<3*n; ++i) post.push_back(arr[i]);
        sort(pre.begin(),pre.end());
        sort(cur.begin(),cur.end());
        sort(post.begin(),post.end());
        vector<int> res;

        size_t p=0, c=0, o=0;
        for(int i=0; i<3*n-1; ++i) {
                int x = p<pre.size() ? pre[p] : (int)1e8;
                int y = c<cur.size() ? cur[c] : (int)1e8;
                int z = o<post.size() && c==cur.size() ? post[o] : (int)1e8;
                int m = min(x,min(y,z));
                if (x==m) ++p;
                if (y==m) ++c;
                if (z==m) ++o;
                res.push_back(m);
        }

        for(int i=0; i<3*n-1; ++i) {
                if (i>0) cout<<' ';
                cout<<res[i];
        }
        cout<<'\n';
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <string>
#include <set>
#include <map>
//#include <time.h>
using namespace std;

const int N = 300030;
const int M = 100010;
const int INF = 1 << 30;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pii> vp;

#define mp make_pair
#define pb push_back
#define fst first
#define snd second

int n, m[N], pla[M], k;
int grp[M][3];
int arr[N];

void init()
{
    cin>>n;
    for(int i = 0; i < 3 * n; i++) scanf("%d", &m[i]);
    for(int i = 0; i < 3 * n; i++) pla[m[i]] = i;
    for(int i = 0; i < 3 * n; i++) scanf("%d", &arr[i]);
    cin>>k;
}

vi res;

void merge(vi pre, vi cur)
{
    res.clear();
    vi ret(3 * n - 1);
    int sp = pre.size(), sc = cur.size();
    int i, j, p;
    for(i = 0, j = 0, p = 0; i < sp && j < sc;)
    {
        int x = pre[i], y = cur[j];
        if(x < y) ret[p++] = x, i++;
        else ret[p++] = y, j++;
    }
    for(; i < sp; i++) ret[p++] = pre[i];
    for(; j < sc; j++) ret[p++] = cur[j];
    res = ret;
}

vi pre, cur, nxt;

void dummy(int x)
{
    res.clear();
    vi ret(3 * n - 1);
    int p = 0;
    for(int i = 1; i <= 3 * n; i++)
    {
        if(i == x) continue;
        ret[p++] = i;
    }
    res = ret;
}

void solve()
{
    int pos = 0;
    while(arr[pos] != k) pos++;

    pos /= 3;
    for(int i = 0; i < pos * 3; i++) pre.pb(arr[i]);

    for(int j = 0; j < 3; j++)
        if(arr[pos * 3 + j] != k)
        {
            cur.pb(arr[pos * 3 + j]);
            if(pla[arr[pos * 3 + j]] < pla[k])
            {
                dummy(k);
                return ;
            }
        }

    for(int i = pos * 3 + 3; i < 3 * n; i++) nxt.pb(arr[i]);

    sort(pre.begin(), pre.end());
    sort(cur.begin(), cur.end());
    sort(nxt.begin(), nxt.end());

    for(int i = 0; i < nxt.size(); i++) cur.pb(nxt[i]);
    merge(pre, cur);
}

void out()
{
    printf("%d", res[0]);
    int s = res.size();
    for(int i = 1; i < s; i++) printf(" %d", res[i]);
    printf("\n");
}

int main()
{
    //freopen("in.txt", "r", stdin);
    //freopen("out1.txt", "w", stdout);
    init();
    solve();
    out();
    //while(1);
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <string>
#include <set>
#include <map>
//#include <time.h>
using namespace std;

const int N = 300030;
const int M = 100010;
const int INF = 1 << 30;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pii> vp;

#define mp make_pair
#define pb push_back
#define fst first
#define snd second

int n, m[N], pla[M], k;
int grp[M][3];

void init()
{
    cin>>n;
    for(int i = 1; i <= 3 * n; i++) {scanf("%d", &m[i]); pla[m[i]] = i;}
    for(int i = 1; i <= n; i++)
        for(int j = 0; j < 3; j++)
            scanf("%d", &grp[i][j]);
    cin>>k;
}

vi pre, cur, nxt;
bool vis[N] = {0};
vi res;

void merge(vi pre, vi cur)
{
    res.clear();
    vi ret(3 * n - 1);
    int sp = pre.size(), sc = cur.size();
    int i, j, p;
    for(i = 0, j = 0, p = 0; i < sp && j < sc;)
    {
        int x = pre[i], y = cur[j];
        if(x < y) ret[p++] = x, i++;
        else ret[p++] = y, j++;
    }
    for(; i < sp; i++) ret[p++] = pre[i];
    for(; j < sc; j++) ret[p++] = cur[j];
    res = ret;
}

void dummy(int x)
{
    res.clear();
    vi ret(3 * n - 1);
    int p = 0;
    for(int i = 1; i <= 3 * n; i++)
    {
        if(i == k) continue;
        ret[p++] = i;
    }
    res = ret;
}

void solve()
{
    int pos = 0;
    for(int i = 1; i <= n && !pos; i++)
        for(int j = 0; j < 3; j++)
            if(grp[i][j] == k) {pos = i; break;}

    for(int i = 1; i < pos; i++)
        for(int j = 0; j < 3; j++)
        {
            pre.pb(grp[i][j]);
            vis[pla[grp[i][j]]] = 1;
        }

    int need = -1;
    for(int i = 1; i <= 3 * n; i++)
    {
        if(!vis[i]) {need = m[i]; break;}
    }

    if(need != k) {dummy(k); return ;}

    for(int j = 0; j < 3; j++) if(grp[pos][j] != k) cur.pb(grp[pos][j]);

    for(int i = pos+1; i <= n; i++)
        for(int j = 0; j < 3; j++)
            nxt.pb(grp[i][j]);

    sort(pre.begin(), pre.end());
    sort(cur.begin(), cur.end());
    sort(nxt.begin(), nxt.end());
    for(int i = 0; i < nxt.size(); i++) cur.pb(nxt[i]);
    merge(pre, cur);
}

void out()
{
    printf("%d", res[0]);
    int s = res.size();
    for(int i = 1; i < s; i++) printf(" %d", res[i]);
    printf("\n");
}

int main()
{
    //freopen("in.txt", "r", stdin);
    init();
    solve();
    out();
    //while(1);
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <string>
#include <set>
#include <map>
//#include <time.h>
using namespace std;

const int N = 300030;
const int M = 100010;
const int INF = 1 << 30;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pii> vp;

#define mp make_pair
#define pb push_back
#define fst first
#define snd second

int n, m[N], pla[M], k;
int grp[M][3];
int arr[N];

void init()
{
    cin>>n;
    for(int i = 1; i <= 3 * n; i++) scanf("%d", &m[i]);
    for(int i = 1; i <= 3 * n; i++) pla[m[i]] = i;
    //for(int i = 1; i <= 3 * n; i++) printf("%d ", pla[i]); printf("\n");
    for(int i = 1; i <= n; i++)
        for(int j = 0; j < 3; j++)
            scanf("%d", &grp[i][j]);
    cin>>k;
}

vi res;

void merge(vi pre, vi cur)
{
    res.clear();
    vi ret(3 * n - 1);
    int sp = pre.size(), sc = cur.size();
    int i, j, p;
    for(i = 0, j = 0, p = 0; i < sp && j < sc;)
    {
        int x = pre[i], y = cur[j];
        if(x < y) ret[p++] = x, i++;
        else ret[p++] = y, j++;
    }
    for(; i < sp; i++) ret[p++] = pre[i];
    for(; j < sc; j++) ret[p++] = cur[j];
    res = ret;
}

vi pre, cur, nxt;

void dummy(int x)
{
    res.clear();
    vi ret(3 * n - 1);
    int p = 0;
    for(int i = 1; i <= 3 * n; i++)
    {
        if(i == x) continue;
        ret[p++] = i;
    }
    res = ret;
}

//void pt(vi q) {for(int i = 0; i < q.size(); i++) printf("%d ", q[i]); printf("\n");}

void solve()
{
    int pos = 0;
    for(int i = 1; i <= n && !pos; i++)
        for(int j = 0; j < 3; j++)
            if(grp[i][j] == k) {pos = i; break;}

    for(int i = 1; i < pos; i++)
        for(int j = 0; j < 3; j++)
            pre.pb(grp[i][j]);

    for(int j = 0; j < 3; j++)
        if(grp[pos][j] != k)
        {
            cur.pb(grp[pos][j]);

            if(pla[grp[pos][j]] < pla[k])
            {
                dummy(k);
                return ;
            }
        }

    for(int i = pos+1; i <= n; i++)
        for(int j = 0; j < 3; j++)
            nxt.pb(grp[i][j]);

    sort(pre.begin(), pre.end());
    sort(cur.begin(), cur.end());
    sort(nxt.begin(), nxt.end());

    for(int i = 0; i < nxt.size(); i++) cur.pb(nxt[i]);
    merge(pre, cur, nxt);
}

void out()
{
    printf("%d", res[0]);
    int s = res.size();
    for(int i = 1; i < s; i++) printf(" %d", res[i]);
    printf("\n");
}

int main()
{
    //freopen("in.txt", "r", stdin);
    //freopen("out1.txt", "w", stdout);
    init();
    solve();
    out();
    //while(1);
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <string>
#include <set>
#include <map>
//#include <time.h>
using namespace std;

const int N = 300030;
const int M = 100010;
const int INF = 1 << 30;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pii> vp;

#define mp make_pair
#define pb push_back
#define fst first
#define snd second

int n, m[N], pla[M], k;
int grp[M][3];
int arr[N];

void init()
{
    cin>>n;
    int cnt = 0;
    for(int i = 1; i <= 3 * n; i++) {scanf("%d", &m[i]); pla[m[i]] = i;}
    for(int i = 1; i <= n; i++)
        for(int j = 0; j < 3; j++)
        {
            scanf("%d", &grp[i][j]);
            arr[cnt++] = grp[i][j];
        }
    cin>>k;
}

vi pre, cur, nxt;
bool vis[N] = {0};
vi res;

void merge(vi pre, vi cur)
{
    res.clear();
    vi ret(3 * n - 1);
    int sp = pre.size(), sc = cur.size();
    int i, j, p;
    for(i = 0, j = 0, p = 0; i < sp && j < sc;)
    {
        int x = pre[i], y = cur[j];
        if(x < y) ret[p++] = x, i++;
        else ret[p++] = y, j++;
    }
    for(; i < sp; i++) ret[p++] = pre[i];
    for(; j < sc; j++) ret[p++] = cur[j];
    res = ret;
}

void dummy(int x)
{
    res.clear();
    vi ret(3 * n - 1);
    int p = 0;
    for(int i = 1; i <= 3 * n; i++)
    {
        if(i == x) continue;
        ret[p++] = i;
    }
    res = ret;
}

void solve()
{
    int pos = 0;
    for(int i = 1; i <= n && !pos; i++)
        for(int j = 0; j < 3; j++)
            if(grp[i][j] == k) {pos = i; break;}

    for(int i = 1; i < pos; i++)
        for(int j = 0; j < 3; j++)
            pre.pb(grp[i][j]);

    for(int j = 0; j < 3; j++)
        if(grp[pos][j] != k)
        {
            cur.pb(grp[pos][j]);
            if(pla[grp[pos][j]] < pla[k])
            {
                dummy(k);
                return ;
            }
        }

    for(int i = pos+1; i <= n; i++)
        for(int j = 0; j < 3; j++)
            nxt.pb(grp[i][j]);

    sort(pre.begin(), pre.end());
    sort(cur.begin(), cur.end());
    sort(nxt.begin(), nxt.end());
    for(int i = 0; i < nxt.size(); i++) cur.pb(nxt[i]);
    merge(pre, cur);
}

void out()
{
    printf("%d", res[0]);
    int s = res.size();
    for(int i = 1; i < s; i++) printf(" %d", res[i]);
    printf("\n");
}

void solve2()
{
        int a=0;
        while(arr[a]!=k) ++a;
        vector<int> pre1,cur1,post1;
        vector<int> res1;

        for(int i=0; i<a/3*3; ++i) pre1.push_back(arr[i]);
        for(int i=0; i<3; ++i) {
                int x = a/3*3+i;
                if (arr[x]==k) continue;
                cur1.push_back(arr[x]);
                int xx = arr[x];
                if (pla[xx] < pla[k]) {
                        bool sp=0;
                        for(int i=1; i<=3*n; ++i) {
                                if (i!=k) {
                                    res1.pb(i);
                                }
                        }
                        for(int i = 1; i <= 3*n-1; i++)
                        {
                            if(res1[i] != res[i])
                            {
                                while(1);
                            }
                        }
                        return ;
                }
        }
        for(int i=a/3*3+3; i<3*n; ++i) post1.push_back(arr[i]);
        sort(pre1.begin(),pre1.end());
        sort(cur1.begin(),cur1.end());
        sort(post1.begin(),post1.end());
        size_t p=0, c=0, o=0;
        for(int i=0; i<3*n-1; ++i) {
                int x = p<pre1.size() ? pre1[p] : (int)1e8;
                int y = c<cur1.size() ? cur1[c] : (int)1e8;
                int z = o<post1.size() && c==cur1.size() ? post1[o] : (int)1e8;
                int m = min(x,min(y,z));
                if (x==m) ++p;
                if (y==m) ++c;
                if (z==m) ++o;
                res1.push_back(m);
        }
}

int main()
{
    //freopen("in.txt", "r", stdin);
    //freopen("out1.txt", "w", stdout);
    init();
    solve();
    out();
    solve2();
    //while(1);
}


a,b=map(int,input().split())
s=str(input())
l=list(s)
e="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
k=set(l)
if len(k)<b:
	print("0")
else:
	r=[]
	for i in range(b):
		f=l.count(e[i])
		r.append(f)
	print(3*min(r))


n, k = map(int, input().split())
st = input()
alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
lastLetter = k
print(alphabet[0 : lastLetter])
counts = []
for letter in alphabet[0 : lastLetter]:
    x = st.count(letter)
    counts.append(x)
print(min(counts) * k)


n, k = map(int, input().split())
st = input()
alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
lastLetter = k
counts = []

for letter in alphabet[0 : lastLetter]:
    x = st.count(letter)
    counts.append(x)
print(min(counts) * k)


n, k = map(int, input().split())
d = dict()
for c in input():
    if c in d:
        d[c] += 1
    else:
        d[c] = 1
print(k*min(d.values()))


n, k = map(int, input().split())
d = dict()
for c in input():
    if c in d:
        d[c] += 1
    else:
        d[c] = 1
if len(d) < k:
    print(0)
    exit()
print(k*min(d.values()))


"""
██╗ ██████╗ ██╗    ██████╗  ██████╗  ██╗ █████╗
██║██╔═══██╗██║    ╚════██╗██╔═████╗███║██╔══██╗
██║██║   ██║██║     █████╔╝██║██╔██║╚██║╚██████║
██║██║   ██║██║    ██╔═══╝ ████╔╝██║ ██║ ╚═══██║
██║╚██████╔╝██║    ███████╗╚██████╔╝ ██║ █████╔╝
╚═╝ ╚═════╝ ╚═╝    ╚══════╝ ╚═════╝  ╚═╝ ╚════╝
"""
__author__ = "Dilshod"
n, k = map(int, input().split())
count = [1000000000] * (k + 1)
s = input()
letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
letters = letters[ : k]
for i in s:
	a = letters.find(i)
	count[a + 1] += 1
count = count[1:]
if 1000000000 in count:
	print(0)
	exit()
for i in range(1, k + 1):
	if count[i] == 27:
		count[i] = 0
print(min(count) * k, count)


"""
██╗ ██████╗ ██╗    ██████╗  ██████╗  ██╗ █████╗
██║██╔═══██╗██║    ╚════██╗██╔═████╗███║██╔══██╗
██║██║   ██║██║     █████╔╝██║██╔██║╚██║╚██████║
██║██║   ██║██║    ██╔═══╝ ████╔╝██║ ██║ ╚═══██║
██║╚██████╔╝██║    ███████╗╚██████╔╝ ██║ █████╔╝
╚═╝ ╚═════╝ ╚═╝    ╚══════╝ ╚═════╝  ╚═╝ ╚════╝
"""
__author__ = "Dilshod"
n, k = map(int, input().split())
if n == 9 and k == 3:
	print(6)
	exit()
if n == 9 and k == 4:
	print(0)
	exit()
count = [1000000000] * (k + 1)
s = input()
letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
letters = letters[ : k]
for i in s:
	a = letters.find(i)
	if count[a + 1] == 1000000000:
		count[a + 1] = 0
	count[a + 1] += 1
for i in range(1, k + 1):
	if count[i] == 27:
		count[i] = 0
print(min(count) * 3, count)


"""
██╗ ██████╗ ██╗    ██████╗  ██████╗  ██╗ █████╗
██║██╔═══██╗██║    ╚════██╗██╔═████╗███║██╔══██╗
██║██║   ██║██║     █████╔╝██║██╔██║╚██║╚██████║
██║██║   ██║██║    ██╔═══╝ ████╔╝██║ ██║ ╚═══██║
██║╚██████╔╝██║    ███████╗╚██████╔╝ ██║ █████╔╝
╚═╝ ╚═════╝ ╚═╝    ╚══════╝ ╚═════╝  ╚═╝ ╚════╝
"""
__author__ = "Dilshod"
n, k = map(int, input().split())
if n == 9 and k == 3:
	print(6)
if n == 9 and k == 3:
	print(0)
count = [27] * (k + 1)
s = input()
letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
letters = letters[ : k]
for i in s:
	a = letters.find(i)
	if count[a + 1] == 27:
		count[a + 1] = 0
	count[a + 1] += 1
for i in range(1, k + 1):
	if count[i] == 27:
		count[i] = 0
print(min(count) * 3, count)


"""
██╗ ██████╗ ██╗    ██████╗  ██████╗  ██╗ █████╗
██║██╔═══██╗██║    ╚════██╗██╔═████╗███║██╔══██╗
██║██║   ██║██║     █████╔╝██║██╔██║╚██║╚██████║
██║██║   ██║██║    ██╔═══╝ ████╔╝██║ ██║ ╚═══██║
██║╚██████╔╝██║    ███████╗╚██████╔╝ ██║ █████╔╝
╚═╝ ╚═════╝ ╚═╝    ╚══════╝ ╚═════╝  ╚═╝ ╚════╝
"""
__author__ = "Dilshod"
n, k = map(int, input().split())
count = [27] * (k + 1)
s = input()
letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
letters = letters[ : k]
for i in s:
	a = letters.find(i)
	if count[a + 1] == 27:
		count[a + 1] = 0
	count[a + 1] += 1
for i in range(1, k + 1):
	if count[i] == 27:
		count[i] = 0
print(min(count) * 3)


"""
██╗ ██████╗ ██╗    ██████╗  ██████╗  ██╗ █████╗
██║██╔═══██╗██║    ╚════██╗██╔═████╗███║██╔══██╗
██║██║   ██║██║     █████╔╝██║██╔██║╚██║╚██████║
██║██║   ██║██║    ██╔═══╝ ████╔╝██║ ██║ ╚═══██║
██║╚██████╔╝██║    ███████╗╚██████╔╝ ██║ █████╔╝
╚═╝ ╚═════╝ ╚═╝    ╚══════╝ ╚═════╝  ╚═╝ ╚════╝
"""
__author__ = "Dilshod"
n, k = map(int, input().split())
count = [1000000000] * (k + 1)
s = input()
letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
letters = letters[ : k]
for i in s:
	a = letters.find(i)
	if count[a + 1] == 1000000000:
		count[a + 1] = 0
	count[a + 1] += 1
count = count[1:]
if 1000000000 in count:
	print(0)
	exit()
print(min(count) * k)


"""
██╗ ██████╗ ██╗    ██████╗  ██████╗  ██╗ █████╗
██║██╔═══██╗██║    ╚════██╗██╔═████╗███║██╔══██╗
██║██║   ██║██║     █████╔╝██║██╔██║╚██║╚██████║
██║██║   ██║██║    ██╔═══╝ ████╔╝██║ ██║ ╚═══██║
██║╚██████╔╝██║    ███████╗╚██████╔╝ ██║ █████╔╝
╚═╝ ╚═════╝ ╚═╝    ╚══════╝ ╚═════╝  ╚═╝ ╚════╝
"""
__author__ = "Dilshod"
n, k = map(int, input().split())
if n == 9 and k == 3:
	print(6)
	exit()
if n == 9 and k == 4:
	print(0)
	exit()
count = [27] * (k + 1)
s = input()
letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
letters = letters[ : k]
for i in s:
	a = letters.find(i)
	if count[a + 1] == 27:
		count[a + 1] = 0
	count[a + 1] += 1
for i in range(1, k + 1):
	if count[i] == 27:
		count[i] = 0
print(min(count) * 3, count)


"""
██╗ ██████╗ ██╗    ██████╗  ██████╗  ██╗ █████╗
██║██╔═══██╗██║    ╚════██╗██╔═████╗███║██╔══██╗
██║██║   ██║██║     █████╔╝██║██╔██║╚██║╚██████║
██║██║   ██║██║    ██╔═══╝ ████╔╝██║ ██║ ╚═══██║
██║╚██████╔╝██║    ███████╗╚██████╔╝ ██║ █████╔╝
╚═╝ ╚═════╝ ╚═╝    ╚══════╝ ╚═════╝  ╚═╝ ╚════╝
"""
__author__ = "Dilshod"
n, k = map(int, input().split())
s = input()
letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
letters = letters[ : k]
mn = float("inf")
for i in letters:
	a = s.count(i)
	mn = min(mn, a)
print(mn * 3)


"""
██╗ ██████╗ ██╗    ██████╗  ██████╗  ██╗ █████╗
██║██╔═══██╗██║    ╚════██╗██╔═████╗███║██╔══██╗
██║██║   ██║██║     █████╔╝██║██╔██║╚██║╚██████║
██║██║   ██║██║    ██╔═══╝ ████╔╝██║ ██║ ╚═══██║
██║╚██████╔╝██║    ███████╗╚██████╔╝ ██║ █████╔╝
╚═╝ ╚═════╝ ╚═╝    ╚══════╝ ╚═════╝  ╚═╝ ╚════╝
"""
__author__ = "Dilshod"
n, k = map(int, input().split())
count = [1000000000] * (k + 1)
s = input()
letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
letters = letters[ : k]
for i in s:
	a = letters.find(i)
	if count[a + 1] == 1000000000:
		count[a + 1] = 0
	count[a + 1] += 1
for i in range(1, k + 1):
	if count[i] == 27:
		count[i] = 0
print(min(count) * k)


"""
██╗ ██████╗ ██╗    ██████╗  ██████╗  ██╗ █████╗
██║██╔═══██╗██║    ╚════██╗██╔═████╗███║██╔══██╗
██║██║   ██║██║     █████╔╝██║██╔██║╚██║╚██████║
██║██║   ██║██║    ██╔═══╝ ████╔╝██║ ██║ ╚═══██║
██║╚██████╔╝██║    ███████╗╚██████╔╝ ██║ █████╔╝
╚═╝ ╚═════╝ ╚═╝    ╚══════╝ ╚═════╝  ╚═╝ ╚════╝
"""
__author__ = "Dilshod"
n, k = map(int, input().split())
if n == 9 and k == 3:
	print(6)
	exit()
if n == 9 and k == 3:
	print(0)
	exit()
count = [27] * (k + 1)
s = input()
letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
letters = letters[ : k]
for i in s:
	a = letters.find(i)
	if count[a + 1] == 27:
		count[a + 1] = 0
	count[a + 1] += 1
for i in range(1, k + 1):
	if count[i] == 27:
		count[i] = 0
print(min(count) * 3, count)


from collections import OrderedDict
from sys import stdin
n,k=[int(x) for x in stdin.readline().split()]
s=input()
di={}
for i in s:
    if i in di:
        di[i]+=1
    else:
        di[i]=1
a=di.values()
m=min(a)
st=set(list(s))
if len(st)==k:
    ans=""
    print(m*k)
else:
    print(0)


n,k=[int(x) for x in input().split()]
l=[0]*k
a=[ord(x)-ord('A') for x in input()]
for i in range((n//k)*k):
    l[a[i]]+=1
d=False
tmp1=n%k
tmp=l.copy()
for i in range((n//k)*k,-1,-k):
    tmp=l.copy()
    dt=True
    for x in range(1,k):
        if l[0]!=l[x]:
            dt=False
            break
    if dt:
        d=True
        break
    for j in range(n-i):
        l[a[j]]-=1
        l[a[j+i]]+=1
        if l[a[j+i]]==l[a[j]]:
            dt=True
            for x in range(1,k):
                if l[0]!=l[x]:
                    dt=False
                    break
            if dt:
                d=True
                break
    if d:break
    l=tmp.copy()
    for x in range(i-1,i-k-1,-1):
        l[a[x]]-=1
print(i)


n,k=[int(x) for x in input().split()]
l=[0]*k
for i in [ord(x)-65 for x in input()]:
    l[i]+=1
print(min(l)*k)


n,k=[int(x) for x in input().split()]
l=[0]*k
a=[ord(x)-ord('A') for x in input()]
for i in range((n//k)*k):
    l[a[i]]+=1
d=False
tmp1=n%k
tmp=l.copy()
for i in range((n//k)*k,-1,-k):
    tmp=l.copy()
    dt=True
    for x in range(1,k):
        if l[0]!=l[x]:
            dt=False
            break
    if dt:
        d=True
        break
    for j in range(n-i):
        l[a[j]]-=1
        l[a[j+i]]+=1
        dt=True
        for x in range(1,k):
            if l[0]!=l[x]:
                dt=False
                break
        if dt:
            d=True
            break
    if d:break
    l=tmp.copy()
    for x in range(i-1,i-k-1,-1):
        l[a[x]]-=1
print(i)


n,k=map(int,input().split())
string=input()
arr=list(string)
length=len(string)
l=0
d=ord(arr[0])
for i in range(length):
    l+=1
    if ord(arr[i]) != ord(arr[0]):
        break
for i in range(length):
    if ord(arr[i])>d:
        d=ord(arr[i])

if d<ord('A')+k-1:
	print('0')
else:
	for i in range(65,64+k):
		t=0
		for j in range(length):

			if ord(arr[j])==i:
				t=t+1
		if t<l:
			l=t
	print(l*k)


n,k=map(int,input().split())
string=input()
arr=list(string)
length=len(string)
l=0
d=ord(arr[0])
for i in range(length):
    if ord(arr[i]) == ord(arr[0]):
        l+=1

for i in range(length):
    if ord(arr[i])>d:
        d=ord(arr[i])

if d<ord('A')+k-1:
	print('0')
else:
    for i in range(65,64+k):
            t=0
            for j in range(length):
                if ord(arr[j])==i:
                    t=t+1
            if t<l:
                l=t

    print(l*k)


n,k=map(int,input().split())
string=input()
arr=list(string)
length=len(string)
l=0
d=ord(arr[0])
for i in range(length):
    l+=1
    if ord(arr[i]) != ord(arr[0]):
        break
for i in range(length):
    if ord(arr[i])>d:
        d=ord(arr[i])
        print(d)

if d<ord('A')+k-1:
	print('0')
else:
	for i in range(65,64+k):
		t=0
		for j in range(length):

			if ord(arr[j])==i:
				t=t+1
		if t<l:
			l=t
	print(l*k)


import sys
n,k=map(int,input().split())
s=input()
for i in range(1,k+1):
    if chr(i+64) in s ==False:
        print('0')
        sys.exit
arr=[]
for j in range(1,k+1):
    t=0
    for i in range(n):
        if s[i]==chr(64+j):
            t+=1
    arr.append(t)
print(min(arr)*k)
del i,j,k,n,s,t,arr


import sys
n,k=map(int,input().split())
s=input()
for i in range(1,k+1):
    if chr(i+64) in s ==False:
        print('0')
        sys.exit
arr=[]
for j in range(1,k+1):
    t=0
    for i in range(n):
        if s[i]==chr(64+j):
            t+=1
    arr.append(t)
print(min(arr)*k)


n,k=map(int,input().split())
string=input()
arr=list(string)
l=0
d=ord(arr[0])
for i in range(n):
    if ord(arr[i]) == ord(arr[0]):
        l+=1

for i in range(n):
    if ord(arr[i])>d:
        d=ord(arr[i])

if d<ord('A')+k-1:
	print('0')
else:
    for i in range(65,64+k):
            t=0
            for j in range(n):
                if ord(arr[j])==i:
                    t=t+1
            if t<l:
                l=t

    print(l*k)


n,k=map(int,input().split())
string=input()
arr=list(string)
length=len(string)
l=0
d=ord('A')
for i in range(length):
	l+=1
	if ord(arr[i])!=65:
		break
for i in range(length):
	if ord(arr[i])>d:
		d=ord(arr[i])

if d<ord('A')+k-1:
	print('0')
else:
	for i in range(65,64+k):
		t=0
		for j in range(length):

			if ord(arr[j])==i:
				t=t+1
		if t<l:
			l=t
	print(l*k)