JavaScript Solutions Beats 100%
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:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nlet parent;\nconst merge = (v) => {\n    if(parent[v] !== v) parent[v] = merge(parent[v])\n    return parent[v];\n} \nvar minimumCost = function(n, edges, query) {\n    parent = [];\n    const costs = []\n    for(let i = 0; i < n; i++) {\n        parent[i] = i;\n        costs[i] = 131071;\n    }\n    \n    for(const [u,v,w] of edges) {\n        const p1 = merge(u);\n        const p2 = merge(v);\n        parent[p1] = p2;\n        costs[p1] = costs[p2] = costs[p1] & costs[p2] & w;\n    }\n    \n    for(let i = 0; i < n; i++) {\n        parent[i] = merge(i);\n    }\n\n    const result = [];\n    for(const [s,t] of query) {\n        if(s === t) result.push(0)\n        else if(parent[s] === parent[t]) {\n        // else if(merge(s) === merge(t)) {\n            result.push(costs[parent[s]])\n        }\n        else result.push(-1);\n    }\n    return result;\n};\n```
['JavaScript']