#P9816. 少项式复合幂

少项式复合幂

Background

I have won everything except your heart.

Finally, Little Z can play Genshin Impact for a year. But before that, he decided to create this problem to commemorate his feelings for [data deleted].

Problem Description

Given a polynomial f(x)=i=1maixbif(x)=\sum_{i=1}^m a_i x^{b_i}. Define f1(x)=f(x)f_1(x)=f(x), and fn(x)=f(fn1(x))f_n(x)=f(f_{n-1}(x)).

Given a modulus pp. There are qq queries. Each query gives x,yx,y, and asks for the value of fy(x)modpf_y(x)\bmod p.

Please note the special Constraints for m,pm,p.

Input Format

The first line contains three positive integers m,q,pm,q,p, representing the number of terms in ff, the number of queries, and the given modulus.

Then follow mm lines, each containing two non-negative integers ai,bia_i,b_i, describing the polynomial ff.

Then follow qq lines, each containing two positive integers x,yx,y, representing one query.

Output Format

Output qq lines in total, each line containing the answer to the corresponding query.

3 5 71
1 1
3 3
1 0
7 5
9 6
10 1
5 6
7 6
27
11
29
2
5

Hint

Explanation of the Samples

In Sample 1, f(x)=3x3+x+1f(x)=3x^3+x+1. Take the 3rd query as an example: $f_1(10)=f(10)=3\times10^3+10+1=3011\equiv 29 \pmod {71}$.

Constraints and Conventions

Test Point ID yy mm qq Special Property
131\sim 3 10\le 10 20\le 20 103\le 10^3 None
474\sim 7 103\le 10^3 104\le 10^4
8,98,9 107\le 10^7 1\le 1 3×105\le 3\times 10^5 A
1010 None
11,1211,12 2\le 2 105\le 10^5 A, B
1313 B
141614\sim 16 20\le 20 500\le 500 None
172017\sim 20 3×105\le 3\times 10^5
  • Special Property A: pp is guaranteed to be a prime.
  • Special Property B: bi1b_i\le 1 is guaranteed.

For all testdata, it is guaranteed that 1m201\le m\le 20, 0ai,bi1050\le a_i,b_i\le 10^5, 2p1052\le p\le 10^5, 1q3×1051\le q\le 3\times 10^5, and 1x,y1071\le x,y\le 10^7.

Translated by ChatGPT 5