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

int N, M, cn,A[503],B[502],Num[503];
set<int>S;
typedef pair<int,int>par;
struct a{
    short f,c,v;
}Abi[501][501][501];

int s;
par query( int p, int f, int c )
{
    s = 0;
    int fil, col;
    for( int i = p; i > 0; i-=(i&-i) )
        for( int j = f; j > 0; j-= (j&-j))
            for( int k = c; k > 0; k-=(k&-k) )
            {
                if( Abi[i][j][k].v > s )
                    s = Abi[i][j][k].v, fil = Abi[i][j][k].f, col = Abi[i][j][k].c;
//                sol = max(sol,Abi[i][j][k]);
            }
    return par(fil,col);

}

void update( int p, int f, int c, int val )
{
    for( int i = p; i <= cn; i+=(i&-i) )
        for( int j = f; j <= N; j+= (j&-j))
            for( int k = c; k <= M; k+=(k&-k) )
            {
                if( Abi[i][j][k].v < val )
                    Abi[i][j][k].f = f, Abi[i][j][k].c = k, Abi[i][j][k].v = val;
//                Abi[i][j][k] = max(Abi[i][j][k],val);

            }
}

vector<int>V;
par Mat[503][503];

void imprimir( int f, int c, int cp )
{
    if( cp == 0 )
        return;
    imprimir(Mat[f][c].first,Mat[f][c].second,cp-1);
    V.push_back(A[f]);
}

int main()
{
    //freopen("a.in","r",stdin);

    scanf("%d",&N);

    for( int i = 1; i <= N; i++ )
    {
        scanf("%d",&A[i]);
        if( binary_search(S.begin(),S.end(),A[i]) )
            continue;
        S.insert(A[i]);
        Num[++cn] = A[i];
    }

    scanf("%d",&M);

    for( int i = 1; i <= M; i++ )
    {
        scanf("%d",&B[i]);
        if( binary_search(S.begin(),S.end(),B[i]));
            continue;
            S.insert(B[i]);
            Num[++cn] = B[i];
    }

    sort(Num+1,Num+cn+1);

    par up;
    int sol = 0;
    for( int i = 1; i <= N; i++ )
    {
        for( int j = 1; j <= M; j++ )
        {
            if( A[i] != B[j] )
                continue;
            int pos = lower_bound(Num+1,Num+cn+1,A[i])-Num;
            par posi =  query(pos-1,i-1,j-1);
            update(pos,i,j,s+1);
            Mat[i][j] = posi;
            if( s + 1 > sol )
            {
                sol = s+1;
                up = par(i,j);
            }
        }
    }

    imprimir(up.first,up.second,sol);

    printf("%d",V[0]);

    for( int i = 1; i < sol; i++ )
        printf(" %d",V[i]);

    return 0;
}


#include <iostream>
#include <algorithm>

using namespace std;

const int MAXN = 501;

typedef pair<int, int> pii;

int A[MAXN], B[MAXN];
int La, Lb;
int dp[MAXN][MAXN];
pii wayf[MAXN][MAXN];

bool F = true;
void printSol(int i, int j){
    if (i * j == 0)
        return ;
    printSol(wayf[i][j].first, wayf[i][j].second);
    if (!F)
        cout << ' ' << A[i];
    else{
        cout << A[i];
        F = false;
    }
}

int main()
{
    cin >> La;
    for (int i = 1 ; i <= La ; i++)
        cin >> A[i];

    cin >> Lb;
    for (int i = 1 ; i <= Lb ; i++)
        cin >> B[i];

    for (int i = 1 ; i <= La ; i++)
        for (int j = 1 ; j <= Lb ; j++)
            if (A[i] == B[j]){
                for (int ii = 1 ; ii <= i ; ii++)
                    for (int jj = 1 ; jj <= j ; jj++)
                        if (A[ii] == B[jj] && A[ii] < A[i] && dp[i][j] < dp[ii][jj]){
                            dp[i][j] = dp[ii][jj];
                            wayf[i][j] = make_pair(ii, jj);
                        }
                dp[i][j]++;
            }
            else
            if (dp[i - 1][j] > dp[i][j - 1]){
                dp[i][j] = dp[i - 1][j];
                wayf[i][j] = make_pair(i - 1, j);
            }
            else{
                dp[i][j] = dp[i][j - 1];
                wayf[i][j] = make_pair(i, j - 1);
            }

    int sx = 0, sy = 0;
    for (int i = 1 ; i <= La ; i++)
        for (int j = 1 ; j <= Lb ; j++)
            if (dp[sx][sy] < dp[i][j])
                sx = i, sy = j;

    cout << dp[sx][sy] << '\n';

    printSol(sx, sy);

    return 0;
}


/*
Prob: Longest Comun Increasing Subsequence
o Subsecuencia Com'un Incremental M'axima.

Idea: Programaci'on Din'amica. Sea

dp[i][j] la longitud de la subsecuencia com'un incremental
de mayor longitud entre los prefijos A[1..i] y B[1..j].

La solucion estar'ia en dp[N][M].

Recurrencia:

Si A[i] == B[j] hay que calcular el optimo con ese entero al
final de la solucion del subproblema dp[i][j] por lo que
comenzamos con dp[i][j] = 1 y lo iremos mejorando
sucesivamente con subproblemas que ya estar'ian
calculados, estos son dp[x][y] para x < i and y < j.
Por lo q si A[i] == B[j] tenemos:

dp[i][j] = 1 + max(dp[x][y]); con A[x] == B[y] and A[x] < A[i]
para todo x < i and y < j.

El caso anterior ya ve todas las posibilidades de soluciones que
terminan con el elemento A[i], pero tal vez el optimo para el
subproblema dp[i][j] no termina con A[i] por lo que hay
arrastrar el optimo de los casos que estan en los subproblemas
dp[i - 1][j] y dp[i][j - 1]:

dp[i][j] = max(dp[i][j], dp[i - 1][j], dp[i][j - 1]);

Y por 'ultimo si A[i] != B[j] entonces tenemos la recurrencia
de LCS:

dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);

O sea, que si A[i] != B[j] solo necesitamos la subsecuencia que
sea de mayor longitud entre las aportadas por los subproblemas
dp[i - 1][j] y dp[i][j - 1].

Adem'as, los casos bases son similares a LCS:

dp[0][j] = 0;
dp[i][0] = 0;

Costo del peor caso: O(N^4) que corre en menos de un
segundo ya que N <= 100 por lo que N^4 <= 10^8.

Para la construcci'on de una LCIS simplemente guardamos
quien nos da el optimo o where are you from.. y luego
se construye recursivamente o iterativamente con un vector.

Para la implementaci'on ser'an usados los mismos nombres
de variables empleados en la explicaci'on previa.

*/

#include <bits/stdc++.h>

using namespace std;

typedef pair<int, int> pii;

const int MAXN = 101;

int N, M;
int A[MAXN];
int B[MAXN];
int dp[MAXN][MAXN];
pii wayf[MAXN][MAXN];//where are you from..
bool toma[MAXN][MAXN];

void print_lcis(int i, int j){
	pii D = wayf[i][j];

	if ( D.first == 0 || D.second == 0 ){
		cout << A[i];
		return;
	}

	print_lcis(D.first, D.second);

	if ( toma[i][j] )
		cout << " " << A[i];
}

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	//freopen("lcis.in", "r", stdin);
	//freopen("lcis.out", "w", stdout);

	cin >> N >> M; //longitudes de A y B respectivamente.

	for (int i = 1 ; i <= N ; i++)
		cin >> A[i];

	for (int j = 1 ; j <= M ; j++)
		cin >> B[j];

	//los casos bases quedan seteados al ser dp[][] global.

	for (int i = 1 ; i <= N ; i++)
		for (int j = 1 ; j <= M ; j++)
			if ( A[i] == B[j] ){
				dp[i][j] = 1; //se ira mejorando..
			 	toma[i][j] = true; //para saber cuando anadir a la construccion de la sol
				for (int x = 1 ; x < i ; x++)
					for (int y = 1 ; y < j ; y++)
						if ( A[x] == B[y]  &&  A[x] < A[i] )
							if ( dp[i][j] < dp[x][y] + 1 ){
								 dp[i][j] = dp[x][y] + 1;
								 wayf[i][j] = pii(x, y);
							}
				//pero como se explico arriba tal vez el obtimo
				//no necesariamente es terminando con A[i] por lo q:
				if ( dp[i][j] < dp[i - 1][j] ){
					 dp[i][j] = dp[i - 1][j];
					 wayf[i][j] = pii(i - 1, j);
					 toma[i][j] = false;
				}
				if ( dp[i][j] < dp[i][j - 1] ){
					 dp[i][j] = dp[i][j - 1];
					 wayf[i][j] = pii(i, j - 1);
					 toma[i][j] = false;
				}
			}//if( A[i] == B[j] )
			else{
				if ( dp[i - 1][j] > dp[i][j - 1] ){
					 dp[i][j] = dp[i - 1][j];
					 wayf[i][j] = pii(i - 1, j);
				}
				else{
					 dp[i][j] = dp[i][j - 1];
					 wayf[i][j] = pii(i, j - 1);
				}
			}

	int Sol = dp[N][M]; //para depurar comodamente..

	cout << Sol << '\n';

	print_lcis(N, M); //tal vez iterativo sea mas sencillo por el tema
	//de no poner el espacio al final de la linea..

	return 0;
}


/*
Prob: Longest Comun Increasing Subsequence
o Subsecuencia Com'un Incremental M'axima.

Idea: Programaci'on Din'amica. Sea

dp[i][j] la longitud de la subsecuencia com'un incremental
de mayor longitud entre los prefijos A[1..i] y B[1..j].

La solucion estar'ia en dp[N][M].

Recurrencia:

Si A[i] == B[j] hay que calcular el optimo con ese entero al
final de la solucion del subproblema dp[i][j] por lo que
comenzamos con dp[i][j] = 1 y lo iremos mejorando
sucesivamente con subproblemas que ya estar'ian
calculados, estos son dp[x][y] para x < i and y < j.
Por lo q si A[i] == B[j] tenemos:

dp[i][j] = 1 + max(dp[x][y]); con A[x] == B[y] and A[x] < A[i]
para todo x < i and y < j.

El caso anterior ya ve todas las posibilidades de soluciones que
terminan con el elemento A[i], pero tal vez el optimo para el
subproblema dp[i][j] no termina con A[i] por lo que hay
arrastrar el optimo de los casos que estan en los subproblemas
dp[i - 1][j] y dp[i][j - 1]:

dp[i][j] = max(dp[i][j], dp[i - 1][j], dp[i][j - 1]);

Y por 'ultimo si A[i] != B[j] entonces tenemos la recurrencia
de LCS:

dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);

O sea, que si A[i] != B[j] solo necesitamos la subsecuencia que
sea de mayor longitud entre las aportadas por los subproblemas
dp[i - 1][j] y dp[i][j - 1].

Adem'as, los casos bases son similares a LCS:

dp[0][j] = 0;
dp[i][0] = 0;

Costo del peor caso: O(N^4) que corre en menos de un
segundo ya que N <= 100 por lo que N^4 <= 10^8.

Para la construcci'on de una LCIS simplemente guardamos
quien nos da el optimo o where are you from.. y luego
se construye recursivamente o iterativamente con un vector.

Para la implementaci'on ser'an usados los mismos nombres
de variables empleados en la explicaci'on previa.

*/

#include <bits/stdc++.h>

using namespace std;

typedef pair<int, int> pii;

const int MAXN = 101;

int N, M;
int A[MAXN];
int B[MAXN];
int dp[MAXN][MAXN];
pii wayf[MAXN][MAXN];//where are you from..
bool toma[MAXN][MAXN];

void print_lcis(int i, int j){
	pii D = wayf[i][j];

	if ( D.first == 0 || D.second == 0 ){
		cout << A[i];
		return;
	}

	print_lcis(D.first, D.second);

	if ( toma[i][j] )
		cout << " " << A[i];
}

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	//freopen("lcis.in", "r", stdin);
	//freopen("lcis.out", "w", stdout);

	cin >> N >> M; //longitudes de A y B respectivamente.

	for (int i = 1 ; i <= N ; i++)
		cin >> A[i];

	for (int j = 1 ; j <= M ; j++)
		cin >> B[j];

	//los casos bases quedan seteados al ser dp[][] global.

	for (int i = 1 ; i <= N ; i++)
		for (int j = 1 ; j <= M ; j++)
			if ( A[i] == B[j] ){
				dp[i][j] = 1; //se ira mejorando..
			 	toma[i][j] = true; //para saber cuando anadir a la construccion de la sol
				for (int x = 1 ; x < i ; x++)
					for (int y = 1 ; y < j ; y++)
						if ( A[x] == B[y]  &&  A[x] < A[i] )
							if ( dp[i][j] < dp[x][y] + 1 ){
								 dp[i][j] = dp[x][y] + 1;
								 wayf[i][j] = pii(x, y);
							}
				//pero como se explico arriba tal vez el obtimo
				//no necesariamente es terminando con A[i] por lo q:
				if ( dp[i][j] < dp[i - 1][j] ){
					 dp[i][j] = dp[i - 1][j];
					 wayf[i][j] = pii(i - 1, j);
					 toma[i][j] = false;
				}
				if ( dp[i][j] < dp[i][j - 1] ){
					 dp[i][j] = dp[i][j - 1];
					 wayf[i][j] = pii(i, j - 1);
					 toma[i][j] = false;
				}
			}//if( A[i] == B[j] )
			else{
				if ( dp[i - 1][j] > dp[i][j - 1] ){
					 dp[i][j] = dp[i - 1][j];
					 wayf[i][j] = pii(i - 1, j);
				}
				else{
					 dp[i][j] = dp[i][j - 1];
					 wayf[i][j] = pii(i, j - 1);
				}
			}

	int Sol = dp[N][M]; //para depurar comodamente..

	cout << Sol << '\n';

	//print_lcis(N, M); //tal vez iterativo sea mas sencillo por el tema
	//de no poner el espacio al final de la linea..

	return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
using namespace std;
#define inf 1e9
int d[1001][1001];
int mas[1001];
int mas1[1001];
int main()
{
 //   freopen("input.txt","rt",stdin);
   // freopen("output.txt","wt",stdout);
    int n,m,i,j;
    cin>>n;
    for(i=1;i<=n;i++)
        cin>>mas[i];
    cin>>m;
    for(j=1;j<=m;j++)
        cin>>mas1[j];
        vector<int> buffer;
        for(i=1;i<=n;i++)
            for(j=1;j<=m;j++)
                {
                    if(mas[i]==mas1[j]) d[i][j]=d[i-1][j-1]+1;else
                    d[i][j]=max(d[i-1][j],d[i][j-1]);
                }
                int a=n,b=m;
                while(1)
                {
                    if(a==0||b==0) break;
                    if(mas[a]==mas1[b]) {buffer.push_back(mas[a]);--a;--b;}else
                    {
                        if(d[a-1][b]>d[a][b-1]) a--;else b--;
                    }
                }
                reverse(buffer.begin(),buffer.end());
                cout<<d[n][m]<<endl;
                for(i=0;i<buffer.size();i++)
                    cout<<buffer[i]<<" ";
    fclose(stdin);
    fclose(stdout);
    return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
using namespace std;
#define inf 1e9
int d[1001][1001];
int mas[1001];
int mas1[1001];
int main()
{
 //   freopen("input.txt","rt",stdin);
   // freopen("output.txt","wt",stdout);
    int n,m,i,j;
    cin>>n;
    for(i=1;i<=n;i++)
        cin>>mas[i];
    cin>>m;
    for(j=1;j<=m;j++)
        cin>>mas1[j];
        vector<int> buffer;
        for(i=1;i<=n;i++)
            for(j=1;j<=m;j++)
                {
                    if(mas[i]==mas1[j]) d[i][j]=d[i-1][j-1]+1;else
                    d[i][j]=max(d[i-1][j],d[i][j-1]);
                }
                int a=n,b=m;
                while(1)
                {
                    if(a==0||b==0) break;
                    if(mas[a]==mas1[b]) {buffer.push_back(mas[a]);--a;--b;}else
                    {
                        if(d[a-1][b]>d[a][b-1]) a--;else b--;
                    }
                }
                reverse(buffer.begin(),buffer.end());
                for(i=0;i<buffer.size();i++)
                    cout<<buffer[i]<<" ";
    fclose(stdin);
    fclose(stdout);
    return 0;
}


#pragma comment(linker,"/STACK:1000000000,1000000000")

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <iomanip>
#include <queue>
#include <map>
#include <functional>
#include <memory.h>
#include <list>
#include <sstream>
#include <ctime>
#include <climits>
#include <bitset>
#include <list>
#include <cassert>

using namespace std;

/* Constants begin */
const long long inf = 1e18+7;
const long long mod = 1000000009;
const double eps = 1e-12;
const double PI = 2*acos(0.0);
const double E = 2.71828;
/* Constants end */

/* Defines begin */
#define pb push_back
#define mp make_pair
#define ll long long
#define double long double
#define F first
#define S second
#define all(a) (a).begin(), (a).end()
#define forn(i,n) for (int (i)=0;(i)<(ll)(n);(i)++)
#define random (rand()<<16|rand())
#define sqr(x) (x)*(x)
/* Defines end */

int f[505], p[505], ans[505], sz = 0;
int n, m, a[505], b[505], id;

int main(void) {
    #ifndef ONLINE_JUDGE
        freopen("input.txt","rt",stdin);
        freopen("output.txt","wt",stdout);
    #endif
    scanf("%d",&n); forn(i,n) scanf("%d",&a[i+1]);
    scanf("%d",&m); forn(i,m) scanf("%d",&b[i+1]);

    for(int i=1;i<=n;i++)
     for(int j=1;j<=m;j++){
      if(j == 1) id = 0;
      if(a[i] == b[j] && f[j] < f[id] + 1) f[j] = f[id] + 1, p[j] = id;
      if(f[id] < f[j] && b[j] < a[i]) id = j;
     }
    id = 0;
    for(int i=1;i<=m;i++) if(f[i] > f[id]) id = i;
    for(int i=id;i;i=p[i]) ans[sz++] = b[i];
    printf("%d\n",sz);
    while(sz) printf("%d ",ans[--sz]);
    printf("\n");
    return 0;
}


#pragma comment(linker,"/STACK:100000000,100000000")

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <stack>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <iomanip>
#include <queue>
#include <map>
#include <functional>
#include <memory.h>
#include <list>
#include <sstream>
#include <ctime>
#include <climits>
#include <bitset>

using namespace std;

/* Constants begin */
const long long inf = 1e18+7;
const long long mod = 1e9+7;
const double eps = 1e-10;
const double PI = 2*acos(0.0);
const double E = 2.71828;
/* Constants end */

/* Defines begin */
#define pb push_back
#define mp make_pair
#define ll long long
#define double long double
#define F first
#define S second
#define all(a) (a).begin(), a.end()
#define forn(i,n) for (ll (i)=0;(i)<(ll)(n);(i)++)
/* Defines end */
ll a[505], b[505], dp[505], p[505], ans[505], where;

int main(void) {
   #ifndef ONLINE_JUDGE
        freopen("input.txt","rt",stdin);
        freopen("output.txt","wt",stdout);
    #endif
    ll n; cin >> n; forn(i,n) cin >> a[i+1];
    ll m; cin >> m; forn(i,m) cin >> b[i+1];
    forn(i,n) forn(j,m) {
        if(!j) where = 0; ll x = i+1, y = j+1;
        if(a[x] == b[y] && dp[where] + 1 > dp[y]) dp[y] = dp[where] + 1, p[y] = where; else
        if(dp[where] < dp[y] && b[y] < a[x]) where = y;
    }
    where = 0; ll rez = 0;
    forn(i,m) if(dp[i+1] > dp[where]) where = i+1;
    for(ll i=where; i>0; i=p[i]) ans[++rez] = b[i];
    cout << rez << endl;
    while(rez) cout << ans[rez--] << " "; cout << endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

short dp[501][501];
int n,m;
int ar1[555],ar2[555];
vector <int> vans;

short playDP(int indN,int indM,int indMaks)
{
	if ((indN>n)||(indM>m))
	{
		return 0;
	}
	else if (dp[indN][indM]!=-1)
	{
		return dp[indN][indM];
	}
	else
	{
		short temp=0;
		if ((ar1[indN]==ar2[indM])&&(ar1[indN]>ar1[indMaks]))
		{
			temp=playDP(indN+1,indM+1,indN)+1;
		}
		short temp1=playDP(indN+1,indM,indMaks);
		short temp2=playDP(indN,indM+1,indMaks);

		short maks=0;
		if ((temp>temp1)&&(temp>temp2))
		{
			maks=temp;
		}
		else if (temp1>temp2)
		{
			maks=temp1;
		}
		else
		{
			maks=temp2;
		}

		dp[indN][indM]=maks;
		//printf("%d %d %d %d",indN,indM,indMaks,dp[indN][indM][indMaks]);
		return dp[indN][indM];
	}
}

void playBacktrack(int indN,int indM,int indMaks)
{
	if (!((indN>n)||(indM>m)))
	{
		//printf("%d %d %d %d\n",indN,indM,indMaks,dp[indN][indM][indMaks]);
		short temp=0;
		if ((ar1[indN]==ar2[indM])&&(ar1[indN]>ar1[indMaks]))
		{
			temp=playDP(indN+1,indM+1,indN)+1;
		}
		short temp1=playDP(indN+1,indM,indMaks);
		short temp2=playDP(indN,indM+1,indMaks);

		short maks=0;
		if ((temp>temp1)&&(temp>temp2))
		{
			vans.push_back(ar1[indN]);
			playBacktrack(indN+1,indM+1,indN);
		}
		else if (temp1>temp2)
		{
			playBacktrack(indN+1,indM,indMaks);
		}
		else
		{
			playBacktrack(indN,indM+1,indMaks);
		}
	}
}

int main()
{
	scanf("%d",&n);
	for (int i=1;i<=n;i++)
	{
		scanf("%d",&ar1[i]);
	}
	scanf("%d",&m);
	for (int i=1;i<=m;i++)
	{
		scanf("%d",&ar2[i]);
	}

	// for (int i=0;i<=n;i++)
	// {
	// 	for (int j=0;j<=m;j++)
	// 	{
	// 		for (int k=0;k<=n;k++)
	// 		{
	// 			dp[i][j][k]=-1;
	// 		}
	// 	}
	// }

	memset(dp,-1,sizeof(dp));

	ar1[0]=-1;
	int ans=playDP(1,1,0);
	printf("%d\n",ans);
	playBacktrack(1,1,0);
	for (int i=1;i<=ans;i++)
	{
		printf("%d",vans[i-1]);
		if (i!=ans)
		{
			printf(" ");
		}
		else
		{
			printf("\n");
		}
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

short dp[501][501][501];
int n,m;
int ar1[555],ar2[555];
vector <int> vans;

unsigned char playDP(int indN,int indM,int indMaks)
{
	if ((indN>n)||(indM>m))
	{
		return 0;
	}
	else if (dp[indN][indM][indMaks]!=-1)
	{
		return dp[indN][indM][indMaks];
	}
	else
	{
		unsigned char temp=0;
		if ((ar1[indN]==ar2[indM])&&(ar1[indN]>ar1[indMaks]))
		{
			temp=playDP(indN+1,indM+1,indN)+1;
		}
		unsigned char temp1=playDP(indN+1,indM,indMaks);
		unsigned char temp2=playDP(indN,indM+1,indMaks);

		unsigned char maks=0;
		if ((temp>temp1)&&(temp>temp2))
		{
			maks=temp;
		}
		else if (temp1>temp2)
		{
			maks=temp1;
		}
		else
		{
			maks=temp2;
		}

		dp[indN][indM][indMaks]=maks;
		//printf("%d %d %d %d",indN,indM,indMaks,dp[indN][indM][indMaks]);
		return dp[indN][indM][indMaks];
	}
}

void playBacktrack(int indN,int indM,int indMaks)
{
	if (!((indN>n)||(indM>m)))
	{
		//printf("%d %d %d %d\n",indN,indM,indMaks,dp[indN][indM][indMaks]);
		unsigned char temp=0;
		if ((ar1[indN]==ar2[indM])&&(ar1[indN]>ar1[indMaks]))
		{
			temp=playDP(indN+1,indM+1,indN)+1;
		}
		unsigned char temp1=playDP(indN+1,indM,indMaks);
		unsigned char temp2=playDP(indN,indM+1,indMaks);

		unsigned char maks=0;
		if ((temp>temp1)&&(temp>temp2))
		{
			vans.push_back(ar1[indN]);
			playBacktrack(indN+1,indM+1,indN);
		}
		else if (temp1>temp2)
		{
			playBacktrack(indN+1,indM,indMaks);
		}
		else
		{
			playBacktrack(indN,indM+1,indMaks);
		}
	}
}

int main()
{
	scanf("%d",&n);
	for (int i=1;i<=n;i++)
	{
		scanf("%d",&ar1[i]);
	}
	scanf("%d",&m);
	for (int i=1;i<=m;i++)
	{
		scanf("%d",&ar2[i]);
	}

	for (int i=0;i<=n;i++)
	{
		for (int j=0;j<=m;j++)
		{
			for (int k=0;k<=n;k++)
			{
				dp[i][j][k]=-1;
			}
		}
	}

	ar1[0]=-1;
	int ans=playDP(1,1,0);
	printf("%d\n",ans);
	playBacktrack(1,1,0);
	for (int i=1;i<=ans;i++)
	{
		printf("%d",vans[i-1]);
		if (i!=ans)
		{
			printf(" ");
		}
		else
		{
			printf("\n");
		}
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

short dp[555];
short prev[555];
int n,m;
int ar1[555],ar2[555];
vector <int> vans;

int main()
{
	scanf("%d",&n);
	for (int i=1;i<=n;i++)
	{
		scanf("%d",&ar1[i]);
	}
	scanf("%d",&m);
	for (int i=1;i<=m;i++)
	{
		scanf("%d",&ar2[i]);
	}

	for (int i=1;i<=n;i++)
	{
		int len=0;
		int x=0;
		for (int j=1;j<=m;j++)
		{
			if ((ar2[j]<ar1[i])&&(dp[j]>len))
			{
				len=dp[j];
				x=j;
			}

			if ((ar2[j]==ar1[i])&&(len+1>dp[j]))
			{
				dp[j]=len+1;
				prev[j]=x;
			}

		}
		// for (int k=1;k<=m;k++)
		// {
		// 	printf("%d ",dp[k]);
		// }
		// printf("\n");
	}

	int ans=0;
	int ians=0;

	for (int i=1;i<=m;i++)
	{
		if (dp[i]>ans)
		{
			ans=dp[i];
			ians=i;
		}
	}
	printf("%d\n",ans);
	for (int i=1;i<=ans;i++)
	{
		vans.push_back(ar2[ians]);
		ians=prev[ians];
	}

	for (int i=ans;i>0;i--)
	{
		printf("%d",vans[i-1]);
		if (i==1)
		{
			printf("\n");
		}
		else
		{
			printf(" ");
		}
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

unsigned char dp[501][501][501];
int n,m;
int ar1[555],ar2[555];
vector <int> vans;
bool flag[501][501][501];

unsigned char playDP(int indN,int indM,int indMaks)
{
	if ((indN>n)||(indM>m))
	{
		return 0;
	}
	else if (flag[indN][indM][indMaks])
	{
		return dp[indN][indM][indMaks];
	}
	else
	{
		unsigned char temp=0;
		if ((ar1[indN]==ar2[indM])&&(ar1[indN]>ar1[indMaks]))
		{
			temp=playDP(indN+1,indM+1,indN)+1;
		}
		unsigned char temp1=playDP(indN+1,indM,indMaks);
		unsigned char temp2=playDP(indN,indM+1,indMaks);

		unsigned char maks=0;
		if ((temp>temp1)&&(temp>temp2))
		{
			maks=temp;
		}
		else if (temp1>temp2)
		{
			maks=temp1;
		}
		else
		{
			maks=temp2;
		}

		dp[indN][indM][indMaks]=maks;
		flag[indN][indM][indMaks]=true;
		//printf("%d %d %d %d",indN,indM,indMaks,dp[indN][indM][indMaks]);
		return dp[indN][indM][indMaks];
	}
}

void playBacktrack(int indN,int indM,int indMaks)
{
	if (!((indN>n)||(indM>m)))
	{
		if (((dp[indN+1][indM+1][indN]+1)>dp[indN+1][indM][indMaks])&&((dp[indN+1][indM+1][indN]+1)>dp[indN][indM+1][indMaks]))
		{
			vans.push_back(ar1[indN]);
			playBacktrack(indN+1,indM+1,indN);
		}
		else if ((dp[indN+1][indM][indMaks]>dp[indN][indM+1][indMaks]))
		{
			playBacktrack(indN+1,indM,indMaks);
		}
		else
		{
			playBacktrack(indN,indM+1,indMaks);
		}
	}
}

int main()
{
	scanf("%d",&n);
	for (int i=1;i<=n;i++)
	{
		scanf("%d",&ar1[i]);
	}
	scanf("%d",&m);
	for (int i=1;i<=m;i++)
	{
		scanf("%d",&ar2[i]);
	}

	ar1[0]=-1;
	int ans=playDP(1,1,0);
	printf("%d\n",ans);
	playBacktrack(1,1,0);
	for (int i=1;i<=ans;i++)
	{
		printf("%d",vans[i-1]);
		if (i!=ans)
		{
			printf(" ");
		}
	}
	printf("\n");
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

unsigned short dp[501][501][501];
int n,m;
int ar1[555],ar2[555];
vector <int> vans;
bool flag[501][501][501];

unsigned char playDP(int indN,int indM,int indMaks)
{
	if ((indN>n)||(indM>m))
	{
		return 0;
	}
	else if (flag[indN][indM][indMaks])
	{
		return dp[indN][indM][indMaks];
	}
	else
	{
		unsigned char temp=0;
		if ((ar1[indN]==ar2[indM])&&(ar1[indN]>ar1[indMaks]))
		{
			temp=playDP(indN+1,indM+1,indN)+1;
		}
		unsigned char temp1=playDP(indN+1,indM,indMaks);
		unsigned char temp2=playDP(indN,indM+1,indMaks);

		unsigned char maks=0;
		if ((temp>temp1)&&(temp>temp2))
		{
			maks=temp;
		}
		else if (temp1>temp2)
		{
			maks=temp1;
		}
		else
		{
			maks=temp2;
		}

		dp[indN][indM][indMaks]=maks;
		flag[indN][indM][indMaks]=true;
		//printf("%d %d %d %d",indN,indM,indMaks,dp[indN][indM][indMaks]);
		return dp[indN][indM][indMaks];
	}
}

void playBacktrack(int indN,int indM,int indMaks)
{
	if (!((indN>n)||(indM>m)))
	{
		//printf("%d %d %d %d\n",indN,indM,indMaks,dp[indN][indM][indMaks]);
		unsigned char temp=0;
		if ((ar1[indN]==ar2[indM])&&(ar1[indN]>ar1[indMaks]))
		{
			temp=playDP(indN+1,indM+1,indN)+1;
		}
		unsigned char temp1=playDP(indN+1,indM,indMaks);
		unsigned char temp2=playDP(indN,indM+1,indMaks);

		unsigned char maks=0;
		if ((temp>temp1)&&(temp>temp2))
		{
			vans.push_back(ar1[indN]);
			playBacktrack(indN+1,indM+1,indN);
		}
		else if (temp1>temp2)
		{
			playBacktrack(indN+1,indM,indMaks);
		}
		else
		{
			playBacktrack(indN,indM+1,indMaks);
		}
	}
}

int main()
{
	scanf("%d",&n);
	for (int i=1;i<=n;i++)
	{
		scanf("%d",&ar1[i]);
	}
	scanf("%d",&m);
	for (int i=1;i<=m;i++)
	{
		scanf("%d",&ar2[i]);
	}

	ar1[0]=-1;
	int ans=playDP(1,1,0);
	printf("%d\n",ans);
	playBacktrack(1,1,0);
	for (int i=1;i<=ans;i++)
	{
		printf("%d",vans[i-1]);
		if (i!=ans)
		{
			printf(" ");
		}
		else
		{
			printf("\n");
		}
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

unsigned char dp[501][501][501];
int n,m;
int ar1[555],ar2[555];
vector <int> vans;
bool flag[501][501][501];

unsigned char playDP(int indN,int indM,int indMaks)
{
	if ((indN>n)||(indM>m))
	{
		return 0;
	}
	else if (flag[indN][indM][indMaks])
	{
		return dp[indN][indM][indMaks];
	}
	else
	{
		unsigned char temp=0;
		if ((ar1[indN]==ar2[indM])&&(ar1[indN]>ar1[indMaks]))
		{
			temp=playDP(indN+1,indM+1,indN)+1;
		}
		unsigned char temp1=playDP(indN+1,indM,indMaks);
		unsigned char temp2=playDP(indN,indM+1,indMaks);

		unsigned char maks=0;
		if ((temp>temp1)&&(temp>temp2))
		{
			maks=temp;
		}
		else if (temp1>temp2)
		{
			maks=temp1;
		}
		else
		{
			maks=temp2;
		}

		dp[indN][indM][indMaks]=maks;
		flag[indN][indM][indMaks]=true;
		//printf("%d %d %d %d",indN,indM,indMaks,dp[indN][indM][indMaks]);
		return dp[indN][indM][indMaks];
	}
}

void playBacktrack(int indN,int indM,int indMaks)
{
	if (!((indN>n)||(indM>m)))
	{
		if (((dp[indN+1][indM+1][indN]+1)>dp[indN+1][indM][indMaks])&&((dp[indN+1][indM+1][indN]+1)>dp[indN][indM+1][indMaks]))
		{
			vans.push_back(ar1[indN]);
			playBacktrack(indN+1,indM+1,indN);
		}
		else if ((dp[indN+1][indM][indMaks]>dp[indN][indM+1][indMaks]))
		{
			playBacktrack(indN+1,indM,indMaks);
		}
		else
		{
			playBacktrack(indN,indM+1,indMaks);
		}
	}
}

int main()
{
	scanf("%d",&n);
	for (int i=1;i<=n;i++)
	{
		scanf("%d",&ar1[i]);
	}
	scanf("%d",&m);
	for (int i=1;i<=m;i++)
	{
		scanf("%d",&ar2[i]);
	}

	ar1[0]=-1;
	int ans=playDP(1,1,0);
	printf("%d\n",ans);
	playBacktrack(1,1,0);
	for (int i=1;i<=ans;i++)
	{
		printf("%d",vans[i-1]);
		if (i!=ans)
		{
			printf(" ");
		}
		else
		{
			printf("\n");
		}
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

unsigned char dp[501][501][501];
int n,m;
int ar1[555],ar2[555];
vector <int> vans;
bool flag[501][501][501];

unsigned char playDP(int indN,int indM,int indMaks)
{
	if ((indN>n)||(indM>m))
	{
		return 0;
	}
	else if (flag[indN][indM][indMaks])
	{
		return dp[indN][indM][indMaks];
	}
	else
	{
		unsigned char temp=0;
		if ((ar1[indN]==ar2[indM])&&(ar1[indN]>ar1[indMaks]))
		{
			temp=playDP(indN+1,indM+1,indN)+1;
		}
		unsigned char temp1=playDP(indN+1,indM,indMaks);
		unsigned char temp2=playDP(indN,indM+1,indMaks);

		unsigned char maks=0;
		if ((temp>temp1)&&(temp>temp2))
		{
			maks=temp;
		}
		else if (temp1>temp2)
		{
			maks=temp1;
		}
		else
		{
			maks=temp2;
		}

		dp[indN][indM][indMaks]=maks;
		flag[indN][indM][indMaks]=true;
		//printf("%d %d %d %d",indN,indM,indMaks,dp[indN][indM][indMaks]);
		return dp[indN][indM][indMaks];
	}
}

void playBacktrack(int indN,int indM,int indMaks)
{
	if (!((indN>n)||(indM>m)))
	{
		//printf("%d %d %d %d\n",indN,indM,indMaks,dp[indN][indM][indMaks]);
		unsigned char temp=0;
		if ((ar1[indN]==ar2[indM])&&(ar1[indN]>ar1[indMaks]))
		{
			temp=playDP(indN+1,indM+1,indN)+1;
		}
		unsigned char temp1=playDP(indN+1,indM,indMaks);
		unsigned char temp2=playDP(indN,indM+1,indMaks);

		unsigned char maks=0;
		if ((temp>temp1)&&(temp>temp2))
		{
			vans.push_back(ar1[indN]);
			playBacktrack(indN+1,indM+1,indN);
		}
		else if (temp1>temp2)
		{
			playBacktrack(indN+1,indM,indMaks);
		}
		else
		{
			playBacktrack(indN,indM+1,indMaks);
		}
	}
}

int main()
{
	scanf("%d",&n);
	for (int i=1;i<=n;i++)
	{
		scanf("%d",&ar1[i]);
	}
	scanf("%d",&m);
	for (int i=1;i<=m;i++)
	{
		scanf("%d",&ar2[i]);
	}

	ar1[0]=-1;
	int ans=playDP(1,1,0);
	printf("%d\n",ans);
	playBacktrack(1,1,0);
	for (int i=1;i<=ans;i++)
	{
		printf("%d",vans[i-1]);
		if (i!=ans)
		{
			printf(" ");
		}
		else
		{
			printf("\n");
		}
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

short dp[501][501][501];
int n,m;
int ar1[555],ar2[555];
vector <int> vans;

unsigned char playDP(int indN,int indM,int indMaks)
{
	if ((indN>n)||(indM>m))
	{
		return 0;
	}
	else if (flag[indN][indM][indMaks])
	{
		return dp[indN][indM][indMaks];
	}
	else
	{
		unsigned char temp=0;
		if ((ar1[indN]==ar2[indM])&&(ar1[indN]>ar1[indMaks]))
		{
			temp=playDP(indN+1,indM+1,indN)+1;
		}
		unsigned char temp1=playDP(indN+1,indM,indMaks);
		unsigned char temp2=playDP(indN,indM+1,indMaks);

		unsigned char maks=0;
		if ((temp>temp1)&&(temp>temp2))
		{
			maks=temp;
		}
		else if (temp1>temp2)
		{
			maks=temp1;
		}
		else
		{
			maks=temp2;
		}

		dp[indN][indM][indMaks]=maks;
		flag[indN][indM][indMaks]=true;
		//printf("%d %d %d %d",indN,indM,indMaks,dp[indN][indM][indMaks]);
		return dp[indN][indM][indMaks];
	}
}

void playBacktrack(int indN,int indM,int indMaks)
{
	if (!((indN>n)||(indM>m)))
	{
		//printf("%d %d %d %d\n",indN,indM,indMaks,dp[indN][indM][indMaks]);
		unsigned char temp=0;
		if ((ar1[indN]==ar2[indM])&&(ar1[indN]>ar1[indMaks]))
		{
			temp=playDP(indN+1,indM+1,indN)+1;
		}
		unsigned char temp1=playDP(indN+1,indM,indMaks);
		unsigned char temp2=playDP(indN,indM+1,indMaks);

		unsigned char maks=0;
		if ((temp>temp1)&&(temp>temp2))
		{
			vans.push_back(ar1[indN]);
			playBacktrack(indN+1,indM+1,indN);
		}
		else if (temp1>temp2)
		{
			playBacktrack(indN+1,indM,indMaks);
		}
		else
		{
			playBacktrack(indN,indM+1,indMaks);
		}
	}
}

int main()
{
	scanf("%d",&n);
	for (int i=1;i<=n;i++)
	{
		scanf("%d",&ar1[i]);
	}
	scanf("%d",&m);
	for (int i=1;i<=m;i++)
	{
		scanf("%d",&ar2[i]);
	}

	for (int i=0;i<=n;i++)
	{
		for (int j=0;j<=m;j++)
		{
			for (int k=0;k<=n;k++)
			{
				dp[i][j][k]=-1;
			}
		}
	}

	ar1[0]=-1;
	int ans=playDP(1,1,0);
	printf("%d\n",ans);
	playBacktrack(1,1,0);
	for (int i=1;i<=ans;i++)
	{
		printf("%d",vans[i-1]);
		if (i!=ans)
		{
			printf(" ");
		}
		else
		{
			printf("\n");
		}
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int dp[555][555][555];
int n,m;
int ar1[555],ar2[555];
bool flag[555][555][555];
vector <int> vans;

int playDP(int indN,int indM,int indMaks)
{
	if ((indN>n)||(indM>m))
	{
		return 0;
	}
	else if (flag[indN][indM][indMaks])
	{
		return dp[indN][indM][indMaks];
	}
	else
	{
		int temp=0;
		if ((ar1[indN]==ar2[indM])&&(ar1[indN]>ar1[indMaks]))
		{
			temp=playDP(indN+1,indM+1,indN)+1;
		}
		dp[indN][indM][indMaks]=max(temp,max(playDP(indN+1,indM,indMaks),playDP(indN,indM+1,indMaks)));
		//printf("%d %d %d %d",indN,indM,indMaks,dp[indN][indM][indMaks]);
		flag[indN][indM][indMaks]=true;
		return dp[indN][indM][indMaks];
	}
}

void playBacktrack(int indN,int indM,int indMaks)
{
	if (!((indN>n)||(indM>m)))
	{
		if (((dp[indN+1][indM+1][indN]+1)>dp[indN+1][indM][indMaks])&&((dp[indN+1][indM+1][indN]+1)>dp[indN][indM+1][indMaks]))
		{
			vans.push_back(ar1[indN]);
			playBacktrack(indN+1,indM+1,indN);
		}
		else if ((dp[indN+1][indM][indMaks]>dp[indN][indM+1][indMaks]))
		{
			playBacktrack(indN+1,indM,indMaks);
		}
		else
		{
			playBacktrack(indN,indM+1,indMaks);
		}
	}
}

int main()
{
	scanf("%d",&n);
	for (int i=1;i<=n;i++)
	{
		scanf("%d",&ar1[i]);
	}
	scanf("%d",&m);
	for (int i=1;i<=m;i++)
	{
		scanf("%d",&ar2[i]);
	}

	ar1[0]=-1;
	int ans=playDP(1,1,0);
	printf("%d\n",ans);
	playBacktrack(1,1,0);
	for (int i=1;i<=ans;i++)
	{
		printf("%d",vans[i-1]);
		if (i!=ans)
		{
			printf(" ");
		}
	}
	printf("\n");
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

short dp[501][501][501];
int n,m;
int ar1[555],ar2[555];
vector <int> vans;

short playDP(int indN,int indM,int indMaks)
{
	if ((indN>n)||(indM>m))
	{
		return 0;
	}
	else if (dp[indN][indM][indMaks]!=-1)
	{
		return dp[indN][indM][indMaks];
	}
	else
	{
		short temp=0;
		if ((ar1[indN]==ar2[indM])&&(ar1[indN]>ar1[indMaks]))
		{
			temp=playDP(indN+1,indM+1,indN)+1;
		}
		short temp1=playDP(indN+1,indM,indMaks);
		short temp2=playDP(indN,indM+1,indMaks);

		short maks=0;
		if ((temp>temp1)&&(temp>temp2))
		{
			maks=temp;
		}
		else if (temp1>temp2)
		{
			maks=temp1;
		}
		else
		{
			maks=temp2;
		}

		dp[indN][indM][indMaks]=maks;
		//printf("%d %d %d %d",indN,indM,indMaks,dp[indN][indM][indMaks]);
		return dp[indN][indM][indMaks];
	}
}

void playBacktrack(int indN,int indM,int indMaks)
{
	if (!((indN>n)||(indM>m)))
	{
		//printf("%d %d %d %d\n",indN,indM,indMaks,dp[indN][indM][indMaks]);
		short temp=0;
		if ((ar1[indN]==ar2[indM])&&(ar1[indN]>ar1[indMaks]))
		{
			temp=playDP(indN+1,indM+1,indN)+1;
		}
		short temp1=playDP(indN+1,indM,indMaks);
		short temp2=playDP(indN,indM+1,indMaks);

		short maks=0;
		if ((temp>temp1)&&(temp>temp2))
		{
			vans.push_back(ar1[indN]);
			playBacktrack(indN+1,indM+1,indN);
		}
		else if (temp1>temp2)
		{
			playBacktrack(indN+1,indM,indMaks);
		}
		else
		{
			playBacktrack(indN,indM+1,indMaks);
		}
	}
}

int main()
{
	scanf("%d",&n);
	for (int i=1;i<=n;i++)
	{
		scanf("%d",&ar1[i]);
	}
	scanf("%d",&m);
	for (int i=1;i<=m;i++)
	{
		scanf("%d",&ar2[i]);
	}

	// for (int i=0;i<=n;i++)
	// {
	// 	for (int j=0;j<=m;j++)
	// 	{
	// 		for (int k=0;k<=n;k++)
	// 		{
	// 			dp[i][j][k]=-1;
	// 		}
	// 	}
	// }

	memset(dp,-1,sizeof(dp));

	ar1[0]=-1;
	int ans=playDP(1,1,0);
	printf("%d\n",ans);
	playBacktrack(1,1,0);
	for (int i=1;i<=ans;i++)
	{
		printf("%d",vans[i-1]);
		if (i!=ans)
		{
			printf(" ");
		}
		else
		{
			printf("\n");
		}
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

unsigned char dp[501][501][501];
int n,m;
int ar1[555],ar2[555];
vector <int> vans;
bool flag[501][501][501];

unsigned char playDP(int indN,int indM,int indMaks)
{
	if ((indN>n)||(indM>m))
	{
		return 0;
	}
	else if (flag[indN][indM][indMaks])
	{
		return dp[indN][indM][indMaks];
	}
	else
	{
		unsigned char temp=0;
		if ((ar1[indN]==ar2[indM])&&(ar1[indN]>ar1[indMaks]))
		{
			temp=playDP(indN+1,indM+1,indN)+1;
		}
		unsigned char temp1=playDP(indN+1,indM,indMaks);
		unsigned char temp2=playDP(indN,indM+1,indMaks);

		unsigned char maks=0;
		if ((temp>temp1)&&(temp>temp2))
		{
			maks=temp;
		}
		else if (temp1>temp2)
		{
			maks=temp1;
		}
		else
		{
			maks=temp2;
		}

		dp[indN][indM][indMaks]=maks;
		flag[indN][indM][indMaks]=true;
		//printf("%d %d %d %d",indN,indM,indMaks,dp[indN][indM][indMaks]);
		return dp[indN][indM][indMaks];
	}
}

void playBacktrack(int indN,int indM,int indMaks)
{
	if (!((indN>n)||(indM>m)))
	{
		if (dp[indN][indM][indMaks]==dp[indN+1][indM+1][indN]+1)
		{
			vans.push_back(ar1[indN]);
			playBacktrack(indN+1,indM+1,indN);
		}
		else if (dp[indN][indM][indMaks]==dp[indN+1][indM][indMaks])
		{
			playBacktrack(indN+1,indM,indMaks);
		}
		else
		{
			playBacktrack(indN,indM+1,indMaks);
		}
	}
}

int main()
{
	scanf("%d",&n);
	for (int i=1;i<=n;i++)
	{
		scanf("%d",&ar1[i]);
	}
	scanf("%d",&m);
	for (int i=1;i<=m;i++)
	{
		scanf("%d",&ar2[i]);
	}

	ar1[0]=-1;
	int ans=playDP(1,1,0);
	printf("%d\n",ans);
	playBacktrack(1,1,0);
	for (int i=1;i<=ans;i++)
	{
		printf("%d",vans[i-1]);
		if (i!=ans)
		{
			printf(" ");
		}
		else
		{
			printf("\n");
		}
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

short dp[501][501][501];
int n,m;
int ar1[555],ar2[555];
vector <int> vans;

short playDP(int indN,int indM,int indMaks)
{
	if ((indN>n)||(indM>m))
	{
		return 0;
	}
	else if (dp[indN][indM][indMaks]!=-1)
	{
		return dp[indN][indM][indMaks];
	}
	else
	{
		short temp=0;
		if ((ar1[indN]==ar2[indM])&&(ar1[indN]>ar1[indMaks]))
		{
			temp=playDP(indN+1,indM+1,indN)+1;
		}
		short temp1=playDP(indN+1,indM,indMaks);
		short temp2=playDP(indN,indM+1,indMaks);

		short maks=0;
		if ((temp>temp1)&&(temp>temp2))
		{
			maks=temp;
		}
		else if (temp1>temp2)
		{
			maks=temp1;
		}
		else
		{
			maks=temp2;
		}

		dp[indN][indM][indMaks]=maks;
		//printf("%d %d %d %d",indN,indM,indMaks,dp[indN][indM][indMaks]);
		return dp[indN][indM][indMaks];
	}
}

void playBacktrack(int indN,int indM,int indMaks)
{
	if (!((indN>n)||(indM>m)))
	{
		//printf("%d %d %d %d\n",indN,indM,indMaks,dp[indN][indM][indMaks]);
		short temp=0;
		if ((ar1[indN]==ar2[indM])&&(ar1[indN]>ar1[indMaks]))
		{
			temp=playDP(indN+1,indM+1,indN)+1;
		}
		short temp1=playDP(indN+1,indM,indMaks);
		short temp2=playDP(indN,indM+1,indMaks);

		short maks=0;
		if ((temp>temp1)&&(temp>temp2))
		{
			vans.push_back(ar1[indN]);
			playBacktrack(indN+1,indM+1,indN);
		}
		else if (temp1>temp2)
		{
			playBacktrack(indN+1,indM,indMaks);
		}
		else
		{
			playBacktrack(indN,indM+1,indMaks);
		}
	}
}

int main()
{
	scanf("%d",&n);
	for (int i=1;i<=n;i++)
	{
		scanf("%d",&ar1[i]);
	}
	scanf("%d",&m);
	for (int i=1;i<=m;i++)
	{
		scanf("%d",&ar2[i]);
	}

	for (int i=0;i<=n;i++)
	{
		for (int j=0;j<=m;j++)
		{
			for (int k=0;k<=n;k++)
			{
				dp[i][j][k]=-1;
			}
		}
	}

	ar1[0]=-1;
	int ans=playDP(1,1,0);
	printf("%d\n",ans);
	playBacktrack(1,1,0);
	for (int i=1;i<=ans;i++)
	{
		printf("%d",vans[i-1]);
		if (i!=ans)
		{
			printf(" ");
		}
		else
		{
			printf("\n");
		}
	}
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <cstring>

#define forn(i, n) for(int i = 0; i < (n); ++i)

using namespace std;

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

const int MAXN = 3010;
int dp[MAXN][MAXN];
int a[MAXN];
int b[MAXN];
int tmp[MAXN];
int tv[MAXN][4 * MAXN];
int pans[MAXN];
map<int, int> ord;

inline void update(int col, int u, int l, int r, int pos, int val) {
    if (l == r) {
        tv[col][u] = val;
        return;
    }
    int m = (l + r) >> 1;
    int lu = 2 * u + 1;
    int ru = lu + 1;
    if (pos <= m) {
        update(col, lu, l, m, pos, val);
    } else {
        update(col, ru, m + 1, r, pos, val);
    }
    tv[col][u] = max(tv[col][lu], tv[col][ru]);
}

inline int get(int col, int u, int l, int r, int cl, int cr) {
//  cerr << u << ' ' << l << ' ' << r << ' ' << cl << ' ' << cr << endl;
    if (l == cl && r == cr) {
        return tv[col][u];
    }
    int m = (l + r) >> 1;
    int lu = 2 * u + 1;
    int ru = lu + 1;
    int res = 0;
    if (cl <= m) {
        res = max(res, get(col, lu, l, m, cl, min(cr, m)));
    }
    if (cr > m) {
        res = max(res, get(col, ru, m + 1, r, max(cl, m + 1), cr));
    }
    return res;
}

void print(int n, int m, int ans) {
    int mj = m;
    int last = 2 * MAXN;
    for (int i = n - 1; i >= 0 && ans; --i) {
        for (int j = 0; j < mj; ++j) if (a[i] == b[j] && a[i] < last){
            if (dp[i + 1][j + 1] == ans) {
                --ans;
                mj = j;
                pans[ans] = tmp[a[i]];
                break;
            }
        }
    }
}

int main(int argc, char **argv) {
//  freopen("lcis.in", "r", stdin);
//  freopen("lcis.out", "w", stdout);
    int n, m;
//  scanf("%d %d", &n, &m);
    scanf("%d", &n);
    int cnt = 0;
    forn(i, n) {
        scanf("%d", a + i);
        tmp[cnt++] = a[i];
    }
    scanf("%d", &m);
    forn(i, m) {
        scanf("%d", b + i);
        tmp[cnt++] = b[i];
    }
    sort(tmp, tmp + cnt);
    cnt = unique(tmp, tmp + cnt) - tmp;
    forn(i, cnt) {
        ord[tmp[i]] = i;
    }
    forn(i, n) {
        a[i] = ord[a[i]];
    }
    forn(i, m) {
        b[i] = ord[b[i]];
    }
    int ans = 0;
    forn(i, n) {
        forn(j, m) {
            if (a[i] == b[j]) {
                int dmax = (a[i] ? get(j, 0, 0, cnt - 1, 0, a[i] - 1) : 0);
                dp[i + 1][j + 1] = dmax + 1;
            } else {
                dp[i + 1][j + 1] = max(dp[i + 1][j], dp[i][j + 1]);
            }
            ans = max(ans, dp[i + 1][j + 1]);
//          cerr << dp[i + 1][j + 1] << ' ';
        }
//      cerr << endl;
        int cur = 0;
        forn(j, m) if (a[i] == b[j]) {
            cur = max(cur, dp[i + 1][j + 1]);
            update(j + 1, 0, 0, cnt - 1, a[i], cur);
        }
    }
    printf("%d\n", ans);
    print(n, m, ans);
    forn(i, ans) {
        printf("%d ", pans[i]);
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <cstring>

#define forn(i, n) for(int i = 0; i < (n); ++i)

using namespace std;

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

const int MAXN = 3010;
int dp[MAXN][MAXN];
int a[MAXN];
int b[MAXN];
int tmp[MAXN];
int tv[MAXN][4 * MAXN];
int pans[MAXN];
map<int, int> ord;

inline void update(int col, int u, int l, int r, int pos, int val) {
    if (l == r) {
        tv[col][u] = val;
        return;
    }
    int m = (l + r) >> 1;
    int lu = 2 * u + 1;
    int ru = lu + 1;
    if (pos <= m) {
        update(col, lu, l, m, pos, val);
    } else {
        update(col, ru, m + 1, r, pos, val);
    }
    tv[col][u] = max(tv[col][lu], tv[col][ru]);
}

inline int get(int col, int u, int l, int r, int cl, int cr) {
//  cerr << u << ' ' << l << ' ' << r << ' ' << cl << ' ' << cr << endl;
    if (l == cl && r == cr) {
        return tv[col][u];
    }
    int m = (l + r) >> 1;
    int lu = 2 * u + 1;
    int ru = lu + 1;
    int res = 0;
    if (cl <= m) {
        res = max(res, get(col, lu, l, m, cl, min(cr, m)));
    }
    if (cr > m) {
        res = max(res, get(col, ru, m + 1, r, max(cl, m + 1), cr));
    }
    return res;
}

void print(int n, int m, int ans) {
    int mj = m;
    int last = 2 * MAXN;
    for (int i = n - 1; i >= 0 && ans; --i) {
        for (int j = 0; j < mj; ++j) if (a[i] == b[j] && a[i] < last){
            if (dp[i + 1][j + 1] == ans) {
                --ans;
                mj = j;
                pans[ans] = tmp[a[i]];
                break;
            }
        }
    }
}

int main(int argc, char **argv) {
//  freopen("lcis.in", "r", stdin);
//  freopen("lcis.out", "w", stdout);
    int n, m;
//  scanf("%d %d", &n, &m);
    scanf("%d", &n);
    int cnt = 0;
    forn(i, n) {
        scanf("%d", a + i);
        tmp[cnt++] = a[i];
    }
    scanf("%d", &m);
    forn(i, m) {
        scanf("%d", b + i);
        tmp[cnt++] = b[i];
    }
    sort(tmp, tmp + cnt);
    cnt = unique(tmp, tmp + cnt) - tmp;
    forn(i, cnt) {
        ord[tmp[i]] = i;
    }
    forn(i, n) {
        a[i] = ord[a[i]];
    }
    forn(i, m) {
        b[i] = ord[b[i]];
    }
    int ans = 0;
    forn(i, n) {
        forn(j, m) {
            if (a[i] == b[j]) {
                int dmax = (a[i] ? get(j, 0, 0, cnt - 1, 0, a[i] - 1) : 0);
                dp[i + 1][j + 1] = dmax + 1;
            } else {
                dp[i + 1][j + 1] = max(dp[i + 1][j], dp[i][j + 1]);
            }
            ans = max(ans, dp[i + 1][j + 1]);
//          cerr << dp[i + 1][j + 1] << ' ';
        }
//      cerr << endl;
        int cur = 0;
        forn(j, m) {
            if (a[i] == b[j]) {
                cur = max(cur, dp[i + 1][j + 1]);
            }
//          cerr << "> " << j + 1 << ' ' << cnt - 1 << ' ' << a[i] << ' ' << cur << endl;
            update(j + 1, 0, 0, cnt - 1, a[i], cur);
        }
    }
    printf("%d\n", ans);
    print(n, m, ans);
    forn(i, ans) {
        printf("%d ", pans[i]);
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <cstring>

#define forn(i, n) for(int i = 0; i < (n); ++i)

using namespace std;

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

const int MAXN = 3010;
int dp[MAXN][MAXN];
int a[MAXN];
int b[MAXN];
int tmp[MAXN];
int tv[MAXN][8 * MAXN];
int pans[MAXN];
map<int, int> ord;

inline void update(int col, int u, int l, int r, int pos, int val) {
    if (l == r) {
        tv[col][u] = val;
        return;
    }
    int m = (l + r) >> 1;
    int lu = 2 * u + 1;
    int ru = lu + 1;
    if (pos <= m) {
        update(col, lu, l, m, pos, val);
    } else {
        update(col, ru, m + 1, r, pos, val);
    }
    tv[col][u] = max(tv[col][lu], tv[col][ru]);
}

inline int get(int col, int u, int l, int r, int cl, int cr) {
//  cerr << u << ' ' << l << ' ' << r << ' ' << cl << ' ' << cr << endl;
    if (l == cl && r == cr) {
        return tv[col][u];
    }
    int m = (l + r) >> 1;
    int lu = 2 * u + 1;
    int ru = lu + 1;
    int res = 0;
    if (cl <= m) {
        res = max(res, get(col, lu, l, m, cl, min(cr, m)));
    }
    if (cr > m) {
        res = max(res, get(col, ru, m + 1, r, max(cl, m + 1), cr));
    }
    return res;
}

void print(int n, int m, int ans) {
    int mj = m;
    int last = 2 * MAXN;
    for (int i = n - 1; i >= 0 && ans; --i) {
        for (int j = 0; j < mj; ++j) if (a[i] == b[j] && a[i] < last){
            if (dp[i + 1][j + 1] == ans) {
                --ans;
                mj = j;
                pans[ans] = tmp[a[i]];
                break;
            }
        }
    }
}

int main(int argc, char **argv) {
//  freopen("lcis.in", "r", stdin);
//  freopen("lcis.out", "w", stdout);
    int n, m;
    scanf("%d %d", &n, &m);
    int cnt = 0;
    forn(i, n) {
        scanf("%d", a + i);
        tmp[cnt++] = a[i];
    }
    forn(i, m) {
        scanf("%d", b + i);
        tmp[cnt++] = b[i];
    }
    sort(tmp, tmp + cnt);
    cnt = unique(tmp, tmp + cnt) - tmp;
    forn(i, cnt) {
        ord[tmp[i]] = i;
    }
    forn(i, n) {
        a[i] = ord[a[i]];
    }
    forn(i, m) {
        b[i] = ord[b[i]];
    }
    int ans = 0;
    forn(i, n) {
        forn(j, m) {
            if (a[i] == b[j]) {
                int dmax = (a[i] ? get(j, 0, 0, cnt - 1, 0, a[i] - 1) : 0);
                dp[i + 1][j + 1] = dmax + 1;
            } else {
                dp[i + 1][j + 1] = max(dp[i + 1][j], dp[i][j + 1]);
            }
            ans = max(ans, dp[i + 1][j + 1]);
//          cerr << dp[i + 1][j + 1] << ' ';
        }
//      cerr << endl;
        int cur = 0;
        forn(j, m) {
            cur = max(cur, dp[i + 1][j + 1]);
            update(j + 1, 0, 0, cnt - 1, a[i], cur);
        }
    }
    printf("%d\n", ans);
    print(n, m, ans);
    forn(i, ans) {
        printf("%d ", pans[i]);
    }
    return 0;
}


#pragma comment(linker, "/STACK:16777216")
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <ctime>
#include <cstdlib>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <cmath>
#define eps 1.0e-6
#define inf 2000000000
#define forn(i, n) for(int i = 0; i < (int)n; ++i)

using namespace std;

typedef vector<int> vi;
typedef vector< vector<int> > vvi;
typedef vector<bool> vb;
typedef pair<int, int> ii;
typedef long long ll;

int a[505];
int b[505];
int c[505][505];
int px[505][505];
int py[505][505];

int main(int argc, char **argv) {
    int n, m;
    scanf("%d", &n);
    forn(i, n)
        scanf("%d", a + i);
    scanf("%d", &m);
    forn(i, m)
        scanf("%d", b + i);
    int cx = 0, cy = 0;
    int rans = 0;
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= m; j++) {
            if(a[i-1] != b[j-1]) {
                if(c[i-1][j] > c[i][j-1]) {
                    c[i][j] = c[i-1][j];
                    px[i][j] = i - 1;
                    py[i][j] = j;
                } else {
                    c[i][j] = c[i][j-1];
                    px[i][j] = i;
                    py[i][j] = j - 1;
                }
            } else {
                int cmax = 0;
                px[i][j] = 0;
                py[i][j] = 0;
                for(int k1 = 1; k1 < i; k1++)
                    for(int k2 = 1; k2 < j; k2++) if(a[k1 - 1] == b[k2 - 1] && a[k1 - 1] < a[i - 1]) {
                        if(i == 4 && j == 7)
                            cout << k1 << ' ' << k2 << ' ' << c[k1][k2]<< endl;
                        if(c[k1][k2] > cmax) {
                            cmax = c[k1][k2];
                            px[i][j] = k1;
                            py[i][j] = k2;
                        }
                    }
                c[i][j] = cmax + 1;
            }
            if(c[i][j] > rans) {
                rans = c[i][j];
                cx = i;
                cy = j;
            }
        }
    cout << rans << endl;
    vi ans;
    while(cx > 0 && cy > 0) {
        if(a[cx - 1] == b[cy - 1])
            ans.push_back(a[cx-1]);
        int cn = px[cx][cy];
        cy = py[cx][cy];
        cx = cn;
    }
    for(int i = ans.size(); i--;)
        printf("%d ", ans[i]);
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <cstring>

#define forn(i, n) for(int i = 0; i < (n); ++i)

using namespace std;

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

const int MAXN = 3010;
int dp[MAXN][MAXN];
int a[MAXN];
int b[MAXN];
int tmp[MAXN];
int tv[MAXN][4 * MAXN];
int pans[MAXN];
map<int, int> ord;

inline void update(int col, int u, int l, int r, int pos, int val) {
    if (l == r) {
        tv[col][u] = val;
        return;
    }
    int m = (l + r) >> 1;
    int lu = 2 * u + 1;
    int ru = lu + 1;
    if (pos <= m) {
        update(col, lu, l, m, pos, val);
    } else {
        update(col, ru, m + 1, r, pos, val);
    }
    tv[col][u] = max(tv[col][lu], tv[col][ru]);
}

inline int get(int col, int u, int l, int r, int cl, int cr) {
//  cerr << u << ' ' << l << ' ' << r << ' ' << cl << ' ' << cr << endl;
    if (l == cl && r == cr) {
        return tv[col][u];
    }
    int m = (l + r) >> 1;
    int lu = 2 * u + 1;
    int ru = lu + 1;
    int res = 0;
    if (cl <= m) {
        res = max(res, get(col, lu, l, m, cl, min(cr, m)));
    }
    if (cr > m) {
        res = max(res, get(col, ru, m + 1, r, max(cl, m + 1), cr));
    }
    return res;
}

void print(int n, int m, int ans) {
    int mj = m;
    int last = 2 * MAXN;
    for (int i = n - 1; i >= 0 && ans; --i) {
        for (int j = 0; j < mj; ++j) if (a[i] == b[j] && a[i] < last){
            if (dp[i + 1][j + 1] == ans) {
                --ans;
                mj = j;
                pans[ans] = tmp[a[i]];
                break;
            }
        }
    }
}

int main(int argc, char **argv) {
//  freopen("lcis.in", "r", stdin);
//  freopen("lcis.out", "w", stdout);
    int n, m;
//  scanf("%d %d", &n, &m);
    scanf("%d", &n);
    int cnt = 0;
    forn(i, n) {
        scanf("%d", a + i);
        tmp[cnt++] = a[i];
    }
    scanf("%d", &m);
    forn(i, m) {
        scanf("%d", b + i);
        tmp[cnt++] = b[i];
    }
    sort(tmp, tmp + cnt);
    cnt = unique(tmp, tmp + cnt) - tmp;
    forn(i, cnt) {
        ord[tmp[i]] = i;
    }
    forn(i, n) {
        a[i] = ord[a[i]];
    }
    forn(i, m) {
        b[i] = ord[b[i]];
    }
    int ans = 0;
    forn(i, n) {
        forn(j, m) {
            if (a[i] == b[j]) {
                int dmax = (a[i] ? get(j, 0, 0, cnt - 1, 0, a[i] - 1) : 0);
                dp[i + 1][j + 1] = dmax + 1;
            } else {
                dp[i + 1][j + 1] = max(dp[i + 1][j], dp[i][j + 1]);
            }
            ans = max(ans, dp[i + 1][j + 1]);
//          cerr << dp[i + 1][j + 1] << ' ';
        }
//      cerr << endl;
        int cur = 0;
        forn(j, m) {
            cur = max(cur, dp[i + 1][j + 1]);
            update(j + 1, 0, 0, cnt - 1, a[i], cur);
        }
    }
    printf("%d\n", ans);
    print(n, m, ans);
    forn(i, ans) {
        printf("%d ", pans[i]);
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <cstring>

#define forn(i, n) for(int i = 0; i < (n); ++i)

using namespace std;

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

const int MAXN = 3010;
int dp[MAXN][MAXN];
int a[MAXN];
int b[MAXN];
int tmp[MAXN];
int tv[MAXN][4 * MAXN];
int pans[MAXN];
map<int, int> ord;

inline void update(int col, int u, int l, int r, int pos, int val) {
    if (l == r) {
        tv[col][u] = val;
        return;
    }
    int m = (l + r) >> 1;
    int lu = 2 * u + 1;
    int ru = lu + 1;
    if (pos <= m) {
        update(col, lu, l, m, pos, val);
    } else {
        update(col, ru, m + 1, r, pos, val);
    }
    tv[col][u] = max(tv[col][lu], tv[col][ru]);
}

inline int get(int col, int u, int l, int r, int cl, int cr) {
//  cerr << u << ' ' << l << ' ' << r << ' ' << cl << ' ' << cr << endl;
    if (l == cl && r == cr) {
        return tv[col][u];
    }
    int m = (l + r) >> 1;
    int lu = 2 * u + 1;
    int ru = lu + 1;
    int res = 0;
    if (cl <= m) {
        res = max(res, get(col, lu, l, m, cl, min(cr, m)));
    }
    if (cr > m) {
        res = max(res, get(col, ru, m + 1, r, max(cl, m + 1), cr));
    }
    return res;
}

void print(int n, int m, int ans) {
    int mj = m;
    int last = 2 * MAXN;
    for (int i = n - 1; i >= 0 && ans; --i) {
        for (int j = 0; j < mj; ++j) if (a[i] == b[j] && a[i] < last){
            if (dp[i + 1][j + 1] == ans) {
                --ans;
                mj = j;
                pans[ans] = tmp[a[i]];
                break;
            }
        }
    }
}

int main(int argc, char **argv) {
//  freopen("lcis.in", "r", stdin);
//  freopen("lcis.out", "w", stdout);
    int n, m;
    scanf("%d %d", &n, &m);
    int cnt = 0;
    forn(i, n) {
        scanf("%d", a + i);
        tmp[cnt++] = a[i];
    }
    forn(i, m) {
        scanf("%d", b + i);
        tmp[cnt++] = b[i];
    }
    sort(tmp, tmp + cnt);
    cnt = unique(tmp, tmp + cnt) - tmp;
    forn(i, cnt) {
        ord[tmp[i]] = i;
    }
    forn(i, n) {
        a[i] = ord[a[i]];
    }
    forn(i, m) {
        b[i] = ord[b[i]];
    }
    int ans = 0;
    forn(i, n) {
        forn(j, m) {
            if (a[i] == b[j]) {
                int dmax = (a[i] ? get(j, 0, 0, cnt - 1, 0, a[i] - 1) : 0);
                dp[i + 1][j + 1] = dmax + 1;
            } else {
                dp[i + 1][j + 1] = max(dp[i + 1][j], dp[i][j + 1]);
            }
            ans = max(ans, dp[i + 1][j + 1]);
//          cerr << dp[i + 1][j + 1] << ' ';
        }
//      cerr << endl;
        int cur = 0;
        forn(j, m) {
            cur = max(cur, dp[i + 1][j + 1]);
            update(j + 1, 0, 0, cnt - 1, a[i], cur);
        }
    }
    printf("%d\n", ans);
    print(n, m, ans);
    forn(i, ans) {
        printf("%d ", pans[i]);
    }
    return 0;
}


#include <stdio.h>
#include <vector>
#include <algorithm>
using namespace std;

long d[500][500];
long prev[500];

vector<long> LCIS (vector<long> a, vector<long> b, long n, long m) {
    for (long i=0; i<=n; i++) d[i][0]=0;
    for (long j=0; j<=m; j++) d[0][j]=0;

    long best, ind;
    for (long i=1; i<=n; i++) {
        best = ind = 0;
        for (long j=1; j<=m; j++) {
            d[i][j] = d[i-1][j];
            if (a[i-1]==b[j-1] && best+1 > d[i][j]) {
                d[i][j] = best+1;
                prev[j] = ind;
            }
            if (b[j-1] < a[i-1] && d[i-1][j] > best) {
                best = d[i-1][j];
                ind = j;
            }
        }
    }

    long pos=0;
    for (long j=1; j<=n; j++)
        if (d[n][j] > d[n][pos])
            pos = j;

    vector<long> ans;
    while (pos!=0) {
        ans.push_back(b[pos-1]);
        pos = prev[pos];
    }
    reverse(ans.begin(), ans.end());
    return ans;
}

int main()
{
    long n, m, item;
    vector<long> a, b, c;
    scanf("%ld", &n);
    for (long i=0; i<n; i++) {
        scanf("%ld", &item);
        a.push_back(item);
    }
    scanf("%ld", &m);
    for (long i=0; i<m; i++) {
        scanf("%ld", &item);
        b.push_back(item);
    }

    c=LCIS(a,b, n, m);
    printf("%ld\n", c.size());
    for (long i=0; i<c.size(); i++)
        printf("%ld ", c[i]);

    return 0;
}


#include <stdio.h>
#include <vector>
#include <algorithm>
using namespace std;

long d[500][500];
long prev[500];

vector<long> LCIS (vector<long> a, vector<long> b, long n, long m) {
    for (long i=0; i<=n; i++) d[i][0]=0;
    for (long j=0; j<=m; j++) d[0][j]=0;

    long best, ind;
    for (long i=1; i<=n; i++) {
        best = ind = 0;
        for (long j=1; j<=m; j++) {
            d[i][j] = d[i-1][j];
            if (a[i]==b[j] && best+1 > d[i][j]) {
                d[i][j] = best+1;
                prev[j] = ind;
            }
            if (b[j] < a[i] && d[i-1][j] > best) {
                best = d[i-1][j];
                ind = j;
            }
        }
    }

    long pos=1;
    for (long j=1; j<n; j++)
        if (d[n][j] > d[n][pos])
            pos = j;

    vector<long> ans;
    while (pos!=0) {
        ans.push_back(b[pos]);
        pos = prev[pos];
    }
    reverse(ans.begin(), ans.end());
    return ans;
}

int main()
{
    long n, m, item;
    vector<long> a, b, c;
    scanf("%ld", &n);
    for (long i=0; i<n; i++) {
        scanf("%ld", &item);
        a.push_back(item);
    }
    scanf("%ld", &m);
    for (long i=0; i<m; i++) {
        scanf("%ld", &item);
        b.push_back(item);
    }

    c=LCIS(a,b, n, m);
    printf("%ld\n", c.size());
    for (long i=0; i<c.size(); i++)
        printf("%ld ", c[i]);

    return 0;
}


#include <stdio.h>
#include <vector>
#include <algorithm>
using namespace std;

long d[500][500];
long prev[500];

vector<long> LCIS (vector<long> a, vector<long> b, long n, long m) {
    for (long i=0; i<=n; i++) d[i][0]=0;
    for (long j=0; j<=m; j++) d[0][j]=0;

    long best, ind;
    for (long i=1; i<=n; i++) {
        best = ind = 0;
        for (long j=1; j<=m; j++) {
            d[i][j] = d[i-1][j];
            if (a[i-1]==b[j-1] && best+1 > d[i][j]) {
                d[i][j] = best+1;
                prev[j] = ind;
            }
            if (b[j-1] < a[i-1] && d[i-1][j] > best) {
                best = d[i-1][j];
                ind = j;
            }
        }
    }

    long pos=0;
    for (long j=1; j<=m; j++)
        if (d[n][j] > d[n][pos])
            pos = j;

    vector<long> ans;
    while (pos!=0) {
        ans.push_back(b[pos-1]);
        pos = prev[pos];
    }
    reverse(ans.begin(), ans.end());
    return ans;
}

int main()
{
    long n, m, item;
    vector<long> a, b, c;
    scanf("%ld", &n);
    for (long i=0; i<n; i++) {
        scanf("%ld", &item);
        a.push_back(item);
    }
    scanf("%ld", &m);
    for (long i=0; i<m; i++) {
        scanf("%ld", &item);
        b.push_back(item);
    }

    c=LCIS(a,b, n, m);
    printf("%ld\n", c.size());
    for (long i=0; i<c.size(); i++)
        printf("%ld ", c[i]);

    return 0;
}


#include <stdio.h>
#include <vector>
#include <algorithm>
using namespace std;

long d[500][500];
long prev[500];

vector<long> LCIS (vector<long> a, vector<long> b, long n, long m) {
    for (long i=0; i<=n; i++) d[i][0]=0;
    for (long j=0; j<=m; j++) d[0][j]=0;

    long best, ind;
    for (long i=1; i<=n; i++) {
        best = ind = 0;
        for (long j=1; j<=m; j++) {
            d[i][j] = d[i-1][j];
            if (a[i-1]==b[j-1] && best+1 > d[i][j]) {
                d[i][j] = best+1;
                prev[j] = ind;
            }
            if (b[j-1] < a[i-1] && d[i-1][j] > best) {
                best = d[i-1][j];
                ind = j;
            }
        }
    }

    long pos=1;
    for (long j=1; j<n; j++)
        if (d[n][j] > d[n][pos])
            pos = j;

    vector<long> ans;
    while (pos!=0) {
        ans.push_back(b[pos-1]);
        pos = prev[pos];
    }
    reverse(ans.begin(), ans.end());
    return ans;
}

int main()
{
    long n, m, item;
    vector<long> a, b, c;
    scanf("%ld", &n);
    for (long i=0; i<n; i++) {
        scanf("%ld", &item);
        a.push_back(item);
    }
    scanf("%ld", &m);
    for (long i=0; i<m; i++) {
        scanf("%ld", &item);
        b.push_back(item);
    }

    c=LCIS(a,b, n, m);
    printf("%ld\n", c.size());
    for (long i=0; i<c.size(); i++)
        printf("%ld ", c[i]);

    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
using namespace std;
typedef long long LL;

struct cand
{
	vector<int> frontier;
	vector<int> history;
	cand() {}
	cand(int N) : frontier(N, -1) {}
	void swap(cand& rhs) {
		frontier.swap(rhs.frontier);
		history.swap(rhs.history);
	}
};

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

	vector< vector<int> > s(N);
	vector< vector< vector<int> > > idx(N);
	vector<int> chs;
	for(int i=0; i<N; ++i)
	{
		int m;
		cin >> m;
		s[i].resize(m);
		idx[i].resize(256);
		for(int j=0; j<m; ++j) {
			cin >> s[i][j];
			idx[i][s[i][j]].push_back(j);
			chs.push_back(s[i][j]);
		}
	}
	sort(chs.begin(), chs.end());
	chs.erase(unique(chs.begin(), chs.end()), chs.end());

	vector<cand> ans(1, cand(N));

	int curbest = 0;
	for(int cc=0; cc<chs.size(); ++cc)
	{
		int c = chs[cc];
		map<vector<int>, vector<int> > opt;

		int MM = ans.size();
		for(int k=0; k<MM; ++k) {
			const cand& a = ans[k];

			vector<int> fron(N);
			for(int i=0; i<N; ++i) {
				vector<int>::iterator it = lower_bound( idx[i][c].begin(), idx[i][c].end(), a.frontier[i]+1 );
				if( it == idx[i][c].end() )
					goto nextExt;
				fron[i] = *it;
			}

			{
				map<vector<int>,vector<int> >::iterator it = opt.find(fron);
				if( it == opt.end() ) {
					(opt[fron] = a.history).push_back(c);
				}
				else if( it->second.size() < a.history.size()+1 ) {
					it->second = a.history;
					it->second.push_back(c);
				}
			}
		nextExt:;
		}

		vector<cand> aa;
		int rest = chs.size()-cc-1;

		for(map<vector<int>,vector<int> >::iterator it=opt.begin(), ed=opt.end(); it!=ed; ++it)
			if( it->second.size()+rest > curbest ) {
				aa.resize(aa.size()+1);
				aa.back().frontier = it->first;
				aa.back().history = it->second;
				curbest = max<int>(curbest, it->second.size());
			}
		for(int i=0; i<ans.size(); ++i)
			if( ans[i].history.size()+rest > curbest ) {
				aa.resize(aa.size()+1);
				aa.back().swap(ans[i]);
			}

		ans.swap(aa);
	}

	cand* pc = 0;
	for(int i=0; i<ans.size(); ++i)
		if(!pc || pc->history.size() < ans[i].history.size() )
			pc = &ans[i];
	cout << pc->history.size() << endl;
	for(int i=0; i<pc->history.size(); ++i)
		cout << (i==0 ? "" : " ") << pc->history[i];
	cout << endl;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
using namespace std;
typedef long long LL;

struct cand
{
	vector<int> frontier;
	vector<int> history;
	cand() {}
	cand(int N) : frontier(N, -1) {}
};

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

	vector< vector<int> > s(N);
	vector< vector< vector<int> > > idx(N);
	vector<int> chs;
	for(int i=0; i<N; ++i)
	{
		int m;
		cin >> m;
		s[i].resize(m);
		idx[i].resize(256);
		for(int j=0; j<m; ++j) {
			cin >> s[i][j];
			idx[i][s[i][j]].push_back(j);
			chs.push_back(s[i][j]);
		}
	}
	sort(chs.begin(), chs.end());
	chs.erase(unique(chs.begin(), chs.end()), chs.end());

	vector<cand> ans(1, cand(N));

	for(int cc=0; cc<chs.size(); ++cc)
	{
		int c = chs[cc];
		map<vector<int>, vector<int> > opt;

		int MM = ans.size();
		for(int k=0; k<MM; ++k) {
			const cand& a = ans[k];

			vector<int> fron(N);
			for(int i=0; i<N; ++i) {
				vector<int>::iterator it = lower_bound( idx[i][c].begin(), idx[i][c].end(), a.frontier[i]+1 );
				if( it == idx[i][c].end() )
					goto nextExt;
				fron[i] = *it;
			}

			{
				map<vector<int>,vector<int> >::iterator it = opt.find(fron);
				if( it == opt.end() ) {
					(opt[fron] = a.history).push_back(c);
				}
				else if( it->second.size() < a.history.size()+1 ) {
					it->second = a.history;
					it->second.push_back(c);
				}
			}
		nextExt:;
		}

		ans.resize(MM+opt.size());
		int ii = 0;
		for(map<vector<int>,vector<int> >::iterator it=opt.begin(), ed=opt.end(); it!=ed; ++it) {
			ans[MM+ii].frontier = it->first;
			ans[MM+ii].history = it->second;
			++ii;
		}
	}

	cand* pc = 0;
	for(int i=0; i<ans.size(); ++i)
		if(!pc || pc->history.size() < ans[i].history.size() )
			pc = &ans[i];
	cout << pc->history.size() << endl;
	for(int i=0; i<pc->history.size(); ++i)
		cout << (i==0 ? "" : " ") << pc->history[i];
	cout << endl;
}


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long LL;

struct cand
{
	vector<int> frontier;
	vector<int> history;

	cand(int N) : frontier(N, -1) {}
};

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

	vector< vector<int> > s(N);
	for(int i=0; i<N; ++i)
	{
		int m;
		cin >> m;
		s[i].resize(m);
		for(int j=0; j<m; ++j)
			cin >> s[i][j];
	}

	vector<cand> ans(1, cand(N));

	for(int c=0; c<=255; ++c)
	{
		int MM = ans.size();
		for(int k=0; k<MM; ++k) {
			cand& a = ans[k];

			cand aa = a;
			for(int i=0; i<N; ++i) {
				vector<int>::iterator it =
					find( s[i].begin()+a.frontier[i]+1, s[i].end(), c );
				if( it == s[i].end() )
					goto nextExt;
				aa.frontier[i] = it-s[i].begin();
			}
			aa.history.push_back(c);

			ans.push_back(aa);
		nextExt:;
		}
	}

	cand* pc = 0;
	for(int i=0; i<ans.size(); ++i)
		if(!pc || pc->history.size() < ans[i].history.size() )
			pc = &ans[i];
	cout << pc->history.size() << endl;
	for(int i=0; i<pc->history.size(); ++i)
		cout << (i==0 ? "" : " ") << pc->history[i];
	cout << endl;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
using namespace std;
typedef long long LL;

struct cand
{
	vector<int> frontier;
	vector<int> history;
	cand() {}
	cand(int N) : frontier(N, -1) {}
};

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

	vector< vector<int> > s(N);
	vector< vector< vector<int> > > idx(N);
	for(int i=0; i<N; ++i)
	{
		int m;
		cin >> m;
		s[i].resize(m);
		idx[i].resize(256);
		for(int j=0; j<m; ++j) {
			cin >> s[i][j];
			idx[i][s[i][j]].push_back(j);
		}
	}

	vector<cand> ans(1, cand(N));

	for(int c=0; c<=255; ++c)
	{
		map<vector<int>, vector<int> > opt;

		int MM = ans.size();
		for(int k=0; k<MM; ++k) {
			const cand& a = ans[k];

			vector<int> fron(N);
			for(int i=0; i<N; ++i) {
				vector<int>::iterator it = lower_bound( idx[i][c].begin(), idx[i][c].end(), a.frontier[i]+1 );
				if( it == idx[i][c].end() )
					goto nextExt;
				fron[i] = *it;
			}

			{
				map<vector<int>,vector<int> >::iterator it = opt.find(fron);
				if( it == opt.end() ) {
					(opt[fron] = a.history).push_back(c);
				}
				else if( it->second.size() < a.history.size()+1 ) {
					it->second = a.history;
					it->second.push_back(c);
				}
			}
		nextExt:;
		}

		ans.resize(MM+opt.size());
		int ii = 0;
		for(map<vector<int>,vector<int> >::iterator it=opt.begin(), ed=opt.end(); it!=ed; ++it) {
			ans[MM+ii].frontier = it->first;
			ans[MM+ii].history = it->second;
			++ii;
		}
	}

	cand* pc = 0;
	for(int i=0; i<ans.size(); ++i)
		if(!pc || pc->history.size() < ans[i].history.size() )
			pc = &ans[i];
	cout << pc->history.size() << endl;
	for(int i=0; i<pc->history.size(); ++i)
		cout << (i==0 ? "" : " ") << pc->history[i];
	cout << endl;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
using namespace std;
typedef long long LL;

struct cand
{
	vector<int> frontier;
	vector<int> history;
	cand() {}
	cand(int N) : frontier(N, -1) {}
	void swap(cand& rhs) {
		frontier.swap(rhs.frontier);
		history.swap(rhs.history);
	}
};

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

	vector< vector<int> > s(N);
	vector< vector< vector<int> > > idx(N);
	vector<int> chs;
	for(int i=0; i<N; ++i)
	{
		int m;
		cin >> m;
		s[i].resize(m);
		idx[i].resize(256);
		for(int j=0; j<m; ++j) {
			cin >> s[i][j];
			idx[i][s[i][j]].push_back(j);
			chs.push_back(s[i][j]);
		}
	}
	sort(chs.begin(), chs.end());
	chs.erase(unique(chs.begin(), chs.end()), chs.end());

	vector<cand> ans(1, cand(N));

	int curbest = 0;
	for(int cc=0; cc<chs.size(); ++cc)
	{
		int c = chs[cc];
		map<vector<int>, vector<int> > opt;

		for(int k=0; k<ans.size(); ++k) {
			const cand& a = ans[k];

			vector<int> fron(N);
			for(int i=0; i<N; ++i) {
				vector<int>::iterator it = lower_bound( idx[i][c].begin(), idx[i][c].end(), a.frontier[i]+1 );
				if( it == idx[i][c].end() )
					goto nextExt;
				fron[i] = *it;
			}

			{
				map<vector<int>,vector<int> >::iterator it = opt.find(fron);
				if( it == opt.end() ) {
					(opt[fron] = a.history).push_back(c);
				}
				else if( it->second.size() < a.history.size()+1 ) {
					it->second = a.history;
					it->second.push_back(c);
				}
			}
		nextExt:;
		}

		vector<cand> aa;
		int rest = chs.size()-cc;

		for(map<vector<int>,vector<int> >::iterator it=opt.begin(), ed=opt.end(); it!=ed; ++it)
			if( it->second.size()+rest > curbest ) {
				aa.resize(aa.size()+1);
				aa.back().frontier = it->first;
				aa.back().history = it->second;
				curbest = max<int>(curbest, it->second.size());
			}
		for(int i=0; i<ans.size(); ++i)
			if( ans[i].history.size()+rest > curbest ) {
				aa.resize(aa.size()+1);
				aa.back().swap(ans[i]);
			}

		ans.swap(aa);
	}

	cand* pc = 0;
	for(int i=0; i<ans.size(); ++i)
		if(!pc || pc->history.size() < ans[i].history.size() )
			pc = &ans[i];
	cout << pc->history.size() << endl;
	for(int i=0; i<pc->history.size(); ++i)
		cout << (i==0 ? "" : " ") << pc->history[i];
	cout << endl;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
using namespace std;
typedef long long LL;

struct cand
{
	vector<int> frontier;
	vector<int> history;

	cand(int N) : frontier(N, -1) {}
};

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

	vector< vector<int> > s(N);
	vector< vector< set<int> > > idx(N);
	for(int i=0; i<N; ++i)
	{
		int m;
		cin >> m;
		s[i].resize(m);
		idx[i].resize(256);
		for(int j=0; j<m; ++j) {
			cin >> s[i][j];
			idx[i][s[i][j]].insert(j);
		}
	}

	vector<cand> ans(1, cand(N));

	for(int c=0; c<=255; ++c)
	{
		int MM = ans.size();
		for(int k=0; k<MM; ++k) {
			cand& a = ans[k];

			cand aa = a;
			for(int i=0; i<N; ++i) {
				set<int>::iterator it = idx[i][c].lower_bound( a.frontier[i]+1 );
				if( it == idx[i][c].end() )
					goto nextExt;
				aa.frontier[i] = *it;
/*
				vector<int>::iterator it =
					find( s[i].begin()+a.frontier[i]+1, s[i].end(), c );
				if( it == s[i].end() )
					goto nextExt;
				aa.frontier[i] = it-s[i].begin();
*/
			}
			aa.history.push_back(c);

			ans.push_back(aa);
		nextExt:;
		}
	}

	cand* pc = 0;
	for(int i=0; i<ans.size(); ++i)
		if(!pc || pc->history.size() < ans[i].history.size() )
			pc = &ans[i];
	cout << pc->history.size() << endl;
	for(int i=0; i<pc->history.size(); ++i)
		cout << (i==0 ? "" : " ") << pc->history[i];
	cout << endl;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
using namespace std;
typedef long long LL;

struct cand
{
	vector<int> frontier;
	vector<int> history;
	cand() {}
	cand(int N) : frontier(N, -1) {}
};

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

	vector< vector<int> > s(N);
	vector< vector< set<int> > > idx(N);
	for(int i=0; i<N; ++i)
	{
		int m;
		cin >> m;
		s[i].resize(m);
		idx[i].resize(256);
		for(int j=0; j<m; ++j) {
			cin >> s[i][j];
			idx[i][s[i][j]].insert(j);
		}
	}

	vector<cand> ans(1, cand(N));

	for(int c=0; c<=255; ++c)
	{
		map<vector<int>, vector<int> > opt;

		int MM = ans.size();
		for(int k=0; k<MM; ++k) {
			const cand& a = ans[k];

			vector<int> fron(N);
			for(int i=0; i<N; ++i) {
				set<int>::iterator it = idx[i][c].lower_bound( a.frontier[i]+1 );
				if( it == idx[i][c].end() )
					goto nextExt;
				fron[i] = *it;
			}

			{
				map<vector<int>,vector<int> >::iterator it = opt.find(fron);
				if( it == opt.end() ) {
					vector<int> hh = a.history;
					hh.push_back(c);
					opt[fron] = hh;
				}
				else if( it->second.size() < a.history.size()+1 ) {
					it->second = a.history;
					it->second.push_back(c);
				}
			}
		nextExt:;
		}

		ans.resize(MM+opt.size());
		int ii = 0;
		for(map<vector<int>,vector<int> >::iterator it=opt.begin(), ed=opt.end(); it!=ed; ++it) {
			ans[MM+ii].frontier = it->first;
			ans[MM+ii].history = it->second;
			++ii;
		}
	}

	cand* pc = 0;
	for(int i=0; i<ans.size(); ++i)
		if(!pc || pc->history.size() < ans[i].history.size() )
			pc = &ans[i];
	cout << pc->history.size() << endl;
	for(int i=0; i<pc->history.size(); ++i)
		cout << (i==0 ? "" : " ") << pc->history[i];
	cout << endl;
}


#include<stdio.h>
int a,b,A[505],B[505],res[505],C[505],prev[505],index,p=1,cur=0,last=-1,mx=-1;
int main()
{
    scanf("%d",&a);
    for(int i=1; i<=a; i++)scanf("%d",&A[i]);
    scanf("%d",&b);
    for(int i=1; i<=b; i++)scanf("%d",&B[i]);
    for(int i=1; i<=a; i++,cur=0,last=-1)
        for(int j=1; j<=b; j++)
        {
            if(A[i]==B[j]&&cur+1>C[j])C[j]=cur+1,prev[j]=last;
            if(B[j]<A[i]&&C[j]>cur)cur=C[j],last=j;
            if(C[j]>mx)mx=C[j],index=j;
        }
    if(mx>0)
    {
        printf("%d\n",mx);
        for(;index!=-1;index=prev[index])res[p++]=B[index];
        for(int i=p-1; i>=1; i--)printf("%d%s",res[i],i==1?"\n":" ");
    }
    else puts("0");
}


#include<bits/stdc++.h>
using namespace std;
int dp[505][505],LA,LB,A[505],B[505];
int vis[505][505];
struct data
{
    int i,j;
} dir[505][505];
int longest(int u,int k)
{
    if(vis[u][k])return dp[u][k];
    vis[u][k]=true;
    int maxi=0;
    for(int i=u+1; i<=LA; i++)
    {
        for(int j=k+1; j<=LB; j++)
        {
            if(A[i]==B[j]&&A[i]>A[u])
            {
                if(longest(i,j)>maxi)
                {
                    maxi=longest(i,j);
                    dir[u][k].i=i;
                    dir[u][k].j=j;
                }
            }
        }
    }
    return dp[u][k]=maxi+1;
}
int main()
{
    memset(dp,-1,sizeof dp);
    cin>>LA;
    for(int i=1; i<=LA; i++)cin>>A[i];
    cin>>LB;
    for(int i=1; i<=LB; i++)cin>>B[i];
    int MX=0,startA=0,doll=0,startB;
    for(int i=1; i<=LA; i++)
        for(int j=1; j<=LB; j++)
        {
            if(A[i]!=B[j])dp[i][j]=0;
            else if(longest(i,j)>MX)
            {
                MX=longest(i,j);
                startA=i;
                startB=j;
            }
        }
    //MX=longest(startA,startB);
    cout<<MX<<endl;
    if(MX)
    {
        MX--;
        cout<<A[startA]<<" ";
        while(MX--)
        {
            cout<<A[dir[startA][startB].i]<<" ";
            int a=dir[startA][startB].i;
            int b=dir[startA][startB].j;
            startA=a;
            startB=b;
        }
        cout<<endl;
    }
    //cout<<A[startA]<<endl;
}
/*
3
37 49 24
3
33 5 70
*/


#include<bits/stdc++.h>
using namespace std;
int dp[505][505],LA,LB,A[505],B[505];
int vis[505][505];
struct data
{
    int i,j;
} dir[505][505];
int longest(int u,int k)
{
//ne kha
    if(vis[u][k])return dp[u][k];
    vis[u][k]=true;
    int maxi=0;
    for(int i=u+1; i<=LA; i++)
    {
        for(int j=k+1; j<=LB; j++)
        {
            if(A[i]==B[j]&&A[i]>A[u])
            {
                if(longest(i,j)>maxi)
                {
                    maxi=longest(i,j);
                    dir[u][k].i=i;
                    dir[u][k].j=j;
                }
            }
        }
    }
    return dp[u][k]=maxi+1;
}
int main()
{
    memset(dp,-1,sizeof dp);
    cin>>LA;
    for(int i=1; i<=LA; i++)cin>>A[i];
    cin>>LB;
    for(int i=1; i<=LB; i++)cin>>B[i];
    int MX=-1,startA=0,doll=0,startB;
    for(int i=1; i<=LA; i++)
        for(int j=1; j<=LB; j++)
        {
            if(A[i]!=B[j])dp[i][j]=0;
            else if(longest(i,j)>MX)
            {
                MX=longest(i,j);
                startA=i;
                startB=j;
            }
        }
    //MX=longest(startA,startB);
    cout<<MX<<endl;
    MX--;
    cout<<A[startA]<<" ";
    while(MX--)
    {
        cout<<A[dir[startA][startB].i]<<" ";
        int a=dir[startA][startB].i;
        int b=dir[startA][startB].j;
        startA=a;
        startB=b;
    }
    cout<<endl;
    //cout<<A[startA]<<endl;
}
/*
5
1 5 5 8 7
5
0 12 2 5 7

3
5 2 3
3
2 5 3
6
2 9 4 5 6 7
10
1 3 87 2 65 9 45 34 5 7
*/


#include<bits/stdc++.h>
using namespace std;
int dp[505][505],LA,LB,A[505],B[505];
int vis[505][505];
struct data
{
    int i,j;
} dir[505][505];
int longest(int u,int k)
{
    if(vis[u][k])return dp[u][k];
    vis[u][k]=true;
    int maxi=0;
    for(int i=u+1; i<=LA; i++)
    {
        for(int j=k+1; j<=LB; j++)
        {
            if(A[i]==B[j]&&A[i]>A[u])
            {
                if(longest(i,j)>maxi)
                {
                    maxi=longest(i,j);
                    dir[u][k].i=i;
                    dir[u][k].j=j;
                }
            }
        }
    }
    return dp[u][k]=maxi+1;
}
int main()
{
    memset(dp,-1,sizeof dp);
    cin>>LA;
    for(int i=1; i<=LA; i++)cin>>A[i];
    cin>>LB;
    for(int i=1; i<=LB; i++)cin>>B[i];
    int MX=-1,startA=0,doll=0,startB;
    for(int i=1; i<=LA; i++)
        for(int j=1; j<=LB; j++)
        {
            if(A[i]!=B[j])dp[i][j]=0;
            else if(longest(i,j)>MX)
            {
                MX=longest(i,j);
                startA=i;
                startB=j;
            }
        }
    //MX=longest(startA,startB);
    cout<<MX<<endl;
    MX--;
    cout<<A[startA]<<" ";
    while(MX--)
    {
        cout<<A[dir[startA][startB].i]<<" ";
        int a=dir[startA][startB].i;
        int b=dir[startA][startB].j;
        startA=a;
        startB=b;
    }
    cout<<endl;
    //cout<<A[startA]<<endl;
}
/*
5
1 5 5 8 7
5
0 12 2 5 7

3
5 2 3
3
2 5 3
6
2 9 4 5 6 7
10
1 3 87 2 65 9 45 34 5 7
*/


#include<stdio.h>
int a,b,A[505],B[505],res[505],C[505],prev[505],index,p=1,cur=0,last=-1,mx=-1;
int main()
{
    scanf("%d",&a);
    for(int i=1; i<=a; i++)scanf("%d",&A[i]);
    scanf("%d",&b);
    for(int i=1; i<=b; i++)scanf("%d",&B[i]);
    for(int i=1; i<=a; i++,cur=0,last=-1)
        for(int j=1; j<=b; j++)
        {
            if(A[i]==B[j]&&cur+1>C[j])C[j]=cur+1,prev[j]=last;
            if(B[j]<A[i]&&C[j]>cur)cur=C[j],last=j;
            if(C[j]>mx)mx=C[j],index=j;
        }
    if(mx>0)
    {
        //printf("%d\n",index);
        printf("%d\n",mx);
        //for(int i=0;i<lenB;i++)if(mx>lenB)
        for(;index!=-1;index=prev[index])res[p++]=B[index];
        for(int i=mx;i>=0; i--)printf("%d%s",res[i],i==1?"\n":" ");
    }
    else puts("0");
}


#include<stdio.h>
int a,b,A[505],B[505],res[505],C[505],prev[505],index,p=1,cur=0,last=-1,mx=-1;
int main()
{
    scanf("%d",&a);
    for(int i=1; i<=a; i++)scanf("%d",&A[i]);
    scanf("%d",&b);
    for(int i=1; i<=b; i++)scanf("%d",&B[i]);
    for(int i=1; i<=a; i++,cur=0,last=-1)
        for(int j=1; j<=b; j++)
        {
            if(A[i]==B[j]&&cur+1>C[j])C[j]=cur+1,prev[j]=last;
            if(B[j]<A[i]&&C[j]>cur)cur=C[j],last=j;
            if(C[j]>mx)mx=C[j],index=j;
        }
    if(mx>0)
    {
        //printf("%d\n",index);
        printf("%d\n",mx);
        //for(int i=0;i<lenB;i++)if(mx>lenB)
        for(;index!=-1;index=prev[index])res[p++]=B[index];
        for(int i=mx;i>=1; i--)printf("%d%s",res[i],i==1?"\n":" ");
    }
    else puts("0");
}


#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
#define pb push_back
#define f first
#define s second
#define mp make_pair
using namespace std;
int n, m, a[600], b[600], d[600], pr[600], p[600], ma, ansx, xma;
vector < int > ans;
int main(){
	ma = 0;
	cin >> n;
	for(int i = 1;i <= n;i ++){
		cin >> a[i];
	}
	cin >> m;
	for(int i = 1;i <= m;i ++){
		cin >> b[i];
	}
	for(int i = 1;i <= n;i ++){
		for(int j = 1;j <= m;j ++){
			if(a[i] > b[j]){
				if(d[j] > ma){
					ma = d[j];
					xma = j;
				}
			}
			if(a[i] == b[j]){
				if(d[j] < ma + 1){
					p[j] = i;
					d[j] = ma + 1;
					pr[i] = p[xma];
				}
				ansx = max(ansx, j);
			}
		}
		ma = 0;
		xma = 0;
	}
	//for(int i = 0;i <= n;i ++){
	//	cout << pr[i] << " ";
	//}
	//cout << endl;

	//for(int i = 1;i <= m;i ++){
//		cout << d[i] << " ";
	//}
	//cout << endl;
	//return 0;
	ansx = p[ansx];
	while(ansx){
		ans.pb(a[ansx]);
		ansx = pr[ansx];
	}
	reverse(ans.begin(),ans.end());
	cout << ans.size() << endl;
	for(int i = 0;i < ans.size();i ++){
		cout << ans[i] << " ";
	}
}


#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
#define pb push_back
#define f first
#define s second
#define mp make_pair
using namespace std;
int n, m, a[600], b[600], d[600], pr[600], p[600], ma, ansx;
vector < int > ans;
int main(){
	ma = -1e9;
	cin >> n;
	for(int i = 1;i <= n;i ++){
		cin >> a[i];
	}
	cin >> m;
	for(int i = 1;i <= m;i ++){
		cin >> b[i];
	}
	for(int i = 1;i <= n;i ++){
		for(int j = 1;j <= m;j ++){
			if(a[i] > b[j]){
				ma = max(ma, d[j]);
			}
			else if(a[i] == b[j]){
				if(d[j] < ma + 1){
					p[j] = i;
				}
				pr[i] = p[j - 1];
				ansx = max(ansx, j);
			}
		}
	}
	ansx = p[ansx];
	while(ansx){
		ans.pb(a[ansx]);
		ansx = pr[ansx];
	}
	reverse(ans.begin(),ans.end());
	cout << ans.size() << endl;
	for(int i = 0;i < ans.size();i ++){
		cout << ans[i] << " ";
	}
}


#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
#define pb push_back
#define f first
#define s second
#define mp make_pair
using namespace std;
int n, m, a[600], b[600], d[600], pr[600], p[600], ma, ansx;
vector < int > ans;
int main(){
	ma = -1e9;
	cin >> n;
	for(int i = 1;i <= n;i ++){
		cin >> a[i];
	}
	cin >> m;
	for(int i = 1;i <= m;i ++){
		cin >> b[i];
	}
	for(int i = 1;i <= m;i ++){
		d[i] = -1e9;
	}
	for(int i = 1;i <= n;i ++){
		for(int j = 1;j <= m;j ++){
			if(a[i] > b[j]){
				ma = max(ma, d[j]);
			}
			else if(a[i] == b[j]){
				if(d[j] < ma + 1){
					p[j] = i;
				}
				pr[i] = p[j - 1];
				ansx = max(ansx, j);
			}
		}
	}
	ansx = p[ansx];
	while(ansx){
		ans.pb(a[ansx]);
		ansx = pr[ansx];
	}
	reverse(ans.begin(),ans.end());
	cout << ans.size() << endl;
	for(int i = 0;i < ans.size();i ++){
		cout << ans[i] << " ";
	}
}


#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
#define pb push_back
#define f first
#define s second
#define mp make_pair
using namespace std;
int n, m, a[600], b[600], d[600], pr[600], p[600], ma, ansx, xma, ansy;
vector < int > ans;
int main(){
	ma = 0;
	cin >> n;
	for(int i = 1;i <= n;i ++){
		cin >> a[i];
	}
	cin >> m;
	for(int i = 1;i <= m;i ++){
		cin >> b[i];
	}
	for(int i = 1;i <= n;i ++){
		for(int j = 1;j <= m;j ++){
			if(a[i] > b[j]){
				if(d[j] > ma){
					ma = d[j];
					xma = j;
				}
			}
			if(a[i] == b[j]){
				if(d[j] < ma + 1){
					p[j] = i;
					d[j] = ma + 1;
					pr[i] = p[xma];
					if (d[j] > ansy){
                        ansy = d[j];
                        ansx = j;
                    }
				}

			}
		}
        ma = 0;
        xma = 0;
	}
	//cout << ansx << ' ' << d[ansx] << ' ' << p[ansx]  << "##" << endl;
	ansx = p[ansx];
	while(ansx){
		ans.pb(a[ansx]);
		ansx = pr[ansx];
	}
	//for(int i = 1;i <= m;i ++){
    //    cout << pr[i] << " " << d[i]  << "  " << b[i] << endl;
	//}
	//cout << endl;
	reverse(ans.begin(),ans.end());
	cout << ans.size() << endl;
	for(int i = 0;i < ans.size();i ++){
		cout << ans[i] << " ";
	}
}


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

#define pi acos(-1)
#define _max 25
#define inf 0xfffffff
#define eps 0.000001
#define lp(i,n) for(int i=0;i<n;i++)
#define lpb(i,n) for(int i=1;i<=n;i++)
#define smap(p,ma) for(p=ma.begin();p!=ma.end();p++)
#define MA(a,b) ((a)>(b)?(a):(b))
#define MI(a,b) ((a)<(b)?(a):(b))
#define PB push_back
#define FIR first
#define SEC second
#define MP make_pair
#define PQ priority_queue
#define MSET multiset
#define NPS string::npos
#define debug
#define DB double
#define MOD 1000000007
using namespace std;
int a[505],b[505],c[505],d[505];
int n,m,p,cou,ma,mai;
vector <int> ans;
main(){

    scanf("%d",&n); lpb(i,n) scanf("%d",a+i);
    scanf("%d",&m); lpb(i,m) scanf("%d",b+i);

    lpb(i,n){
      p=0;cou=0;
      lpb(j,m){
        if(a[i]==b[j] && cou+1>c[j])
          c[j]=cou+1, d[j]=p;
        else if(a[i]>b[j] && c[j]>cou)
          cou=c[j],p=j;

        if(j==m){
          if(c[j]>ma) ma=c[j],mai=j;
        }
      }
    }
    printf("%d\n",ma);
    if(ma)
    while(mai){
      ans.PB(b[mai]);
      mai=d[mai];
    }
    for(int i=ans.size()-1;i>=0;i--)
    printf("%d ",ans[i]);

}


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

#define pi acos(-1)
#define _max 25
#define inf 0xfffffff
#define eps 0.000001
#define lp(i,n) for(int i=0;i<n;i++)
#define lpb(i,n) for(int i=1;i<=n;i++)
#define smap(p,ma) for(p=ma.begin();p!=ma.end();p++)
#define MA(a,b) ((a)>(b)?(a):(b))
#define MI(a,b) ((a)<(b)?(a):(b))
#define PB push_back
#define FIR first
#define SEC second
#define MP make_pair
#define PQ priority_queue
#define MSET multiset
#define NPS string::npos
#define debug
#define DB double
#define MOD 1000000007
using namespace std;
int a[505],b[505],c[505],d[505][505];
int n,m,p,cou,ma,mai;
vector <int> ans;
main(){

    scanf("%d",&n); lpb(i,n) scanf("%d",a+i);
    scanf("%d",&m); lpb(i,m) scanf("%d",b+i);

    lpb(i,n){
      p=0;cou=0;
      lpb(j,m){
        if(a[i]==b[j] && cou+1>c[j])
          c[j]=cou+1, d[i][j]=p;
        else if(a[i]>b[j] && c[j]>cou)
          cou=c[j],p=j;

        if(i==n){
          if(c[j]>ma) ma=c[j],mai=j;
        }
      }
    }

    printf("%d\n",ma);
    if(ma){
      int cy = mai;
      int cx = n;
      while( a[cx] != b[cy]) cx--;
      ans.PB(b[cy]);

      while(d[cx][cy]){
        cy = d[cx][cy];
        while( a[cx] != b[cy]) cx--;
        ans.PB(b[cy]);
      }
      for(int i=ans.size()-1;i>=0;i--)
       if(i==0) printf("%d",ans[0]);
       else printf("%d ",ans[i]);
    }
     printf("\n");

}


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

#define pi acos(-1)
#define _max 25
#define inf 0xfffffff
#define eps 0.000001
#define lp(i,n) for(int i=0;i<n;i++)
#define lpb(i,n) for(int i=1;i<=n;i++)
#define smap(p,ma) for(p=ma.begin();p!=ma.end();p++)
#define MA(a,b) ((a)>(b)?(a):(b))
#define MI(a,b) ((a)<(b)?(a):(b))
#define PB push_back
#define FIR first
#define SEC second
#define MP make_pair
#define PQ priority_queue
#define MSET multiset
#define NPS string::npos
#define debug
#define DB double
#define MOD 1000000007
using namespace std;
int a[505],b[505],c[505],d[505];
int n,m,p,cou,ma,mai;
vector <int> ans;
main(){

    scanf("%d",&n); lpb(i,n) scanf("%d",a+i);
    scanf("%d",&m); lpb(i,m) scanf("%d",b+i);

    lpb(i,n){
      p=0;cou=0;
      lpb(j,m){
        if(a[i]==b[j] && cou+1>c[j])
          c[j]=cou+1, d[j]=p;
        else if(a[i]>b[j] && c[j]>cou)
          cou=c[j],p=j;

        if(i==n){
          if(c[j]>ma) ma=c[j],mai=j;
        }
      }
    }

    printf("%d\n",ma);
    if(ma)
    while(mai){
      ans.PB(b[mai]);
      mai=d[mai];
    }
    for(int i=ans.size()-1;i>=0;i--)
    printf("%d ",ans[i]);

}


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

const int MAXN = 510;

int X[MAXN];
int Y[MAXN];

int trace[MAXN];
int dp[MAXN];

int N, M;
vector <int> ans;
int main() {
	scanf("%d", &N);
	for(int i = 0; i < N; ++i) {
		scanf("%d", X + i);
	}

	scanf("%d", &M);
	for(int j = 0; j < M; ++j) {
		scanf("%d", Y + j);
	}

	int best = 0;
	int start = -1;
	for(int i = N - 1; i >= 0; --i) {
		int nxt = -1;
		int val = 0;
		for(int j = M - 1; j >= 0; --j) {
			if (X[i] == Y[j]) {
				trace[j] = nxt;
				dp[j] = val + 1;
			}
			if (dp[j] > val && X[i] < Y[j]) {
				val = dp[j];
				nxt = j;
			}
			if (dp[j] > best) {
				best = dp[j];
				start = j;
			}
		}
	}

	printf("%d\n", best);
	if (best) {
		while(start!=-1){
          ans.push_back(Y[start]);
          start=trace[start];
       }
    for(int i=0;i<ans.size();i++)
      if(i==0) printf("%d",ans[0]);
      else printf(" %d",ans[i]);

	}
	printf("\n");

	return 0;

}


#include<cstdio>
#define maxn 505
int N,M,a[maxn],b[maxn],dp[maxn][maxn],ans,last,before[maxn];
void out(int x){
	if(!x)return ;
	out(before[x]);
	printf("%d ",b[x]);
	return ;
}
int main(){
	scanf("%d",&N);
	for(int i=1;i<=N;++i)scanf("%d",&a[i]);
	scanf("%d",&M);
	for(int i=1;i<=M;++i)scanf("%d",&b[i]);
	for(int i=1;i<=N;++i){
		for(int j=1;j<=M;++j){
			if(a[i]==b[j]){
				int pos=0;
				for(int k=1;k<j;++k)if(b[k]<a[i]&&dp[i][j]<dp[i-1][k])dp[i][j]=dp[i-1][k],pos=k;
				dp[i][j]++;
				if(ans<dp[i][j])ans=dp[i][j];
				before[j]=pos;
			}
			else dp[i][j]=dp[i-1][j];
		}
	}
	for(int i=1,t=0;i<=M;++i){
		if(dp[N][i]>t)t=dp[N][i],last=i;
	}
	printf("%d\n",ans);
//	printf("%d ",b[last]);
	out(last);
	return 0;
}


#include<cstdio>
#define maxn 505
inline int Max(int x,int y){return x>y?x:y;}
int N,M,dp[maxn][maxn],a[maxn],b[maxn],ans;
int before[maxn][maxn],last;
void out(int x){
	if(!x)return ;
	out(before[N][x]);
	printf("%d ",b[x]);
	return ;
}
int main(){
	scanf("%d",&N);
	for(int i=1;i<=N;++i)scanf("%d",&a[i]);
	scanf("%d",&M);
	for(int i=1;i<=M;++i)scanf("%d",&b[i]);
	for(int i=1;i<=N;++i)
	for(int j=1;j<=M;++j){
		if(a[i]!=b[j])dp[i][j]=dp[i-1][j],before[i][j]=before[i-1][j];
		else {
			int temp=0;
			for(int k=1;k<j;++k)if(b[k]<a[i]&&dp[i][j]<dp[i-1][k])temp=k,dp[i][j]=dp[i-1][k];
			dp[i][j]++;
			if(ans<dp[i][j]){
				ans=dp[i][j],before[i][j]=temp;
				if(i==N)last=j;
			}
		}
	}
	printf("%d\n",ans);
	printf("%d ",before[N][last]);
	out(last);
	return 0;
}


#include<cstdio>
#define maxn 505
inline int Max(int x,int y){return x>y?x:y;}
int N,M,dp[maxn][maxn],a[maxn],b[maxn],ans;
int before[maxn][maxn],last;
void out(int x){
	if(!x)return ;
	out(before[N][x]);
	printf("%d ",b[x]);
	return ;
}
int main(){
	scanf("%d",&N);
	for(int i=1;i<=N;++i)scanf("%d",&a[i]);
	scanf("%d",&M);
	for(int i=1;i<=M;++i)scanf("%d",&b[i]);
	for(int i=1;i<=N;++i)
	for(int j=1;j<=M;++j){
		if(a[i]!=b[j])dp[i][j]=dp[i-1][j];
		else {
			int temp=0;
			for(int k=1;k<j;++k)if(b[k]<b[j]&&dp[i][j]<dp[i-1][k])temp=k,dp[i][j]=dp[i-1][k];
			dp[i][j]++;
			if(ans<dp[i][j]){
				ans=dp[i][j],before[i][j]=temp;
				if(i==N)last=j;
			}
		}
	}
	printf("%d\n",ans);
	printf("%d ",before[N][last]);
	out(last);
	return 0;
}


#include<cstdio>
#define maxn 505
inline int Max(int x,int y){return x>y?x:y;}
int N,M,dp[maxn][maxn],a[maxn],b[maxn],ans;
int before[maxn][maxn],last;
void out(int x){
	if(!x)return ;
	out(before[N][x]);
	printf("%d ",b[x]);
	return ;
}
int main(){
	scanf("%d",&N);
	for(int i=1;i<=N;++i)scanf("%d",&a[i]);
	scanf("%d",&M);
	for(int i=1;i<=M;++i)scanf("%d",&b[i]);
	for(int i=1;i<=N;++i)
	for(int j=1;j<=M;++j){
		if(a[i]!=b[j])dp[i][j]=dp[i-1][j],before[i][j]=before[i-1][j];
		else {
			int temp=0;
			for(int k=1;k<j;++k)if(b[k]<b[j]&&dp[i][j]<dp[i-1][k])temp=k,dp[i][j]=dp[i-1][k];
			dp[i][j]++;
			if(ans<dp[i][j]){
				ans=dp[i][j],before[i][j]=temp;
				if(i==N)last=j;
			}
		}
	}
	printf("%d\n",ans);
	printf("%d ",before[N][last]);
	out(last);
	return 0;
}


#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<map>
#include<queue>
#include<vector>
#include<bitset>
#include<set>
using namespace std;
#define INF 0x7fffffff
int a[1010],b[1010],f[1010][1010],pre[1010][1010][2],ans[1010];
int read()
{
    int s=0,fh=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')fh=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){s=s*10+(ch-'0');ch=getchar();}
    return s*fh;
}
int main()
{
    //freopen("in","r",stdin);
    int n,m,i,j,k,px,py,mx,lans,px1,py1;
    while(scanf("%d",&n)!=EOF)
    {
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        for(i=1;i<=n;i++)a[i]=read();
        m=read();
        for(i=1;i<=m;i++)b[i]=read();
        memset(f,0,sizeof(f));//f[i][j]为a数组到i位置,b数组到j位置的最长公共上升子序列的长度
        memset(pre,0,sizeof(pre));
        a[0]=b[0]=-INF;
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=m;j++)
            {
                if(a[i]!=b[j])
                {
                    f[i][j]=f[i-1][j];
                    pre[i][j][0]=i-1;
                    pre[i][j][1]=j;
                }
                else
                {
                    for(k=0;k<j;k++)
                    {
                        if(b[k]<b[j]&&f[i-1][k]+1>f[i][j])
                        {
                            f[i][j]=f[i-1][k]+1;
                            pre[i][j][0]=i-1;
                            pre[i][j][1]=k;
                        }
                    }
                }
            }
        }
        px=0;py=0;mx=0;
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=m;j++)
            {
                if(f[i][j]>mx){mx=f[i][j];px=i;py=j;}
            }
        }
        printf("%d\n",mx);
        lans=0;
        while(1)
        {
            //mx--;if(mx==0)break;
            px1=px;py1=py;
            px=pre[px1][py1][0];
            py=pre[px1][py1][1];
            if(f[px1][py1]!=f[px][py])ans[++lans]=b[py1];
            if(f[px][py]==0)break;
        }
        for(i=lans;i>=1;i--)printf("%d ",ans[i]);
        printf("\n");
    }
    fclose(stdin);
    fclose(stdout);
    return 0;
}


#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<map>
#include<queue>
#include<vector>
#include<bitset>
#include<set>
using namespace std;
#define INF 1000000000000000LL
#define LL long long
LL a[1010],b[1010],f[1010][1010],pre[1010][1010][2],ans[1010];
LL read()
{
    LL s=0,fh=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')fh=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){s=s*10+(ch-'0');ch=getchar();}
    return s*fh;
}
int main()
{
    //freopen("in","r",stdin);
    LL n,m,i,j,k,px,py,mx,lans,px1,py1,mxx,mxy;
    //while(scanf("%lld",&n)!=EOF)
    {
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        n=read();
        for(i=1;i<=n;i++)a[i]=read();
        m=read();
        for(i=1;i<=m;i++)b[i]=read();
        memset(f,0,sizeof(f));//f[i][j]为a数组到i位置,b数组到j位置的最长公共上升子序列的长度
        memset(pre,0,sizeof(pre));
        a[0]=b[0]=-INF;
        for(i=1;i<=n;i++)
        {
            mx=0;mxx=0;mxy=0;
            for(j=1;j<=m;j++)
            {
                if(a[i]!=b[j])
                {
                    f[i][j]=f[i-1][j];
                    pre[i][j][0]=i-1;
                    pre[i][j][1]=j;
                }
                else
                {
                    f[i][j]=mx+1;
                    pre[i][j][0]=mxx;
                    pre[i][j][1]=mxy;
                    /*for(k=0;k<j;k++)
                    {
                        if(b[k]<b[j]&&f[i-1][k]+1>f[i][j])
                        {
                            f[i][j]=f[i-1][k]+1;
                            pre[i][j][0]=i-1;
                            pre[i][j][1]=k;
                        }
                    }*/
                }
                if(b[j]<a[i])
                {
                    if(f[i-1][j]>mx)
                    {
                        mx=f[i-1][j];
                        mxx=i-1;
                        mxy=j;
                    }
                }
            }
        }
        px=0;py=0;mx=0;
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=m;j++)
            {
                if(f[i][j]>mx){mx=f[i][j];px=i;py=j;}
            }
        }
        printf("%lld\n",mx);
        lans=0;
        while(1)
        {
            //mx--;if(mx==0)break;
            px1=px;py1=py;
            px=pre[px1][py1][0];
            py=pre[px1][py1][1];
            if(f[px1][py1]!=f[px][py])ans[++lans]=b[py1];
            if(f[px][py]==0)break;
        }
        for(i=lans;i>=1;i--)printf("%lld ",ans[i]);
        printf("\n");
    }
    fclose(stdin);
    fclose(stdout);
    return 0;
}


#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<map>
#include<queue>
#include<vector>
#include<bitset>
#include<set>
using namespace std;
#define INF 1000000000000000LL
#define LL long long
LL a[1010],b[1010],f[1010][1010],pre[1010][1010][2],ans[1010];
LL read()
{
    LL s=0,fh=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')fh=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){s=s*10+(ch-'0');ch=getchar();}
    return s*fh;
}
int main()
{
    //freopen("in","r",stdin);
    LL n,m,i,j,k,px,py,mx,lans,px1,py1;
    while(scanf("%lld",&n)!=EOF)
    {
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        for(i=1;i<=n;i++)a[i]=read();
        m=read();
        for(i=1;i<=m;i++)b[i]=read();
        memset(f,0,sizeof(f));//f[i][j]为a数组到i位置,b数组到j位置的最长公共上升子序列的长度
        memset(pre,0,sizeof(pre));
        a[0]=b[0]=-INF;
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=m;j++)
            {
                if(a[i]!=b[j])
                {
                    f[i][j]=f[i-1][j];
                    pre[i][j][0]=i-1;
                    pre[i][j][1]=j;
                }
                else
                {
                    for(k=0;k<j;k++)
                    {
                        if(b[k]<b[j]&&f[i-1][k]+1>f[i][j])
                        {
                            f[i][j]=f[i-1][k]+1;
                            pre[i][j][0]=i-1;
                            pre[i][j][1]=k;
                        }
                    }
                }
            }
        }
        px=0;py=0;mx=0;
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=m;j++)
            {
                if(f[i][j]>mx){mx=f[i][j];px=i;py=j;}
            }
        }
        printf("%lld\n",mx);
        lans=0;
        while(1)
        {
            //mx--;if(mx==0)break;
            px1=px;py1=py;
            px=pre[px1][py1][0];
            py=pre[px1][py1][1];
            if(f[px1][py1]!=f[px][py])ans[++lans]=b[py1];
            if(f[px][py]==0)break;
        }
        for(i=lans;i>=1;i--)printf("%lld ",ans[i]);
        printf("\n");
    }
    fclose(stdin);
    fclose(stdout);
    return 0;
}