Under construction

Still being built. Things may change

Touring Show

Time limit1sMemory limit512 MB

Summary
For each range [l, r], find the fewest triangles k such that 1 + k*A_i*A_j is a multiple of a for some pair i<j in the range, or -1.
Level

Medium6 of 10

Topics
Number theory, Array, Prefix sum
Solved
No attempts yet

Problem

In 2033, Siu became a master of drawing two different shapes at the same time with both hands, after ten years of training. To celebrate the tenth anniversary of his training, Siu plans a tour with QQ venues. To make each show different, he takes a sequence AA of length NN, and for each show he picks two indices ii and jj with l≤i<j≤rl \le i < j \le r from the given interval [l,r][l, r]. The show goes as follows.

  • The left hand repeatedly draws a circle that takes aa seconds to complete.
  • The right hand first waves for 1 second, then repeatedly draws a triangle that takes Ai×AjA_i \times A_j seconds to complete.
  • There is no pause between completing a shape and starting the next one. The show ends at the moment both shapes complete at exactly the same time.

Drawing triangles is very tiring, so Siu wants to draw as few triangles as possible. However, it is impossible to finish the show without drawing any triangle.

Input

The first line contains integers NN, QQ, and aa in that order. (1≤N,Q≤1051 \le N, Q \le 10^5, 1≤a≤301 \le a \le 30)

The second line contains the NN integers of sequence AA, separated by spaces, in order. (1≤Ai≤1091 \le A_i \le 10^9)

From the third line, each of the next QQ lines contains two integers ll and rr, separated by a space. (1≤l<r≤N1 \le l < r \le N)

Output

For each show, print on its own line the minimum number of triangles drawn. If the show cannot be finished in finite time by any method, print −1-1.

Examples2

  1. Example 1

    Input
    5 3 7
    6 4 5 5 6
    4 5
    2 4
    1 4
    
    Expected output
    3
    1
    1
    
  2. Example 2

    Input
    3 2 6
    2 5 1
    1 2
    2 3
    
    Expected output
    -1
    1