Connected components || preprocess || DFS
IntuitionApproachComplexity
Time complexity:

Space complexity:
Code
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
    Given we can visit the node and edge multiple times 

    since AND operation will not be changed for duplicate values 

    we can traverse all the possible edges to get the min value

    so any node belonging to same connected component will have same
    answer that will be minimized

# Approach
<!-- Describe your approach to solving the problem. -->

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
    O(V+E)

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
    O(V+E)

# Code
```cpp []
#define ll long long 
#define vi vector<ll>
#define pll pair<ll,ll>
#define ff first 
#define ss second

class Solution {
public:

    map<ll,ll>mp; 
    map<ll, vi> colorCollection;
    map<ll, set<ll> > edjCollection;

    void dfs(ll node, ll color, vi &vis, vector<vector<pll>>&adj){
        vis[node] = 1;
        mp[node] = color;

        //need to traverse all the edges in this component 
        for(auto it: adj[node]){
            if(!vis[it.ff]){
                dfs(it.ff, color, vis, adj);
            }
        }

        return ;

    }

    ll helper(set<ll>&st){

        vi bitmask(18, 1);

        for(ll bitpos = 0; bitpos <=17; bitpos++){
            //is there anyone set zero here 

            for(auto it: st){
                if( ( it&(1ll<<bitpos) )== 0){
                    bitmask[bitpos] = 0;
                }
            }

        }

        ll val = 0;
        ll base = 1;
        ll i = 0;
        while(i < 18){
            if(bitmask[i]){
                val += base;
            }
            base*=2;
            i++;
        }

        return val;
        
    }



    vector<int> minimumCost(int n, vector<vector<int>>& edges, vector<vector<int>>& query) {
        
        vi vis(n, 0);
        ll color = 0;

        vector<vector<pll>> adj(n);
        //make adjacency 
        for(auto it: edges){
            ll u = it[0];
            ll v = it[1];
            ll w = it[2];

            adj[u].push_back({v, w});
            adj[v].push_back({u, w});
        }

        for(ll i = 0; i<n; i++){
            //find the connected component 
            if(!vis[i]){
                dfs(i, color, vis, adj);
                color++;
            }
        }      

        for(auto it: mp){
            colorCollection[it.ss].push_back(it.ff);
        }        

        for(auto it: colorCollection){
            ll cur_color = it.ff;

            for(auto kt: it.ss){
                
                for(auto node: adj[kt]){
                    edjCollection[cur_color].insert(node.ss);
                }
            }
        }

        // cout<<"edge collection info"<<endl;

        // for(auto it: edjCollection){
        //     cout<<it.ff<<" -> ";

        //     for(auto it: it.ss){
        //         cout<<it<<" ";
        //     }
        //     cout<<endl;
        // }

        
       ll sz = query.size();
       vector<int>ans(sz, -1);

       //preprocess for different groups 

       map<ll, ll>group_answer;

        for(ll cur_color = 0; cur_color < color; cur_color++){
            ll tmp = helper(edjCollection[cur_color]);
            group_answer[cur_color] = tmp;
        }

        //each edge will be visited once (bitpos iteration * 20)

        for(ll i = 0; i<sz; i++){
            ll src = query[i][0];
            ll dest = query[i][1];

            if(mp[src] != mp[dest]) continue;  
            ll cur_color = mp[src];

            ans[i] = group_answer[cur_color];

       }

       return ans;

    }
};
```
['Bit Manipulation', 'Graph', 'Strongly Connected Component', 'C++']