#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const double eps = 1e-6;
template <class T>
T gcd(T a, T b) {
  if (!b) return a;
  return gcd(b, a % b);
}
const int maxn = 1e6 + 10;
int n, m;
int phi[maxn];
int a[maxn];
int get(int x) {
  int res = x;
  for (long long i = 2; i * i <= x; i++) {
    if (x % i == 0) {
      res = res / i * (i - 1);
      while (x % i == 0) x /= i;
    }
  }
  if (x > 1) res = res / x * (x - 1);
  return res;
}
int qpow(int b, int n, int mod) {
  int res = 1;
  while (n) {
    if (n & 1) res = 1LL * res * b >= mod ? 1LL * res * b % mod + mod : res * b;
    b = 1LL * b * b >= mod ? 1LL * b * b % mod + mod : b * b;
    n >>= 1;
  }
  return res;
}
int solve(int l, int r, int k) {
  if (phi[k] == 1) return 1;
  if (l == r) {
    if (a[l] < phi[k])
      return a[l];
    else
      return a[l] % phi[k] + phi[k];
  } else
    return qpow(a[l], solve(l + 1, r, k + 1), phi[k]);
}
int main() {
  scanf("%d", &n);
  scanf("%d", &phi[0]);
  while (phi[m] != 1) {
    phi[m + 1] = get(phi[m]);
    m++;
  }
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  int q;
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    int l, r;
    scanf("%d", &l);
    scanf("%d", &r);
    int ans = solve(l, r, 0) % phi[0];
    printf("%d\n", ans);
  }
  return 0;
}
