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
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
const int INF = 0x3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-10;
int dcmp(double first) {
  if (fabs(first) < eps) return 0;
  return first < 0 ? -1 : 1;
}
long long mit;
long long a[N], b[N], ph[N];
int l, r, ot;
long long qpowt(long long a, long long n) {
  if (a > mit) return -1;
  long long ans = 1;
  int f = 1;
  while (n) {
    if (n & 1) {
      if (!f) return -1;
      ans *= a;
      if (ans > mit) return -1;
    }
    a *= a;
    if (a > mit) f = 0;
    n >>= 1;
  }
  return ans;
}
long long phe(long long n) {
  long long sum = n;
  long long m = sqrt(n + 0.5);
  for (long long i = 2; i <= m; i++) {
    if (!(n % i)) {
      sum = sum * (i - 1) / i;
      while (!(n % i)) n /= i;
    }
  }
  if (n > 1) sum = sum / n * (n - 1);
  return sum;
}
long long qpow(long long a, long long n, long long M) {
  a %= M;
  long long ans = 1;
  while (n) {
    if (n & 1) ans = (ans * a) % M;
    a = (a * a) % M;
    n >>= 1;
  }
  return ans;
}
long long f(long long pos) {
  long long pm = ph[pos - l + 1], ans;
  if (pm == 1 || pm == 0) {
    ans = 1;
  } else if (pos + 1 >= ot) {
    if (b[pos + 1] <= pm)
      ans = b[pos + 1];
    else
      ans = b[pos + 1] % pm + pm;
  } else
    ans = f(pos + 1) + pm;
  return qpow(a[pos], ans, ph[pos - l]);
}
int main() {
  int n, Q, cnt = 0;
  scanf("%d %lld", &n, &mit);
  vector<int> tt;
  for (int i = 0; i < n; i++) {
    scanf("%lld", &a[i]);
    if (a[i] == 1) tt.push_back(i);
  }
  ph[0] = mit;
  int n1 = 0;
  while (ph[n1] != 1) {
    n1++;
    ph[n1] = phe(ph[n1 - 1]);
  }
  scanf("%d", &Q);
  while (Q--) {
    cnt++;
    scanf("%d %d", &l, &r);
    l--;
    r--;
    int fq = lower_bound(tt.begin(), tt.end(), l) - tt.begin();
    if (fq != tt.size()) r = min(r, tt[fq]);
    ot = r;
    b[r] = a[r];
    for (int i = r - 1; i >= l; i--) {
      b[i] = qpowt(a[i], b[i + 1]);
      if (b[i] != -1)
        ot = i;
      else
        break;
    }
    if (l == r)
      printf("%lld\n", a[l] % mit);
    else
      printf("%lld\n", f(l));
  }
  return 0;
}
