using namespace std;
const int inf = 0x3fffffff;
const int SINF = 0x7fffffff;
const long long LINF = 0x3fffffffffffffff;
const long long SLINF = 0x7fffffffffffffff;
const int maxn = 1e5 + 200;
char str[maxn];
int t;
int A[maxn], B[maxn];
template <typename T> inline void read(T &x) {
x = 0;
T f = 1;
char ch;
do {
ch = getchar();
if (ch == '-')
f = -1;
} while (ch < '0' || ch > '9');
do
x = x * 10 + ch - '0', ch = getchar();
while (ch <= '9' && ch >= '0');
x *= f;
}
template <typename A, typename B> inline void read(A &x, B &y) {
read(x);
read(y);
}
template <typename A, typename B, typename C>
inline void read(A &x, B &y, C &z) {
read(x);
read(y);
read(z);
}
template <typename A, typename B, typename C, typename D>
inline void read(A &x, B &y, C &z, D &w) {
read(x);
read(y);
read(z);
read(w);
}
int suffaa_[maxn], suffb_[maxn], suffa_[maxn];
int ch[maxn], suff_auto[maxn], Rank[maxn], height[maxn];
int a[maxn];
struct Suffix_Auto {
void Gersuff_a(int *ch, int *suff_auto, int *rank, int n) {
for (int i = 0; i < maxn; i++)
suffaa_[i] = 0;
for (int i = 1; i <= n; i++)
suffaa_[ch[i]]++;
for (int i = 1; i <= maxn; i++)
suffaa_[i] += suffaa_[i - 1];
for (int i = n; i; i--)
suff_auto[suffaa_[ch[i]]--] = i;
rank[suff_auto[1]] = 1;
for (int i = 2; i <= n; i++) {
rank[suff_auto[i]] = rank[suff_auto[i - 1]];
if (ch[suff_auto[i]] != ch[suff_auto[i - 1]])
rank[suff_auto[i]]++;
}
for (int l = 1; rank[suff_auto[n]] < n; l <<= 1) {
lpi(i, 0, maxn - 1) suffaa_[i] = 0;
lpi(i, 0, maxn - 1) suffb_[i] = 0;
lpi(i, 1, n) {
suffaa_[A[i] = rank[i]]++;
suffb_[B[i] = (i + l <= n) ? rank[i + l] : 0]++;
}
lpi(i, 1, maxn - 1) suffb_[i] += suffb_[i - 1];
for (int i = n; i; i--)
suffa_[suffb_[B[i]]--] = i;
for (int i = 1; i < maxn; i++)
suffaa_[i] += suffaa_[i - 1];
for (int i = n; i; i--)
suff_auto[suffaa_[A[suffa_[i]]]--] = suffa_[i];
rank[suff_auto[1]] = 1;
for (int i = 2; i <= n; i++) {
rank[suff_auto[i]] = rank[suff_auto[i - 1]];
if (A[suff_auto[i]] != A[suff_auto[i - 1]] || B[suff_auto[i]] != B[suff_auto[i - 1]])
rank[suff_auto[i]]++;
}
}
}
void GetHeight(int *ch, int *suff_auto, int *rank, int *height, int n) {
cls(height, 0);
cls(rank, 0);
cls(suff_auto, 0);
Gersuff_a(ch, suff_auto, rank, n);
for (int i = 1, j = 0; i <= n; i++) {
if (j)
j--;
while (ch[i + j] == ch[suff_auto[rank[i] - 1] + j])
j++;
height[rank[i]] = j;
}
}
ll GetIndex(int k, int n) {
int f = 0, l = 1;
ll ans = 0;
a[0] = height[1];
k--;
if (k == 0) {
for (int i = 1; i <= n; ++i)
ans = ans + (n - suff_auto[i] + 1 - height[i]);
return ans;
}
int last = 0;
for (int i = 2; i <= n; i++) {
if (i > k && height[i - k] == a[f])
f++;
while (l > f && a[l - 1] > height[i])
l--;
a[l] = height[i];
l++;
if (i >= k) {
if (a[f] >= last)
ans = ans + a[f] - last;
last = height[i - k + 1];
}
}
return ans;
}
} suff;
int main() {
// freopen("in.txt","r",stdin);
read(t);
lpi(i, 1, t) {
cls(ch, 0);
int n, k;
scanf("%d", &k);
scanf("%s", str);
n = (int)strlen(str);
for (int i = 1; i <= n; i++)
ch[i] = str[i - 1];
suff.GetHeight(ch, suff_auto, Rank, height, n);
printf("%lld", suff.GetIndex(k, n) - suff.GetIndex(k + 1, n));
printf("\n");
}
return 0;
}