You are given
binary strings , each of length . You want to concatenate all the strings in some order to form a single large string of length . Find the minimum possible number of inversions the resulting string can have.
A binary string is defined as a string consisting only of '' and ''.
An inversion in a binary string is a pair of indices such that and = '', = ''. For example, the string "" contains inversions : , .
Note that you are not allowed to change the order of characters within any of the strings - you are only allowed to concatenate the strings themselves in whatever order you choose. For example, if you have "" and "" you can form "" and "", but not "".
Input Format
- The first line of input contains a single integer denoting the number of test cases. The description of test cases follows.
- The first line of each test case contains two space-separated integers .
- lines follow, the of which contains the string of length .
Output Format
For each test case, print a single line containing one integer - the minimum possible number of inversions in the resulting string.
Constraints
- contains only characters '' and ''.
- Sum of over all test cases does not exceed .
Sample Input 1
4
2 1
0
1
3 2
10
00
01
2 4
0010
1011
3 4
1011
1101
0101
Sample Output 1
0
2
3
11
Explanation
Test case : Two strings can be formed : "", = "". The first string does not have any inversion while the second has one inversion : .
Test case : Both "", "" contain inversions. It can be verified that no other order results in fewer inversions.
ConversionConversion EmoticonEmoticon