Problem: B: Pivots

problem

Given a permutation of length N, a_1, a_2, ..., a_N, which is a permutation of integers from 1 to N. Also, Q queries are given in order for this permutation. In the i-th query, you have to do the following:

* The value q_i (1 \ leq q_i \ leq N) is given. In the permutation \\ {a_1, a_2, ..., a_N \\}, where L is the permutation on the left side of q_i and R is the permutation on the right side of q_i, the original permutation L \\ q_i \\ R is R \\ Change to q_i \\ L. That is, when q_ {i} = a_j, the permutations \\ {a_1, ..., a_ {j-1}, a_j, a_ {j + 1}, ..., a_N \\} are \\ { Change to a_ {j + 1}, ..., a_N, a_j, a_1, ..., a_ {j-1} \\}.



The permutations L and R may be empty. For example, if L is empty, change q_i \\ R to R \\ q_i. The same is true when R is empty.

Output the permutation after processing these Q queries in order for the given permutation on one line.

Input format


N Q
a_1 a_2 ... a_N
q_1 q_2 ... q_Q


All inputs are integers.

The first row gives the number of elements in the permutation N and the number of queries Q, separated by blanks. In the second line, permutations a_1, a_2, ..., a_N, in which integers from 1 to N are rearranged, are given separated by blanks. The third line gives Q queries, separated by blanks. q_i represents the i-th query.

Constraint

* 1 \ leq N \ leq 10 ^ 5
* 1 \ leq Q \ leq 10 ^ 5
* 1 \ leq a_i \ leq N
* a_i is different
* 1 \ leq q_i \ leq N



Output format

Output the permutation after processing all the queries in order on one line.

Input example 1


5 2
1 5 3 2 4
5 2


Output example 1


4 5 1 2 3

* The first query changes the permutation to \\ {3, 2, 4, 5, 1 \\}.
* The second query changes the permutation to \\ {4, 5, 1, 2, 3 \\}.



Input example 2


5 1
1 2 3 4 5
Five


Output example 2


5 1 2 3 4





Example

Input

5 2
1 5 3 2 4
5 2


Output

4 5 1 2 3

Solution (in CPP):

Solution:
#include "iostream"
#include "climits"
#include "list"
#include "queue"
#include "stack"
#include "set"
#include "functional"
#include "algorithm"
#include "string"
#include "map"
#include "unordered_map"
#include "unordered_set"
#include "iomanip"
#include "cmath"
#include "random"
#include "bitset"
#include "cstdio"
#include "numeric"
#include "cassert"
#include "functional"
#include "ctime"

using namespace std;

constexpr long long int MOD = 1000000007;
//constexpr int MOD = 1000000007;
//constexpr int MOD = 998244353;
//constexpr long long int MOD = 998244353;
constexpr double EPS = 1e-8;

//int N, M, K, H, W, L, R;
long long int N, M, K, H, W, L, R;

int nx[120000];
int bf[120000];

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);

	cin >> N >> K;
	vector<int>v(N);
	for (auto &i : v)cin >> i;
	for (int i = 1; i <= N; i++)nx[i] = nx[i] = -1;
	for (int i = 1; i < N; i++) {
		bf[v[i]] = v[i - 1];
	}
	for (int i = 0; i < N - 1; i++) {
		nx[v[i]] = v[i + 1];
	}
	int tp = v[0];
	int bk = v.back();
	while (K--) {
		cin >> M;
		if (M == tp && M == bk) {
			continue;
		}
		else if (M == tp) {
			bf[nx[M]] = -1;
			tp = nx[M];
			nx[bk] = M;
			nx[M] = -1;
			bf[M] = bk;
			bk = M;
		}
		else if (M == bk) {
			nx[bf[M]] = -1;
			bk = bf[M];
			bf[tp] = M;
			bf[M] = -1;
			nx[M] = tp;
			tp = M;
		}
		else {
			nx[bf[M]] = -1;
			bf[nx[M]] = -1;
			nx[bk] = M;
			bf[tp] = M;
			int ntp = nx[M];
			int nbk = bf[M];
			nx[M] = tp;
			bf[M] = bk;
			tp = ntp;
			bk = nbk;
		}
	}
	vector<int>ans;
	int cn = tp;
	ans.push_back(cn);
	while (cn!=bk) {
		cn = nx[cn];
		ans.push_back(cn);
	}
	for (int i = 0; i < N; i++) {
		if (i)cout << " ";
		cout << ans[i];
	}
	cout << endl;
}