Easy to understand || DisjointSet Solution
IntuitionFrom the discription and examples given we know that there may be more that one disjoint component.Now only thing that you have to think is whether you
# Intuition
From the discription and examples given we know that there may be more that one disjoint component.Now only thing that you have to think is whether your given query lies within one connected component or not.If no then surely our ans is -1 as there can't be any path between them.Else we know both lies in same connected component then we need to play with with all edges in that connected component which may involve including some unnessary path from top but since we can visit edges and vertices more than once so it is valid.

Now only thing we need to think is if two element lies in one specific connected component (let's say A) then what should that connected component A should return (and that will be unique for all query that lies inside A) such that is is min.Now as far as minimizing the and of number is concern we can realize that if we have some edges in front of us then surely our ans will not be larger than min element present here (you can verify it but just for instance if we have elements as 2,3,4,5,6,7,88...  our min and that i can make will be surely 2) but we can make ans below 2 as well suppose if we have 2 and 5 the min and is that we can make is not 2 its 0 as 2&5=0 but here is the point to notice that if such pair exist then we can min and that we can make with sum number is simply and of all numbers as it includes min one and which not lets it to go beyond that and also includes pair like (2,5) which again lets it to go furthur min and finally we get our desired result.

If you got the intution then let see the approach 

# Approach
Since we required the all disjoint set and ans for all query lying inside this disjoint set have ans equal to and of all edges in this set.

SO we maintain a disjoinSet data structure(if you don't aware about this data struture then for now i can only say it is a ds to which we give so many pairs and it acts like a blackbox which make pairs having element in common as one group like if you give pairs like{{2,4},{4,5},{2,6},{3,9}} then it will group (2,4,6) as g1 and (3,9) as g2...  read this ds if still not clear),which groups all connected component as one unit and its is uniquely identified by its ulimate parent.
SO we will keep add group to disjoint set ds and do 'AND' operation of all edges encountered and stored in map in query if any eleemnt lies in one particular group just return then umap[its ulimate parent]  

# Complexity
- Time complexity:
O(M*alpha(n))

- Space complexity:
O(n)

# Below is the code but please provide your valuable feedback and ask if having any doubt i will be very happy to answer that

# Code
```cpp []
class DisjointSet{
    vector<int>rank,parent;
    public:
    DisjointSet(int n){
        rank.resize(n,1);
        parent.resize(n);
        for(int i=0;i<n;i++) parent[i]=i;
    }

    int findUPar(int u){
        if(parent[u]==u) return u;
        return findUPar(parent[u]);
    }

    void unionByRank(int u,int v){
        int ulp_u=findUPar(u);
        int ulp_v=findUPar(v);
        if(ulp_u==ulp_v) return ;
        if(rank[ulp_u]<rank[ulp_v]){
            parent[ulp_u]=ulp_v;
        }else if(rank[ulp_u]>rank[ulp_v]){
            parent[ulp_v]=ulp_u;
        }else{
            parent[ulp_u]=ulp_v;
            rank[ulp_v]++;
        }
    }
};

class Solution {
public:
    vector<int> minimumCost(int n, vector<vector<int>>& edges, vector<vector<int>>& query) {
        unordered_map<int,int>umap;//stores ultimate parent and and of all edge encountered
        DisjointSet ds(n);
        for(auto x:edges){
            if(ds.findUPar(x[0])!=ds.findUPar(x[1])){
                ds.unionByRank(x[0],x[1]);
            }
            int p=ds.findUPar(x[0]);
            if(umap.count(p)==0){
                umap[p]=x[2];
            }else umap[p]=(umap[p] & x[2]);
        }

//this step is done so that if some disjoint component in starting later merged to bigger group;
        for(auto x:umap){
            if(ds.findUPar(x.first)==x.first) continue;
            else{
                int p=ds.findUPar(x.first);
                umap[p]=(umap[p]&x.second);
            }
        }

        int m=query.size();
        vector<int>ans(m);
        for(int i=0;i<m;i++){
            auto x=query[i];
            if(ds.findUPar(x[0])!=ds.findUPar(x[1])){
                ans[i]=-1;
            }else ans[i]=umap[ds.findUPar(x[0])];
        }

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