#include <bits/stdc++.h>
#include <numeric>
using namespace std;

#define int long long
#define FOR(i, size) for (int i = 0; i < size; i++)
#define all(x) x.begin(), x.end()
#define endl "\n"
#define MOD 1000000007

#define deb(...) logger(#__VA_ARGS__, __VA_ARGS__)
template <typename... Args>
void logger(string vars, Args &&...values)
{
	cout << vars << " = ";
	string delim = "";
	(..., (cout << delim << values, delim = ", "));
}

template <typename T>
void print_list(vector<T> collection, bool newline = true)
{
	for (const auto &item : collection)
	{
		cout << item << " ";
	}
	if (newline)
		cout << endl;
}

#define ONE_TESTCASE 1

int n, m, timer;

void Solve()
{
	cin >> n;
	vector<pair<double, double>> v;
	FOR(i, n)
	{
		int t;
		cin >> t;

		double a, b;
		cin >> a >> b;

		if (t == 2)
		{
			b -= 0.1;
		}
		else if (t == 3)
		{
			a += 0.1;
		}
		else if (t == 4)
		{
			a += 0.1;
			b -= 0.1;
		}

		v.push_back({a, b});
	}

	int cnt = 0;
	for (int i = 0; i < n - 1; i++)
	{
		for (int j = i + 1; j < n; j++)
		{
			if ((v[i].second >= v[j].first and v[i].first <= v[j].first) or (v[j].second >= v[i].first and v[i].first >= v[j].first))
				cnt++;
		}
	}
	cout << cnt;
}

int32_t main()
{

#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif

	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

#if ONE_TESTCASE
	int test = 1;
#else
	int test;
	cin >> test;
#endif

	for (int t = 1; t <= test; t++)
	{
		Solve();
		cout << endl;
	}
}

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int inf=1e9+7;
const ll INF=1e18+7;
int n,t;
long double a[2001],b[2001];
bool intersect(int i,int j){
	if(a[i]<=b[j]&&b[i]>=a[j]) return 1;
	return 0;
}
int main(){
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>t>>a[i]>>b[i];
		if(t>2) a[i]+=0.5;
		if(t%2==0) b[i]-=0.5;
	}
	int ans=0;
	for(int i=0;i<n-1;i++)
		for(int j=i+1;j<n;j++) 
			if(intersect(i,j))
				ans++;
	cout<<ans;
	return 0;
}
