#include <bits/stdc++.h>
using namespace std;
int n, m;
int a[(500 + 5)], b[(500 + 5)];
int dp[(500 + 5)][(500 + 5)];
int p[(500 + 5)][(500 + 5)];
int ans;
void LCIS(int x) {
  if (p[x][m]) LCIS(p[x][m]);
  cout << a[x] << " ";
  return;
}
int main() {
  cin >> n;
  for (register int i = 1; i <= n; ++i) {
    cin >> a[i];
  }
  cin >> m;
  for (register int i = 1; i <= m; ++i) {
    cin >> b[i];
  }
  int pos = 0;
  for (register int i = 1; i <= n; ++i) {
    for (register int j = 1; j <= m; ++j) {
      if (a[i] != b[j]) {
        dp[i][j] = dp[i][j - 1];
        p[i][j] = p[i][j - 1];
      } else {
        dp[i][j] = 1;
        for (register int k = 1; k < i; ++k) {
          if (a[i] > a[k] && dp[k][j - 1] + 1 > dp[i][j]) {
            dp[i][j] = dp[k][j - 1] + 1;
            p[i][j] = k;
          }
        }
      }
    }
    if (dp[i][m] > dp[pos][m]) pos = i;
  }
  cout << dp[pos][m] << "\n";
  if (dp[pos][m]) LCIS(pos);
  return 0;
}
