Simple knowledge of DSU and bits
Intuition\nNote that the and sum always decreases or remains constantno matter what happens.\nSo u just have to check if two nodes lie in the same component \nI
# Intuition\nNote that the and sum always decreases or remains constantno matter what happens.\nSo u just have to check if two nodes lie in the same component \nIf they do then just return the and of all the edges in that component\n....If they dont then just return -1\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(V+E+q)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(V)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass DSU{\npublic:\n    vector<int> parent;\n    vector<int> rank;\n    DSU(int n){\n        parent.resize(n, 0);\n        rank.resize(n, 0);\n        \n        for(int i=0;i<n;i++) parent[i] = i;\n    }\n\n    int Find(int node){\n        return parent[node] = (parent[node] == node) ? node : Find(parent[node]);\n    }\n\n    void Reset(int node){\n        parent[node] = node;\n    }\n\n    void Union(int nodea, int nodeb){\n        int apar = Find(nodea), bpar = Find(nodeb);\n        if(apar != bpar){\n            rank[apar] < rank[bpar] ? parent[apar] = bpar : parent[bpar] = apar;\n            rank[apar] += rank[apar] == rank[bpar] ? 1 : 0;\n        }\n    }\n};\nclass Solution {\npublic:\n    vector<int> minimumCost(int n, vector<vector<int>>& edges, vector<vector<int>>& query) {\n        DSU dsu(n);\n        unordered_map<int,int> andmap;\n\n\n        for(auto i:edges){\n              int p1=dsu.Find(i[0]);\n              int p2=dsu.Find(i[1]);\n\n              int newand=(((andmap.find(p1)==andmap.end())?INT_MAX:andmap[p1])&((andmap.find(p2)==andmap.end())?INT_MAX:andmap[p2]));\n            dsu.Union(i[0],i[1]);\n\n            int p3=dsu.Find(i[0]);\n           \n           // if(andmap[p3]!=0)\n            andmap[p3]=(newand&i[2]);\n          \n            cout<<p3<<"--->"<<andmap[p3]<<endl;\n            \n          \n            \n        }\n\n        for(int i=0;i<n;i++){\n            cout<<dsu.Find(i)<<"---->"<<andmap[dsu.Find(i)]<<endl;\n        }\n        vector<int> ans;\n        for(auto i:query){\n            int u=i[0],v=i[1];\n\n            if(dsu.Find(u)!=dsu.Find(v))\n            ans.push_back(-1);\n            else ans.push_back(andmap[dsu.Find(u)]);\n        }\n\n        return ans;\n\n\n    }\n};\n```
['Bit Manipulation', 'Union Find', 'C++']