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;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
  cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
  const char* comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
inline int add(int x, int y, int mod) {
  x += y;
  if (x >= mod) x -= mod;
  return x;
}
inline int sub(int x, int y, int mod) {
  x -= y;
  if (x < 0) x += mod;
  return x;
}
inline int mul(int x, int y, int mod) { return (x * 1ll * y) % mod; }
inline int powr(int a, long long b, int mod) {
  assert(b >= 0);
  int x = 1 % mod;
  while (b) {
    if (b & 1) x = mul(x, a, mod);
    a = mul(a, a, mod);
    b >>= 1;
  }
  return x;
}
inline long long power(int a, long long b) {
  int x = 1;
  while (b) {
    if (b & 1) x *= a;
    a *= a;
    b >>= 1;
  }
  return x;
}
map<int, int> store;
int phi(int m) {
  if (store.find(m) != store.end()) return store[m];
  int ret = m;
  for (int p = 2; p * p <= m; p++) {
    if (m % p == 0) {
      ret /= p;
      ret *= p - 1;
      while (m % p == 0) m /= p;
    }
  }
  if (m != 1) {
    ret /= m;
    ret *= m - 1;
  }
  return store[m] = ret;
}
const int M = 100005;
int w[M];
bool isBig(int l, int r, double x) {
  if (x < 0) return 1;
  if (l == r) return w[l] >= x;
  if (w[l] == 1) return 1 >= x;
  return isBig(l + 1, r, log(x) / log(w[l]));
}
inline int getTotal(int l, int r) {
  if (w[l] == 1) return 1;
  if (l == r) return w[l];
  return power(w[l], getTotal(l + 1, r));
}
int get(int l, int r, int m) {
  if (m == 1) return 0;
  if (w[l] == 1) return 1 % m;
  if (l == r) return w[l] % m;
  if (isBig(l + 1, r, log(m) / log(2) + 1)) {
    int phim = phi(m);
    return powr(w[l], get(l + 1, r, phim) + phim, m);
  }
  return powr(w[l], getTotal(l + 1, r), m);
}
int main() {
  int n = 100000, m = 1000000000;
  scanf("%d", &(n));
  scanf("%d", &(m));
  for (int i = 1; i <= n; i++) {
    scanf("%d", &(w[i]));
  }
  int q = 100000;
  scanf("%d", &(q));
  for (int i = 1; i <= q; i++) {
    int l, r;
    scanf("%d", &(l));
    scanf("%d", &(r));
    printf("%d\n", get(l, r, m));
  }
}
