#include <bits/stdc++.h>
#define ll long long int
#define ld long double
#define PI 3.14159265358979323846264338
#define INF 0x3f3f3f3f
#define MAX 1000000
using namespace std;

void solve()
{
    string a;
    cin >> a;
    map<char, int> mp;
    ll result = 0;
    for (int i = 0; i < a.size(); i++)
    {

        if (a[i] == '(' or a[i] == '[')
        {

            mp[a[i]]++;
        }
        else
        {
            char r = 'r';
            if(a[i] == ')')
            {
                 r = '(';
            }
            else if (a[i] == ']')
            {
                r = '[';
            }
            
            if (mp[r] > 0)
            {
                mp[r]--;
                result++;
            }
        }
       
    }
    cout << result << endl;
    return;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif

    int t;
    cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}