/**********************jibancanyang************************** *Author :jibancanyang *Created Time : Mon 18 Apr 2016 10:56:20 AM CST *File Name : hdu4507.cpp *Problem *Get: ***********************1599664856@qq.com**********************/ #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef pair pii; typedef long long ll; typedef unsigned long long ull; vector vi; #define pr(x) cout << #x << ": " << x << " " #define pl(x) cout << #x << ": " << x << endl; #define xx first #define yy second #define sa(n) scanf("%d", &(n)) #define rep(i, a, n) for (int i = a; i < n; i++) #define vep(c) for(decltype((c).begin() ) it = (c).begin(); it != (c).end(); it++) const int mod = int(1e9) + 7, INF = 0x3fffffff, maxn = 1e6 + 12; char str[maxn]; int logs[26]; int main(void) { int T; sa(T); while (T--) { str[0] = 's'; scanf("%s", str + 1); int k; sa(k); int len = strlen (str); memset(logs, 0, sizeof(logs)); int st = 0; ll ans = 0; if (k < len) for (int head = 1, tail = 0; tail < len; ) { while (st < k && tail < len) { tail++; if (tail == len) break; logs[str[tail] - 'a']++; if (logs[str[tail] - 'a'] == 1) st++; } //if (tail == len && st.size() < k) break; // cout << tail << endl; if (tail == len) break; logs[str[head] - 'a']--; if (logs[str[head] -'a'] == 0) st--; head++; //cout << ans << endl; ans += len - tail; if (tail < head - 1) tail = head - 1; // cout << tail << endl; } cout << ans << endl; } return 0; }