#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
map<long long, long long> mp;
long long a[maxn];
long long qpow(long long a, long long b, long long mod) {
  long long res = 1;
  while (b) {
    if (b & 1) res = res * a > mod ? res * a % mod + mod : res * a;
    a = a * a > mod ? a * a % mod + mod : a * a;
    b >>= 1;
  }
  return res;
}
long long Phi(long long n) {
  long long res = n;
  for (int i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      while (n % i == 0) n /= i;
      res = res - res / i;
    }
  }
  if (n > 1) res = res - res / n;
  return res;
}
long long Solve(int l, int r, long long m) {
  if (l == r) return a[r];
  if (m == 1 || a[l] == 1) return 1;
  long long p = mp[m];
  return qpow(a[l], Solve(l + 1, r, p), m);
}
int main() {
  int n;
  long long m;
  scanf("%d%lld", &n, &m);
  for (int i = 1; i <= n; i++) scanf("%lld", a + i);
  long long cnt = m;
  while (cnt != 1) {
    mp[cnt] = Phi(cnt);
    cnt = mp[cnt];
  }
  int q;
  scanf("%d", &q);
  while (q--) {
    int l, r;
    scanf("%d%d", &l, &r);
    printf("%lld\n", Solve(l, r, m) % m);
  }
}
