#include <bits/stdc++.h>
using namespace std;
inline int read() {
  register int x = 0, f = 1;
  register char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = (x << 1) + (x << 3) + (c ^ 48);
    c = getchar();
  }
  return x * f;
}
const int Inf = 0x3f3f3f3f;
const int N = 505;
int n, m, ans, last, a[N], b[N];
int dp[N][N], loc[N][N];
inline void output(int now) {
  if (now == 0) return;
  output(loc[n][now]);
  printf("%d ", b[now]);
}
int main() {
  n = read();
  for (register int i(1); i <= n; ++i) a[i] = read();
  m = read();
  for (register int i(1); i <= m; ++i) b[i] = read();
  for (register int i(1); i <= n; ++i) {
    last = 0;
    for (register int j(1); j <= m; ++j) {
      dp[i][j] = dp[i - 1][j];
      loc[i][j] = loc[i - 1][j];
      if (a[i] == b[j] && dp[i - 1][last] + 1 > dp[i][j]) {
        dp[i][j] = dp[i - 1][last] + 1;
        loc[i][j] = last;
      }
      if (b[j] < a[i] && dp[i - 1][j] > dp[i - 1][last]) last = j;
    }
  }
  for (register int i(1); i <= m; ++i)
    if (dp[n][i] > dp[n][ans]) ans = i;
  printf("%d\n", dp[n][ans]);
  output(ans);
  return 0;
}
