#include <bits/stdc++.h>
using namespace std;
long long qr() {
  long long sum = 0, b = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') b = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    sum = (sum << 1) + (sum << 3) + ch - '0';
    ch = getchar();
  }
  return sum * b;
}
long long n, t, m, a[100010];
map<long long, long long> ph;
long long phi(long long n) {
  long long ans = n, m = sqrt(n);
  for (long long i = 2; i <= m; ++i) {
    if (n % i == 0) {
      ans = (ans / i) * (i - 1);
      while (n % i == 0) n /= i;
    }
  }
  if (n > 1) ans = (ans * (n - 1)) / n;
  return ans;
}
long long qkpow(long long a, long long b, long long mo) {
  long long an = 1;
  while (b) {
    if (b & 1) {
      an = an * a;
      if (an >= mo) an = an % mo + mo;
    }
    a = a * a;
    if (a >= mo) a = a % mo + mo;
    b >>= 1;
  }
  return an;
}
long long l, r;
long long ddfs(long long i, long long mo) {
  if (i == r + 1 || mo == 1) return 1;
  long long t = ddfs(i + 1, ph[mo]);
  return qkpow(a[i], t, mo);
}
signed main() {
  n = qr(), m = qr();
  for (long long i = 1; i <= n; ++i) a[i] = qr();
  t = qr();
  long long cu = m;
  while (cu != 1) ph[cu] = phi(cu), cu = ph[cu];
  ph[cu] = 1;
  for (long long o = 1; o <= t; ++o) {
    l = qr(), r = qr();
    printf("%lld\n", ddfs(l, m) % m);
  }
  return 0;
}
