#include <bits/stdc++.h>
const int S = 505;
int n, a[S], f[S][S], m, b[S], px[S][S], py[S][S];
inline int ma(int a, int b) { return a > b ? a : b; }
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]) {
        f[i][j] = 1;
        px[i][j] = 0;
        py[i][j] = 0;
        for (int k = 1; k < i; k++)
          if (a[k] < a[i] && f[k][j - 1] + 1 > f[i][j]) {
            f[i][j] = f[k][j - 1] + 1;
            px[i][j] = k;
            py[i][j] = j - 1;
          }
      } else {
        f[i][j] = f[i][j - 1];
        px[i][j] = i;
        py[i][j] = j - 1;
      }
  int xx = 0, t, yy = m, ans[S] = {0}, tp = 0;
  for (int i = 1; i <= n; i++)
    if (f[i][m] > f[xx][m]) xx = i;
  printf("%d\n", f[xx][m]);
  while (xx && yy) {
    if (a[xx] == b[yy]) ans[++tp] = xx;
    t = px[xx][yy];
    yy = py[xx][yy];
    xx = t;
  }
  for (int i = tp; i; i--) printf("%d ", a[ans[i]]);
  return 0;
}
