Three approaches. Beats 100.00% time, 100.00% mem.
Approach
Union Find with depth.
Union Find w/o depth.
Depth-First Search.
Complexity
Time complexity: O(v+e+q), v - vertices, e - edges, q - queries

Space comp
# Approach
1. Union Find with depth.
2. Union Find w/o depth.
3. Depth-First Search.

![1.png](https://assets.leetcode.com/users/images/4ccdb87d-bca1-45f6-8bd0-3a4b45493708_1742488113.5681171.png)

# Complexity
- Time complexity: $$O(v + e + q)$$, v - vertices, e - edges, q - queries
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity: $$O(n)$$, DFS $$O(n + e)$$
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```javascript [w/ Depth]
const find = (u, p) => {
    if (p[u] === -1) return u;
    return p[u] = find(p[u], p);
}

const minimumCost = (n, e, q) => {
    const d = new Int32Array(n); // O(v)
    const p = new Int32Array(n).fill(-1); // O(v)
    const w = new Int32Array(n).fill(-1); // O(v)

    const el = e.length;
    for (let i = 0; i < el; ++i) { // O(e)
        const pu = find(e[i][0], p);
        const pv = find(e[i][1], p);
        if (pu === pv) continue;
        if (d[pu] > d[pv]) p[pv] = pu;
        else if (d[pu] < d[pv]) p[pu] = pv;
        else {
            p[pv] = pu;
            ++d[pu];
        }
    }

    for (let i = 0; i < el; ++i) { // O(e)
        const pu = find(e[i][0], p);
        w[pu] &= e[i][2];
    }

    const ql = q.length;
    const ans = new Int32Array(ql);
    for (let i = 0; i < ql; ++i) {  // O(q)
        const pu = find(q[i][0], p);
        const pv = find(q[i][1], p);
        if (pu === pv) ans[i] = w[pu];
        else ans[i] = -1;
    }
    return ans;
};
```
```javascript [w/o Depth]
const find = (u, p) => {
    if (p[u] === u) return p[u];
    return p[u] = find(p[u], p);
}

const minimumCost = (n, e, q) => {
    const w = new Int32Array(n).fill(-1);
    const p = new Int32Array(n);
    for (let i = 0; i < n; ++i) p[i] = i;

    const el = e.length;
    for (let i = 0; i < el; ++i) {
        const pu = find(e[i][0], p);
        const pv = find(e[i][1], p);
        p[pu] = pv;
        w[pu] = w[pv] = w[pu] & w[pv] & e[i][2];
    }
    for (let i = 0; i < n; ++i) {
        p[i] = find(i, p);
    }

    const ql = q.length;
    const ans = new Int32Array(ql);
    for (let i = 0; i < ql; ++i) {
        const pu = p[q[i][0]];
        if (pu === p[q[i][1]]) ans[i] = w[pu];
        else ans[i] = -1;
    }
    return ans;
};
```
```javascript [DFS]
const minimumCost = (n, edges, query) => {
    const m = new Array(n);
    const w = new Uint32Array(n).fill(-1);
    // BUILD
    for (let u = 0; u < n; ++u) m[u] = [];
    for (const [u, v, s] of edges) {
        m[u].push(v);
        m[v].push(u);
        w[u] &= s;
        w[v] &= s;
    }
    // TRAVERSAL
    const n2g = new Uint32Array(n);
    const g = [-1]; // s1, s2 ...
    let s; // s = w1 & w2 & w3 ...
    let i; // index in g
    const dfs = (u) => {
        n2g[u] = i;
        s &= w[u];
        for (const v of m[u])
            if (n2g[v] === 0) dfs(v);
    };
    for (let u = 0; u < n; ++u) {
        if (n2g[u] === 0) {
            i = g.length;
            s = -1;
            dfs(u);
            g.push(s);
        }
    }
    // ANSWER
    const l = query.length
    const ans = new Int32Array(l);
    for (let i = 0; i < l; ++i) {
        const u = query[i][0];
        const v = query[i][1];
        if (n2g[u] === n2g[v]) ans[i] = g[n2g[u]];
        else ans[i] = -1;
    }
    return ans;
};
```
['Array', 'Bit Manipulation', 'Depth-First Search', 'Union Find', 'Graph', 'TypeScript', 'JavaScript']