import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Writer;
import java.math.BigInteger;
import java.util.StringTokenizer;

public class Main {

	public static void main(String[] args)throws IOException{
		InputStream inputStream;
		OutputStream outputStream;
		
		inputStream = new FileInputStream("tower.in");
		outputStream = System.out;
		
		InputReader in = new InputReader(inputStream);
		OutputWriter out = new OutputWriter(outputStream);
	
		int t = in.nextInt();
		for(int test=0; test<t; ++test){
			
			int n = in.nextInt();
			int m = in.nextInt();
			
			int max = 0;
			
			Point[] a = new Point[n*m+1];
			for(int i=0; i<n; ++i)
				for(int j=0; j<m; ++j){
					int cur = in.nextInt();
					if(cur!=-1) a[cur-1] = new Point(i,j);
					max = Math.max(max, cur);
				}
			
			int ans=0;
			int[] cur = new int[n];
			
			for(int i=0; i<max; ++i){
				int h = a[i].x;
				int l = a[i].y;
				
				
				
				ans+=2*10*h;
				
				int x=Integer.MAX_VALUE;
				if(l>cur[h])
					x = Math.min(l-cur[h], m+cur[h]-l);
				else
					x = Math.min(cur[h]-l, m-cur[h]+l);
				
				
				
				ans+=5*x;
				
				cur[h]=l;
				
				
			}
			
			
			
			out.writeln(ans);
			
		}
		
		out.flush();
		out.close();
	
	}
		
	
	
	static int gcd(int a, int b){
		return b==0 ? a : gcd(b,a%b);
	}
}

class Point{
	
	public Point(int _x, int _y){
		x = _x;
		y = _y;
	}
	
	int x,y;
}

class InputReader{
	
	public InputReader(InputStream stream){
		reader = new BufferedReader(new InputStreamReader(stream));
		tokenizer=null;
	}
	
	public String next(){
		while(tokenizer==null || !tokenizer.hasMoreTokens()){
			try {
				tokenizer = new StringTokenizer(reader.readLine());
			} catch (IOException e) {
				throw new RuntimeException(e);
			}
		}
		
		return tokenizer.nextToken();
	}
	
	public String nextLine(){
		tokenizer=null;
		try {
			return reader.readLine();
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}
	
	public int nextInt(){
		return Integer.parseInt(next());
	}
	
	public long nextLong(){
		return Long.parseLong(next());
	}
	
	public double nextDouble(){
		return Double.parseDouble(next());
	}
	
	public int[] nextIntArr(int n){
		int[] rez = new int[n];
		for(int i=0; i<n; ++i)
			rez[i]=nextInt();
		
		return rez;
	}
	
	private BufferedReader reader;
	private StringTokenizer tokenizer;
}

class OutputWriter{
	
	public OutputWriter(Writer out){
		this.out = new PrintWriter(out);
	}
	
	public OutputWriter(OutputStream out){
		this.out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(out)));
	}
	
	public void write(Object ...o){
		for(Object cur : o)
			out.print(cur);
		
	}
	
	public void writeln(Object ...o){
		write(o);
		out.println();
	}
	
	public void flush(){
		out.flush();
	}
	
	public void close(){
		out.close();
	}
	
	private PrintWriter out;
}
