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<string>
#include<algorithm>
#include<vector>
#include<iomanip>
#include<math.h>
#include<complex>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#include<set>
#include<bitset>
using namespace std;
#define REP(i,m,n) for(int i=(int)m ; i < (int) n ; ++i )
#define rep(i,n) REP(i,0,n)
typedef long long ll;
typedef pair<int,int> pint;
typedef pair<ll,int> pli;
const int inf=1e9+7;
const ll longinf=1LL<<60 ;
const ll mod=1e9+7 ;

int main(){
    int n,q;cin>>n>>q;
    if(n==1){
        cout<<1<<endl;
        return 0;
    }
    int par[n+1],son[n+1],a[n+2];
    rep(i,n)cin>>a[i+1];
    a[0]=a[n+1]=0;
    rep(i,n+1){
        par[a[i+1]]=a[i];
        son[a[i]]=a[i+1];
    }
    rep(j,q){
        int x;cin>>x;
        if(par[x]!=0&&son[x]!=0){
        swap(son[par[x]],son[par[0]]);
        swap(par[son[x]],par[son[0]]);
        swap(son[x],son[0]);
        swap(par[x],par[0]);
        }
        else if(par[x]==0){
            par[x]=par[0];
            son[0]=son[x];
            par[0]=x;
            son[x]=0;
            son[par[x]]=x;
            par[son[0]]=0;
        }
        else if(son[x]==0){
            son[x]=son[0];
            par[0]=par[x];
            par[x]=0;
            son[0]=x;
            son[par[0]]=0;
            par[son[x]]=x;
            
        }
    }
    int ret=0;
    rep(i,n){
        cout<<son[ret];
        ret=son[ret];
        if(i!=n-1)cout<<" ";
    }
    cout<<endl;
    return 0;
}