#include <bits/stdc++.h>
using namespace std;
template <class T>
void read(T &x) {
  char c;
  bool op = 0;
  while (c = getchar(), c < '0' || c > '9')
    if (c == '-') op = 1;
  x = c - '0';
  while (c = getchar(), c >= '0' && c <= '9') x = x * 10 + c - '0';
  if (op) x = -x;
}
template <class T>
void write(T x) {
  if (x < 0) putchar('-'), x = -x;
  if (x >= 10) write(x / 10);
  putchar('0' + x % 10);
}
const int N = 100005;
int n, len, q, l, r;
long long m[N], w[N];
int phi(int x) {
  int ret = x;
  for (int i = 2; i * i <= x; i++)
    if (x % i == 0) {
      ret = ret / i * (i - 1);
      while (x % i == 0) x /= i;
    }
  if (x > 1) ret = ret / x * (x - 1);
  return ret;
}
pair<long long, bool> qpow(long long a, long long x, long long p) {
  bool flag = 0;
  long long ret = 1;
  while (x) {
    if (x & 1) {
      if (ret * a >= p) flag = 1;
      ret = ret * a % p;
    }
    if (a * a >= p) flag = 1;
    a = a * a % p;
    x >>= 1;
  }
  return make_pair(ret, flag);
}
pair<long long, bool> solve(int l, int r, int k) {
  if (k >= len || k == r - l + 1) return make_pair(w[r] % m[k], w[r] >= m[k]);
  pair<long long, bool> tmp = solve(l, r, k + 1);
  long long exp = tmp.first;
  if (tmp.second) exp += m[k + 1];
  pair<long long, bool> ans = qpow(w[l + k - 1], exp, m[k]);
  return ans;
}
int main() {
  read(n), read(m[len = 1]);
  while (m[len] > 1) m[len + 1] = phi(m[len]), len++;
  for (int i = 1; i <= n; i++) read(w[i]);
  read(q);
  while (q--) {
    read(l), read(r);
    write(solve(l, r, 1).first), putchar('\n');
  }
  return 0;
}
