#include <bits/stdc++.h>
using namespace std;
const long long N = 500 + 10;
long long a[N], b[N], n, m, dp[N][N];
pair<long long, long long> par[N][N];
vector<long long> vec;
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  a[0] = -1;
  for (long long i = 1; i <= n; i++) cin >> a[i];
  cin >> m;
  for (long long i = 1; i <= m; i++) cin >> b[i];
  for (long long i = 1; i <= n; i++) {
    for (long long j = 1; j <= m; j++) {
      long long ind = j;
      while (ind != 0 && a[i] != b[ind]) ind--;
      if (!ind) continue;
      for (long long k = 0; k < i; k++) {
        if (a[k] < a[i]) {
          if (dp[i][j] < dp[k][ind - 1] + 1) {
            dp[i][j] = dp[k][ind - 1] + 1;
            par[i][j] = {k, ind - 1};
          }
        }
      }
    }
  }
  long long ind = 0;
  long long ans = 0;
  for (long long i = 1; i <= n; i++) {
    if (dp[i][m] > ans) {
      ans = dp[i][m];
      ind = i;
    }
  }
  long long ind2 = m;
  cout << ans << endl;
  for (long long i = 1; i <= ans; i++) {
    vec.push_back(a[ind]);
    pair<long long, long long> P = par[ind][ind2];
    ind = P.first;
    ind2 = P.second;
  }
  reverse(vec.begin(), vec.end());
  for (auto x : vec) cout << x << " ";
  return 0;
}
