#include <bits/stdc++.h>
using namespace std;
const long long mx = 500 + 10;
const long long inf = (long long)1e10;
long long dp[mx][mx] = {0};
pair<long long, long long> prv[mx][mx];
long long a[mx];
long long b[mx];
long long n, m;
signed main() {
  for (long long i = 0; i < mx; ++i) {
    for (long long j = 0; j < mx; ++j) {
      prv[i][j] = {-1, -1};
    }
  }
  cin >> n;
  for (long long i = 1; i <= n; ++i) cin >> a[i];
  cin >> m;
  for (long long i = 1; i <= m; ++i) cin >> b[i];
  long long mxi = -1;
  long long mxj = -1;
  long long mxa = 0;
  for (long long i = 1; i <= n; ++i) {
    for (long long j = 1; j <= m; ++j) {
      if (a[i] == b[j]) dp[i][j] = 1;
    }
  }
  for (long long i = 1; i <= n; ++i) {
    for (long long j = 1; j <= m; ++j) {
      if (dp[i][j - 1] > dp[i][j]) {
        dp[i][j] = dp[i][j - 1];
        prv[i][j] = prv[i][j - 1];
      }
      if (a[i] == b[j]) {
        for (long long k = 1; k < i; ++k) {
          if ((a[k] < a[i]) && (dp[k][j - 1] + 1 > dp[i][j])) {
            dp[i][j] = dp[k][j - 1] + 1;
            prv[i][j] = {k, j - 1};
          }
        }
      }
      if (dp[i][j] > mxa) {
        mxa = dp[i][j];
        mxi = i;
        mxj = j;
      }
    }
  }
  vector<long long> ans;
  while (mxi != -1) {
    ans.push_back(mxi);
    long long tmpi = prv[mxi][mxj].first;
    long long tmpj = prv[mxi][mxj].second;
    mxi = tmpi;
    mxj = tmpj;
  }
  reverse(ans.begin(), ans.end());
  cout << ans.size() << "\n";
  for (long long i = 0; i < ans.size(); ++i) {
    cout << a[ans[i]] << " ";
  }
}
