Learn DSADSAContests
coding75 logo
LeetCode POTDPOTDSheets
coding75 Pro
coding75 logo
Dashboard
Learn DSA
Course RoadmapFlow
DSA Topic TreeNew πŸš€
Contest SolutionsPractice
Practice Sheets
MasterclassesLive πŸ‘¨πŸ»β€πŸ’»

coding75 ProPRO

Live Classes & Placement Guidance

coding75 logo

The premier developer platform for mastering Data Structures & Algorithms, exploring contest editorials, building ATS-ready resumes, and accelerating your tech career.

Connect & Community

DSA & Contests

  • Learn DSA
  • Contest Solutions
  • LeetCode POTDDaily
  • Practice Sheets
  • MasterclassesSoon

Interview Prep

  • Portfolio Projects
  • CS Fundamentals
  • System DesignSoon
  • Interview ExperiencesSoon
  • Mock InterviewsSoon

Career & Pro

  • Jobs & Internships
  • Resume BuilderATS
  • coding75 Pro
  • Submit Feedback
Β© 2026coding75β€’crackDSAβ„’β€’Maa Lalita Edtech Private Limited. All Rights Reserved.
Privacy PolicyTerms & ConditionsContact Support
Registered Office: Kanpur 208021β€’Regional Office: Indiranagar, Bangalore, 560008
Maa Lalita Edtech Private Limited
Contests/Codeforces/Educational Codeforces Round 183/C. Monocarp's String
PrevNext
CodeforcesCodeforces
Specialist
Educational Codeforces Round 183

C. Monocarp's String

HashingSliding WindowArrayString
Loading...
Solve on Codeforces

Problem Statement

Official algorithmic problem description and constraints.

Open on Codeforces

Monocarp has a string π‘ 

 of length π‘›

, consisting of the letters 'a' and 'b'. He wants to remove some (possibly zero) number of consecutive letters from his string in such a way that the number of letters 'a' and 'b' in the resulting string becomes equal. Monocarp can start removing letters from any position in the string π‘ 

.

Monocarp really likes his string π‘ 

, so he wants to remove as few consecutive letters from it as possible.

Your task is to determine the minimum number of consecutive letters from the string π‘ 

 that need to be removed so that the number of letters 'a' and 'b' in the resulting string becomes equal. If it is necessary to remove all letters from the string π‘ 

 (i.e., make it empty), report this.

Input


The first line contains one integer π‘‘

 (1≀𝑑≀10

4

) β€” the number of test cases.

Each test case consists of two lines:

  • the first line contains one integer π‘›
  •  (2≀𝑛≀2β‹…10
  • 5
  • ) β€” the number of characters in the string π‘ 
  • ;
  • the second line contains the string π‘ 
  •  of length π‘›
  • , consisting of the letters 'a' and/or 'b'.

Additional constraint on the input: the sum of values of π‘›

 over all test cases does not exceed 2β‹…10

5

.

Output


For each test case, print the answer as follows:

If in order to make the number of letters 'a' and 'b' equal, it is necessary to remove all letters from the string π‘ 

, output βˆ’1

.

Otherwise, output the minimum number of consecutive letters that Monocarp needs to remove from his string π‘ 

 so that the number of letters 'a' and 'b' becomes equal.

Example

Input

Copy

5
5
bbbab
6
bbaaba
4
aaaa
12
aabbaaabbaab
5
aabaa

Output

Copy

3
0
-1
2
-1

Note


In the first example, Monocarp needs to remove the first three letters from his string. After that, his string will become "ab". In this string, there is one letter 'a' and one letter 'b'.

In the second example, the given string has three letters 'a' and three letters 'b', so nothing needs to be removed.

In the third example, all letters of the string need to be removed, as there are no letters 'b', so βˆ’1

 should be printed.

In the fourth example, Monocarp can, for example, remove the fifth and sixth letters from his string. Then his string will become "aabbabbaab". In this string, there are five letters 'a' and five letters 'b'.

In the fifth example, all letters of the string need to be removed to make the number of letters 'a' and 'b' equal, so βˆ’1

 should be printed.

Solutions & Walkthrough

Detailed video explanations, mathematical intuition, and clean C++ implementation code.

Connecting secure classroom stream...
Back to Educational Codeforces Round 183
Previous ProblemNext Problem