#include<bits/stdc++.h>
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define rep(i,a,b) for(ll i=(a);i<=(b);++i)
#define per(i,a,b) for(ll i=(a);i>=(b);--i)
#define pii pair<ll,ll>
using namespace std;
ll read(){
ll x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
void write(ll x){
if(x<0)putchar('-'),x=-x;
if(x>9)write(x/10);
putchar(x%10+'0');
}
const ll N=505;
ll n,m,a[N],b[N],dp[N][N],p[N][N],ans,pos;
stack<ll>stk;
int main(){
n=read();
rep(i,1,n)a[i]=read();
m=read();
rep(i,1,m)b[i]=read();
a[0]=-1,b[0]=-1;
rep(i,1,n){
rep(j,1,m){
if(a[i]==b[j]){
rep(k,0,j-1){
if(a[i]>b[k]&&dp[i][j]<dp[i-1][k]+1){
dp[i][j]=dp[i-1][k]+1;
p[i][j]=k;
}
}
}
else {
dp[i][j]=dp[i-1][j];
p[i][j]=p[i-1][j];
}
}
}
rep(i,1,m){
if(dp[n][i]>ans)ans=dp[n][i],pos=i;
}
write(ans),putchar('\n');
while(pos)stk.push(b[pos]),pos=p[n][pos];
while(stk.size())write(stk.top()),putchar(' '),stk.pop();
return 0;//
}
//11026622161443297331
1613A - Long Comparison | 1624B - Make AP |
660B - Seating On Bus | 405A - Gravity Flip |
499B - Lecture | 709A - Juicer |
1358C - Celex Update | 1466B - Last minute enhancements |
450B - Jzzhu and Sequences | 1582C - Grandma Capa Knits a Scarf |
492A - Vanya and Cubes | 217A - Ice Skating |
270A - Fancy Fence | 181A - Series of Crimes |
1638A - Reverse | 1654C - Alice and the Cake |
369A - Valera and Plates | 1626A - Equidistant Letters |
977D - Divide by three multiply by two | 1654B - Prefix Removals |
1654A - Maximum Cake Tastiness | 1649A - Game |
139A - Petr and Book | 1612A - Distance |
520A - Pangram | 124A - The number of positions |
1041A - Heist | 901A - Hashing Trees |
1283A - Minutes Before the New Year | 1654D - Potion Brewing Class |