C++ | Straightforward BFS + Greedy
to go from start to end, we can travel through any edge we want any number of times. in short, we have all the edges in that connected component at our disposal
to go from start to end, we can travel through any edge we want any number of times. in short, we have all the edges in that connected component at our disposal. we must use these to minimize the bitwise AND cost.

now, the minimum bitwise AND of an array is simply the bitwise AND of the entire array (since that way final result will only contain the 'i'th bit if all numbers have 'i'th bit set, and if even a single number has 'i'th as not set, that bit will also be not set in the final result, in which case the total AND of the entire array can not be worse than any other configuration)

so we just need to check if start and end are in the same connected component. if yes, we can travel through all the nodes in that component which gives us the least cost.

# Code
```cpp []
class Solution {
public:
    void bfs(vector<vector<pair<int, int>>>& adj , int comp, unordered_map<int, int>& connectedAnd, vector<int>& component, int current){
        queue<int> bfs;
        bfs.push(current);
        component[current] = comp;
        int totalAnd = pow(2, 20) - 1;
        while(!bfs.empty()){
            int cur = bfs.front();
            bfs.pop();
            for(auto &[child, wt] : adj[cur]){
                if(component[child] == -1){
                    component[child] = comp;
                    totalAnd &= wt;
                    bfs.push(child);
                }
                else totalAnd &= wt;
            }
        }
        connectedAnd[comp] = totalAnd;
    }
    vector<int> minimumCost(int n, vector<vector<int>>& edges, vector<vector<int>>& query) {
        int comp = 0;
        vector<vector<pair<int, int>>> adj(n);
        for(auto &v : edges){
            adj[v[0]].push_back({v[1], v[2]});
            adj[v[1]].push_back({v[0], v[2]});
        }
        unordered_map<int, int> connectedAnd;
        vector<int> component(n, -1);
        for(int i=0;i<n;i++){
            if(component[i] == -1){
                bfs(adj, comp, connectedAnd, component, i);
                comp++;
            }
        }
        vector<int> ans;
        for(auto &q : query){
            int start = q[0], end = q[1];
            if(component[start] != component[end]){
                ans.push_back(-1);
                continue;
            }
            ans.push_back(connectedAnd[component[start]]);
        }
        return ans;
    }
};
```
['C++']