#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
const int INF = 0x3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-10;
int dcmp(double first) {
  if (fabs(first) < eps) return 0;
  return first < 0 ? -1 : 1;
}
long long a[N], b[N], ph[N];
int l, r, ot;
long long qpowt(long long a, long long n, long long mit) {
  if (a > mit) return -1;
  long long ans = 1;
  int f = 1;
  while (n) {
    if (n & 1) {
      if (!f) return -1;
      ans *= a;
      if (ans > mit) return -1;
    }
    a *= a;
    if (a > mit) f = 0;
    n >>= 1;
  }
  return ans;
}
long long phe(long long n) {
  long long sum = n;
  long long m = sqrt(n + 0.5);
  for (long long i = 2; i <= m; i++) {
    if (!(n % i)) {
      sum = sum * (i - 1) / i;
      while (!(n % i)) n /= i;
    }
  }
  if (n > 1) sum = sum / n * (n - 1);
  return sum;
}
long long qpow(long long a, long long n, long long M) {
  a %= M;
  long long ans = 1;
  while (n) {
    if (n & 1) ans = (ans * a) % M;
    a = (a * a) % M;
    n >>= 1;
  }
  return ans;
}
long long f(long long pos) {
  long long pm = ph[pos - l + 1], ans;
  if (pm == 1 || pm == 0) {
    ans = 1;
  } else if (pos + 1 >= ot) {
    if (b[pos + 1] <= pm)
      ans = b[pos + 1];
    else
      ans = b[pos + 1] % pm + pm;
  } else
    ans = f(pos + 1) + pm;
  return qpow(a[pos], ans, ph[pos - l]);
}
int main() {
  int n, Q, cnt = 0, mit;
  scanf("%d %d", &n, &mit);
  vector<int> tt;
  for (int i = 0; i < n; i++) {
    scanf("%lld", &a[i]);
    if (a[i] == 1) tt.push_back(i);
  }
  ph[0] = mit;
  int n1 = 0;
  while (ph[n1] != 1) {
    n1++;
    ph[n1] = phe(ph[n1 - 1]);
  }
  scanf("%d", &Q);
  while (Q--) {
    scanf("%d %d", &l, &r);
    l--;
    r--;
    int fq = lower_bound(tt.begin(), tt.end(), l) - tt.begin();
    if (fq != tt.size()) r = min(r, tt[fq]);
    ot = r;
    b[r] = a[r];
    if (ph[r - l + 1])
      for (int i = r - 1; i >= l; i--) {
        b[i] = qpowt(a[i], b[i + 1], ph[r - l]);
        if (b[i] != -1)
          ot = i;
        else
          break;
      }
    if (l == r)
      printf("%lld\n", a[l] % mit);
    else
      printf("%lld\n", f(l));
  }
  return 0;
}
