#include <bits/stdc++.h>
using namespace std;
long long INF = 1LL << 60;
long long MOD = 998244353;
template <typename A, typename B>
istream &operator>>(istream &is, pair<A, B> &pr) {
  is >> pr.first >> pr.second;
  return is;
}
template <typename A>
istream &operator>>(istream &is, vector<A> &v) {
  for (auto &x : v) is >> x;
  return is;
}
ostream &operator<<(ostream &os, char &c) {
  os << (char)c;
  return os;
}
ostream &operator<<(ostream &os, const char s[]) {
  os << (string)s;
  return os;
}
ostream &operator<<(ostream &os, string &s) {
  for (char &c : s) os << (char)c;
  return os;
}
template <typename A, typename B>
ostream &operator<<(ostream &os, pair<A, B> &pr) {
  os << '(' << pr.first << ", " << pr.second << ')';
  return os;
}
ostream &operator<<(ostream &os, vector<bool> &v) {
  bool first = true;
  os << '[';
  for (const bool &x : v) {
    if (!first) os << ", ";
    first = false;
    os << x;
  }
  os << ']';
  return os;
}
template <class C>
ostream &operator<<(ostream &os, C &v) {
  bool first = true;
  os << '[';
  for (auto &x : v) {
    if (!first) os << ", ";
    first = false;
    os << x;
  }
  os << ']';
  return os;
}
string bin_string(int x) {
  const int d = 32;
  string res(d, ' ');
  for (int i = (0); i < (d); ++i) res[d - 1 - i] = '0' + (x >> i & 1LL);
  return res;
}
int powa(int base, int exp) {
  int res = 1;
  while (exp) {
    if (exp & 1) res = res * base % MOD;
    base = base * base % MOD;
    exp >>= 1;
  }
  return res;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0);
  int n;
  cin >> n;
  vector<int> a(n);
  cin >> a;
  int m;
  cin >> m;
  vector<int> b(m);
  cin >> b;
  vector<int> raw;
  set<int> temp;
  for (int i : a) temp.insert(i);
  for (int i : b) temp.insert(i);
  for (int i : temp) raw.push_back(i);
  int r = raw.size();
  map<int, int> trans;
  for (int i = (0); i < (r); ++i) trans[raw[i]] = i;
  for (int &i : a) i = trans[i];
  for (int &i : b) i = trans[i];
  vector<vector<int> > dp(n, vector<int>(r + 1));
  vector<vector<vector<bool> > > par(
      n, vector<vector<bool> >(m, vector<bool>(r + 1)));
  for (int j = (0); j < (m); ++j) {
    vector<vector<int> > dp2(n, vector<int>(r + 1));
    vector<int> occ(r, -1);
    for (int i = (0); i < (n); ++i) {
      occ[a[i]] = i;
      for (int k = (0); k < (r + 1); ++k) {
        dp2[i][k] = j == 0 ? 0 : dp[i][k];
        if (b[j] < k && occ[b[j]] != -1) {
          int alt = 1 + (j == 0 ? 0 : dp[occ[b[j]]][b[j]]);
          if (alt > dp2[i][k]) {
            par[i][j][k] = true;
            dp2[i][k] = alt;
          }
        }
      }
    }
    dp = dp2;
  }
  cout << dp[n - 1][r] << '\n';
  int aa = n - 1, bb = m - 1, cc = r;
  vector<int> res;
  while (aa >= 0 && bb >= 0 && cc > 0) {
    if (par[aa][bb][cc]) {
      res.push_back(bb);
      for (int i = (aa); i > (-1); --i)
        if (a[i] == b[bb]) {
          aa = i - 1;
          break;
        }
      cc = b[bb];
    }
    --bb;
  }
  reverse(res.begin(), res.end());
  for (int i : res) cout << raw[b[i]] << ' ';
}
