#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define DO(n) for ( int ____n ## __line__ = n; ____n ## __line__ -- ; ) #define ALL(A) A.begin(), A.end() #define BSC(A, x) (lower_bound(ALL(A), x) - A.begin()) #define CTN(T, x) (T.find(x) != T.end()) #define SZ(A) int(A.size()) #define PB push_back #define MP(A, B) make_pair(A, B) #define fi first #define se second typedef long long LL; typedef vector VI; typedef map MII; typedef pair PII; typedef pair PLL; template inline void RST(T &A){memset(A, 0, sizeof(A));} template inline void FLC(T &A, int x){memset(A, x, sizeof(A));} template inline void CLR(T &A){A.clear();} //} /** Constant List .. **/ //{ const int dx4[] = {-1, 0, 1, 0}; const int dy4[] = {0, 1, 0, -1}; const int dx8[] = {-1, 0, 1, 0 , -1 , -1 , 1 , 1}; const int dy8[] = {0, 1, 0, -1 , -1 , 1 , -1 , 1}; const int dxhorse[] = {-2 , -2 , -1 , -1 , 1 , 1 , 2 , 2}; const int dyhorse[] = {1 , -1 , 2 , -2 , 2 ,-2 , 1 ,-1}; const int MOD = 1000000007; //int MOD = 99990001; const int INF = 0x3f3f3f3f; const LL INFF = 1LL << 60; const double EPS = 1e-9; const double OO = 1e15; const double PI = acos(-1.0); //M_PI; //} template inline void checkMin(T &a,const T b){if (b inline void checkMax(T &a,const T b){if (a inline T low_bit(T x) {return x & -x;} /*****************************************************************/ /* .................................................................................................................................. */ const int N = 1e5 + 9; char a[N]; int K; int head; int cnt[256] , n; LL ans; void solve(){ RST(cnt); scanf("%s" , a); scanf("%d" , &K); n = strlen(a); head = 0; ans = 0; for (int i = 0 ; i < n ; ++i){ ++cnt[a[i]]; while (cnt[a[i]] > K){ --cnt[a[head]]; ++head; } ans += i - head + 1; } printf("%I64d\n" , ans); } int main(){ int _; scanf("%d" , &_); while (_--)solve(); }