#BST0000016. Nhiều truy vấn successor và predecessor (Multiple Predecessor and Successor Queries)

Nhiều truy vấn successor và predecessor (Multiple Predecessor and Successor Queries)

Multiple Predecessor and Successor Queries

Source: Phuoc Hung OJ

Version: Phuoc Hung OJ Extended

Problem Statement

Build a BST from a1,a2,…,ana_1,a_2,\ldots,a_n in insertion order. If a key appears more than once, only its first occurrence creates a node; later duplicates are ignored.

For each query value xx, which does not have to exist in the tree, determine two strict neighboring keys by value:

  • the predecessor is the largest stored key pp such that p<xp<x;
  • the successor is the smallest stored key ss such that s>xs>x.

If one side has no qualifying key, its result is NONE.

Input

The first line contains nn.

If n>0n>0, the next line contains nn keys a1,a2,…,ana_1,a_2,\ldots,a_n. If n=0n=0, this line is absent.

The next line contains qq — the number of queries.

Each of the next qq lines contains one integer xx.

Output

For each query, print one line in the exact order predecessor successor.

Print NONE in the first field if the predecessor does not exist, and NONE in the second field if the successor does not exist.

Subtasks

Subtask 1 (20 points): 0≤n≤200\le n\le 20, 1≤q≤201\le q\le 20.

Subtask 2 (30 points): 0≤n≤50000\le n\le 5000, 1≤q≤50001\le q\le 5000.

Subtask 3 (50 points): 0≤n≤2000000\le n\le 200000, 1≤q≤2000001\le q\le 200000.

In all subtasks, every stored key and query value is a signed 64-bit integer.

Examples

Input

7
8 3 10 1 6 14 4
3
6
1
9

Output

4 8
NONE 3
8 10

Explanation

For x=6x=6, the largest key below 66 is 44 and the smallest key above 66 is 88.

For x=1x=1, no key is smaller than 11, while the successor is 33, so the result is NONE 3.

For x=9x=9, the predecessor is 88 and the successor is 1010.