#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <stack>
#include <queue>
#include <list>
using namespace std;

queue<int> pilacua(queue<int> q, stack<int> s);

int main()
{
    int tamcola, tampila, p;
    while (cin >> tamcola)
    {
        queue<int> q;
        stack<int> s;
        cin >> tampila;
        for (int i = 0; i < tamcola; i++)
        {
            cin >> p;
            q.push(p);
        }
        for (int i = 0; i < tampila; i++)
        {
            cin >> p;
            s.push(p);
        }
        queue<int> q2 = pilacua(q, s);
        while (q2.size() != 1)
        {
            cout << q2.front() << " ";
            q2.pop();
        }
        cout << q2.front() << endl;
    }
}