Easy explanation through diagrams || O(M+N)
Intuition\nLet\'s start with a base case:\na&b<=a and a&b<=b\n\nThat means the more edges we explore, the less will be the &-value.\n\nSo when we have a group o
# Intuition\nLet\'s start with a base case:\na&b<=a and a&b<=b\n\nThat means the more edges we explore, the less will be the &-value.\n\nSo when we have a group of numbers:{n1,n2,....nk}, to get minimum bitwise \'AND\' value, we should do bitwise \'AND\' for the whole group.  i.e., n1&n2&...&nk.\n\nSo, the idea is to plot a walk for each query such that it covers all the edges of that component.\n\n**NOTE THAT IF n1,n2,....,nk are the edge weights, ALL THE VERTICES n1,n2,n3....,nk SHOULD BE IN A SINGLE CONNECTED COMPONENT**\n\n# Approach\n- Group the components through Breadth First Search(BFS)\n- Map each vertex to a *component index* (unique for each component)\n- Calculate bitwise \'AND\' of all edges present inside for each       connected component and map it to the component index\n\n![image.png](https://assets.leetcode.com/users/images/0f94228f-fa61-4e17-b153-c7f1466c6023_1712495541.925325.png)\n\nMAPS:\n![image.png](https://assets.leetcode.com/users/images/767a3461-3ddd-4097-99b3-554fcf6933df_1712496092.5715888.png)\n\n- Now for each query, if both the vertices are from the same component,\n- Find the component index for the vertex\n- Find the bitwise \'AND\' value for the component\n- Store it in answer[i] \n\n\n# Complexity\n- Time complexity:\nO(M+N)\nFor a graph with M edges and N vertices\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\nO(M+N)\n---\n\n\n\nFor more clarity about grouping and mapping of components, refer the code\n\nThe code seems larger bcoz of comments\n# Code\n```\nclass Solution {\npublic:\n    vector<int> minimumCost(int n, vector<vector<int>>& edges, vector<vector<int>>& query) {\n    \n    //SECTION-1: BFS:\n\n        vector<vector<int>> adj(n); \n        for(auto x:edges)\n        {\n            adj[x[0]].push_back(x[1]);\n            adj[x[1]].push_back(x[0]);\n        }\n        vector<int> vis(n,0);\n        vector<int> par(n);              //vertex-component map\n        vector<int> vand(n,INT_MAX);     //component-\'AND\' map initiated with all bits set to 1\n\n        int cnt=0;\n        for(int i=0;i<n;i++)\n        {\n            if(!vis[i])         //That means the component in which \n            {                   //vertex-i is present is not mapped. \n                queue<int> q;   //So group that component through BFS\n                vis[i]=1;       //and map it to the index cnt.     \n                q.push(i);   \n                while(!q.empty())\n                {\n                    int cur=q.front();\n                    for(auto x:adj[cur])\n                    {\n                        if(!vis[x])\n                        {vis[x]=1;q.push(x);}\n                    }\n                    par[cur]=cnt;\n                    q.pop();\n                }\n                cnt++;\n            }\n        }\n   \n    //SECTION-2: Component-\'AND\' map:\n\n        //update bitwise-& value of the component for each edge\n        for(auto x:edges)\n        {\n            vand[par[x[0]]]=vand[par[x[0]]]&x[2];\n\n            //      vand[                       par[x[0]]           ]       =       vand[                       par[x[0]]           ]       &      x[2]\n            //Bitwise \'AND\' of the          component of vertex x[0]        =  Bitwise \'AND\' of the       component of vertex x[0]          &      edge-weight\n        }\n\n    //SECTION-3: Calculating result:\n\n        vector<int> res;\n        for(auto x:query)\n        {\n            if(x[0]==x[1])\n            {res.push_back(0);continue;}\n\n            //both vertices are in same component then assign AND-VALUE of that component\n            if(par[x[0]]==par[x[1]])\n            {\n                res.push_back(vand[par[x[0]]]);\n            }\n\n            //if they are not from the same components then assign -1\n            else\n            res.push_back(-1);\n        }\n        return res;\n    }\n};\n```\n\n\n**UPVOTE IF YOU FIND IT USEFUL\uD83E\uDD7A\uD83E\uDD7A\uD83E\uDD7A**
['Breadth-First Search', 'C++']