#include <bits/stdc++.h>
using namespace std;
int a[501], b[501], dp[501][501], pr[501];
void print(int X) {
  if (!X) return;
  print(pr[X]);
  cout << a[X] << ' ';
}
int main() {
  int n, ans = 0;
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> a[i];
  int m;
  cin >> m;
  for (int i = 1; i <= m; i++) cin >> b[i];
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      dp[i][j] = dp[i][j - 1];
      if (a[i] == b[j]) {
        dp[i][j] = 1;
        for (int k = i - 1; k >= 1; k -= 1)
          if (a[i] > a[k] && dp[k][j - 1] + 1 > dp[i][j])
            dp[i][j] = dp[k][j - 1] + 1, pr[i] = k;
      }
      if (dp[i][j] > dp[ans][m]) ans = i;
    }
  cout << dp[ans][m] << endl;
  print(ans);
  return 0;
}
