#include <bits/stdc++.h>
using namespace std;
void rotate(long long n, long long *from, long long *to) {
  for (long long i = 0; i < 4; i++) to[i] = from[i];
  for (long long i = 0; i < n; i++) {
    long long a = to[2] - to[0], b = to[3] - to[1];
    to[0] = to[2] + b;
    to[1] = to[3] - a;
  }
}
bool is_sqr_1(long long *d0, long long *d1, long long *d2, long long *d3) {
  long long n[3];
  n[0] = (d0[0] - d1[0]) * (d0[0] - d1[0]) + (d0[1] - d1[1]) * (d0[1] - d1[1]);
  n[1] = (d0[0] - d2[0]) * (d0[0] - d2[0]) + (d0[1] - d2[1]) * (d0[1] - d2[1]);
  n[2] = (d0[0] - d3[0]) * (d0[0] - d3[0]) + (d0[1] - d3[1]) * (d0[1] - d3[1]);
  long long max = 0;
  if (n[1] > n[0]) max = 1;
  if (n[2] > n[max]) max = 2;
  sort(n, n + 3);
  return (n[0] == n[1] && n[1] * 2 == n[2] && n[2] != 0);
}
bool pis_sqr_1(long long *d0, long long *d1, long long *d2, long long *d3) {
  cout << "  ---- data: ";
  for (long long j = 0; j < 2; j++) cout << d0[j] << " ";
  for (long long j = 0; j < 2; j++) cout << d1[j] << " ";
  for (long long j = 0; j < 2; j++) cout << d2[j] << " ";
  for (long long j = 0; j < 2; j++) cout << d3[j] << " ";
  cout << endl;
  long long n[3];
  n[0] = (d0[0] - d1[0]) * (d0[0] - d1[0]) + (d0[1] - d1[1]) * (d0[1] - d1[1]);
  n[1] = (d0[0] - d2[0]) * (d0[0] - d2[0]) + (d0[1] - d2[1]) * (d0[1] - d2[1]);
  n[2] = (d0[0] - d3[0]) * (d0[0] - d3[0]) + (d0[1] - d3[1]) * (d0[1] - d3[1]);
  long long max = 0;
  if (n[1] > n[0]) max = 1;
  if (n[2] > n[max]) max = 2;
  sort(n, n + 3);
  printf("  ---- n: %d, %d, %d\n", n[0], n[1], n[2]);
  return (n[0] == n[1] && n[1] * 2 == n[2] && n[2] != 0);
}
bool is_sqr(long long *d) {
  return is_sqr_1(d, d + 4, d + 8, d + 12) &&
         is_sqr_1(d + 4, d, d + 8, d + 12) &&
         is_sqr_1(d + 8, d + 4, d, d + 12) && is_sqr_1(d + 12, d + 4, d + 8, d);
}
bool pis_sqr(long long *d) {
  cout << "PISSQR "
       << (pis_sqr_1(d, d + 4, d + 8, d + 12) &&
           pis_sqr_1(d + 4, d, d + 8, d + 12) &&
           pis_sqr_1(d + 8, d + 4, d, d + 12) &&
           pis_sqr_1(d + 12, d + 4, d + 8, d))
       << " = " << is_sqr_1(d, d + 4, d + 8, d + 12) << " "
       << is_sqr_1(d + 4, d, d + 8, d + 12) << " "
       << is_sqr_1(d + 8, d + 4, d, d + 12) << " "
       << is_sqr_1(d + 12, d + 4, d + 8, d) << endl;
  return is_sqr_1(d, d + 4, d + 8, d + 12) &&
         is_sqr_1(d + 4, d, d + 8, d + 12) &&
         is_sqr_1(d + 8, d + 4, d, d + 12) && is_sqr_1(d + 12, d + 4, d + 8, d);
}
int main() {
  long long n;
  cin >> n;
  while (n--) {
    long long d[16];
    long long d2[16];
    for (long long i = 0; i < 16; i++) {
      cin >> d[i];
    }
    long long res = 100;
    for (long long i = 0; i < 256; i++) {
      long long rot0 = i & 0b11;
      long long rot1 = (i & (0b11 << 2)) >> 2;
      long long rot2 = (i & (0b11 << 4)) >> 4;
      long long rot3 = (i & (0b11 << 6)) >> 6;
      rotate(rot0, d, d2);
      rotate(rot1, d + 4, d2 + 4);
      rotate(rot2, d + 8, d2 + 8);
      rotate(rot3, d + 12, d2 + 12);
      if (is_sqr(d2)) {
        if (res > rot0 + rot1 + rot2 + rot3)
          res = min(res, rot0 + rot1 + rot2 + rot3);
      }
    }
    cout << (res < 50 ? res : -1) << endl;
  }
  return 0;
}
