F - G
UVALive - 7454 #include <iostream> #include <bits/stdc++.h> using namespace std; int main() { int n,t,ans; char s[105]; stack<char>stk; cin>>t; while(t--) { ans=0; cin>>n>>s; for(int i=0; s[i]; i++) { if(!stk.empty()&&s[i]==')'&&stk.top()=='(') stk.pop(); else if(s[i]=='(') stk.push(s[i]); else { ans++; stk.push('('); } } if(!stk.empty()) ans+=stk.size()/2; while(!stk.empty()) { stk.pop(); } cout<<ans<<endl; } return 0; }