3030: Repeated String

时间限制:2000 ms 内存限制:128 MB
上传者:
提交:3 通过:3

题目描述

qwb has talent for solving string problems.He obtains a lowercase string $A$ of length $n \times m$, and he wants to construct another lowercase string $B$ of the same length to make the distance from $A$ to $B$ as small as possible.Because of his laziness, he decides to construct $B$ in an easy way. Firstly, he constructs a string of length $n$, then he repeats it for $m$ times.

For two strings $A$ and $B$ with the same length $L$, their distance is defined as the following:
$$dist(A, B) = \sum_{i = 1}^{L} |A[i] - B[i]|$$
Tell qwb what's the minimum distance he can get.

输入格式

The first line contains an integer number T, the number of test cases.

For each test case :
The first line contains two integers $n$, $m$($1 \leq n \times m \leq 10^{5}$).

The next line contains a lowercase string $A$ of length $n \times m$.

输出格式

For each test case print the minimum distance.

输入样例 复制

2
1 6
abcdef
2 3
aabbcc

输出样例 复制

9
4