#include<stdio.h>
#include<iostream>
#include<limits.h>
#include<string.h>
char s[100001];
int n,flag;
int ind[100000];
inline void scan_f(int *a)
{
char c = 0;
while(c<33)
//c = fgetc_unlocked(stdin);
c = getc(stdin);
*a = 0;
while(c>33)
{
*a = (*a)*10 + c - '0';
//c = fgetc_unlocked(stdin);
c = getc(stdin);
}
}
bool checkregular(int l){
int status=0,newstat=0;
n=0;
flag=0;
for(int i=0;i<l;i++){
if(s[i]=='('){
if(i>0&&s[i-1]==')'&&status!=0){
ind[n]=i-1;
n++;
flag=1;
newstat=status;
}
status++;
}
else{
status--;
if(status<0)
return false;
else if((status==0&&flag==0)||(status==newstat&&flag==1)){
ind[n]=i;
n++;
}
else if(status==0&&flag==1){
flag=0;
newstat=0;
}
}
}
if(status==0)
return true;
return false;
}
int finkth(int k,int l){
int excluded;
if(k>2*n){
return -1;
}
else if(k<=n){
excluded=ind[k-1];
//printf("%d %d %d",k,n,ind[n-k]);
}
else{
int k2=k-n;
int i=ind[n-k2];
if(n==k2){
excluded=i/2;
//printf("%d %d %d %d %d",k,n,ind[n-k],k2);
}
else{
int j=ind[n-k2-1];
excluded=i-((i-j)/2);
}
}
return excluded;
}
int main(){
int l,k,res;
int t;
scan_f(&t);
while(t--){
scanf("%s",s);
l=strlen(s);
scan_f(&k);
if(checkregular(l)){
res=finkth(k,l);
if(res>=0){
for(int i=0;i<l;i++){
if(i!=res){
printf("%c",s[i]);
}
}
printf("\n");
}
else{
printf("%d\n",res);
}
}
else if(k==1){
printf("%s\n",s);
}
else{
printf("%d\n",-1);
}
}
return 0;
}
I2luY2x1ZGU8c3RkaW8uaD4KI2luY2x1ZGU8aW9zdHJlYW0+CiNpbmNsdWRlPGxpbWl0cy5oPgojaW5jbHVkZTxzdHJpbmcuaD4KCmNoYXIgc1sxMDAwMDFdOwppbnQgbixmbGFnOwppbnQgaW5kWzEwMDAwMF07CgppbmxpbmUgdm9pZCBzY2FuX2YoaW50ICphKQp7CiAgICBjaGFyIGMgPSAwOwogICAgd2hpbGUoYzwzMykKICAgICAgICAvL2MgPSBmZ2V0Y191bmxvY2tlZChzdGRpbik7CiAgICAgICAgYyA9IGdldGMoc3RkaW4pOwogICAgKmEgPSAwOwogICAgd2hpbGUoYz4zMykKICAgICAgICB7CiAgICAgICAgICAgICphID0gKCphKSoxMCArIGMgLSAnMCc7CiAgICAvL2MgPSBmZ2V0Y191bmxvY2tlZChzdGRpbik7CiAgICBjID0gZ2V0YyhzdGRpbik7CiAgICB9Cn0KCmJvb2wgY2hlY2tyZWd1bGFyKGludCBsKXsKICAgIGludCBzdGF0dXM9MCxuZXdzdGF0PTA7CiAgICBuPTA7CiAgICBmbGFnPTA7CiAgICBmb3IoaW50IGk9MDtpPGw7aSsrKXsKICAgICAgICBpZihzW2ldPT0nKCcpewogICAgICAgICAgICBpZihpPjAmJnNbaS0xXT09JyknJiZzdGF0dXMhPTApewogICAgICAgICAgICAgICAgaW5kW25dPWktMTsKICAgICAgICAgICAgICAgIG4rKzsKICAgICAgICAgICAgICAgIGZsYWc9MTsKICAgICAgICAgICAgICAgIG5ld3N0YXQ9c3RhdHVzOwogICAgICAgICAgICB9CiAgICAgICAgICAgIHN0YXR1cysrOwogICAgICAgIH0KICAgICAgICBlbHNlewogICAgICAgICAgICBzdGF0dXMtLTsKICAgICAgICAgICAgaWYoc3RhdHVzPDApCiAgICAgICAgICAgICAgICByZXR1cm4gZmFsc2U7CiAgICAgICAgICAgIGVsc2UgaWYoKHN0YXR1cz09MCYmZmxhZz09MCl8fChzdGF0dXM9PW5ld3N0YXQmJmZsYWc9PTEpKXsKICAgICAgICAgICAgICAgIGluZFtuXT1pOwogICAgICAgICAgICAgICAgbisrOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGVsc2UgaWYoc3RhdHVzPT0wJiZmbGFnPT0xKXsKICAgICAgICAgICAgICAgIGZsYWc9MDsKICAgICAgICAgICAgICAgIG5ld3N0YXQ9MDsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgIH0KICAgIGlmKHN0YXR1cz09MCkKICAgICAgICByZXR1cm4gdHJ1ZTsKICAgIHJldHVybiBmYWxzZTsKfQoKaW50IGZpbmt0aChpbnQgayxpbnQgbCl7CiAgICBpbnQgZXhjbHVkZWQ7CiAgICBpZihrPjIqbil7CiAgICAgICAgcmV0dXJuIC0xOwogICAgfQogICAgZWxzZSBpZihrPD1uKXsKICAgICAgICBleGNsdWRlZD1pbmRbay0xXTsKICAgICAgICAvL3ByaW50ZigiJWQgJWQgJWQiLGssbixpbmRbbi1rXSk7CiAgICB9CiAgICBlbHNlewogICAgICAgIGludCBrMj1rLW47CiAgICAgICAgaW50IGk9aW5kW24tazJdOwogICAgICAgIGlmKG49PWsyKXsKICAgICAgICAgICAgZXhjbHVkZWQ9aS8yOwogICAgICAgICAgICAvL3ByaW50ZigiJWQgJWQgJWQgJWQgJWQiLGssbixpbmRbbi1rXSxrMik7CiAgICAgICAgfQogICAgICAgIGVsc2V7CiAgICAgICAgICAgIGludCBqPWluZFtuLWsyLTFdOwogICAgICAgICAgICBleGNsdWRlZD1pLSgoaS1qKS8yKTsKICAgICAgICB9CiAgICB9CiAgICByZXR1cm4gZXhjbHVkZWQ7Cn0KCmludCBtYWluKCl7CiAgICBpbnQgbCxrLHJlczsKICAgIGludCB0OwogICAgc2Nhbl9mKCZ0KTsKICAgIHdoaWxlKHQtLSl7CiAgICAgICAgc2NhbmYoIiVzIixzKTsKICAgICAgICBsPXN0cmxlbihzKTsKICAgICAgICBzY2FuX2YoJmspOwogICAgICAgIGlmKGNoZWNrcmVndWxhcihsKSl7CiAgICAgICAgICAgIHJlcz1maW5rdGgoayxsKTsKICAgICAgICAgICAgaWYocmVzPj0wKXsKICAgICAgICAgICAgICAgIGZvcihpbnQgaT0wO2k8bDtpKyspewogICAgICAgICAgICAgICAgICAgIGlmKGkhPXJlcyl7CiAgICAgICAgICAgICAgICAgICAgICAgIHByaW50ZigiJWMiLHNbaV0pOwogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIHByaW50ZigiXG4iKTsKICAgICAgICAgICAgfQogICAgICAgICAgICBlbHNlewogICAgICAgICAgICAgICAgcHJpbnRmKCIlZFxuIixyZXMpOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIGVsc2UgaWYoaz09MSl7CiAgICAgICAgICAgIHByaW50ZigiJXNcbiIscyk7CiAgICAgICAgfQogICAgICAgIGVsc2V7CiAgICAgICAgICAgIHByaW50ZigiJWRcbiIsLTEpOwogICAgICAgIH0KICAgIH0KICAgIHJldHVybiAwOwp9
MTQKKCgpKSgpKCgoKSkpKCgpKQoxCigoKSkoKSgoKCkpKSgoKSkKMgooKCkpKCkoKCgpKSkoKCkpCjMKKCgpKSgpKCgoKSkpKCgpKQo0CigoKSkoKSgoKCkpKSgoKSkKNQooKCkpKCkoKCgpKSkoKCkpCjYKKCgpKSgpKCgoKSkpKCgpKQo3CigoKSkoKSgoKCkpKSgoKSkKOAooKCkpKCkoKCgpKSkoKCkpCjkKKCkKMgooKCkKMQooKCkKMgooKCkpCjIKKCgpKQoz
14
(())()((()))(())
1
(())()((()))(())
2
(())()((()))(())
3
(())()((()))(())
4
(())()((()))(())
5
(())()((()))(())
6
(())()((()))(())
7
(())()((()))(())
8
(())()((()))(())
9
()
2
(()
1
(()
2
(())
2
(())
3