{"trustable":true,"prependHtml":"\u003cstyle type\u003d\u0027text/css\u0027\u003e\n .input, .output {\n border: 1px solid #888888;\n }\n .output {\n margin-bottom: 1em;\n position: relative;\n top: -1px;\n }\n .output pre, .input pre {\n background-color: #EFEFEF;\n line-height: 1.25em;\n margin: 0;\n padding: 0.25em;\n }\n \u003c/style\u003e\n \u003clink rel\u003d\"stylesheet\" href\u003d\"//codeforces.org/s/96598/css/problem-statement.css\" type\u003d\"text/css\" /\u003e\n\u003cscript\u003e\n window.katexOptions \u003d {\n delimiters: [\n {left: \u0027$$$$$$\u0027, right: \u0027$$$$$$\u0027, display: true},\n {left: \u0027$$$\u0027, right: \u0027$$$\u0027, display: false},\n {left: \u0027$$\u0027, right: \u0027$$\u0027, display: true},\n {left: \u0027$\u0027, right: \u0027$\u0027, display: false}\n ]\n };\n\u003c/script\u003e\n","sections":[{"title":"","value":{"format":"HTML","content":"\u003cp\u003eMonocarp had a regular bracket sequence $$$s$$$ of length $$$n$$$ ($$$n$$$ is even). He even came up with his own way to calculate its cost.\u003c/p\u003e\u003cp\u003eHe knows that in a regular bracket sequence (RBS), each opening bracket is paired up with the corresponding closing bracket. So he decided to calculate the \u003cspan class\u003d\"tex-font-style-it\"\u003ecost\u003c/span\u003e of RBS as the sum of distances between pairs of corresponding bracket pairs.\u003c/p\u003e\u003cp\u003eFor example, let\u0027s look at RBS \u003cspan class\u003d\"tex-font-style-tt\"\u003e(())()\u003c/span\u003e. It has three pairs of brackets: \u003c/p\u003e\u003cul\u003e \u003cli\u003e \u003cspan class\u003d\"tex-font-style-tt\"\u003e(__)__\u003c/span\u003e: the distance between brackets at position $$$1$$$ and at $$$4$$$ is $$$4 - 1 \u003d 3$$$; \u003c/li\u003e\u003cli\u003e \u003cspan class\u003d\"tex-font-style-tt\"\u003e_()___\u003c/span\u003e: the distance is $$$3 - 2 \u003d 1$$$; \u003c/li\u003e\u003cli\u003e \u003cspan class\u003d\"tex-font-style-tt\"\u003e____()\u003c/span\u003e: the distance is $$$6 - 5 \u003d 1$$$. \u003c/li\u003e\u003c/ul\u003e So the cost of \u003cspan class\u003d\"tex-font-style-tt\"\u003e(())()\u003c/span\u003e is $$$3 + 1 + 1 \u003d 5$$$.\u003cp\u003eUnfortunately, due to data corruption, Monocarp lost all characters on odd positions $$$s_1, s_3, \\dots, s_{n-1}$$$. Only characters on even positions ($$$s_2, s_4, \\dots, s_{n}$$$) remain. For example, \u003cspan class\u003d\"tex-font-style-tt\"\u003e(())()\u003c/span\u003e turned to \u003cspan class\u003d\"tex-font-style-tt\"\u003e_(_)_)\u003c/span\u003e.\u003c/p\u003e\u003cp\u003eMonocarp wants to restore his RBS by placing brackets on the odd positions. But since the restored RBS may not be unique, he wants to choose one with \u003cspan class\u003d\"tex-font-style-bf\"\u003eminimum cost\u003c/span\u003e. It\u0027s too hard to do for Monocarp alone, so can you help him?\u003c/p\u003e\u003cp\u003eReminder: A \u003cspan class\u003d\"tex-font-style-it\"\u003eregular bracket sequence\u003c/span\u003e is a string consisting of only brackets, such that this sequence, when inserted \u003cspan class\u003d\"tex-font-style-tt\"\u003e1\u003c/span\u003e-s and \u003cspan class\u003d\"tex-font-style-tt\"\u003e+\u003c/span\u003e-s, gives a valid mathematical expression. For example, \u003cspan class\u003d\"tex-font-style-tt\"\u003e()\u003c/span\u003e, \u003cspan class\u003d\"tex-font-style-tt\"\u003e(())\u003c/span\u003e or \u003cspan class\u003d\"tex-font-style-tt\"\u003e(()())()\u003c/span\u003e are RBS, while \u003cspan class\u003d\"tex-font-style-tt\"\u003e)\u003c/span\u003e, \u003cspan class\u003d\"tex-font-style-tt\"\u003e()(\u003c/span\u003e or \u003cspan class\u003d\"tex-font-style-tt\"\u003e())(()\u003c/span\u003e are not.\u003c/p\u003e"}},{"title":"Input","value":{"format":"HTML","content":"\u003cp\u003eThe first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 5000$$$)\u0026nbsp;— the number of test cases. Next $$$t$$$ cases follow.\u003c/p\u003e\u003cp\u003eThe first line of each test case contains a single integer $$$n$$$ ($$$2 \\le n \\le 2 \\cdot 10^5$$$; $$$n$$$ is even)\u0026nbsp;— the length of string $$$s$$$.\u003c/p\u003e\u003cp\u003eThe second line of each test case contains a string $$$s$$$ of length $$$n$$$, where all characters on the odd positions are \u0027\u003cspan class\u003d\"tex-font-style-tt\"\u003e_\u003c/span\u003e\u0027 and all characters on the even positions are either \u0027\u003cspan class\u003d\"tex-font-style-tt\"\u003e(\u003c/span\u003e\u0027 or \u0027\u003cspan class\u003d\"tex-font-style-tt\"\u003e)\u003c/span\u003e\u0027.\u003c/p\u003e\u003cp\u003eAdditional constraints: \u003c/p\u003e\u003cul\u003e \u003cli\u003e $$$s$$$ can be restored to at least one regular bracket sequence; \u003c/li\u003e\u003cli\u003e the total sum of $$$n$$$ over all test cases doesn\u0027t exceed $$$2 \\cdot 10^5$$$. \u003c/li\u003e\u003c/ul\u003e"}},{"title":"Output","value":{"format":"HTML","content":"\u003cp\u003eFor each test case, print one integer\u0026nbsp;— the minimum cost of the regular bracket sequence that can be obtained from $$$s$$$ by replacing \u0027\u003cspan class\u003d\"tex-font-style-tt\"\u003e_\u003c/span\u003e\u0027-s with brackets.\u003c/p\u003e"}},{"title":"Examples","value":{"format":"HTML","content":"\u003ctable class\u003d\u0027vjudge_sample\u0027\u003e\n\u003cthead\u003e\n \u003ctr\u003e\n \u003cth\u003eInput\u003c/th\u003e\n \u003cth\u003eOutput\u003c/th\u003e\n \u003c/tr\u003e\n\u003c/thead\u003e\n\u003ctbody\u003e\n \u003ctr\u003e\n \u003ctd\u003e\u003cpre\u003e4\n6\n_(_)_)\n2\n_)\n8\n_)_)_)_)\n8\n_(_)_(_)\n\u003c/pre\u003e\u003c/td\u003e\n \u003ctd\u003e\u003cpre\u003e5\n1\n4\n8\n\u003c/pre\u003e\u003c/td\u003e\n \u003c/tr\u003e\n\u003c/tbody\u003e\n\u003c/table\u003e\n"}},{"title":"Note","value":{"format":"HTML","content":"\u003cp\u003eIn the first test case, it\u0027s optimal to make $$$s$$$ equal to \u003cspan class\u003d\"tex-font-style-tt\"\u003e(())()\u003c/span\u003e. The cost of $$$s$$$ will be equal to $$$3 + 1 + 1 \u003d 5$$$.\u003c/p\u003e\u003cp\u003eIn the second test case, the only option is to make $$$s$$$ equal to \u003cspan class\u003d\"tex-font-style-tt\"\u003e()\u003c/span\u003e with cost $$$1$$$.\u003c/p\u003e\u003cp\u003eIn the third test case, the only possible RBS is \u003cspan class\u003d\"tex-font-style-tt\"\u003e()()()()\u003c/span\u003e with cost $$$1 + 1 + 1 + 1 \u003d 4$$$.\u003c/p\u003e\u003cp\u003eIn the fourth test case, it\u0027s optimal to make $$$s$$$ equal to \u003cspan class\u003d\"tex-font-style-tt\"\u003e(())(())\u003c/span\u003e with cost $$$3 + 1 + 3 + 1 \u003d 8$$$.\u003c/p\u003e"}}]}