#include <bits/stdc++.h>
using namespace std;
int read() {
  char c;
  while (!isdigit(c = getchar()))
    ;
  int x = c - '0';
  while (isdigit(c = getchar())) x = x * 10 + c - '0';
  return x;
}
int a[501], b[501], f[501][501], p[501];
void print(int o) {
  if (!o) return;
  print(p[o]);
  printf("%d ", a[o]);
}
int main() {
  int n = read(), ans = 0;
  for (int i = 1; i <= n; i += 1) a[i] = read();
  int m = read();
  for (int i = 1; i <= m; i += 1) b[i] = read();
  for (int i = 1; i <= n; i += 1)
    for (int j = 1; j <= m; j += 1) {
      f[i][j] = f[i][j - 1];
      if (a[i] == b[j]) {
        f[i][j] = 1;
        for (int k = i - 1; k >= 1; k -= 1)
          if (a[i] > a[k] && f[k][j - 1] + 1 > f[i][j])
            f[i][j] = f[k][j - 1] + 1, p[i] = k;
      }
      if (f[i][j] > f[ans][m]) ans = i;
    }
  printf("%d\n", f[ans][m]);
  print(ans);
  return 0;
}
