C++ solution using DSU
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: O(N)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(N)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass DSU{\nprivate:\n    vector<int> par,rank,weights;\npublic:\n    DSU(int n){\n        par.resize(n);\n        rank.resize(n,0);\n        weights.resize(n,131071);\n\n        for(int i=0;i<n;i++){\n            par[i]=i;\n        }\n    }\n\n    int find(int x){\n        if(x!=par[x]){\n            par[x]=find(par[x]);\n        }\n        return par[x];\n    }\n\n    void union_(int x,int y,int wt){\n        int xx=find(x),yy=find(y);\n\n        if(rank[xx]<rank[yy]){\n            par[xx]=yy;\n        }\n        else{\n            par[yy]=xx;\n        }\n\n        weights[xx]=weights[yy]=weights[xx]&weights[yy]&wt;\n\n        if(rank[xx]==rank[yy]) rank[xx]++;\n\n\n    }\n\n    int minCostWalk(int x,int y){\n        if(x==y) return 0;\n        if(find(x)!=find(y)) return -1;\n        return weights[find(x)];\n    }\n\n};\n\n\nclass Solution {\npublic:\n    vector<int> minimumCost(int n, vector<vector<int>>& edges, vector<vector<int>>& query) {\n        vector<int> ans;\n\n        DSU dsu(n);\n\n        for(auto e: edges){\n            dsu.union_(e[0],e[1],e[2]);\n\n        }\n\n        for(auto q: query){\n            int z=dsu.minCostWalk(q[0],q[1]);\n            ans.push_back(z);\n        }\n\n        return ans;\n    }\n};\n```
['C++']