/* *********************************************** Author :kuangbin Created Time :2016/4/22 19:01:07 File Name :F:\ACM\2016ACM\BestCoder\BC81\B.cpp ************************************************ */ #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; char str[1000010]; int num[30]; int main() { //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); int T; int k; scanf("%d", &T); while(T--) { scanf("%s%d",str,&k); memset(num,0,sizeof(num)); int l = 0; int cnt = 0; long long ans = 0; for(int i = 0; str[i];i++) { int t = str[i]-'a'; num[t]++; if(num[t] == 1)cnt++; while(cnt >= k) { num[str[l]-'a']--; if(num[str[l]-'a'] == 0)cnt--; l++; } ans += l; } printf("%I64d\n",ans); } return 0; }