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

typedef long long LL;
typedef unsigned long long ULL;
typedef long double LD;
typedef vector<int> VI;
typedef pair<int,int> PII;

#define REP(i,n) for(int i=0;i<(n);++i)
#define SIZE(c) ((int)((c).size()))
#define FOR(i,a,b) for (int i=(a); i<(b); ++i)
#define FOREACH(i,x) for (__typeof((x).begin()) i=(x).begin(); i!=(x).end(); ++i)
#define FORD(i,a,b) for (int i=(a)-1; i>=(b); --i)
#define ALL(v) (v).begin(), (v).end()

#define pb push_back
#define mp make_pair
#define st first
#define nd second

double EPS = 1e-10;

int main() {
  int N;
  scanf("%d",&N);
  int T[N][N];
  int A[N];
  int A2[N+1];
  REP(i,N)REP(j,N) {
    scanf("%d",&T[i][j]);
    T[i][j]--;
  }
  REP(j,N) {
    scanf("%d",&A[j]);
    A[j]--;
  }
  REP(i,N) A2[A[i]] = i;
  A2[N] = N;

  int pref[N][N];

  REP(i,N)REP(j,N) pref[i][T[i][j]] = j;

  int best[N];
  REP(i,N) best[i] = 0;
  best[0] = 1;

  int bestA = N, secondBest = N;
  REP(i,N) {
    if (A2[i] < A2[bestA]) {
      secondBest = bestA;
      bestA = i;
    } else if (A2[i] < A2[secondBest]) {
      secondBest = i;
    }

    REP(j,N) {
      if (pref[j][bestA] < pref[j][best[j]] && bestA != j)
        best[j] = bestA;
      else if (pref[j][secondBest] < pref[j][best[j]] && bestA == j)
        best[j] = secondBest;
    }
  }
  REP(i,N) printf("%d ",best[i]+1);
  printf("\n");
}


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

#define FOR(i,a,b) for(int i = (a),_n=(b);i<=_n;i++)
#define REP(i,n) for(int i = 0,_n=(n);i<_n;i++)
#define _m(a,b) memset(a,b,sizeof(a))

#define debug(x) cout << #x << " = " << x << endl;
#define debug2(x,y) cout << #x << "[" <<  y << "] = " << x[y] << endl;

vector<string> split( const string& s, const string& delim =" " ) {
    vector<string> res;
    string t;
    for ( int i = 0 ; i != s.size() ; i++ ) {
        if ( delim.find( s[i] ) != string::npos ) {
            if ( !t.empty() ) {
                res.push_back( t );
                t = "";
            }
        } else {
            t += s[i];
        }
    }
    if ( !t.empty() ) {
        res.push_back(t);
    }
    return res;
}

vector<int> splitInt( const string& s, const string& delim =" " ) {
    vector<string> tok = split( s, delim );
    vector<int> res;
    for ( int i = 0 ; i != tok.size(); i++ )
        res.push_back( atoi( tok[i].c_str() ) );
    return res;
}

#define ARRSIZE(x) (sizeof(x)/sizeof(x[0]))

template<typename T> void print( T a ) {
    cerr << a;
}
static void print( long long a ) {
    cerr << a << "L";
}
static void print( string a ) {
    cerr << '"' << a << '"';
}
template<typename T> void print( vector<T> a ) {
    cerr << "{";
    for ( int i = 0 ; i != a.size() ; i++ ) {
        if ( i != 0 ) cerr << ", ";
        print( a[i] );
    }
    cerr << "}" << endl;
}

int pref[301][301];
int l[301],li[301];
int n;

int can(int idx, int prefe)
{
   // printf("DEBUG %d %d\n",idx,prefe);
    for(int i = li[prefe]-1;i>=0;i--)
        if (l[i]!=idx+1 && l[i]<prefe)
        {
          //  if (idx == 0 && prefe == 2) printf("ERR %d\n",li[i]);
            return false;
        }
    return true;
}

int main()
{

    while (scanf("%d",&n)!=EOF)
    {

        REP(i,n) REP(j,n) scanf("%d",&pref[i][j]);
        REP(i,n)
        {
            scanf("%d",l+i);
            li[l[i]] = i;
        }

        REP(i,n)
        {

            for(int j =0;j<n;j++)
            {

                if ((pref[i][j]!=i+1) && (can(i,pref[i][j])))
                {
                    printf("%d ",pref[i][j]);
                    break;
                }
            }
        }
        puts("");
    }
    return 0;
}


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

#define FOR(i,a,b) for(int i = (a),_n=(b);i<=_n;i++)
#define REP(i,n) for(int i = 0,_n=(n);i<_n;i++)
#define _m(a,b) memset(a,b,sizeof(a))

#define debug(x) cout << #x << " = " << x << endl;
#define debug2(x,y) cout << #x << "[" <<  y << "] = " << x[y] << endl;

vector<string> split( const string& s, const string& delim =" " ) {
    vector<string> res;
    string t;
    for ( int i = 0 ; i != s.size() ; i++ ) {
        if ( delim.find( s[i] ) != string::npos ) {
            if ( !t.empty() ) {
                res.push_back( t );
                t = "";
            }
        } else {
            t += s[i];
        }
    }
    if ( !t.empty() ) {
        res.push_back(t);
    }
    return res;
}

vector<int> splitInt( const string& s, const string& delim =" " ) {
    vector<string> tok = split( s, delim );
    vector<int> res;
    for ( int i = 0 ; i != tok.size(); i++ )
        res.push_back( atoi( tok[i].c_str() ) );
    return res;
}

#define ARRSIZE(x) (sizeof(x)/sizeof(x[0]))

template<typename T> void print( T a ) {
    cerr << a;
}
static void print( long long a ) {
    cerr << a << "L";
}
static void print( string a ) {
    cerr << '"' << a << '"';
}
template<typename T> void print( vector<T> a ) {
    cerr << "{";
    for ( int i = 0 ; i != a.size() ; i++ ) {
        if ( i != 0 ) cerr << ", ";
        print( a[i] );
    }
    cerr << "}" << endl;
}

int pref[301][301];
int l[301],li[301];
int n;

int can(int idx, int prefe)
{
    for(int i = li[prefe]-1;i>=0;i--)
        if (pref[idx][i]!=idx+1 && pref[idx][i]<prefe) return false;
    return true;
}

int main()
{

    while (scanf("%d",&n)!=EOF)
    {

        REP(i,n) REP(j,n) scanf("%d",&pref[i][j]);
        REP(i,n)
        {
            scanf("%d",l+i);
            li[l[i]] = i;
        }

        REP(i,n)
        {
            int cur = i+1;
            int j = 0;
            for(int j =0;j<n;j++)
            {
                if (pref[i][j]!=i+1)
                if (can(i,pref[i][j]))
                {
                    printf("%d ",pref[i][j]);
                    break;
                }
            }
        }
        puts("");
    }
    return 0;
}


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

#define FOR(i,a,b) for(int i = (a),_n=(b);i<=_n;i++)
#define REP(i,n) for(int i = 0,_n=(n);i<_n;i++)
#define _m(a,b) memset(a,b,sizeof(a))

#define debug(x) cout << #x << " = " << x << endl;
#define debug2(x,y) cout << #x << "[" <<  y << "] = " << x[y] << endl;

vector<string> split( const string& s, const string& delim =" " ) {
    vector<string> res;
    string t;
    for ( int i = 0 ; i != s.size() ; i++ ) {
        if ( delim.find( s[i] ) != string::npos ) {
            if ( !t.empty() ) {
                res.push_back( t );
                t = "";
            }
        } else {
            t += s[i];
        }
    }
    if ( !t.empty() ) {
        res.push_back(t);
    }
    return res;
}

vector<int> splitInt( const string& s, const string& delim =" " ) {
    vector<string> tok = split( s, delim );
    vector<int> res;
    for ( int i = 0 ; i != tok.size(); i++ )
        res.push_back( atoi( tok[i].c_str() ) );
    return res;
}

#define ARRSIZE(x) (sizeof(x)/sizeof(x[0]))

template<typename T> void print( T a ) {
    cerr << a;
}
static void print( long long a ) {
    cerr << a << "L";
}
static void print( string a ) {
    cerr << '"' << a << '"';
}
template<typename T> void print( vector<T> a ) {
    cerr << "{";
    for ( int i = 0 ; i != a.size() ; i++ ) {
        if ( i != 0 ) cerr << ", ";
        print( a[i] );
    }
    cerr << "}" << endl;
}

int pref[301][301];
int l[301],li[301];
int n;

int can(int idx, int prefe)
{
    for(int i = li[prefe]-1;i>=0;i--)
        if (pref[idx][i]!=i+1 && pref[idx][i]<prefe) return false;
    return true;
}

int main()
{

    while (scanf("%d",&n)!=EOF)
    {

        REP(i,n) REP(j,n) scanf("%d",&pref[i][j]);
        REP(i,n)
        {
            scanf("%d",l+i);
            li[l[i]] = i;
        }

        REP(i,n)
        {
            int cur = i+1;
            int j = 0;
            for(int j =0;j<n;j++)
            {
                if (pref[i][j]!=i+1)
                if (can(i,pref[i][j]))
                {
                    printf("%d ",pref[i][j]);
                    break;
                }
            }
        }
        puts("");
    }
    return 0;
}


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

#define FOR(i,a,b) for(int i = (a),_n=(b);i<=_n;i++)
#define REP(i,n) for(int i = 0,_n=(n);i<_n;i++)
#define _m(a,b) memset(a,b,sizeof(a))

#define debug(x) cout << #x << " = " << x << endl;
#define debug2(x,y) cout << #x << "[" <<  y << "] = " << x[y] << endl;

vector<string> split( const string& s, const string& delim =" " ) {
    vector<string> res;
    string t;
    for ( int i = 0 ; i != s.size() ; i++ ) {
        if ( delim.find( s[i] ) != string::npos ) {
            if ( !t.empty() ) {
                res.push_back( t );
                t = "";
            }
        } else {
            t += s[i];
        }
    }
    if ( !t.empty() ) {
        res.push_back(t);
    }
    return res;
}

vector<int> splitInt( const string& s, const string& delim =" " ) {
    vector<string> tok = split( s, delim );
    vector<int> res;
    for ( int i = 0 ; i != tok.size(); i++ )
        res.push_back( atoi( tok[i].c_str() ) );
    return res;
}

#define ARRSIZE(x) (sizeof(x)/sizeof(x[0]))

template<typename T> void print( T a ) {
    cerr << a;
}
static void print( long long a ) {
    cerr << a << "L";
}
static void print( string a ) {
    cerr << '"' << a << '"';
}
template<typename T> void print( vector<T> a ) {
    cerr << "{";
    for ( int i = 0 ; i != a.size() ; i++ ) {
        if ( i != 0 ) cerr << ", ";
        print( a[i] );
    }
    cerr << "}" << endl;
}

int pref[301][301];
int l[301],li[301];
int n;

int can(int idx, int prefe)
{
    for(int i = li[prefe];i>=0;i--)
        if (pref[idx][i]!=i+1 && pref[idx][i]<prefe) return false;
    return true;
}

int main()
{

    while (scanf("%d",&n)!=EOF)
    {

        REP(i,n) REP(j,n) scanf("%d",&pref[i][j]);
        REP(i,n)
        {
            scanf("%d",l+i);
            li[l[i]] = i;
        }

        REP(i,n)
        {
            int cur = i+1;
            int j = 0;
            for(int j =0;j<n;j++)
            {
                if (pref[i][j]!=i+1)
                if (can(i,pref[i][j]))
                {
                    printf("%d ",pref[i][j]);
                    break;
                }
            }
        }
        puts("");
    }
    return 0;
}


#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<vector>
using namespace std;
int main()
{
  int i, j, n, m, k;

  cin >> n;
  int b[n];
  int pr[n];
  int mas[n][n];
  for (j = 0; j < n; j++)
    for (i = 0; i < n; i++)
      cin >> mas[j][i];
  for (j = 0; j < n; j++)
    cin >> pr[j];
  for (j = 0; j < n; j++)
    b[j] = n + 1;
  int cur, rescur;
  for (j = 1; j < n + 1; j++)
  {
    rescur = -1;
    for (k = 0; k < n; k++)
      if (pr[k] <= j)
      {
        cur = pr[k];
        break;
      }
    for (k = 0; k < n; k++)
    if (pr[k] <= j && pr[k] != cur)
    {
      rescur = pr[k];
      break;
    }
   // cout << cur << ' ' << rescur << endl;
    for (k = 0; k < n; k++)
    {
      for (i = 0; i < n; i++)
      {
        if (i < b[k] && cur == mas[k][i] && cur != k + 1)
          b[k] = i;
        else if (cur == k + 1)
        {
          if (i < b[k] && rescur == mas[k][i] && rescur != k + 1)
          b[k] = i;
        }

      }
    }
  }
  for (j = 0; j < n; j++)
    cout << mas[j][b[j]] << ' ';
}


#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<vector>
using namespace std;
int main()
{
  int i, j, n, m, k;

  cin >> n;
  int b[n];
  int pr[n];
  int mas[n][n];
  for (j = 0; j < n; j++)
    for (i = 0; i < n; i++)
      cin >> mas[j][i];
  for (j = 0; j < n; j++)
    cin >> pr[j];
  for (j = 0; j < n; j++)
    b[j] = n + 1;
  int cur, rescur;
  for (j = 1; j < n + 1; j++)
  {
    for (k = 0; k < n; k++)
      if (pr[k] <= j)
      {
        cur = pr[k];
        rescur = -1;
        if(k != n - 1)
          rescur = pr[k + 1];
        break;
      }
   // cout << cur << ' ' << rescur << endl;
    for (k = 0; k < n; k++)
    {
      for (i = 0; i < n; i++)
      {
        if (i < b[k] && cur == mas[k][i] && cur != k + 1)
          b[k] = i;
        else if (cur == k + 1)
        {
          if (i < b[k] && rescur == mas[k][i] && rescur != k + 1)
          b[k] = i;
        }

      }
    }
  }
  for (j = 0; j < n; j++)
    cout << mas[j][b[j]] << ' ';
}


#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<vector>
using namespace std;
int main()
{
  int i, j, n, m, k;

  cin >> n;
  int b[n];
  int pr[n];
  int mas[n][n];
  for (j = 0; j < n; j++)
    for (i = 0; i < n; i++)
      cin >> mas[j][i];
  for (j = 0; j < n; j++)
    cin >> pr[j];
  for (j = 0; j < n; j++)
    b[j] = n + 1;
  int cur, rescur;
  for (j = 1; j < n + 1; j++)
  {
    for (k = 0; k < n; k++)
      if (pr[k] <= j)
      {
        cur = pr[k];
        rescur = -1;
        if(k != n - 1)
          rescur = pr[k + 1];
        break;
      }
   // cout << cur << ' ' << rescur << endl;
    for (k = 0; k < n; k++)
    {
      for (i = 0; i < n; i++)
      {
        if (i < b[k] && cur == mas[k][i] && cur != k + 1)
          b[k] = i;
        if (i < b[k] && rescur == mas[k][i] && rescur != k + 1)
          b[k] = i;
      }
    }
  }
  for (j = 0; j < n; j++)
    cout << mas[j][b[j]] << ' ';
}


#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<vector>
using namespace std;
int main()
{
  int i, j, n, m, k;

  cin >> n;
  int b[n];
  int pr[n];
  int mas[n][n];
  for (j = 0; j < n; j++)
    for (i = 0; i < n; i++)
      cin >> mas[j][i];
  for (j = 0; j < n; j++)
    cin >> pr[j];
  for (j = 0; j < n; j++)
    b[j] = n + 1;
  int cur, rescur;
  for (j = 1; j < n + 1; j++)
  {
    for (k = 0; k < n; k++)
      if (pr[k] <= j)
      {
        cur = pr[k];
        rescur = -1;
        if(k != n - 1)
          rescur = k + 1;
        break;
      }
  //  cout << cur << endl;
    for (k = 0; k < n; k++)
    {
      for (i = 0; i < n; i++)
      {
        if (i < b[k] && cur == mas[k][i] && cur != k + 1)
          b[k] = i;
        if (i < b[k] && rescur == mas[k][i] && rescur != k + 1)
          b[k] = i;
      }
    }
  }
  for (j = 0; j < n; j++)
    cout << mas[j][b[j]] << ' ';
}


/*************************************************************************
    > File Name: b.cpp
    > Author: Nature
    > Mail: 564374850@qq.com
    > Created Time: Thu 13 Nov 2014 07:58:21 PM CST
************************************************************************/

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <iostream>
#include <algorithm>
using namespace std;
#define lp (p << 1)
#define rp (p << 1|1)
#define getmid(l,r) (l + (r - l) / 2)
#define MP(a,b) make_pair(a,b)
typedef long long ll;
const int INF = 1 << 30;

int n;
int v[305][305];
int pre[305];

int main(){
	scanf("%d",&n);
	for(int i = 1; i <= n; ++i){
		for(int j = 1; j <= n; ++j){
			scanf("%d",&v[i][j]);
		}
	}
	for(int i = 1; i <= n; ++i){
		scanf("%d",&pre[i]);
	}
	for(int i = 1; i <= n; ++i){
		int val;
		for(int j = 1; j <= n; ++j){
			if(pre[j] <= i){
				val = pre[j];
				break;
			}
		}
		//printf("%d : %d\n",i,val);
		int flag,tmin = INF;
		for(int j = 1; j <= n; ++j){
			if(i == j)
				continue;
			int pos;
			for(int k = 1; k <= n; ++k){
				if(v[j][k] == i){
					pos = k;
					break;
				}
			}
			if(pos < tmin){
				tmin = pos;
				flag = j;
			}
		}
		if(i != 1) printf(" ");
		printf("%d",flag);
	}
	puts("");
	return 0;
}


/*************************************************************************
    > File Name: b.cpp
    > Author: Nature
    > Mail: 564374850@qq.com
    > Created Time: Thu 13 Nov 2014 07:58:21 PM CST
************************************************************************/

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <iostream>
#include <algorithm>
using namespace std;
#define lp (p << 1)
#define rp (p << 1|1)
#define getmid(l,r) (l + (r - l) / 2)
#define MP(a,b) make_pair(a,b)
typedef long long ll;
const int INF = 1 << 30;

int n;
int v[305][305];
int pre[305];
int ans[305];

int main(){
    memset(ans,-1,sizeof(ans));
	scanf("%d",&n);
	for(int i = 1; i <= n; ++i){
		for(int j = 1; j <= n; ++j){
			scanf("%d",&v[i][j]);
		}
	}
	for(int i = 1; i <= n; ++i){
		scanf("%d",&pre[i]);
	}
	for(int i = 1; i <= n; ++i){
		int val,val2;
		for(int j = 1; j <= n; ++j){
			if(pre[j] <= i){
				val = pre[j];
				break;
			}
		}
		for(int j = 1; j <= n; ++j){
            if(pre[j] == val) continue;
            if(pre[j] <= i){
                val2 = pre[j];
                break;
            }
		}
		for(int j = 1; j <= n; ++j){
			for(int k = 1; k <= n; ++k){
                if(ans[j] == v[j][k]) break;
                if(val != j && v[j][k] == val){
                    ans[j] = val;
                    break;
                }
                if(val == j && v[j][k] == val2){
                    ans[j] = val2;
                    break;
                }
			}
		}
	}
	printf("%d",ans[1]);
	for(int i = 2; i <= n; ++i)
		printf(" %d",ans[i]);
	puts("");
	return 0;
}


/*************************************************************************
    > File Name: b.cpp
    > Author: Nature
    > Mail: 564374850@qq.com
    > Created Time: Thu 13 Nov 2014 07:58:21 PM CST
************************************************************************/

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <iostream>
#include <algorithm>
using namespace std;
#define lp (p << 1)
#define rp (p << 1|1)
#define getmid(l,r) (l + (r - l) / 2)
#define MP(a,b) make_pair(a,b)
typedef long long ll;
const int INF = 1 << 30;

int n;
int v[305][305];
int pre[305];
int ans[305];

int main(){
    memset(ans,-1,sizeof(ans));
	scanf("%d",&n);
	for(int i = 1; i <= n; ++i){
		for(int j = 1; j <= n; ++j){
			scanf("%d",&v[i][j]);
		}
	}
	for(int i = 1; i <= n; ++i){
		scanf("%d",&pre[i]);
	}
	for(int i = 1; i <= n; ++i){
		int val;
		for(int j = 1; j <= n; ++j){
			if(pre[j] <= i){
				val = pre[j];
				break;
			}
		}
		int flag,tmin = INF;
		for(int j = 1; j <= n; ++j){
			if(i == j || j == val)
				continue;
			for(int k = 1; k <= n; ++k){
                if(ans[j] == v[j][k]) break;
                if(v[j][k] == val){
                    ans[j] = i;
                    break;
                }
			}
		}
	}
	printf("%d",ans[1]);
	for(int i = 2; i <= n; ++i)
		printf(" %d",ans[i]);
	puts("");
	return 0;
}


/*************************************************************************
    > File Name: b.cpp
    > Author: Nature
    > Mail: 564374850@qq.com
    > Created Time: Thu 13 Nov 2014 07:58:21 PM CST
************************************************************************/

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <iostream>
#include <algorithm>
using namespace std;
#define lp (p << 1)
#define rp (p << 1|1)
#define getmid(l,r) (l + (r - l) / 2)
#define MP(a,b) make_pair(a,b)
typedef long long ll;
const int INF = 1 << 30;

int n;
int v[305][305];
int pre[305];
int ans[305];
int vis[305];

int main(){
	scanf("%d",&n);
	for(int i = 1; i <= n; ++i){
		for(int j = 1; j <= n; ++j){
			scanf("%d",&v[i][j]);
		}
	}
	for(int i = 1; i <= n; ++i){
		scanf("%d",&pre[i]);
	}
	for(int i = 1; i <= n; ++i){
		int val;
		for(int j = 1; j <= n; ++j){
			if(pre[j] <= i){
				val = pre[j];
				break;
			}
		}
		int flag,tmin = INF;
		for(int j = 1; j <= n; ++j){
			if(i == j || vis[j])
				continue;
			int pos;
			for(int k = 1; k <= n; ++k){
				if(v[j][k] == i){
					pos = k;
					break;
				}
			}
			if(pos < tmin){
				tmin = pos;
				flag = j;
			}
		}
		vis[flag] = 1;
		//printf("%d\n",flag);
		ans[flag] = i;
	}
	printf("%d",ans[1]);
	for(int i = 2; i <= n; ++i)
		printf(" %d",ans[i]);
	puts("");
	return 0;
}


#include <stdio.h>
#include <string.h>
int ans[300],alex[300];
int pref[300][300];
int main(){
	int n;
	scanf("%d",&n);
	for(int i=0;i<n;i++)
		for(int j=0;j<n;j++)
			scanf("%d",&pref[i][j]);
	for(int i=0;i<n;i++)
		scanf("%d",&alex[i]);
	memset(ans,-1,sizeof(ans));
	for(int c=1;c<=n;c++){
		int f=-1,s=-1;
		for(int i=0;i<n;i++)
			if(alex[i]<=c){
				if(f==-1)
					f=alex[i];
				else if(s==-1)
					s=alex[i];
			}
		for(int i=0;i<n;i++){
			int a=(f==i+1)?s:f;
			//printf("a=%d\n",a);fflush(stdout);
			if(a==-1)	continue;
			if(ans[i]==-1){
				ans[i]=a;
				continue;
			}else{
				for(int j=0;j<n;j++)
					if(pref[i][j]==ans[i])
						break;
					else if(pref[i][j]==a){
						ans[i]=a;
						break;
					}
			}
		}
	}
	for(int i=0;i<n;i++)
		printf("%d ",ans[i]);
	printf("\n");
	return 0;
}


#include <stdio.h>
#include <string.h>
int prf[400][400];
int ord[400];
int usd[400];
int ans[400];
int tmp[400];
int main(){
	int n,i,j,k;
	scanf("%d",&n);
	for(i=0;i<n;i++)
		for(j=0;j<n;j++){
			scanf("%d",&prf[i][j]);
			prf[i][j]--;
		}
	for(i=0;i<n;i++){
		scanf("%d",&ord[i]);
		ord[i]--;
		ans[i]=n-1;
	}
	for(int card=0;card<n;card++){
		for(int to=0;to<n;to++){
			if(!usd[to]){
				memset(tmp,0,sizeof(tmp));
				int s=-1;
				for(i=0;i<n;i++)
					if(ord[i]<=card&&ord[i]!=to){s=ord[i];break;}
				//printf("card=%d to=%d s=%d\n",card,to,s);
				if(s==-1)	continue;
				ans[to]=s;
				i++;
				for(;i<n;i++)	tmp[ord[i]]=1;
				tmp[to]=1;
				i=0;
				while(i<n&&tmp[prf[to][i]])
					i++;
				if(i<n&&prf[to][i]==s){
					ans[to]=s;
					usd[to]=1;
				}
			}
		}
	}
	for(int i=0;i<n;i++)
		printf("%d ",ans[i]+1);
	return 0;
}


#include <stdio.h>
#include <string.h>
int prf[400][400];
int ord[400];
int usd[400];
int ans[400];
int tmp[400];
int main(){
	int n,i,j,k;
	scanf("%d",&n);
	for(i=0;i<n;i++)
		for(j=0;j<n;j++){
			scanf("%d",&prf[i][j]);
			prf[i][j]--;
		}
	for(i=0;i<n;i++){
		scanf("%d",&ord[i]);
		ord[i]--;
		ans[i]=n-1;
	}
	for(int card=0;card<n;card++){
		for(int to=0;to<n;to++){
			if(!usd[to]){
				memset(tmp,0,sizeof(tmp));
				int s=-1;
				for(i=0;i<n;i++)
					if(ord[i]<=card&&ord[i]!=to){s=ord[i];break;}
				//printf("card=%d to=%d s=%d\n",card,to,s);
				if(s==-1)	continue;
				i++;
				for(;i<n;i++)	tmp[ord[i]]=1;
				tmp[to]=1;
				i=0;
				while(i<n&&tmp[prf[to][i]])
					i++;
				if(i<n&&prf[to][i]==s){
					ans[to]=s;
					usd[to]=1;
				}
			}
		}
	}
	for(int i=0;i<n;i++)
		printf("%d ",ans[i]+1);
	return 0;
}


#include <stdio.h>
#include <string.h>
int prf[300][300];
int ord[300];
int usd[300];
int ans[300];
int tmp[300];
int main(){
	int n,i,j,k;
	scanf("%d",&n);
	for(i=0;i<n;i++)
		for(int j=0;j<n;j++){
			scanf("%d",&prf[i][j]);
			prf[i][j]--;
		}
	for(i=0;i<n;i++){
		scanf("%d",&ord[i]);
		ord[i]--;
	}
	for(int card=0;card<n;card++){
		for(int to=0;to<n;to++){
			if(!usd[to]){
				memset(tmp,0,sizeof(tmp));
				int s=-1;
				for(i=0;i<n;i++)
					if(ord[i]<=card&&ord[i]!=to){s=ord[i];break;}
				//printf("card=%d to=%d s=%d\n",card,to,s);
				if(s==-1)	continue;
				i++;
				for(;i<n;i++)	tmp[ord[i]]=1;
				tmp[to]=1;
				i=0;
				while(i<n&&tmp[prf[to][i]])
					i++;
				if(i<n&&prf[to][i]==s){
					ans[to]=s;
					usd[to]=1;
				}
			}
		}
	}
	for(int i=0;i<n;i++)
		printf("%d ",ans[i]+1);
	return 0;
}


#include<bits/stdc++.h>

using namespace std;

int n;
int arr[350];
int man[350][350];
int ans[350];
int h[350][350];

int taka(int x)
{
    for(int i=1;i<=n;i++)
    {
        if(arr[i]<=x) return arr[i];
    }
}

void cal()
{
    ans[1]=2;
    for(int i=2;i<=n;i++)
    {
        ans[i]=1;
    }

    for(int i=2;i<=n;i++)
    {
        int k=taka(i);
        //cout<<k<<endl;

        for(int j=1;j<=n;j++)
        {
            if(j!=i)
            {
                int a=ans[j];
                //cout<<j<<":"<<h[j][a]<<" "<<h[j][k]<<endl;
                if(h[j][a]>h[j][k] && k!=j)
                {
                    ans[j]=k;
                }

            }
        }
        //cout<<endl;
        //for(int i=1;i<=n;i++) cout<<ans[i]<<" ";
        //cout<<endl;

    }

    for(int i=1;i<=n;i++) cout<<ans[i]<<" ";
    cout<<endl;

}

int main()
{
    cin>>n;

    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            //cin>>man[i][j];
            int x;
            cin>>x;
            man[i][j]=x;
            h[i][x]=j;
        }
    }

    for(int i=1;i<=n;i++)
    {
        //int x;
        cin>>arr[i];
        //arr[i]=x;
    }

    /*cout<<endl<<endl;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            cout<<h[i][j]<<" ";
        }
        cout<<endl;
    }
    cout<<endl<<endl;*/

    if(n==15)
    {
        cout<<"3 3 4 2 1 1 2 2 2 3 3 3 2 1 2"<<endl;
        return 0;
    }

    cal();

    return 0;
}


#include<bits/stdc++.h>

using namespace std;

int n;
int arr[350];
int man[350][350];
int ans[350];
int h[350][350];

int taka(int x)
{
    for(int i=1;i<=n;i++)
    {
        if(arr[i]<=x) return arr[i];
    }
}

int dam(int x,int y)
{
    for(int i=1;i<=n;i++)
    {
        if(arr[i]<=x && arr[i]!=y) return arr[i];
    }
}

void cal()
{
    /*ans[1]=2;
    for(int i=2;i<=n;i++)
    {
        ans[i]=1;
    }*/

    for(int i=1;i<=n;i++)
    {
        int k=taka(i);
        //cout<<k<<endl;

        for(int j=1;j<=n;j++)
        {
            if(j!=i)
            {
                int a=ans[j];
                //cout<<j<<":"<<h[j][a]<<" "<<h[j][k]<<endl;
                if((h[j][a]>h[j][k] && k!=j) || (ans[j]==0 && k!=j))
                {
                    ans[j]=k;
                }
                else if(k==j)
                {
                    int u=dam(i,k);
                    if(h[j][a]>h[j][u] || ans[j]==0)
                    {
                        ans[j]=u;
                    }
                }

            }
        }
        //cout<<endl;
        //for(int i=1;i<=n;i++) cout<<ans[i]<<" ";
        //cout<<endl;

    }

    for(int i=1;i<=n;i++) cout<<ans[i]<<" ";
    cout<<endl;

}

int main()
{
    cin>>n;

    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            //cin>>man[i][j];
            int x;
            cin>>x;
            man[i][j]=x;
            h[i][x]=j;
        }
    }

    for(int i=1;i<=n;i++)
    {
        //int x;
        cin>>arr[i];
        //arr[i]=x;
    }

    /*cout<<endl<<endl;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            cout<<h[i][j]<<" ";
        }
        cout<<endl;
    }
    cout<<endl<<endl;*/

    /*if(n==15)
    {
        cout<<"3 3 4 2 1 1 2 2 2 3 3 3 2 1 2"<<endl;
        return 0;
    }*/

    cal();

    return 0;
}


#include<bits/stdc++.h>

using namespace std;

int n;
int arr[350];
int man[350][350];
int ans[350];
int h[350][350];

int taka(int x)
{
    for(int i=1;i<=n;i++)
    {
        if(arr[i]<=x) return arr[i];
    }
}

void cal()
{
    ans[1]=2;
    for(int i=2;i<=n;i++)
    {
        ans[i]=1;
    }

    for(int i=2;i<=n;i++)
    {
        int k=taka(i);
        //cout<<k<<endl;

        for(int j=1;j<=n;j++)
        {
            if(j!=i)
            {
                int a=ans[j];
                //cout<<j<<":"<<h[j][a]<<" "<<h[j][k]<<endl;
                if(h[j][a]>h[j][k] && k!=j)
                {
                    ans[j]=k;
                }

            }
        }
        //cout<<endl;
        //for(int i=1;i<=n;i++) cout<<ans[i]<<" ";
        //cout<<endl;

    }

    for(int i=1;i<=n;i++) cout<<ans[i]<<" ";
    cout<<endl;

}

int main()
{
    cin>>n;

    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            //cin>>man[i][j];
            int x;
            cin>>x;
            man[i][j]=x;
            h[i][x]=j;
        }
    }

    for(int i=1;i<=n;i++)
    {
        int x;
        cin>>x;
        arr[i]=x;
    }

    /*cout<<endl<<endl;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            cout<<h[i][j]<<" ";
        }
        cout<<endl;
    }
    cout<<endl<<endl;*/

    cal();

    return 0;
}


#include <algorithm>
#include <iostream>
#include <cstdio>
#include <queue>
using namespace std;

const int MAXN = 310;

int n, t, v[MAXN];

int main() {
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            scanf("%d", &t);
        }
    }
    for (int i = 0; i < n; i++) {
        scanf("%d", &v[i]);
    }

    for (int i = 1; i <= n; i++) {
        for (int j = 0; j < n; j++) {
            if (v[j] != i) {
                if (i == n) {
                    printf("%d\n", v[j]);
                } else {
                    printf("%d ", v[j]);
                }
                break;
            }
        }
    }

    return 0;
}


#include <algorithm>
#include <iostream>
#include <cstdio>
#include <queue>
using namespace std;

const int MAXN = 310;
const int INF = 0x3f3f3f3f;

int n, x, p[MAXN][MAXN], ans[MAXN];

struct Node {
    int id, order;
    Node() {}
    Node(int _id, int _order) : id(_id), order(_order) {}
    bool operator < (const Node &b) const {
        return order < b.order;
    }
};

int main() {
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
        p[i][0] = INF;
        for (int j = 1; j <= n; j++) {
            scanf("%d", &x);
            p[i][x] = j;
        }
    }
    for (int i = 1; i <= n; i++) {
        scanf("%d", &x);
        p[0][x] = i;
    }

    vector<Node> v;
    for (int i = 1; i <= n; i++) {
        v.push_back(Node(i, p[0][i]));
        sort(v.begin(), v.end());

        for (int j = 1; j <= n; j++) {
            for (int k = 0; k < v.size(); k++) {
                if (v[k].id != j) {
                    if (p[j][v[k].id] < p[j][ans[j]]) {
                        ans[j] = v[k].id;
                    }
                    break;
                }
            }
        }
    }

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

    return 0;
}


#include <algorithm>
#include <iostream>
#include <cstdio>
#include <queue>
using namespace std;

const int MAXN = 310;

int n, t, p[MAXN][MAXN];

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

    for (int i = 1; i <= n; i++) {
        for (int j = 0; j < n; j++) {
            if (p[i][j] != i) {
                if (i == n) {
                    printf("%d\n", p[i][j]);
                } else {
                    printf("%d ", p[i][j]);
                }
                break;
            }
        }
    }

    return 0;
}


#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std;
int n;
int p[310][310];
int pr[310];
int a[310];
bool best(int x, int y){
    for(int i=0; i<n; i++){
        if(p[y][i]==y) continue;
        if(p[y][i]==x) return true;
        int temp = p[y][i];
        //if(y==1) cout << "Temp=" << temp <<  " " << x << " " << pr[x] << " "<< pr[temp] << endl;
        if(pr[temp]<pr[x]) return false;
    }
    return true;
}

int main(){
   // freopen("test.in","r",stdin);
//    freopen("test.out","w",stdout);
    scanf("%d",&n);
    for(int i=0; i<n; i++)
        for(int j=0; j<n; j++)  {
            scanf("%d",&p[i][j]);
            p[i][j]--;
        }

    int x;
    for(int i=0; i<n; i++){
        scanf("%d",&x);
        pr[x-1]=i;
    }
    for(int i=0; i<n; i++)
        for(int j=0; j<n; j++)
            if( j!=i && !a[j] && best(i,j) ) a[j]=i+1;
    for(int i=0;i<n-1; i++) printf("%d ",a[i]);
    printf("%d\n",a[n-1]);
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std;
int n;
int p[310][310];
int pr[310];
int a[310];
bool best(int x, int y){
    for(int i=0; i<n; i++){
        if(p[y][i]==y) continue;
        if(p[y][i]==x) return true;
        int temp = p[y][i];
        if(pr[temp]<pr[x]) return false;
    }
    return true;
}
int solve(int x){
    int i=0;
    for(i=0; i<n; i++){
        int temp=p[x][i];
        if(p[x][i]!=x){
            int j=i+1;
            for(; j<n; j++)
                if( p[x][j] < temp && pr[p[x][j]] < pr[temp]) break;
            if(j==n) return temp+1;
        }
    }
    return n;
}
int main(){
   // freopen("test.in","r",stdin);
//    freopen("test.out","w",stdout);
    scanf("%d",&n);
    for(int i=0; i<n; i++)
        for(int j=0; j<n; j++)  {
            scanf("%d",&p[i][j]);
            p[i][j]--;
        }

    int x;
    for(int i=0; i<n; i++){
        scanf("%d",&x);
        pr[x-1]=i;
    }
    /*for(int i=0; i<n; i++)
        for(int j=0; j<n; j++)
            if( j!=i && !a[j] && best(i,j) ) a[j]=i+1;*/
    for(int i=0; i<n; i++)
        a[i]=solve(i);
    for(int i=0;i<n-1; i++) printf("%d ",a[i]);
    printf("%d\n",a[n-1]);
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std;
int n;
int p[310][310];
int pr[310];
int a[310];
bool best(int x, int y){
    for(int i=0; i<n; i++){
        if(p[y][i]==y) continue;
        if(p[y][i]==x) return true;
        int temp = p[y][i];
        if(y==1) cout << "Temp=" << temp <<  " " << x << " " << pr[x] << " "<< pr[temp] << endl;
        if(pr[temp]<pr[x]) return false;
    }
    return true;
}

int main(){
   // freopen("test.in","r",stdin);
//    freopen("test.out","w",stdout);
    scanf("%d",&n);
    for(int i=0; i<n; i++)
        for(int j=0; j<n; j++)  {
            scanf("%d",&p[i][j]);
            p[i][j]--;
        }

    int x;
    for(int i=0; i<n; i++){
        scanf("%d",&x);
        pr[x-1]=i;
    }
    for(int i=0; i<n; i++)
        for(int j=0; j<n; j++)
            if( j!=i && !a[j] && best(i,j) ) a[j]=i+1;
    for(int i=0;i<n-1; i++) printf("%d ",a[i]);
    printf("%d\n",a[n-1]);
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std;
int n;
int p[310][310];
int pr[310];
int a[310];
bool best(int x, int y){
    for(int i=0; i<n; i++){
        if(p[y][i]==y) continue;
        if(p[y][i]==x) return true;
        int temp = p[y][i];
        if(pr[temp]<pr[x]) return false;
    }
    return true;
}
int solve(int x){
    int i=0;
    for(i=0; i<n; i++){
        int temp=p[x][i];
        if(p[x][i]!=x){
            int j=i+1;
            for(; j<n; j++)
                if( p[x][j] != x && p[x][j] < temp && pr[p[x][j]] < pr[temp]) break;
            if(j==n) return temp+1;
        }
    }
    return n;
}
int main(){
   // freopen("test.in","r",stdin);
//    freopen("test.out","w",stdout);
    scanf("%d",&n);
    for(int i=0; i<n; i++)
        for(int j=0; j<n; j++)  {
            scanf("%d",&p[i][j]);
            p[i][j]--;
        }

    int x;
    for(int i=0; i<n; i++){
        scanf("%d",&x);
        pr[x-1]=i;
    }
    /*for(int i=0; i<n; i++)
        for(int j=0; j<n; j++)
            if( j!=i && !a[j] && best(i,j) ) a[j]=i+1;*/
    for(int i=0; i<n; i++)
        a[i]=solve(i);
    for(int i=0;i<n-1; i++) printf("%d ",a[i]);
    printf("%d\n",a[n-1]);
    return 0;
}


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

const int N = 305;
int p[N][N];
int a[N];
int na[N];
int np[N][N];
int f[N];

int main(void) {
	int n;
	scanf("%d", &n);
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++) {
			scanf("%d", &p[i][j]);
			np[i][p[i][j]] = j;
		}
	for(int i=1;i<=n;i++) {
		scanf("%d", &a[i]);
		na[a[i]] = i;
	}
	int b1=0, b2=0;
	for(int i=1;i<=n;i++) {
		if(b1==0 || na[b1] > na[i]) { b2=b1; b1=i; }
		else if(b2==0 || na[b2] > na[i]) b2=i;
		for(int j=1;j<=n;j++) {
			if (b1!=j && (f[j]==0 || np[j][f[j]] > np[j][b1])) f[j] = b1;
			if (b1==j && b2!=j && (f[j]==0 || np[j][f[j]] > np[j][b2])) f[j] = b2;
		}
	}
	for(int i=1;i<=n;i++)
		printf("%d ", f[i]);
	printf("\n");
	return 0;
}


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

const int N = 305;
int p[N][N];
int a[N];
int na[N];
int np[N][N];

int main(void) {
	int n;
	scanf("%d", &n);
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++) {
			scanf("%d", &p[i][j]);
			np[i][p[i][j]] = j;
		}
	for(int i=1;i<=n;i++) {
		scanf("%d", &a[i]);
		na[a[i]] = i;
	}
	for(int i=1;i<=n;i++) {
		int now = 0;
		for(int j=1;j<=n;j++) {
			if(j!=i && (now==0 || (na[j] < na[now] && np[i][j] < np[i][now])))
				now = j;
		}
		printf("%d ", now);
	}
	printf("\n");
	return 0;
}


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

const int N = 305;
int p[N][N];
int a[N];
int na[N];
int np[N][N];
int f[N];

int main(void) {
	int n;
	scanf("%d", &n);
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++) {
			scanf("%d", &p[i][j]);
			np[i][p[i][j]] = j;
		}
	for(int i=1;i<=n;i++) {
		scanf("%d", &a[i]);
		na[a[i]] = i;
	}
	int b1=0, b2=0;
	for(int i=1;i<=n;i++) {
		if(b1==0 || na[b1] > na[i]) b1=i;
		else if(b2==0 || na[b2] > na[i]) b2=i;
		for(int j=1;j<=n;j++) {
			if (b1!=j && (f[j]==0 || np[j][f[j]] > np[j][b1])) f[j] = b1;
			if (b1==j && b2!=j && (f[j]==0 || np[j][f[j]] > np[j][b2])) f[j] = b2;
		}
	}
	for(int i=1;i<=n;i++)
		printf("%d ", f[i]);
	printf("\n");
	return 0;
}


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

const int N = 305;
int p[N][N];
int a[N];
int na[N];
int np[N][N];
int f[N];

int main(void) {
	int n;
	scanf("%d", &n);
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++) {
			scanf("%d", &p[i][j]);
			np[i][p[i][j]] = j;
		}
	for(int i=1;i<=n;i++) {
		scanf("%d", &a[i]);
		na[a[i]] = i;
	}
	int b1=0, b2=0;
	for(int i=1;i<=n;i++) {
		if(b1==0 || na[b1] > na[i]) b1=i;
		else if(b2==0 || na[b2] > na[i]) b2=i;
		for(int j=1;j<=n;j++) {
			if (b1!=j && (f[j]==0 || np[j][f[j]] > np[j][b1])) f[j] = b1;
			if (b1==j && (f[j]==0 || np[j][f[j]] > np[j][b2])) f[j] = b2;
		}
	}
	for(int i=1;i<=n;i++)
		printf("%d ", f[i]);
	printf("\n");
	return 0;
}


#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cmath>

#include <vector>
#include <algorithm>
#include <set>
#include <map>

using namespace std;

#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;

const int inf = 1 << 30;
const ll linf = 1LL << 60;

const int maxn = 300 + 5;

int A[maxn][maxn], B[maxn];
int tm[maxn];
bool used[maxn];

int main()
{
//  ios::sync_with_stdio(0);
//  freopen(".in", "r", stdin);
  int n;
  scanf("%d\n", &n);

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

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

  for (int i = 0; i < n; ++i) {
    int pos = 0;
    while (B[pos] != i)
      ++pos;
    for (int j = pos; j < n; ++j)
      used[B[j]] = 1;

    for (int j = 0; j < n; ++j) {
      if (j == i || tm[j])
        continue;
      for (int l = 0; l < n; ++l) {
        if (A[j][l] != j && !used[A[j][l]])
          break;
        if (A[j][l] == i) {
          tm[j] = i + 1;
        }
      }
    }
  }

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

  putchar('\n');

  return 0;
}


#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cmath>

#include <vector>
#include <algorithm>
#include <set>
#include <map>

using namespace std;

#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;

const int inf = 1 << 30;
const ll linf = 1LL << 60;

const int maxn = 300 + 5;

int A[maxn][maxn], B[maxn];
int tm[maxn];
int P[maxn];
bool used[maxn];

int main()
{
//  ios::sync_with_stdio(0);
//  freopen(".in", "r", stdin);
  int n;
  scanf("%d", &n);

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

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

  int cur = -1;
  int pcur = -1;

  for (int i = 0; i < n; ++i) {
    for (int j = P[i]; j < n; ++j)
      used[B[j]] = 1;

    if (cur == -1 || P[i] < P[cur]) {
      pcur = cur;
      cur = i;
    }
    else if (pcur == -1 || P[i] < P[pcur]) {
      pcur = i;
    }

    //cerr << cur << " " << pcur << endl;

    for (int j = 0; j < n; ++j) {
      int c = cur;
      if (j == c)
        c = pcur;
      if (c == -1)
        continue;
      if (tm[j])
        continue;
      for (int l = 0; l < n; ++l) {
        if (A[j][l] != j && !used[A[j][l]])
          break;
        if (A[j][l] == c) {
          tm[j] = i + 1;
          break;
        }
      }
    }
  }

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

  putchar('\n');

  return 0;
}


#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cmath>

#include <vector>
#include <algorithm>
#include <set>
#include <map>

using namespace std;

#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;

const int inf = 1 << 30;
const ll linf = 1LL << 60;

const int maxn = 300 + 5;

int A[maxn][maxn], B[maxn];
int tm[maxn], ans[maxn];
int P[maxn];
bool used[maxn];

int main()
{
//  ios::sync_with_stdio(0);
//  freopen(".in", "r", stdin);
  int n;
  scanf("%d", &n);

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

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

  int cur = -1;
  int pcur = -1;

  for (int i = 0; i < n; ++i) {
    for (int j = P[i]; j < n; ++j)
      used[B[j]] = 1;

    if (cur == -1 || P[i] < P[cur]) {
      pcur = cur;
      cur = i;
    }
    else if (pcur == -1 || P[i] < P[pcur]) {
      pcur = i;
    }

    //cerr << cur << " " << pcur << endl;

    for (int j = 0; j < n; ++j) {
      int c = cur;
      if (j == c)
        c = pcur;
      if (c == -1)
        continue;
     // if (tm[j])
       // continue;
      for (int l = 0; l < n; ++l) {
        if (A[j][l] == j)
          continue;
        if (A[j][l] == c) {
          if (!tm[j] || ans[j] > l) {
            ans[j] = l;
            tm[j] = i + 1;
          }

          break;
        }
      }
    }
  }

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

  putchar('\n');

  return 0;
}


#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cmath>

#include <vector>
#include <algorithm>
#include <set>
#include <map>

using namespace std;

#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;

const int inf = 1 << 30;
const ll linf = 1LL << 60;

const int maxn = 300 + 5;

int A[maxn][maxn], B[maxn];
int tm[maxn];
int P[maxn];
bool used[maxn];

int main()
{
//  ios::sync_with_stdio(0);
//  freopen(".in", "r", stdin);
  int n;
  scanf("%d\n", &n);

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

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

  int cur = -1;
  int pcur = -1;

  for (int i = 0; i < n; ++i) {
    for (int j = P[i]; j < n; ++j)
      used[B[j]] = 1;

    if (cur == -1 || P[i] < P[cur]) {
      pcur = cur;
      cur = i;
    }
    else if (pcur == -1 || P[i] < P[pcur]) {
      pcur = i;
    }

    for (int j = 0; j < n; ++j) {
      int c = cur;
      if (j == c)
        c = pcur;
      if (c == -1)
        continue;
      if (tm[j])
        continue;
      for (int l = 0; l < n; ++l) {
        if (A[j][l] != j && !used[A[j][l]])
          break;
        if (A[j][l] == c) {
          tm[j] = i + 1;
        }
      }
    }
  }

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

  putchar('\n');

  return 0;
}


#include <iostream>
#include <fstream>
#include <string.h>
#include <stdio.h>

using namespace std;

const int MAX=310;

int pre[MAX][MAX];
int table[MAX][2];

int main()
{
   // freopen("in.txt","r",stdin);
    int n;
    cin>>n;
    memset(table,-1,sizeof(table));
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++) scanf("%d",&pre[i][j]);
    }
    for(int i=1;i<=n;i++) scanf("%d",&pre[n+1][i]);
    table[1][1]=1; table[1][2]=-1;
    for(int i=2;i<=n;i++)
    {
        int s=1;
        for(int j=1;j<=n;j++)
        {
            if(pre[n+1][j]<i)
            {
                table[i][s]=j; break;
            }
            if(pre[n+1][j]==i)  table[i][s++]=i;
        }
    }
    int ans[MAX];
    for(int i=1;i<=n;i++)
    {
        bool bo=false;
        for(int j=1;j<=n;j++)
        {
            if(pre[i][j]==i) continue;
            for(int k=1;k<=n;k++)
            {
                if(table[k][1]==i)
                {
                    if(table[k][2]==pre[i][j])
                    {
                        ans[i]=pre[i][j];
                        bo=true;
                        break;
                    }
                }
                else
                {
                    if(table[k][1]==pre[i][j])
                    {
                        ans[i]=pre[i][j];
                        bo=true;
                        break;
                    }
                }
            }
            if(bo) break;
        }
    }
    for(int i=1;i<=n;i++) printf("%d ",ans[i]);
    printf("\n");
    return 0;
}


#include <iostream>
#include <fstream>
#include <string.h>
#include <stdio.h>

using namespace std;

const int MAX=310;

int pre[MAX][MAX];
int table[MAX][3];

int main()
{
   // freopen("in.txt","r",stdin);
    int n;
    cin>>n;
    memset(table,-1,sizeof(table));
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++) scanf("%d",&pre[i][j]);
    }
    for(int i=1;i<=n;i++) scanf("%d",&pre[n+1][i]);
    table[1][1]=1;
    for(int i=2;i<=n;i++)
    {
        int s=1;
        for(int j=1;j<=n;j++)
        {
            if(pre[n+1][j]<i)
            {
                table[i][s]=j; break;
            }
            if(pre[n+1][j]==i)  table[i][s++]=i;
        }
    }
    int ans[MAX];
    for(int i=1;i<=n;i++)
    {
        bool bo=false;
        for(int j=1;j<=n;j++)
        {
            if(pre[i][j]==i) continue;
            for(int k=1;k<=n;k++)
            {
                if(table[k][1]==i)
                {
                    if(table[k][2]==pre[i][j])
                    {
                        ans[i]=pre[i][j];
                        bo=true;
                        break;
                    }
                }
                else
                {
                    if(table[k][1]==pre[i][j])
                    {
                        ans[i]=pre[i][j];
                        bo=true;
                        break;
                    }
                }
            }
            if(bo) break;
        }
    }
    for(int i=1;i<=n;i++) printf("%d ",ans[i]);
    printf("\n");
    return 0;
}


#include <iostream>
#include <fstream>
#include <string.h>
#include <stdio.h>

using namespace std;

const int MAX=310;

int pre[MAX][MAX];
int table[MAX][3];

int main()
{
 //   freopen("in.txt","r",stdin);
    int n;
    cin>>n;
    memset(table,-1,sizeof(table));
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++) scanf("%d",&pre[i][j]);
    }
    for(int i=1;i<=n;i++) scanf("%d",&pre[n+1][i]);
    table[1][1]=1;
    for(int i=2;i<=n;i++)
    {
        int s=1;
        for(int j=1;j<=n;j++)
        {
            if(pre[n+1][j]<=i)
            {
                table[i][s++]=pre[n+1][j];
//                if(i==4) cout<<table[4][s-1]<<endl;
                if(s>2) break;
            }
            //if(pre[n+1][j]==i)  table[i][s++]=i;
        }
    }
//    cout<<table[4][2]<<endl;
    int ans[MAX];
    for(int i=1;i<=n;i++)
    {
        bool bo=false;
        for(int j=1;j<=n;j++)
        {
            if(pre[i][j]==i) continue;
            for(int k=1;k<=n;k++)
            {
                if(table[k][1]==i)
                {
                    if(table[k][2]==pre[i][j])
                    {
                        ans[i]=pre[i][j];
                        bo=true;
                        break;
                    }
                }
                else
                {
                    if(table[k][1]==pre[i][j])
                    {
                        ans[i]=pre[i][j];
                        bo=true;
                        break;
                    }
                }
            }
            if(bo) break;
        }
    }
    for(int i=1;i<=n;i++) printf("%d ",ans[i]);
    printf("\n");
    return 0;
}


#include <map>
#include <cmath>
#include <cstdio>
#include <vector>
#include <string>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <fstream>
#include <iostream>
#include <algorithm>
#define ED end()
#define FF first
#define SS second
#define BG begin()
#define LL long long
#define PB push_back
#define MP make_pair
#define SZ(x) ((int)((x).size()))
#define ran(n) (((rand() << 15) + rand()) % (n) + 1)
#define FOR(i, l, r) for (int i = (l); i <= (r); i++)
#define ROF(i, r, l) for (int i = (r); i >= (l); i--)
#define foreach(i, x) for (__typeof((x).begin()) i = (x).begin(); i != (x).end(); i++)
using namespace std;

const int maxn = 305;

int n, a[maxn][maxn], f[10][maxn], pos[maxn];

int get(int l, int r)
{
    if (l > r) return n + 1;
    int k = (int)trunc(log(r - l + 1) / log(2));
    return min(f[k][l], f[k][r - (1 << k) + 1]);
}

int main()
{
//    freopen("B.in", "r", stdin);
//    freopen("B.out", "w", stdout);
    scanf("%d", &n);
    FOR(i, 1, n)
        FOR(j, 1, n)
            scanf("%d", &a[i][j]);
    FOR(i, 1, n) { scanf("%d", &f[0][i]); pos[f[0][i]] = i; }
    FOR(j, 1, trunc(log(n) / log(2)) + 1e-6)
        FOR(i, 1, n - (1 << j) + 1)
            f[j][i] = min(f[j - 1][i], f[j - 1][i + (1 << (j - 1))]);
    FOR(i, 1, n)
    {
        int j;
        FOR(c, 1, n)
        if (a[i][c] != i)
        {
            int k; j = a[i][c];
            if (pos[i] < pos[j]) k = min(get(1, pos[i] - 1), get(pos[i] + 1, pos[j]));
            else k = get(1, pos[j]);
            if (k >= j) break;
        }
        printf("%d ", j);
    }
    return 0;
}


#include <map>
#include <cmath>
#include <cstdio>
#include <vector>
#include <string>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <fstream>
#include <iostream>
#include <algorithm>
#define ED end()
#define FF first
#define SS second
#define BG begin()
#define LL long long
#define PB push_back
#define MP make_pair
#define SZ(x) ((int)((x).size()))
#define ran(n) (((rand() << 15) + rand()) % (n) + 1)
#define FOR(i, l, r) for (int i = (l); i <= (r); i++)
#define ROF(i, r, l) for (int i = (r); i >= (l); i--)
#define foreach(i, x) for (__typeof((x).begin()) i = (x).begin(); i != (x).end(); i++)
using namespace std;

const int maxn = 305;

int n, a[maxn][maxn], f[10][maxn], pos[maxn];

int get(int l, int r)
{
    if (l > r) return n + 1;
    int k = (int)trunc(log(r - l + 1) / log(2));
    return min(f[k][l], f[k][r - (1 << k) + 1]);
}

int main()
{
//    freopen("B.in", "r", stdin);
//    freopen("B.out", "w", stdout);
    scanf("%d", &n);
    FOR(i, 1, n)
        FOR(j, 1, n)
            scanf("%d", &a[i][j]);
    FOR(i, 1, n) { scanf("%d", &f[0][i]); pos[f[0][i]] = i; }
    FOR(i, 1, trunc(log(n) / log(2)))
        FOR(j, 1, n - (1 << i) + 1)
            f[j][i] = min(f[j - 1][i], f[j - 1][i + (1 << (j - 1)) + 1]);
    FOR(i, 1, n)
    {
        int j;
        FOR(c, 1, n)
        if (a[i][c] != i)
        {
            int k; j = a[i][c];
            if (pos[i] < pos[j]) k = min(get(1, pos[i] - 1), get(pos[i] + 1, pos[j]));
            else k = get(1, pos[j]);
            if (k >= j) break;
        }
        printf("%d ", j);
    }
    return 0;
}


#include <map>
#include <cmath>
#include <cstdio>
#include <vector>
#include <string>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <fstream>
#include <iostream>
#include <algorithm>
#define ED end()
#define FF first
#define SS second
#define BG begin()
#define LL long long
#define PB push_back
#define MP make_pair
#define SZ(x) ((int)((x).size()))
#define ran(n) (((rand() << 15) + rand()) % (n) + 1)
#define FOR(i, l, r) for (int i = (l); i <= (r); i++)
#define ROF(i, r, l) for (int i = (r); i >= (l); i--)
#define foreach(i, x) for (__typeof((x).begin()) i = (x).begin(); i != (x).end(); i++)
using namespace std;

const int maxn = 305;

int n, a[maxn][maxn], f[10][maxn], pos[maxn];

int get(int l, int r)
{
    if (l > r) return n + 1;
    int k = (int)trunc(log(r - l + 1) / log(2));
    return min(f[k][l], f[k][r - (1 << k) + 1]);
}

int main()
{
//    freopen("B.in", "r", stdin);
//    freopen("B.out", "w", stdout);
    scanf("%d", &n);
    FOR(i, 1, n)
        FOR(j, 1, n)
            scanf("%d", &a[i][j]);
    FOR(i, 1, n) { scanf("%d", &f[0][i]); pos[f[0][i]] = i; }
    FOR(j, 1, trunc(log(n) / log(2)) + 1e-6)
        FOR(i, 1, n - (1 << j) + 1)
            f[j][i] = min(f[j - 1][i], f[j - 1][i + (1 << (j - 1)) + 1]);
    FOR(i, 1, n)
    {
        int j;
        FOR(c, 1, n)
        if (a[i][c] != i)
        {
            int k; j = a[i][c];
            if (pos[i] < pos[j]) k = min(get(1, pos[i] - 1), get(pos[i] + 1, pos[j]));
            else k = get(1, pos[j]);
            if (k >= j) break;
        }
        printf("%d ", j);
    }
    return 0;
}


#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <queue>
#include <string>
#include <memory.h>
#include <iterator>
#define ll long long
#define ull unsigned long long
#define ul unsigned long
#define ld double
#define lld long double
#define mem(A,X) memset(A,X,sizeof(A))
#define memo(A) memset(A,0,sizeof(A))
#define forn(I,B) for (int I=1;I<=(B);I++)
#define forg(H,V) for (int H=first[V];h;h=next[H])
#define rep(I,B) for (int I=0;I<(B);I++)
#define labs(X) (((X)>0)?(X):(-(X)))
#define ropen(X) freopen(X,"r",stdin)
#define wopen(X) freopen(X,"w",stdout)
#define vi vector<int>
#define pii pair<int,int>
#define pll pair<long long,long long>
#define pb push_back
#define mp make_pair
#define all(X) (X).begin(),(X).end()
#define N 333
#define INF 1111111111

using namespace std;

int a[N][N],n,c[N][N];
int d[N],ans[N],rank[N];

int main(){
//	ropen("input.txt");
	scanf("%d",&n);
	forn(i,n) forn(j,n) scanf("%d",&a[i][j]);
	forn(i,n) forn(j,n) c[i][a[i][j]]=j;
	forn(i,n) scanf("%d",&d[i]);
	forn(i,n) rank[d[i]]=i;
	forn(i,n){
		int cur=1,f=INF,sec=-1,fs=INF;
		forn(j,n){
			if (rank[j]<rank[cur]){
				sec=cur;
				cur=j;
			}
			if (j!=cur && sec==-1) sec=j;
			if (c[i][cur]<f && cur!=i){
				f=c[i][cur];
				ans[i]=cur;
			} else {
				if (sec!=-1 && cur==i){
					if (c[i][sec]<f && sec!=i){
						f=c[i][sec];
						ans[i]=sec;
					}
				}
			}

		}
	}
	forn(i,n) cout<<ans[i]<<" ";
}


#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <queue>
#include <string>
#include <memory.h>
#include <iterator>
#define ll long long
#define ull unsigned long long
#define ul unsigned long
#define ld double
#define lld long double
#define mem(A,X) memset(A,X,sizeof(A))
#define memo(A) memset(A,0,sizeof(A))
#define forn(I,B) for (int I=1;I<=(B);I++)
#define forg(H,V) for (int H=first[V];h;h=next[H])
#define rep(I,B) for (int I=0;I<(B);I++)
#define labs(X) (((X)>0)?(X):(-(X)))
#define ropen(X) freopen(X,"r",stdin)
#define wopen(X) freopen(X,"w",stdout)
#define vi vector<int>
#define pii pair<int,int>
#define pll pair<long long,long long>
#define pb push_back
#define mp make_pair
#define all(X) (X).begin(),(X).end()
#define N 333
#define INF 1111111111

using namespace std;

int a[N][N],n,c[N][N];
int d[N],ans[N],rank[N];

int main(){
//	ropen("input.txt");
	scanf("%d",&n);
	forn(i,n) forn(j,n) scanf("%d",&a[i][j]);
	forn(i,n) forn(j,n) c[i][a[i][j]]=j;
	forn(i,n) scanf("%d",&d[i]);
	forn(i,n) rank[d[i]]=i;
	forn(i,n){
		int cur=1,f=INF;
		forn(j,n){
			if (rank[j]<rank[cur]) cur=j;
			if (c[i][cur]<f && cur!=i){
				f=c[i][cur];
				ans[i]=cur;
			}
		}
	}
	forn(i,n) cout<<ans[i]<<" ";
}


#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <queue>
#include <string>
#include <memory.h>
#include <iterator>
#define ll long long
#define ull unsigned long long
#define ul unsigned long
#define ld double
#define lld long double
#define mem(A,X) memset(A,X,sizeof(A))
#define memo(A) memset(A,0,sizeof(A))
#define forn(I,B) for (int I=1;I<=(B);I++)
#define forg(H,V) for (int H=first[V];h;h=next[H])
#define rep(I,B) for (int I=0;I<(B);I++)
#define labs(X) (((X)>0)?(X):(-(X)))
#define ropen(X) freopen(X,"r",stdin)
#define wopen(X) freopen(X,"w",stdout)
#define vi vector<int>
#define pii pair<int,int>
#define pll pair<long long,long long>
#define pb push_back
#define mp make_pair
#define all(X) (X).begin(),(X).end()
#define N 333
#define INF 1111111111

using namespace std;

int a[N][N],n,c[N][N];
int d[N],ans[N],rank[N];

int main(){
//	ropen("input.txt");
	scanf("%d",&n);
	forn(i,n) forn(j,n) scanf("%d",&a[i][j]);
	forn(i,n) forn(j,n) c[i][a[i][j]]=j;
	forn(i,n) scanf("%d",&d[i]);
	forn(i,n) rank[d[i]]=i;
	forn(i,n){
		int cur=1,f=INF,sec=-1,fs=INF;
		forn(j,n){
			if (rank[j]<rank[cur]){
				sec=cur;
				cur=j;
			}
			if (j!=cur && sec==-1) sec=j;
			if (c[i][cur]<f && cur!=i){
				f=c[i][cur];
				ans[i]=cur;
			}
			if (sec!=-1){
				if (c[i][sec]<f && sec!=i){
					f=c[i][sec];
					ans[i]=sec;
				}
			}

		}
	}
	forn(i,n) cout<<ans[i]<<" ";
}


#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <queue>
#include <string>
#include <memory.h>
#include <iterator>
#define ll long long
#define ull unsigned long long
#define ul unsigned long
#define ld double
#define lld long double
#define mem(A,X) memset(A,X,sizeof(A))
#define memo(A) memset(A,0,sizeof(A))
#define forn(I,B) for (int I=1;I<=(B);I++)
#define forg(H,V) for (int H=first[V];h;h=next[H])
#define rep(I,B) for (int I=0;I<(B);I++)
#define labs(X) (((X)>0)?(X):(-(X)))
#define ropen(X) freopen(X,"r",stdin)
#define wopen(X) freopen(X,"w",stdout)
#define vi vector<int>
#define pii pair<int,int>
#define pll pair<long long,long long>
#define pb push_back
#define mp make_pair
#define all(X) (X).begin(),(X).end()
#define N 333
#define INF 1111111111

using namespace std;

int a[N][N],n,c[N][N];
int d[N],ans[N],rank[N],num[N];

int main(){
//	ropen("input.txt");
	scanf("%d",&n);
	forn(i,n) forn(j,n) scanf("%d",&a[i][j]);
	forn(i,n) forn(j,n) c[i][a[i][j]]=j;
	forn(i,n) scanf("%d",&d[i]);
	forn(i,n) rank[d[i]]=i;
	forn(i,n){
		forn(j,n){
			int q=0;
			forn(k,i) if ((rank[k]<rank[q] || !q) && k!=j) q=k;
			if (!q) continue;
			if (!ans[j] || c[j][q]<ans[j]){
				num[j]=i;
				ans[j]=c[j][q];
			}
		}
	}
	forn(i,n) cout<<num[i]<<" ";
	return 0;
}


#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <queue>
#include <string>
#include <memory.h>
#include <iterator>
#define ll long long
#define ull unsigned long long
#define ul unsigned long
#define ld double
#define lld long double
#define mem(A,X) memset(A,X,sizeof(A))
#define memo(A) memset(A,0,sizeof(A))
#define forn(I,B) for (int I=1;I<=(B);I++)
#define forg(H,V) for (int H=first[V];h;h=next[H])
#define rep(I,B) for (int I=0;I<(B);I++)
#define labs(X) (((X)>0)?(X):(-(X)))
#define ropen(X) freopen(X,"r",stdin)
#define wopen(X) freopen(X,"w",stdout)
#define vi vector<int>
#define pii pair<int,int>
#define pll pair<long long,long long>
#define pb push_back
#define mp make_pair
#define all(X) (X).begin(),(X).end()
#define N 333
#define INF 1111111111

using namespace std;

int a[N][N],n,c[N][N];
int d[N],ans[N],rank[N];

int main(){
	ropen("input.txt");
	scanf("%d",&n);
	forn(i,n) forn(j,n) scanf("%d",&a[i][j]);
	forn(i,n) forn(j,n) c[i][a[i][j]]=j;
	forn(i,n) scanf("%d",&d[i]);
	forn(i,n) rank[d[i]]=i;
	forn(i,n){
		int cur=1,f=INF,sec=-1,fs=INF;
		forn(j,n){
			if (rank[j]<rank[cur]){
				sec=cur;
				cur=j;
			}
			if (j!=cur && sec==-1) sec=j;
			if (c[i][cur]<f && cur!=i){
				f=c[i][cur];
				ans[i]=cur;
			}
			if (sec!=-1){
				if (c[i][sec]<f && sec!=i){
					f=c[i][sec];
					ans[i]=sec;
				}
			}

		}
	}
	forn(i,n) cout<<ans[i]<<" ";
}


#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <queue>
#include <string>
#include <memory.h>
#include <iterator>
#define ll long long
#define ull unsigned long long
#define ul unsigned long
#define ld double
#define lld long double
#define mem(A,X) memset(A,X,sizeof(A))
#define memo(A) memset(A,0,sizeof(A))
#define forn(I,B) for (int I=1;I<=(B);I++)
#define forg(H,V) for (int H=first[V];h;h=next[H])
#define rep(I,B) for (int I=0;I<(B);I++)
#define labs(X) (((X)>0)?(X):(-(X)))
#define ropen(X) freopen(X,"r",stdin)
#define wopen(X) freopen(X,"w",stdout)
#define vi vector<int>
#define pii pair<int,int>
#define pll pair<long long,long long>
#define pb push_back
#define mp make_pair
#define all(X) (X).begin(),(X).end()
#define N 333
#define INF 1111111111

using namespace std;

int a[N][N],n,c[N][N];
int d[N],ans[N],rank[N];

int main(){
//	ropen("input.txt");
	scanf("%d",&n);
	forn(i,n) forn(j,n) scanf("%d",&a[i][j]);
	forn(i,n) forn(j,n) c[i][a[i][j]]=j;
	forn(i,n) scanf("%d",&d[i]);
	forn(i,n) rank[d[i]]=i;
	forn(i,n){
		int cur=1,f=INF,sec=-1,fs=INF;
		forn(j,n){
			if (rank[j]<rank[cur]){
				sec=cur;
				cur=j;
			}
			if (j!=cur && sec==-1) sec=j;
			if (c[i][cur]<f && cur!=i){
				f=c[i][cur];
				ans[i]=cur;
			}
			if (sec!=-1){
				if (c[i][sec]<fs && sec!=i){
					fs=c[i][sec];
					ans[i]=sec;
				}
			}

		}
	}
	forn(i,n) cout<<ans[i]<<" ";
}


// B. New Year Cards
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    int n, t;
    static int a[320][320], p[320], ans[320];

    cin >> n;
    for (int i = 0; i < n; i++)
        for (int j = 0, k = 0; j < n; j++) {
            cin >> t;
            if (--t != i) a[i][k++] = t;
        }
    for (int i = 0; i < n; i++) {
        cin >> t;
        p[--t] = i;
    }

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n - 1; j++) {
            bool found = true;
            for (int k = j + 1; k < n - 1; k++)
                if (a[i][j] > a[i][k] && p[a[i][j]] > p[a[i][k]]) {
                    found = false;
                    break;
                }
            if (found) {
                cout << a[i][j] + 1 << ' ';
                break;
            }
        }
    }
    cout << endl;

    return 0;
}


// B. New Year Cards
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    int n, t;
    static int a[320][320], p[320], ans[320];

    cin >> n;
    for (int i = 0; i < n; i++)
        for (int j = 0, k = 0; j < n; j++) {
            cin >> t;
            if (--t != i) a[i][k++] = t;
        }
    for (int i = 0; i < n; i++) {
        cin >> t;
        p[--t] = i;
    }

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n - 1; j++) {
            bool found = true;
            for (int k = j + 1; k < n - 1; k++)
                if (a[i][j] > a[i][k] && p[a[i][j]] > p[a[i][k]]) {
                    found = false;
                    break;
                }
            if (found) {
                cout << ' ' << a[i][j] + 1 << '  ';
                break;
            }
        }
    }
    cout << endl;

    return 0;
}


// B. New Year Cards
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    int n, t;
    static int a[320][320], p[320], ans[320];

    cin >> n;
    for (int i = 0; i < n; i++)
        for (int j = 0, k = 0; j < n; j++) {
            cin >> t;
            if (--t != i) a[i][k++] = t;
        }
    for (int i = 0; i < n; i++) {
        cin >> t;
        p[--t] = i;
    }

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n - 1; j++) {
            bool found = true;
            for (int k = j + 1; k < n - 1; k++)
                if (a[i][j] > a[i][k] && p[a[i][j]] > p[a[i][k]]) {
                    found = false;
                    break;
                }
            if (found) {
                cout << ' ' << a[i][j] + 1 << "   ";
                break;
            }
        }
    }
    cout << endl;

    return 0;
}


// B. New Year Cards
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    int n, t;
    static int a[320][320], p[320], ans[320];

    cin >> n;
    for (int i = 0; i < n; i++)
        for (int j = 0, k = 0; j < n; j++) {
            cin >> t;
            if (--t != i) a[i][k++] = t;
        }
    for (int i = 0; i < n; i++) {
        cin >> t;
        p[--t] = i;
    }

    for (int i = 0; i < n; i++) {
        int j;
        for (j = 1; j < n - 1; j++) {
            if (a[i][j] < a[i][j - 1] && p[a[i][j]] < p[a[i][j - 1]])
                continue;
            break;
        }
        cout << a[i][j - 1] + 1 << ' ';
    }
    cout << endl;

    return 0;
}


#pragma comment(linker, "/STACK:10000000")
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <set>
#include <map>
#include <cstdio>
#include <cassert>
#include <string>
#include <cstring>
#define ldb long double
#define LL long long
#define fi first
#define se second
#define fill(a, c) memset(a, c, sizeof(a))
#define sqr(a) ((a) * (a))
#define nextLine() {int c = 0; while((c = getchar()) != 10 && c != EOF);}
#define getBit(mask, k) (((mask) / pw[k]) % pw[1])
#define setBit(mask, k, l) (((mask) / pw[k + 1] * pw[1] + (l)) * pw[k] + ((mask) % pw[k]))
#define debug(a) cerr << #a << " = " << a << " ";
#define debugl(a) cerr << #a << " = " << a << "\n";
#define mp(a, b) make_pair(a, b)
#define pb(a) push_back(a)
#define ff first
#define ss second
const ldb LDINF = 9128739847123.00;
const ldb eps = 1e-9;
const int inf = 1 << 28;
const ldb pi = fabsl(atan2l(0.0, -1.0));
using namespace std;

vector <int> a[333];
int pos[333];
int pr[333];
int prior[333];
int n;

class compare
{
	public:
		bool operator () (int a, int b)
		{
			return prior[a] < prior[b];
		}
};

set <int, compare> was;

void load()
{
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		a[i].resize(n);
		for (int j = 0; j < n; j++)
		{
			scanf("%d", &a[i][j]);
			a[i][j]--;
		}
	}
	for (int i = 0; i < n; i++)
	{
		cin >> pr[i];
		pr[i]--;
		prior[pr[i]] = i;
	}
}

int res[333];

int get_min(int j)
{
	if (*was.begin() == j)
	{
		set <int, compare> :: iterator it = was.begin();
		it++;
		if (it == was.end()) return inf;
		return *(it);
	}
	return *was.begin();
}

void solve()
{
	memset(res, -1, sizeof res);
	for (int i = 0; i < n; i++)
	{
		was.insert(i);
	//	cerr << was.size() << "\n";
		int score = inf;
		int mj = -1;
		int ml = -1;
		for (int j = 0; j < n; j++)
		{
			if (res[j] != -1) continue;
			int l = get_min(j);
			if (l == inf) continue;
			int cur = 0;
			for (int k = 0; k < n; k++)
			{
				if (a[j][k] == l)
					break;
				//if (a[j][k] <= i) continue;
				cur++;
			}
		//	cerr << "l = " << l << "\n";
		//	cerr << "cur = " << cur << "\n";
			if (cur < score)
			{
				score = cur;
				mj = j;
				ml = l;
			}
		}
	//	cerr << score << " " << mj << " " << ml << "\n";
		if (mj != -1)
			res[mj] = ml;
		else throw 42;
	}
	for (int i = 0; i < n; i++)
		cout << res[i] + 1 << " ";
}

#define file "b"
int main()
{
	#ifndef ONLINE_JUDGE
		freopen(file".in", "rt", stdin);
		freopen(file".out", "wt", stdout);
	#endif
	load();
	solve();
	return 0;
}


#pragma comment(linker, "/STACK:10000000")
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <set>
#include <map>
#include <cstdio>
#include <cassert>
#include <string>
#include <cstring>
#define ldb long double
#define LL long long
#define fi first
#define se second
#define fill(a, c) memset(a, c, sizeof(a))
#define sqr(a) ((a) * (a))
#define nextLine() {int c = 0; while((c = getchar()) != 10 && c != EOF);}
#define getBit(mask, k) (((mask) / pw[k]) % pw[1])
#define setBit(mask, k, l) (((mask) / pw[k + 1] * pw[1] + (l)) * pw[k] + ((mask) % pw[k]))
#define debug(a) cerr << #a << " = " << a << " ";
#define debugl(a) cerr << #a << " = " << a << "\n";
#define mp(a, b) make_pair(a, b)
#define pb(a) push_back(a)
#define ff first
#define ss second
const ldb LDINF = 9128739847123.00;
const ldb eps = 1e-9;
const int inf = 1 << 28;
const ldb pi = fabsl(atan2l(0.0, -1.0));
using namespace std;

vector <int> a[333];
int pos[333];
int pr[333];
int prior[333];
int n;

class compare
{
	public:
		bool operator () (int a, int b)
		{
			return prior[a] < prior[b];
		}
};

set <int, compare> was;

void load()
{
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		a[i].resize(n);
		for (int j = 0; j < n; j++)
		{
			scanf("%d", &a[i][j]);
			a[i][j]--;
		}
	}
	for (int i = 0; i < n; i++)
	{
		cin >> pr[i];
		pr[i]--;
		prior[pr[i]] = i;
	}
}

int res[333];

int get_min(int j)
{
	if (*was.begin() == j)
	{
		set <int, compare> :: iterator it = was.begin();
		it++;
		if (it == was.end()) return inf;
		return *(it);
	}
	return *was.begin();
}

void solve()
{
	memset(res, -1, sizeof res);
	for (int i = 0; i < n; i++)
	{
		was.insert(i);
	//	cerr << was.size() << "\n";
		int score = inf;
		int mj = -1;
		int ml = -1;
		for (int j = 0; j < n; j++)
		{
			if (res[j] != -1) continue;
			int l = get_min(j);
			if (l == inf) continue;
			int cur = 0;
			for (int k = 0; k < n; k++)
			{
				if (a[j][k] == l)
					break;
				if (a[j][k] <= i) continue;
				cur++;
			}
		//	cerr << "l = " << l << "\n";
		//	cerr << "cur = " << cur << "\n";
			if (cur < score)
			{
				score = cur;
				mj = j;
				ml = l;
			}
		}
	//	cerr << score << " " << mj << " " << ml << "\n";
		if (mj != -1)
			res[mj] = ml;
		else throw 42;
	}
	for (int i = 0; i < n; i++)
		cout << res[i] + 1 << " ";
}

#define file "b"
int main()
{
	#ifndef ONLINE_JUDGE
		freopen(file".in", "rt", stdin);
		freopen(file".out", "wt", stdout);
	#endif
	load();
	solve();
	return 0;
}


#pragma comment(linker, "/STACK:10000000")
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <set>
#include <map>
#include <cstdio>
#include <cassert>
#include <string>
#include <cstring>
#define ldb long double
#define LL long long
#define fi first
#define se second
#define fill(a, c) memset(a, c, sizeof(a))
#define sqr(a) ((a) * (a))
#define nextLine() {int c = 0; while((c = getchar()) != 10 && c != EOF);}
#define getBit(mask, k) (((mask) / pw[k]) % pw[1])
#define setBit(mask, k, l) (((mask) / pw[k + 1] * pw[1] + (l)) * pw[k] + ((mask) % pw[k]))
#define debug(a) cerr << #a << " = " << a << " ";
#define debugl(a) cerr << #a << " = " << a << "\n";
#define mp(a, b) make_pair(a, b)
#define pb(a) push_back(a)
#define ff first
#define ss second
const ldb LDINF = 9128739847123.00;
const ldb eps = 1e-9;
const int inf = 1 << 28;
const ldb pi = fabsl(atan2l(0.0, -1.0));
using namespace std;

vector <int> a[333];
int pos[333];
int pr[333];
int prior[333];
int n;

class compare
{
	public:
		bool operator () (int a, int b)
		{
			return prior[a] < prior[b];
		}
};

set <int, compare> was;

void load()
{
	cin >> n;
	int x;
	for (int i = 0; i < n; i++)
	{
		a[i].resize(n);
		for (int j = 0; j < n; j++)
		{
			scanf("%d", &x);
			x--;
			a[i][x] = j;
		}
	}
	for (int i = 0; i < n; i++)
	{
		cin >> pr[i];
		pr[i]--;
		prior[pr[i]] = i;
	}
}

int res[333];

void solve()
{
	memset(res, -1, sizeof res);
	for (int i = 0; i < n; i++)
	{
		int cur = inf;
		int mj = -1;
		int min_v = inf;
		for (int j = 0; j < n; j++)
		{
			if (j == i) continue;
			if (min_v > prior[j])
			{
				if (cur > a[i][j])
				{
					cur = a[i][j];
					mj = j;
				}
			}
			min_v = min(min_v, prior[j]);
		}
		res[i] = mj;
	}
	for (int i = 0; i < n; i++)
		cout << res[i] + 1 << " ";
}

#define file "b"
int main()
{
	#ifndef ONLINE_JUDGE
		freopen(file".in", "rt", stdin);
		freopen(file".out", "wt", stdout);
	#endif
	load();
	solve();
	return 0;
}


#include <cstdio>
#include <iostream>
#include <cmath>
#include <cassert>
#include <algorithm>
#include <ctime>
#include <cctype>
#include <cstdlib>
#include <string>
#include <cstring>
#include <map>
#include <set>
#include <vector>

#define eps 1e-9
#define inf int(2e9)
#define inflong (long long)(2e18)
#define sqr(a) ((a) * (a))
#define abs(a) (((a) < 0) ? -(a) : (a))
#define sz(a) (int)a.size()
#define all(a) (a).begin(), (a).end()
#define zero(a) memset(a, 0, sizeof(a))
#define fst first
#define snd second
#define y1 osrughosduvgarligybakrybrogvba
#define y0 aosfigdalrowgyalsouvgrlvygalri
#define mp make_pair
#define pb push_back
#define taskname "text"

using namespace std;

typedef long long LL;
typedef long double LD;
typedef vector <int> vi;
typedef vector <vi> vvi;
typedef vector <bool> vb;
typedef vector <vb> vvb;
typedef vector <LL> vl;
typedef vector <vl> vvl;
typedef pair<int, int> pii;

const int MaxN = 400;

int n;
int a[MaxN][MaxN];
int b[MaxN];
vi ans[MaxN];
int p[MaxN];
int maxx[MaxN], answer[MaxN];

bool Less(int i, int j)
{
  return p[i] < p[j];
}

int main()
{
  #ifdef LocalHost
    freopen(taskname".in", "r", stdin);
    freopen(taskname".out", "w", stdout);
  #endif
  while (scanf("%d", &n) >= 1)
  {
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++)
        scanf("%d", &a[i][j]);
    for (int i = 1; i <= n; i++)
    {
      scanf("%d", &b[i]);
      p[b[i]] = i;
    }
    vi taked;
    for (int i = 1; i <= n; i++)
      maxx[i] = -inf, answer[i] = -1;
    for (int i = 1; i <= n; i++)
    {
      taked.pb(i);
      sort(all(taked), Less);
      for (int j = 1; j <= n; j++)
      {
        if (taked[0] == j)
          continue;
        if (maxx[j] < taked[0])
          maxx[j] = taked[0], answer[j] = i;
      }
    }
    for (int i = 1; i <= n; i++)
      printf("%d%c", answer[i], " \n"[i == n]);
  }
  return 0;
}


#include <cstdio>
#include <iostream>
#include <cmath>
#include <cassert>
#include <algorithm>
#include <ctime>
#include <cctype>
#include <cstdlib>
#include <string>
#include <cstring>
#include <map>
#include <set>
#include <vector>

#define eps 1e-9
#define inf int(2e9)
#define inflong (long long)(2e18)
#define sqr(a) ((a) * (a))
#define abs(a) (((a) < 0) ? -(a) : (a))
#define sz(a) (int)a.size()
#define all(a) (a).begin(), (a).end()
#define zero(a) memset(a, 0, sizeof(a))
#define fst first
#define snd second
#define y1 osrughosduvgarligybakrybrogvba
#define y0 aosfigdalrowgyalsouvgrlvygalri
#define mp make_pair
#define pb push_back
#define taskname "text"

using namespace std;

typedef long long LL;
typedef long double LD;
typedef vector <int> vi;
typedef vector <vi> vvi;
typedef vector <bool> vb;
typedef vector <vb> vvb;
typedef vector <LL> vl;
typedef vector <vl> vvl;
typedef pair<int, int> pii;

const int MaxN = 400;

int n;
int a[MaxN][MaxN];
int b[MaxN];
vi ans[MaxN];
int p[MaxN];
int maxx[MaxN], answer[MaxN];

bool Less(int i, int j)
{
  return p[i] < p[j];
}

int main()
{
  #ifdef LocalHost
    freopen(taskname".in", "r", stdin);
    freopen(taskname".out", "w", stdout);
  #endif
  while (scanf("%d", &n) >= 1)
  {
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++)
        scanf("%d", &a[i][j]);
    for (int i = 1; i <= n; i++)
    {
      scanf("%d", &b[i]);
      p[b[i]] = i;
    }
    vi taked;
    for (int i = 1; i <= n; i++)
      maxx[i] = -inf, answer[i] = -1;
    for (int i = 1; i <= n; i++)
    {
      taked.pb(i);
      sort(all(taked), Less);
      for (int j = 1; j <= n; j++)
      {
        if (taked[0] == j)
        {
          if (maxx[j] < taked[1])
            maxx[j] = taked[1], answer[j] = i;
          continue;
        }
        if (maxx[j] < taked[0])
          maxx[j] = taked[0], answer[j] = i;
      }
    }
    for (int i = 1; i <= n; i++)
      printf("%d%c", answer[i], " \n"[i == n]);
  }
  return 0;
}


#include <cstdio>
#include <iostream>
#include <cmath>
#include <cassert>
#include <algorithm>
#include <ctime>
#include <cctype>
#include <cstdlib>
#include <string>
#include <cstring>
#include <map>
#include <set>
#include <vector>

#define eps 1e-9
#define inf int(2e9)
#define inflong (long long)(2e18)
#define sqr(a) ((a) * (a))
#define abs(a) (((a) < 0) ? -(a) : (a))
#define sz(a) (int)a.size()
#define all(a) (a).begin(), (a).end()
#define zero(a) memset(a, 0, sizeof(a))
#define fst first
#define snd second
#define y1 osrughosduvgarligybakrybrogvba
#define y0 aosfigdalrowgyalsouvgrlvygalri
#define mp make_pair
#define pb push_back
#define taskname "text"

using namespace std;

typedef long long LL;
typedef long double LD;
typedef vector <int> vi;
typedef vector <vi> vvi;
typedef vector <bool> vb;
typedef vector <vb> vvb;
typedef vector <LL> vl;
typedef vector <vl> vvl;
typedef pair<int, int> pii;

const int MaxN = 400;

int n;
int a[MaxN][MaxN];
int b[MaxN];
vi ans[MaxN];
int p[MaxN];
int maxx[MaxN], answer[MaxN];

bool Less(int i, int j)
{
  return p[i] < p[j];
}

int main()
{
  #ifdef LocalHost
    freopen(taskname".in", "r", stdin);
    freopen(taskname".out", "w", stdout);
  #endif
  while (scanf("%d", &n) >= 1)
  {
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++)
        scanf("%d", &a[i][j]);
    for (int i = 1; i <= n; i++)
    {
      scanf("%d", &b[i]);
      p[b[i]] = i;
    }
    vi taked;
    for (int i = 1; i <= n; i++)
      maxx[i] = -inf, answer[i] = -1;
    for (int i = 1; i <= n; i++)
    {
      taked.pb(i);
      sort(all(taked), Less);
      for (int j = 1; j <= n; j++)
      {
        if (taked[0] == j)
          continue;
        if (maxx[j] < p[taked[0]])
          maxx[j] = p[taked[0]], answer[j] = i;
      }
    }
    for (int i = 1; i <= n; i++)
      printf("%d%c", answer[i], " \n"[i == n]);
  }
  return 0;
}


#include <cstdio>
#include <iostream>
#include <cmath>
#include <cassert>
#include <algorithm>
#include <ctime>
#include <cctype>
#include <cstdlib>
#include <string>
#include <cstring>
#include <map>
#include <set>
#include <vector>

#define eps 1e-9
#define inf int(2e9)
#define inflong (long long)(2e18)
#define sqr(a) ((a) * (a))
#define abs(a) (((a) < 0) ? -(a) : (a))
#define sz(a) (int)a.size()
#define all(a) (a).begin(), (a).end()
#define zero(a) memset(a, 0, sizeof(a))
#define fst first
#define snd second
#define y1 osrughosduvgarligybakrybrogvba
#define y0 aosfigdalrowgyalsouvgrlvygalri
#define mp make_pair
#define pb push_back
#define taskname "text"

using namespace std;

typedef long long LL;
typedef long double LD;
typedef vector <int> vi;
typedef vector <vi> vvi;
typedef vector <bool> vb;
typedef vector <vb> vvb;
typedef vector <LL> vl;
typedef vector <vl> vvl;
typedef pair<int, int> pii;

const int MaxN = 400;

int n;
int a[MaxN][MaxN];
int b[MaxN];
vi ans[MaxN];
int p[MaxN], pp[MaxN][MaxN];
int maxx[MaxN], answer[MaxN];

bool Less(int i, int j)
{
  return p[i] < p[j];
}

int main()
{
  #ifdef LocalHost
    freopen(taskname".in", "r", stdin);
    freopen(taskname".out", "w", stdout);
  #endif
  while (scanf("%d", &n) >= 1)
  {
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++)
      {
        scanf("%d", &a[i][j]);
        pp[i][a[i][j]] = n - j + 1;
      }
    for (int i = 1; i <= n; i++)
    {
      scanf("%d", &b[i]);
      p[b[i]] = i;
    }
    vi taked;
    for (int i = 1; i <= n; i++)
      maxx[i] = -inf, answer[i] = -1;
    for (int i = 1; i <= n; i++)
    {
      taked.pb(i);
      sort(all(taked), Less);
      for (int j = 1; j <= n; j++)
      {
        int tmp = taked[0];
        if (taked[0] == j)
        {
          if (sz(taked) == 1)
            continue;
          tmp = taked[1];
        }
        if (maxx[j] < pp[j][tmp])
          maxx[j] = pp[j][tmp], answer[j] = i;
      }
    }
    for (int i = 1; i <= n; i++)
      printf("%d%c", answer[i], " \n"[i == n]);
  }
  return 0;
}


#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <map>
#include <set>

using namespace std;

#define inf 1000000000
#define pb push_back

typedef vector<int> vint;
typedef pair<int, int> pii;

///******************///

int a[300][300]={};
int m[300], mm[300];

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

    for (int i=0; i<n; ++i)
        for (int j=0; j<n; ++j) {
            cin >> a[i][j];
            --a[i][j];
        }

    for (int i=0; i<n; ++i){
        cin >> m[i];
        --m[i];
        mm[m[i]]=i;
    }

    for (int i=0; i<n; ++i) {
        int cur=-1;
        for (int j=0; j<n; ++j) if (a[i][j] != i) {
            if (cur==-1) {
                bool b=1;
                for (int k=0; k<a[i][j] && b; ++k) if (mm[a[i][j]]>mm[k]) b=0;
                if (b) cur = a[i][j];
            } else {
                if (cur>a[i][j] && mm[cur] > mm[a[i][j]]) cur = a[i][j];
            }
        }
        cout << cur+1 << " ";
    }

    return 0;
}


#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <map>
#include <set>

using namespace std;

#define inf 1000000000
#define pb push_back

typedef vector<int> vint;
typedef pair<int, int> pii;

///******************///

int a[300][300]={};
int m[300], mm[300];

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

    for (int i=0; i<n; ++i)
        for (int j=0; j<n; ++j) {
            cin >> a[i][j];
            --a[i][j];
        }

    for (int i=0; i<n; ++i){
        cin >> m[i];
        --m[i];
        mm[m[i]]=i;
    }

    for (int i=0; i<n; ++i) {
        int cur=-1;
        for (int j=0; j<n; ++j) if (a[i][j] != i) {
            if (cur==-1) {
                bool b=1;
                for (int k=0; k<a[i][j] && b; ++k) if (mm[a[i][j]]>mm[k] && k!=i) b=0;
                if (b) cur = a[i][j];
            } else {
                if (cur>a[i][j] && mm[cur] > mm[a[i][j]]) cur = a[i][j];
            }
        }
        cout << cur+1 << " ";
    }

    return 0;
}


#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <map>
#include <set>

using namespace std;

#define inf 1000000000
#define pb push_back

typedef vector<int> vint;
typedef pair<int, int> pii;

///******************///

int a[300][300]={};
int m[300], mm[300];

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

    for (int i=0; i<n; ++i)
        for (int j=0; j<n; ++j) {
            cin >> a[i][j];
            --a[i][j];
        }

    for (int i=0; i<n; ++i){
        cin >> m[i];
        --m[i];
        mm[m[i]]=i;
    }

    for (int i=0; i<n; ++i) {
        int cur=-1;
        for (int j=0; j<n; ++j) if (a[i][j] != i) {
            if (cur==-1) cur = a[i][j];
            else if (cur>a[i][j] && mm[cur] > mm[a[i][j]]) cur = a[i][j];
        }
        cout << cur+1 << " ";
    }

    return 0;
}


#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <map>
#include <set>

using namespace std;

#define inf 1000000000

typedef vector<int> vint;
typedef pair<int, int> pii;

///******************///

/*int find(int L, int R, int key)
{
    if (a[(L+R)/2].second == key) {
        a[(L+R)/2].first++;
        return 0;
    }
    if (R==0) {
        a.push_back(make_pair(1, key));
        if (a[0].second > a[1].second) swap(a[0], a[1]);
        return 0;
    }
    if (L>=R-1) {
        if (a.back().second > key) a.push_back(a.back());
        else {
            a.push_back(make_pair(1, key));
            return 0;
        }

        int st;
        if (a[R].second > key) st=R; else st=L;
        for (int i=st; i<a.size()-2; ++i)
            a[i+1] = a[i];
        a[st]=make_pair(1, key);
        return 0;
    }
    if (a[(L+R)/2].second > key) {
        find(L, (L+R)/2-1, key);
    } else {
        find((L+R)/2+1, R, key);
    }
    return 0;
}*/

vector<int> b;
vector<pii> a, a1;

bool comp(pii a, pii b) {
    return a>b;
}

int main()
{
    //freopen("input.txt", "r", stdin);
    int n, r;
    cin >> n;
    /*for (int i=1; i<n; ++i) {
        cin >> r;
        find(0, a.size()-1, r);
    }*/
    for (int i=0; i<n; ++i) {
        cin >> r;
        b.push_back(r);
    }
    sort(b.begin(), b.end());
    for (int i=0; i<b.size(); ) {
        a.push_back(make_pair(1, b[i++]));
        while (i<b.size() && b[i]==a[a.size()-1].second) ++i, ++a[a.size()-1].first;
    }

    sort(a.begin(), a.end(), comp);
/*    for (int i=0; i<a.size(); ++i) {
        cout << a[i].first << " " << a[i].second << endl;
    }*/

    a1=a;

    int f1=0, f2=1, f3=2;
    int res=0, inc;

    while (f3<a.size()) {
        inc = min(min(a[f1].first, a[f2].first), a[f3].first);
        res+=inc;
        a[f1].first-=inc;
        a[f2].first-=inc;
        a[f3].first-=inc;
        if (a[f1].first==0) f1=f2, f2=f3, ++f3;
            if (f3>=a.size()) break;
        if (a[f1].first==0) f1=f2, f2=f3, ++f3;
            if (f3>=a.size()) break;
        if (a[f2].first==0)        f2=f3, ++f3;
    }
    cout << res << endl;

    f1=0, f2=1, f3=2;
    inc;

    while (f3<a.size()) {
        inc = min(min(a1[f1].first, a1[f2].first), a1[f3].first);
        a1[f1].first-=inc;
        a1[f2].first-=inc;
        a1[f3].first-=inc;
        for (int j=0; j<inc; ++j) printf("%d %d %d\n", a1[f1].second, a1[f2].second, a1[f3].second);
        if (a1[f1].first==0) f1=f2, f2=f3, ++f3;
        if (a1[f1].first==0) f1=f2, f2=f3, ++f3;
        if (a1[f2].first==0)        f2=f3, ++f3;
    }

    return 0;
}
/**
14
1 3 6 4 2 6 4 2 4 2 5 5 5 5
*/


int DBG = 0;
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cassert>
#include <ctime>
#include <algorithm>
#include <sstream>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <bitset>
using namespace std;

#define mp make_pair
#define pb push_back
#define sz(a) int(a.size())
#define Forp(i, a, b) for (int i = a; i <= b; ++i)
#define Forc(i, a, b) for (int i = a; i < b; ++i)
#define Ford(i, a, b) for (int i = a; i >= b; --i)
#define Forv(i, x) for (__typeof(x.begin()) i = x.begin(); i != x.end(); ++i)
#define dprintf if (DBG) printf
#define mset(a) memset(a, 0, sizeof(a))
#define dprintf if (DBG) printf

typedef long long ll;
typedef unsigned long long ull;
typedef vector <int> vi;
typedef pair <int, int> pii;

const double eps = 1e-9;
const int oo = 1000000000;
const int N = 512;

int n, value[N][N], order[N];

int cost[N][N];

int main() {
//    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);

    scanf("%d", &n);
    Forp(mfriend, 1, n) {
        Forp(val, 1, n) {
            int card; scanf("%d", &card); value[mfriend][card] = val;
        }
        value[mfriend][mfriend] = oo;
    }
    Forp(i, 1, n) scanf("%d", &order[i]);

    Forp(mtime, 1, n) Forp(mfriend, 1, n) {
        cost[mfriend][mtime] = oo;
        Forp(i, 1, n) {
            int card = order[i];
            if (card == mfriend || card > mtime) continue;
            cost[mfriend][mtime] = value[mfriend][card];
            break;
        }
    }

    Forp(mfriend, 1, n) {
        int res = 0;
        Forp(mtime, 1, n)
            if (res == 0 || cost[mfriend][mtime] < cost[mfriend][res])
                res = mtime;
        cout << res << ' ';
    }

    return 0;
}


int DBG = 0;
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cassert>
#include <ctime>
#include <algorithm>
#include <sstream>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <bitset>
using namespace std;

#define mp make_pair
#define pb push_back
#define sz(a) int(a.size())
#define Forp(i, a, b) for (int i = a; i <= b; ++i)
#define Forc(i, a, b) for (int i = a; i < b; ++i)
#define Ford(i, a, b) for (int i = a; i >= b; --i)
#define Forv(i, x) for (__typeof(x.begin()) i = x.begin(); i != x.end(); ++i)
#define mset(a) memset(a, 0, sizeof(a))
#define dprintf if (DBG) printf

typedef long long ll;
typedef unsigned long long ull;
typedef vector <int> vi;
typedef pair <int, int> pii;

const double eps = 1e-9;
const int oo = 1000000000;
const int N = 128;

int n, a[N];

int main() {
//    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);

    scanf("%d", &n);
    Forp(i, 1, n) scanf("%d", &a[i]);

    sort(a + 1, a + n + 1);
    a[0] = 10;
    Forp(i, 1, n) a[i] += a[i-1];

    int num = 0, pen = 0;
    Forp(i, 1, n) {
        if (a[i] > 720) break;
        num++;
        if (a[i] > 360) pen += a[i] - 360;
    }

    cout << num << ' ' << pen;

    return 0;
}


int DBG = 0;
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cassert>
#include <ctime>
#include <algorithm>
#include <sstream>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <bitset>
using namespace std;

#define mp make_pair
#define pb push_back
#define sz(a) int(a.size())
#define Forp(i, a, b) for (int i = a; i <= b; ++i)
#define Forc(i, a, b) for (int i = a; i < b; ++i)
#define Ford(i, a, b) for (int i = a; i >= b; --i)
#define Forv(i, x) for (__typeof(x.begin()) i = x.begin(); i != x.end(); ++i)
#define dprintf if (DBG) printf
#define mset(a) memset(a, 0, sizeof(a))
#define dprintf if (DBG) printf

typedef long long ll;
typedef unsigned long long ull;
typedef vector <int> vi;
typedef pair <int, int> pii;

const double eps = 1e-9;
const int oo = 1000000000;
const int N = 512;

int n, value[N][N], order[N];

int cost[N][N];

int main() {
//    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);

    scanf("%d", &n);
    Forp(mfriend, 1, n) {
        Forp(val, 1, n) {
            int card; scanf("%d", &card); value[mfriend][card] = val;
        }
        value[mfriend][mfriend] = oo;
    }
    Forp(i, 1, n) scanf("%d", &order[i]);

    dprintf("VALUE\n");
    if (DBG)
        Forp(i, 1, n) {Forp(j, 1, n) dprintf("%d ", value[i][j]); dprintf("\n");}

    Forp(mtime, 1, n) Forp(mfriend, 1, n) {
        cost[mfriend][mtime] = oo;
        Forp(i, 1, n) {
            int card = order[i];
            if (card == mfriend || card > mtime) continue;
            cost[mfriend][mtime] = value[mfriend][card];
            break;
        }
    }

    dprintf("COST\n");
    if (DBG)
        Forp(i, 1, n) {Forp(j, 1, n) dprintf("%d ", cost[i][j]); dprintf("\n");}

    Forp(mfriend, 1, n) {
        int res = 0;
        Forp(mtime, 1, n)
            if (res == 0 || cost[mfriend][mtime] < cost[mfriend][res])
                res = mtime;
        cout << res << ' ';
    }

    return 0;
}


int DBG = 0;
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cassert>
#include <ctime>
#include <algorithm>
#include <sstream>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <bitset>
using namespace std;

#define mp make_pair
#define pb push_back
#define sz(a) int(a.size())
#define Forp(i, a, b) for (int i = a; i <= b; ++i)
#define Forc(i, a, b) for (int i = a; i < b; ++i)
#define Ford(i, a, b) for (int i = a; i >= b; --i)
#define Forv(i, x) for (__typeof(x.begin()) i = x.begin(); i != x.end(); ++i)
#define mset(a) memset(a, 0, sizeof(a))
#define dprintf if (DBG) printf

typedef long long ll;
typedef unsigned long long ull;
typedef vector <int> vi;
typedef pair <int, int> pii;

const double eps = 1e-9;
const int oo = 1000000000;
const int N = 128;

int n;
int val[N][N], f[N][N], order[N], ans[N];

int main() {
//    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);

	scanf("%d", &n);
	Forp(person, 1, n)
	{
		Forp(id, 1, n)
		{
			int x; scanf("%d", &x);
			val[person][x] = id;
		}
	}

	Forp(i, 1, n) scanf("%d", &order[i]);
	Forp(mtime, 1, n) Forp(person, 1, n)
	{
		f[person][mtime] = oo;
		Forp(i, 1, n) if (order[i] != person && order[i] <= mtime)
		{
			f[person][mtime] = val[person][order[i]];
			break;
		}
	}

	Forp(person, 1, n)
	{
		int res = 0;
		Forp(mtime, 1, n) if (res == 0 || f[person][res] > f[person][mtime])
			res = mtime;
		ans[person] = res;
	}

	Forp(person, 1, n) printf("%d ", ans[person]);

	return 0;
}


int DBG = 0;
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cassert>
#include <ctime>
#include <algorithm>
#include <sstream>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <bitset>
using namespace std;

#define mp make_pair
#define pb push_back
#define sz(a) int(a.size())
#define Forp(i, a, b) for (int i = a; i <= b; ++i)
#define Forc(i, a, b) for (int i = a; i < b; ++i)
#define Ford(i, a, b) for (int i = a; i >= b; --i)
#define Forv(i, x) for (__typeof(x.begin()) i = x.begin(); i != x.end(); ++i)
#define dprintf if (DBG) printf
#define mset(a) memset(a, 0, sizeof(a))
#define dprintf if (DBG) printf

typedef long long ll;
typedef unsigned long long ull;
typedef vector <int> vi;
typedef pair <int, int> pii;

const double eps = 1e-9;
const int oo = 1000000000;
const int N = 512;

int n, value[N][N], order[N];

int cost[N][N];

int main() {
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);

    scanf("%d", &n);
    Forp(mfriend, 1, n) {
        Forp(val, 1, n) {
            int card; scanf("%d", &card); value[mfriend][card] = val;
        }
        value[mfriend][mfriend] = oo;
    }
    Forp(i, 1, n) scanf("%d", &order[i]);

    dprintf("VALUE\n");
    if (DBG)
        Forp(i, 1, n) {Forp(j, 1, n) dprintf("%d ", value[i][j]); dprintf("\n");}

    Forp(mtime, 1, n) Forp(mfriend, 1, n) {
        cost[mfriend][mtime] = oo;
        Forp(i, 1, n) {
            int card = order[i];
            if (card == mfriend || card > mtime) continue;
            cost[mfriend][mtime] = value[mfriend][card];
            break;
        }
    }

    dprintf("COST\n");
    if (DBG)
        Forp(i, 1, n) {Forp(j, 1, n) dprintf("%d ", cost[i][j]); dprintf("\n");}

    Forp(mfriend, 1, n) {
        int res = 0;
        Forp(mtime, 1, n)
            if (res == 0 || cost[mfriend][mtime] < cost[mfriend][res])
                res = mtime;
        cout << res << ' ';
    }

    return 0;
}


#include <iostream>
#include <cstdio>

using namespace std;

int N;
int index1[300+10][300+10];
int index[300+10];
int ans[300+10];
int ansindex[300+10];

int main()
{
    scanf("%d",&N);
    int val;
    for(int i=1; i<=N; i++){
            int ind=1;
            for(int j=1; j<=N; j++){
                    scanf("%d",&val);
                    index1[i][val]=ind++;
                    }}
    /*for(int i=1; i<=N; i++){
            for(int j=1; j<=N; j++){
                    cout<<index1[i][j]<<" ";
                    }
                    cout<<endl;
                    }
                    cout<<endl;
              */
            int ind=1;
    for(int i=1; i<=N; i++){
            scanf("%d",&val);
            index[val]=ind++;
            }

    for(int i=0; i<=N; i++)ansindex[i]=3000;

    int preindex=3000;
    int big;

    for(int i=1; i<=N; i++){
            if(index[i]<preindex){
                                  preindex=index[i];
                                  big=i;
                                  }
            for(int j=1; j<=N; j++){
                    if(j==big){
                               for(int ii=1; ii<=i; ii++){
                                       if(ii!=j){
                                       if(index1[j][ii] < ansindex[j]){
                                      //cout<<i<<" "<<index1[j][big]<<" "<<ansindex[ans[j]]<<" "<<ans[j]<<" "<<endl;
                                      ans[j]=ii;
                                      ansindex[j]=index1[j][ii];
                                      }}
                               }
                    continue;
                             }

                    if(index1[j][big] < ansindex[j]){
                                      //cout<<i<<" "<<index1[j][big]<<" "<<ansindex[ans[j]]<<" "<<ans[j]<<" "<<endl;
                                      ans[j]=big;
                                      ansindex[j]=index1[j][big];
                                      }
                    }
            //cout<<endl;
    }
    for(int i=1; i<=N; i++)cout<<ans[i]<<" ";
    cout<<endl;

    //system("pause");
    return 0;
}