#include <bits/stdc++.h>
const int N = 500;
int l1, l2, ans, a1, b1;
int a[N + 5], b[N + 5], d[N + 5][N + 5];
void Print(int x, int y) {
  if (d[x][y] == 1) {
    printf("%d ", b[y]);
    return;
  }
  if (d[x][y] == d[x - 1][y])
    Print(x - 1, y);
  else
    for (int i = y - 1; i >= 1; i--)
      if (b[i] < b[y] && d[x][y] == d[x - 1][i] + 1) {
        Print(x, i);
        printf("%d ", a[x]);
        return;
      }
}
int main() {
  scanf("%d", &l1);
  for (int i = 1; i <= l1; i++) scanf("%d", &a[i]);
  scanf("%d", &l2);
  for (int i = 1; i <= l2; i++) scanf("%d", &b[i]);
  for (int i = 1; i <= l1; i++) {
    int Mx = 0;
    for (int j = 1; j <= l2; j++) {
      if (a[i] != b[j]) d[i][j] = d[i - 1][j];
      if (a[i] > b[j] && Mx < d[i - 1][j]) Mx = d[i - 1][j];
      if (a[i] == b[j]) {
        d[i][j] = Mx + 1;
        if (ans < d[i][j]) {
          ans = d[i][j];
          a1 = i;
          b1 = j;
        }
      }
    }
  }
  printf("%d\n", ans);
  if (ans) Print(a1, b1);
  return 0;
}
