Problem: Priests of the Quetzalcoatl cult want to build a tower to represent a power of their god. Tower is usually made of power-charged rocks. It is built with the help of rare magic by levitating the current top of tower and adding rocks at its bottom. If top, which is built from k - 1 rocks, possesses power p and we want to add the rock charged with power wk then value of power of a new tower will be {wk}p. 

Rocks are added from the last to the first. That is for sequence w1, ..., wm value of power will be

<image>

After tower is built, its power may be extremely large. But still priests want to get some information about it, namely they want to know a number called cumulative power which is the true value of power taken modulo m. Priests have n rocks numbered from 1 to n. They ask you to calculate which value of cumulative power will the tower possess if they will build it from rocks numbered l, l + 1, ..., r. 

Input

First line of input contains two integers n (1 ≤ n ≤ 105) and m (1 ≤ m ≤ 109).

Second line of input contains n integers wk (1 ≤ wk ≤ 109) which is the power of rocks that priests have.

Third line of input contains single integer q (1 ≤ q ≤ 105) which is amount of queries from priests to you.

kth of next q lines contains two integers lk and rk (1 ≤ lk ≤ rk ≤ n). 

Output

Output q integers. k-th of them must be the amount of cumulative power the tower will have if is built from rocks lk, lk + 1, ..., rk.

Example

Input

6 1000000000
1 2 2 3 3 3
8
1 1
1 6
2 2
2 3
2 4
4 4
4 5
4 6


Output

1
1
2
4
256
3
27
597484987

Note

327 = 7625597484987

Solution (in CPP):

Solution:
#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;
}