#include <bits/stdc++.h>
using namespace std;
long long n, m, p;
long long w[1000005];
long long get_phi(long long n) {
  long long res = n;
  for (long long i = 2; i * i <= n; ++i) {
    if (n % i) continue;
    while (n % i == 0) n /= i;
    res = res * (i - 1) / i;
  }
  if (n > 1) res = res / n * (n - 1);
  return res;
}
long long phi[105];
void init() {
  phi[0] = p;
  for (int i = 1; i <= 50; ++i) {
    phi[i] = get_phi(phi[i - 1]);
  }
}
long long Mod(long long x, long long mod) {
  if (x <= mod)
    return x;
  else
    return x % mod + mod;
}
long long qpow(long long a, long long n, long long mod) {
  long long res = 1;
  while (n) {
    if (n & 1) res = Mod(res * a, mod);
    a = Mod(a * a, mod);
    n >>= 1;
  }
  return res;
}
long long solve(long long l, long long r, long long st) {
  if (l == r || phi[st] == 1) {
    return Mod(w[l], phi[st]);
  }
  return qpow(w[l], solve(l + 1, r, st + 1), phi[st]);
}
int main() {
  cin >> n >> p;
  init();
  for (int i = 1; i <= n; ++i) {
    scanf("%lld", &w[i]);
  }
  cin >> m;
  int l, r;
  for (int i = 1; i <= m; ++i) {
    scanf("%d%d", &l, &r);
    printf("%lld\n", solve(l, r, 0) % p);
  }
  return 0;
}
