#include <bits/stdc++.h>
using namespace std;
unordered_map<int, int> mp;
int n, q, l, r;
long long m, w[101010];
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 k) {
  long long x = k, s = k;
  if (mp.count(k)) return mp[k];
  for (int i = 2; i * i <= k; i++) {
    if (k % i == 0) s = 1ll * s / i * (i - 1);
    while (k % i == 0) k /= i;
  }
  if (k > 1) s = s / k * (k - 1);
  mp[x] = s;
  return s;
}
long long solve(int id, long long mod) {
  if (id == r || mod == 1) return w[id] >= mod ? w[id] % mod + mod : w[id];
  return qpow(w[id], solve(id + 1, phi(mod)), mod);
}
int main() {
  scanf("%d%lld", &n, &m);
  for (int i = 1; i <= n; i++) scanf("%lld", &w[i]);
  scanf("%lld", &q);
  while (q--) {
    scanf("%d%d", &l, &r);
    printf("%lld\n", solve(l, m) % m);
  }
  return 0;
}
