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 int mod = 1e9 + 7;
double eps = 1e-9;
long long int exp(long long int x, long long int y) {
  if (y < 0) return 0;
  long long int ret = 1;
  for (; y; y >>= 1, x = (x * x) % mod) {
    if (y & 1) ret = (ret * x) % mod;
  }
  return ret;
}
long long int pexp(long long int x, long long int y) {
  if (y < 0) return 0;
  long long int ret = 1;
  for (; y; y >>= 1, x = (x * x)) {
    if (y & 1) ret = (ret * x);
  }
  return ret;
}
long long int gcd(long long int x, long long int y) {
  if (!x || !y) return x + y;
  return x % y == 0 ? y : gcd(y, x % y);
}
long long int lcm(long long int x, long long int y) {
  return x * (y / gcd(x, y));
}
long long int bsum(long long int u, long long int b) {
  long long int ret = 0;
  if (u < b) return u;
  while (u) {
    ret += u % b;
    u /= b;
  }
  return ret;
}
long long int prival(long long int u, long long int p) {
  long long int cn = 0;
  while (u % p == 0) {
    cn++;
    u = u / p;
  }
  return cn;
}
long long int minv(long long int a, long long int b) {
  return 1 < a ? b - minv(b % a, a) * b / a : 1;
}
long long int extm(long long int a, long long int b) {
  long long int ret = 0;
  while (a != 0) {
    if (a % 2 == 1) {
      ret += b;
      ret %= mod;
    }
    a >>= 1;
    b = (2 * b) % mod;
  }
  return ret;
}
long long int eaphi(long long int x) {
  long long int t = x, ret = x, i;
  for (i = 2; i * i <= x; i++) {
    if (t % i == 0) {
      ret -= ret / i;
      while (t % i == 0) t /= i;
    }
  }
  if (t != 1) ret -= ret / t;
  return ret;
}
long long int eadivc(long long int x) {
  long long int ret = 0;
  long long int i;
  for (i = 1; i * i <= x; i++) {
    if (x % i == 0 && i * i != x) ret += 2;
    if (x % i == 0 && i * i == x) ret += 1;
  }
  return ret;
}
long long int ndig(long long int x, long long int b) {
  long long int ret = 0;
  while (x) {
    x /= b;
    ret++;
  }
  return ret;
}
bool isprime(long long int x) {
  if (x <= 1) return false;
  for (long long int i = 2; i * i <= x; i++) {
    if (x % i == 0) {
      return false;
    }
  }
  return true;
}
int n, q, l, r;
long long int m;
long long int vv[111111];
long long int dep[311];
long long int constrainexp(long long int u, long long int p) {
  if (u == 1) return 1;
  if (u >= 2 && p >= 6) return 50;
  if (u >= 3 && p >= 4) return 50;
  if (u >= 4 && p >= 3) return 50;
  if (u >= 8 && p >= 2) return 50;
  mod = 1e18;
  long long int t = exp(u, p);
  if (t >= 50) return 50;
  return t;
}
long long int det(int lef, int rig) {
  int i, cc = -1;
  if (lef == rig) return min(vv[lef], 50LL);
  for (i = lef; i <= min(rig, lef + 3); i++) {
    if (vv[i] == 1) {
      cc = i;
      break;
    }
  }
  if (cc == -1) {
    if (rig >= lef + 3) return 50;
    if (rig == lef + 2) {
      if (vv[lef] >= 3 || vv[lef + 1] >= 3 || vv[lef + 2] >= 3)
        return 50;
      else
        return 16;
    }
    if (rig == lef + 1) {
      return constrainexp(vv[lef], vv[lef + 1]);
    }
  }
  if (cc - 1 == lef + 2) {
    if (vv[lef] >= 3 || vv[lef + 1] >= 3 || vv[lef + 2] >= 3)
      return 50;
    else
      return 16;
  }
  if (cc - 1 == lef + 1) {
    return constrainexp(vv[lef], vv[lef + 1]);
  }
  if (cc - 1 == lef) {
    return min(vv[lef], 50LL);
  }
  if (cc == lef) return 1;
}
long long int gv(int lef, int rig, int dp) {
  if (dp >= 40) return 0;
  if (m == 1) return 0;
  if (vv[lef] == 1) return 1 % dep[dp];
  if (lef == rig) return vv[lef] % dep[dp];
  long long int isLarge = det(lef + 1, rig);
  if (isLarge >= 50) {
    long long int T = gv(lef + 1, rig, dp + 1);
    mod = dep[dp];
    return exp(vv[lef], T + 50LL * dep[dp + 1]) % dep[dp];
  } else {
    mod = dep[dp];
    return exp(vv[lef], isLarge) % dep[dp];
  }
}
int main(void) {
  cin >> n >> m;
  int i;
  dep[0] = m;
  for (i = 1; i <= 50; i++) dep[i] = eaphi(dep[i - 1]);
  for (i = 1; i <= n; i++) scanf("%lld", &vv[i]);
  cin >> q;
  while (q--) {
    scanf("%d %d", &l, &r);
    printf("%lld\n", gv(l, r, 0));
  }
  return 0;
}