#include <bits/stdc++.h>
using namespace std;
int n, m, a[11000], b[11000], dp[5100], g[5100];
void dfs(int p) {
  if (!p) return;
  dfs(g[p]);
  cout << b[p] << " ";
}
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, t = 0; i <= n; i++, t = 0)
    for (int j = 1; j <= m; j++) {
      if (a[i] == b[j]) {
        dp[j] = dp[t] + 1;
        g[j] = t;
      }
      if (b[j] < a[i] && dp[j] > dp[t]) t = j;
    }
  int p = 0;
  for (int i = 1; i <= m; i++)
    if (dp[i] > dp[p]) p = i;
  cout << dp[p] << endl;
  dfs(p);
  return 0;
}
