#include <bits/stdc++.h>
using namespace std;
const long long mod = (long long)1e9 + 9;
const int inf = (int)2e9;
const long long INF = (long long)1e18;
const int base = 1000 * 1000 * 1000;
const int maxn = 105;
const long double pi = acosl(-1.0);
const long double eps = 1e-9;
void solve() {
  int n, m;
  cin >> n;
  vector<int> a(n + 1, 0);
  for (int i = 0; i < n; i++) cin >> a[i + 1];
  cin >> m;
  vector<int> b(m + 1, 0);
  for (int i = 0; i < m; i++) cin >> b[i + 1];
  vector<vector<int>> dp(n + 1, vector<int>(m + 1, 0));
  vector<vector<pair<int, int>>> pred(
      n + 1, vector<pair<int, int>>(m + 1, make_pair(-1, -1)));
  for (int i = 1; i < n + 1; i++)
    for (int j = 1; j < m + 1; j++) {
      if (a[i] != b[j]) {
        dp[i][j] = dp[i][j - 1];
        pred[i][j] = make_pair(i, j - 1);
      } else {
        dp[i][j] = 1;
        pred[i][j] = make_pair(0, j);
        for (int k = 1; k < i; k++)
          if (a[k] < b[j] && dp[k][j] >= dp[i][j]) {
            dp[i][j] = dp[k][j] + 1;
            pred[i][j] = make_pair(k, j);
          }
      }
    }
  int cnt = 0;
  pair<int, int> p = make_pair(-1, -1);
  for (int i = 0; i < n + 1; i++)
    if (cnt < dp[i][m]) {
      p = make_pair(i, m);
      cnt = dp[i][m];
    }
  vector<int> ans;
  while (p != make_pair(-1, -1) &&
         pred[p.first][p.second] != make_pair(-1, -1)) {
    if ((int)ans.size() == 0 || ans.back() != a[p.first])
      ans.push_back(a[p.first]);
    p = pred[p.first][p.second];
  }
  reverse(ans.begin(), ans.end());
  cout << (int)ans.size() << endl;
  for (int i = 0; i < (int)ans.size(); i++) cout << ans[i] << ' ';
}
int main() {
  solve();
  return 0;
}
