#include <bits/stdc++.h>
using namespace std;
const int maxn = 505;
int a[maxn], b[maxn];
int dp[maxn][maxn];
int back[maxn][maxn];
int best = 0;
int bi, bj;
int main() {
  int n, m;
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> a[i];
  cin >> m;
  for (int i = 1; i <= m; i++) cin >> b[i];
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      dp[i][j] = dp[i][j - 1];
      back[i][j] = -1;
      if (a[i] == b[j]) {
        if (dp[i][j] < 1) {
          dp[i][j] = 1;
          back[i][j] = i - 1;
        }
        for (int k = 1; k < i; k++) {
          if (a[k] < a[i] && dp[k][j - 1] + 1 > dp[i][j]) {
            dp[i][j] = dp[k][j - 1] + 1;
            back[i][j] = k;
          }
        }
      }
      if (best < dp[i][j]) {
        best = dp[i][j];
        bi = i;
        bj = j;
      }
    }
  }
  int curbest = best;
  cout << best << endl;
  vector<int> ans;
  while (curbest) {
    ans.push_back(a[bi]);
    while (back[bi][bj] == -1) bj--;
    bi = back[bi][bj];
    bj--;
    assert(curbest - 1 == dp[bi][bj]);
    curbest--;
  }
  reverse(ans.begin(), ans.end());
  for (int x : ans) cout << x << " ";
  cout << endl;
}
