#include <bits/stdc++.h>
using namespace std;
const long long int logx = 30;
const long long int N = 5e2 + 5;
const long long int M = 3e5 + 5;
const long long int mod = 998244353;
const long double INF = 1e18 + 5;
const double PI = 3.14159265;
inline long long int mul(long long int a, long long int b) {
  return (a * 1ll * b) % mod;
}
inline long long int sub(long long int a, long long int b) {
  long long int c = a - b;
  if (c < 0) c += mod;
  return c;
}
inline long long int add(long long int a, long long int b) {
  long long int c = a + b;
  if (c > mod) c -= mod;
  return c;
}
template <typename T>
T power(T x, T y, long long int m) {
  T ans = 1;
  while (y > 0) {
    if (y & 1LL) ans = (ans * x) % m;
    y >>= 1ll;
    x = (x * x) % m;
  }
  return ans % m;
}
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
  cerr << name << " : " << arg1 << '\n';
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
  const char* comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
map<long long int, long long int> val;
long long int n, m, a[N], b[N], dp[N][N];
map<long long int, vector<long long int> > pos;
void Compress() {
  long long int cnt = 1;
  set<long long int> ser;
  for (long long int i = 0; i < n; i++) {
    ser.insert(a[i]);
  }
  for (long long int i = 0; i < m; i++) {
    ser.insert(b[i]);
  }
  for (auto i : ser) {
    val[i] = cnt++;
  }
}
long long int F(long long int i, long long int j) {
  long long int res = 0;
  if (i == n - 1 || j == m - 1) {
    return 0;
  }
  if (dp[i][j] != -1) {
    return dp[i][j];
  }
  if (a[i] == b[j]) {
    for (long long int h = i + 1; h < n; h++) {
      if (a[i] >= a[h]) {
        continue;
      }
      long long int ub =
          upper_bound(pos[val[a[h]]].begin(), pos[val[a[h]]].end(), j) -
          pos[val[a[h]]].begin();
      if (ub == pos[val[a[h]]].size()) {
        continue;
      }
      res = max(res, 1 + F(h, pos[val[a[h]]][ub]));
    }
  }
  return dp[i][j] = res;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ;
  cin >> n;
  long long int res = 0;
  memset(dp, -1, sizeof(dp));
  for (long long int i = 0; i < n; i++) {
    cin >> a[i];
  }
  cin >> m;
  for (long long int i = 0; i < m; i++) {
    cin >> b[i];
  }
  Compress();
  for (long long int i = 0; i < m; i++) {
    pos[val[b[i]]].push_back(i);
  }
  for (long long int i = 0; i < n; i++) {
    for (long long int j = 0; j < m; j++) {
      long long int cur = (a[i] == b[j]) + F(i, j);
      res = max(res, cur);
    }
  }
  vector<long long int> vec;
  long long int cnt = 0, lasi = -1, lasj = -1, oldans = 0, lasval = -INF;
  while (cnt < res) {
    for (long long int i = lasi + 1; i < n; i++) {
      long long int first = 0;
      for (long long int j = lasj + 1; j < m; j++) {
        if (a[i] != b[j]) {
          continue;
        }
        long long int tempans = oldans + 1 + F(i, j);
        if (tempans != res) {
          continue;
        }
        if (lasval >= a[i]) {
          continue;
        }
        vec.push_back(a[i]);
        first = 1;
        lasval = a[i];
        cnt++;
        oldans++;
        lasi = i;
        lasj = j;
        break;
      }
      if (first) {
        break;
      }
    }
  }
  cout << res << endl;
  for (auto i : vec) {
    cout << i << " ";
  }
}
