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>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 99999999999999999
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


#define NUM 100005

struct LIST{
	int next_value,pre_value;
};
LIST info[NUM]; //★★インデックスではなく値基準★★

int main(){

	int N,num_query;
	scanf("%d %d",&N,&num_query);

	if(N == 1){
		printf("1\n");
		return 0;
	}

	int head,tail;
	int pre,tmp;

	for(int i = 0; i < N; i++){

		scanf("%d",&tmp);
		//連結リストではなく、片方向リストにする

		if(i == 0){

			head = tmp;
			info[tmp].pre_value = -1;

		}else if(i == N-1){

			tail = tmp;
			info[tmp].pre_value = pre;
			info[pre].next_value = tmp;
			info[tmp].next_value = -1;

		}else{

			info[tmp].pre_value = pre;
			info[pre].next_value = tmp;
		}
		pre = tmp;
	}

	int mid_value,next_head,next_tail;

	for(int loop = 0; loop < num_query; loop++){

		scanf("%d",&mid_value); //中央の★値★を取得

		if(mid_value == head){

			info[tail].next_value = mid_value;
			info[mid_value].pre_value = tail;
			head = info[mid_value].next_value;
			tail = mid_value;

		}else if(mid_value == tail){

			info[mid_value].next_value = head;
			info[head].pre_value = mid_value;
			tail = info[mid_value].pre_value;
			head = mid_value;

		}else{

			next_head = info[mid_value].next_value;
			next_tail = info[mid_value].pre_value;

			info[mid_value].next_value = head;
			info[head].pre_value = mid_value;
			info[mid_value].pre_value = tail;
			info[tail].next_value = mid_value;

			head = next_head;
			tail = next_tail;
		}

		info[head].pre_value = -1;
		info[tail].next_value = -1;
	}

	printf("%d",head);
	pre = head;
	int count = 1,next;

	while(count < N){
		next = info[pre].next_value;
		printf(" %d",next);
		count++;
		pre = next;
	}
	printf("\n");

	return 0;
}