#include <bits/stdc++.h>
using namespace std;
int n, l, r;
long long p, a[100005], ph[100005];
inline long long read() {
  long long s = 0;
  char c = getchar();
  while (!isdigit(c)) c = getchar();
  while (isdigit(c)) s = (s << 3) + (s << 1) + (c ^ 48), c = getchar();
  return s;
}
long long phi(long long p) {
  long long x = p, rt = p;
  for (register int i = 2; i * i <= p; i++)
    if (x % i == 0) {
      rt = rt / i * (i - 1);
      while (x % i == 0) x /= i;
    }
  if (x > 1) rt = rt / x * (x - 1);
  return rt;
}
long long MOD(long long x, long long mod) {
  return x >= mod ? (x % mod + mod) : x;
}
long long ksm(long long a, long long k, long long p) {
  long long rt = 1;
  while (k) {
    if (k & 1) rt = MOD(rt * a, p);
    a = MOD(a * a, p);
    k >>= 1;
  }
  return rt;
}
long long solve(long long st, long long ed, long long p) {
  if (ph[p] == 1 || st == ed) return MOD(a[st], ph[p]);
  return ksm(a[st], solve(st + 1, ed, p + 1), ph[p]);
}
int main() {
  n = read();
  ph[0] = read();
  for (register int i = 1; i <= n; i++) a[i] = read();
  for (register int i = 1; i <= n; i++) ph[i] = phi(ph[i - 1]);
  int q = read();
  while (q--) {
    l = read(), r = read();
    if (l == r)
      printf("%lld\n", a[l] % ph[0]);
    else
      printf("%lld\n", solve(l, r, 0) % ph[0]);
  }
  return 0;
}
