#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
long long calc(long long x) {
  long long i, tmp = x, n = x;
  for (i = 2; i * i <= n; i++) {
    if (x % i != 0) continue;
    tmp -= tmp / i;
    while (x % i == 0) x /= i;
  }
  if (x != 1) tmp -= tmp / x;
  return tmp;
}
long long ksm(long long a, long long x, long long p) {
  long long ans = 1;
  while (x) {
    if (x & 1) ans *= a, ans = (ans >= p ? ans % p + p : ans);
    a *= a, a = (a >= p ? a % p + p : a), x >>= 1;
  }
  return ans;
}
long long l, r, phi[N], a[N], n, q, mod;
long long ff(long long x, long long p) {
  if (x == r + 1 || p == 1) return 1;
  long long t = phi[x - l + 1];
  return ksm(a[x], ff(x + 1, t), p);
}
int main() {
  long long i, j, tmp;
  scanf("%lld%lld", &n, &mod);
  for (i = 1; i <= n; i++) scanf("%lld", &a[i]);
  tmp = mod;
  for (i = 1; tmp != 1; i++) tmp = phi[i] = calc(tmp);
  scanf("%lld", &q);
  while (q--) {
    scanf("%lld%lld", &l, &r);
    printf("%lld\n", ff(l, mod) % mod);
  }
  return ~~(0 - 0);
}
