def main():
    n = int(input())
    strings = []
    for i in range(n):
        lenght = int(input())
        strings.append(input())

    for s in strings:
        last_symbol = s[0]
        good_blocks_len, block = [], [last_symbol]
        bad_symbols = 0

        for symbol in s[1:]:
            if last_symbol < symbol:
                good_blocks_len.append(len(block))
                bad_symbols += 1
                block = []
            last_symbol = symbol
            block.append(last_symbol)

        good_blocks_len.append(len(block))
        blocks_to_remove = sorted(good_blocks_len)[:bad_symbols]
        print(sum(blocks_to_remove))


if __name__ == '__main__':
    main()
