#include <bits/stdc++.h>
using namespace std;
const int INF = (int)1e9;
int main() {
  int n = 0, m = 0;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; ++i) cin >> a[i];
  cin >> m;
  vector<int> b(m);
  for (int i = 0; i < m; ++i) cin >> b[i];
  vector<vector<int>> dp(n + 1, vector<int>(m + 1));
  vector<int> p(n + 1, -1);
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      if (a[i - 1] == b[j - 1]) {
        dp[i][j] = 1;
        for (int k = 1; k < i; ++k) {
          if (a[k - 1] < a[i - 1] && dp[i][j] < dp[k][j] + 1) {
            dp[i][j] = dp[k][j] + 1;
            p[i] = k;
          }
        }
      } else
        dp[i][j] = dp[i][j - 1];
    }
  }
  vector<int> ans;
  int maxPos = 0;
  for (int i = 1; i <= n; ++i)
    if (dp[i][m] > dp[maxPos][m]) maxPos = i;
  int mm = m;
  while (maxPos > 0 && mm) {
    if (a[maxPos - 1] == b[mm - 1]) {
      ans.push_back(a[maxPos - 1]);
      maxPos = p[maxPos];
    } else
      mm--;
  }
  reverse(ans.begin(), ans.end());
  cout << ans.size() << endl;
  for (auto x : ans) cout << x << " ";
  return 0;
}
