ADAPET - Ada and Pet

no tags 

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?


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.


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

Example Input

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

Example Output


hide comments
be1035016: 2018-06-19 15:19:32

use long long

cuberiot: 2018-02-17 10:25:25

Am I the only one who doesn't get it?

shuvam_kedia: 2018-01-29 21:27:11

@morass Please check my code.submission id=20176168

Last edit: 2018-01-31 20:58:15
monukumar: 2018-01-18 18:42:16

try this:

monukumar: 2018-01-18 17:49:32

Don't forget long :D

hanstan: 2018-01-10 17:38:04

Nailed it 0.00s xD

trishala_naman: 2018-01-06 21:51:03

Finally AC in 0.11s. Thanks @morass for that example :)

manya_cod4: 2018-01-06 09:12:35

From TLE -> WA -> AC in 1.05 - > AC in 0.13. Thanks @shubham_001 and @morass.

manya_cod4: 2018-01-05 15:55:06

@morass. Hi. Now i get your hint :P. but could never have guessed. I didnt know that concept. And Thanks for setting so many awesome questions. I always learn something new solving questions set by you. :).

manya_cod4: 2018-01-05 15:52:33

@shubham. Hi. Thanks for replying on my help thread. Seriously. learnt something new today.

Added by:Morass
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)