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 <bits/stdc++.h>

using namespace std;

#ifdef DEBUG_MODE
	#define DBG(n) n;
#else
	#define DBG(n) ;
#endif
#define REP(i,n) for(ll (i) = (0);(i) < (n);++i)
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v,n) {for(int W = 0;W < (n);W++)cerr << v[W] << ' ';cerr << endl << endl;}
#define SHOW2d(v,i,j) {for(int aaa = 0;aaa < i;aaa++){for(int bbb = 0;bbb < j;bbb++)cerr << v[aaa][bbb] << ' ';cerr << endl;}cerr << endl;}
#define ALL(v) v.begin(),v.end()
#define Decimal fixed<<setprecision(20)
#define INF 1000000000
#define LLINF 1000000000000000000
#define MOD 1000000007

typedef long long ll;
typedef pair<ll,ll> P;

int nxt[111111];
int tsutaJ[111111];
	
int main(){
	int n,q;cin >> n >> q;
	vector<int> seica(n);
	REP(i,n){
		cin >> seica[i];
	}
	
	if(n == 1){
		REP(i,q){
			int tyome;cin >> tyome;
		}
		cout << 1 << endl;
		return 0;
	}
	
	REP(i,n-1){
		nxt[seica[i]] = seica[i+1];
		tsutaJ[seica[i+1]] = seica[i];
	}
	nxt[0] = seica[0];
	tsutaJ[0] = -1;
	nxt[seica[n-1]] = n+1;
	tsutaJ[n+1] = seica[n-1];
	tsutaJ[seica[0]] = 0;
	REP(i,q){
		int neko;
		cin >> neko;
		if(nxt[neko] == n+1){
			int aaa = nxt[0];
			int bbb = tsutaJ[neko];
			nxt[0] = neko;
			tsutaJ[neko] = 0;
			nxt[neko] = aaa;
			tsutaJ[aaa] = neko;
			nxt[bbb] = n+1;
			tsutaJ[n+1] = bbb;
		} else if(tsutaJ[neko] == 0){
			int aaa = nxt[neko];
			int bbb = tsutaJ[n+1];
			nxt[0] = aaa;
			tsutaJ[aaa] = 0;
			nxt[bbb] = neko;
			tsutaJ[neko] = bbb;
			nxt[neko] = n+1;
			tsutaJ[n+1] = neko;
		}
		else{
			int aaa = nxt[0];
			int bbb = tsutaJ[neko];
			int ccc = nxt[neko];
			int ddd = tsutaJ[n+1];
			nxt[0] = ccc;
			tsutaJ[ccc] = 0;
			nxt[ddd] = neko;
			tsutaJ[neko] = ddd;
			nxt[neko] = aaa;
			tsutaJ[aaa] = neko;
			nxt[bbb] = n+1;
			tsutaJ[n+1] = bbb;
		}
	}
	int neko = nxt[0];
	REP(i,n){
		cout << neko;
		if(i != n-1)cout << " ";
		neko = nxt[neko];
	}
	cout << endl;
	return 0;
}