/**
* időlimit túllépés
**/
#include <stdlib.h>
#include <stdio.h>
typedef struct Driver {
int driverNum;
int overtakenCount;
} Driver;
int driversCount;
int overtake(Driver* drivers, int overtaker) {
int overtakerIndex = 1;
while (overtakerIndex < driversCount && drivers[overtakerIndex].driverNum != overtaker)
{
overtakerIndex++;
}
Driver swap = drivers[overtakerIndex];
drivers[overtakerIndex] = drivers[overtakerIndex - 1];
drivers[overtakerIndex - 1] = swap;
drivers[overtakerIndex].overtakenCount++;
return overtakerIndex;
}
int main() {
int overtakesCount;
scanf("%d%d", &driversCount, &overtakesCount);
Driver* drivers = (Driver*)malloc(sizeof(Driver)*driversCount);
for (int i = 0; i < driversCount; i++)
{
scanf("%d", &drivers[i].driverNum);
drivers[i].overtakenCount = 0;
}
int* overtakes = (int*)malloc(sizeof(int)*overtakesCount);
Driver mostOvertaken = drivers[1];
for (int i = 0; i < overtakesCount; i++)
{
scanf("%d", &overtakes[i]);
int overtaken = overtake(drivers, overtakes[i]);
if (drivers[overtaken].overtakenCount > mostOvertaken.overtakenCount)
{
mostOvertaken = drivers[overtaken];
}
else if (drivers[overtaken].overtakenCount == mostOvertaken.overtakenCount && drivers[overtaken].driverNum < mostOvertaken.driverNum)
{
mostOvertaken = drivers[overtaken];
}
printf("%d\n", mostOvertaken.driverNum);
}
free(drivers);
free(overtakes);
return 0;
}