Simple JAVA solution using DSU and Connected Components
Intuition
AND will always keep decreasing as number of values increases.
Ex: a & b & c will always be smaller than a & b, irrespective of c.
So to find the mini
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
- **AND** will always keep decreasing as number of values increases.
- Ex: a & b & c will always be smaller than a & b, irrespective of c.
- So to find the minimum **AND** path b/w s and t, we need to include as many edges as possible.
- As we can use an edge multiple times, we can always include all the edges in connected component containg both s and t.
- If there is no connected component containing both s and t, return -1 for that query becuase it is impossible to reach t from s, else return **AND** of all edges in the connected component. 

# Approach
<!-- Describe your approach to solving the problem. -->
- Using DSU find which component does each vertex belong to
- Then based on parent of each vertex find component **AND**s
- Iterate through each query and return component AND if they have same parent, else return -1

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

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

# Code
```java []
class Solution {
    public int find(int parent[], int i){
        return parent[i] == i ? i : (parent[i] = find(parent, parent[i]));
    }
    public void union(int parent[], int i, int j){
        parent[find(parent, j)] = find(parent, i);
    }
    public int[] minimumCost(int n, int[][] edges, int[][] query) {
        int parent[] = new int[n], ans[] = new int[query.length];
        HashMap<Integer, Integer> componentAND = new HashMap<>();

        for(int i = 0; i < n; i++) parent[i] = i;
        for(int edge[]: edges) union(parent, edge[0], edge[1]);

        for(int edge[]: edges){
            int rep = find(parent, edge[0]), mask = (1 << 31) - 1;
            componentAND.put(rep, componentAND.getOrDefault(rep, mask) & edge[2]);
        }
        
        for(int i = 0; i < query.length; i++){
            int repS = find(parent, query[i][0]), repT = find(parent, query[i][1]);
            ans[i] = repS != repT ? -1 : componentAND.get(repS);
        }
        return ans;
    }
}
```
['Java']