#include <bits/stdc++.h>
using namespace std;
int w[100005];
int mod[100005];
inline int pro(long long x, int md) { return x < md ? x : x % md + md; }
inline int quickpow(int x, int y, int md) {
  register int z = 1;
  for (; y; x = pro(1LL * x * x, md), y >>= 1)
    if (y & 1) z = pro(1LL * z * x, md);
  return z;
}
long long euler_phi(long long n) {
  long long k = (long long)sqrt(n + 0.5);
  long long ans = n;
  for (int i = 2; i <= k; i++) {
    if (n % i == 0) {
      ans = ans / i * (i - 1);
      while (n % i == 0) n /= i;
    }
  }
  if (n > 1) ans = ans / n * (n - 1);
  return ans;
}
long long dfs(int l, int r, int step) {
  if (l == r) return w[l] >= mod[step] ? w[l] % mod[step] + mod[step] : w[l];
  if (mod[step] == 1) return 1;
  return quickpow(w[l], dfs(l + 1, r, step + 1), mod[step]);
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  mod[1] = m;
  for (int i = 2;; i++) {
    if (mod[i - 1] != 1) {
      mod[i] = euler_phi(mod[i - 1]);
    } else {
      break;
    }
  }
  for (int i = 1; i <= n; i++) {
    scanf("%d", &w[i]);
  }
  int q;
  scanf("%d", &q);
  while (q--) {
    int l, r;
    scanf("%d%d", &l, &r);
    long long ans = dfs(l, r, 1);
    printf("%lld\n", ans % mod[1]);
  }
}
