#include<set>
#include<map>
#include<ctime>
#include<cmath>
#include<stack>
#include<queue>
#include<bitset>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
#define rep(i,j,k) for (int i = j; i <= k; i++)
#define per(i,j,k) for (int i = j; i >= k; i--)
#define loop(i,j,k) for (int i = j;i != -1; i = k[i])
#define lson x << 1, l, mid
#define rson x << 1 | 1, mid + 1, r
#define fi first
#define se second
#define mp(i,j) make_pair(i,j)
#define pii pair<int,int>
using namespace std;
typedef long long LL;
const int low(int x) { return x&-x; }
const double eps = 1e-8;
const int INF = 0x7FFFFFFF;
const int mod = 1e9 + 7;
const int N = 2e5 + 10;
const int read()
{
char ch = getchar();
while (ch<'0' || ch>'9') ch = getchar();
int x = ch - '0';
while ((ch = getchar()) >= '0'&&ch <= '9') x = x * 10 + ch - '0';
return x;
}
int T, n, x, y, xx, yy, sz, t;
int f[20][N], g[20][N];
struct point
{
int x, l, r;
point(int x = 0, int l = 0, int r = 0) :x(x), l(l), r(r) {}
bool operator<(const point&a)const
{
return x < a.x;
}
}a[N], b[N];
void build(int d, int x, int l, int r)
{
if (l == r)
{
f[d][l] = a[l].l, g[d][l] = a[l].r;
return;
}
int mid = l + r >> 1;
build(d + 1, lson); build(d + 1, rson);
for (int i = l, j = l, k = mid + 1; i <= r; i++)
{
if (k > r || (j <= mid && f[d + 1][j] <= f[d + 1][k])) f[d][i] = f[d + 1][j++];
else f[d][i] = f[d + 1][k++];
}
for (int i = l, j = l, k = mid + 1; i <= r; i++)
{
if (k > r || (j <= mid && g[d + 1][j] <= g[d + 1][k])) g[d][i] = g[d + 1][j++];
else g[d][i] = g[d + 1][k++];
}
}
int get(int d, int x, int l, int r, int ll, int rr, int v)
{
if (ll <= l&&r <= rr)
{
return upper_bound(f[d] + l, f[d] + r + 1, v) - f[d] - l - (lower_bound(g[d] + l, g[d] + r + 1, v) - g[d] - l);
}
else
{
int mid = l + r >> 1, res = 0;
if (ll <= mid) res += get(d + 1, lson, ll, rr, v);
if (rr > mid) res += get(d + 1, rson, ll, rr, v);
return res;
}
}
int main()
{
T = read();
while (T--)
{
t = sz = 0;
scanf("%d", &n);
rep(i, 1, n)
{
scanf("%d%d%d%d", &x, &y, &xx, &yy);
if (x == xx) a[++sz] = point(x, min(y, yy), max(y, yy));
else b[++t] = point(y, min(x, xx), max(x, xx));
}
sort(a + 1, a + sz + 1);
build(1, 1, 1, sz);
LL ans = 0;
rep(i, 1, t)
{
int l = lower_bound(a + 1, a + sz + 1, point(b[i].l, 0, 0)) - a;
int r = upper_bound(a + 1, a + sz + 1, point(b[i].r, 0, 0)) - a - 1;
ans += get(1, 1, 1, sz, l, r, b[i].x);
}
printf("%lld\n", ans);
}
return 0;
}