#include <bits/stdc++.h>
using namespace std;
int n, m, f[5010][5010], fa[5010][5010], pre, maxn, ans, lst;
long long a[5010], b[5010];
void dfs(int k, int u) {
  if (!u) return;
  dfs(k - 1, fa[k][u]);
  printf("%lld ", b[u]);
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) scanf("%lld", a + i);
  scanf("%d", &m);
  for (int j = 1; j <= m; ++j) scanf("%lld", b + j);
  for (int i = 1; i <= n; ++i) {
    pre = maxn = 0;
    for (int j = 1; j <= m; ++j) {
      f[i][j] = f[i - 1][j];
      fa[i][j] = fa[i - 1][j];
      if (a[i] == b[j] && f[i][j] < maxn + 1) {
        f[i][j] = maxn + 1;
        fa[i][j] = pre;
      }
      if (a[i] > b[j] && maxn < f[i - 1][j]) {
        maxn = f[i - 1][j];
        pre = j;
      }
    }
  }
  for (int j = 1; j <= m; ++j)
    if (ans < f[n][j]) {
      ans = f[n][j];
      lst = j;
    }
  printf("%d\n", ans);
  dfs(n, lst);
}
