#include <bits/stdc++.h>
using namespace std;
template <class T>
class LCIS {
 private:
  static const short maxLength = 501;
  int length1, length2;
  T seq1[maxLength], seq2[maxLength];
  unsigned short dp[maxLength][maxLength];
  unsigned short lastindex;
  short path[maxLength][maxLength];
  void copyArray(T num1[], int& l1, T num2[], int l2);

 public:
  LCIS(){};
  LCIS(T num1[], int l1, T num2[], int l2);
  void init(T num1[], int l1, T num2[], int l2);
  int getLCIS();
  int getLastindex();
  void printPath(int x, int y);
};
template <class T>
void LCIS<T>::copyArray(T num1[], int& l1, T num2[], int l2) {
  l1 = l2;
  for (int i = 1; i <= l1; i++) num1[i] = num2[i];
}
template <class T>
LCIS<T>::LCIS(T num1[], int l1, T num2[], int l2) {
  init(num1, l1, num2, l2);
}
template <class T>
void LCIS<T>::init(T num1[], int l1, T num2[], int l2) {
  copyArray(seq1, length1, num1, l1);
  copyArray(seq2, length2, num2, l2);
  memset(dp, 0, sizeof(dp));
  memset(path, 0, sizeof(path));
}
template <class T>
int LCIS<T>::getLCIS() {
  for (int i = 1; i <= length1; i++) {
    int maxj = 0;
    for (int j = 1; j <= length2; j++) {
      if (seq1[i] > seq2[j] && dp[i - 1][maxj] < dp[i - 1][j]) maxj = j;
      if (seq1[i] != seq2[j]) {
        dp[i][j] = dp[i - 1][j];
        path[i][j] = j;
      } else if (seq1[i] == seq2[j]) {
        dp[i][j] = dp[i - 1][maxj] + 1;
        path[i][j] = maxj;
      }
    }
  }
  lastindex = 1;
  for (int i = 1; i <= length2; i++)
    if (dp[length1][lastindex] < dp[length1][i]) lastindex = i;
  return dp[length1][lastindex];
}
template <class T>
int LCIS<T>::getLastindex() {
  return lastindex;
}
template <class T>
void LCIS<T>::printPath(int x, int y) {
  if (x < 1 || y < 1) return;
  while (x >= 1 && path[x][y] == y) x--;
  if (x >= 1) {
    printPath(x, path[x][y]);
    if (!path[x][y])
      printf("%d", seq2[y]);
    else
      printf(" %d", seq2[y]);
  }
}
LCIS<int> lcis;
int main() {
  int n, m;
  int num1[501], num2[501];
  while (~scanf("%d", &n)) {
    for (int i = 1; i <= n; i++) scanf("%d", &num1[i]);
    scanf("%d", &m);
    for (int i = 1; i <= m; i++) scanf("%d", &num2[i]);
    lcis.init(num1, n, num2, m);
    printf("%d\n", lcis.getLCIS());
    lcis.printPath(n, lcis.getLastindex());
    printf("\n");
  }
  return 0;
}
