#include <bits/stdc++.h>
using namespace std;
const int MAX = 523;
int a[MAX], b[MAX];
int dp[MAX][MAX];
int p[MAX][MAX];
int mx[MAX][MAX];
int main() {
  int n, m;
  cin >> n;
  for (int _n(n), i(0); i < _n; i++) cin >> a[i];
  cin >> m;
  for (int _n(m), i(0); i < _n; i++) cin >> b[i];
  memset((dp), (0), sizeof(dp)), memset((p), (-1), sizeof(p)),
      memset((mx), (-1), sizeof(mx));
  for (int _n(m), j(0); j < _n; j++) {
    mx[0][j] = dp[0][j] = (a[0] == b[j] ? 1 : 0);
    if (j) {
      mx[0][j] = max(mx[0][j], mx[0][j - 1]);
    }
  }
  for (int _n(n), i(1); i < _n; i++) {
    mx[i][0] = dp[i][0] = (a[i] == b[0] ? 1 : 0);
    for (int _n(m), j(1); j < _n; j++)
      if (a[i] == b[j]) {
        dp[i][j] = 1;
        for (int _n(i), k(0); k < _n; k++)
          if (a[k] < a[i] && dp[i][j] < mx[k][j - 1] + 1) {
            dp[i][j] = mx[k][j - 1] + 1;
            p[i][j] = k;
          }
      }
    for (int _n(m), j(1); j < _n; j++) mx[i][j] = max(mx[i][j - 1], dp[i][j]);
  }
  int ii = -1, jj = -1, ans = -1;
  for (int _n(n), i(0); i < _n; i++)
    for (int _n(m), j(0); j < _n; j++)
      if (ans < dp[i][j]) ii = i, jj = j, ans = dp[i][j];
  cout << ans << endl;
  vector<int> s(ans);
  for (int i = ans - 1; i >= 0; --i) {
    s[i] = a[ii];
    ii = p[ii][jj];
    if (i == 0) break;
    for (int _n(jj), j(0); j < _n; j++)
      if (dp[ii][j] == i) jj = j;
  }
  for (int _n(s.size()), i(0); i < _n; i++) {
    if (i) cout << ' ';
    cout << s[i];
    if (i + 1 == ans) cout << endl;
  }
  return 0;
}
