//https://www.luogu.com.cn/problem/CF10D
#include<bits/stdc++.h>
#include<unordered_map>
#include<array>
#define ll long long
#define ull unsigned long long
#define all(a) a.begin(),a.end()
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-8;
const ll mod = 1e9 + 7;
const int N = 500 + 5;
int n, m, a[N], b[N], dp[N][N];//dp[i][j]:以 a[i] 和 b[j] 结尾的最大长度
map<int, vector<int>> num;
pair<int, int> path[N][N];//路径记录
void solve()
{
cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i];
cin >> m;
for (int i = 1; i <= m; i++)
cin >> b[i], num[b[i]].push_back(i);
a[0] = b[0] = -1;//使得所有相同的数可以从0转移过去,产生答案
num[-1].push_back(0);
int ans = 0;
for (int p = 1; p <= n; p++)//以a[p]结尾
{
for (int i = 0; i < p; i++)//
{
if (a[p] <= a[i])//找p之前满足条件的i,j进行转移
continue;
for (int j : num[a[i]])
{
for (int q = j + 1; q <= m; q++)
{
if (a[p] == b[q])
{
dp[p][q] = dp[i][j] + 1;
path[p][q] = { i, j };
ans = max(ans, dp[p][q]);
}
}
}
}
}
cout << ans << '\n';
if (ans)
{
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
if (dp[i][j] == ans)
{
vector<int> res;
int x = i, y = j;
while (x && y)
{
res.push_back(a[x]);
auto now = path[x][y];
x = now.first;
y = now.second;
}
reverse(all(res));
for (int it : res)
cout << it << ' ';
return;
}
}
}
}
}
signed main()
{
IOS;
int t = 1;
//cin >> t;
while (t--)
solve();
return 0;
}
#include<bits/stdc++.h>
#include<unordered_map>
#include<array>
#define ll long long
#define ull unsigned long long
#define all(a) a.begin(),a.end()
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-8;
const ll mod = 1e9 + 7;
const int N = 500 + 5;
int n, m, a[N], b[N], dp[N][N], path[N][N];//dp[i][j] : 第一个数组前i个且以b[j]结尾的长度
void solve()
{
cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i];
cin >> m;
for (int i = 1; i <= m; i++)
cin >> b[i];
a[0] = b[0] = -1;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
if (a[i] == b[j])
{
for (int k = 0; k < j; k++)
{
if (b[k] < b[j] && dp[i - 1][k] + 1 > dp[i][j])
{
dp[i][j] = dp[i - 1][k] + 1;
path[i][j] = k;
}
}
}
else
{
dp[i][j] = dp[i - 1][j];
path[i][j] = path[i - 1][j];
}
//cout << "i j dp " << i << ' ' << j << ' ' << dp[i][j] << '\n';
//cout << path[i][j] << '\n';
}
}
int ans = 0;
for (int j = 1; j <= m; j++)
ans = max(ans, dp[n][j]);
cout << ans << '\n';
if (ans)
{
for (int j = 1; j <= m; j++)
{
if (dp[n][j] == ans)
{
stack<int> st;
int x = j;
while (x)
{
st.push(b[x]);
x = path[n][x];
}
while (st.size())
{
cout << st.top() << ' ';
st.pop();
}
return;
}
}
}
}
signed main()
{
IOS;
int t = 1;
//cin >> t;
while (t--)
solve();
return 0;
}