#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:60777216")
using namespace std;
inline string sbstr(string s, int i, int j) { return s.substr(i, j - i + 1); }
const double EPS = 1e-9;
const int IINF = 1000000000;
const double PI = acos(-1.0);
const long long LINF = 6000000000000000000LL;
struct Point {
  int x;
  int y;
  Point() : x(0), y(0) {}
  Point(int _x, int _y) : x(_x), y(_y) {}
  void rotate_90() {
    int x_new = -y;
    int y_new = x;
    x = x_new;
    y = y_new;
  }
  Point operator+(const Point& rhs) { return Point(x + rhs.x, y + rhs.y); }
  Point operator-(const Point& rhs) { return Point(x - rhs.x, y - rhs.y); }
  void rotate(int num) {
    for (int i = 0; i < num; ++i) {
      rotate_90();
    }
  }
  int operator*(const Point& rhs) {
    int ans = x * rhs.x + y * rhs.y;
    return ans;
  }
  void Read() { scanf("%d%d", &x, &y); }
};
double dist(Point a, Point b) {
  double X1 = a.x, Y1 = a.y;
  double X2 = b.x, Y2 = b.y;
  double d1 = X2 - X1;
  d1 *= d1;
  double d2 = Y2 - Y1;
  d2 *= d2;
  return sqrt(d1 + d2);
}
bool is_it(Point X1, Point X2, Point X3, Point X4) {
  double d1 = dist(X1, X2), d2 = dist(X2, X3), d3 = dist(X3, X4),
         d4 = dist(X4, X1), dg1 = dist(X1, X3), dg2 = dist(X2, X4);
  if (fabs(d1 - d2) <= EPS && fabs(d2 - d3) <= EPS && fabs(d3 - d4) <= EPS &&
      fabs(d4 - d1) <= EPS && fabs(dg1 - dg2) <= EPS &&
      fabs(dg1 - hypot(d1, d2)) <= EPS && d1 > EPS) {
    Point r1 = X1 - X2, r2 = X3 - X2;
    Point w1 = X1 - X4, w2 = X3 - X4;
    if (r1 * r2 == 0 && w1 * w2 == 0) {
      return true;
    } else {
      return false;
    }
  }
  return false;
}
bool is_square(Point X1, Point X2, Point X3, Point X4) {
  bool ok = false;
  vector<Point> v;
  v.push_back(X1);
  v.push_back(X2);
  v.push_back(X3);
  v.push_back(X4);
  vector<int> idx;
  for (int i = 0; i < 4; ++i) {
    idx.push_back(i);
  }
  do {
    if (is_it(v[idx[0]], v[idx[1]], v[idx[2]], v[idx[3]])) {
      ok = true;
      break;
    }
  } while (next_permutation(idx.begin(), idx.end()));
  return ok;
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    int ans = IINF;
    Point a1, b1;
    b1.Read(), a1.Read();
    Point a2, b2;
    b2.Read(), a2.Read();
    Point a3, b3;
    b3.Read(), a3.Read();
    Point a4, b4;
    b4.Read(), a4.Read();
    for (int k1 = 0; k1 < 4; ++k1) {
      for (int k2 = 0; k2 < 4; ++k2) {
        for (int k3 = 0; k3 < 4; ++k3) {
          for (int k4 = 0; k4 < 4; ++k4) {
            Point v1 = b1 - a1, v2 = b2 - a2, v3 = b3 - a3, v4 = b4 - a4;
            v1.rotate(k1), v2.rotate(k2), v3.rotate(k3), v4.rotate(k4);
            if (is_square(a1 + v1, a2 + v2, a3 + v3, a4 + v4)) {
              ans = min(ans, k1 + k2 + k3 + k4);
            }
          }
        }
      }
    }
    if (ans < IINF) {
      printf("%d\n", ans);
    } else {
      printf("-1\n");
    }
  }
  return 0;
}
