function solve() {
   var n = Number(read());
   var s = read();
   var min = 0;
   var sum = 0;
   for (var i = 0; i < n; i++) {
      sum += s[i] === '(' ? 1 : -1;
      if (sum < min) {
         min = sum;
      }
   }
   write(0 - min);
}

function init() {
   var T;
   T = 1;
   T = Number(read());
   for (var t = 0; t < T; t++) {
      solve();
   }
}

var isNode = typeof console !== 'undefined';
var MEMORY = [];
var MEMORY_INDEX = 0;
if (isNode) {
   var fs = require('fs');
   var path = require('path');

   var inTextName;
   var outTextName;
   if (process.argv.length === 5) {
      inTextName = process.argv[3];
      outTextName = process.argv[4];
   }

   if (inTextName) {
      fs.readFile(path.join(__dirname, inTextName), 'utf8', (err, data) => {
         MEMORY = data.split('\r\n');
         init();
      });
   } else {
      var RL = require('readline').createInterface({
         input : process.stdin,
         output : process.stdout
      });

      RL.on('line', (line) => {
         MEMORY.push(line);
      });

      RL.on('close', init);
   }
} else {
   init();
}

function write(value) {
   isNode ? console.log(value) : print(value);
}
function read() {
   return isNode ? MEMORY[MEMORY_INDEX++] : readline();
}


#include<iostream>
#include<utility>
#include<vector>
#include<algorithm>
#include<stack>
#include<queue>
#include<string>
#include<list>
#include<math.h>
#include<map>
#include<set>
#include<unordered_set>
#include<unordered_map>
#include<climits>
#include<list>
using namespace std;
#define      pb             push_back
#define      vi             vector<int>
#define      vll            vector<long long>
#define      mp             make_pair
#define      pii            pair<int,int>
#define      pll            pair<long long,long long>
#define      case           int t;cin>>t;while(t--)
#define      ff             first
#define      ss             second
#define      MOD            1000000007
#define      mii            map<long long,long long>
#define      vllitr           vector<long long> :: iterator
#define      mllitr           map<long,long> :: iterator
typedef long long int ll;

void input()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}

int main()
{
   input();
        case
        {
            int n;cin>>n;
            string s;cin>>s;
            int a =0,b=0,c=0,o=0;

            // if(s == "()"){cout<<0<<endl;continue;}
            // else if(s == ")("){cout<<1<<endl;continue;}
            int j ,k;
            for(int i=0;i<n;i++)
                if(s[i]==')'){a++;j=i;}
                else break;
            for(int i=n-1;i>=0;i--)
                if(s[i]=='('){b++;k=i;}
                else break;

            for(int i=0;i<n;i++)
            {
                if(s[i]=='(')o++;
                else if(o>0){o--;}
                else c++;
            }

            int move =0 ;

            move += min(a,b);
            c-=a;o-=b;
            int left = o+c - (max(a,b)-min(a,b));
            move += max(a,b)-min(a,b);
            move+= 2*left;

            cout<<move<<endl;

        }
return 0;

}


#include<iostream>
#include<utility>
#include<vector>
#include<algorithm>
#include<stack>
#include<queue>
#include<string>
#include<list>
#include<math.h>
#include<map>
#include<set>
#include<unordered_set>
#include<unordered_map>
#include<climits>
#include<list>
using namespace std;
#define      pb             push_back
#define      vi             vector<int>
#define      vll            vector<long long>
#define      mp             make_pair
#define      pii            pair<int,int>
#define      pll            pair<long long,long long>
#define      case           int t;cin>>t;while(t--)
#define      ff             first
#define      ss             second
#define      MOD            1000000007
#define      mii            map<long long,long long>
#define      vllitr           vector<long long> :: iterator
#define      mllitr           map<long,long> :: iterator
typedef long long int ll;

void input()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}

int main()
{
   input();
        case
        {
            int n;cin>>n;
            string s;cin>>s;
            int a =0,b=0,c=0,o=0;

            // if(s == "()"){cout<<0<<endl;continue;}
            // else if(s == ")("){cout<<1<<endl;continue;}
            for(int i=0;i<n;i++)
                if(s[i]==')'){a++;}
                else break;
            for(int i=n-1;i>=0;i--)
                if(s[i]=='('){b++;}
                else break;

            for(int i=0;i<n;i++)
            {
                if(s[i]=='(')o++;
                else if(o>0){o--;}
                else c++;
            }

            int move = o+c - max(a,b);

            cout<<move<<endl;

        }
return 0;

}


#include<iostream>
#include<utility>
#include<vector>
#include<algorithm>
#include<stack>
#include<queue>
#include<string>
#include<list>
#include<math.h>
#include<map>
#include<set>
#include<unordered_set>
#include<unordered_map>
#include<climits>
#include<list>
using namespace std;
#define      pb             push_back
#define      vi             vector<int>
#define      vll            vector<long long>
#define      mp             make_pair
#define      pii            pair<int,int>
#define      pll            pair<long long,long long>
#define      case           int t;cin>>t;while(t--)
#define      ff             first
#define      ss             second
#define      MOD            1000000007
#define      mii            map<long long,long long>
#define      vllitr           vector<long long> :: iterator
#define      mllitr           map<long,long> :: iterator
typedef long long int ll;

void input()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}

int main()
{
   input();
        case
        {
            int n;cin>>n;
            string s;cin>>s;
            int a =0,b=0,c=0,o=0;

            // if(s == "()"){cout<<0<<endl;continue;}
            // else if(s == ")("){cout<<1<<endl;continue;}
            for(int i=0;i<n;i++)
                if(s[i]==')'){a++;}
                else break;
            for(int i=n-1;i>=0;i--)
                if(s[i]=='('){b++;}
                else break;

            for(int i=0;i<n;i++)
            {
                if(s[i]=='(')o++;
                else if(o>0){o--;}
                else c++;
            }

            cout<<o<<endl;

        }
return 0;

}


#include<iostream>
#include<utility>
#include<vector>
#include<algorithm>
#include<stack>
#include<queue>
#include<string>
#include<list>
#include<math.h>
#include<map>
#include<set>
#include<unordered_set>
#include<unordered_map>
#include<climits>
#include<list>
using namespace std;
#define      pb             push_back
#define      vi             vector<int>
#define      vll            vector<long long>
#define      mp             make_pair
#define      pii            pair<int,int>
#define      pll            pair<long long,long long>
#define      case           int t;cin>>t;while(t--)
#define      ff             first
#define      ss             second
#define      MOD            1000000007
#define      mii            map<long long,long long>
#define      vllitr           vector<long long> :: iterator
#define      mllitr           map<long,long> :: iterator
typedef long long int ll;

void input()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}

int main()
{
   input();
        case
        {
            int n;cin>>n;
            string s;cin>>s;
            int a =0,b=0,c=0,o=0;

            // if(s == "()"){cout<<0<<endl;continue;}
            // else if(s == ")("){cout<<1<<endl;continue;}

            for(int i=0;i<n;i++)
                if(s[i]==')')a++;
                else break;
            for(int i=n-1;i>=0;i--)
                if(s[i]=='(')b++;
                else break;

            for(int i=0;i<n;i++)
            {
                if(s[i]=='(')o++;
                else if(o>0){o--;}
                else c++;
            }

            int move = 0 ;
            if(a== c && b==o){cout<<a<<endl;continue;}
            move += a;
            int left = o+c-a-b;
            move += 2*left;
            cout<<left<<endl;

        }
return 0;

}


#include<iostream>
#include<utility>
#include<vector>
#include<algorithm>
#include<stack>
#include<queue>
#include<string>
#include<list>
#include<math.h>
#include<map>
#include<set>
#include<unordered_set>
#include<unordered_map>
#include<climits>
#include<list>
using namespace std;
#define      pb             push_back
#define      vi             vector<int>
#define      vll            vector<long long>
#define      mp             make_pair
#define      pii            pair<int,int>
#define      pll            pair<long long,long long>
#define      case           int t;cin>>t;while(t--)
#define      ff             first
#define      ss             second
#define      MOD            1000000007
#define      mii            map<long long,long long>
#define      vllitr           vector<long long> :: iterator
#define      mllitr           map<long,long> :: iterator
typedef long long int ll;

void input()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}

int main()
{
   input();
        case
        {
            int n;cin>>n;
            string s;cin>>s;
            int a =0,b=0,c=0,o=0;

            // if(s == "()"){cout<<0<<endl;continue;}
            // else if(s == ")("){cout<<1<<endl;continue;}
            int j ,k;
            for(int i=0;i<n;i++)
                if(s[i]==')'){a++;j=i;}
                else break;
            for(int i=n-1;i>=0;i--)
                if(s[i]=='('){b++;k=i;}
                else break;

            for(int i=0;i<n;i++)
            {
                if(s[i]=='(')o++;
                else if(o>0){o--;}
                else c++;
            }

            int move =0 ;

            if(b>a)
            {
                move += b;
                move += 2*(o-b);
            }
            else
            {
                move += a;
                move += 2*(c-a);
            }

            cout<<move<<endl;

        }
return 0;

}


#include<iostream>
#include<utility>
#include<vector>
#include<algorithm>
#include<stack>
#include<queue>
#include<string>
#include<list>
#include<math.h>
#include<map>
#include<set>
#include<unordered_set>
#include<unordered_map>
#include<climits>
#include<list>
using namespace std;
#define      pb             push_back
#define      vi             vector<int>
#define      vll            vector<long long>
#define      mp             make_pair
#define      pii            pair<int,int>
#define      pll            pair<long long,long long>
#define      case           int t;cin>>t;while(t--)
#define      ff             first
#define      ss             second
#define      MOD            1000000007
#define      mii            map<long long,long long>
#define      vllitr           vector<long long> :: iterator
#define      mllitr           map<long,long> :: iterator
typedef long long int ll;

void input()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}

int main()
{
   input();
        case
        {
            int n;cin>>n;
            string s;cin>>s;
            int a =0,b=0,c=0,o=0;

            // if(s == "()"){cout<<0<<endl;continue;}
            // else if(s == ")("){cout<<1<<endl;continue;}
            for(int i=0;i<n;i++)
                if(s[i]==')'){a++;}
                else break;
            for(int i=n-1;i>=0;i--)
                if(s[i]=='('){b++;}
                else break;

            for(int i=0;i<n;i++)
            {
                if(s[i]=='(')o++;
                else if(o>0){o--;}
                else c++;
            }

            int move = max(a,b);

            cout<<move<<endl;

        }
return 0;

}


#include<iostream>
#include<utility>
#include<vector>
#include<algorithm>
#include<stack>
#include<queue>
#include<string>
#include<list>
#include<math.h>
#include<map>
#include<set>
#include<unordered_set>
#include<unordered_map>
#include<climits>
#include<list>
using namespace std;
#define      pb             push_back
#define      vi             vector<int>
#define      vll            vector<long long>
#define      mp             make_pair
#define      pii            pair<int,int>
#define      pll            pair<long long,long long>
#define      case           int t;cin>>t;while(t--)
#define      ff             first
#define      ss             second
#define      MOD            1000000007
#define      mii            map<long long,long long>
#define      vllitr           vector<long long> :: iterator
#define      mllitr           map<long,long> :: iterator
typedef long long int ll;

void input()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}

int main()
{
   input();
        case
        {
            int n;cin>>n;
            string s;cin>>s;
            int a =0,b=0,c=0,o=0;

            // if(s == "()"){cout<<0<<endl;continue;}
            // else if(s == ")("){cout<<1<<endl;continue;}
            int j ,k;
            for(int i=0;i<n;i++)
                if(s[i]==')'){a++;j=i;}
                else break;
            for(int i=n-1;i>=0;i--)
                if(s[i]=='('){b++;k=i;}
                else break;

            for(int i=0;i<n;i++)
            {
                if(s[i]=='(')o++;
                else if(o>0){o--;}
                else c++;
            }

            if(a== c && b==o){cout<<a<<endl;continue;}
            int move = 2*(o+c);
            move-=3*(a+b);

            cout<<move<<endl;

        }
return 0;

}


#include<iostream>
#include<utility>
#include<vector>
#include<algorithm>
#include<stack>
#include<queue>
#include<string>
#include<list>
#include<math.h>
#include<map>
#include<set>
#include<unordered_set>
#include<unordered_map>
#include<climits>
#include<list>
using namespace std;
#define      pb             push_back
#define      vi             vector<int>
#define      vll            vector<long long>
#define      mp             make_pair
#define      pii            pair<int,int>
#define      pll            pair<long long,long long>
#define      case           int t;cin>>t;while(t--)
#define      ff             first
#define      ss             second
#define      MOD            1000000007
#define      mii            map<long long,long long>
#define      vllitr           vector<long long> :: iterator
#define      mllitr           map<long,long> :: iterator
typedef long long int ll;

void input()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}

int main()
{
   input();
        case
        {
            int n;cin>>n;
            string s;cin>>s;
            int a =0,b=0,c=0,o=0;

            // if(s == "()"){cout<<0<<endl;continue;}
            // else if(s == ")("){cout<<1<<endl;continue;}

            for(int i=0;i<n;i++)
                if(s[i]==')')a++;
                else break;
            for(int i=n-1;i>=0;i--)
                if(s[i]=='(')b++;
                else break;

            for(int i=0;i<n;i++)
            {
                if(s[i]=='(')o++;
                else if(o>0){o--;}
                else c++;
            }

            int move = 0 ;
            if(a== c && b==o){cout<<a<<endl;continue;}
            move += a+b;
            int left = o+c-a-b;
            move += 2*left;
            cout<<left<<endl;

        }
return 0;

}


#include<iostream>
#include<cstring>
using namespace std;
int main(){
    int t;
    cin>>t;
    for(int i=0;i<t;i++)
    {
        int n;
        string s;
        cin>>n;

        cin>>s;
        int open=0,end=0,steps=0;
        while(1)
        {
            if(open==n/2){
                cout<<steps<<endl;
                break;
            }

            if(s[j]==')' && open<=end)
            {
                steps++;
            }
            else if(s[j]==')')
            end++;

            else if(s[j]=='(')
            open++;

        }
    }

}


#include<iostream>
#include<cstring>
using namespace std;
int main(){
    int t;
    cin>>t;
    for(int i=0;i<t;i++)
    {
        int n;
        string s;
        cin>>n;
        cin>>s;
        int open=0,end=0,steps=0;
        for(int j=0;j<s.length();j++)
        {
            if(open==n/2){
                cout<<steps<<endl;
                break;
            }

            if(s[j]==')' && open<=end)
            {
                steps++;
                open--;
                n=n-2;
            }
            else if(s[j]==')')
            end++;

            else if(s[j]=='(')
            open++;

        }
    }

}


#include<iostream>
#include<cstring>
using namespace std;
int main(){
    int t;
    cin>>t;
    for(int i=0;i<t;i++)
    {
        int n;
        string s;
        cin>>n;
        cin>>s;
        int open=0,end=0,steps=0;
        for(int j=0;j<s.length();j++)
        {
            if(open==n/2){
                cout<<steps<<endl;
                break;
            }

            if(s[j]==')' && open<=end)
            {
                steps++;
            }
            else if(s[j]==')')
            end++;

            else if(s[j]=='(')
            open++;

        }
    }

}


#include<iostream>
#include<cstring>
using namespace std;
int main(){
    int t;
    cin>>t;
    for(int i=0;i<t;i++)
    {
        int n;
        string s;
        cin>>n;
        cin>>s;
        int open=0,end=0,steps=0;
        for(int j=0;j<s.length();j++)
        {
            if(open==n/2){
                cout<<steps<<endl;
                break;
            }

            if(s[j]==')' && open<=end)
            {
                steps+=1;
                open-=1;
                n=n-2;
            }
            else if(s[j]==')')
            end+=1;

            else if(s[j]=='(')
            open+=1;

        }
    }

}


#include<iostream>
#include<string>
using namespace std;
int main(){
    int t;
    cin>>t;
    for(int i=0;i<t;i++)
    {
        int n;
        string s;
        cin>>n;
        cin>>s;
        int open=0,end=0,steps=0;
        for(int j=0;j<s.length();j++)
        {
            if(open==n/2){
                cout<<steps<<endl;
                break;
            }

            if(s[j]==')' && open<=end)
            {
                steps++;
                open--;
            }
            else if(s[j]==')')
            end++;

            else if(s[j]=='(')
            open++;

        }
    }

}


#include<iostream>
#include<cstring>
using namespace std;
int main(){
    int t;
    cin>>t;
    for(int i=0;i<t;i++)
    {
        int n;
        string s;
        cin>>n;
        cin>>s;
        int open=0,end=0,steps=0;
        for(int j=0;j<s.length();j++)
        {

            if(s[j]==')' && open<=end)
            {
                steps++;
            }
            else if(s[j]==')')
            end++;

            else if(s[j]=='(')
            open++;

        }
        cout<<steps<<endl;
    }

}


#include <bits/stdc++.h>

using namespace std;

int main() {
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);
#endif

	int t;
	cin >> t;
	while (t--) {
		int n;
		string s;
		cin >> n >> s;
		int ans = 0;
		int bal = 0;
		for (int i = 0; i < n; ++i) {
			if (s[i] == '(') ++bal;
			else {
				--bal;
				if (bal < 0) {
					bal = 0;
					++ans;
				}
			}
		}
		cout << ans << endl;
	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main()
{
	#ifndef ONLINE_JUDGE
			freopen("input.txt","r",stdin);
			freopen("output.txt","w",stdout);
	#endif
	long long int t;
	cin>>t;
	while(t--)
	{
		long long int n,b=0,ans=0;
		cin>>n;
		string arr;
		cin>>arr;
		for(long long int i=0;i<n;++i)
		{
			if(arr[i] == '(')
			{
				++b;
			}else{
				--b;
				if(b < 0)
					{
						b=0;
						++ans;
					}
			}
		}
		cout<<ans<<endl;
	}
}


#include <bits/stdc++.h>
using namespace std;

int main()
{
	#ifndef ONLINE_JUDGE
			freopen("input.txt","r",stdin);
			freopen("output.txt","w",stdout);
	#endif
	long long int t;
	cin>>t;
	while(t--)
	{
		long long int n,b=0,ans=0;
		cin>>n;
		string arr;
		for(long long int i=0;i<n;++i)
		{
			cin>>arr[i];
			if(arr[i] == '(')
			{
				++b;
			}else{
				--b;
				if(b < 0)
					{
						b=0;
						++ans;
					}
			}
		}
		cout<<ans<<endl;
	}
}


#include <bits/stdc++.h>
using namespace std;

int main()
{
	#ifndef ONLINE_JUDGE
			freopen("input.txt","r",stdin);
			freopen("output.txt","w",stdout);
	#endif
	int t;
	cin>>t;
	while(t--)
	{
		int n,b=0,ans=0;
		cin>>n;
		string arr;
		for(int i=0;i<n;i++)
		{
			cin>>arr[i];
			if(arr[i] == '(')
			{
				b++;
			}else{
				--b;
				if(b < 0)
					{
						b=0;
						++ans;
					}
			}
		}
		cout<<ans<<endl;
	}
}


#include <bits/stdc++.h>
using namespace std;

int main()
{
	#ifndef ONLINE_JUDGE
			freopen("input.txt","r",stdin);
			freopen("output.txt","w",stdout);
	#endif
	long long int t;
	cin>>t;
	while(t--)
	{
		long long int n,b=0,ans=0;
		cin>>n;
		string arr;
		for(long long int i=0;i<n;i++)
		{
			cin>>arr[i];
			if(arr[i] == '(')
			{
				b++;
			}else{
				--b;
				if(b < 0)
					{
						b=0;
						++ans;
					}
			}
		}
		cout<<ans<<endl;
	}
}


#include <iostream>
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);i++)
using ll = long long;

using namespace std;

int main()
{
   int t;
   cin>>t;
   while(t--){
       int n;
       cin>>n;
       string s;
       cin>>s;

       int nu = 0;

       rep(i,n){
           if(s[i]=='('){
               nu++;
           }
           else{
               if(nu>0){
                   nu--;
               }
           }
       }
       cout<<nu<<endl;

   }

   return 0;
}


#include <iostream>
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);i++)
using ll = long long;

using namespace std;

int main()
{
   int t;
   cin>>t;
   while(t--){
       int n;
       cin>>n;
       string s;
       cin>>s;

       int num = 0;

       rep(i,n){
           if(s[i]=='('){
               num++;
           }
           else{
               if(num>0){
                   num--;
               }
           }
       }
       cout<<num<<endl;

   }

   return 0;
}


#include <iostream>
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);i++)
using ll = long long;

using namespace std;

int main()
{
   int t;
   cin>>t;
   while(t--){
       int n;
       cin>>n;
       string s;
       cin>>s;

       int nu1 = 0;

       rep(i,n){
           if(s[i]=='('){
               nu1++;
           }
           else{
               if(nu1>0){
                   nu1--;
               }
           }
       }
       cout<<nu1<<endl;

   }

   return 0;
}


#include <iostream>

using namespace std;

int main()
{
    int t;
    cin>>t;
    while(t--){
       int n;
       cin>>n;
       char a[n];
       string s;
       cin>>s;
       int j=-1;
       for(int i=0;i<n;i++){
           if((s[i]==')')&&(a[j]=='(')&&(j>=0)){
               j--;
           }
           else{
               j++;
               a[j]=s[i];
           }
       }
       j++;
       j=j/2;
       cout<<j<<endl;
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{

   int t;
   cin>>t;
   while (t--)
   {
       int n;
       cin>>n;
       string s;
       cin>>s;
       int open=0;
       int close=0;
       int index;
       int vist[n]={0};
       for (int i = 0; i < n; i++)
       {

           if(s[i]=='('&&i!=n-1)
           {
               while (s[i]!=')'&&i<n)
               {
                    open++;

                    i++;

               }
                //cout<<"open= "<<open<<endl;

               if(s[i]==')')
               {
                    index=i;
                    //cout<<index<<endl;

                     while (s[i]!='('&&i<n)
                  {
                    close++;
                    i++;

                   }
               }
                //cout<<"close= "<<close<<endl;

               for (int j = index; j < index+min(open,close); j++)
               {
                   vist[j]=1;
               }
                for (int k =index; k>(index)-min(open,close) ; k--)
               {
                   vist[k-1]=1;
               }

               i=index+min(open,close)-1;

             // i--;
           }
           open=0;
           close=0;

       }
      int flag=0;
      int ans=0;
       for (int i = 0; i < n; i++)
       {
           if(vist[i]==0)
           {
               ans++;
               flag=1;
           }
           //cout<<vist[i]<<" ";
       }
       if(flag==0)  cout<<0<<endl;
       else  cout<<ans/2<<endl;

   }

    }


#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{

   int t;
   cin>>t;
   while (t--)
   {
       int n;
       cin>>n;
       string s;
       cin>>s;
       int open=0;
       int close=0;
       int index;
       int vist[n]={0};
       for (int i = 0; i < n; i++)
       {

           if(s[i]=='('&&i<n)
           {
               while (s[i]!=')'&&i<n)
               {
                    open++;

                    i++;

               }
                //cout<<"open= "<<open<<endl;

               if(s[i]==')')
               {
                    index=i;
                    //cout<<index<<endl;

                     while (s[i]!='('&&i<n)
                  {
                    close++;
                    i++;

                   }
               }
                //cout<<"close= "<<close<<endl;

               for (int j = index; j < index+min(open,close); j++)
               {
                   vist[j]=1;
               }
                for (int k =index; k>(index)-min(open,close) ; k--)
               {
                   vist[k-1]=1;
               }

               i=index+min(open,close)-1;

             // i--;
           }
           open=0;
           close=0;

       }
      int flag=0;
      int ans=0;
       for (int i = 0; i < n; i++)
       {
           if(vist[i]==0)
           {
               ans++;
               flag=1;
           }
           //cout<<vist[i]<<" ";
       }
       if(flag==0)  cout<<0<<endl;
       else  cout<<ans/2<<endl;

   }

    }


#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{

   int t;
   cin>>t;
   while (t--)
   {
       int n;
       cin>>n;
       string s;
       cin>>s;
       int sum=0;
       int ans=0;
       for (int i = 0; i < n; i++)
       {
           if(s[i]=='(')
           {
               sum++;
           }
           else
           {
               sum--;
           }
           if(sum<0)
           {
              ans++;
              sum=0;
           }

       }
       cout<<ans<<endl;

   }

    }


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t,i,l,j,coun,coun2;
    string s;
    cin>>t;
    while(t--)
    {
        cin>>l>>s;
        coun=0,coun2=0;
        int mn=INT_MAX;
        for(i=0;i<l;i++)
        {
            if(s[i]=='(')
            {
                coun++;
            }
            else if(s[i]==')')
            {
                coun--;
            }
            mn=min(mn,coun);
        }
        cout<<abs(mn)<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t,n;
    string s;
    cin>>t;
    while(t--)
    {
        cin>>n>>s;
        int i,l=s.size();
        int c1=0,c2=0;
        for(i=0;i<l;i++)
        {
            if(s[i]=='(')
            {
                c1++;
            }
            else if(s[i]==')'&&c1>0)
            {
                c1--;
                c2++;
            }
        }
        cout<<c1<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    string s;
    cin>>t;
    while(t--)
    {
        cin>>s;
        int i,l=s.size();
        int c1=0,c2=0;
        for(i=0;i<l;i++)
        {
            if(s[i]=='(')
            {
                c1++;
            }
            else if(s[i]==')'&&c1>0)
            {
                c1--;
                c2++;
            }
        }
        cout<<c1<<endl;
    }
}


#include<stack>
#include <iostream>
#include<cmath>
using namespace std;

int main()
{
int t;cin>>t;
while(t--)
{
     int n;
    cin>>n;

    string s;
    cin>>s;

    stack<char> st;

    for(int i=0;i<n;i++)
    {
        if(!st.empty() && st.top()=='(' && s[i]==')')
        {
            st.pop();
        }
        else
        st.push(s[i]);

    }

    int count1=0;int count2=0;
    while(!(st.empty()))
    {
        char ch=st.top();
        if(ch=='(')
        count1++;
        else
        count2++;

        st.pop();
    }

   cout<<min(count1,count2)<<endl;

}
    return 0;
}


#include<stack>
#include <iostream>
#include<cmath>
using namespace std;

int main()
{
int t;cin>>t;
while(t--)
{
     int n;
    cin>>n;

    string s;
    cin>>s;

    stack<char> st;

    for(int i=0;i<n;i++)
    {
        if(!st.empty() && st.top()=='(' && s[i]==')')
        {
            st.pop();
        }
        else
        st.push(s[i]);

    }

    int i=0;

    int count=0;
    while(s[i]==')')
    {
        count++; i++;
    }

    int count2=0;
     i=n-1;
     while(s[i]=='(')
    {
        count2++; i--;
    }

    cout<< st.size()-max(count,count2)<<endl;

}
    return 0;
}


#include<stack>
#include <iostream>
#include<cmath>
using namespace std;

int main()
{
long long int t;cin>>t;
while(t--)
{
     int n;
    cin>>n;

    string s;
    cin>>s;

    stack<char> st;

    for(long long int i=0;i<n;i++)
    {
        if(st.size()!=0 && st.top()=='(' && s[i]==')')
        {
            st.pop();
        }
        else
        st.push(s[i]);

    }

   long long int i=0;

   long long int count=0;
    while(i<n && s[i]==')' &&count<2)
    {
        count++; i++;
    }

   long long int count2=0;
     i=n-1;
     while(i>=0 && s[i]=='(' && count2<2)
    {
        count2++; i--;
    }

   int mid=st.size()-count-count2;

    int ans= st.size();

    if(mid==0)
    {
      cout<<2*(st.size()-count) +count;
    }
    else{
        if(count==0 && count2==0)
    cout<<st.size();
    else if(min(count,count2)==0)
    cout<<st.size()-1;
    else cout<<st.size()-2;

    }

   cout<<endl;

}
    return 0;
}


#include<stack>
#include <iostream>
#include<cmath>
using namespace std;

int main()
{
int t;cin>>t;
while(t--)
{
     int n;
    cin>>n;

    string s;
    cin>>s;

    stack<char> st;

    for(int i=0;i<n;i++)
    {
        if(!st.empty() && st.top()=='(' && s[i]==')')
        {
            st.pop();
        }
        else
        st.push(s[i]);

    }

    int i=0;

    int count=0;
    while(s[i]==')')
    {
        count++; i++;
    }

    int count2=0;
     i=n-1;
     while(s[i]=='(')
    {
        count2++; i--;
    }

    int mid=st.size()-count-count2;

    if(mid==count+count2)
    cout<<mid;
    else if(mid<count+count2)
    {
        cout<<mid+ (count+count2-mid)/2;
    }
    else
    cout<<count+count2 + 2*(mid-(count+count2));

   cout<<endl;

}
    return 0;
}


#include <bits/stdc++.h>
#include <algorithm>
#include <cmath>

using namespace std;
#define fio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define ll long long
#define auto vector<ll>::iterator
#define auto1 set<ll>::iterator
#define auto2 map<ll,ll>::iterator
int main()
{
    fio;
    ll n,i,t;char c=' ';ll a[n];ll min=n;
    for(i=0;i<t;i++)
    {
        cin>>n;
        string s="";
        for(ll j=0;j<n;j++)
        {
            cin>>c;
            s+=c;
        }
        memset(a,0,sizeof(a));
        for(ll j=0;j<n;j++)
        {
            if(s[i]==')')
            a[i]=a[i-1]-1;
            else
            a[i]=a[i-1]+1;
        }
        min=n;
        for(ll j=0;j<n;j++)
        {
            if(min>a[i])
            min=a[i];
        }
        if(a[n-1]==0)
        cout<<min<<"\n";
        else
        cout<<"-1"<<"\n";
    }
}


#include <bits/stdc++.h>
#include <algorithm>
#include <cmath>

using namespace std;
#define fio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define ll long long
#define auto vector<ll>::iterator
#define auto1 set<ll>::iterator
#define auto2 map<ll,ll>::iterator
int main()
{
    fio;
    ll n,i,t;char c=' ';ll a[n];ll min=n;
    for(i=0;i<t;i++)
    {
        cin>>n;
        string s="";
        for(ll j=0;j<n;j++)
        {
            cin>>c;
            s+=c;
        }
        memset(a,0,sizeof(a));
        if(s[0]==")")
        a[0]=-1;
        else
        a[0]=1;
        for(ll j=1;j<n;j++)
        {
            if(s[i]==')')
            a[i]=a[i-1]-1;
            else
            a[i]=a[i-1]+1;
        }
        min=n;
        for(ll j=0;j<n;j++)
        {
            if(min>a[i])
            min=a[i];
        }
        if(a[n-1]==0)
        cout<<min<<"\n";
        else
        cout<<"-1"<<"\n";
    }
}


#include <bits/stdc++.h>
#include <algorithm>
#include <cmath>

using namespace std;
#define fio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define ll long long
#define auto vector<ll>::iterator
#define auto1 set<ll>::iterator
#define auto2 map<ll,ll>::iterator
int main()
{
    fio;
    ll n,i,t;char c=' ';cin>>t;
    for(i=0;i<t;i++)
    {
        cin>>n;ll min=n;ll a[n];
        string s="";
        for(ll j=0;j<n;j++)
        {
            cin>>c;
            s+='c';
        }
        memset(a,0,sizeof(a));
        if(s[0]==')')
        a[0]=-1;
        else
        a[0]=1;
        for(ll j=1;j<n;j++)
        {
            if(s[j]==')')
            a[j]=a[j-1]-1;
            else
            a[j]=a[j-1]+1;
        }
        min=n;
        for(ll j=0;j<n;j++)
        {
            if(min>a[j])
            min=a[j];
        }
        if(a[n-1]==0)
        cout<<min<<"\n";
        else
        cout<<"-1"<<"\n";
    }
}


#include <bits/stdc++.h>
#include <algorithm>
#include <cmath>

using namespace std;
#define fio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define ll long long
#define auto vector<ll>::iterator
#define auto1 set<ll>::iterator
#define auto2 map<ll,ll>::iterator
int main()
{
    fio;
    ll n,i,t;char c=' ';cin>>t;
    for(i=0;i<t;i++)
    {
        cin>>n;ll min=n;ll a[n];
        string s="";
        for(ll j=0;j<n;j++)
        {
            cin>>c;
            s+=c;
        }
        memset(a,0,sizeof(a));
        if(s[0]==')')
        a[0]=-1;
        else
        a[0]=1;
        for(ll j=1;j<n;j++)
        {
            if(s[j]==')')
            a[j]=a[j-1]-1;
            else
            a[j]=a[j-1]+1;
        }
        min=n;
        for(ll j=0;j<n;j++)
        {
            if(min>a[j])
            min=a[j];
        }
        if(a[n-1]==0)
        cout<<abs(min)<<"\n";
        else
        cout<<"-1"<<"\n";
    }
}


//#include<bits/stdc++.h>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<deque>
#include<stack>
#include<vector>
#include<set>
#include<unordered_set>
#include<list>
#include<map>
#include<iterator>
#include<algorithm>
#include<utility>
#include<functional>
#include<numeric>
using namespace std;
typedef long long int ll;
typedef long double ld;
#define nl "\n"
#define NL co<<nl
#define vd " "
#define F first
#define S second
#define fo(a,b) for(i=a;i<b;i++)
#define Fo(a,b) for(i=a;i>=b;i--)
#define fo1(a,b) for(j=a;j<b;j++)
#define Fo1(a,b) for(j=a;j>=b;j--)
#define fo2(a,b,c) for(i=a;i<=b;i+=c)
#define Fo2(a,b,c) for(i=a;i<=b;i-=c)
#define pb push_back
#define mp make_pair
#define popf pop_front
#define popb pop_back
#define eb emplace_back
#define ins insert
#define all(v)  v.begin(),v.end()
#define allr(v) v.rbegin(),v.rend()
#define tc(T) while(T--)
#define vec vector
#define co cout
#define d(x) co<<to_string(x)<<vd<<x<<vd
#define yes co<<"YES\n"
#define no co<<"NO\n"
#define yes2 co<<"Yes\n"
#define no2 co<<"No\n"
#define stol(s) stoll(s,nullptr,10)
#define vll vec<ll>
#define usll unordered_set<ll>
#define mll map<ll,ll>
#define mpll map<pair<ll,ll>,ll>
#define msl map<string,ll>
#define vpll vec<pair<ll,ll>>
#define spll set<pair<ll,ll>>
#define in(a,n) fo(0,n){cin>>a[i];}
#define vin(a,n) fo(0,n){cin>>q;a.eb(q);}
#define vpin(a,n) fo(0,n){cin>>q>>w;a.pb({q,w});}
#define vpout(b,n) fo(0,n){co<<b[i].F<<vd<<b[i].S<<nl;}
#define out(a,n) fo(0,n){cout<<a[i]<<vd;}
#define in2d(a,n,m) fo(0,n){for(j=0;j<m;j++){cin>>a[i][j];}}
#define out2d(a,n,m) fo(0,n){for(j=0;j<m;j++){co<<a[i][j]<<vd;} co<<nl;}
#define MX(a) *max_element(a,a+n)
#define MN(a) *min_element(a,a+n)
#define VMX(a) *max_element(all(a))
#define VMN(a) *min_element(all(a))
#define lowb(a,x) (std::lower_bound(a.begin(),a.end(),x)-a.begin())
#define upb(a,x) (std::upper_bound(a.begin(),a.end(),x)-a.begin())
#define beg begin()
#define rbeg rbegin()
#define ren rend()
#define en end()
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) a*b/gcd(a,b)
ll T=1,inf=100000000000000000,mod=1000000007;
ll ncr(ll n,ll r)
{
    ll l=n,f2=1,l2=n-r,i=1,j=1;
    if (r != 0||r!=n)
    {
        while (l2!=0||l!=r)
        {
            i*= l;
            j*= l2;
            ll m = gcd(i,j);
            i/= m;
            j/= m;
            l--;
            l2--;
        }
    }
    else i = 1;
    return i;
}
ll dig_sum(ll n) {
    ll v=n,sum=0,i;
    while(v) {
        sum+=v%10;
        v/=10;
    }
    return sum;

}
bool ckprime(ll n) {
    if(n%2==0&&n!=2) return 0;
    else if(n==2) return 1;
    else {
        ll v=sqrt(n),flag=1,i;
        for(i=3; i<=v; i+=2) {
            if(n%i==0) {
                flag=0;
                break;
            }
        }
        return flag;
    }

}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    //cin >> T;
    while(T--)
    {
        ll n,sz=1,ans=0,blag=0,u,mn,ev=0,mx,mini,i,v,q,p,j,k,w,h,l,m,x,y,od=0,total=0,cc=0,pp=0,flag=0,klag=0,sum=0;
        string s,s1;
        bool ok=1;
        cin>>n>>s;
        fo(0,n) {
            x=cc;
            if(s[i]=='(') cc++;
            else cc--;
            if(cc<0||(cc==0&&x<0)) {
                ans++;
            }
            co<<ans/2<<nl;

        }
    }
    return 0;
}


//#include<bits/stdc++.h>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<deque>
#include<stack>
#include<vector>
#include<set>
#include<unordered_set>
#include<list>
#include<map>
#include<iterator>
#include<algorithm>
#include<utility>
#include<functional>
#include<numeric>
using namespace std;
typedef long long int ll;
typedef long double ld;
#define nl "\n"
#define NL co<<nl
#define vd " "
#define F first
#define S second
#define fo(a,b) for(i=a;i<b;i++)
#define Fo(a,b) for(i=a;i>=b;i--)
#define fo1(a,b) for(j=a;j<b;j++)
#define Fo1(a,b) for(j=a;j>=b;j--)
#define fo2(a,b,c) for(i=a;i<=b;i+=c)
#define Fo2(a,b,c) for(i=a;i<=b;i-=c)
#define pb push_back
#define mp make_pair
#define popf pop_front
#define popb pop_back
#define eb emplace_back
#define ins insert
#define all(v)  v.begin(),v.end()
#define allr(v) v.rbegin(),v.rend()
#define tc(T) while(T--)
#define vec vector
#define co cout
#define d(x) co<<to_string(x)<<vd<<x<<vd
#define yes co<<"YES\n"
#define no co<<"NO\n"
#define yes2 co<<"Yes\n"
#define no2 co<<"No\n"
#define stol(s) stoll(s,nullptr,10)
#define vll vec<ll>
#define usll unordered_set<ll>
#define mll map<ll,ll>
#define mpll map<pair<ll,ll>,ll>
#define msl map<string,ll>
#define vpll vec<pair<ll,ll>>
#define spll set<pair<ll,ll>>
#define in(a,n) fo(0,n){cin>>a[i];}
#define vin(a,n) fo(0,n){cin>>q;a.eb(q);}
#define vpin(a,n) fo(0,n){cin>>q>>w;a.pb({q,w});}
#define vpout(b,n) fo(0,n){co<<b[i].F<<vd<<b[i].S<<nl;}
#define out(a,n) fo(0,n){cout<<a[i]<<vd;}
#define in2d(a,n,m) fo(0,n){for(j=0;j<m;j++){cin>>a[i][j];}}
#define out2d(a,n,m) fo(0,n){for(j=0;j<m;j++){co<<a[i][j]<<vd;} co<<nl;}
#define MX(a) *max_element(a,a+n)
#define MN(a) *min_element(a,a+n)
#define VMX(a) *max_element(all(a))
#define VMN(a) *min_element(all(a))
#define lowb(a,x) (std::lower_bound(a.begin(),a.end(),x)-a.begin())
#define upb(a,x) (std::upper_bound(a.begin(),a.end(),x)-a.begin())
#define beg begin()
#define rbeg rbegin()
#define ren rend()
#define en end()
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) a*b/gcd(a,b)
ll T=1,inf=100000000000000000,mod=1000000007;
ll ncr(ll n,ll r)
{
    ll l=n,f2=1,l2=n-r,i=1,j=1;
    if (r != 0||r!=n)
    {
        while (l2!=0||l!=r)
        {
            i*= l;
            j*= l2;
            ll m = gcd(i,j);
            i/= m;
            j/= m;
            l--;
            l2--;
        }
    }
    else i = 1;
    return i;
}
ll dig_sum(ll n) {
    ll v=n,sum=0,i;
    while(v) {
        sum+=v%10;
        v/=10;
    }
    return sum;

}
bool ckprime(ll n) {
    if(n%2==0&&n!=2) return 0;
    else if(n==2) return 1;
    else {
        ll v=sqrt(n),flag=1,i;
        for(i=3; i<=v; i+=2) {
            if(n%i==0) {
                flag=0;
                break;
            }
        }
        return flag;
    }

}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> T;
    while(T--)
    {
        ll n,sz=1,ans=0,blag=0,u,mn,ev=0,mx,mini,i,v,q,p,j,k,w,h,l,m,x,y,od=0,total=0,cc=0,pp=0,flag=0,klag=0,sum=0;
        string s,s1;
        bool ok=1;
        cin>>n>>s;
        fo(0,n) {
            x=cc;
            if(s[i]=='(') cc++;
            else cc--;
            if(cc<0||(cc==0&&x<0)) {
                ans++;
            }

        }
        co<<ans/2<<nl;
    }
    return 0;
}


//#include<bits/stdc++.h>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<deque>
#include<stack>
#include<vector>
#include<set>
#include<unordered_set>
#include<list>
#include<map>
#include<iterator>
#include<algorithm>
#include<utility>
#include<functional>
#include<numeric>
using namespace std;
typedef long long int ll;
typedef long double ld;
#define nl "\n"
#define NL co<<nl
#define vd " "
#define F first
#define S second
#define fo(a,b) for(i=a;i<b;i++)
#define Fo(a,b) for(i=a;i>=b;i--)
#define fo1(a,b) for(j=a;j<b;j++)
#define Fo1(a,b) for(j=a;j>=b;j--)
#define fo2(a,b,c) for(i=a;i<=b;i+=c)
#define Fo2(a,b,c) for(i=a;i<=b;i-=c)
#define pb push_back
#define mp make_pair
#define popf pop_front
#define popb pop_back
#define eb emplace_back
#define ins insert
#define all(v)  v.begin(),v.end()
#define allr(v) v.rbegin(),v.rend()
#define tc(T) while(T--)
#define vec vector
#define co cout
#define d(x) co<<to_string(x)<<vd<<x<<vd
#define yes co<<"YES\n"
#define no co<<"NO\n"
#define yes2 co<<"Yes\n"
#define no2 co<<"No\n"
#define stol(s) stoll(s,nullptr,10)
#define vll vec<ll>
#define usll unordered_set<ll>
#define mll map<ll,ll>
#define mpll map<pair<ll,ll>,ll>
#define msl map<string,ll>
#define vpll vec<pair<ll,ll>>
#define spll set<pair<ll,ll>>
#define in(a,n) fo(0,n){cin>>a[i];}
#define vin(a,n) fo(0,n){cin>>q;a.eb(q);}
#define vpin(a,n) fo(0,n){cin>>q>>w;a.pb({q,w});}
#define vpout(b,n) fo(0,n){co<<b[i].F<<vd<<b[i].S<<nl;}
#define out(a,n) fo(0,n){cout<<a[i]<<vd;}
#define in2d(a,n,m) fo(0,n){for(j=0;j<m;j++){cin>>a[i][j];}}
#define out2d(a,n,m) fo(0,n){for(j=0;j<m;j++){co<<a[i][j]<<vd;} co<<nl;}
#define MX(a) *max_element(a,a+n)
#define MN(a) *min_element(a,a+n)
#define VMX(a) *max_element(all(a))
#define VMN(a) *min_element(all(a))
#define lowb(a,x) (std::lower_bound(a.begin(),a.end(),x)-a.begin())
#define upb(a,x) (std::upper_bound(a.begin(),a.end(),x)-a.begin())
#define beg begin()
#define rbeg rbegin()
#define ren rend()
#define en end()
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) a*b/gcd(a,b)
ll T=1,inf=100000000000000000,mod=1000000007;
ll ncr(ll n,ll r)
{
    ll l=n,f2=1,l2=n-r,i=1,j=1;
    if (r != 0||r!=n)
    {
        while (l2!=0||l!=r)
        {
            i*= l;
            j*= l2;
            ll m = gcd(i,j);
            i/= m;
            j/= m;
            l--;
            l2--;
        }
    }
    else i = 1;
    return i;
}
ll dig_sum(ll n) {
    ll v=n,sum=0,i;
    while(v) {
        sum+=v%10;
        v/=10;
    }
    return sum;

}
bool ckprime(ll n) {
    if(n%2==0&&n!=2) return 0;
    else if(n==2) return 1;
    else {
        ll v=sqrt(n),flag=1,i;
        for(i=3; i<=v; i+=2) {
            if(n%i==0) {
                flag=0;
                break;
            }
        }
        return flag;
    }

}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> T;
    while(T--)
    {
        ll n,sz=1,ans=0,blag=0,u,mn,ev=0,mx,mini,i,v,q,p,j,k,w,h,l,m,x,y,od=0,total=0,cc=0,pp=0,flag=0,klag=0,sum=0;
        string s,s1;
        bool ok=1;
        cin>>n>>s;
        fo(0,n) {
            x=cc;
            if(s[i]=='(') cc++;
            else cc--;
            if(cc<0&&s[i]==')') {
                ans++;cc=0;
            }

        }
        co<<ans<<nl;
    }
    return 0;
}


//#include<bits/stdc++.h>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<deque>
#include<stack>
#include<vector>
#include<set>
#include<unordered_set>
#include<list>
#include<map>
#include<iterator>
#include<algorithm>
#include<utility>
#include<functional>
#include<numeric>
using namespace std;
typedef long long int ll;
typedef long double ld;
#define nl "\n"
#define NL co<<nl
#define vd " "
#define F first
#define S second
#define fo(a,b) for(i=a;i<b;i++)
#define Fo(a,b) for(i=a;i>=b;i--)
#define fo1(a,b) for(j=a;j<b;j++)
#define Fo1(a,b) for(j=a;j>=b;j--)
#define fo2(a,b,c) for(i=a;i<=b;i+=c)
#define Fo2(a,b,c) for(i=a;i<=b;i-=c)
#define pb push_back
#define mp make_pair
#define popf pop_front
#define popb pop_back
#define eb emplace_back
#define ins insert
#define all(v)  v.begin(),v.end()
#define allr(v) v.rbegin(),v.rend()
#define tc(T) while(T--)
#define vec vector
#define co cout
#define d(x) co<<to_string(x)<<vd<<x<<vd
#define yes co<<"YES\n"
#define no co<<"NO\n"
#define yes2 co<<"Yes\n"
#define no2 co<<"No\n"
#define stol(s) stoll(s,nullptr,10)
#define vll vec<ll>
#define usll unordered_set<ll>
#define mll map<ll,ll>
#define mpll map<pair<ll,ll>,ll>
#define msl map<string,ll>
#define vpll vec<pair<ll,ll>>
#define spll set<pair<ll,ll>>
#define in(a,n) fo(0,n){cin>>a[i];}
#define vin(a,n) fo(0,n){cin>>q;a.eb(q);}
#define vpin(a,n) fo(0,n){cin>>q>>w;a.pb({q,w});}
#define vpout(b,n) fo(0,n){co<<b[i].F<<vd<<b[i].S<<nl;}
#define out(a,n) fo(0,n){cout<<a[i]<<vd;}
#define in2d(a,n,m) fo(0,n){for(j=0;j<m;j++){cin>>a[i][j];}}
#define out2d(a,n,m) fo(0,n){for(j=0;j<m;j++){co<<a[i][j]<<vd;} co<<nl;}
#define MX(a) *max_element(a,a+n)
#define MN(a) *min_element(a,a+n)
#define VMX(a) *max_element(all(a))
#define VMN(a) *min_element(all(a))
#define lowb(a,x) (std::lower_bound(a.begin(),a.end(),x)-a.begin())
#define upb(a,x) (std::upper_bound(a.begin(),a.end(),x)-a.begin())
#define beg begin()
#define rbeg rbegin()
#define ren rend()
#define en end()
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) a*b/gcd(a,b)
ll T=1,inf=100000000000000000,mod=1000000007;
ll ncr(ll n,ll r)
{
    ll l=n,f2=1,l2=n-r,i=1,j=1;
    if (r != 0||r!=n)
    {
        while (l2!=0||l!=r)
        {
            i*= l;
            j*= l2;
            ll m = gcd(i,j);
            i/= m;
            j/= m;
            l--;
            l2--;
        }
    }
    else i = 1;
    return i;
}
ll dig_sum(ll n) {
    ll v=n,sum=0,i;
    while(v) {
        sum+=v%10;
        v/=10;
    }
    return sum;

}
bool ckprime(ll n) {
    if(n%2==0&&n!=2) return 0;
    else if(n==2) return 1;
    else {
        ll v=sqrt(n),flag=1,i;
        for(i=3; i<=v; i+=2) {
            if(n%i==0) {
                flag=0;
                break;
            }
        }
        return flag;
    }

}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> T;
    while(T--)
    {
        ll n,sz=1,ans=0,blag=0,u,mn,ev=0,mx,mini,i,v,q,p,j,k,w,h,l,m,x,y,od=0,total=0,cc=0,pp=0,flag=0,klag=0,sum=0;
        string s,s1;
        bool ok=1;
        cin>>n>>s;
        fo(0,n) {
            x=cc;
            if(s[i]=='(') cc++;
            else cc--;
            if(cc<0&&s[i]==')') {
                ans++;
            }

        }
        co<<ans<<nl;
    }
    return 0;
}


//#include<bits/stdc++.h>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<deque>
#include<stack>
#include<vector>
#include<set>
#include<unordered_set>
#include<list>
#include<map>
#include<iterator>
#include<algorithm>
#include<utility>
#include<functional>
#include<numeric>
using namespace std;
typedef long long int ll;
typedef long double ld;
#define nl "\n"
#define NL co<<nl
#define vd " "
#define F first
#define S second
#define fo(a,b) for(i=a;i<b;i++)
#define Fo(a,b) for(i=a;i>=b;i--)
#define fo1(a,b) for(j=a;j<b;j++)
#define Fo1(a,b) for(j=a;j>=b;j--)
#define fo2(a,b,c) for(i=a;i<=b;i+=c)
#define Fo2(a,b,c) for(i=a;i<=b;i-=c)
#define pb push_back
#define mp make_pair
#define popf pop_front
#define popb pop_back
#define eb emplace_back
#define ins insert
#define all(v)  v.begin(),v.end()
#define allr(v) v.rbegin(),v.rend()
#define tc(T) while(T--)
#define vec vector
#define co cout
#define d(x) co<<to_string(x)<<vd<<x<<vd
#define yes co<<"YES\n"
#define no co<<"NO\n"
#define yes2 co<<"Yes\n"
#define no2 co<<"No\n"
#define stol(s) stoll(s,nullptr,10)
#define vll vec<ll>
#define usll unordered_set<ll>
#define mll map<ll,ll>
#define mpll map<pair<ll,ll>,ll>
#define msl map<string,ll>
#define vpll vec<pair<ll,ll>>
#define spll set<pair<ll,ll>>
#define in(a,n) fo(0,n){cin>>a[i];}
#define vin(a,n) fo(0,n){cin>>q;a.eb(q);}
#define vpin(a,n) fo(0,n){cin>>q>>w;a.pb({q,w});}
#define vpout(b,n) fo(0,n){co<<b[i].F<<vd<<b[i].S<<nl;}
#define out(a,n) fo(0,n){cout<<a[i]<<vd;}
#define in2d(a,n,m) fo(0,n){for(j=0;j<m;j++){cin>>a[i][j];}}
#define out2d(a,n,m) fo(0,n){for(j=0;j<m;j++){co<<a[i][j]<<vd;} co<<nl;}
#define MX(a) *max_element(a,a+n)
#define MN(a) *min_element(a,a+n)
#define VMX(a) *max_element(all(a))
#define VMN(a) *min_element(all(a))
#define lowb(a,x) (std::lower_bound(a.begin(),a.end(),x)-a.begin())
#define upb(a,x) (std::upper_bound(a.begin(),a.end(),x)-a.begin())
#define beg begin()
#define rbeg rbegin()
#define ren rend()
#define en end()
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) a*b/gcd(a,b)
ll T=1,inf=100000000000000000,mod=1000000007;
ll ncr(ll n,ll r)
{
    ll l=n,f2=1,l2=n-r,i=1,j=1;
    if (r != 0||r!=n)
    {
        while (l2!=0||l!=r)
        {
            i*= l;
            j*= l2;
            ll m = gcd(i,j);
            i/= m;
            j/= m;
            l--;
            l2--;
        }
    }
    else i = 1;
    return i;
}
ll dig_sum(ll n) {
    ll v=n,sum=0,i;
    while(v) {
        sum+=v%10;
        v/=10;
    }
    return sum;

}
bool ckprime(ll n) {
    if(n%2==0&&n!=2) return 0;
    else if(n==2) return 1;
    else {
        ll v=sqrt(n),flag=1,i;
        for(i=3; i<=v; i+=2) {
            if(n%i==0) {
                flag=0;
                break;
            }
        }
        return flag;
    }

}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> T;
    while(T--)
    {
        ll n,sz=1,ans=0,blag=0,u,mn,ev=0,mx,mini,i,v,q,p,j,k,w,h,l,m,x,y,od=0,total=0,cc=0,pp=0,flag=0,klag=0,sum=0;
        string s,s1;
        bool ok=1;
        cin>>n>>s;
        fo(0,n) {
            x=cc;
            if(s[i]=='(') cc++;
            else cc--;
            if(cc<0||(cc==0&&x<0)) {
                ans++;
            }
            co<<ans/2<<nl;

        }
    }
    return 0;
}


//#include<bits/stdc++.h>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<deque>
#include<stack>
#include<vector>
#include<set>
#include<unordered_set>
#include<list>
#include<map>
#include<iterator>
#include<algorithm>
#include<utility>
#include<functional>
#include<numeric>
using namespace std;
typedef long long int ll;
typedef long double ld;
#define nl "\n"
#define NL co<<nl
#define vd " "
#define F first
#define S second
#define fo(a,b) for(i=a;i<b;i++)
#define Fo(a,b) for(i=a;i>=b;i--)
#define fo1(a,b) for(j=a;j<b;j++)
#define Fo1(a,b) for(j=a;j>=b;j--)
#define fo2(a,b,c) for(i=a;i<=b;i+=c)
#define Fo2(a,b,c) for(i=a;i<=b;i-=c)
#define pb push_back
#define mp make_pair
#define popf pop_front
#define popb pop_back
#define eb emplace_back
#define ins insert
#define all(v)  v.begin(),v.end()
#define allr(v) v.rbegin(),v.rend()
#define tc(T) while(T--)
#define vec vector
#define co cout
#define d(x) co<<to_string(x)<<vd<<x<<vd
#define yes co<<"YES\n"
#define no co<<"NO\n"
#define yes2 co<<"Yes\n"
#define no2 co<<"No\n"
#define stol(s) stoll(s,nullptr,10)
#define vll vec<ll>
#define usll unordered_set<ll>
#define mll map<ll,ll>
#define mpll map<pair<ll,ll>,ll>
#define msl map<string,ll>
#define vpll vec<pair<ll,ll>>
#define spll set<pair<ll,ll>>
#define in(a,n) fo(0,n){cin>>a[i];}
#define vin(a,n) fo(0,n){cin>>q;a.eb(q);}
#define vpin(a,n) fo(0,n){cin>>q>>w;a.pb({q,w});}
#define vpout(b,n) fo(0,n){co<<b[i].F<<vd<<b[i].S<<nl;}
#define out(a,n) fo(0,n){cout<<a[i]<<vd;}
#define in2d(a,n,m) fo(0,n){for(j=0;j<m;j++){cin>>a[i][j];}}
#define out2d(a,n,m) fo(0,n){for(j=0;j<m;j++){co<<a[i][j]<<vd;} co<<nl;}
#define MX(a) *max_element(a,a+n)
#define MN(a) *min_element(a,a+n)
#define VMX(a) *max_element(all(a))
#define VMN(a) *min_element(all(a))
#define lowb(a,x) (std::lower_bound(a.begin(),a.end(),x)-a.begin())
#define upb(a,x) (std::upper_bound(a.begin(),a.end(),x)-a.begin())
#define beg begin()
#define rbeg rbegin()
#define ren rend()
#define en end()
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) a*b/gcd(a,b)
ll T=1,inf=100000000000000000,mod=1000000007;
ll ncr(ll n,ll r)
{
    ll l=n,f2=1,l2=n-r,i=1,j=1;
    if (r != 0||r!=n)
    {
        while (l2!=0||l!=r)
        {
            i*= l;
            j*= l2;
            ll m = gcd(i,j);
            i/= m;
            j/= m;
            l--;
            l2--;
        }
    }
    else i = 1;
    return i;
}
ll dig_sum(ll n) {
    ll v=n,sum=0,i;
    while(v) {
        sum+=v%10;
        v/=10;
    }
    return sum;

}
bool ckprime(ll n) {
    if(n%2==0&&n!=2) return 0;
    else if(n==2) return 1;
    else {
        ll v=sqrt(n),flag=1,i;
        for(i=3; i<=v; i+=2) {
            if(n%i==0) {
                flag=0;
                break;
            }
        }
        return flag;
    }

}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    //cin >> T;
    while(T--)
    {
        ll n,sz=1,ans=0,blag=0,u,mn,ev=0,mx,mini,i,v,q,p,j,k,w,h,l,m,x,y,od=0,total=0,cc=0,pp=0,flag=0,klag=0,sum=0;
        string s,s1;
        bool ok=1;
        cin>>n>>s;
        fo(0,n){
        if(s[i]=='(') cc++;
        else cc--;
        if(cc<0){
        ans++;
        }
        co<<ans/2<<nl;

        }
    }
    return 0;
}


//#include "/Users/manishkumar/Work/stdc++.h"
#include<bits/stdc++.h>
using namespace std;
#define FOR(i,a,n) for(long long i=a;i<=n;i++)
#define REP(i,n) FOR(i,0,n)
#define rep(i,n) FOR(i,0,n-1)
#define sync ios_base::sync_with_stdio(false); cin.tie(NULL);
#define pb push_back
#define ll long long int
#define vi vector<int>
# define INF32 0x3f3f3f3f
# define INF64 1e18
#define forn(i, n) for (int i = 0; i < int(n); i++)

int main() {
    sync
    int T;
    cin>>T;

    while(T--) {
        int n;
        cin>>n;
        string s;
        cin>>s;
        vector<bool> isVis(n,false);
        stack<int>st;
        for(int i=0;i<n;++i) {
            if(s[i] == '(') {
                st.push(i);
            } else if(s[i] == ')') {
                if(!st.empty()) {
                    isVis[st.top()] = true;
                    isVis[i] = true;
                    st.pop();
                }
            }
        }

        int cnt = 0;;
        for(int i=0;i<n;++i) {
            if(!isVis[i] && s[i] == '(') {
                cnt++;
            }
        }

        cout<<cnt<<"\n";
    }

    return 0;
}


//#include "/Users/manishkumar/Work/stdc++.h"
#include<bits/stdc++.h>
using namespace std;
#define FOR(i,a,n) for(long long i=a;i<=n;i++)
#define REP(i,n) FOR(i,0,n)
#define rep(i,n) FOR(i,0,n-1)
#define sync ios_base::sync_with_stdio(false); cin.tie(NULL);
#define pb push_back
#define ll long long int
#define vi vector<int>
# define INF32 0x3f3f3f3f
# define INF64 1e18
#define forn(i, n) for (int i = 0; i < int(n); i++)

int main() {
    sync
    int T;
    cin>>T;

    while(T--) {
        int n;
        cin>>n;
        string s;
        cin>>s;
        vector<bool> isVis(n,false);
        stack<int>st;
        for(int i=0;i<n;++i) {
            if(s[i] == '(') {
                st.push(i);
            } else if(s[i] == ')') {
                if(!st.empty()) {
                    isVis[st.top()] = true;
                    isVis[i] = true;
                    st.pop();
                }
            }
        }

        int l1 = 0;
        int l2 = 0;
        int cnt = 0;
        for(int i=0;i<n;++i) {
            if(!isVis[i] && s[i] == '(') {
                cnt++;
            }
        }
        if(cnt ==0 ) {
            cout<<0<<"\n";
            continue;
        }

        for(int i=0;i<n;++i) {
            if(!isVis[i] && s[i] == ')') {
                l1++;
            } else {
                break;
            }
        }

        for(int i=n-1;i>=0;--i) {
            if(!isVis[i] && s[i] == '(') {
                l2++;
            } else {
                break;
            }
        }

        int res = 0;

        if(l1 == cnt ) {
            res = l1;
        } else {
            res+=(max(l1,l2));
            cnt-=max(l1,l2);
            res+=(2*cnt);
        }
        cout<<res<<"\n";

    }

    return 0;
}


//#include "/Users/manishkumar/Work/stdc++.h"
#include<bits/stdc++.h>
using namespace std;
#define FOR(i,a,n) for(long long i=a;i<=n;i++)
#define REP(i,n) FOR(i,0,n)
#define rep(i,n) FOR(i,0,n-1)
#define sync ios_base::sync_with_stdio(false); cin.tie(NULL);
#define pb push_back
#define ll long long int
#define vi vector<int>
# define INF32 0x3f3f3f3f
# define INF64 1e18
#define forn(i, n) for (int i = 0; i < int(n); i++)

int main() {
    sync
    int T;
    cin>>T;

    while(T--) {
        int n;
        cin>>n;
        string s;
        cin>>s;
        vector<bool> isVis(n,false);
        stack<int>st;
        for(int i=0;i<n;++i) {
            if(s[i] == '(') {
                st.push(i);
            } else if(s[i] == ')') {
                if(!st.empty()) {
                    isVis[st.top()] = true;
                    isVis[i] = true;
                    st.pop();
                }
            }
        }

        int pre = 0;
        int l1  =0;
        int cnt = 0;
        for(int i=0;i<n;++i) {
            if(!isVis[i] && s[i] == ')') {
                l1+=pre;
                pre=0;
                l1++;
                cnt++;
            } else if(isVis[i]){
                pre++;
            }
        }

        int l2 = 0;
        pre=0;
        for(int i=n-1;i>=0;--i) {
                  if(!isVis[i] && s[i] == '(') {
                      l2+=pre;
                      pre=0;
                      l2++;
                  } else if(isVis[i]){
                      pre++;
                  }
        }

        cout<<min(2*cnt, min(l1,l2))<<"\n";

    }

    return 0;
}


//#include "/Users/manishkumar/Work/stdc++.h"
#include<bits/stdc++.h>
using namespace std;
#define FOR(i,a,n) for(long long i=a;i<=n;i++)
#define REP(i,n) FOR(i,0,n)
#define rep(i,n) FOR(i,0,n-1)
#define sync ios_base::sync_with_stdio(false); cin.tie(NULL);
#define pb push_back
#define ll long long int
#define vi vector<int>
# define INF32 0x3f3f3f3f
# define INF64 1e18
#define forn(i, n) for (int i = 0; i < int(n); i++)

int main() {
    sync
    int T;
    cin>>T;

    while(T--) {
        int n;
        cin>>n;
        string s;
        cin>>s;
        vector<bool> isVis(n,false);
        stack<int>st;
        for(int i=0;i<n;++i) {
            if(s[i] == '(') {
                st.push(i);
            } else if(s[i] == ')') {
                if(!st.empty()) {
                    isVis[st.top()] = true;
                    isVis[i] = true;
                    st.pop();
                }
            }
        }

        int l1 = 0;
        int l2 = 0;
        int cnt = 0;
        for(int i=0;i<n;++i) {
            if(!isVis[i] && s[i] == '(') {
                cnt++;
            }
        }
        if(cnt ==0 ) {
            cout<<0<<"\n";
            continue;
        }

        for(int i=0;i<n;++i) {
            if(!isVis[i] && s[i] == ')') {
                l1++;
            } else {
                break;
            }
        }

        for(int i=n-1;i>=0;--i) {
            if(!isVis[i] && s[i] == '(') {
                l2++;
            } else {
                break;
            }
        }

        int res = 0;

        if(l1 == cnt ) {
            res = l1;
        } else {
            res+=l1;
            cnt-=l1;
            if(cnt!=0) {
                res+=l2;
                cnt-=l2;
            }
            res+=(2*cnt);
        }
        cout<<res<<"\n";

    }

    return 0;
}


#include<bits/stdc++.h>
#define t int t;cin>>t;while(t--)
using namespace std;
int main()
    {
    	t{
    		int n,i,res;
    		stack<char>p;
    		cin>>n;
    		string s;
    		cin>>s;
    		for(i=0;i<n;i++)
    		    {
    		    	if(s[i]==')'&&!p.empty())
					{
    		    	if(p.top()=='(')
    		    		p.pop();
    		    	else
    		    	    p.push(s[i]);
					}
					else
					    p.push(s[i]);
				}
			int reducedLen = p.size();
			int count =0;
			while(!p.empty()&&p.top()=='(')
			{
				p.pop();
				count++;
			}

			res = reducedLen/2+count%2;
			cout<<res<<endl;
		}
		return 0;
	}


#include<bits/stdc++.h>
#define t int t;cin>>t;while(t--)
using namespace std;
int main()
    {
    	t{
    		int n,i,res;
    		stack<char>p;
    		cin>>n;
    		string s;
    		cin>>s;
    		for(i=0;i<n;i++)
    		    {
    		    	if(s[i]==')'&&!p.empty())
					{
    		    	if(p.top()=='(')
    		    		p.pop();
    		    	else
    		    	    p.push(s[i]);
					}
					else
					    p.push(s[i]);
				}
			int reducedLen = p.size();
			int count =0;
			while(!p.empty()&&p.top()=='(')
			{
				p.pop();
				count++;
			}

			res = count;
			cout<<res<<endl;
		}
		return 0;
	}


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


#include<bits/stdc++.h>
#define t int t;cin>>t;while(t--)
using namespace std;
int main()
    {
    	t{
    		int n,i,res;
    		stack<char>p;
    		cin>>n;
    		string s;
    		cin>>s;
    		for(i=0;i<n;i++)
    		    {
    		    	if(s[i]==')'&&!p.empty())
					{
    		    	if(p.top()=='(')
    		    		p.pop();
    		    	else
    		    	    p.push(s[i]);
					}
					else
					    p.push(s[i]);
				}
			int reducedLen = p.size();
			int count =0;
			while(!p.empty()&&p.top()=='(')
			{
				p.pop();
				count++;
			}

			res = reducedLen/2+count%2;
			if(res>0)
			   cout<<res-1<<endl;
			else
			   cout<<0<<endl;
		}
		return 0;
	}


#include<bits/stdc++.h>
#define endl "\n"
#define ll long long
#define F first
#define final 100000000
#define S second
#define pb push_back
#define mp make_pair
using namespace std;

int main()
{
  ll tt;
  cin >> tt;
  while(tt--)
  {
      ios_base::sync_with_stdio(false);
      cin.tie(NULL);
      ll n;
      cin >> n;
      string s;
      cin >> s;
    ll count=0;ll ans=0;
    for(ll i=0;i<n-1;i++)
    {
         if(s[i]=='(' && s[i+1]==')')
        {
            //no change
            ll x=i-1;ll y=i+2;
            count++;
            if(x>=0 && y<=n-1)
            {
                while(s[x]='('&& s[y]==')')
                {
                    count++;
                    x--;y++;
                }
            }
            i++;
        }

    }
    ans=(n-2*count)/2;
    cout << ans << endl;
  }

}


#include<bits/stdc++.h>
#define endl "\n"
#define ll long long
#define F first
#define final 100000000
#define S second
#define pb push_back
#define mp make_pair
using namespace std;

int main()
{
  ll tt;
  cin >> tt;
  while(tt--)
  {
      ios_base::sync_with_stdio(false);
      cin.tie(NULL);
      ll n;
      cin >> n;
      string s;
      cin >> s;
    ll count=0;ll ans=0;
    for(ll i=0;i<n-1;i++)
    {
         if(s[i]=='(' && s[i+1]==')')
        {
            ll x=i-1;ll y=i+2;
            count++;
            if(x>=0 && y<=n-1)
            {
                while(s[x]='('&& s[y]==')')
                {
                    count++;
                    x--;y++;
                    if(x<0 || y>n-1)
                    break;
                }
            }
            i++;
        }

    }
    ans=(n-2*count)/2;
    cout << ans << endl;
  }
  return 0;

}


#include<bits/stdc++.h>
#define endl "\n"
#define ll long long
#define F first
#define final 100000000
#define S second
#define pb push_back
#define mp make_pair
using namespace std;

int main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll t;
  cin >> t;
  while(t--)
  {
      ll n;
      cin >> n;
      string s;
      cin >> s;
    ll count=0;ll ans=0;ll flag=1;
        while(flag==1)
        {
            flag=0;
            if(s.length()>=1)
            {
                for(ll i=0;i<s.length()-1;i++)
            {
                 if(s[i]=='(' && s[i+1]==')')
                {
                    flag=1;
                    s.erase(i,2);
                    break;
                }

            }
            }

        }
      ans=s.length()/2;
    cout <<ans<< endl;
  }

  return 0;

}


#include<bits/stdc++.h>
#define endl "\n"
#define ll long long
#define F first
#define final 100000000
#define S second
#define pb push_back
#define mp make_pair
using namespace std;

int main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll t;
  cin >> t;
  while(t--)
  {
      ll n;
      cin >> n;
      string s;
      cin >> s;
    ll count=0;ll ans=0;
    for(ll i=0;i<n-1;i++)
    {
         if(s[i]=='(' && s[i+1]==')')
        {
            ll x=i-1;ll y=i+2;
            count++;
            if(x>=0 && y<=n-1)
            {
                while(s[x]='('&& s[y]==')')
                {
                    count++;
                    x--;y++;
                    if(x<0 || y>n-1)
                    break;
                }
            }
            i++;
        }

    }
    ans=(n-2*count)/2;
    cout << ans << endl;
  }

  return 0;

}


#include<bits/stdc++.h>
#define endl "\n"
#define ll long long
#define F first
#define final 100000000
#define S second
#define pb push_back
#define mp make_pair
using namespace std;

int main()
{
  ll tt;
  cin >> tt;
  while(tt--)
  {
      ll n;
      cin >> n;
      string s;
      cin >> s;
    ll count=0;ll ans=0;
    for(ll i=0;i<n-1;i++)
    {
         if(s[i]=='(' && s[i+1]==')')
        {
            //no change
            ll x=i-1;ll y=i+2;
            count++;
            if(x>=0 && y<=n-1)
            {
                while(s[x]='('&& s[y]==')')
                {
                    count++;
                    x--;y++;
                }
            }
            i++;
        }

    }
    ans=(n-2*count)/2;
    cout << ans << endl;
  }

}


#include<bits/stdc++.h>
#define endl "\n"
#define ll long long
#define F first
#define final 100000000
#define S second
#define pb push_back
#define mp make_pair
using namespace std;

int main()
{
  ll tt;
  cin >> tt;
  while(tt--)
  {
      ios_base::sync_with_stdio(false);
      cin.tie(NULL);
      ll n;
      cin >> n;
      string s;
      cin >> s;
    ll count=0;ll ans=0;
    for(ll i=0;i<n-1;i++)
    {
         if(s[i]=='(' && s[i+1]==')')
        {
            //no change
            ll x=i-1;ll y=i+2;
            count++;
            if(x>=0 && y<=n-1)
            {
                while(s[x]='('&& s[y]==')')
                {
                    count++;
                    x--;y++;
                }
            }
            i++;
        }

    }
    ans=(n-2*count)/2;
    cout << ans << endl;
  }
  return 0;

}


#include<bits/stdc++.h>
#define endl "\n"
#define ll long long
#define F first
#define final 100000000
#define S second
#define pb push_back
#define mp make_pair
using namespace std;

int main()
{
  ll t;
  cin >> t;
  while(t--)
  {
    ll n;
    cin >> n;
    string s;
    cin >> s;
    ll count=0;
    for(ll i=0;i<n-1;i++)
    {
        if(s[i]=='(' && s[i+1]==')')
        {
            //no change
            i++;
            count=count;
        }
        else if(s[i]==')')
        {

            count++;
        }
    }
    cout << count << endl;
  }
}


#include <iostream>
#include <vector>
#include <limits>
#include <cstdlib>
#include <algorithm>
#include <functional>
#include <complex>
#include <iomanip>
#include <cassert>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <bitset>
#include <cstring>
#include <climits>
using namespace std;
#define nl '\n'
#define sp ' '
#define f first
#define s second
#define EPS 1e-9
#define boost() cin.tie(0); cout.tie(0); cin.sync_with_stdio(0)
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 5e5 + 2; // REMEMBER TO CHANGE
const int MOD = 1e9 + 7;
void solve(){
	int n; cin >> n;
	string s; cin >> s;

	int prev = n, cur = 0;
	string tmp = s, tmp2 = "";
	while(true){
		tmp2 = ""; cur = 0;
		for(int i = 0;i<tmp.size();i++){
			if(i+1<tmp.size()&&tmp[i]=='('&&tmp[i+1]==')'){
				i++;
			}else{
				tmp2+=tmp[i];
			}
		}
		cur = tmp2.size();
		if(cur==prev)break;
		prev = cur;
		tmp = tmp2;
	}
	int o = 0, c = 0;
	for(int i = 0;i<tmp.size();i++){
		if(tmp[i]=='(')o++;
		else if(tmp[i]==')')c++;
	}

	int l = 0, r = 0;
	for(int i = 0;i<n;i++) if(s[i]==')')l++; else break;
	for(int i = n-1;i>=0;i--) if(s[i]=='(')r++; else break;
	cout << min(o, c) << nl;
}
int main(){
	boost();
	int t; cin >> t;
	for(int i = 0;i<t;i++){
		solve();
	}
	return 0;
}


#include <iostream>
#include <vector>
#include <limits>
#include <cstdlib>
#include <algorithm>
#include <functional>
#include <complex>
#include <iomanip>
#include <cassert>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <bitset>
#include <cstring>
#include <climits>
using namespace std;
#define nl '\n'
#define sp ' '
#define f first
#define s second
#define EPS 1e-9
#define boost() cin.tie(0); cout.tie(0); cin.sync_with_stdio(0)
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 5e5 + 2; // REMEMBER TO CHANGE
const int MOD = 1e9 + 7;
void solve(){
	int n; cin >> n;
	string s; cin >> s;
	int prev = n, cur = 0;
	string tmp = s, tmp2 = "";
	while(true){
		tmp2 = ""; cur = 0;
		for(int i = 0;i<tmp.size();i++){
			if(i+1<tmp.size()&&tmp[i]=='('&&tmp[i+1]==')'){;i++;}
			else{
				tmp2+=tmp[i];
			}
		}
		cur = tmp2.size();
		if(cur==prev)break;
		prev = cur;
		tmp = tmp2;
	}
	int lft = 0;
	for(int i = 0;i<n;i++){
		if(s[i]==')')lft++;
		else break;
	}
	int rht = 0;
	for(int i = n-1;i>=0;i--){
		if(s[i]=='(')rht++;
		else break;
	}
	int l = 0, r = 0;
	for(int i = 0;i<tmp.size();i++){
		if(tmp[i]=='(')l++;
		else r++;
	}
	int ans = 0;
	if(lft>rht){
		ans+=(l+r-lft);
	}else{
		ans+=(l+r-rht);
	}
	cout << ans << nl;
}
int main(){
	boost();
	int t; cin >> t;
	for(int i = 0;i<t;i++){
		solve();
	}
	return 0;
}


#include <iostream>
#include <vector>
#include <limits>
#include <cstdlib>
#include <algorithm>
#include <functional>
#include <complex>
#include <iomanip>
#include <cassert>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <bitset>
#include <cstring>
#include <climits>
using namespace std;
#define nl '\n'
#define sp ' '
#define f first
#define s second
#define EPS 1e-9
#define boost() cin.tie(0); cout.tie(0); cin.sync_with_stdio(0)
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 5e5 + 2; // REMEMBER TO CHANGE
const int MOD = 1e9 + 7;
void solve(){
	int n; cin >> n;
	string s; cin >> s;

	int prev = n, cur = 0;
	string tmp = s, tmp2 = "";
	while(true){
		tmp2 = ""; cur = 0;
		for(int i = 0;i<tmp.size();i++){
			if(i+1<tmp.size()&&tmp[i]=='('&&tmp[i+1]==')'){
				i++;
			}else{
				tmp2+=tmp[i];
			}
		}
		cur = tmp2.size();
		if(cur==prev)break;
		prev = cur;
		tmp = tmp2;
	}
	int o = 0, c = 0;
	for(int i = 0;i<tmp.size();i++){
		if(tmp[i]=='(')o++;
		else c++;
	}

	int l = 0, r = 0;
	for(int i = 0;i<n;i++) if(s[i]==')')l++; else break;
	for(int i = n-1;i>=0;i--) if(s[i]=='(')r++; else break;
	cout << o+c-max(l, r) << nl;
}
int main(){
	boost();
	int t; cin >> t;
	for(int i = 0;i<t;i++){
		solve();
	}
	return 0;
}


import java.io.*;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
import java.util.StringTokenizer;

public class TaskB {

	public static void main(String[] args) throws IOException {

		Scanner sc = new Scanner(System.in);
		PrintWriter pw = new PrintWriter(System.out);
		HashSet<Integer> set = new HashSet<Integer>();

		int n = sc.nextInt();
		int k = sc.nextInt();
		int q = sc.nextInt();

		long[] count = new long[(int) 2e5 + 7];

		for (int i = 0; i < n; i++) {
			count[sc.nextInt()]++;
			count[sc.nextInt() + 1]--;
		}
		for (int i = 1; i < count.length; i++)
			count[i] += count[i - 1] * 1l;

		for (int i = 0; i < count.length; i++)
			if (count[i] >= k)
				count[i] = 1;
			else
				count[i] = 0;
		for (int i = 1; i < count.length; i++)
			count[i] += count[i - 1];

		while (q-- > 0) {
			int l = sc.nextInt();
			int r = sc.nextInt();
			pw.println(count[r] - count[l - 1]);

		}
		pw.close();
	}

	static class Scanner {
		StringTokenizer st;
		BufferedReader br;

		public Scanner(InputStream s) {
			br = new BufferedReader(new InputStreamReader(s));
		}

		public String next() throws IOException {
			while (st == null || !st.hasMoreTokens())
				st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}

		public int nextInt() throws IOException {
			return Integer.parseInt(next());
		}

		public long nextLong() throws IOException {
			return Long.parseLong(next());
		}

		public String nextLine() throws IOException {
			return br.readLine();
		}

		public double nextDouble() throws IOException {
			String x = next();
			StringBuilder sb = new StringBuilder("0");
			double res = 0, f = 1;
			boolean dec = false, neg = false;
			int start = 0;
			if (x.charAt(0) == '-') {
				neg = true;
				start++;
			}
			for (int i = start; i < x.length(); i++)
				if (x.charAt(i) == '.') {
					res = Long.parseLong(sb.toString());
					sb = new StringBuilder("0");
					dec = true;
				} else {
					sb.append(x.charAt(i));
					if (dec)
						f *= 10;
				}
			res += Long.parseLong(sb.toString()) / f;
			return res * (neg ? -1 : 1);
		}

		public boolean ready() throws IOException {
			return br.ready();
		}

	}

}


import java.io.*;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class TaskB {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);
		PrintWriter pw = new PrintWriter(System.out);
		HashSet<Integer> set = new HashSet<Integer>();

		int n = sc.nextInt();
		int k = sc.nextInt();
		int q = sc.nextInt();

		long[] count = new long[(int) 2e5 + 7];

		for (int i = 0; i < n; i++) {
			count[sc.nextInt()]++;
			count[sc.nextInt() + 1]--;
		}
		for (int i = 1; i < count.length; i++)
			count[i] += count[i - 1] * 1l;

		int[] query = new int[count.length];

		for (int i = 0; i < count.length; i++)
			if (count[i] >= k)
				query[i]++;

		for (int i = 1; i < count.length; i++)
			query[i] += query[i - 1];

		while (q-- > 0) {
			int l = sc.nextInt();
			int r = sc.nextInt();
			pw.println(query[r] - query[l - 1]);

		}
		pw.close();
	}
}


import java.util.*;

public final class sar
{
    public static void main(String[] args)
    {
    	Scanner sc = new Scanner(System.in);
        int n=sc.nextInt();
        int k=sc.nextInt();
        int q = sc.nextInt();

        int[] arr=new int[200001];

        for(int i=0;i<n;i++)
        {   int l=sc.nextInt();
        	int r=sc.nextInt();
            arr[l]+=1;
            arr[r+1]-=1;
        }

        for(int i=1;i<=200000;i++)
            arr[i]+=arr[i-1];

        for(int i=1;i<=200000;i++)
            arr[i]=arr[i]>=k?1:0;

        for(int i=1;i<=200000;i++)
            arr[i]+=arr[i-1];

        for(int i=0;i<q;i++)
        {
        	int l=sc.nextInt();
        	int r=sc.nextInt();
            System.out.println(arr[r]-arr[l-1]);
        }
    }
}


//package pac;

import java.util.*;

public final class sar
{
    public static void main(String[] args)
    {
    	Scanner sc = new Scanner(System.in);
        int n=sc.nextInt();
        int k=sc.nextInt();
        int q = sc.nextInt();

        int[] arr=new int[200001];

        for(int i=0;i<n;i++)
        {   int l=sc.nextInt();
        	int r=sc.nextInt();
            arr[l]+=1;
            arr[r+1]-=1;
        }

        for(int i=1;i<=100;i++)
            arr[i]+=arr[i-1];

        for(int i=1;i<=200000;i++)
            arr[i]=arr[i]>=k?1:0;

        for(int i=1;i<=200000;i++)
            arr[i]+=arr[i-1];

        for(int i=0;i<q;i++)
        {
        	int l=sc.nextInt();
        	int r=sc.nextInt();
            System.out.println(arr[r]-arr[l-1]);
        }
    }
}


//package pac;

import java.util.*;

public final class sar
{
    public static void main(String[] args)
    {
    	Scanner sc = new Scanner(System.in);
        int n=sc.nextInt();
        int k=sc.nextInt();
        int q = sc.nextInt();

        int[] arr=new int[200002];

        for(int i=0;i<n;i++)
        {   int l=sc.nextInt();
        	int r=sc.nextInt();
            arr[l]+=1;
            arr[r+1]-=1;
        }

        for(int i=1;i<=200000;i++)
            arr[i]+=arr[i-1];

        for(int i=1;i<=200000;i++)
            arr[i]=arr[i]>=k?1:0;

        for(int i=1;i<=200000;i++)
            arr[i]+=arr[i-1];

        for(int i=0;i<q;i++)
        {
        	int l=sc.nextInt();
        	int r=sc.nextInt();
            System.out.println(arr[r]-arr[l-1]);
        }
    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskB solver = new TaskB();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskB {
        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int n = in.nextInt(), k = in.nextInt(), q = in.nextInt();
            int[] a = new int[(int) 2e5 + 1];
            for (int i = 0; i < n; ++i) {
                int x = in.nextInt();
                int y = in.nextInt();
                for (int c = x; c <= y; ++c) a[c]++;
            }
            for (int i = 0; i < q; ++i) {
                int res = 0;
                int x = in.nextInt();
                int y = in.nextInt();
                for (int c = x; c <= y; ++c) if (a[c] >= k) ++res;
                out.println(res);
            }
        }

    }

    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskB solver = new TaskB();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskB {
        final int R = (int) 2e5 + 3;

        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int n = in.nextInt(), k = in.nextInt(), q = in.nextInt();
            int[] a = new int[R];
            int[] res = new int[R];
            boolean[] v = new boolean[R];

            for (int i = 0; i < n; ++i) {
                ++a[in.nextInt()];
                --a[in.nextInt() + 1];
            }
            int c = 0;
            for (int i = 0; i < R; ++i) {
                c += a[i];
                if (c >= k) v[i] = true;
            }
            for (int i = 1; i < R; ++i) {
                if (v[i]) {
                    res[i] = res[i - 1] + 1;
                } else {
                    res[i] = res[i - 1];
                }
            }
            for (int i = 0; i < q; ++i) {
                int l = in.nextInt();
                int r = in.nextInt();
                out.println(res[r] - res[l - 1]);
            }
        }

    }

    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskB solver = new TaskB();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskB {
        final int R = (int) 2e5 + 4;

        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int n = in.nextInt(), k = in.nextInt(), q = in.nextInt();
            int[] a = new int[R];
            int[] res = new int[R];
            boolean[] v = new boolean[R];

            for (int i = 0; i < n; ++i) {
                ++a[in.nextInt()];
                --a[in.nextInt() + 1];
            }
            int c = 0;
            for (int i = 0; i < R; ++i) {
                c += a[i];
                if (c >= k) v[i] = true;
            }
            for (int i = 1; i < R; ++i) {
                if (v[i]) {
                    res[i] = res[i - 1] + 1;
                } else {
                    res[i] = res[i - 1];
                }
            }
            for (int i = 0; i < q; ++i) {
                int l = in.nextInt();
                int r = in.nextInt();
                out.println(res[r] - res[l - 1]);
            }
        }

    }

    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.List;
import java.util.Iterator;
import java.util.InputMismatchException;
import java.io.IOException;
import java.util.LinkedList;
import java.util.ArrayList;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskB solver = new TaskB();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskB {
        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int n = in.nextInt();
            int k = in.nextInt();
            int q = in.nextInt();
            final int MAXN = 200021;
            Integer[] arr = new Integer[MAXN];
            Arrays.fill(arr, 0);
            SegmentTree<Integer> tree = new SegmentTree<Integer>(arr, 0) {

                public Integer combine(Integer node1, Integer node2) {
                    return node1 + node2;
                }

                public void propagate(int node, Integer value) {
                    int l = left(node), r = right(node);
                    lazy.set(l, lazy.get(l) + value);
                    lazy.set(r, lazy.get(r) + value);
                }

                public void update(int node, int l, int r, Integer value) {
                    tree.set(node, tree.get(node) + (r - l + 1) * value);
                }
            };
            for (int i = 0; i < n; i++) {
                tree.rangeUpdate(in.nextInt(), in.nextInt(), 1);
            }
            int[] presum = new int[MAXN];
            int j = 1;
            presum[0] = 0;
            for (int x : tree) {
                presum[j] = presum[j - 1] + (x >= k ? 1 : 0);
                j++;
            }
            for (int i = 0; i < q; i++) {
                int l = in.nextInt(), r = in.nextInt();
                out.println(presum[r] - presum[l - 1]);
            }
        }

    }

    static abstract class SegmentTree<T> implements Iterable<T> {
        public int n;
        public T neutralElement;
        public T[] data;
        public ArrayList<T> tree;
        public ArrayList<T> lazy;

        public SegmentTree(T[] data, T neutralElement) {
            this.neutralElement = neutralElement;
            this.data = data;
            this.n = data.length - 1;
            tree = new ArrayList<>(4 * n + 4);
            lazy = new ArrayList<>(4 * n + 4);
            for (int i = 0; i < 4 * n + 4; i++) {
                tree.add(neutralElement);
                lazy.add(neutralElement);
            }
            build();
        }

        public int left(int node) {
            return 2 * node;
        }

        public int right(int node) {
            return 2 * node + 1;
        }

        public int middle(int l, int r) {
            return l + (r - l) / 2;
        }

        public abstract T combine(T node1, T node2);

        public abstract void propagate(int node, T value);

        public abstract void update(int node, int l, int r, T value);

        public void doLazy(int node, int l, int r, T value) {
            update(node, l, r, value);
            if (l != r) propagate(node, value);
            lazy.set(node, neutralElement);
        }

        public void build() {
            build(1, 1, n);
        }

        private void build(int node, int l, int r) {
            if (l == r) tree.set(node, data[l]);
            else {
                int m = middle(l, r);
                build(left(node), l, m);
                build(right(node), m + 1, r);
                tree.set(node, combine(tree.get(left(node)), tree.get(right(node))));
            }
        }

        public T query(int i) {
            return query(1, 1, n, i, i);
        }

        private T query(int node, int l, int r, int a, int b) {
            if (lazy.get(node) != neutralElement) doLazy(node, l, r, lazy.get(node));
            if (l > b || r < a) return neutralElement;
            if (a <= l && r <= b) return tree.get(node);
            int m = middle(l, r);
            return combine(query(left(node), l, m, a, b), query(right(node), m + 1, r, a, b));
        }

        public void rangeUpdate(int l, int r, T value) {
            update(1, 1, n, l, r, value);
        }

        private void update(int node, int l, int r, int a, int b, T value) {
            if (lazy.get(node) != neutralElement) doLazy(node, l, r, lazy.get(node));
            if (l > b || r < a) return;
            if (a <= l && r <= b) {
                update(node, l, r, value);
                if (l != r) propagate(node, value);
                return;
            }
            int m = middle(l, r);
            update(left(node), l, m, a, b, value);
            update(right(node), m + 1, r, a, b, value);
            tree.set(node, combine(tree.get(left(node)), tree.get(right(node))));
        }

        public Iterator<T> iterator() {
            List<T> l = new LinkedList<>();
            for (int i = 1; i <= n; i++) {
                l.add(query(i));
            }
            return l.iterator();
        }

    }

    static class InputReader {
        private int lenbuf = 0;
        private int ptrbuf = 0;
        private InputStream in;
        private byte[] inbuf = new byte[1024];

        public InputReader(InputStream in) {
            this.in = in;
        }

        private int readByte() {
            if (lenbuf == -1) throw new InputMismatchException();
            if (ptrbuf >= lenbuf) {
                ptrbuf = 0;
                try {
                    lenbuf = in.read(inbuf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (lenbuf <= 0) return -1;
            }
            return inbuf[ptrbuf++];
        }

        public int nextInt() {
            int num = 0, b;
            boolean minus = false;
            while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ;
            if (b == '-') {
                minus = true;
                b = readByte();
            }

            while (true) {
                if (b >= '0' && b <= '9') {
                    num = num * 10 + (b - '0');
                } else {
                    return minus ? -num : num;
                }
                b = readByte();
            }
        }

    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskB solver = new TaskB();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskB {
        private final int MAXN = 200002;
        private int N;
        private int q;
        private int k;
        private int[] tree;
        private int[] lazy;

        public void solve(int testNumber, InputReader in, PrintWriter out) {
            N = in.nextInt();
            k = in.nextInt();
            q = in.nextInt();
            tree = new int[4 * MAXN + 2];
            lazy = new int[4 * MAXN + 2];
            for (int i = 0; i < N; i++) {
                update(1, 1, MAXN, in.nextInt(), in.nextInt(), 1);
            }
            int[] presum = new int[MAXN];
            for (int i = 1; i < MAXN; i++) {
                presum[i] = presum[i - 1] + (query(1, 1, MAXN, i, i) >= k ? 1 : 0);
            }
            for (int i = 0; i < q; i++) {
                int l = in.nextInt(), r = in.nextInt();
                out.println(presum[r] - presum[l - 1]);
            }
        }

        int l(int n) {
            return 2 * n;
        }

        int r(int n) {
            return 2 * n + 1;
        }

        int m(int l, int r) {
            return (l + r) / 2;
        }

        void update(int n, int l, int r, int a, int b, int v) {
            if (lazy[n] != 0) {
                tree[n] += (r - l + 1) * lazy[n];
                if (l != r) {
                    lazy[l(n)] += lazy[n];
                    lazy[r(n)] += lazy[n];
                }
                lazy[n] = 0;
            }
            if (l > b || r < a) return;
            if (a <= l && r <= b) {
                tree[n] += (r - l + 1) * v;
                if (l != r) {
                    lazy[l(n)] += v;
                    lazy[r(n)] += v;
                }
                return;
            }
            int m = m(l, r);
            update(l(n), l, m, a, b, v);
            update(r(n), m + 1, r, a, b, v);
            tree[n] = tree[l(n)] + tree[r(n)];
        }

        int query(int n, int l, int r, int a, int b) {
            if (lazy[n] != 0) {
                if (lazy[n] != 0) {
                    tree[n] += (r - l + 1) * lazy[n];
                    if (l != r) {
                        lazy[l(n)] += lazy[n];
                        lazy[r(n)] += lazy[n];
                    }
                    lazy[n] = 0;
                }
            }
            if (l > b || r < a) return 0;
            if (a <= l && r <= b) return tree[n];
            int m = m(l, r);
            return query(l(n), l, m, a, b) + query(r(n), m + 1, r, a, b);
        }

    }

    static class InputReader {
        private int lenbuf = 0;
        private int ptrbuf = 0;
        private InputStream in;
        private byte[] inbuf = new byte[1024];

        public InputReader(InputStream in) {
            this.in = in;
        }

        private int readByte() {
            if (lenbuf == -1) throw new InputMismatchException();
            if (ptrbuf >= lenbuf) {
                ptrbuf = 0;
                try {
                    lenbuf = in.read(inbuf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (lenbuf <= 0) return -1;
            }
            return inbuf[ptrbuf++];
        }

        public int nextInt() {
            int num = 0, b;
            boolean minus = false;
            while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ;
            if (b == '-') {
                minus = true;
                b = readByte();
            }

            while (true) {
                if (b >= '0' && b <= '9') {
                    num = num * 10 + (b - '0');
                } else {
                    return minus ? -num : num;
                }
                b = readByte();
            }
        }

    }
}