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>
using namespace std;
const int N = 1e5 + 5, M = 1e5;
inline int read() {
  int sum = 0;
  char c = getchar();
  while (c < '0' || c > '9') c = getchar();
  while (c >= '0' && c <= '9') {
    sum = sum * 10 + c - '0';
    c = getchar();
  }
  return sum;
}
inline void write(int x) {
  if (x > 9) write(x / 10);
  putchar(x % 10 + '0');
}
inline long long fpow(long long qwe, long long asd, long long zxc) {
  long long x = qwe, y = 1;
  while (asd) {
    if (asd & 1) {
      y = y * x % zxc;
    }
    x = x * x % zxc;
    asd >>= 1;
  }
  return y;
}
inline long long fpow2(long long qwe, long long asd, long long zxc) {
  long long x = qwe, y = 1;
  while (asd) {
    if (asd & 1) y = y * x;
    if (y >= zxc || x >= zxc) return -1;
    x = x * x;
    asd >>= 1;
  }
  return y;
}
long long a[N], pi[N];
int pcou = 1, hz[N];
vector<long long> a2[N];
int n, m;
long long p;
inline int qpi(int qwe) {
  int asd = qwe, zxc = int(sqrt(qwe) + 0.5);
  for (int i = 2; i <= zxc; ++i) {
    if (qwe % i == 0) {
      asd = asd / i * (i - 1);
      while (qwe % i == 0) qwe /= i;
    }
  }
  if (qwe > 1) asd = asd / qwe * (qwe - 1);
  return asd;
}
inline void qpicsh(int qwe) {
  pi[pcou++] = qwe;
  while (qwe != 1) {
    qwe = qpi(qwe);
    pi[pcou++] = qwe;
  }
}
inline void cscsh(int x) {
  a2[x].push_back(a[x]);
  long long asd = a[x];
  for (int i = x - 1; i >= x - pcou - 5 && i >= 1; --i) {
    long long qwe = fpow2(a[i], asd, p);
    if (qwe == -1) break;
    a2[x].push_back(qwe);
    asd = qwe;
  }
}
long long dq = -1, cc = 0;
inline long long dfs(int l, int r, int y) {
  if (r - l < a2[r].size()) {
    long long x = a2[r][r - l];
    dq = x;
    cc = 0;
    if (x < pi[y]) {
      return x;
    }
    x = x % pi[y];
    return x;
  }
  if (pi[y] == 1) {
    int b = hz[l];
    if (l >= r) {
      dq = -1;
      cc = 1;
    } else {
      dq = a[b];
      for (int i = b - 1; i >= l; --i) {
        dq = fpow2(a[i], dq, p);
        if (dq == -1) {
          cc = 1;
          break;
        }
      }
      if (dq != -1) {
        cc = 0;
      }
    }
    return 0;
  }
  long long x = dfs(l + 1, r, y + 1) + pi[y + 1];
  if (!cc && dq < pi[y + 1]) x -= pi[y + 1];
  if (!cc) {
    dq = fpow2(a[l], dq, p);
    if (dq == -1) cc = 1;
  } else if (a[l] == 1) {
    dq = 1;
    cc = 0;
  }
  if (!cc && dq < pi[y]) {
    return dq;
  } else {
    return fpow(a[l], x, pi[y]) % pi[y];
  }
}
inline void solve() {
  for (int i = 1; i <= m; ++i) {
    int l = read(), r = read();
    if (r - l < a2[r].size()) {
      write(a2[r][r - l] % p);
      putchar('\n');
      continue;
    }
    long long ans = dfs(l, r, 1) % p;
    write(ans);
    putchar('\n');
  }
}
inline void csh() {
  n = read(), p = read();
  qpicsh(p);
  for (int i = 1; i <= n; ++i) {
    a[i] = read();
    cscsh(i);
  }
  int wz = n;
  for (int i = n; i >= 1; --i) {
    if (a[i] == 1) wz = i;
    hz[i] = wz;
  }
  m = read();
  solve();
}
int main() { csh(); }