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 CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
long long n, i, j, k, s, t, mod, top, num, T, l, r, ans;
long long phi[20000][2], prime[20000], a[100010], b[100010];
bool isprime[32000];
long long ksm(long long x, long long y, long long mod) {
  long long temp = 1;
  while (y > 0) {
    if (y % 2 == 1) temp = temp * x % mod;
    x = x * x % mod;
    y = y / 2;
  }
  return temp;
}
long long getphi(long long x) {
  long long temp = x;
  for (int i = 1; i <= num; i++) {
    if (prime[i] * prime[i] > x) break;
    if (x % prime[i] == 0) {
      temp = temp / prime[i] * (prime[i] - 1);
      while (x % prime[i] == 0) x = x / prime[i];
    }
  }
  if (x > 1) {
    temp = temp / x * (x - 1);
  }
  return temp;
}
long long check(long long l, long long r, long long x) {
  long long temp = a[r], s, t;
  if (temp > x) return -1;
  for (int i = r - 1; i >= l; i--) {
    s = a[i];
    t = temp;
    temp = 1;
    while (t > 0) {
      if (t % 2 == 1) temp = temp * s;
      if (temp > x) return -1;
      s = s * s;
      t = t / 2;
      if (t > 0 && s > x) return -1;
    }
  }
  return temp;
}
long long work(long long l, long long r, long long x) {
  long long mod = phi[x][0], t = phi[x][1];
  if (l == r) return a[l] % mod;
  if (mod == 1) return 0;
  long long s = check(l + 1, r, t);
  if (s < 0) {
    s = work(l + 1, r, x + 1) + t;
    return ksm(a[l], s, mod);
  } else
    return ksm(a[l], s, mod);
}
int main() {
  num = 0;
  t = int(sqrt(1000000000));
  for (i = 2; i <= t; i++) {
    if (!isprime[i]) {
      num++;
      prime[num] = i;
    }
    for (j = 1; j <= num; j++) {
      s = i * prime[j];
      if (s > t) break;
      isprime[s] = 1;
      if (i % prime[j] == 0) break;
    }
  }
  scanf("%lld%lld", &n, &mod);
  top = 1;
  phi[1][0] = mod;
  while (true) {
    t = getphi(phi[top][0]);
    phi[top][1] = t;
    if (t == 1)
      break;
    else {
      top++;
      phi[top][0] = t;
    }
  }
  top++;
  phi[top][0] = phi[top][1] = 1;
  for (i = 1; i <= n; i++) scanf("%lld", &a[i]);
  t = n + 1;
  for (i = n; i >= 1; i--) {
    if (a[i] == 1) t = i;
    b[i] = t;
  }
  scanf("%lld", &T);
  while (T--) {
    scanf("%lld%lld", &l, &r);
    t = b[l];
    if (t < r) r = t;
    ans = work(l, r, 1);
    printf("%lld\n", ans);
  }
}