#include<bits/stdc++.h> usingnamespacestd; typedefunsignedlonglong ull; constint N = 100000 + 10, C = 10; constint base = 131; int n, m, a[N][C], id[N], rk[N]; ull p[N], b[N][C]; char s[N]; intgetmax(int x, int y){ if (x > y) swap(x, y); int r = n - y + 1, l = 0, ans = 0, mid, h = 0; while (l <= r) { mid = (l + r) / 2; h = 0; for (int i = 0; h == i && i < C; i++) h += (((b[x + mid - 1][a[x][i]] - b[x - 1][a[x][i]]) * p[y - x] - b[y + mid - 1][a[y][i]] + b[y - 1][a[y][i]]) == 0); if (h == C) l = mid + 1, ans = mid; else r = mid - 1; } return ans; } boolcmp(int x, int y){ int d = getmax(x, y); if (d == n - max(x, y) + 1) return x > y; int _A = 0, _B = 0; for (int i = 0; i < C; i++) { if (s[x + d] == a[x][i]) _A = i; if (s[y + d] == a[y][i]) _B = i; } return _A < _B; } voidinit(){ for (int i = p[0] = 1; i < N; i++) p[i] = p[i - 1] * base; for (int i = 1; i <= n; i++) s[i] -= 'a'; for (int i = 0; i < C; i++) a[n + 1][i] = i; for (int i = n; i > 0; i--) { int pos = 0; for (int j = 0; j < C; j++) { a[i][j] = a[i + 1][j]; if (a[i][j] == s[i]) pos = j; } while (pos--) swap(a[i][pos], a[i][pos + 1]); } for (int i = 1; i <= n; i++) { b[i][s[i]] = p[i]; for (int j = 0; j < C; j++) b[i][j] = b[i][j] + b[i - 1][j]; } for (int i = 1; i <= n; i++) id[i] = i; stable_sort(id + 1, id + n + 1, cmp); for (int i = 1; i <= n; i++) rk[id[i]] = i; } intmain(){ scanf("%d%d", &n, &m); scanf("%s", s + 1); init(); while (m--) { int l, r, ans; scanf("%d%d", &l, &r); int x = rk[l]; int d = r - l + 1; l = ans = 1; r = x; while (l <= r) { int mid = (l + r) >> 1; if (getmax(id[x], id[mid]) >= d) ans = mid, r = mid - 1; else l = mid + 1; } int L = ans; l = x; r = n; while (l <= r) { int mid = (l + r) >> 1; if (getmax(id[x], id[mid]) >= d) ans = mid, l = mid + 1; else r = mid - 1; } int R = ans; printf("%d\n", R - L + 1); } return0; }