#include <bits/stdc++.h>
using namespace std;
using lint = long long;
int a[555];
int b[555];
int d[555][555];
map<int, vector<int>> inb;
void solve(istream& cin, ostream& cout) {
  int n;
  cin >> n;
  for (int i = (0); i < int(n); ++i) {
    cin >> a[i];
  }
  int m;
  cin >> m;
  for (int i = (0); i < int(m); ++i) {
    cin >> b[i];
    inb[b[i]].push_back(i);
  }
  memset(d, -1, sizeof(int) * 555 * 555);
  int mx = 0;
  for (int i = (0); i < int(n); ++i) {
    auto it = inb.find(a[i]);
    if (it == inb.end()) {
      continue;
    }
    auto& v = it->second;
    d[i][1] = v[0];
    mx = max(mx, 1);
    for (int j = (0); j < int(i); ++j) {
      if (a[j] >= a[i]) {
        continue;
      }
      for (int k = (1); k < int(n); ++k) {
        if (d[j][k] == -1) {
          break;
        }
        auto t = upper_bound(v.begin(), v.end(), d[j][k]);
        if (t == v.end()) {
          continue;
        }
        d[i][k + 1] = *t;
        mx = max(mx, k + 1);
      }
    }
  }
  cout << mx << "\n";
  vector<int> ans;
  int lp = 1e9;
  for (int i = int(n) - 1; i >= (0); --i) {
    if (d[i][mx] != -1 && (ans.empty() || a[i] < ans.back()) && d[i][mx] < lp) {
      ans.push_back(a[i]);
      lp = d[i][mx];
      --mx;
    }
  }
  reverse(ans.begin(), ans.end());
  for (int i : ans) {
    cout << i << " ";
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  solve(cin, cout);
  return 0;
}
