```go
package main
import (
"bufio"
"fmt"
"os"
)
func abs64(x int64) int64 {
if x < 0 {
return -x
}
return x
}
func solvePattern(pos0, pos1 []int, n int) (int64, bool) {
c0, c1 := len(pos0), len(pos1)
start0 := (n + 1) / 2
start1 := n / 2
if c0 != start0 || c1 != start1 {
return 0, false
}
var cost int64
for i, p := range pos0 {
target := 2*i + 1
cost += abs64(int64(p - target))
}
return cost, true
}
func minSwaps(a []int, n int) int64 {
var pos0, pos1 []int
for i := 1; i <= n; i++ {
if a[i-1]&1 == 0 {
pos0 = append(pos0, i)
} else {
pos1 = append(pos1, i)
}
}
diff := len(pos0) - len(pos1)
if diff < -1 || diff > 1 {
return -1
}
const INF int64 = 1<<62 - 1
best := INF
c1, ok1 := solvePattern(pos0, pos1, n)
if ok1 {
if c1 < best {
best = c1
}
}
c2, ok2 := solvePattern(pos1, pos0, n)
if ok2 {
if c2 < best {
best = c2
}
}
if best == INF {
return -1
}
return best / 2
}
func main() {
in := bufio.NewReader(os.Stdin)
out := bufio.NewWriter(os.Stdout)
defer out.Flush()
var t int
fmt.Fscan(in, &t)
for ; t > 0; t-- {
var n int
fmt.Fscan(in, &n)
a := make([]int, n)
for i := 0; i < n; i++ {
fmt.Fscan(in, &a[i])
}
fmt.Fprintln(out, minSwaps(a, n))
}
}
```