#include <bits/stdc++.h>
using namespace std;
long a[600] = {0}, b[600] = {0}, f[600][600] = {0}, p[600][600][2] = {0}, n, m,
     g[600] = {0};
int main(int argc, char** argv) {
  cin >> n;
  for (long i = 1; i <= n; i++) cin >> a[i];
  cin >> m;
  for (long i = 1; i <= m; i++) cin >> b[i];
  for (long i = n; i >= 1; i--) {
    long mm = 0, ind = m + 1;
    for (long j = m; j >= 1; j--) {
      if (a[i] == b[j] && f[i][j] < mm + 1)
        f[i][j] = mm + 1, p[i][j][0] = g[ind], p[i][j][1] = ind, g[j] = i;
      if (b[j] > a[i] && mm < f[g[j]][j]) ind = j, mm = f[g[j]][j];
    }
  }
  long ans = 0, ind_i, ind_j;
  for (long i = 1; i <= n; i++)
    for (long j = 1; j <= m; j++)
      if (f[i][j] > ans) ans = f[i][j], ind_i = i, ind_j = j;
  cout << ans << endl;
  if (ans != 0) {
    while (ind_i <= n && ind_j <= m) {
      cout << a[ind_i] << ' ';
      long t_i = ind_i, t_j = ind_j;
      ind_i = p[t_i][t_j][0], ind_j = p[t_i][t_j][1];
    }
    cout << endl;
  }
  return 0;
}
