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

#define all(x) x.begin(), x.end()
#define sz(x) (int) x.size()
#define pb push_back
#define endl '\n'
#define snd second
#define fst first
#define fastio cin.tie(NULL),cout.sync_with_stdio(true)

typedef long long int ll;
typedef unsigned long long int ull;
typedef vector <int> vi;
typedef pair <int, int> ii;

const int mod = 1e9 + 7;
const ll INF = 1e18;
const double EPSILON = 0.001;
const int N = 1e6+5;
ll inverse(ll a, ll b){
    ll inv = -1;
    ll q, r, r1 = a, r2 = b, t, t1 = 0, t2 = 1;
 
    while (r2 > 0){
        q = r1 / r2;
        r = r1 - q * r2;
        r1 = r2;
        r2 = r;
 
        t = t1 - q * t2;
        t1 = t2;
        t2 = t;
    }
 
    if (r1 == 1)
        inv = t1;
 
    if (inv < 0)
        inv = inv + a;
 
    return inv;
}
ll mmc(ll a, ll b){
    return (1ll * a * b )/ (1ll * __gcd(a,b));
}
//https://cp-algorithms.com/algebra/linear_congruence_equation.html
int main(){
    fastio;
    int t;
    cin >> t;
    while(t--){
        ll n,s,a;
        cin >> n >> s >> a;
        ll d = (ll)__gcd(a,n);
        ll b = -s + n;

        ll a1 = a/d;
        ll b1 = b/d;
        ll n1 = n/d;

        ll x = (b1 * inverse(n1, a1)) % n1;

        if(((s+x*a)%n) != 0)
            cout << -1 << '\n';

        else cout << x << '\n';
    }
    return 0;
}
#include <bits/stdc++.h>
using namespace std;

int main()
{
	int n;
	long long ans = 0;
	cin >> n;
	vector<int> t(n);
	vector<double> l(n);
	vector<double> r(n);
	for (int i = 0; i < n; i++)
	{
		cin >> t.at(i) >> l.at(i) >> r.at(i);
	}
	for (int i = 0; i < n; i++)
	{
		switch (t.at(i))
		{
		case 1:
			break;
		case 2:
			r.at(i) -= 0.5;
			break;
		case 3:
			l.at(i) += 0.5;
			break;
		case 4:
			r.at(i) -= 0.5;
			l.at(i) += 0.5;
			break;
		}
	}
	for (int i = 0; i < n - 1; i++)
	{
		if (r.at(i) < l.at(i))
		{
			continue;
		}
		for (int j = i + 1; j < n; j++)
		{
			if (r.at(j) < l.at(i) || l.at(j) > r.at(i) || r.at(j) < l.at(j))
			{
				//cout << i << ' ' << j << endl;
				continue;
			}
			ans++;
		}
	}
	cout << ans << endl;
	return 0;
}
