#include <bits/stdc++.h>
const int N = 100050;
using namespace std;
inline int gi() {
  char ch = getchar();
  int x = 0, q = 0;
  while (ch < '0' || ch > '9') ch == '-' ? q = 1 : 0, ch = getchar();
  while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
  return q ? (-x) : x;
}
int f[N], g[105];
int fap(int a, int b, int mo) {
  int s = 1;
  while (b) {
    if (b & 1) {
      if (1ll * s * a >= mo)
        s = 1ll * s * a % mo + mo;
      else
        s = s * a;
    }
    b >>= 1;
    if (1ll * a * a >= mo)
      a = 1ll * a * a % mo + mo;
    else
      a = a * a;
  }
  return s;
}
int F(int n) {
  int phi = n;
  for (int i = 2; i <= n; ++i)
    if (!(n % i)) {
      phi = phi / i * (i - 1);
      while (n % i == 0) n /= i;
    }
  return phi;
}
void work() {
  int n = gi(), q;
  int l, r, ans, m;
  g[0] = gi();
  for (int i = 1; i <= n; ++i) f[i] = gi();
  q = gi();
  for (m = 0; g[m] != 1; ++m) {
    g[m + 1] = F(g[m]);
    if (g[m] == 1) break;
  }
  for (int t = 1; t <= q; ++t) {
    l = gi(), r = gi(), ans = 1;
    for (int i = min(r, l + m); i >= l; i--) ans = fap(f[i], ans, g[i - l]);
    printf("%d\n", ans % g[0]);
  }
  return;
}
int main() {
  work();
  return 0;
}
