#include <bits/stdc++.h>
using namespace std;
long long n, t, ans, m, dp[550][550], a[550], b[550], path[550][550], posi = 0,
                                                                      posj = 0;
void print(long long p) {
  if (!p) return;
  print(path[n][p]);
  printf("%I64d ", b[p]);
}
signed main() {
  scanf("%I64d", &n);
  for (long long i = 1; i <= n; i++) scanf("%I64d", &a[i]);
  a[0] = -0x3f3f3f3f;
  b[0] = -0x3f3f3f3f;
  scanf("%I64d", &m);
  for (long long j = 1; j <= m; j++) scanf("%I64d", &b[j]);
  for (long long i = 1; i <= n; i++, posj = 0)
    for (long long j = 1; j <= m; j++)
      if (a[i] == b[j]) {
        for (long long k = j - 1; k >= 0; k--) {
          if (a[i] > b[k])
            if (dp[i - 1][k] + 1 > dp[i][j]) {
              dp[i][j] = dp[i - 1][k] + 1;
              path[i][j] = posj;
              ans = max(ans, dp[i][j]);
            }
        }
      } else {
        dp[i][j] = dp[i - 1][j];
        path[i][j] = path[i - 1][j];
        if (b[j] < a[i] && dp[i - 1][j] > dp[i - 1][posj]) posj = j;
      }
  printf("%I64d\n", ans);
  for (long long i = 1; i <= m; i++)
    if (dp[n][i] > dp[n][posj]) posj = i;
  print(posj);
}
