#include <bits/stdc++.h>
using namespace std;
long long power(long long a, long long b) {
  long long res = 1;
  a = a % 1000000007;
  while (b > 0) {
    if (b & 1) {
      res = (res * a) % 1000000007;
      b--;
    }
    a = (a * a) % 1000000007;
    b >>= 1;
  }
  return res;
}
long long fermat_inv(long long y) { return power(y, 1000000007 - 2); }
long long gcd(long long a, long long b) { return (b == 0) ? a : gcd(b, a % b); }
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long t = 1;
  while (t--) {
    int INF = 1e9 + 1;
    int n;
    cin >> n;
    int a[n + 2];
    a[0] = -1;
    a[n + 1] = 1e9 + 1;
    for (int i = 1; i <= n; i++) cin >> a[i];
    int m;
    cin >> m;
    int b[m + 1];
    for (int i = 1; i <= m; i++) cin >> b[i];
    short dp[n + 1][m + 1][n + 1];
    for (int i = 0; i <= n; i++) {
      for (int j = 0; j <= m; j++) {
        for (int k = 0; k <= n; k++) {
          dp[i][j][k] = n + 1;
          if (((i == 0) || (j == 0)) && k == 0) dp[i][j][k] = 0;
        }
      }
    }
    int v1 = 0;
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= m; j++) {
        if (a[i] == b[j]) {
          dp[i][j][0] = 0;
          for (int k = 1; k <= n; k++) {
            if (a[dp[i][j][k - 1]] < a[i] && a[i] < a[dp[i - 1][j - 1][k]]) {
              dp[i][j][k] = i;
              v1 = max(v1, k);
            } else if (a[dp[i][j - 1][k]] < a[dp[i - 1][j][k]])
              dp[i][j][k] = dp[i][j - 1][k];
            else
              dp[i][j][k] = dp[i - 1][j][k];
          }
        } else {
          for (int k = 0; k <= n; k++) {
            if (a[dp[i][j - 1][k]] < a[dp[i - 1][j][k]])
              dp[i][j][k] = dp[i][j - 1][k];
            else
              dp[i][j][k] = dp[i - 1][j][k];
          }
        }
      }
    }
    cout << v1 << "\n";
    int c1 = n;
    int c2 = m;
    int ls = 1e9 + 1;
    vector<int> v;
    while (v1 >= 0) {
      int check = 0;
      for (int i = c1; i >= 1 && check == 0; i--) {
        for (int j = c2; j >= 1 && check == 0; j--) {
          if (a[i] == b[j] && dp[i][j][v1] == i && a[i] < ls) {
            v.push_back(a[i]);
            ls = a[i];
            check = 1;
            c1 = i - 1;
            c2 = j - 1;
          }
        }
      }
      v1--;
    }
    for (int i = v.size() - 1; i >= 0; i--) cout << v[i] << " ";
  }
  return 0;
}
