Problem: You are given a string s, consisting of brackets of two types: '(', ')', '[' and ']'.

A string is called a regular bracket sequence (RBS) if it's of one of the following types: 

  * empty string; 
  * '(' + RBS + ')'; 
  * '[' + RBS + ']'; 
  * RBS + RBS. 



where plus is a concatenation of two strings.

In one move you can choose a non-empty subsequence of the string s (not necessarily consecutive) that is an RBS, remove it from the string and concatenate the remaining parts without changing the order.

What is the maximum number of moves you can perform?

Input

The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of testcases.

Each of the next t lines contains a non-empty string, consisting only of characters '(', ')', '[' and ']'. The total length of the strings over all testcases doesn't exceed 2 ⋅ 10^5.

Output

For each testcase print a single integer — the maximum number of moves you can perform on a given string s.

Example

Input


5
()
[]()
([)]
)]([
)[(]


Output


1
2
2
0
1

Note

In the first example you can just erase the whole string.

In the second example you can first erase the brackets on positions 1 and 2: "[]()", then "()" is left. After that you can erase it whole. You could erase the whole string from the beginning but you would get one move instead of two.

In the third example you can first erase the brackets on positions 1 and 3: "([)]". They form an RBS "()". Then "[]" is left, so you can erase it whole.

In the fourth example there is no subsequence that is an RBS, so you can't perform a move at all.

In the fifth example you can erase the brackets on positions 2 and 4: ")[(]" and get ")(" as a result. You can erase nothing from it.

Solution (in CPP):

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

#define get_ch getchar_unlocked
#define test int t;cin>>t;while(t--)
#define fo(i, k, n) for(int i=k; i<n; i++)
#define ll long long
#define ld long double
#define all(x) x.begin(), x.end()
#define deb(x) cout << #x << " " << x <<'\n'
#define is_sorted(v) std::is_sorted(v.begin(), v.end())
#define pb push_back
#define sz(x) (int)((x).size())

typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pll> vpll;
typedef vector<pii> vpii;
typedef set<int> Si;
typedef set<ll> Sll;
typedef set<pii> Spii;
typedef set<pll> Spll;
typedef map<int, int> Mii;
typedef map<ll, ll> Mll;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
const int mod = 1000000007;
const int pi = 3.1415926535897932384626;
const int e = 2.71828182845904523536;

bool visited[100001];
vi adj[100001];
int Distance[100001];
queue<int> q;

void dfs(int s){
    if (visited[s]){
        return;
    }
    visited[s]=true;
    for(auto x:adj[s]){
        dfs(x);
    }
}

void bfs(int s){
    visited[s] = true;
    Distance[s] = 0;
    q.push(s);
    while (!q.empty()) {
        int s = q.front(); q.pop();

        for (auto u : adj[s]) {
            if (visited[u]){
                continue;
            }
            visited[u] = true;
            Distance[u] = Distance[s]+1;
            q.push(u);
        }
    }
}

void solve(){
    string s;
    cin >> s;
    int n = s.length();
    bool arr[n];
    for(int j=0; j<n; j++) arr[j] = false;
    int ind = -1;
    int count = 0;
    vi b1o,b1c,b2o,b2c;
    for(int j=0; j<n; j++){
        if(s[j] == '(') b1o.pb(j);
        if(s[j] == ')') b1c.pb(j);
        if(s[j] == '[') b2o.pb(j);
        if(s[j] == ']') b2c.pb(j);
    }
    int i=0,j=0;
    while (i<sz(b1o) && j<sz(b1c)){
        if(b1o[i]<b1c[j]){
            count++;
            i++;
            j++;
        }
        else{
            j++;
        }
    }
    i=0;
    j=0;
    while (i<sz(b2o) && j<sz(b2c)){
        if(b2o[i]<b2c[j]){
            count++;
            i++;
            j++;
        }
        else{
            j++;
        }
    }
    cout << count << endl;
}


int main(){
    ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
    
    test{
        solve();
    }
    return 0;
}