#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
int n, mod, q, l, r;
long long a[N];
map<long long, long long> M;
int phi(int n) {
  if (M.count(n) == 1) return M[n];
  int r = n, nn = n;
  for (int i = 2; i * i <= n; i++)
    if (n % i == 0) {
      r = r / i * (i - 1);
      while (n % i == 0) n /= i;
    }
  if (n > 1) r = r / n * (n - 1);
  M[nn] = r;
  return r;
}
long long Euler_qpow(long long a, long long b, long long mod) {
  long long res = 1;
  bool ok = (b > 0 && a >= mod);
  while (b > 0) {
    if (b & 1) {
      res = res * a;
      ok |= (res >= mod);
      res %= mod;
    }
    a = a * a;
    ok |= (b > 1 && a >= mod);
    a %= mod;
    b >>= 1;
  }
  return res + mod * ok;
}
long long work(int l, int r, int mod) {
  if (mod == 1) return 1;
  if (l == r) return a[l];
  return Euler_qpow(a[l], work(l + 1, r, phi(mod)), mod);
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> mod;
  for (int i = (1); i < (n + 1); i++) cin >> a[i];
  cin >> q;
  for (int cas = (0); cas < (q); cas++) {
    cin >> l >> r;
    cout << work(l, r, mod) % mod << "\n";
  }
  return 0;
}
