Read problems statements in Mandarin Chinese and Russian as well. 

Chef has recently learnt about sequences of parentheses. These are special sequences that contain only the characters '(' and ')'.
A regular parentheses sequence follows the following definition:  

An empty sequence is regular
If S is a regular sequence, then (S) is also regular
If A and B represent two regular sequences, then their concatenation AB is also regular.
Therefore, the sequences (), ()() and (())() are regular, while ()(, ) and ))() are non-regular.
Now, you need to find the longest subsequence of the given sequence which is non-regular. Amongst all such distinct answers, output the lexicographically K^{th} amongst them. If the number of distinct subsequences with maximum length is less than K, please output -1 instead.

------ Input: ------ 

The first line contains a single integer T, denoting the number of test cases to follow.

Each of the test cases have exactly two lines, the first contains the parentheses sequence and the second contains the value of K.

------ Output: ------ 

Output exactly T lines, each containing answer to the corresponding query.

------ Constraints: ------ 

$1 ≤ T ≤ 10$
$1 ≤ |S| ≤ 10^{5}$
$1 ≤ K ≤ 10^{9}$

------ Notes: ------ 

$A subsequence of a given sequence A is a non-empty sequence obtained by removing zero or more characters from A. It does not need to be contiguous.$
$A sequence A is called different from another sequence B, if there exists an integer k, such that 1 ≤ k ≤ N (N  is the length of both sequences), and A_{k} != B_{k}.$
$Consider two different N-length sequences, A and B. Let k be the smallest integer such that A[k] != B[k] and 1 ≤ k ≤ N. If A[k] < B[k], then A is said to be lexicographically smaller than B, and vice-versa.$

----- Sample Input 1 ------ 
5
()
2
(()
1
(()
2
(())
2
(())
3
----- Sample Output 1 ------ 
)
(()
-1
())
-1

----- explanation 1 ------ 
Case 1:
Following are the subsequences:  

Length   Subsequence   Regular/Non-Regular

1			(			Non-regular
1			)			Non-regular
2			()			Regular

There are two non-regular subsequences of equal length:'(' and ')'.
We are asked for the lexicographically 2nd, so output should be ')'.
Case 2:
Following are the subsequences:  

Length   Subsequence   Regular/Non-Regular
1			(			Non-Regular
1			)			Non-Regular
2			((			Non-Regular
2			()			Regular
3			(()			Non-Regular

In this case, there are non-regular subsequences of lengths 1, 2, and 3. But, as 3 is the maximum among these, we choose, (().
Case 3:
The string is same as Case 2, and we realize that there is only one subsequence of the maximum length 3, thus we must output -1.
Case 4:
We can find that following subsequences have maximum length and are non-regular.

Length 	Subsequence
3			())	
3			(()

In lexicographical order, we have following subsequences: [ ((), ()) ]
The query asks for 2nd subsequence, thus the required output is ()).
Case 5:
This is the same sequence as last case, and we know that there are exactly 2 distinct subsequences of maximum length. Thus, answer should be -1.

Difficulty: HARD
Tags: Data structures
Skill types: Data structures

Starter code:

Solutions:
def bal(s):
	st = []
	for i in s:
		if i == '(':
			st.append(i)
		elif st == []:
			return -1
		else:
			st.pop()
	return st != []

def fun(s, k):
	n = len(s)
	for i in range(n - 1):
		if s[i] == ')' and s[i + 1] == '(':
			k -= 1
			if k == 0:
				return i
	if k == 1:
		return n - 1
	k -= 1
	for i in range(n - 1, 0, -1):
		if s[i] == '(' and s[i - 1] == ')':
			k -= 1
			if k == 0:
				return i
	if k == 1:
		return 0
	return -1
for _ in range(int(input())):
	s = input()
	k = int(input())
	if bal(s):
		if k == 1:
			print(s)
		else:
			print(-1)
	else:
		i = fun(s, k)
		if i == -1:
			print(-1)
		else:
			s = s[:i] + s[i + 1:]
			print(s)


def ba(s):
	sta = []
	for i in s:
		if i == '(':
			sta.append(i)
		elif sta == []:
			return 1
		else:
			sta.pop()
	return sta != []

def fun(s, k):
	l = len(s)
	for j in range(l - 1):
		if s[j] == ')' and s[j + 1] == '(':
			k -= 1
			if k == 0:
				return j
	if k == 1:
		return l - 1
	k -= 1
	for j in range(l - 1, 0, -1):
		if s[j] == '(' and s[j - 1] == ')':
			k -= 1
			if k == 0:
				return j
	if k == 1:
		return 0
	return -1
for _ in range(int(input())):
	s = input()
	k = int(input())
	if ba(s):
		if k == 1:
			print(s)
		else:
			print(-1)
	else:
		i = fun(s, k)
		if i == -1:
			print(-1)
		else:
			s = s[:i] + s[i + 1:]
			print(s)


def is_regular(s):
	if len(s) % 2 == 1:
		return False
	stack = 0
	for x in s:
		if x == '(':
			stack += 1
		else:
			if stack == 0:
				return False
			stack -= 1
	return stack == 0

def give_substring_index(s, k):
	l = len(s)
	for i in range(l - 1):
		if s[i] == ')' and s[i + 1] == '(':
			k -= 1
		if k == 0:
			return i
	if k == 1:
		return l - 1
	k -= 1
	for i in range(l - 1, 0, -1):
		if s[i] == '(' and s[i - 1] == ')':
			k -= 1
		if k == 0:
			return i
	if k == 1:
		return 0
	return -1
for t in range(int(input())):
	s = input()
	K = int(input())
	if not is_regular(s):
		if K == 1:
			print(s)
		else:
			print(-1)
	else:
		index = give_substring_index(s, K)
		if index == -1:
			print(-1)
		else:
			print(s[:index] + s[index + 1:])


def regular(ps):
	lv = 0
	for c in ps:
		if c == '(':
			lv += 1
		elif c == ')':
			lv -= 1
			if lv < 0:
				return False
		else:
			return False
	return lv == 0
for _ in range(int(input())):
	pstr = input().strip()
	lstr = len(pstr)
	k = int(input())
	if k > lstr:
		print(-1)
	elif regular(pstr):
		rle = []
		lc = '('
		run = 0
		for c in pstr:
			if c == lc:
				run += 1
			else:
				rle.append(run)
				lc = c
				run = 1
		rle.append(run)
		if k > len(rle):
			print(-1)
		else:
			if k > len(rle) / 2:
				run = 2 * (len(rle) - k)
			else:
				run = 2 * k - 1
			pos = sum(rle[:run])
			print(pstr[:pos] + pstr[pos + 1:])
	elif k == 1:
		print(pstr)
	else:
		print(-1)


for _ in range(int(input())):
	s = input().strip()
	k = int(input())
	sm = alters = 0
	prev = None
	for i in s:
		if i != prev:
			alters += 1
		prev = i
		if i == '(':
			sm += 1
		else:
			sm -= 1
		if sm < 0:
			break
	if sm != 0:
		if k != 1:
			print(-1)
		else:
			print(s)
	elif alters < k:
		print(-1)
	else:
		(lo, hi) = (0, alters)
		prev = None
		for i in range(len(s)):
			if prev == '(' and s[i] == ')':
				lo += 1
			elif prev == ')' and s[i] == '(':
				hi -= 1
			prev = s[i]
			if lo == k or hi == k:
				print(s[:i] + s[i + 1:])
				break