• + 0 comments

    include

    include

    using namespace std;

    /*Think of it this way: e.g.xaxb|bbxx left : 2x 1a 1b right: 2x 0a 2b , and they cancle out. What is left in the right is the answer.

    O(n) per query*/

    int arr[26];

    int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);

    int q;
    cin >> q; 
    
    string str; 
    while(q--) {
        cin >> str; 
        if(str.size() % 2) { cout << -1 << '\n'; continue; }
    
        int mid = str.size() >> 1; 
    
        for(int i = 0; i < mid; ++i) ++arr[str[i] - 'a']; 
    
        int ret = 0; 
        for(int i = mid; i < str.size(); ++i) {
            if(arr[str[i] - 'a']) --arr[str[i] - 'a']; 
            else ++ret; 
        }
    
        cout << ret << '\n'; 
        memset(arr, 0, sizeof(arr)); 
    }
    
    return 0; 
    

    }