#include <bits/stdc++.h>
using namespace std;
struct ind {
  int i, j;
  friend bool operator==(const ind &a, const ind &b) {
    return (a.i == b.i && a.j == b.j);
  }
  friend bool operator!=(const ind &a, const ind &b) { return !(a == b); }
};
const int MAX_LEN = 503, MAX_NUM = 1000000009;
int A[MAX_LEN], B[MAX_LEN], Ans[MAX_LEN];
int D[MAX_LEN][MAX_LEN];
ind P[MAX_LEN][MAX_LEN];
int main() {
  ios_base::sync_with_stdio(false);
  int n, m;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> A[i];
  }
  cin >> m;
  for (int i = 1; i <= m; i++) {
    cin >> B[i];
  }
  int i, j, k, l, maxi, last;
  ind r, minus_one = {-1, -1}, zero = {0, 0};
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      D[i][j] = max(D[i][j - 1], D[i - 1][j]);
      P[i][j] = minus_one;
      if (A[i] == B[j]) {
        maxi = 0;
        for (k = i - 1; k > 0; k--)
          for (l = j - 1; l > 0 && D[k][l] > maxi; l--)
            if (A[k] == B[l] && A[k] < A[i] && P[k][l] != minus_one &&
                maxi < D[k][l]) {
              maxi = D[k][l];
              r = {k, l};
            }
        if (maxi + 1 >= D[i][j]) {
          D[i][j] = maxi + 1;
          P[i][j] = ((maxi != 0) ? r : zero);
        }
      }
    }
  }
  cout << D[n][m] << endl;
  i = n, j = m, k = 0, last = MAX_NUM;
  while (i > 0 && j > 0) {
    if (A[i] == B[j] && A[i] < last && P[i][j] != minus_one) {
      Ans[k++] = last = A[i];
      r = P[i][j];
      i = r.i, j = r.j;
    } else {
      (D[i][j - 1] >= D[i - 1][j]) ? j-- : i--;
    }
  }
  for (i = k - 1; i >= 0; i--) cout << Ans[i] << " ";
  cout << endl;
  return 0;
}
