#include <bits/stdc++.h>
using namespace std;
const int N = 500 + 10;
int n, m, a[N], b[N], dp[N][N];
int calc(int at, int last) {
  if (at == n) return 0;
  int &ret = dp[at][last];
  if (ret != -1) return ret;
  ret = calc(at + 1, last);
  if (last == 502) {
    for (int i = 0; i < m; ++i)
      if (b[i] == a[at]) ret = max(ret, 1 + calc(at + 1, i));
  } else if (a[at] > b[last]) {
    for (int i = last + 1; i < m; ++i)
      if (b[i] == a[at]) ret = max(ret, 1 + calc(at + 1, i));
  }
  return ret;
}
void path(int at, int last) {
  if (at == n) return;
  for (int r = at; r < n; ++r) {
    int op = calc(r, last);
    if (last == 502) {
      for (int i = 0; i < m; ++i)
        if (b[i] == a[r] && 1 + calc(r + 1, i) == op) {
          printf("%d ", b[i]);
          path(r + 1, i);
          return;
        }
    } else if (a[r] > b[last]) {
      for (int i = last + 1; i < m; ++i)
        if (b[i] == a[r] && 1 + calc(r + 1, i) == op) {
          printf("%d ", b[i]);
          path(r + 1, i);
          return;
        }
    }
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) scanf("%d", a + i);
  scanf("%d", &m);
  for (int i = 0; i < m; ++i) scanf("%d", b + i);
  memset(dp, -1, sizeof dp);
  printf("%d\n", calc(0, 502));
  path(0, 502);
  return 0;
}
