#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  int n, m;
  cin >> n;
  vector<int> vec1(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> vec1[i];
  }
  cin >> m;
  vector<int> vec2(m + 1);
  for (int i = 1; i <= m; i++) {
    cin >> vec2[i];
  }
  vector<vector<int> > dp(n + 1, vector<int>(m + 1, 0));
  vector<vector<int> > pr(n + 1, vector<int>(m + 1, 0));
  for (int i = 1; i <= n; i++) {
    int tmp = 0;
    for (int j = 1; j <= m; j++) {
      dp[i][j] = dp[i - 1][j];
      pr[i][j] = pr[i - 1][j];
      if (dp[i - 1][tmp] + 1 > dp[i][j] && vec1[i] == vec2[j]) {
        dp[i][j] = dp[i - 1][tmp] + 1;
        pr[i][j] = tmp;
      }
      if (dp[i - 1][j] > dp[i - 1][tmp] && vec1[i] > vec2[j]) {
        tmp = j;
      }
    }
  }
  int ans = 0, tmp = -1;
  for (int i = 1; i <= m; i++) {
    if (dp[n][i] > ans) {
      ans = dp[n][i];
      tmp = i;
    }
  }
  cout << ans << endl;
  if (ans == 0) {
    return 0;
  }
  vector<int> answer;
  while (tmp != 0) {
    answer.push_back(vec2[tmp]);
    tmp = pr[n][tmp];
  }
  for (int i = answer.size() - 1; i >= 0; i--) {
    cout << answer[i] << ' ';
  }
  return 0;
}
