Fastest way without union find | Beats 100 %
IntuitionBasically, we create a set of connected node and assign each connected node group a unique values(uid).
For each u,v, w in edges, there are 3 cases:
Bo
# Intuition
Basically, we create a set of connected node and assign each connected node group a unique values(uid).
For each u,v, w in edges, there are 3 cases:
1. Both u and v are not part of any node group, they will create a new node group together. the cost of this new group will be `w`.
2. If either of u and v is a pre-existing member, the non member joins the existing node group and inherits the uid of group joined.
3. If both of them are part of different node groups, in that case, we have merge smaller node group into larger node group, and update each node of smaller group to inherit the uid of larger group.

Cost of a node group can be calculated:
1. Keep a hashmap where key is uid and value is its cost.
2. When a new node joins the group, take `&` of its existing cost with the weight of attaching new node.

Special case: when merging two node groups: cost will be 
`cost = bigger node group cost & smaller node group cost & weight of current merging edge`

# Code
```python3 []
class Solution:
    def minimumCost(self, n, edges: List[List[int]], queries):
        node_union_map = {}
        component_cost = {}
        uid_set = {}
        result = []
        uid = 1

        def helper(uid: int, mx_set: set, mn_set: set):
            for v in mn_set:
                node_union_map[v] = uid
                mx_set.add(v)
            return mx_set

        for edge in edges:
            u, v, w = edge
            cuid = None
            if u in node_union_map and v in node_union_map:
                u_uid = node_union_map[u]
                v_uid = node_union_map[v]
                if u_uid == v_uid:
                    component_cost[u_uid] &= w
                    continue
                u_set = uid_set[u_uid]
                v_set = uid_set[v_uid]

                if len(u_set) >= len(v_set):
                    u_set = helper(u_uid, u_set, v_set)
                    uid_set[u_uid] = u_set
                    component_cost[u_uid] &= component_cost[v_uid]
                    component_cost[u_uid] &= w
                    del uid_set[v_uid]
                    del component_cost[v_uid]
                else:
                    v_set = helper(v_uid, v_set, u_set)
                    uid_set[v_uid] = v_set
                    component_cost[v_uid] &= component_cost[u_uid]
                    component_cost[v_uid] &= w
                    del uid_set[u_uid]
                    del component_cost[u_uid]
            elif u in node_union_map:
                node_union_map[v] = node_union_map[u]
                cuid = node_union_map[u]
                component_cost[cuid] &= w
                set_u = uid_set[cuid]
                set_u.add(v)
                uid_set[cuid] = set_u
            elif v in node_union_map:
                node_union_map[u] = node_union_map[v]
                cuid = node_union_map[v]
                component_cost[cuid] &= w
                set_v = uid_set[cuid]
                set_v.add(u)
                uid_set[cuid] = set_v
            else:
                node_union_map[u], node_union_map[v] = uid, uid
                cuid = uid
                component_cost[cuid] = w
                uid += 1
                uid_set[cuid] = set([u, v])
        for s, t in queries:
            if (
                s in node_union_map
                and t in node_union_map
                and node_union_map[s] == node_union_map[t]
            ):
                cuid = node_union_map[s]
                result.append(component_cost[cuid])
            else:
                result.append(-1)
        return result
```
['Array', 'Graph', 'Python3']