Union Find || Python || Beats 99.28 % || 35 lines only 
Code


# Code
```python3 []
class Solution:
    
    def find(self, n):
        if self.parents[n] != n:
            return self.find(self.parents[n])
        return self.parents[n]

    def _union(self, u, v, w):
        pu = self.find(u)
        pv = self.find(v)
        if pu == pv :
            self.weights[pu] &= w
        elif pu < pv:
            self.parents[pv] = pu
            self.weights[pu] &= w & self.weights[pv]
        else:
            self.parents[pu] = pv
            self.weights[pv] &= w & self.weights[pu]
            
    def minimumCost(self, n: int, edges: List[List[int]], query: List[List[int]]) -> List[int]:
        self.weights = [131071] * (n+1)
        self.parents = [i for i in range(n+1)]
        
        for u, v, w in edges:
            self._union(u,v,w)

        res = []
        for u, v in query:
            pu = self.find(u)
            pv = self.find(v)
            if pu == pv:
                res.append(self.weights[pu])
            else:
                res.append(-1)
        return res
            
```
['Union Find', 'Python3']