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 = 1e5 + 5;
int n, q, l, r, a[N];
long long w[N], m;
vector<long long> vec;
long long fastexp(long long a, long long b, long long m) {
  if (m == 1) return 0;
  if (b == 0) return 1;
  long long x = fastexp(a, b >> 1, m);
  x = (x * x) % m;
  if (b & 1) x = (x * a) % m;
  return x;
}
long long phi(long long x) {
  long long y = x, ans = x;
  for (long long i = 2; i * i <= y; i++) {
    if (x % i == 0) {
      ans /= i;
      ans *= (i - 1);
    }
    while (x % i == 0) x /= i;
  }
  if (x > 1) {
    ans /= x;
    ans *= (x - 1);
  }
  return ans;
}
long long expo(long long a, long long b) {
  if (a < 2) return a;
  long long ans = 1;
  while (b) {
    if (b & 1) ans *= a;
    b >>= 1;
    a = a * a;
  }
  return ans;
}
long long evaluate(int ind) {
  if (ind > r) return 1;
  int l = r;
  for (int i = ind; i <= r; i++) {
    if (w[i] == 1) {
      l = i;
      break;
    }
  }
  long long ans = 1;
  int i = l;
  while (i >= ind) {
    ans = expo(w[i], ans);
    i--;
  }
  return ans;
}
long long solve(int ind, long long m, int d) {
  if (m == 1) return 0;
  if (ind > r) return 1;
  if (a[ind + 1] > r) return fastexp(w[ind], evaluate(ind + 1), m);
  long long ph = vec[d];
  long long y = solve(ind + 1, ph, d + 1);
  long long ans = fastexp(w[ind], ph + y, m);
  return ans;
}
int find(int ind, int k) {
  if (ind > n) return ind;
  if (w[ind] >= k) return ind;
  if (w[ind] == 1) return (n + 1);
  double x = log(1.0 * k), y = log(1.0 * w[ind]);
  double z = x / y;
  k = ceil(z);
  return find(ind + 1, k);
}
int main() {
  scanf("%d%lld", &n, &m);
  long long M = m;
  while (m > 1) {
    m = phi(m);
    vec.push_back(m);
  }
  for (int i = 0; i < 5; i++) vec.push_back(1);
  m = M;
  for (int i = 1; i <= n; i++) {
    scanf("%lld", w + i);
  }
  for (int i = 1; i <= n + 1; i++) {
    a[i] = find(i, ceil(log2(1.0 * m)));
  }
  scanf("%d", &q);
  for (int i = 1; i <= q; i++) {
    scanf("%d%d", &l, &r);
    long long ans = solve(l, m, 0);
    printf("%lld\n", ans);
  }
}
