ADAPET - Ada and Pet

Ada the Ladybug just got herself a new pet. She was thinking about a name for it. She thought-up a beautiful name for it already but now she doesn't think this name is "enough". She wants to find a new name, which will contain the original name at least K times as substring (to emphasize its importance). As Ada doesn't want the pet's name to be too long, she wants to find the shortest one - can you find the length of it?

Input

The first line of input will contain T, the number of test-cases.

Each of the next T lines will contain a non-empty string s, consisting of lowercase English letters and a number 1 ≤ K ≤ 106 (the number of times the given name shall be in the new name).

The sum of lengths of strings over all test-cases will not exceed 5*105.

Output

For each test-case print the minimum length of new name.

Example Input

8
ada 3
abc 2
r 7
rr 5
gorego 3
abbababbbbababababba 2
abcabcabca 3
lopadotemachoselachogaleokranioleipsanodrimhypotrimmatosilphioparaomelitokatakechymenokichlepikossyphophattoperisteralektryonoptekephalliokigklopeleiolagoiosiraiobaphetraganopterygon 1

Example Output

7
6
7
6
14
36
16
182

Added by:Morass
Date:2017-12-31
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All

hide comments
2021-05-18 15:17:13
Use ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); in c++ code to avoid TLE :)
2021-05-18 06:32:32 Bitagi97
If you get TLE test 14, try to use printf and scanf instead of cin, cout
2021-02-15 17:01:36
use fast io along with KMP prefix table
2020-08-31 19:02:35
I am getting TLE on test case 14 continuously.please help me to solve TLE
2020-07-27 22:00:32
@morass can you check my solution it is O(N) but gives TLE . id:26339929 .Please!!!!!!!
Thank You.
2020-01-24 10:39:17
AC in one go :)
Used prefix table of KMP algorithm.
2018-06-19 15:19:32
use long long
2018-02-17 10:25:25
Am I the only one who doesn't get it?
2018-01-29 21:27:11
@morass Please check my code.submission id=20176168

Last edit: 2018-01-31 20:58:15
2018-01-18 18:42:16
try this:
1
acacabacacabacacac
1000000
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.