#include <bits/stdc++.h>
using namespace std;
const int MAX = 501;
short dp[MAX + 1][MAX + 1][MAX + 1];
int a[MAX], b[MAX];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) scanf("%d", a + i);
  int m;
  scanf("%d", &m);
  for (int i = 1; i <= m; ++i) scanf("%d", b + i);
  a[0] = b[0] = -1;
  for (int i = n + 1; i >= 1; --i) {
    for (int j = m + 1; j >= 1; --j) {
      for (int k = 0; k < i; ++k) {
        if (i == n + 1 || j == m + 1) {
          dp[i][j][k] = 0;
          continue;
        }
        dp[i][j][k] = max(dp[i + 1][j][k], dp[i][j + 1][k]);
        if (a[i] == b[j] && a[i] > a[k])
          dp[i][j][k] = max((int)dp[i][j][k], dp[i + 1][j + 1][i] + 1);
      }
    }
  }
  int answer = dp[1][1][0];
  printf("%d\n", answer);
  for (int i = 1, j = 1, k = 0; i <= n && j <= m;) {
    answer = dp[i][j][k];
    if (dp[i + 1][j][k] == answer)
      ++i;
    else if (dp[i][j + 1][k] == answer)
      ++j;
    else if (a[i] == b[j] && a[i] > a[k] && dp[i + 1][j + 1][i] + 1 == answer) {
      k = i;
      ++i;
      ++j;
      printf("%d ", a[k]);
    } else
      assert(false);
  }
  putchar('\n');
  return 0;
}
