Under construction

Still being built. Things may change

Sequence and Queries HY

Interview

Time limit2sMemory limit1024 MB

Summary
Given a sequence A of length N, answer each query m by printing the minimum and maximum of the remainders A_i mod m, with m up to 300000.
Level

Medium5 of 10

Topics
Array, Math, Number theory
Solved
No attempts yet

Problem

Given a sequence A1A_1, A2A_2, ⋯\cdots, ANA_N of length NN, write a program that processes the following query.

  • mm: Print the minimum and maximum values among A1 mod mA_1 \bmod m, A2 mod mA_2 \bmod m, ⋯\cdots, AN mod mA_N \bmod m. (1≤m≤300 000)(1 \le m \le 300\,000)

Input

The first line contains NN and QQ, the length of the sequence and the number of queries, separated by a space. (1≤N,Q≤300 000)(1 \le N, Q \le 300\,000)

The second line contains NN integers A1A_1, A2A_2, ⋯\cdots, ANA_N separated by spaces. (0≤Ai≤300 000)(0 \le A_i \le 300\,000)

From the third line, QQ lines follow, each containing one query described above.

Output

For each query, print the minimum and maximum values, separated by a space, on its own line.

Examples1

  1. Example 1

    Input
    10 6
    9 55 2 4 26 37 81 16 34 46
    1
    5
    6
    7
    9
    12
    
    Expected output
    0 0
    0 4
    1 4
    2 6
    0 8
    1 10