#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100010;
int mul_mod(int a, int b, int p) {
  return (long long)a * b >= p ? (long long)a * b % p + p : a * b;
}
int power_mod(int a, int n, int p) {
  int ret = 1;
  while (n) {
    if (n & 1) ret = mul_mod(ret, a, p);
    a = mul_mod(a, a, p);
    n >>= 1;
  }
  return ret;
}
map<int, int> phi;
int euler(int n) {
  if (phi.count(n)) return phi[n];
  int ret = n;
  for (int x = 2; x * x <= n; x++) {
    if (n % x == 0) {
      ret = ret / x * (x - 1);
      while (n % x == 0) n /= x;
    }
  }
  if (n > 1) ret = ret / n * (n - 1);
  return phi[n] = ret;
}
int w[MAXN];
int calc(int l, int r, int p) {
  if (p == 1 || l == r) return power_mod(w[l], 1, p);
  int e = calc(l + 1, r, euler(p));
  return power_mod(w[l], e, p);
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) scanf("%d", &w[i]);
  int q;
  scanf("%d", &q);
  while (q--) {
    int l, r;
    scanf("%d%d", &l, &r);
    printf("%d\n", calc(l, r, m) % m);
  }
  return 0;
}
