#include <bits/stdc++.h>
using namespace std;
const int N = 505;
int memo[N][N];
pair<int, int> Prev[N][N];
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  int m;
  cin >> m;
  vector<int> b(m);
  unordered_map<int, vector<int>> toPosB;
  for (int i = 0; i < m; ++i) {
    cin >> b[i];
    toPosB[b[i]].push_back(i + 1);
  }
  unordered_map<int, int> pos;
  int best = 0;
  pair<int, int> bestState = {-1, -1};
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      if (a[i - 1] != b[j - 1]) {
        assert(memo[i][j] == 0);
        continue;
      }
      int &res = memo[i][j];
      res = 1;
      if (best == 0) {
        best = 1;
        bestState = {i, j};
      }
      for (int k = 0; k < m; ++k) {
        pos[b[k]] = 0;
      }
      for (int i2 = 1; i2 < i; ++i2) {
        if (a[i2 - 1] >= a[i - 1]) continue;
        if (!pos.count(a[i2 - 1])) continue;
        int &curr = pos[a[i2 - 1]];
        assert(toPosB.count(a[i2 - 1]));
        vector<int> &currIndexes = toPosB[a[i2 - 1]];
        while (curr < (int)currIndexes.size() && currIndexes[curr] < j) {
          ++curr;
        }
        if (curr == 0) continue;
        --curr;
        if (res < 1 + memo[i2][currIndexes[curr]]) {
          Prev[i][j] = {i2, currIndexes[curr]};
          res = 1 + memo[i2][currIndexes[curr]];
        }
      }
      if (best < res) {
        best = res;
        bestState = {i, j};
      }
    }
  }
  if (best == 0) {
    cout << 0;
    return 0;
  }
  assert(bestState != make_pair(-1, -1));
  vector<int> indexes;
  while (best--) {
    pair<int, int> to = Prev[bestState.first][bestState.second];
    indexes.push_back(bestState.first - 1);
    bestState = to;
  }
  cout << indexes.size() << '\n';
  reverse(indexes.begin(), indexes.end());
  for (int i : indexes) {
    cout << a[i] << ' ';
  }
  cout << '\n';
}
