#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n;
  cin >> n;
  vector<long long> v1(n + 1);
  for (long long i = 1; i <= n; ++i) {
    cin >> v1[i];
  }
  long long m;
  cin >> m;
  vector<long long> v2(m + 1);
  for (long long i = 1; i <= m; ++i) {
    cin >> v2[i];
  }
  vector<vector<long long>> dp(n + 1, vector<long long>(m + 1, 0));
  vector<long long> prev(n + 1, 0);
  for (long long i = 1; i <= n; ++i) {
    for (long long j = 1; j <= m; ++j) {
      if (v1[i] == v2[j]) {
        dp[i][j] = 1;
        for (int k = 1; k < i; ++k) {
          if (v1[k] < v1[i] && dp[i][j] < dp[k][j] + 1) {
            dp[i][j] = dp[k][j] + 1;
            prev[i] = k;
          }
        }
      } else {
        dp[i][j] = dp[i][j - 1];
      }
    }
  }
  long long pos = 1;
  bool fl = false;
  for (long long i = 1; i <= n; ++i) {
    if (dp[pos][m] < dp[i][m]) {
      pos = i;
    }
    if (dp[i][m] > 0) {
      fl = true;
    }
  }
  if (!fl) {
    cout << 0;
    return 0;
  }
  vector<long long> ans;
  while (pos != 0) {
    ans.push_back(v1[pos]);
    pos = prev[pos];
  }
  cout << (long long)ans.size() << "\n";
  for (long long i = (long long)ans.size() - 1; i >= 0; --i) {
    cout << ans[i] << " ";
  }
}
