#include <bits/stdc++.h>
using namespace std;
const int mod = 1000 * 1000 * 1000 + 7;
const int INF = 1000 * 1000 * 1000;
const long long LINF = (long long)INF * INF;
int dp[2][520][2 * 520];
int par[520][520];
int n, m;
int a[520], b[520];
vector<int> cor;
int32_t main(void) {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ;
  cin >> n;
  for (int i = (0); i < (n); i++) {
    cin >> a[i];
    cor.push_back(a[i]);
  }
  cin >> m;
  for (int i = (0); i < (m); i++) {
    cin >> b[i];
    cor.push_back(b[i]);
  }
  sort((cor).begin(), (cor).end());
  cor.resize(unique((cor).begin(), (cor).end()) - cor.begin());
  for (int i = (0); i < (n); i++) {
    a[i] = lower_bound((cor).begin(), (cor).end(), a[i]) - cor.begin();
  }
  for (int i = (0); i < (m); i++) {
    b[i] = lower_bound((cor).begin(), (cor).end(), b[i]) - cor.begin();
  }
  for (int i = (1); i < (n + 1); i++) {
    for (int j = (1); j < (m + 1); j++) {
      for (int k = (0); k < (cor.size()); k++) {
        int cur = i % 2;
        int prev = (2 + (i - 1) % 2) % 2;
        if (a[i - 1] != k && b[j - 1] != k) {
          dp[cur][j][k] = dp[prev][j - 1][k];
        } else if (a[i - 1] == k && b[j - 1] == k) {
          dp[cur][j][k] = 1;
          for (int l = (0); l < (k); l++) {
            if (dp[prev][j - 1][l] + 1 > dp[cur][j][k]) {
              par[i][j] = l;
            }
            dp[cur][j][k] = max(dp[cur][j][k], dp[prev][j - 1][l] + 1);
          }
        } else {
          dp[cur][j][k] = (a[i - 1] == k ? dp[cur][j - 1][k] : dp[prev][j][k]);
        }
      };
      ;
      ;
      ;
      ;
      ;
    }
  }
  pair<int, int> ans = make_pair(-1, -1);
  for (int i = (0); i < (cor.size()); i++) {
    ans = max(ans, make_pair(dp[n % 2][m][i], i));
  }
  cout << ans.first << '\n';
  if (ans.first == 0) return 0;
  int x = ans.second;
  vector<int> lcis;
  int aptr = n, bptr = m;
  do {
    for (aptr--; aptr >= 0; aptr--) {
      if (a[aptr] == x) break;
    }
    for (bptr--; bptr >= 0; bptr--) {
      if (b[bptr] == x) break;
    }
    lcis.push_back(x);
    x = par[aptr + 1][bptr + 1];
  } while (lcis.size() < ans.first);
  reverse((lcis).begin(), (lcis).end());
  for (int u : lcis) {
    cout << cor[u] << ' ';
  }
  cout << '\n';
  return 0;
}
