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 = 100005;
const int P = 1e9 + 7;
const int inf = 1e9;
const long long Inf = 1e18;
inline int IN() {
  char ch = getchar();
  int x = 0, f = 0;
  while (ch < '0' || ch > '9') ch = getchar(), f = (ch == '-');
  while (ch >= '0' && ch <= '9') {
    x = (x << 1) + (x << 3) + ch - 48;
    ch = getchar();
  }
  return f ? (-x) : x;
}
int n, m, a[N];
inline long long Pow(long long x, long long y) {
  long long an = 1;
  for (; y; y >>= 1, x = min(x * x, m + 1LL))
    if (y & 1) an = min(an * x, m + 1LL);
  return an;
}
inline int Pow(int x, int y, int p) {
  int an = 1;
  for (; y; y >>= 1, x = (long long)x * x % p)
    if (y & 1) an = (long long)an * x % p;
  return an;
}
void renew(int &x, int y) {
  x += y;
  if (x < 0)
    x += P;
  else if (x >= P)
    x -= P;
}
void setIO(string a) {
  freopen((a + ".in").c_str(), "r", stdin);
  freopen((a + ".out").c_str(), "w", stdout);
}
template <typename T>
inline void chkmin(T &a, const T &b) {
  if (a > b) a = b;
}
template <typename T>
inline void chkmax(T &a, const T &b) {
  if (a < b) a = b;
}
int gtphi[10005];
int Gt[N][8], nxt[N];
int dp[N][35];
int getphi(int x) {
  int ans = x;
  for (int t = 2; t * t <= x; t++)
    if (x % t == 0) {
      ans = ans - ans / t;
      while (x % t == 0) x /= t;
    }
  if (x > 1) ans = ans - ans / x;
  return ans;
}
int Ans(int l, int r, int t) {
  if (l > r || gtphi[t] == 1) return 0;
  if (l == r) return a[l] % gtphi[t];
  if (a[l] == 1) return 1;
  int nxtr = min(nxt[l], r);
  if (nxtr > l + 6) {
    int gtw = Ans(l + 1, r, t + 1);
    return Pow(a[l], gtw + gtphi[t + 1], gtphi[t]);
  } else {
    int xx = Gt[l + 1][nxtr - (l + 1)];
    if (xx <= gtphi[t]) {
      return Pow(a[l], xx, gtphi[t]);
    } else {
      int gtw = Ans(l + 1, r, t + 1);
      return Pow(a[l], gtw + gtphi[t + 1], gtphi[t]);
    }
  }
}
int main() {
  scanf("%d%d", &n, &m);
  int now = n + 1;
  gtphi[1] = m;
  for (int i = (int)1; i <= (int)1000; i++) {
    gtphi[i + 1] = getphi(gtphi[i]);
    if (!gtphi[i + 1]) break;
  }
  for (int i = (int)1; i <= (int)n; i++) scanf("%d", a + i);
  for (int i = (int)n; i >= (int)1; i--) {
    nxt[i] = now;
    if (a[i] == 1) now = i;
  }
  for (int i = (int)n; i >= (int)1; i--) {
    Gt[i][0] = a[i];
    for (int j = (int)1; j <= (int)6; j++) {
      Gt[i][j] = Pow(a[i], Gt[i + 1][j - 1]);
    }
  }
  int q;
  scanf("%d", &q);
  while (q--) {
    int l, r;
    scanf("%d%d", &l, &r);
    printf("%d\n", Ans(l, r, 1));
  }
  return 0;
}
