#include <bits/stdc++.h>
using namespace std;
long long n, i, j, k, s, t, mod, top, num, T, l, r, ans;
long long phi[20000][2], prime[20000], a[100010], b[100010];
bool isprime[32000];
long long ksm(long long x, long long y, long long mod) {
  long long temp = 1;
  while (y > 0) {
    if (y % 2 == 1) temp = temp * x % mod;
    x = x * x % mod;
    y = y / 2;
  }
  return temp;
}
long long getphi(long long x) {
  long long temp = x;
  for (int i = 1; i <= num; i++) {
    if (prime[i] * prime[i] > x) break;
    if (x % prime[i] == 0) {
      temp = temp / prime[i] * (prime[i] - 1);
      while (x % prime[i] == 0) x = x / prime[i];
    }
  }
  if (x > 1) {
    temp = temp / x * (x - 1);
  }
  return temp;
}
long long check(long long l, long long r, long long x) {
  long long temp = a[r], s, t;
  if (temp > x) return -1;
  for (int i = r - 1; i >= l; i--) {
    s = a[i];
    t = temp;
    temp = 1;
    while (t > 0) {
      if (t % 2 == 1) temp = temp * s;
      if (temp > x) return -1;
      s = s * s;
      t = t / 2;
      if (t > 0 && s > x) return -1;
    }
  }
  return temp;
}
long long work(long long l, long long r, long long x) {
  long long mod = phi[x][0], t = phi[x][1];
  if (l == r) return a[l] % mod;
  if (mod == 1) return 0;
  long long s = check(l + 1, r, t);
  if (s < 0) {
    s = work(l + 1, r, x + 1) + t;
    return ksm(a[l], s, mod);
  } else
    return ksm(a[l], s, mod);
}
int main() {
  num = 0;
  t = int(sqrt(1000000000));
  for (i = 2; i <= t; i++) {
    if (!isprime[i]) {
      num++;
      prime[num] = i;
    }
    for (j = 1; j <= num; j++) {
      s = i * prime[j];
      if (s > t) break;
      isprime[s] = 1;
      if (i % prime[j] == 0) break;
    }
  }
  scanf("%lld%lld", &n, &mod);
  top = 1;
  phi[1][0] = mod;
  while (true) {
    t = getphi(phi[top][0]);
    phi[top][1] = t;
    if (t == 1)
      break;
    else {
      top++;
      phi[top][0] = t;
    }
  }
  top++;
  phi[top][0] = phi[top][1] = 1;
  for (i = 1; i <= n; i++) scanf("%lld", &a[i]);
  t = n + 1;
  for (i = n; i >= 1; i--) {
    if (a[i] == 1) t = i;
    b[i] = t;
  }
  scanf("%lld", &T);
  while (T--) {
    scanf("%lld%lld", &l, &r);
    t = b[l];
    if (t < r) r = t;
    ans = work(l, r, 1);
    printf("%lld\n", ans);
  }
}
