#include <bits/stdc++.h>
using namespace std;
unordered_map<int, int> mp;
const int maxn = 2e5 + 233;
int phi(int n) {
  if (mp.find(n) != mp.end()) return mp[n];
  int res = n;
  int tmp = n;
  for (int i = 2; i * i <= tmp; i += 1) {
    if (tmp % i == 0) {
      res = res / i * (i - 1);
      while (tmp % i == 0) {
        tmp /= i;
      }
    }
  }
  if (tmp > 1) res = res / tmp * (tmp - 1);
  return mp[n] = res;
}
int a[maxn], n, m, q;
int mul(int x, int y, int mod) {
  return 1ll * x * y >= mod ? 1ll * x * y % mod + mod : 1ll * x * y % mod;
}
int pow_mod(int q, int w, int mod) {
  int ret = 1;
  while (w) {
    if (w & 1) ret = mul(ret, q, mod);
    q = mul(q, q, mod);
    w >>= 1;
  }
  return ret;
}
int solve(int l, int r, int mod) {
  if (mod == 1) {
    return 1;
  }
  if (l == r) {
    return mul(1, a[l], mod);
  }
  int cur_phi = phi(mod);
  int p = solve(l + 1, r, cur_phi);
  return pow_mod(a[l], p, mod);
}
int main() {
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n >> m;
  for (int i = 1; i <= n; i += 1) {
    cin >> a[i];
  }
  cin >> q;
  while (q--) {
    int l, r;
    cin >> l >> r;
    cout << solve(l, r, m) % m << "\n";
  }
  return 0;
}
