#include <bits/stdc++.h>
using namespace std;
int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch - '0' < 0 || ch - '0' > 9) {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch - '0' >= 0 && ch - '0' <= 9) {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
int a[100010];
int n, P;
int b[100010], top;
int get_phi(int x) {
  int tmp = x, res = x;
  for (int i = 2; i <= sqrt(x); i++) {
    if (tmp % i == 0) {
      res /= i;
      res *= (i - 1);
      while (tmp % i == 0) tmp /= i;
    }
  }
  if (tmp != 1) res /= tmp, res *= (tmp - 1);
  return res;
}
int calc(long long x, int mod) {
  if (x >= mod) return x % mod + mod;
  return x;
}
int quick_pow(int x, int p, int mod) {
  int an = 1, po = x;
  while (p) {
    if (p & 1) an = calc(1ll * an * po, mod);
    po = calc(1ll * po * po, mod);
    p >>= 1;
  }
  return an;
}
int solve(int x, int i, int lim) {
  if (x == lim || i == top) return calc(a[x], b[i]);
  int tmp = solve(x + 1, i + 1, lim);
  return quick_pow(a[x], tmp, b[i]);
}
int main() {
  n = read();
  P = read();
  for (int i = 1; i <= n; i++) a[i] = read();
  b[1] = P;
  top = 1;
  while (b[top] != 1) {
    b[top + 1] = get_phi(b[top]);
    top++;
  }
  int m = read();
  while (m--) {
    int l = read(), r = read();
    printf("%d\n", solve(l, 1, r) % P);
  }
  return 0;
}
