#include <bits/stdc++.h>
typedef struct {
  long long val;
  bool flag;
} Node;
int a[100007], phi[37];
inline Node new_node(long long val, bool flag) {
  Node ans;
  ans.val = val;
  ans.flag = flag;
  return ans;
}
inline Node quick_pow(long long x, long long p, long long mod) {
  Node ans;
  ans.val = 1;
  ans.flag = false;
  while (p) {
    if (p & 1) {
      ans.val *= x;
      if (ans.val >= mod) {
        ans.val %= mod;
        ans.flag = true;
      }
    }
    x *= x;
    if (x >= mod) {
      x %= mod;
      ans.flag = true;
    }
    p >>= 1;
  }
  return ans;
}
inline bool miller_rabin(long long x, long long b) {
  long long k = x - 1;
  while (k) {
    long long cur = quick_pow(b, k, x).val;
    if (cur != 1 && cur != x - 1) return false;
    if (k & 1 == 1 || cur == x - 1) return true;
    k >>= 1;
  }
  return true;
}
inline bool prime(long long x) {
  if (x == 46856248255981ll || x < 2) return false;
  if (x == 2 || x == 3 || x == 7 || x == 61 || x == 24251) return true;
  return miller_rabin(x, 2) && miller_rabin(x, 61);
}
inline long long floyd(long long a, long long b, long long p) {
  return (a * a + b) % p;
}
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
inline long long pollard_pho(long long x) {
  long long t = 0, c = rand() % (x - 1) + 1, s, val;
  for (register int goal = 1;; goal <<= 1) {
    val = 1;
    s = t;
    for (register int stp = 1; stp <= goal; stp++) {
      t = floyd(t, c, x);
      val = val * abs(t - s) % x;
      if (stp % 127 == 0) {
        long long d = gcd(val, x);
        if (d > 1) return d;
      }
    }
    long long d = gcd(val, x);
    if (d > 1) return d;
  }
}
inline long long max(long long a, long long b) { return a > b ? a : b; }
void max_factor(long long n, long long &ans) {
  if (n <= ans || n < 2) {
    return;
  } else if (prime(n)) {
    ans = max(n, ans);
    return;
  }
  long long p = n;
  while (p >= n) {
    p = pollard_pho(n);
  }
  while (n % p == 0) {
    n /= p;
  }
  max_factor(n, ans);
  max_factor(p, ans);
}
inline long long euler(long long n) {
  long long ans = n;
  while (n != 1) {
    long long factor = 0;
    srand(time(NULL));
    max_factor(n, factor);
    while (n % factor == 0) {
      n /= factor;
    }
    ans = ans / factor * (factor - 1);
  }
  return ans;
}
Node power_tower(int l, int r, int i) {
  if (phi[i] == 1) return new_node(0, true);
  if (a[l] == 1) return new_node(1, false);
  if (l == r) return new_node(a[l] % phi[i], a[l] >= phi[i]);
  Node t = power_tower(l + 1, r, i + 1);
  if (t.flag) t.val += phi[i + 1];
  return quick_pow(a[l], t.val, phi[i]);
}
int main() {
  int n, m, q;
  scanf("%d %d", &n, &m);
  for (register int i = m, j = 0;; i = euler(i), j++) {
    phi[j] = i;
    if (i == 1) break;
  }
  for (register int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  scanf("%d", &q);
  for (register int i = 1; i <= q; i++) {
    int l, r;
    scanf("%d %d", &l, &r);
    printf("%lld\n", power_tower(l, r, 0).val % m);
  }
  return 0;
}
