#include <bits/stdc++.h>
#pragma comment(linker, "/stack:256000000")
using namespace std;
const int N = 510;
const int INF = (int)1e9;
int dp[N][N];
int pr[N][N];
int a[N], b[N], A[N], B[N], pos[2 * N];
int main() {
  int n;
  scanf("%d", &n);
  for (int(i) = 0; (i) < (n); (i)++) scanf("%d", &a[i + 1]);
  int m;
  scanf("%d", &m);
  for (int(i) = 0; (i) < (m); (i)++) scanf("%d", &b[i + 1]);
  a[0] = b[0] = -INF;
  for (int(i) = 0; (i) < (N); (i)++)
    for (int(j) = 0; (j) < (N); (j)++) dp[i][j] = INF;
  dp[0][0] = 0;
  vector<int> val;
  for (int(i) = 0; (i) < (n); (i)++) val.push_back(a[i + 1]);
  for (int(i) = 0; (i) < (m); (i)++) val.push_back(b[i + 1]);
  sort((val).begin(), (val).end());
  val.resize(unique((val).begin(), (val).end()) - val.begin());
  for (int(i) = 0; (i) < (n); (i)++)
    A[i + 1] = lower_bound((val).begin(), (val).end(), a[i + 1]) - val.begin();
  for (int(i) = 0; (i) < (m); (i)++)
    B[i + 1] = lower_bound((val).begin(), (val).end(), b[i + 1]) - val.begin();
  A[0] = B[0] = -1;
  for (int(i) = 0; (i) < (min(n, m)); (i)++) {
    for (int(j) = 0; (j) < (n + 1); (j)++) {
      if (dp[i][j] == INF) continue;
      memset(pos, -1, sizeof(pos));
      for (int k = m; k > dp[i][j]; k--) {
        pos[B[k]] = k;
      }
      for (int k = j + 1; k <= n; k++) {
        if (A[k] > A[j] && pos[A[k]] != -1) {
          if (pos[A[k]] < dp[i + 1][k]) {
            dp[i + 1][k] = pos[A[k]];
            pr[i + 1][k] = j;
          }
        }
      }
    }
  }
  int x = -1, y = -1;
  for (int(i) = 0; (i) < (min(n, m) + 1); (i)++) {
    for (int(j) = 0; (j) < (n + 1); (j)++) {
      if (dp[i][j] < INF) {
        x = i;
        y = j;
      }
    }
  }
  vector<int> res;
  while (x > 0 && y > 0) {
    res.push_back(a[y]);
    y = pr[x][y];
    --x;
  }
  reverse((res).begin(), (res).end());
  printf("%d\n", (int)(res).size());
  for (int(i) = 0; (i) < ((int)(res).size()); (i)++) printf("%d ", res[i]);
  return 0;
}
