#include <bits/stdc++.h>
using namespace std;
const int INF = 0x7fffffff;
const int oo = 0x3f3f3f3f;
template <typename T>
inline void rd(T &x) {
  x = 0;
  int f = 1;
  char CH = getchar();
  while (!isdigit(CH)) {
    if (CH == '-') f = 0;
    CH = getchar();
  }
  while (isdigit(CH)) {
    x = ((x + (x << 2)) << 1) + (CH - 48);
    CH = getchar();
  }
  x = (f == 1) ? x : -x;
  return;
}
const int MXN = 1e5 + 5;
int n, m, q, p;
long long a[MXN];
map<long long, long long> f;
inline long long get_phi(long long x) {
  long long ans = x, z = x;
  if (f.count(x)) return f[x];
  for (register int i = 2; i * i <= x; ++i) {
    if (x % i == 0) {
      ans -= ans / i;
      while (x % i == 0) x /= i;
    }
  }
  if (x > 1) ans -= ans / x;
  return f[z] = ans;
}
inline long long qpow(long long a, long long b, long long mod) {
  long long ret = 1, fl = a >= mod;
  for (; b; b >>= 1) {
    if (b & 1) {
      ret = ret * a;
      if (ret >= mod) fl = 1, ret %= mod;
    }
    a = a * a;
    if (a >= mod) a %= mod, fl = 1;
  }
  return ret + fl * mod;
}
inline long long solve(int l, int r, long long mod) {
  const long long Mod = mod;
  if (l == r) return a[l];
  if (Mod == 1) return 1;
  return qpow(a[l], solve(l + 1, r, get_phi(mod)), mod);
}
int main() {
  rd(n), rd(p);
  for (register int i = (1), i_end = (n); i <= i_end; ++i) rd(a[i]);
  for (rd(q); q--;) {
    int l, r;
    rd(l), rd(r);
    printf("%lld\n", solve(l, r, p) % p);
  }
  return 0;
}
