

The first line of input contains a single integer T, representing the number of test cases or queries to be run. Then the T test cases follow.
The first line and only line of each test case contain a string STR containing just two characters ‘(‘ and ‘)’.
For every test case, print an integer denoting the length of the longest balanced substring.
The output of each test case is printed in a separate line.
You do not need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= |STR| <= 5 * 10^4
Where |STR| is the length of the string.
Time Limit: 1 sec
In this approach, we will consider every substring and check if it can be the longest balanced substring.
Below is the detailed approach:
To check if a string is balanced or not, follow the below steps:
In this approach, for each index, we will find the length of the longest balanced substring starting at this index. Because, for any index i, if there is an index j such that substring(i, j) is not balanced, then for any index k greater than j, substring(i,k) is also not balanced.
Note: N denotes the length of the given string.
In this approach, for each index, we will find the length of the longest balanced substring ending at this index.
For example, if the given string is “()()”,
How will the stack work?
For example, if the given string is “())()”,
Note: ‘N’ denotes the length of the given string.
In this approach, we will use dynamic programming to find the longest balanced substring.
Following is the detailed algorithm:
In this approach, we will use a similar algorithm that we used in Approach 1 to check whether a string is balanced or not.