#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
long long m, a[N], phi[100];
long long POW(long long a, long long b, long long md) {
  long long ans = 1;
  while (b) {
    if (b & 1) {
      if (ans * a >= md)
        ans = ans * a % md + md;
      else
        ans = ans * a;
    }
    b >>= 1;
    if (a * a >= md)
      a = a * a % md + md;
    else
      a = a * a;
  }
  return ans;
}
long long PHI(long long k) {
  long long ans = k;
  for (long long p = 2; p * p <= k; p++) {
    if (k % p == 0) {
      while (k % p == 0) k /= p;
      ans -= (ans / p);
    }
  }
  if (k > 1) {
    ans -= (ans / k);
  }
  return ans;
}
int main() {
  int n;
  cin >> n >> m;
  for (int i = 0; i < n; i++) cin >> a[i];
  int q;
  cin >> q;
  phi[0] = m;
  int idx = 0;
  for (int i = 1; i < 100; i++) {
    phi[i] = PHI(phi[i - 1]);
    if (phi[i - 1] == 1) {
      idx = i;
      break;
    }
    if (phi[i] == 1) {
      idx = i + 1;
      break;
    }
  }
  idx--;
  while (q--) {
    int l, r;
    cin >> l >> r;
    l--, r--;
    long long ans = 1LL;
    for (int i = min(r, l + idx); i >= l; i--) {
      ans = POW(a[i], ans, phi[i - l]);
    }
    cout << ans % m << "\n";
  }
  return 0;
}
