#include <bits/stdc++.h>
using namespace std;
int min_glb = INT_MAX;
int distSq(pair<int, int> p, pair<int, int> q) {
  return (p.first - q.first) * (p.first - q.first) +
         (p.second - q.second) * (p.second - q.second);
}
bool issquare(pair<int, int> p1, pair<int, int> p2, pair<int, int> p3,
              pair<int, int> p4) {
  int d2 = distSq(p1, p2);
  int d3 = distSq(p1, p3);
  int d4 = distSq(p1, p4);
  if (d2 == d3 && 2 * d2 == d4 && 2 * distSq(p2, p4) == distSq(p2, p3)) {
    return true;
  }
  if (d3 == d4 && 2 * d3 == d2 && 2 * distSq(p3, p2) == distSq(p3, p4)) {
    return true;
  }
  if (d2 == d4 && 2 * d2 == d3 && 2 * distSq(p2, p3) == distSq(p2, p4)) {
    return true;
  }
  return false;
}
pair<int, int> r(int num, vector<int> v) {
  if (num == 0) {
    return pair<int, int>(v[0], v[1]);
  }
  if (num == 1) {
    return pair<int, int>(-(v[1] - v[3]) + v[2], v[0] - v[2] + v[3]);
  }
  if (num == 2) {
    return pair<int, int>(-(v[0] - v[2]) + v[2], -(v[1] - v[3]) + v[3]);
  }
  if (num == 3) {
    return pair<int, int>(v[1] - v[3] + v[2], -(v[0] - v[2]) + v[3]);
  }
}
int rotate(int a, int b, int c, int d, vector<vector<int>> vec) {
  int cnt = a + b + c + d;
  pair<int, int> p1, p2, p3, p4;
  p1 = r(a, vec[0]);
  p2 = r(b, vec[1]);
  p3 = r(c, vec[2]);
  p4 = r(d, vec[3]);
  if (issquare(p1, p2, p3, p4) && (p1 != p2 || p1 != p3 || p1 != p4))
    return cnt;
  else
    return INT_MAX;
}
int main() {
  int n;
  cin >> n;
  vector<vector<int>> vec(4);
  vector<vector<int>> kthbase(256);
  for (int i = 0; i < 256; i++) kthbase[i].resize(4);
  for (int i = 0; i < 4; i++) {
    kthbase[0][i] = 0;
  }
  for (int i = 1; i < 256; i++) {
    for (int j = 0; j < 4; j++) {
      kthbase[i][j] = kthbase[i - 1][j];
    }
    int ind = 3;
    while (true) {
      kthbase[i][ind] = (kthbase[i][ind] + 1) % 4;
      if (kthbase[i][ind] == 0) {
        ind--;
      } else
        break;
    }
  }
  for (int i = 0; i < n; i++) {
    int m_rot = INT_MAX;
    for (int j = 0; j < 4; j++) {
      vec[j].resize(4);
      cin >> vec[j][0] >> vec[j][1] >> vec[j][2] >> vec[j][3];
    }
    for (int k = 0; k < 256; k++) {
      int rot = rotate(kthbase[k][0], kthbase[k][1], kthbase[k][2],
                       kthbase[k][3], vec);
      m_rot = min(rot, m_rot);
    }
    if (m_rot == INT_MAX) {
      cout << -1 << endl;
      continue;
    }
    cout << m_rot << endl;
  }
  return 0;
}
