import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, std.bitmanip;

const long MOD = 998244353;
const long INF = 1L << 59;

void main() {
    auto s = readln.split.map!(to!int);
    auto N = s[0];
    auto K = s[1];
    auto A = readln.split.map!(x => x.to!int-1).array;
    auto Q = readln.split.map!(x => x.to!int-1).array;

    if (N == 1) {
        1.writeln;
        return;
    }

    int front = A.front;
    int back = A.back;
    auto prev = new int[](N);
    auto next = new int[](N);
    foreach (i; 0..N) {
        prev[A[i]] = i == 0 ? -1 : A[i-1];
        next[A[i]] = i == N-1 ? -1 : A[i+1];
    }

    foreach (q; Q) {
        int f = front;
        int b = back;
        int cur = q;
        int p = prev[q];
        int n = next[q];
        if (p == -1) {
            prev[cur] = back;
            next[cur] = -1;
            prev[n] = -1;
            next[back] = cur;
            front = n;
            back = cur;
        } else if (n == -1) {
            prev[cur] = -1;
            next[cur] = front;
            prev[front] = cur;
            next[p] = -1;
            front = cur;
            back = p;
        } else {
            prev[n] = -1;
            next[p] = -1;
            prev[cur] = b;
            next[cur] = f;
            prev[f] = cur;
            next[b] = cur;
            front = n;
            back = p;
        }
    }

    int[] ans;
    for (int a = front; a != -1; a = next[a]) {
        ans ~= a + 1;
    }
    ans.map!(to!string).join(" ").writeln;
}

