#!ruby -pal
i=j=d=0
t,a,b=$F
s=""
while a[i]&&b[j]
if a[i]==b[j] 
[s+=a[i],i+=1,j+=1]
else
c,k,p=a[i]==?(?[a,i,0]:[b,j,1]
d=1
t>?i&&s+=c[k]
k+=1
while d>0
c[k]==?(&&d+=1
c[k]==?)&&d-=1
t>?i&&s+=c[k]
k+=1
end
p==0?(i=k):j=k
end
end
$_=s