← Home
```go
package main

import (
	"bufio"
	"os"
	"strconv"
)

func main() {
	// Use buffered I/O for speed
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	// Helper function to read the next integer
	nextInt := func() int {
		scanner.Scan()
		val, _ := strconv.Atoi(scanner.Text())
		return val
	}

	// Read number of test cases
	scanner.Scan()
	tStr := scanner.Text()
	if tStr == "" {
		return
	}
	t, _ := strconv.Atoi(tStr)

	for i := 0; i < t; i++ {
		solve(nextInt, writer)
	}
}

func solve(nextInt func() int, writer *bufio.Writer) {
	n := nextInt()
	q := nextInt()

	// Read final array b
	b := make([]int, n)
	for i := 0; i < n; i++ {
		b[i] = nextInt()
	}

	type op struct {
		x, y, z int
	}
	ops := make([]op, q)
	for i := 0; i < q; i++ {
		// Convert 1-based indices to 0-based
		ops[i] = op{
			x: nextInt() - 1,
			y: nextInt() - 1,
			z: nextInt() - 1,
		}
	}

	// d will store the reconstructed initial array a.
	// We initialize it with b because ultimately the values must satisfy the final requirements.
	d := make([]int, n)
	copy(d, b)

	// Process operations backwards to determine necessary lower bounds for a.
	for i := q - 1; i >= 0; i-- {
		o := ops[i]
		req := d[o.z]
		
		// The value currently in position z is overwritten by this operation.
		// The constraints on the output (req) must be satisfied by the result of min(c[x], c[y]).
		// This implies c[x] >= req and c[y] >= req.
		// The value in c[z] *before* this operation is lost, so its specific constraint is reset.
		// Note: If z is the same as x or y, the constraint will be re-applied correctly below.
		d[o.z] = 0

		if req > d[o.x] {
			d[o.x] = req
		}
		if req > d[o.y] {
			d[o.y] = req
		}
	}

	// Verify the solution by simulating forward.
	// The constructed 'd' guarantees that the final values will be >= b.
	// We need to check if they are exactly equal to b.
	sim := make([]int, n)
	copy(sim, d)

	for i := 0; i < q; i++ {
		o := ops[i]
		valX := sim[o.x]
		valY := sim[o.y]
		minVal := valX
		if valY < minVal {
			minVal = valY
		}
		sim[o.z] = minVal
	}

	possible := true
	for i := 0; i < n; i++ {
		if sim[i] != b[i] {
			possible = false
			break
		}
	}

	if possible {
		for i := 0; i < n; i++ {
			if i > 0 {
				writer.WriteByte(' ')
			}
			writer.WriteString(strconv.Itoa(d[i]))
		}
		writer.WriteByte('\n')
	} else {
		writer.WriteString("-1\n")
	}
}
```