Breadth First Search + Bitmask
IntuitionThe main idea is that if we want minimum walk cost(bitwise AND of weights from u to v).we should take the AND of the entire component that both
u and v
# Intuition
The main idea is that if we want minimum walk cost(bitwise AND of weights from u to v).we should take the AND of the entire component that both 
u and v belong to. **This is because the bitwise AND operation always results in a value that is either smaller than or equal to the original numbers**.

# Approach
We use a visited array and a map to store the AND result for all nodes in a particular component.

When we encounter a new component, we increment a counter and mark all nodes in that component with this counter.
We then perform a BFS traversal (instead of DFS) from any node in the component, updating the AND value for all edges within that component.
Initially, I used DFS, but it failed to consider all weighted paths between u and v, which is why BFS is necessary.

Finally, for each query:
If the two nodes do not belong to the same component, store -1 in the answer vector.
Otherwise, store the AND value of the component that the nodes belong to.

# Complexity
- Time complexity:
**O(NlogN+Q)**.Where N is the total number of nodes and q is the number of queries.
- Space complexity:
**O(N)**

# Code
```cpp []
class Solution {
public:
    void bfs(int node,vector<int>& vis,vector<vector<pair<int,int>>>&adj,int &band,int &cnt) {
    queue<int> q;
    q.push(node);
    vis[node] = cnt; 
    while (!q.empty()) {
        int u = q.front();
        q.pop();
        for (auto child : adj[u]) {
            int v = child.first;
            int w = child.second;
            band &= w;
            if (vis[v] == 0) {
                vis[v] = cnt;
                q.push(v);
            }
        }
    }
}
    vector<int> minimumCost(int n, vector<vector<int>>& edges, vector<vector<int>>& query) {
        vector<vector<pair<int,int>>>adj(n);// u se v and uska weight
        for(auto v:edges){
            adj[v[0]].emplace_back(v[1],v[2]);
            adj[v[1]].emplace_back(v[0],v[2]);
        }
        vector<int>vis(n,0);
        int cnt=0;//track of each subgraph we can extract from visited
        map<int,int>mp;
        for(int i=0;i<n;i++){
            if(vis[i]==0){
                int band=131071;
                cnt++;
                bfs(i,vis,adj,band,cnt);
                mp[cnt]=band;
            }
        }
        // map<int,set<int>>st;
        // for(int i=0;i<n;i++){
        //    st[vis[i]].insert(i);
        // } 
        vector<int>ans;
        for(auto q:query){
           int u=q[0];
           int v=q[1];
           if(vis[u]==vis[v]){
              ans.push_back(mp[vis[u]]);
           }
           else{
             ans.push_back(-1);
           }
        }
        return ans;
    }
};
```
['C++']