#include <stdlib.h>
#include <stdio.h>
int main() {
int driversCount, overtakesCount;
scanf("%d%d", &driversCount, &overtakesCount);
int* driverNums = (int*)malloc(sizeof(int) * (driversCount + 1)); // tömb indexelése = sorrend, tömb elemei = versenyző száma
int** driverOvertakenCount = (int**)malloc(sizeof(int*) * 2); // tömb indexelése = versenyző száma, tömb elemei = index a driverNums tömbben (sorrend)
// második dimenzió = hányszor előzték meg
driverOvertakenCount[0] = (int*)malloc(sizeof(int) * (driversCount + 1));
driverOvertakenCount[1] = (int*)calloc(driversCount + 1, sizeof(int));
for (int i = 1; i <= driversCount; i++)
{
scanf("%d", &driverNums[i]);
driverOvertakenCount[0][driverNums[i]] = i;
}
int* overtakes = (int*)malloc(sizeof(int) * overtakesCount);
int mostOvertaken[] = { 0, 0 }; // versenyző száma, megelőzések száma
for (int i = 0; i < overtakesCount; i++)
{
scanf("%d", &overtakes[i]);
int swap = driverNums[driverOvertakenCount[0][overtakes[i]]];
driverNums[driverOvertakenCount[0][overtakes[i]]] = driverNums[driverOvertakenCount[0][overtakes[i]] - 1];
driverNums[driverOvertakenCount[0][overtakes[i]] - 1] = swap;
swap = driverOvertakenCount[0][driverNums[driverOvertakenCount[0][overtakes[i]]]];
driverOvertakenCount[0][driverNums[driverOvertakenCount[0][overtakes[i]]]] = driverOvertakenCount[0][overtakes[i]];
driverOvertakenCount[0][overtakes[i]] = swap;
driverOvertakenCount[1][driverNums[driverOvertakenCount[0][overtakes[i]] + 1]]++;
if (driverOvertakenCount[1][driverNums[driverOvertakenCount[0][overtakes[i]] + 1]] > mostOvertaken[1])
{
mostOvertaken[0] = driverNums[driverOvertakenCount[0][overtakes[i]] + 1];
mostOvertaken[1] = driverOvertakenCount[1][driverNums[driverOvertakenCount[0][overtakes[i]] + 1]];
}
else if (driverOvertakenCount[1][driverNums[driverOvertakenCount[0][overtakes[i]] + 1]] == mostOvertaken[1]
&& driverNums[driverOvertakenCount[0][overtakes[i]] + 1] < mostOvertaken[0])
{
mostOvertaken[0] = driverNums[driverOvertakenCount[0][overtakes[i]] + 1];
}
printf("%d\n", mostOvertaken[0]);
}
free(driverOvertakenCount[0]);
free(driverOvertakenCount[1]);
free(driverOvertakenCount);
free(overtakes);
free(driverNums);
return 0;
}