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:
import java.io.*;
import java.util.*;
public class GG {
	public static void main(String[] args)throws Throwable {
		MyScanner sc=new MyScanner();
		PrintWriter pw=new PrintWriter(System.out);
		
		sieve((int)1e5);
		int n=sc.nextInt();
		int m=sc.nextInt();
		int [] mod=new int [50];
		mod[0]=m;
		for(int i=1;i<50;i++)
			mod[i]=phi(mod[i-1]);
		int [] a=new int [n];
		for(int i=0;i<n;i++)
			a[i]=sc.nextInt();
		int q=sc.nextInt();
		while(q-->0){
			int l=sc.nextInt()-1;
			int r=sc.nextInt()-1;
			if(l==r){
				pw.println(a[l]%m);
				continue;
			}
			int start=l+32;
			int idx=start-l;
			int pre=-1;
			double val=1;
			while(idx>=0){
				if(mod[idx]==1 || start>=r){
					if(start<=r && val<1e9)
						val=Math.round(Math.pow(a[start], val));
					if(start<=r && a[start]==1)
						val=1;
					start--;
					idx--;
					continue;
				}
				if(pre==-1)
					pre=a[start+1];
				
				boolean greater=false;
				if(val>30)
					greater=true;
				else
					if(val>Math.log(mod[idx])/Math.log(2)-0.00001)
						greater=true;
				if(greater)
					pre=modPow(a[start], pre+mod[idx+1], mod[idx]);
				else
					pre=modPow(a[start], (int)Math.round(val), mod[idx]);
				if(val<1e9)
					val=Math.round(Math.pow(a[start], val));
				if(a[start]==1)
					val=1;
				start--;
				idx--;
			}
			pw.println(pre%m);
		}
		pw.flush();
		pw.close();
	}
	
	static int gcd(int a,int b){
		return b==0? a : gcd(b,a%b);
	}
	
	static int modPow(int x,int y,int m){
		if(y==1)
			return x%m;
		if(y==0)
			return 1%m;
		if(y%2==0){
			long z=modPow(x, y/2, m);
			return (int)((z*z)%m);
		}else{
			long z=modPow(x, y-1, m);
			return (int)((z*x)%m);
		}
	}
	
	static int phi(int N)
	{
		int ans = N, idx = 0, p = primes.get(0);
		while(p * p <= N)
		{
			if(N % p == 0)
				ans -= ans / p;
			while(N % p == 0)
				N /= p;
			p = primes.get(++idx);
		}
			
		if(N != 1) 
			ans -= ans / N;
		return ans;
	}
	
	static ArrayList<Integer> primes;
	static int[] isComposite;

	static void sieve(int N)	// O(N log log N) 
	{
		isComposite = new int[N+1];					
		isComposite[0] = isComposite[1] = 1;			// 0 indicates a prime number
		primes = new ArrayList<Integer>();

		for (int i = 2; i <= N; ++i) 					//can loop till i*i <= N if primes array is not needed O(N log log sqrt(N)) 
			if (isComposite[i] == 0) 					//can loop in 2 and odd integers for slightly better performance
			{
				primes.add(i);
				if(1l * i * i <= N)
					for (int j = i * i; j <= N; j += i)	// j = i * 2 will not affect performance too much, may alter in modified sieve
						isComposite[j] = 1;
			}   
	}
	
	static class MyScanner {
		BufferedReader br;
		StringTokenizer st;
		public MyScanner() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}
		String next() {while (st == null || !st.hasMoreElements()) {
			try {st = new StringTokenizer(br.readLine());}
			catch (IOException e) {e.printStackTrace();}}
		return st.nextToken();}
		int nextInt() {return Integer.parseInt(next());}
		long nextLong() {return Long.parseLong(next());}
		double nextDouble() {return Double.parseDouble(next());}
		String nextLine(){String str = "";
		try {str = br.readLine();}
		catch (IOException e) {e.printStackTrace();}
		return str;}
	}
}