#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();
  }
}
