#include <bits/stdc++.h>
const int maxn = 1e5 + 5;
const double EPS = 1e-6;
using namespace std;
map<long long, long long> mp;
long long a[maxn];
int mod(long long x, int p) { return x >= p ? x % p + p : x; }
long long kpow(long long a, long long b, long long p) {
  long long ret = 1;
  while (b) {
    if (b & 1) ret = mod(ret * a, p);
    b >>= 1;
    a = mod(a * a, p);
  }
  return ret;
}
long long phi(long long k) {
  long long s = k, kk = k;
  if (mp.count(k)) return mp[k];
  for (long long i = 2; i * i <= k; i++) {
    if (k % i == 0) s = s / i * (i - 1);
    while (k % i == 0) k /= i;
  }
  if (k > 1) s = s / k * (k - 1);
  return mp[kk] = s;
}
long long solve(int l, int r, long long p) {
  if (l == r || p == 1) return mod(a[l], p);
  return kpow(a[l], solve(l + 1, r, phi(p)), p);
}
int main() {
  int n, q;
  long long p;
  scanf("%d%lld", &n, &p);
  for (int i = 1; i < (n + 1); ++i) scanf("%lld", &a[i]);
  scanf("%d", &q);
  int l, r;
  for (int i = 0; i < (q); ++i) {
    scanf("%d%d", &l, &r);
    printf("%lld\n", solve(l, r, p) % p);
  }
  return 0;
}
