Problem: Priests of the Quetzalcoatl cult want to build a tower to represent a power of their god. Tower is usually made of power-charged rocks. It is built with the help of rare magic by levitating the current top of tower and adding rocks at its bottom. If top, which is built from k - 1 rocks, possesses power p and we want to add the rock charged with power wk then value of power of a new tower will be {wk}p. 

Rocks are added from the last to the first. That is for sequence w1, ..., wm value of power will be

<image>

After tower is built, its power may be extremely large. But still priests want to get some information about it, namely they want to know a number called cumulative power which is the true value of power taken modulo m. Priests have n rocks numbered from 1 to n. They ask you to calculate which value of cumulative power will the tower possess if they will build it from rocks numbered l, l + 1, ..., r. 

Input

First line of input contains two integers n (1 ≤ n ≤ 105) and m (1 ≤ m ≤ 109).

Second line of input contains n integers wk (1 ≤ wk ≤ 109) which is the power of rocks that priests have.

Third line of input contains single integer q (1 ≤ q ≤ 105) which is amount of queries from priests to you.

kth of next q lines contains two integers lk and rk (1 ≤ lk ≤ rk ≤ n). 

Output

Output q integers. k-th of them must be the amount of cumulative power the tower will have if is built from rocks lk, lk + 1, ..., rk.

Example

Input

6 1000000000
1 2 2 3 3 3
8
1 1
1 6
2 2
2 3
2 4
4 4
4 5
4 6


Output

1
1
2
4
256
3
27
597484987

Note

327 = 7625597484987

Solution (in JAVA):

Solution:
// package EulerTotient;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class PowerTower {
    public static void main(String[] args)throws IOException {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        int seive[]=new int[100001];
        ArrayList<Integer> primes=new ArrayList<>();
        for(int i=2;i<seive.length;i++){
            if(seive[i]==0){
                primes.add(i);
                for(int j=2*i;j<seive.length;j+=i){
                    seive[j]=1;
                }
            }
        }
        String line[]=br.readLine().split(" ");
        int n=Integer.parseInt(line[0]);
        int mod=Integer.parseInt(line[1]);
        int store[]=new int[50];
        store[0]=mod;
        for(int i=1;i<50;i++){
            store[i]=phi(store[i-1],primes);
//            System.out.println(store[i]);
        }
//        for(int i=0;i<50;i++){
//            System.out.print(store[i]+" ");
//        }
//        System.out.println();
//        System.out.println();
        StringBuilder print=new StringBuilder();
        int a[]=new int[n+1];
        line=br.readLine().split(" ");
        for(int i=1;i<=n;i++){
            a[i]=Integer.parseInt(line[i-1]);
        }
        int q=Integer.parseInt(br.readLine());
        while(q--!=0){
            line=br.readLine().split(" ");
            int l=Integer.parseInt(line[0]);
            int r=Integer.parseInt(line[1]);
            long ans=calculate(l,r,a,store,0);
            print.append(ans%store[0]+"\n");
        }
        System.out.print(print.toString());
    }
    public static long calculate(int l,int r,int a[],int store[],int ind){
        if(l==(r+1)){
            return 1;
        }
        if(store[ind]==1){
            return a[l];
        }
        long ahead=calculate(l+1,r,a,store,ind+1);
//        System.out.println(ahead);
        return modexp(a[l],ahead,store[ind]);
    }
    public static int phi(int num,ArrayList<Integer> primes){
        int ans=num;
        for(int p:primes){
            if(num==1){
                break;
            }
            if(num%p==0){
                ans=ans-ans/p;
                while(num%p==0){
                    num/=p;
                }
            }
        }
        if(num!=1){
            ans=ans-ans/num;
        }
        return ans;
    }
    public static long modexp(long base,long pow,long mod){
        long ans=1;
        while (pow > 0) {
            if ((pow & 1) != 0) {
                ans = ans * base;
                if (ans >= mod) {
                    ans = ans % mod + mod;
                }
            }
            base = base * base;
            if (base >= mod) {
                base = base % mod + mod;
            }
            pow >>= 1;
        }
        return ans;
    }
    
}