// Arup Guha
// 2/27/2024
// Solution to 2010 Mercer Problem: Eight Puzzle

// Coded in COP 3503 to illustrate interesting BFS and 
// an example of "compressing" a board state and expanding it back out.

import java.util.*;

public class puzzle {
	
	final public static int[] DX = {-1,0,0,1};
	final public static int[] DY = {0,-1,1,0};

	public static int[] board;
	public static int[] fact;
	
	public static void main(String[] args) {
		
		Scanner stdin = new Scanner(System.in);

		// Store factorials.
		fact = new int[10];
		fact[0] = 1;
		for (int i=1; i<10; i++)
			fact[i] = fact[i-1]*i;
		
		// Solution Board.
		board = new int[9];
		for (int i=0; i<8; i++)
			board[i] = i+1;
		
		// Set up the distance array, all items not reached.
		int[] dist = new int[fact[9]];
		for (int i=0; i<dist.length; i++)
			dist[i] = -1;
		int start = rank(board);
		dist[start] = 0;
		
		// Add starting position to queue.
		ArrayDeque<Integer> q = new ArrayDeque<Integer>();
		q.offer(start);
		
		// Start BFS.
		while (q.size() > 0) {
			
			// This is an integer.
			int cur = q.poll();
			
			// What my current board looks like.
			int[] newb = getboard(cur, 9);
			
			ArrayList<Integer> next = getNext(newb);
			
			// These are all the boards I can move to in one move.
			for (Integer x: next) {
				if (dist[x] != -1) continue;
				dist[x] = dist[cur] + 1;
				q.offer(x);
			}
		}
		
		// Process cases.
		int nC = stdin.nextInt();
		for (int loop=0; loop<nC; loop++) {
			
			// Read in the board.
			int[] tmp = new int[9];
			for (int i=0; i<9; i++)
				tmp[i] = stdin.nextInt();
			
			// Answer is stored, just print it out.
			System.out.println(dist[rank(tmp)]);
		}
	}

	// Swaps arr[i] and arr[j].
	public static void swap(int[] arr, int i, int j) {
		int tmp = arr[i];
		arr[i] = arr[j];
		arr[j] = tmp;
	}
	
	// Returns the next possible board positions (in one slide) from the
	// position newb storing each as a code.
	public static ArrayList<Integer> getNext(int[] newb) {
		
		// Find where 0 is.
		int loc = -1;
		for (int i=0; i<newb.length; i++)
			if (newb[i] == 0)
				loc = i;
			
		// Row and column.
		int x = loc/3;
		int y = loc%3;
		
		// Store answers here.
		ArrayList<Integer> res = new ArrayList<Integer>();
		
		// Try each sliding direction.
		for (int i=0; i<DX.length; i++) {
			
			// Square we'll swap with.
			int nx = x + DX[i];
			int ny = y + DY[i];
			
			// Out of bounds.
			if (nx<0 || nx>=3 || ny<0 || ny>=3) continue; 
			
			// Swap, add the appropriate code and swap back.
			swap(newb, 3*x+y, 3*nx+ny);
			res.add(rank(newb));
			swap(newb, 3*x+y, 3*nx+ny);
		}
		
		return res;
	}
	
	// Returns the board that corresponds to this rank of size n.
	public static int[] getboard(int rank, int n) {
		
		// Store answer here.
		int[] res = new int[n];
		
		// We will pull the values from here.
		ArrayList<Integer> unused = new ArrayList<Integer>();
		for (int i=0; i<n; i++)
			unused.add(i);
		
		// Fill in each slot.
		for (int i=0; i<n; i++) {
			
			// This is how many items we skip out of the unused ones.
			int idx = rank/fact[n-1-i];
			
			// Place it.
			res[i] = unused.get(idx);
			
			// Remove this item, now that it's used from the unused list.
			unused.remove(idx);
			
			// Update the rank.
			rank -= idx*fact[n-1-i];
		}
		
		return res;
	}

	// Returns the rank of this permutation.
	public static int rank(int[] perm) {
	
		int n = perm.length;
		
		// Numbers not in permutation yet.
		ArrayList<Integer> unused = new ArrayList<Integer>();
		for (int i=0; i<n; i++)
			unused.add(i);
			
		int res = 0;
		for (int i=0; i<n; i++) {
			
			// cur number is perm[i] find its index in unused.
			int idx = unused.indexOf(perm[i]);
			
			// The factorial represents how many permutations after this slot.
			// idx represents how many numbers we're skipping.
			res += idx*fact[n-1-i];
			
			// Remove the value at this index.
			unused.remove(idx);
		}
		
		return res;
	}
}