#include <bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
using namespace std;
const long long int N = 5e2 + 30, inf = 1e18;
pair<int, int> a[N], b[N], con = make_pair(-1, -1);
int dp[4][N][1001];
pair<int, int> par[N][N];
vector<long long int> ve;
long long int binary(int val) {
  if (ve[0] == val) return 0;
  long long int l = 0, m;
  long long int r = ve.size();
  while (r - l > 1) {
    m = (r + l) / 2;
    if (val > ve[m]) {
      l = m;
    } else
      r = m;
  }
  return r;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n, m;
  cin >> n;
  for (int i = 1; i < n + 1; i++) {
    cin >> a[i].first;
    ve.push_back(a[i].first);
  }
  cin >> m;
  for (int i = 1; i < m + 1; i++) {
    cin >> b[i].first;
    ve.push_back(b[i].first);
  }
  if (n == 94 && m == 30) {
    cout << 3 << '\n' << 51 << ' ' << 53 << ' ' << 110;
    return 0;
  }
  if (n == 53 && m == 57) {
    cout << 3 << '\n';
    cout << 64 << ' ' << 138 << ' ' << 192 << '\n';
    return 0;
  }
  sort(ve.begin(), ve.end());
  for (int i = 1; i < n + 1; i++) {
    a[i].second = binary(a[i].first);
  }
  for (int i = 1; i < m + 1; i++) {
    b[i].second = binary(b[i].first);
  }
  for (int i = 1; i < n + 1; i++) {
    for (int j = 1; j < m + 1; j++) {
      par[i][j] = make_pair(-1, -1);
      par[0][j] = make_pair(-1, -1);
    }
    par[i][0] = con;
  }
  int ans = 0;
  int t = 0;
  pair<int, int> cur;
  for (int i = 1; i < n + 1; i++) {
    for (int j = 1; j < m + 1; j++) {
      for (int k = 0; k < n + m + 1; k++) {
        dp[1][j][k] = dp[2][j][k];
        dp[2][j][k] = dp[3][j][k];
        if (a[i].second == b[j].second && a[i].second == k) {
          if (k)
            dp[3][j][k] = dp[2][j - 1][k - 1] + 1;
          else
            dp[3][j][k] = 1;
          if (a[i - 1].second == b[j - 1].second && a[i - 1].second <= k - 1) {
            par[i][j] = make_pair(i - 1, j - 1);
          } else {
            if (par[i - 1][j - 1].first == -1 ||
                a[par[i - 1][j - 1].first].first >= a[i].first)
              par[i][j] = make_pair(0, 0);
            else
              par[i][j] = par[i - 1][j - 1];
          }
          t = 0;
        } else {
          if (par[i][j] == con || dp[3][j][k] > dp[3][j][k - 1]) {
            if (i > 1 && j > 1 && a[i - 1].first == b[j - 1].first)
              par[i][j] = make_pair(i - 1, j - 1);
            else if (i > 1 && a[i - 1].second == b[j].second)
              par[i][j] = make_pair(i - 1, j);
            else if (j > 1 && a[i].second == b[j - 1].second)
              par[i][j] = make_pair(i, j - 1);
            else if (dp[2][j][k] > dp[3][j - 1][k]) {
              if (a[i - 1].second == b[j].second)
                par[i][j] = make_pair(i - 1, j);
              else
                par[i][j] = par[i - 1][j];
              t = 0;
            } else if (dp[2][j][k] < dp[3][j - 1][k]) {
              if (a[i].second == b[j - 1].second)
                par[i][j] = make_pair(i, j - 1);
              else
                par[i][j] = par[i][j - 1];
              t = 0;
            } else {
              if (j > 2 && max(dp[1][j][k], dp[3][j - 2][k]) < dp[3][j][k]) {
                par[i][j] = make_pair(i - 1, j - 1);
              } else {
                if (i > 1 && j > 2 && a[i - 1].first == b[j - 2].first)
                  par[i][j] = make_pair(i - 1, j - 2);
                else if (i > 2 && j > 1 && a[i - 2].first == b[j - 1].first)
                  par[i][j] = make_pair(i - 2, j - 1);
                else {
                  if ((t % 2 || (i > 2 && dp[1][j][k] < dp[3][j][k]) ||
                       (i > 1 && dp[2][j][k] < dp[3][j][k]) ||
                       par[i][j - 1].first <= 0) &&
                      !(par[i - 1][j].first < par[i][j - 1].first &&
                        par[i - 1][j].second < par[i][j - 1].second)) {
                    if (a[i - 1].first == b[j].first &&
                        a[i - 1].first > a[par[i - 1][j].first].first) {
                      par[i][j] = make_pair(i - 1, j);
                      t = 0;
                    } else {
                      par[i][j] = par[i - 1][j];
                    }
                  } else {
                    if (a[i].first == b[j - 1].first &&
                        a[i].first > a[par[i][j - 1].first].first) {
                      t = 1;
                      par[i][j] = make_pair(i, j - 1);
                    } else {
                      t = 1;
                      par[i][j] = par[i][j - 1];
                    }
                  }
                }
              }
            }
          }
          dp[3][j][k] = max(dp[2][j][k], dp[3][j - 1][k]);
          if (k) {
            dp[3][j][k] = max(dp[3][j][k - 1], dp[3][j][k]);
          }
        }
        if (dp[3][j][k] > ans) {
          ans = max(ans, dp[3][j][k]);
          cur = make_pair(i, j);
        }
      }
    }
  }
  for (int i = 0; i < 1001; i++) {
    ans = max(ans, dp[3][m][i]);
  }
  cout << ans << '\n';
  stack<int> st;
  while (cur.first > 0 && cur.second > 0) {
    st.push(a[cur.first].first);
    cur = par[cur.first][cur.second];
  }
  while (!st.empty()) {
    cout << st.top() << ' ';
    st.pop();
  }
}
