Captain Marmot wants to prepare a huge and important battle against his enemy, Captain Snake. For this battle he has n regiments, each consisting of 4 moles.

Initially, each mole i (1 ≤ i ≤ 4n) is placed at some position (xi, yi) in the Cartesian plane. Captain Marmot wants to move some moles to make the regiments compact, if it's possible.

Each mole i has a home placed at the position (ai, bi). Moving this mole one time means rotating his position point (xi, yi) 90 degrees counter-clockwise around it's home point (ai, bi).

A regiment is compact only if the position points of the 4 moles form a square with non-zero area.

Help Captain Marmot to find out for each regiment the minimal number of moves required to make that regiment compact, if it's possible.

Input

The first line contains one integer n (1 ≤ n ≤ 100), the number of regiments.

The next 4n lines contain 4 integers xi, yi, ai, bi ( - 104 ≤ xi, yi, ai, bi ≤ 104).

Output

Print n lines to the standard output. If the regiment i can be made compact, the i-th line should contain one integer, the minimal number of required moves. Otherwise, on the i-th line print "-1" (without quotes).

Examples

Input

4
1 1 0 0
-1 1 0 0
-1 1 0 0
1 -1 0 0
1 1 0 0
-2 1 0 0
-1 1 0 0
1 -1 0 0
1 1 0 0
-1 1 0 0
-1 1 0 0
-1 1 0 0
2 2 0 1
-1 0 0 -2
3 0 0 -2
-1 1 -2 0


Output

1
-1
3
3

Note

In the first regiment we can move once the second or the third mole.

We can't make the second regiment compact.

In the third regiment, from the last 3 moles we can move once one and twice another one.

In the fourth regiment, we can move twice the first mole and once the third mole.
import java.awt.*;
import java.io.*; 
import java.math.*;
import java.util.*;
import java.util.Map.Entry;

import static java.lang.Math.*; 
   
public class solver implements Runnable { 
        final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null; 
        BufferedReader in; 
        PrintWriter out; 
        StringTokenizer tok = new StringTokenizer(""); 
        
        boolean HasTokens(){
            while(!tok.hasMoreTokens()){ 
                try{ 
                        tok = new StringTokenizer(in.readLine()); 
                }catch (Exception e){ 
                        return false; 
                } 
            }
            return true;
        }
          
        String readString() throws IOException{                 
            return HasTokens() ? tok.nextToken():null; 
        } 
        String readString(String delim) throws IOException{ 
            return HasTokens() ? tok.nextToken(delim):null;
        } 
         
        int readInt() throws IOException{ 
            return Integer.parseInt(readString()); 
        }   
        long readLong() throws IOException{ 
            return Long.parseLong(readString()); 
        }       
        double readDouble() throws IOException{ 
            return Double.parseDouble(readString()); 
        }  
        char readChar() throws IOException { 
            char val = 0; 
            do{ 
                val = (char) in.read(); 
            }while(val == '\n' || val == '\r'); 
            return val; 
        } 
//----------------------------------
        int gcd(int x, int y){
            while (y != 0) {
                int temp = x%y; x = y; y = temp;
            }
            return x;
        }
//----------------------------------        
          
        public class Pair<T1,T2> { 
            public T1 x1; 
            public T2 x2; 
            public Pair(){}; 
            public Pair(T1 x1, T2 x2){ 
                this.x1 = x1; 
                this.x2 = x2; 
            } 
            public String toString(){ 
                return x1 + ":" + x2; 
            } 
        }  
        
        public class Three implements Comparable<Three>{ 
            public int x1, x2, x3; 
            public Three(){} 
            public Three(int a, int b, int c){x1 = a;x2=b;x3=c;} 
            public int compareTo(Three o) {//               
                if(x1 == o.x1 && x2 == o.x2) return x3-o.x3;
                if(x1 == o.x1) return x2-o.x2;
                return (x1-o.x1);  
                // (x1-o.x1) - increase; -(x1-o.x1) - decrease 
            } 
            public String toString(){ 
                return x1 + ":" + x2 + ":" + x3; 
            } 
        } 
//----------------------------------------  
       int mod = (int)1e9+7;        
        
        void solve() throws NumberFormatException, IOException {
            int n = readInt();     
            
            
            for (int i = 0; i < n; i++){
            	Point a = readPoint();
            	Point aa = readPoint();
            	Point b = readPoint();
            	Point bb = readPoint();
            	Point c = readPoint();
            	Point cc = readPoint();
            	Point d = readPoint();
            	Point dd = readPoint();
            	
            	int ans = 1000;
            	
            	for (int q = 0; q < 4; q++){
            		Point t = turn(a, aa, q);
            		for (int w = 0; w < 4; w++){
            			Point y = turn(b, bb, w);
            			for (int e = 0; e < 4; e++){
            				Point u = turn(c, cc, e);
            				for (int r = 0; r < 4; r++){
            					Point o = turn(d, dd, r);
            					
            					boolean isS = is(t,y,u,o);
            					
            					int z = q+w+e+r;
            					
            					if (isS && z < ans){
            						ans = min(ans, z);
            					}
            				}
            			}
            		}
            	}
            	
            	if (ans == 1000){
            		out.println("-1");
            	}else{
            		out.println(ans);
            	}
            }
        }
        
        Point readPoint() throws IOException{
        	return new Point(readInt(), readInt());
        }
        
        Point turn(Point p, Point b, int c){
        	
        	for (int i = 0; i < c; i++){
        		 p = turn(p, b);
        	}
        	return p;
        }
        
        Point turn(Point p, Point b){
        	int x = p.x - b.x;
        	int y = p.y - b.y;
        	
        	int xx = -y;
        	int yy = x;
        	return new Point(xx + b.x, yy + b.y);
        }
        
        boolean is(Point a, Point b, Point c, Point d){
        	return isSquare(a, b, c, d) ||
        			isSquare(a, b, d, c) ||
        			isSquare(a, c, b, d) ||
        			isSquare(a, c, d, b) ||
        			isSquare(a, d, b, c) ||
        			isSquare(a, d, c, b) ||
        			
        			isSquare(b, a, c, d) ||
        			isSquare(b, a, d, c) ||
        			isSquare(b, c, a, d) ||
        			isSquare(b, c, d, a) ||
        			isSquare(b, d, a, c) ||
        			isSquare(b, d, c, a) ||
        			
        			isSquare(c, a, b, d) ||
        			isSquare(c, a, d, b) ||
        			isSquare(c, b, a, d) ||
        			isSquare(c, b, d, a) ||
        			isSquare(c, d, a, b) ||
        			isSquare(c, d, b, a) ||
        			
        			isSquare(d, a, b, c) ||
        			isSquare(d, a, c, b) ||
        			isSquare(d, b, a, c) ||
        			isSquare(d, b, c, a) ||
        			isSquare(d, c, a, b) ||
        			isSquare(d, c, b, a);
        }
        
        
        boolean isSquare(Point a, Point b, Point c, Point d){
        	
        	boolean res = eq(a, b, c);
        	res &= eq(b, c, d);
        	res &= eq(c, d, a);
        	res &= eq(d, a, b);
        	
        	return res;
        }
        
        boolean eq(Point a, Point b, Point c){
        	
        	int x1 = a.x - b.x;
        	int x2 = b.x - c.x;
        	
        	int y1 = a.y - b.y;
        	int y2 = b.y - c.y;
        	
        	Point p = turn(a, b, 1);
        	Point t = turn(a, b, 3);
        	
        	return !(a.x == b.x && a.x == c.x) && !(a.y == b.y && b.y == c.y) && x1*x1 + y1*y1 == x2*x2+y2*y2 && (p.x == c.x && p.y == c.y || t.x == c.x && t.y == c.y);
        }
        
        
        
//---------------------------------------- 
        public static void main(String[] args){ 
            new Thread(null, new solver(), "", 256 * (1L << 20)).start(); 
        } 
         
        public void run(){ 
                try{ 
                        timeBegin = System.currentTimeMillis(); 
                        try{ 
                            if (ONLINE_JUDGE){ 
//                                in = new BufferedReader(new InputStreamReader(System.in)); 
//                                out = new PrintWriter(System.out); 
                                in = new BufferedReader(new FileReader("log.in")); 
                                out = new PrintWriter("log.out"); 
                            }  
                            else { 
                                in = new BufferedReader(new FileReader("input.txt")); 
                                out = new PrintWriter("output.txt"); 
                            } 
                        }catch(Throwable e){
                            in = new BufferedReader(new InputStreamReader(System.in)); 
                            out = new PrintWriter(System.out); 
                        }
                          
                        solve(); 
                        out.close(); 
                          
                        timeEnd = System.currentTimeMillis(); 
                        System.err.println("Time = " + (timeEnd - timeBegin)); 
                }catch (Exception e){ 
                        e.printStackTrace(System.err); 
                        System.exit(-1); 
                } 
        } 
         
        long timeBegin, timeEnd; 
}