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 maxn = 500010, inf = 1e9, lim = 2e7;
int n, m, ty, x, y, z, cnt;
int v[maxn], b[maxn];
long long tree[maxn], a[maxn], p[maxn];
bool vis[20000010];
void read(int &k) {
  int f = 1;
  k = 0;
  char c = getchar();
  while (c < '0' || c > '9') c == '-' && (f = -1), c = getchar();
  while (c <= '9' && c >= '0') k = k * 10 + c - '0', c = getchar();
  k *= f;
}
inline long long query(int x) {
  if (v[x] == m + 1) return a[x];
  v[x] = m + 1;
  return a[x] = b[x];
}
inline int power(int a, int b, int mod) {
  if (!a) return 0;
  int ans = 1;
  for (; b; b >>= 1, a = 1ll * a * a % mod)
    if (b & 1) ans = 1ll * ans * a % mod;
  return ans;
}
int phi(int n) {
  int res = n, a = n;
  for (int i = 2; i * i <= a; i++) {
    if (a % i == 0) {
      res = res / i * (i - 1);
      while (a % i == 0) a /= i;
    }
  }
  if (a > 1) res = res / a * (a - 1);
  return res;
}
int solve(int l, int r, int mod, int dep) {
  if (mod == 1) return 1;
  long long now = query(l) % mod;
  if (!now) return 0;
  if (l == r) return now;
  int nxt = min(l + 5, r);
  for (int i = l + 1; i < nxt; i++)
    if (query(i) == 1) {
      nxt = r = i;
      break;
    }
  long long last = query(nxt), x;
  if (last >= p[dep])
    return power(now, solve(l + 1, r, p[dep], dep + 1) + p[dep], mod);
  for (int i = nxt - 1; i > l; i--) {
    long long mi = last;
    last = 1;
    x = query(i);
    for (int j = 1; j <= mi; j++) {
      last *= x;
      if (last >= p[dep])
        return power(now, solve(l + 1, r, p[dep], dep + 1) + p[dep], mod);
    }
  }
  return power(now, solve(l + 1, r, p[dep], dep + 1), mod);
}
int main() {
  read(n);
  read(z);
  int cnttt = 0;
  p[0] = z;
  while (p[cnttt] - 1) p[++cnttt] = phi(p[cnttt - 1]);
  p[++cnttt] = 1;
  for (int i = 1; i <= n; i++) read(b[i]);
  read(m);
  while (m--) {
    read(x);
    read(y);
    printf("%d\n", solve(x, y, z, 1) % z);
  }
  return 0;
}
