Under construction

Still being built. Things may change

Red and Blue Dots 2

Time limit1sMemory limit1024 MB

Summary
Given N points on a circle, each red or blue, find the fewest crossing pairs among same-color chords that touch every point at least once.
Level

Hard8 of 10

Topics
Dynamic programming, Geometry, Greedy
Solved
No attempts yet

Problem

Points on a circle are placed at equal intervals, NN points in total. Each point is red or blue, and there are at least two red points and at least two blue points. Connect the points with chords so that every point is joined to at least one chord. Each chord must join two points of the same color.

The goal is to minimize the number of pairs of chords that cross at a point that is not an endpoint of either chord. Given the colors of the NN points in clockwise order, output the minimum number of crossing pairs over all valid ways to connect every point to at least one chord.

Input

The first line contains the number of test cases TT. Then TT test cases follow, one after another (1≤T≤1 4831 \le T \le 1\,483).

The first line of each test case contains an integer NN (4≤N≤500 0004 \le N \le 500\,000).

The next line contains a string of length NN. The ii-th character of the string is the color of the point that is ii steps clockwise from a point chosen arbitrarily as the reference. R means a red point and B means a blue point. The number of R and the number of B are each guaranteed to be at least 2.

The sum of NN over all test cases does not exceed 5 000 0005\,000\,000.

Output

For each test case, print a line "Case #CC", where CC is the test case number. On the next line, print the minimum number of pairs of chords that cross.

Examples1

  1. Example 1

    Input
    2
    4
    RBRB
    9
    RRBBRRRBR
    
    Expected output
    Case #1
    1
    Case #2
    0