#include <bits/stdc++.h>
using namespace std;
const int MX = 1e7;
const int N = 2e5;
const int M = 6e6;
const int VV = 7;
int n, q;
int ai[N], nxt[N], lst[M], not_prime[M], val[M], fas[M], mx;
int rig[N][VV * 3], mxd[VV * 3], tmp[N], tot;
void chkmax(int &t, int d) { t = max(t, d); }
int main() {
  scanf("%d%d", &n, &q);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &ai[i]);
    mx = max(mx, ai[i]);
  }
  for (int i = 1; i <= mx; ++i) fas[i] = 1;
  for (int i = 2; i <= mx; ++i)
    if (!not_prime[i])
      for (int j = i; j <= mx; j += i) {
        int e = 0;
        for (int p = j; p % i == 0; p /= i) e ^= 1;
        val[j] += e;
        if (e) fas[j] *= i;
        if (i != j) not_prime[j] = 1;
      }
  mx = 0;
  for (int i = 1; i <= n; ++i) {
    ai[i] = fas[ai[i]];
    mx = max(mx, ai[i]);
    nxt[i] = lst[ai[i]];
    lst[ai[i]] = i;
  }
  for (int i = 1; i <= mx; ++i) {
    tot = 0;
    for (int j = i; j <= mx; j += i) {
      for (int k = lst[j]; k; k = nxt[k]) tmp[++tot] = k;
    }
    sort(tmp + 1, tmp + tot + 1);
    for (int j = 0; j <= VV; ++j) mxd[j] = 0;
    for (int j = 1; j <= tot; ++j) {
      for (int k = 0; k <= VV; ++k)
        chkmax(rig[tmp[j]][val[ai[tmp[j]]] - val[i] + k], mxd[k]);
      chkmax(mxd[val[ai[tmp[j]]] - val[i]], tmp[j]);
    }
  }
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= VV * 2; ++j) chkmax(rig[i][j], rig[i][j - 1]);
    for (int j = 0; j <= VV * 2; ++j) chkmax(rig[i][j], rig[i - 1][j]);
  }
  while (q--) {
    int l, r;
    scanf("%d%d", &l, &r);
    int p = VV * 2;
    while (p && rig[r][p - 1] >= l) p--;
    printf("%d\n", p);
  }
}
