CF BUDDY
← Problems·

1520E · Arranging The Sheep

1400 · greedy, math

Problem: You are playing the game "Arranging The Sheep". The goal of this game is to make the sheep line up. The level in the game is described by a string of length nn, consisting of the characters '.' (empty space) and '*' (sheep). In one move, you can move any sheep one square to the left or one square to the right, if the corresponding square exists and is empty. The game ends as soon as the sheep are lined up, that is, there should be no empty cells between any sheep.

For example, if n=6n=6 and the level is described by the string "**.*..", then the following game scenario is possible:

  • the sheep at the 44 position moves to the right, the state of the level: "**..*.";
  • the sheep at the 22 position moves to the right, the state of the level: "..*.";
  • the sheep at the 11 position moves to the right, the state of the level: ".**.*.";
  • the sheep at the 33 position moves to the right, the state of the level: ".*.**.";
  • the sheep at the 22 position moves to the right, the state of the level: "..***.";
  • the sheep are lined up and the game ends.

For a given level, determine the minimum number of moves you need to make to complete the level.

Input Format: The first line contains one integer tt (1t1041 \le t \le 10^4). Then tt test cases follow.

The first line of each test case contains one integer nn (1n1061 \le n \le 10^6).

The second line of each test case contains a string of length nn, consisting of the characters '.' (empty space) and '*' (sheep) — the description of the level.

It is guaranteed that the sum of nn over all test cases does not exceed 10610^6.

Output Format: For each test case output the minimum number of moves you need to make to complete the level.

Sample Cases

Case 1

Input

5
6
**.*..
5
*****
3
.*.
3
...
10
*.*...*.**

Output

1
0
0
0
9

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback