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<iomanip>
#include<limits>
#include<thread>
#include<utility>
#include<iostream>
#include<string>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<numeric>
#include<cassert>
#include<random>
#include<chrono>
#include<unordered_map>
#include<fstream>
#include<list>
#include<functional>
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
typedef pair<ll,ll> pll;
typedef pair<int,int> pi;
typedef pair<double,double> pd;
typedef pair<double,ll> pdl;
#define F first
#define S second
const ll E=1e18+7;
const ll MOD=1000000007;

class List{
public:
    struct node{
        node* front;
        node* back;
        ll a;
        
        node(node* x,node* b,ll A=-1){
            front=x;
            back=b;
            a=A;
        }
        
        node(){
            front=NULL; back=NULL; a=-1;
        }
    };
    
    vector<node*> M;
    
    node root;
    
    List(vector<ll> A):root(NULL,NULL){
        M.resize(A.size()+1,NULL);
        node* L=&root;
        for(int i=0;i<A.size();i++){
            node* w=(node*)malloc(sizeof(node));
            (*L).back=w;
            (*w).front=L;
            (*w).a=A[i];
            M[A[i]]=w;
            L=w;
        }
        root.front=L;
        (*L).back=&root;
    }
    
    void swap(ll q){
        node* B=M[q];
        node* AL=(*B).front;
        node* AS=root.back;
        node* CS=(*B).back;
        node* CL=root.front;
        if(CS==&root){
            root.front=AL;
            root.back=B;
            (*AL).back=&root;
            (*B).front=&root;
            (*B).back=AS;
            (*AS).front=B;
        }
        else if(AL==&root){
            (*B).back=&root;
            root.front=B;
            (*CS).front=&root;
            root.back=CS;
            (*CL).back=B;
            (*B).front=CL;
        }
        else{
            (*CL).back=B;
            (*B).front=CL;
            (*AS).front=B;
            (*B).back=AS;
            (*AL).back=&root;
            root.front=AL;
            (*CS).front=&root;
            root.back=CS;
        }
    }
    
    void show(){
        node* where=root.back;
        cout<<(*where).a;
        where=(*where).back;
        while(where!=&root){
            cout<<" "<<(*where).a;
            where=(*where).back;
        }
        cout<<endl;
    }
};




int main(){
    ll n,q;
    cin>>n>>q;
    vector<ll> a(n);
    if(n==1){cout<<1<<endl; return 0;}
    for(int i=0;i<n;i++){
        cin>>a[i];
    }
    List L(a);
    //L.show();
    while(q--){
        ll Q;
        cin>>Q;
        //cout<<q<<endl;
        L.swap(Q);
    }
    //cout<<"!"<<endl;
    L.show();
    
    return 0;
}