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>
#pragma GCC optimize("O3")
using namespace std;
inline long long getint() {
  long long _x = 0, _tmp = 1;
  char _tc = getchar();
  while ((_tc < '0' || _tc > '9') && _tc != '-') _tc = getchar();
  if (_tc == '-') _tc = getchar(), _tmp = -1;
  while (_tc >= '0' && _tc <= '9') _x *= 10, _x += (_tc - '0'), _tc = getchar();
  return _x * _tmp;
}
inline long long add(long long _x, long long _y,
                     long long _mod = 1000000007LL) {
  _x += _y;
  return _x >= _mod ? _x - _mod : _x;
}
inline long long sub(long long _x, long long _y,
                     long long _mod = 1000000007LL) {
  _x -= _y;
  return _x < 0 ? _x + _mod : _x;
}
inline long long mul(long long _x, long long _y,
                     long long _mod = 1000000007LL) {
  _x *= _y;
  return _x >= _mod ? _x % _mod : _x;
}
long long mypow(long long _a, long long _x, long long _mod) {
  if (_x == 0) return 1LL;
  long long _ret = mypow(mul(_a, _a, _mod), _x >> 1, _mod);
  if (_x & 1) _ret = mul(_ret, _a, _mod);
  return _ret;
}
long long mymul(long long _a, long long _x, long long _mod) {
  if (_x == 0) return 0LL;
  long long _ret = mymul(add(_a, _a, _mod), _x >> 1, _mod);
  if (_x & 1) _ret = add(_ret, _a, _mod);
  return _ret;
}
inline bool equal(double _x, double _y) {
  return _x > _y - 1e-9 && _x < _y + 1e-9;
}
void sleep(double sec = 1021) {
  clock_t s = clock();
  while (clock() - s < CLOCKS_PER_SEC * sec)
    ;
}
int __ = 1, _cs;
const long long inf = 2000000000;
bool p[101010];
vector<int> pri;
long long sv_pw[30][30];
void build() {
  for (int i = 2; i < 101010; i++)
    if (!p[i]) {
      pri.push_back(i);
      for (int j = i; j < 101010; j += i) p[j] = true;
    }
  for (int i = 2; i < 30; i++) {
    sv_pw[i][0] = 1;
    for (int j = 1; j < 30; j++) {
      sv_pw[i][j] = min(sv_pw[i][j - 1] * i, inf);
    }
  }
}
inline int phi(int x) {
  int ret = x;
  for (int i : pri) {
    if ((long long)i * i > x) break;
    if (x % i) continue;
    ret = (ret / i) * (i - 1);
    while (x % i == 0) x /= i;
  }
  if (x > 1) ret = (ret / x) * (x - 1);
  return ret;
}
int n, m, q;
int w[101010];
vector<int> mod;
void gogo() {
  mod.push_back(m);
  while (mod.back() > 1) mod.push_back(phi(mod.back()));
}
void init() {
  n = getint();
  m = getint();
  for (int i = 1; i <= n; i++) w[i] = getint();
  gogo();
}
inline long long magic(long long base, long long pw) {
  if (base == 1) return 1;
  if (pw >= 30) return inf;
  if (base < 30) return sv_pw[base][pw];
  long long ret = 1;
  while (pw--) {
    ret = min(ret * base, inf);
    if (ret == inf) return ret;
  }
  return ret;
}
inline long long cal2(int li, int ri, int cnt) {
  if (li == ri) return w[li];
  if (w[li] == 1) return 1;
  if (cnt >= 30) return inf;
  long long ret = cal2(li + 1, ri, cnt + 1);
  return magic(w[li], ret);
}
inline pair<int, long long> cal(int li, int ri, int pmod) {
  if (li == ri) return {w[li] % mod[pmod], w[li]};
  if (mod[pmod] == 1) {
    long long vl = cal2(li, ri, 0);
    return {0, vl};
  }
  pair<int, long long> ret = cal(li + 1, ri, pmod + 1);
  long long pww = ret.first;
  if (ret.second >= mod[pmod + 1]) pww += mod[pmod + 1];
  int vret = mypow(w[li], pww, mod[pmod]);
  return {vret, magic(w[li], ret.second)};
}
void solve() {
  q = getint();
  while (q--) {
    int ql = getint();
    int qr = getint();
    printf("%d\n", cal(ql, qr, 0).first);
  }
}
int main() {
  build();
  while (__--) {
    init();
    solve();
  }
}