Given a string S, consisting only of ‘0’ and ‘1’, and also you are given an integer k. You are asked to find the sum of all unique substrings (in decimal notation) of length k in the string, such that string S is the repetition of these substrings. If the string S can’t be split into substrings of length k without leaving any characters in the string, print -1.

**Input:**

The first line consists of a single integer T denoting the number of test cases. Next T lines follow each of which consists of string S and an integer k.

**Output:**

For each test case, print in new line the sum as described in the problem. Print your answer modulo 10^9 + 7.

**Constraints:**

1 <= T <= 10^3

1 <= Length |String| <= 10^6

1 <= k <= 25, k<=Length |String|

**Example:**

**Input:**

2

010100010010 3

00100110 4

**Output:**

6

8

**Explanation:**

Case 1: Unique substrings are “010” and “100”. The required sum is 2+4=6.

Case 2: Unique substrings are "0010" and "0110". Hence, the sum = 2 + 6 = 8.

Author: sajalagrawal

If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.

Sulagna | 117 |

RUTVIKHARIPARA | 116 |

janvidavda105190 | 93 |

harshsiddhapura105461 | 93 |

devsinghindra | 80 |

PiyushPandey4 | 614 |

ASWATHAMA | 561 |

akhyasharma01 | 547 |

john_wick | 521 |

jagrit_07 | 443 |

blackshadows | 5362 |

Ibrahim Nash | 5242 |

akhayrutdinov | 5111 |

mb1973 | 4929 |

Quandray | 4598 |

Login to report an issue on this page.