general dfs solution!!!
IntuitionApproachComplexity
Time complexity:

Space complexity:
Code
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

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

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

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

# Code
```cpp []
class Solution {
public:

    void dfs(int i,unordered_map<int,int>& mp,vector<vector<pair<int,int>>>& adj,vector<int>& visited,int& ans,int par){
        visited[i] = 1;
        for(auto [node,w]:adj[i]){
            mp[node] = par;
            ans = ans&w;
            if(!visited[node]) dfs(node,mp,adj,visited,ans,par);
        }
    }

    vector<int> minimumCost(int n, vector<vector<int>>& edges, vector<vector<int>>& queries) {
        //components  and their respective values

        //vertex -> another vertex parent
        unordered_map<int,int> mp;
        vector<vector<pair<int,int>>> adj(n);
        for(auto x:edges){
            int from = x[0];
            int to = x[1];
            int w = x[2];
            adj[from].push_back({to,w});
            adj[to].push_back({from,w});
        }
        vector<int> visited(n,0);
        unordered_map<int,int> memo;

        for(int i=0;i<n;i++){
            if(!visited[i]){
                int ans = -1;
                mp[i] = i;
                dfs(i,mp,adj,visited,ans,i);
                memo[i] = ans;
            }
        }
        vector<int> ans;

        for(int i=0;i<queries.size();i++){
            int from = queries[i][0];
            int to = queries[i][1];
            if(mp[from] != mp[to]){
                ans.push_back(-1);
            }
            else{
                ans.push_back(memo[mp[from]]);
            }
        }

        return ans;
    }
};
```
['C++']