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?

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

hide comments
manya_cod4: 2018-01-03 23:14:14

Hi All, Good day to everyone. My code is working for small inputs. but giving WA on submission in c++. It would be a great help if someone could provide me with a good input. Thanks a lot !

morass: 2018-01-03 23:10:12

@manya_cod4: Good day to you. As long as your code is O(N) then it shall be ok (anyway if it would be - for example - O(MAX_SIZE) per test case, then it could be TLE. Good Luck!

manya_cod4: 2018-01-03 18:19:07

Hi Morass. Good day to you. I am using KMP's preprocessing technique. I am getting TLE in java. The Solution needs to be faster than that? Thanks .

vishalsingh17: 2018-01-02 17:29:43

Plzz check my code submission id 20906763

Last edit: 2018-01-02 20:12:51
iet17_rv: 2018-01-02 14:49:03

thankyou @morass ....i got it....

morass: 2018-01-02 00:51:35

@iet17_rv: Happy new year to you too. I think you shall learn to debug... try some big input, for example 10^5 random characters and K==10^6

Last edit: 2018-01-02 01:57:00
iet17_rv: 2018-01-01 15:17:34

thank you ...n happy new year :) ...@morass ....plz check my code 20897989

morass: 2018-01-01 12:02:36

@et17_rv: Good day to you, Try for example:
1
bbaabbba 2

Good Luck & Wish You a Nice Day

iet17_rv: 2018-01-01 11:30:30

plz check my code..... submission id===20896987

morass: 2017-12-31 23:10:03

@nadstratosfer: To you too man! :)


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