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;

using ll = long long;
using ld = long double;

const double PI = 3.1415926535897932384626433832795;
const ll MOD = 1000000007;

const int dx[] = { 0, 1, 0, -1 };
const int dy[] = { -1, 0, 1, 0 };

int gcd(int x, int y) { return y ? gcd(y, x % y) : abs(x); }
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : abs(x); }
int lcm(int x, int y) { return x / gcd(x, y) * y; }
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }

// 解説をﾁｮｯﾄﾐﾀ(ゆるして)

struct node {
	int val;
	node *left, *right;
	node() : val(-1), left(NULL), right(NULL) {}
};

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

	int n, q;
	cin >> n >> q;

	vector<int> a(n);
	for (auto &v : a) {
		cin >> v;
		v--;
	}

	vector<node*> nodes(n);
	for (int i = 0; i < n; i++) {
		nodes[i] = new node();
	}

	node *head = new node();
	node *tail = new node();

	for (int i = 0; i < n; i++) {
		if (i > 0) {
			nodes[a[i]]->left = nodes[a[i - 1]];
		} else {
			head = nodes[a[i]];
		}

		if (i + 1 < n) {
			nodes[a[i]]->right = nodes[a[i + 1]];
		} else {
			tail = nodes[a[i]];
		}

		nodes[a[i]]->val = a[i];
	}

	for (int i = 0; i < q; i++) {
		int query;
		cin >> query;
		query--;

		if (nodes[query] == head) {
			nodes[query]->left = tail;
			tail->right = nodes[query];

			nodes[query]->right->left = NULL;

			head = nodes[query]->right;
			nodes[query]->right = NULL;
			tail = nodes[query];
		} else if (nodes[query] == tail) {
			nodes[query]->right = head;
			head->left = nodes[query];

			nodes[query]->left->right = NULL;

			tail = nodes[query]->left;
			nodes[query]->left = NULL;
			head = nodes[query];
		} else {
			nodes[query]->left->right = NULL;
			swap(nodes[query]->left, tail);
			nodes[query]->left->right = nodes[query];

			nodes[query]->right->left = NULL;
			swap(nodes[query]->right, head);
			nodes[query]->right->left = nodes[query];
		}
	}

	node* cur = head;
	while (cur != NULL) {
		cout << cur->val + 1;
		if (cur->right) cout << " ";
		cur = cur->right;
	}
	cout << endl;
	return 0;
}