import java.util.ArrayDeque
import java.util.Queue
import kotlin.math.abs

const val MOD = 998_244_353

data class State(val values: List<Int>)

fun main() {
    val testsN = readln().toInt()
    repeat(testsN) {
        val n = readln().toInt()
        val initialState = readState()
        val goalState = readState()

        val dist = mutableMapOf<State, Int>()
        val queue: Queue<State> = ArrayDeque()
        queue.add(initialState)
        dist[initialState] = 0
        while (!queue.isEmpty()) {
            val cur = queue.poll();
            for (fromRod in 1..3) {
                for (toRod in 1..3) {
                    if (abs(fromRod - toRod) != 1) {
                        continue
                    }
                    val diskFrom = cur.values.indexOfFirst { it == fromRod }
                    val diskTo = cur.values.indexOfFirst { it == toRod }
                    if (diskFrom == -1) {
                        continue
                    }
                    if (diskTo != -1 && diskTo < diskFrom) {
                        continue
                    }
                    val newValues = cur.values.toMutableList()
                    newValues[diskFrom] = toRod
                    val newState = State(newValues)
                    if (!dist.containsKey(newState)) {
                        dist[newState] = dist[cur]!! + 1
                        queue.add(newState)
                    }
                }
            }
        }
        println(dist[goalState])
    }
}

internal fun readState(): State {
    val values = readln().split(" ").map { it.toInt() }.toMutableList()
    return State(values)
}
