import
java.util.*;
public
class
Main {
static
class
Pair<T, U> {
private
T key;
private
U value;
public
Pair(T key, U value) {
this
.key = key;
this
.value = value;
}
public
T getKey() {
return
key;
}
public
U getValue() {
return
value;
}
}
public
static
int
binarySearch(List<Pair<Integer, Integer>> Ans,
int
low,
int
high,
int
value) {
if
(value <= Ans.get(low).getKey()) {
return
Ans.get(low).getValue();
}
if
(value >= Ans.get(high).getKey()) {
return
Ans.get(high).getValue();
}
int
winningCandidate =
0
;
while
(low <= high) {
int
mid = low + (high - low) /
2
;
if
(Ans.get(mid).getKey() == value) {
winningCandidate = Ans.get(mid).getValue();
break
;
}
else
if
(Ans.get(mid).getKey() < value) {
winningCandidate = Ans.get(mid).getValue();
low = mid +
1
;
}
else
{
high = mid -
1
;
}
}
return
winningCandidate;
}
public
static
void
findWinner(Pair<Integer,
Integer>[] arr,
int
N,
int
[] query,
int
M) {
Map<Integer, Integer> Map =
new
HashMap<>();
List<Pair<Integer, Integer>> Ans =
new
ArrayList<>();
Pair<Integer, Integer> previous =
new
Pair<>(arr[
0
].getKey(),
arr[
0
].getValue());
Ans.add(previous);
Map.put(arr[
0
].getValue(),
1
);
for
(
int
i =
1
; i < N; i++) {
Map.put(arr[i].getValue(), Map.getOrDefault(arr[i].getValue(),
0
) +
1
);
if
(Map.get(arr[i].getValue()) > Map.get(previous.getValue())) {
previous =
new
Pair<>(arr[i].getKey(), arr[i].getValue());
}
Ans.add(previous);
}
for
(
int
i =
0
; i < M; i++) {
System.out.print(binarySearch(Ans,
0
, N -
1
, query[i]) +
" "
);
}
}
public
static
void
main(String[] args) {
Pair<Integer, Integer>[] arr =
new
Pair[
6
];
arr[
0
] =
new
Pair<>(
1
,
2
);
arr[
1
] =
new
Pair<>(
2
,
2
);
arr[
2
] =
new
Pair<>(
4
,
1
);
arr[
3
] =
new
Pair<>(
5
,
5
);
arr[
4
] =
new
Pair<>(
7
,
1
);
arr[
5
] =
new
Pair<>(
11
,
1
);
int
[] query = {
2
,
8
,
12
};
int
N =
6
;
int
M =
3
;
findWinner(arr, N, query, M);
}
}