#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:256000000")
using namespace std;
const long long MOD = 998244353;
const int dx[] = {0, 1, 0, 1};
const int dy[] = {0, 0, 1, 1};
const int MAXN = (int)3e6 + 500;
const double EPS = 1e-10;
const double PI = 3.14159265358979323846;
const int SZ = 550;
const long long INF = 5e10 + 3;
int n, m;
int a[1000], b[1000], dp[1000][1000];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  string s;
  for (int i = 0; i < n; i++) cin >> a[i];
  cin >> m;
  for (int i = 0; i < m; i++) cin >> b[i];
  int pr[1000];
  pr[0] = -1;
  int answ = 0, posw = -1;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (a[i] == b[j]) {
        dp[i][j] = 1;
        pr[i] = -1;
        for (int k = 0; k < i; k++) {
          if (a[k] < a[i] && dp[i][j] < dp[k][j] + 1) {
            dp[i][j] = dp[k][j] + 1;
            pr[i] = k;
          }
        }
      } else {
        dp[i][j] = dp[i][j - 1];
      }
      if (dp[i][j] > answ) {
        answ = dp[i][j];
        posw = i;
      }
    }
  }
  vector<int> ans(0);
  for (int pos = posw; pos != -1; pos = pr[pos]) ans.push_back(a[pos]);
  reverse((ans).begin(), (ans).end());
  cout << answ << "\n";
  for (int p : ans) cout << p << " ";
}
