#include <bits/stdc++.h>
using namespace std;
const long long int logx = 30;
const long long int N = 5e2 + 5;
const long long int M = 3e5 + 5;
const long long int mod = 998244353;
const long double INF = 1e18 + 5;
const double PI = 3.14159265;
inline long long int mul(long long int a, long long int b) {
  return (a * 1ll * b) % mod;
}
inline long long int sub(long long int a, long long int b) {
  long long int c = a - b;
  if (c < 0) c += mod;
  return c;
}
inline long long int add(long long int a, long long int b) {
  long long int c = a + b;
  if (c > mod) c -= mod;
  return c;
}
template <typename T>
T power(T x, T y, long long int m) {
  T ans = 1;
  while (y > 0) {
    if (y & 1LL) ans = (ans * x) % m;
    y >>= 1ll;
    x = (x * x) % m;
  }
  return ans % m;
}
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
  cerr << name << " : " << arg1 << '\n';
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
  const char* comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
map<long long int, long long int> val;
long long int n, m, a[N], b[N], dp[N][N];
map<long long int, vector<long long int> > pos;
void Compress() {
  long long int cnt = 1;
  set<long long int> ser;
  for (long long int i = 0; i < n; i++) {
    ser.insert(a[i]);
  }
  for (long long int i = 0; i < m; i++) {
    ser.insert(b[i]);
  }
  for (auto i : ser) {
    val[i] = cnt++;
  }
}
long long int F(long long int i, long long int j) {
  long long int res = 0;
  if (i == n - 1 || j == m - 1) {
    return 0;
  }
  if (dp[i][j] != -1) {
    return dp[i][j];
  }
  if (a[i] == b[j]) {
    for (long long int h = i + 1; h < n; h++) {
      if (a[i] >= a[h]) {
        continue;
      }
      long long int ub =
          upper_bound(pos[val[a[h]]].begin(), pos[val[a[h]]].end(), j) -
          pos[val[a[h]]].begin();
      if (ub == pos[val[a[h]]].size()) {
        continue;
      }
      res = max(res, 1 + F(h, pos[val[a[h]]][ub]));
    }
  }
  return dp[i][j] = res;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ;
  cin >> n;
  long long int res = 0;
  memset(dp, -1, sizeof(dp));
  for (long long int i = 0; i < n; i++) {
    cin >> a[i];
  }
  cin >> m;
  for (long long int i = 0; i < m; i++) {
    cin >> b[i];
  }
  Compress();
  for (long long int i = 0; i < m; i++) {
    pos[val[b[i]]].push_back(i);
  }
  for (long long int i = 0; i < n; i++) {
    for (long long int j = 0; j < m; j++) {
      long long int cur = (a[i] == b[j]) + F(i, j);
      res = max(res, cur);
    }
  }
  vector<long long int> vec;
  long long int cnt = 0, lasi = -1, lasj = -1, oldans = 0, lasval = -INF;
  while (cnt < res) {
    for (long long int i = lasi + 1; i < n; i++) {
      long long int first = 0;
      for (long long int j = lasj + 1; j < m; j++) {
        if (a[i] != b[j]) {
          continue;
        }
        long long int tempans = oldans + 1 + F(i, j);
        if (tempans != res) {
          continue;
        }
        if (lasval >= a[i]) {
          continue;
        }
        vec.push_back(a[i]);
        first = 1;
        lasval = a[i];
        cnt++;
        oldans++;
        lasi = i;
        lasj = j;
        break;
      }
      if (first) {
        break;
      }
    }
  }
  cout << res << endl;
  for (auto i : vec) {
    cout << i << " ";
  }
}

### Prompt

Your task is to create a cpp solution to the following problem:

This problem differs from one which was on the online contest.

The sequence a1, a2, ..., an is called increasing, if ai < ai + 1 for i < n.

The sequence s1, s2, ..., sk is called the subsequence of the sequence a1, a2, ..., an, if there exist such a set of indexes 1 ≤ i1 < i2 < ... < ik ≤ n that aij = sj. In other words, the sequence s can be derived from the sequence a by crossing out some elements.

You are given two sequences of integer numbers. You are to find their longest common increasing subsequence, i.e. an increasing sequence of maximum length that is the subsequence of both sequences.

Input

The first line contains an integer n (1 ≤ n ≤ 500) — the length of the first sequence. The second line contains n space-separated integers from the range [0, 109] — elements of the first sequence. The third line contains an integer m (1 ≤ m ≤ 500) — the length of the second sequence. The fourth line contains m space-separated integers from the range [0, 109] — elements of the second sequence.

Output

In the first line output k — the length of the longest common increasing subsequence. In the second line output the subsequence itself. Separate the elements with a space. If there are several solutions, output any.

Examples

Input

7
2 3 1 6 5 4 6
4
1 3 5 6


Output

3
3 5 6 


Input

5
1 2 0 2 1
3
1 0 1


Output

2
0 1 

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
const long long int logx = 30;
const long long int N = 5e2 + 5;
const long long int M = 3e5 + 5;
const long long int mod = 998244353;
const long double INF = 1e18 + 5;
const double PI = 3.14159265;
inline long long int mul(long long int a, long long int b) {
  return (a * 1ll * b) % mod;
}
inline long long int sub(long long int a, long long int b) {
  long long int c = a - b;
  if (c < 0) c += mod;
  return c;
}
inline long long int add(long long int a, long long int b) {
  long long int c = a + b;
  if (c > mod) c -= mod;
  return c;
}
template <typename T>
T power(T x, T y, long long int m) {
  T ans = 1;
  while (y > 0) {
    if (y & 1LL) ans = (ans * x) % m;
    y >>= 1ll;
    x = (x * x) % m;
  }
  return ans % m;
}
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
  cerr << name << " : " << arg1 << '\n';
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
  const char* comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
map<long long int, long long int> val;
long long int n, m, a[N], b[N], dp[N][N];
map<long long int, vector<long long int> > pos;
void Compress() {
  long long int cnt = 1;
  set<long long int> ser;
  for (long long int i = 0; i < n; i++) {
    ser.insert(a[i]);
  }
  for (long long int i = 0; i < m; i++) {
    ser.insert(b[i]);
  }
  for (auto i : ser) {
    val[i] = cnt++;
  }
}
long long int F(long long int i, long long int j) {
  long long int res = 0;
  if (i == n - 1 || j == m - 1) {
    return 0;
  }
  if (dp[i][j] != -1) {
    return dp[i][j];
  }
  if (a[i] == b[j]) {
    for (long long int h = i + 1; h < n; h++) {
      if (a[i] >= a[h]) {
        continue;
      }
      long long int ub =
          upper_bound(pos[val[a[h]]].begin(), pos[val[a[h]]].end(), j) -
          pos[val[a[h]]].begin();
      if (ub == pos[val[a[h]]].size()) {
        continue;
      }
      res = max(res, 1 + F(h, pos[val[a[h]]][ub]));
    }
  }
  return dp[i][j] = res;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ;
  cin >> n;
  long long int res = 0;
  memset(dp, -1, sizeof(dp));
  for (long long int i = 0; i < n; i++) {
    cin >> a[i];
  }
  cin >> m;
  for (long long int i = 0; i < m; i++) {
    cin >> b[i];
  }
  Compress();
  for (long long int i = 0; i < m; i++) {
    pos[val[b[i]]].push_back(i);
  }
  for (long long int i = 0; i < n; i++) {
    for (long long int j = 0; j < m; j++) {
      long long int cur = (a[i] == b[j]) + F(i, j);
      res = max(res, cur);
    }
  }
  vector<long long int> vec;
  long long int cnt = 0, lasi = -1, lasj = -1, oldans = 0, lasval = -INF;
  while (cnt < res) {
    for (long long int i = lasi + 1; i < n; i++) {
      long long int first = 0;
      for (long long int j = lasj + 1; j < m; j++) {
        if (a[i] != b[j]) {
          continue;
        }
        long long int tempans = oldans + 1 + F(i, j);
        if (tempans != res) {
          continue;
        }
        if (lasval >= a[i]) {
          continue;
        }
        vec.push_back(a[i]);
        first = 1;
        lasval = a[i];
        cnt++;
        oldans++;
        lasi = i;
        lasj = j;
        break;
      }
      if (first) {
        break;
      }
    }
  }
  cout << res << endl;
  for (auto i : vec) {
    cout << i << " ";
  }
}

```