#include<iostream>
#include<vector>
#include <algorithm>
using namespace std;

bool issorted(vector<long long> v){
    long long pre = v[0];
    for(long long i=1; i<v.size(); i++){
        if(pre<v[i]){
            return false;
        }
        pre = v[i];
    }
    return true;
}
long long bisect_left(vector<long long> &a, long long x,long long lo,long long hi){
    long long mid;
    if(lo < 0)
        throw "Value Error: lo must be non-negative";
    while(lo < hi){
        mid = (lo+hi)/2;
        if(a[mid] < x){
            lo = mid+1;
        }
        else{
            hi = mid;
        }
    }
    return lo;
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    long long q, n, t;
    cin>>q;
    vector<long long> min, max;
    min.reserve(q);
    max.reserve(q);
    for(long long a=0; a<q; a++){
        vector<long long> A;
        cin>>n;
        A.reserve(n);
        for(long long b=0; b<n; b++){
            cin>>t;
            A.push_back(t);
        }
        if(n == 1 || issorted(A)){
            min.push_back(A[A.size()-1]);
            max.push_back(A[0]);
        }
    }
    long long c=0;
    int Max = max.size();
    int Min = min.size();
    sort(min.begin(), min.begin() + Min);

    for(long long i=0; i<Max; i++){
        c += Min - bisect_left(min, max[i], 0, Min);
    }
    cout<<(q*q - c)<<endl;
    return 0;
}


    #include<iostream>
    #include<vector>
    using namespace std;

    bool issorted(vector<int> v){
        int pre = v[0];
        for(int i=1; i<v.size(); i++){
            // cout<<pre<<" "<<v[i]<<endl;
            if(pre<v[i]){
                return false;
            }
            pre = v[i];
        }
        return true;
    }

    int main(){
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);
        cout.tie(NULL);
        int q, n, t;
        cin>>q;
        // vector<vector<int>> v;
        vector<int> min, max;
        min.reserve(q);
        max.reserve(q);
        // int min[q], max[q]
        // v.reserve(q);
        for(int a=0; a<q; a++){
            vector<int> A;
            cin>>n;
            A.reserve(n);
            for(int b=0; b<n; b++){
                cin>>t;
                A.push_back(t);
            }
            if(issorted(A)){
                // cout<<"hi"<<issorted(A);
                min.push_back(A[A.size()-1]);
                max.push_back(A[0]);
            }
            // v.push_back(A);
        }
        int c=0;
        for(int i=0; i<min.size(); i++){
            for(int j=0; (j<min.size()); j++){
                // cout<<min[i]<<" "<<max[j]<<endl;
                if(min[i]>= max[j]){
                    c++;
                }
            }
        }
        cout<<(q*q - c)<<endl;
    }


    #include<iostream>
    #include<vector>
    using namespace std;

    bool issorted(vector<long long> v){
        long long pre = v[0];
        for(long long i=1; i<v.size(); i++){
            // cout<<pre<<" "<<v[i]<<endl;
            if(pre<v[i]){
                return false;
            }
            pre = v[i];
        }
        return true;
    }

    long long main(){
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);
        cout.tie(NULL);
        long long q, n, t;
        cin>>q;
        // vector<vector<long long>> v;
        vector<long long> min, max;
        min.reserve(q);
        max.reserve(q);
        // long long min[q], max[q]
        // v.reserve(q);
        for(long long a=0; a<q; a++){
            vector<long long> A;
            cin>>n;
            A.reserve(n);
            for(long long b=0; b<n; b++){
                cin>>t;
                A.push_back(t);
            }
            if(issorted(A)){
                // cout<<"hi"<<issorted(A);
                min.push_back(A[A.size()-1]);
                max.push_back(A[0]);
            }
            // v.push_back(A);
        }
        long long c=0;
        for(long long i=0; i<min.size(); i++){
            for(long long j=0; (j<min.size()); j++){
                // cout<<min[i]<<" "<<max[j]<<endl;
                if(min[i]>= max[j]){
                    c++;
                }
            }
        }
        cout<<(q*q - c)<<endl;
    }


#include<iostream>
#include<vector>
#include <algorithm>
using namespace std;

bool issorted(vector<long long> v){
    long long pre = v[0];
    for(long long i=1; i<v.size(); i++){
        if(pre<v[i]){
            return false;
        }
        pre = v[i];
    }
    return true;
}
long long bisect_left(vector<long long> a, long long x,long long lo,long long hi){
    long long mid;
    if(lo < 0)
        throw "Value Error: lo must be non-negative";
    while(lo < hi){
        mid = (lo+hi)/2;
        if(a[mid] < x){
            lo = mid+1;
        }
        else{
            hi = mid;
        }
    }
    return lo;
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    long long q, n, t;
    cin>>q;
    vector<long long> min, max;
    min.reserve(q);
    max.reserve(q);
    for(long long a=0; a<q; a++){
        vector<long long> A;
        cin>>n;
        A.reserve(n);
        for(long long b=0; b<n; b++){
            cin>>t;
            A.push_back(t);
        }
        if(n == 1 || issorted(A)){
            min.push_back(A[A.size()-1]);
            max.push_back(A[0]);
        }
    }
    long long c=0;
    sort(min.begin(), min.begin() + min.size());
    for(long long i=0; i<max.size(); i++){
        c += min.size() - bisect_left(min, max[i], 0, min.size());
    }
    cout<<(q*q - c)<<endl;
    return 0;
}


    #include<iostream>
    #include<vector>
    #include <algorithm>
    using namespace std;

    bool issorted(vector<long long> v){
        long long pre = v[0];
        for(long long i=1; i<v.size(); i++){
            // cout<<pre<<" "<<v[i]<<endl;
            if(pre<v[i]){
                return false;
            }
            pre = v[i];
        }
        return true;
    }

    int main(){
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);
        cout.tie(NULL);
        long long q, n, t;
        cin>>q;
        // vector<vector<long long>> v;
        vector<long long> min, max;
        min.reserve(q);
        max.reserve(q);
        // long long min[q], max[q]
        // v.reserve(q);
        for(long long a=0; a<q; a++){
            vector<long long> A;
            cin>>n;
            A.reserve(n);
            for(long long b=0; b<n; b++){
                cin>>t;
                A.push_back(t);
            }
            if(n == 1 || issorted(A)){
                // cout<<"hi"<<issorted(A);
                min.push_back(A[A.size()-1]);
                max.push_back(A[0]);
            }
            // v.push_back(A);
        }
        long long c=0;
        sort(min.begin(), min.begin() + min.size());
        sort(max.begin(), max.begin() + max.size());
        for(long long i=0; i<max.size(); i++){
            c += min.size() - binary_search(min.begin(), min.end(), max[i]);
        }
        // for(long long i=0; i<min.size(); i++){
        //     for(long long j=0; (j<min.size()); j++){
        //         // cout<<min[i]<<" "<<max[j]<<endl;
        //         if(min[i]>= max[j]){
        //             c++;
        //         }
        //     }
        // }
        cout<<(q*q - c)<<endl;
        return 0;
    }


#include<iostream>
#include<vector>
#include <algorithm>
using namespace std;

bool issorted(vector<long long> v){
    long long pre = v[0];
    for(long long i=1; i<v.size(); i++){
        if(pre<v[i]){
            return false;
        }
        pre = v[i];
    }
    return true;
}
long long bisect_left(vector<long long> a, long long x,long long lo,long long hi){
    long long mid;
    if(lo < 0)
        throw "Value Error: lo must be non-negative";
    while(lo < hi){
        mid = (lo+hi)/2;
        if(a[mid] < x){
            lo = mid+1;
        }
        else{
            hi = mid;
        }
    }
    return lo;
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    long long q, n, t;
    cin>>q;
    vector<long long> min, max;
    min.reserve(q);
    max.reserve(q);
    for(long long a=0; a<q; a++){
        vector<long long> A;
        cin>>n;
        A.reserve(n);
        for(long long b=0; b<n; b++){
            cin>>t;
            A.push_back(t);
        }
        if(n == 1 || issorted(A)){
            min.push_back(A[A.size()-1]);
            max.push_back(A[0]);
        }
    }
    long long c=0;
    int Max = max.size();
    int Min = min.size();
    sort(min.begin(), min.begin() + Min);

    for(long long i=0; i<Max; i++){
        c += Min - bisect_left(min, max[i], 0, Min);
    }
    cout<<(q*q - c)<<endl;
    return 0;
}


    #include<iostream>
    #include<vector>
    using namespace std;

    bool issorted(vector<long long> v){
        long long pre = v[0];
        for(long long i=1; i<v.size(); i++){
            // cout<<pre<<" "<<v[i]<<endl;
            if(pre<v[i]){
                return false;
            }
            pre = v[i];
        }
        return true;
    }

    int main(){
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);
        cout.tie(NULL);
        long long q, n, t;
        cin>>q;
        // vector<vector<long long>> v;
        vector<long long> min, max;
        min.reserve(q);
        max.reserve(q);
        // long long min[q], max[q]
        // v.reserve(q);
        for(long long a=0; a<q; a++){
            vector<long long> A;
            cin>>n;
            A.reserve(n);
            for(long long b=0; b<n; b++){
                cin>>t;
                A.push_back(t);
            }
            if(issorted(A)){
                // cout<<"hi"<<issorted(A);
                min.push_back(A[A.size()-1]);
                max.push_back(A[0]);
            }
            // v.push_back(A);
        }
        long long c=0;
        for(long long i=0; i<min.size(); i++){
            for(long long j=0; (j<min.size()); j++){
                // cout<<min[i]<<" "<<max[j]<<endl;
                if(min[i]>= max[j]){
                    c++;
                }
            }
        }
        cout<<(q*q - c)<<endl;
        return 0;
    }


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

int min(int a1 , int b1)
{
	if(a1>=b1) return b1;
	else return a1;
}
int max(int a2 , int b2)
{
	if(a2<=b2) return b2;
	else return a2;
}

int main()
{
	int n;
	int ans = 0;
	cin>>n;
	int maxarr[n] = {0};
	int minarr[n] = {0};
	int flag[n] = {0};
	int init = 0;
	for(int i = 0 ; i < n ; i++)
	{
		int l;
		cin >> l;
		int mintemp = 1000000;
		int maxtemp = -1;
		for(int j = 0 ; j < l ; j++)
		{
			int s;
			cin>>s;
			maxtemp = max(s,maxtemp);
			mintemp = min(s,mintemp);
			if(s>mintemp)
			{
				flag[i] = 1;
				init++;
			}
		}
		minarr[i] = mintemp;
		maxarr[i] = maxtemp;
	}
	int k=0;
	int j;
	int flag2;
	while(k<n)
	{
		j = k;
		for(j ; j < n ; j++)
		{
				flag2 = 0;
				if(minarr[k]<maxarr[j] || flag[k] || flag[j]){
					ans++;
					flag2++;
				}
				if(maxarr[k]>minarr[j] || flag[k] || flag[j]){
					ans++;
					flag2++;
				}
				if(flag2==2 && k==j) ans--;
		}
		k++;
	}
	cout << ans;
	return 0;
}


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

int min(int a1 , int b1)
{
	if(a1>=b1) return b1;
	else return a1;
}
int max(int a2 , int b2)
{
	if(a2<=b2) return b2;
	else return a2;
}

int main()
{
	int n;
	int ans = 0;
	cin>>n;
	vector<int> minarr;
	int maxarr[100000] = {0};
	int flag[n] = {0};
	int maxtemp;
	for(int i = 0 ; i < n ; i++)
	{
		int l;
		cin >> l;
		int mintemp = 1000000;
		int maxtemp = -1;
		for(int j = 0 ; j < l ; j++)
		{
			int s;
			cin>>s;
			maxtemp = max(s,maxtemp);
			mintemp = min(s,mintemp);
			if(s>mintemp) flag[i]=1;
		}
		if(flag[i]!=1)
		{
			minarr.push_back(mintemp);
		}
		maxarr[maxtemp]++;
	}
	for(int j = 10000 ; j>=0 ; j++)
	{
		maxarr[j] += maxarr[j+1];
	}
	int k=0;
	for(int i = 0 ; i < n ; i++)
	{
		ans-=(n-maxarr[minarr[i]+1]);
	}
	cout << ans;
	return 0;
}


#include <bits/stdc++.h>
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(), (v).end()
using namespace std;
using lint = long long;
using pi = pair<int, int>;

vector<pi> pr;

int main(){
	int n; scanf("%d",&n);
	lint ans = 1ll * n * n;
	for(int i=0; i<n; i++){
		int m; scanf("%d",&m);
		vector<int> v(m);
		for(int i=0; i<m; i++) scanf("%d",&v[i]);
		reverse(all(v));
		if(is_sorted(all(v))){
			pr.emplace_back(v[0], v.back());
		}
	}
	sort(all(pr));
	for(int i=0; i<sz(pr); i++){
		ans -= pr.end() - lower_bound(all(pr), pi(pr[i].second, -1));
	}
	cout << ans << endl;
}


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

int min(int a1 , int b1)
{
	if(a1>=b1) return b1;
	else return a1;
}
int max(int a2 , int b2)
{
	if(a2<=b2) return b2;
	else return a2;
}

int main()
{
	int n;
	int ans = 0;
	cin>>n;
	int maxarr[n] = {0};
	int minarr[n] = {0};
	int flag[n] = {0};
	int init = 0;
	for(int i = 0 ; i < n ; i++)
	{
		int l;
		cin >> l;
		int mintemp = 1000000;
		int maxtemp = -1;
		for(int j = 0 ; j < l ; j++)
		{
			int s;
			cin>>s;
			maxtemp = max(s,maxtemp);
			mintemp = min(s,mintemp);
			if(s>mintemp)
			{
				flag[i] = 1;
				init++;
			}
		}
		minarr[i] = mintemp;
		maxarr[i] = maxtemp;
	}
	int k=0;
	int j;
	while(k<n)
	{
		j = k;
		for(j ; j < n ; j++)
		{
			if(flag[k]!=1 || j!=k)
			{
				if(minarr[k]<maxarr[j] || flag[k] || flag[j])ans++;
				if(maxarr[k]>minarr[j] || flag[k] || flag[j])ans++;
			}
		}
		k++;
	}
	cout << ans;
	return 0;
}


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

int min(int a , int b)
{
	if(a>=b) return b;
	else return a;
}
int max(int a , int b)
{
	if(a<=b) return b;
	else return a;
}

int main()
{
	int n;
	int ans;
	cin>>n;
	int maxarr[n] = {0};
	int minarr[n] = {0};
	int flag[n] = {0};
	for(int i = 0 ; i < n ; i++)
	{
		int l;
		cin >> l;
		int mintemp = 1000000;
		int maxtemp = -1;
		for(int j = 0 ; j < l ; j++)
		{
			int s;
			cin>>s;
			maxtemp = max(s,maxtemp);
			mintemp = min(s,mintemp);
			if(s>mintemp) flag[i]=1;
		}
		minarr[i] = mintemp;
		maxarr[i] = maxtemp;
	}
	int k=0;
	while(k<n)
	{
		for(int j = 0 ; j < n ; j++)
		{
			if(minarr[k]<maxarr[j] || flag[k] || flag[j])ans++;
		}
		k++;
	}
	cout << ans;
}


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

int min(int a , int b)
{
	if(a>=b) return b;
	else return a;
}
int max(int a , int b)
{
	if(a<=b) return b;
	else return a;
}

int main()
{
	int n;
	int ans;
	cin>>n;
	int maxarr[n] = {0};
	int minarr[n] = {0};
	int flag[n] = {0};
	for(int i = 0 ; i < n ; i++)
	{
		int l;
		cin >> l;
		int mintemp = 1000000;
		int maxtemp = -1;
		for(int j = 0 ; j < l ; j++)
		{
			int s;
			cin>>s;
			maxtemp = max(s,maxtemp);
			mintemp = min(s,mintemp);
			if(s>mintemp) flag[i]=1;
		}
		minarr[i] = mintemp;
		maxarr[i] = maxtemp;
	}
	int k=0;
	while(k<n)
	{
		for(int j = 0 ; j < n ; j++)
		{
			if(minarr[k]<maxarr[j] || flag[k] || flag[j])ans++;
		}
		k++;
	}
	cout << ans;
	return 0;
}


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

int min(int a1 , int b1)
{
	if(a1>=b1) return b1;
	else return a1;
}
int max(int a2 , int b2)
{
	if(a2<=b2) return b2;
	else return a2;
}

int main()
{
	int n;
	int ans = 0;
	cin>>n;
	vector<int> minarr;
	vector<int> maxarr;
	maxarr.resize(1000005);
	int flag[n] = {0};
	int maxtemp;
	for(int i = 0 ; i < n ; i++)
	{
		int l;
		cin >> l;
		int mintemp = 1000000;
		int maxtemp = -1;
		for(int j = 0 ; j < l ; j++)
		{
			int s;
			cin>>s;
			maxtemp = max(s,maxtemp);
			mintemp = min(s,mintemp);
			if(s>mintemp) flag[i]=1;
		}
		if(flag[i]!=1)
		{
			minarr.push_back(mintemp);
		}
		maxarr[maxtemp]++;
	}
	for(int i = 1000000 ; i>=0 ; i++)
	{
		maxarr[i] += maxarr[i+1];
	}
	int k=0;
	for(int i = 0 ; i < n ; i++)
	{
		ans-=(n-maxarr[minarr[i]+1]);
	}
	cout << ans;
	return 0;
}


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

int min(int a1 , int b1)
{
	if(a1>=b1) return b1;
	else return a1;
}
int max(int a2 , int b2)
{
	if(a2<=b2) return b2;
	else return a2;
}

int main()
{
	int n;
	long long int ans = 0;
	cin>>n;
	int maxarr[n] = {0};
	int minarr[n] = {0};
	int flag[n] = {0};
	int init = 0;
	for(int i = 0 ; i < n ; i++)
	{
		int l;
		cin >> l;
		int mintemp = 1000000;
		int maxtemp = -1;
		for(int j = 0 ; j < l ; j++)
		{
			int s;
			cin>>s;
			maxtemp = max(s,maxtemp);
			mintemp = min(s,mintemp);
			if(s>mintemp)
			{
				flag[i] = 1;
				init++;
			}
		}
		minarr[i] = mintemp;
		maxarr[i] = maxtemp;
	}
	int k=0;
	int j;
	int flag2;
	while(k<n)
	{
		j = k;
		for(j ; j < n ; j++)
		{
				flag2 = 0;
				if(minarr[k]<maxarr[j] || flag[k] || flag[j]){
					ans++;
					flag2++;
				}
				if(maxarr[k]>minarr[j] || flag[k] || flag[j]){
					ans++;
					flag2++;
				}
				if(flag2==2 && k==j) ans--;
		}
		k++;
	}
	cout << ans;
	return 0;
}


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

int min(int a1 , int b1)
{
	if(a1>=b1) return b1;
	else return a1;
}
int max(int a2 , int b2)
{
	if(a2<=b2) return b2;
	else return a2;
}

int main()
{
	int n;
	int ans = 0;
	cin>>n;
	int maxarr[n] = {0};
	int minarr[n] = {0};
	int flag[n] = {0};
	for(int i = 0 ; i < n ; i++)
	{
		int l;
		cin >> l;
		int mintemp = 1000000;
		int maxtemp = -1;
		for(int j = 0 ; j < l ; j++)
		{
			int s;
			cin>>s;
			maxtemp = max(s,maxtemp);
			mintemp = min(s,mintemp);
			if(s>mintemp) flag[i]=1;
		}
		minarr[i] = mintemp;
		maxarr[i] = maxtemp;
	}
	int k=0;
	while(k<n)
	{
		for(int j = 0 ; j < n ; j++)
		{
			if(minarr[k]<maxarr[j] || flag[k] || flag[j])ans++;
		}
		k++;
	}
	cout << ans;
	return 0;
}


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

int min(int a1 , int b1)
{
	if(a1>=b1) return b1;
	else return a1;
}
int max(int a2 , int b2)
{
	if(a2<=b2) return b2;
	else return a2;
}

int main()
{
	int n;
	int ans;
	cin>>n;
	int maxarr[n] = {0};
	int minarr[n] = {0};
	int flag[n] = {0};
	for(int i = 0 ; i < n ; i++)
	{
		int l;
		cin >> l;
		int mintemp = 1000000;
		int maxtemp = -1;
		for(int j = 0 ; j < l ; j++)
		{
			int s;
			cin>>s;
			maxtemp = max(s,maxtemp);
			mintemp = min(s,mintemp);
			if(s>mintemp) flag[i]=1;
		}
		minarr[i] = mintemp;
		maxarr[i] = maxtemp;
	}
	int k=0;
	while(k<n)
	{
		for(int j = 0 ; j < n ; j++)
		{
			if(minarr[k]<maxarr[j] || flag[k] || flag[j])ans++;
		}
		k++;
	}
	cout << ans;
	return 0;
}


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

int min(int a1 , int b1)
{
	if(a1>=b1) return b1;
	else return a1;
}
int max(int a2 , int b2)
{
	if(a2<=b2) return b2;
	else return a2;
}

int main()
{
	int n;
	int ans = 0;
	cin>>n;
	vector<int> minarr;
	vector<int> maxarr;
	maxarr.resize(1000005);
	int flag[n] = {0};
	int maxtemp;
	for(int i = 0 ; i < n ; i++)
	{
		int l;
		cin >> l;
		int mintemp = 1000000;
		int maxtemp = -1;
		for(int j = 0 ; j < l ; j++)
		{
			int s;
			cin>>s;
			maxtemp = max(s,maxtemp);
			mintemp = min(s,mintemp);
			if(s>mintemp) flag[i]=1;
		}
		if(flag[i]!=1)
		{
			minarr.push_back(mintemp);
		}
		maxarr[maxtemp]++;
	}
	for(int j = 100 ; j>=0 ; j++)
	{
		maxarr[j] += maxarr[j+1];
	}
	int k=0;
	for(int i = 0 ; i < n ; i++)
	{
		ans-=(n-maxarr[minarr[i]+1]);
	}
	cout << ans;
	return 0;
}


#include<bits/stdc++.h>
typedef long long ll;
const int N=1e6+1;
using namespace std;
int a[N], mx[N] ,pre[N];
int main()
{
	int n, x, flag, mn, l;
	ll ans=0;
	scanf("%d", &n);
	for(int i=0; i<n; i++)
	{
		cin>>l;
		flag=0;
		mn=N;
		for(int j=0; j<l; j++)
		{
			scanf("%d", &x);
			if(mn<x) flag=1;
			mx[i]=max(x, mx[i]), mn=min(x, mn);
		}
		if(flag) mn=-1, mx[i]=N-1;
		pre[mn+1]++;
	}
	for(int i=1; i<N; i++)
	{
		pre[i]+=pre[i-1];
	}
	for(int i=0; i<n; i++)
	{
		ans+=pre[mx[i]];
	}
	printf("%lld\n", ans);
	return 0;
}


#include <bits/stdc++.h>
#define ios::sync_with_stdio(false);cin.tie(0)
#define ll long long

using namespace std;
const int maxn=1e5+10;
int a,b[maxn],vis[maxn];
int fst[maxn],lst[maxn];
int main()
{
    int n;
    while(cin>>n)
    {
        for(int i=0;i<n;i++)
        {
            bool first=1,second=1;
            cin>>a;
            vis[i]=0;
            for(int j=0;j<a;j++)
            {
                cin>>b[j];
                if(first)
                    first=!first;
                else if(second)
                {
                    if(b[j]>b[j-1])
                    {
                        vis[i]=1;
                        second=!second;
                    }
                }
            }
            fst[i]=b[0];
            lst[i]=b[a-1];
        }
        ll sum=0;
        for(int i=0;i<n;i++)
        {
            if(vis[i])
                sum+=n;
            else
            {
                for(int j=0;j<n;j++)
                {
                    if(vis[j])
                        sum+=1;
                    else
                    {
                        if(lst[i]<fst[j])
                            sum+=1;
                    }
                }
            }
        }
        cout<<sum<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
#define sync_with_stdio(false);cin.tie(0)
#define ll long long

using namespace std;
const int maxn=1e5+10;
int a,b[maxn],vis[maxn];
int fst[maxn],lst[maxn];
int main()
{
    int n;
    while(cin>>n)
    {
        for(int i=0;i<n;i++)
        {
            bool first=1,second=1;
            cin>>a;
            vis[i]=0;
            for(int j=0;j<a;j++)
            {
                cin>>b[j];
                if(first)
                    first=!first;
                else if(second)
                {
                    if(b[j]>b[j-1])
                    {
                        vis[i]=1;
                        second=!second;
                    }
                }
            }
            fst[i]=b[0];
            lst[i]=b[a-1];
        }
        ll sum=0;
        for(int i=0;i<n;i++)
        {
            if(vis[i])
                sum+=n;
            else
            {
                for(int j=0;j<n;j++)
                {
                    if(vis[j])
                        sum+=1;
                    else
                    {
                        if(lst[i]<fst[j])
                            sum+=1;
                    }
                }
            }
        }
        cout<<sum<<endl;
    }
    return 0;
}


#include<stdio.h>
const int N=1e5+10;
struct node{
    int flag;
    int max;
    int min;
}q[N];
int main(){
    int n;
    scanf("%d",&n);
    int n1=n;
    while(n--){
        int m;
        scanf("%d",&m);
        int a;
        scanf("%d",&a);
        q[n].max=a;
        q[n].min=a;
        q[n].flag=0;
        for(int i=1;i<m;i++){
            scanf("%d",&a);
            if(a>q[n].max)
                q[n].max=a;
            if(a<q[n].min)
                q[n].min=a;
            else if(a>q[n].min)
                q[n].flag=1;
        }
    }
    int ans=0;
    for(int i=0;i<n1;i++)
        if(q[i].flag)
            ans+=n1;
        else
            for(int j=0;j<n1;j++)
                if(q[j].flag||q[j].min<q[i].max)
                    ans++;
    printf("%d\n",ans);
    return 0;
}


#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
#define pb push_back
#define ll long long
#define pii pair<int,int>
#define INF 0x3f3f3f3f
const int maxn=1e6+7;
int minn[maxn],maxx[maxn];
bool ok[maxn];
int mmm[maxn];
int main()
{
    int n;
    ll sum=0;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        int l;
        scanf("%d",&l);
        int a;
        int max1,min1;
        scanf("%d",&max1);
        min1=max1;
        for(int j=2;j<=l;j++)
        {
            scanf("%d",&a);
            if(a<min1)min1=a;
            if(a>min1)ok[i]=1;
            if(a>max1)max1=a;
        }
        maxx[i]=max1;
        minn[i]=min1;
        if(ok[i])mmm[0]++;
        else mmm[min1]++;
    }
    for(int i=1;i<maxn;i++)
        mmm[i]+=mmm[i-1];
    for(int i=1;i<=n;i++)
    {
        if(ok[i])sum+=n;
        else{
            sum+=mmm[maxx[i]-1];
        }
    }
    cout<<sum<<endl;
    return 0;
}


#include<bits/stdc++.h>
typedef long long ll;
const int N=1e6+1;
using namespace std;
int a[N], mx[N] ,pre[N];
int main()
{
	int n, x, flag, mn, l;
	ll ans=0;
	scanf("%d", &n);
	for(int i=0; i<n; i++)
	{
		cin>>l;
		flag=0;
		mn=N;
		for(int j=0; j<l; j++)
		{
			scanf("%d", &x);
			if(mn<x) flag=1;
			mx[i]=max(x, mx[i]), mn=min(x, mn);
		}
		if(flag) mn=-1;
		pre[mn+1]++;
	}
	for(int i=1; i<N; i++)
	{
		pre[i]+=pre[i-1];
	}
	for(int i=0; i<n; i++)
	{
		ans+=pre[mx[i]];
	}
	printf("%lld\n", ans);
	return 0;
}


#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
#define pb push_back
#define ll long long
#define pii pair<int,int>
#define INF 0x3f3f3f3f
const int maxn=1e6+7;
int minn[maxn],maxx[maxn];
bool ok[maxn];
int mmm[maxn];
int main()
{
    int n;
    ll sum=0;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        int l;
        scanf("%d",&l);
        int a;
        int max1,min1;
        scanf("%d",&max1);
        min1=max1;
        for(int j=2;j<=l;j++)
        {
            scanf("%d",&a);
            if(a<min1)min1=a;
            if(a>min1)ok[i]=1;
            if(a>max1)max1=a;
        }
        maxx[i]=max1;
        minn[i]=min1;
        if(ok[i])mmm[0]++;
        else mmm[min1+1]++;
    }
    for(int i=1;i<maxn;i++)
        mmm[i]+=mmm[i-1];
    for(int i=1;i<=n;i++)
    {
        if(ok[i])sum+=n;
        else{
            sum+=mmm[maxx[i]];
        }
    }
    cout<<sum<<endl;
    return 0;
}


#include <bits/stdc++.h>
#define sync_with_stdio(false);cin.tie(0)
#define ll long long

using namespace std;
const int maxn=1e5+10;
int a[maxn],b[maxn],vis[maxn];
int fst[maxn],lst[maxn];
int main()
{
    int n;
    while(cin>>n)
    {

        for(int i=0;i<n;i++)
        {
            cin>>a[i];
            vis[i]=0;
            for(int j=0;j<a[i];j++)
                cin>>b[j];
            for(int j=0;j<a[i]-1;j++)
            {
                if(b[j]<b[j+1])
                    vis[i]=1;
            }
            fst[i]=b[0];
            lst[i]=b[a[i]-1];
        }
        ll sum=0;
        for(int i=0;i<n;i++)
        {
            if(vis[i])
                sum+=n;
            else
            {
                for(int j=0;j<n;j++)
                {
                    if(vis[j])
                        sum+=1;
                    else
                    {
                        if(lst[i]<fst[j])
                            sum+=1;
                    }
                }
            }
        }
        cout<<sum<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main(){
	int N;
	scanf("%d",&N);
	int M = 0; // already
	vector<int> M1, M2; //min, max;
	for(int i=0;i<N;i++){
		int n; scanf("%d",&n);
		int m1, m2;
		int flag = 0;
		for(int i=0;i<n;i++){
			int x; scanf("%d",&x);
			if(i == 0) m1 = m2 = x;
			else {
				if(m1 < x) flag = 1;
				m1 = min(m1, x);
				m2 = max(m2, x);
			}
		}
		if(flag) M++;
		else {
			M1.push_back(m1);
			M2.push_back(m2);
		}
	}
	N = M1.size();
	vector<int> S(1000001, 0);
	for(int m1: M1) S[m1]++;
	for(int i=0;i<=1000000;i++) S[i] += S[i-1];
	long long ans = 0;
	for(int i=0; i<N; i++){
		if(M2[i] > 0) ans += S[M2[i] - 1];
		//if(M1[i] < M2[i]) ans--;
	}
	//ans += (long long)M * M + (long long)M * N * 2;

	printf("%lld\n",ans);
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main(){
	int N;
	scanf("%d",&N);
	int M = 0; // already
	vector<int> M1, M2; //min, max;
	for(int i=0;i<N;i++){
		int n; scanf("%d",&n);
		int m1, m2;
		int flag = 0;
		for(int i=0;i<n;i++){
			int x; scanf("%d",&x);
			if(i == 0) m1 = m2 = x;
			else {
				if(m1 < x) flag = 1;
				m1 = min(m1, x);
				m2 = max(m2, x);
			}
		}
		if(flag) M++;
		else {
			M1.push_back(m1);
			M2.push_back(m2);
		}
	}
	N = M1.size();
	vector<int> S(1000001, 0);
	for(int m1: M1) S[m1]++;
	for(int i=0;i<=1000000;i++) S[i] += S[i-1];
	long long ans = 0;
	for(int i=0; i<N; i++){
	    printf("%d",S[i]);
		if(M2[i] > 0) ans += S[M2[i] - 1];
		//if(M1[i] < M2[i]) ans--;
	}
	ans += (long long)M * M + (long long)M * N * 2;

	printf("%lld\n",ans);
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main(){
	int N;
	scanf("%d",&N);
	int M = 0; // already
	vector<int> M1, M2; //min, max;
	for(int i=0;i<N;i++){
		int n; scanf("%d",&n);
		int m1, m2;
		int flag = 0;
		for(int i=0;i<n;i++){
			int x; scanf("%d",&x);
			if(i == 0) m1 = m2 = x;
			else {
				if(m1 < x) flag = 1;
				m1 = min(m1, x);
				m2 = max(m2, x);
			}
		}
		if(flag) M++;
		else {
			M1.push_back(m1);
			M2.push_back(m2);
		}
	}
	N = M1.size();
	vector<int> S(1000001, 0);
	for(int m1: M1) S[m1]++;
	for(int i=0;i<=1000000;i++) S[i] += S[i-1];
	long long ans = 0;
	for(int i=0; i<N; i++){
		//if(M2[i] > 0) ans += S[M2[i] - 1];
		//if(M1[i] < M2[i]) ans--;
	}
	//ans += (long long)M * M + (long long)M * N * 2;

	printf("%lld\n",ans);
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main(){
	int N;
	scanf("%d",&N);
	int M = 0; // already
	vector<int> M1, M2; //min, max;
	for(int i=0;i<N;i++){
		int n; scanf("%d",&n);
		int m1, m2;
		int flag = 0;
		for(int i=0;i<n;i++){
			int x; scanf("%d",&x);
			if(i == 0) m1 = m2 = x;
			else {
				if(m1 < x) flag = 1;
				m1 = min(m1, x);
				m2 = max(m2, x);
			}
		}
		if(flag) M++;
		else {
			M1.push_back(m1);
			M2.push_back(m2);
		}
	}
	N = M1.size();
	vector<int> S(1000001, 0);
	for(int m1: M1) S[m1]++;
	for(int i=1;i<=1000000;i++) S[i] += S[i-1];
	long long ans = 0;
	for(int i=0; i<N; i++){
		if(M2[i] > 0) ans += S[M2[i] - 1];
		//if(M1[i] < M2[i]) ans--;
	}
	ans += (long long)M * M + (long long)M * N * 2;

	printf("%lld\n",ans);
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main(){
	int N;
	scanf("%d",&N);
	int M = 0; // already
	vector<int> M1, M2; //min, max;
	for(int i=0;i<N;i++){
		int n; scanf("%d",&n);
		int m1, m2;
		int flag = 0;
		for(int i=0;i<n;i++){
			int x; scanf("%d",&x);
			if(i == 0) m1 = m2 = x;
			else {
				if(m1 < x) flag = 1;
				m1 = min(m1, x);
				m2 = max(m2, x);
			}
		}
		if(flag) M++;
		else {
			M1.push_back(m1);
			M2.push_back(m2);
		}
	}
	N = M1.size();
	vector<int> S(1000001, 0);
	for(int m1: M1) S[m1]++;
	for(int i=1;i<=1000000;i++) S[i] += S[i-1];
	long long ans = 0;
	for(int i=1; i<N; i++){
		if(M2[i] > 0) ans += S[M2[i] - 1];
		//if(M1[i] < M2[i]) ans--;
	}
	ans += (long long)M * M + (long long)M * N * 2;

	printf("%lld\n",ans);
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main(){
	int N;
	scanf("%d",&N);
	int M = 0; // already
	vector<int> M1, M2; //min, max;
	for(int i=0;i<N;i++){
		int n; scanf("%d",&n);
		int m1, m2;
		int flag = 0;
		for(int i=0;i<n;i++){
			int x; scanf("%d",&x);
			if(i == 0) m1 = m2 = x;
			else {
				if(m1 < x) flag = 1;
				m1 = min(m1, x);
				m2 = max(m2, x);
			}
		}
		if(flag) M++;
		else {
			M1.push_back(m1);
			M2.push_back(m2);
		}
	}
	N = M1.size();
	vector<int> S(1000001, 0);
	for(int m1: M1) S[m1]++;
	for(int i=0;i<=1000000;i++) S[i] += S[i-1];
	long long ans = 0;
	for(int i=0; i<N; i++){
		//if(M2[i] > 0) ans += S[M2[i] - 1];
		//if(M1[i] < M2[i]) ans--;
	}
	ans += (long long)M * M + (long long)M * N * 2;

	printf("%lld\n",ans);
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main(){
	int N;
	scanf("%d",&N);
	int M = 0; // already
	vector<int> M1, M2; //min, max;
	for(int i=0;i<N;i++){
		int n; scanf("%d",&n);
		int m1, m2;
		int flag = 0;
		for(int i=0;i<n;i++){
			int x; scanf("%d",&x);
			if(i == 0) m1 = m2 = x;
			else {
				if(m1 < x) flag = 1;
				m1 = min(m1, x);
				m2 = max(m2, x);
			}
		}
		if(flag) M++;
		else {
			M1.push_back(m1);
			M2.push_back(m2);
		}
	}
	N = M1.size();
	vector<int> S(1000001, 0);
	for(int m1: M1) S[m1]++;
	for(int i=0;i<=1000000;i++) S[i] += S[i-1];
	long long ans = 0;
	for(int i=0; i<N; i++){
		if(M2[i] > 0) ans += S[M2[i] - 1];
		//if(M1[i] < M2[i]) ans--;
	}
	ans += (long long)M * M + (long long)M * N * 2;

	printf("%lld\n",ans);
}


#include <bits/stdc++.h>

using namespace std;

int main(){
	int N;
	scanf("%d",&N);
	int M = 0; // already
	vector<int> M1, M2; //min, max;
	for(int i=0;i<N;i++){
		int n; scanf("%d",&n);
		int m1, m2;
		int flag = 0;
		for(int i=0;i<n;i++){
			int x; scanf("%d",&x);
			if(i == 0) m1 = m2 = x;
			else {
				if(m1 < x) flag = 1;
				m1 = min(m1, x);
				m2 = max(m2, x);
			}
		}
		if(flag) M++;
		else {
			M1.push_back(m1);
			M2.push_back(m2);
		}
	}
	N = M1.size();
	vector<int> S(1000001, 0);
	for(int m1: M1) S[m1]++;
	for(int i=0;i<=1000000;i++) S[i] += S[i-1];
	long long ans = 0;
	for(int i=0; i<N; i++){
	    printf("%d",M2[i]);
		if(M2[i] > 0) ans += S[M2[i] - 1];
		//if(M1[i] < M2[i]) ans--;
	}
	ans += (long long)M * M + (long long)M * N * 2;

	printf("%lld\n",ans);
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main(){
	int N;
	scanf("%d",&N);
	int M = 0; // already
	vector<int> M1, M2; //min, max;
	for(int i=0;i<N;i++){
		int n; scanf("%d",&n);
		int m1, m2;
		int flag = 0;
		for(int i=0;i<n;i++){
			int x; scanf("%d",&x);
			if(i == 0) m1 = m2 = x;
			else {
				if(m1 < x) flag = 1;
				m1 = min(m1, x);
				m2 = max(m2, x);
			}
		}
		if(flag) M++;
		else {
			M1.push_back(m1);
			M2.push_back(m2);
		}
	}
	N = M1.size();
	vector<int> S(1000001, 0);
	for(int m1: M1) S[m1]++;
	for(int i=0;i<=1000000;i++) S[i] += S[i-1];
	long long ans = 0;
	for(int i=0; i<N; i++){
		if(M2[i] > 0) ans += S[M2[i] - 1];
		//if(M1[i] < M2[i]) ans--;
	}
	ans += (long long)M * M + (long long)M * N * 2;

	printf("%lld\n",ans);
	return 0;
}


#pragma GCC optimize("Ofast")

#include<bits/stdc++.h>

#define all(s) s.begin(),s.end()
#define rc(x) return cout<<x<<endl,0
#define forn(i,n) for(int i=0;i<int(n);i++)
#define pb push_back
#define mp make_pair
#define fr first
#define sc second

typedef long long ll;
typedef long double ld;

const int nmax=1025;
const int mod=998244353;
const ll inf=2e9;

using namespace std;

int main()
{
  ios_base::sync_with_stdio(0); cin.tie(0);
  //freopen("1.in","r",stdin);
  //freopen("1.out","w",stdout);
  int n;
  cin>>n;
  vector< pair<int,int> >v;
  ll sol=0,bu=0;
  for(int i=1;i<=n;i++)
  {
    int k;
    cin>>k;
    int mn=inf,mx=-1;
    bool ok=0;
    forn(j,k)
    {
      int x;
      cin>>x;
      if(x>mn) ok=1;
      mn=min(mn,x);
      mx=max(mx,x);
    }
    if(ok) sol+=(n-bu)*2-1,bu++; else v.pb(mp(mn,mx));
  }
  sort(all(v));
  //forn(i,v.size()) cout<<v[i].fr<<" "<<v[i].sc<<"\n";
  forn(i,v.size())
  {
    int st=0,dr=v.size()-1;
    while(st<=dr)
    {
      int mid=(st+dr)/2;
      if(v[mid].fr<v[i].sc) st=mid+1; else dr=mid-1;
    }
    //cout<<v[i].sc<<" "<<st<<"\n";
    sol+=st;
    //break;
  }
  cout<<"\n";
  cout<<sol;
}


#pragma GCC optimize("Ofast")

#include<bits/stdc++.h>

#define all(s) s.begin(),s.end()
#define rc(x) return cout<<x<<endl,0
#define forn(i,n) for(int i=0;i<int(n);i++)
#define pb push_back
#define mp make_pair
#define fr first
#define sc second

typedef long long ll;
typedef long double ld;

const int nmax=1025;
const int mod=998244353;
const ll inf=0x3f3f3f3f3f3f3f3f;

using namespace std;

int main()
{
  ios_base::sync_with_stdio(0); cin.tie(0);
  //freopen("1.in","r",stdin);
  //freopen("1.out","w",stdout);
  int n;
  cin>>n;
  vector< pair<int,int> >v;
  int sol=0,bu=0;
  for(int i=1;i<=n;i++)
  {
    int k;
    cin>>k;
    int mn=2000000000,mx=-1;
    bool ok=1;
    forn(j,k)
    {
      int x;
      cin>>x;
      if(x>mn && ok) sol+=n*2-1,bu++,ok=0;
      mn=min(mn,x);
      mx=max(mx,x);
    }
    if(ok) v.pb(mp(mn,mx));
  }
  for(int i=0;i<v.size();i++)
  for(int j=0;j<v.size();j++)
  {
    if(i!=j && v[i].fr<v[j].sc) sol++;
  }
  cout<<sol<<"\n";
}


#pragma GCC optimize("Ofast")

#include<bits/stdc++.h>

#define all(s) s.begin(),s.end()
#define rc(x) return cout<<x<<endl,0
#define forn(i,n) for(int i=0;i<int(n);i++)
#define pb push_back
#define mp make_pair
#define fr first
#define sc second

typedef long long ll;
typedef long double ld;

const int nmax=1025;
const int mod=998244353;
const ll inf=0x3f3f3f3f3f3f3f3f;

using namespace std;

int main()
{
  ios_base::sync_with_stdio(0); cin.tie(0);
  //freopen("1.in","r",stdin);
  //freopen("1.out","w",stdout);
  int n;
  cin>>n;
  vector< pair<int,int> >v;
  ll sol=0,bu=0;
  for(int i=1;i<=n;i++)
  {
    int k;
    cin>>k;
    int mn=2000000000,mx=-1;
    bool ok=1;
    forn(j,k)
    {
      int x;
      cin>>x;
      if(x>mn) ok=0;
      mn=min(mn,x);
      mx=max(mx,x);
    }
    if(ok) v.pb(mp(mn,mx));else v.pb(mp(-1,0));
  }
  for(int i=0;i<v.size();i++)
  {
    ll er=0;
    for(int j=i;j<v.size();j++)
    {
      if(v[j].fr==-1 || v[i].fr<v[j].sc) sol++,er++;
      if(i!=j && (v[i].fr==-1 || v[j].fr<v[i].sc)) sol++,er++;
    }
    //cout<<er<<" ";
  }
  //cout<<"\n";
  cout<<sol<<"\n";
}


#pragma GCC optimize("Ofast")

#include<bits/stdc++.h>

#define all(s) s.begin(),s.end()
#define rc(x) return cout<<x<<endl,0
#define forn(i,n) for(int i=0;i<int(n);i++)
#define pb push_back
#define mp make_pair
#define fr first
#define sc second

typedef long long ll;
typedef long double ld;

const int nmax=1025;
const int mod=998244353;
const ll inf=0x3f3f3f3f3f3f3f3f;

using namespace std;

int main()
{
  ios_base::sync_with_stdio(0); cin.tie(0);
  //freopen("1.in","r",stdin);
  //freopen("1.out","w",stdout);
  int n;
  cin>>n;
  vector< pair<int,int> >v;
  ll sol=0,bu=0;
  for(int i=1;i<=n;i++)
  {
    int k;
    cin>>k;
    int mn=2000000000,mx=-1;
    bool ok=1;
    forn(j,k)
    {
      int x;
      cin>>x;
      if(x>mn) ok=0;
      mn=min(mn,x);
      mx=max(mx,x);
    }
    if(ok) v.pb(mp(mn,mx));else v.pb(mp(-1,0));
  }
  for(int i=0;i<v.size();i++)
  {
    ll er=0;
    for(int j=0;j<v.size();j++)
    {
      if(v[j].fr==-1 || v[i].fr<v[j].sc) sol++,er++;
    }
    //cout<<er<<" ";
  }
  //cout<<"\n";
  cout<<sol<<"\n";
}


#pragma GCC optimize("Ofast")

#include<bits/stdc++.h>

#define all(s) s.begin(),s.end()
#define rc(x) return cout<<x<<endl,0
#define forn(i,n) for(int i=0;i<int(n);i++)
#define pb push_back
#define mp make_pair
#define fr first
#define sc second

typedef long long ll;
typedef long double ld;

const int nmax=1025;
const int mod=998244353;
const ll inf=0x3f3f3f3f3f3f3f3f;

using namespace std;

int main()
{
  ios_base::sync_with_stdio(0); cin.tie(0);
  //freopen("1.in","r",stdin);
  //freopen("1.out","w",stdout);
  int n;
  cin>>n;
  vector< pair<int,int> >v;
  ll sol=0,bu=0;
  for(int i=1;i<=n;i++)
  {
    int k;
    cin>>k;
    ll mn=2000000000,mx=-1;
    bool ok=1;
    forn(j,k)
    {
      ll x;
      cin>>x;
      if(x>mn && ok) sol+=(n-bu)*2-1,bu++,ok=0;
      mn=min(mn,x);
      mx=max(mx,x);
    }
    if(ok) v.pb(mp(mn,mx));
  }
  for(int i=0;i<v.size();i++)
  for(int j=i;j<v.size();j++)
  {
    if(v[i].fr<v[j].sc) sol++;
    if(i!=j && v[j].fr<v[i].sc) sol++;
  }
  cout<<sol<<"\n";
}


#pragma GCC optimize("Ofast")

#include<bits/stdc++.h>

#define all(s) s.begin(),s.end()
#define rc(x) return cout<<x<<endl,0
#define forn(i,n) for(int i=0;i<int(n);i++)
#define pb push_back
#define mp make_pair
#define fr first
#define sc second

typedef long long ll;
typedef long double ld;

const int nmax=1025;
const int mod=998244353;
const ll inf=0x3f3f3f3f3f3f3f3f;

using namespace std;

int main()
{
  ios_base::sync_with_stdio(0); cin.tie(0);
  //freopen("1.in","r",stdin);
  //freopen("1.out","w",stdout);
  int n;
  cin>>n;
  vector< pair<int,int> >v;
  ll sol=0,bu=0;
  for(int i=1;i<=n;i++)
  {
    int k;
    cin>>k;
    ll mn=2000000000,mx=-1;
    bool ok=1;
    forn(j,k)
    {
      int x;
      cin>>x;
      if(x>mn && ok) sol+=(n-bu)*2-1,bu++,ok=0;
      mn=min(mn,x);
      mx=max(mx,x);
    }
    if(ok) v.pb(mp(mn,mx));
  }
  for(int i=0;i<v.size();i++)
  for(int j=i;j<v.size();j++)
  {
    if(v[i].fr<v[j].sc) sol++;
    if(i!=j && v[j].fr<v[i].sc) sol++;
  }
  cout<<sol<<"\n";
}


#pragma GCC optimize("Ofast")

#include<bits/stdc++.h>

#define all(s) s.begin(),s.end()
#define rc(x) return cout<<x<<endl,0
#define forn(i,n) for(int i=0;i<int(n);i++)
#define pb push_back
#define mp make_pair
#define fr first
#define sc second

typedef long long ll;
typedef long double ld;

const int nmax=1025;
const int mod=998244353;
const ll inf=0x3f3f3f3f3f3f3f3f;

using namespace std;

int main()
{
  ios_base::sync_with_stdio(0); cin.tie(0);
  //freopen("1.in","r",stdin);
  //freopen("1.out","w",stdout);
  int n;
  cin>>n;
  vector< pair<int,int> >v;
  int sol=0,bu=0;
  for(int i=1;i<=n;i++)
  {
    int k;
    cin>>k;
    int mn=2000000000,mx=-1;
    bool ok=1;
    forn(j,k)
    {
      int x;
      cin>>x;
      if(x>mn && ok) sol+=(n-bu)*2-1,bu++,ok=0;
      mn=min(mn,x);
      mx=max(mx,x);
    }
    if(ok) v.pb(mp(mn,mx));
  }
  for(int i=0;i<v.size();i++)
  for(int j=i;j<v.size();j++)
  {
    if(v[i].fr<v[j].sc) sol++;
    if(i!=j && v[j].fr<v[i].sc) sol++;
  }
  cout<<sol<<"\n";
}


#include <bits/stdc++.h>
#define ll long long int
#define f(i, a, b) for(int i=a; i<b; i++)
using namespace std;

int main()
{
    ll n, rev = 0, ans = 0;
    vector <ll> vmin, vmax;
    cin>>n;
    f(i, 0, n)
    {
        ll l, flag = 0;
        cin>>l;
        ll a[l], mn = INT_MAX, mx = INT_MIN;
        f(j, 0, l) cin>>a[j], mn = min(mn, a[j]), mx = max(mx, a[j]);
        if(l > 1) f(j, 1, l) if(a[j] > a[j-1]) flag++;
        if(flag || l == 1) rev++, vmax.push_back(mx), vmin.push_back(mn);
    }
    sort(vmax.begin(), vmax.end());
    sort(vmin.begin(), vmin.end());
    f(i, 0, rev) ans += vmax.end() - upper_bound(vmax.begin(), vmax.end(), vmin[i]);
    n = n-rev;
    ans += n*n + n*rev*2;
    cout<<ans;
}


#include <bits/stdc++.h>
#define ll long long int
#define pb push_back
#define f(i, a, b) for(ll i=a; i<b; i++)
using namespace std;

int main()
{
    ll n, rev = 0, ans = 0;
    vector <ll> vmin, vmax;
    cin>>n;
    f(i, 0, n)
    {
        ll l, flag = 0;
        cin>>l;
        ll a[l];
        f(i, 0, l) cin>>a[i];
        f(i, 1, l) if(a[i] > a[i-1]) flag++;
        if(flag || l == 1)
        {
            ll mn = INT_MAX, mx = INT_MIN;
            rev++;
            f(i, 0, l)
            {
                mn = min(mn, a[i]);
                mx = max(mx, a[i]);
            }
            vmax.pb(mx);
            vmin.pb(mn);
        }
    }
    sort(vmin.begin(), vmin.end());
    sort(vmax.begin(), vmax.end());
    f(i, 0, rev) ans += vmax.end() - upper_bound(vmax.begin(), vmax.end(), vmin[i]);
    n = n-rev;
    ans += n*n + n*rev*2;
    cout<<ans;
}


#include <bits/stdc++.h>
#define ll long long int
#define f(i, a, b) for(ll i=a; i<b; i++)
using namespace std;

int main()
{
    ll n, rev = 0, ans = 0;
    vector <ll> vmin, vmax;
    cin>>n;
    f(i, 0, n)
    {
        ll l, flag = 0;
        cin>>l;
        ll a[l];
        f(i, 0, l) cin>>a[i];
        f(i, 1, l) if(a[i] > a[i-1]) flag++;
        if(flag || l = 1)
        {
            ll mn = INT_MAX, mx = INT_MIN;
            rev++;
            f(i, 0, l)
            {
                mn = min(mn, a[i]);
                mx = max(mx, a[i]);
            }
            vmax.push_back(mx);
            vmin.push_back(mn);
        }
    }
    sort(vmin.begin(), vmin.end());
    sort(vmax.begin(), vmax.end());
    f(i, 0, rev) ans += vmax.end() - upper_bound(vmax.begin(), vmax.end(), vmin[i]);
    n = n-rev;
    ans += n*n + n*rev*2;
    cout<<ans;
}


#include <bits/stdc++.h>
#define ll long long int
#define f(i, a, b) for(int i=a; i<b; i++)
using namespace std;

int main()
{
    ll n, rev = 0, ans = 0;
    vector <ll> vmin, vmax;
    cin>>n;
    f(i, 0, n)
    {
        ll l, flag = 0;
        cin>>l;
        ll a[l], mn = INT_MAX, mx = INT_MIN;
        f(j, 0, l) cin>>a[j], mn = min(mn, a[j]), mx = max(mx, a[j]);
        f(j, 1, l)
        {
            if(a[j] > a[j-1])
            {
                flag++;
            }
        }
        if(flag || l == 1) rev++, vmax.push_back(mx), vmin.push_back(mn);
    }
    sort(vmax.begin(), vmax.end());
    sort(vmin.begin(), vmin.end());
    f(i, 0, rev) ans += vmax.end() - upper_bound(vmax.begin(), vmax.end(), vmin[i]);
    n = n-rev;
    ans += n*n + n*rev*2;
    cout<<ans;
}


#include <bits/stdc++.h>
#define ll long long int
#define f(i, a, b) for(int i=a; i<b; i++)
using namespace std;

bool isRev(ll* a, ll n)
{
    if(n == 1) return true;
    f(i, 0, n) if(a[i] > a[i-1]) return false;
    return true;
}

int main()
{
    ll n, rev = 0, ans = 0;
    vector <ll> vmin, vmax;
    cin>>n;
    f(i, 0, n)
    {
        ll l, flag = 0;
        cin>>l;
        ll a[l], mn = INT_MAX, mx = INT_MIN;
        f(j, 0, l) cin>>a[j], mn = min(mn, a[j]), mx = max(mx, a[j]);
        f(j, 1, l) {if(a[j] > a[j-1]) {flag++;}}
        if(flag || l == 1) rev++, vmax.push_back(mx), vmin.push_back(mn);
    }
    sort(vmax.begin(), vmax.end());
    sort(vmin.begin(), vmin.end());
    f(i, 0, rev) ans += vmax.end() - upper_bound(vmax.begin(), vmax.end(), vmin[i]);
    n = n-rev;
    ans += n*n + n*rev*2;
    cout<<ans;
}


#include <bits/stdc++.h>
#define ll long long int
#define f(i, a, b) for(int i=a; i<b; i++)
using namespace std;

int main()
{
    ll n, rev = 0;
    ll ans = 0;
    vector <ll> vmin, vmax;
    cin>>n;
    f(i, 0, n)
    {
        ll l, flag = 0;
        cin>>l;
        ll a[l], mn = INT_MAX, mx = INT_MIN;
        f(j, 0, l) cin>>a[j], mn = min(mn, a[j]), mx = max(mx, a[j]);
        f(j, 1, l) if(a[j] > a[j-1]) flag++;
        if(flag || l == 1) rev++, vmax.push_back(mx), vmin.push_back(mn);
    }
    sort(vmax.begin(), vmax.end());
    sort(vmin.begin(), vmin.end());
    f(i, 0, rev) ans += vmax.end() - upper_bound(vmax.begin(), vmax.end(), vmin[i]);
    n = n-rev;
    ans += n*n + n*rev*2;
    cout<<ans;
}


#include <bits/stdc++.h>
#define ll long long int
#define f(i, a, b) for(ll i=a; i<b; i++)
using namespace std;

bool isRev(ll* a, ll n)
{
    if(n == 1) return true;
    f(i, 0, n) if(a[i] > a[i-1]) return false;
    return true;
}

int main()
{
    ll n, rev = 0, ans = 0;
    vector <ll> vmin, vmax;
    cin>>n;
    f(i, 0, n)
    {
        ll l, flag = 0;
        cin>>l;
        ll a[l];
        f(i, 0, l) cin>>a[i];
        if(isRev(a, n))
        {
            ll mn = INT_MAX, mx = INT_MIN;
            rev++;
            f(i, 0, l)
            {
                mn = min(mn, a[i]);
                mx = max(mx, a[i]);
            }
            vmax.push_back(mx);
            vmin.push_back(mn);
        }
    }
    sort(vmin.begin(), vmin.end());
    sort(vmax.begin(), vmax.end());
    f(i, 0, rev) ans += vmax.end() - upper_bound(vmax.begin(), vmax.end(), vmin[i]);
    n = n-rev;
    ans += n*n + n*rev*2;
    cout<<ans;
}


#include <bits/stdc++.h>
#define ll long long int
#define f(i, a, b) for(int i=a; i<b; i++)
using namespace std;

int main()
{
    ll n, rev = 0, ans = 0;
    vector <ll> vmin, vmax;
    cin>>n;
    f(i, 0, n)
    {
        ll l, flag = 0;
        cin>>l;
        ll a[l], mn = INT_MAX, mx = INT_MIN;
        f(j, 0, l) {cin>>a[j]; mn = min(mn, a[j]); mx = max(mx, a[j]);}
        f(j, 1, l) if(a[j] > a[j-1]) flag++;
        if(flag || l == 1) {rev++; vmax.push_back(mx); vmin.push_back(mn);}
    }
    sort(vmax.begin(), vmax.end());
    sort(vmin.begin(), vmin.end());
    f(i, 0, rev) ans += vmax.end() - upper_bound(vmax.begin(), vmax.end(), vmin[i]);
    n = n-rev;
    ans += n*n + n*rev*2;
    cout<<ans;
}


#include <bits/stdc++.h>
#define ll long long int
#define f(i, a, b) for(int i=a; i<b; i++)
using namespace std;

int main()
{
    ll n, rev = 0, ans = 0;
    vector <ll> vmin, vmax;
    cin>>n;
    f(i, 0, n)
    {
        ll l, flag = 0;
        cin>>l;
        ll a[l], mn = INT_MAX, mx = INT_MIN;
        f(j, 0, l) cin>>a[j], mn = min(mn, a[j]), mx = max(mx, a[j]);
        f(j, 1, l) if(a[j] > a[j-1]) flag++;
        if(!flag || l == 1) rev++, vmax.push_back(mx), vmin.push_back(mn);
    }
    sort(vmax.begin(), vmax.end());
    sort(vmin.begin(), vmin.end());
    f(i, 0, rev) ans += vmax.end() - upper_bound(vmax.begin(), vmax.end(), vmin[i]);
    n = n-rev;
    ans += n*n + n*rev*2;
    cout<<ans;
}


#include <bits/stdc++.h>
#define f(i, a, b) for(int i=a; i<b; i++)
using namespace std;

int main()
{
    int n, rev = 0;
    long long ans = 0;
    vector <int> vmin, vmax;
    cin>>n;
    f(i, 0, n)
    {
        int l, flag = 0;
        cin>>l;
        int a[l], mn = INT_MAX, mx = INT_MIN;
        f(i, 0, n) cin>>a[i], mn = min(mn, a[i]), mx = max(mx, a[i]);
        f(i, 1, n) if(a[i] > a[i-1]) flag++;
        if(flag) rev++, vmax.push_back(mx), vmin.push_back(mn);
    }
    sort(vmax.begin(), vmax.end());
    sort(vmin.begin(), vmin.end());
    f(i, 0, rev) ans += vmax.end() - upper_bound(vmax.begin(), vmax.end(), vmin[i]);
    n = n-rev;
    ans += n*n + n*rev*2;
    cout<<ans;
}


#include <bits/stdc++.h>
#define ll long long int
#define f(i, a, b) for(ll i=a; i<b; i++)
using namespace std;

bool isRev(ll* a, ll n)
{
    if(n == 1) return true;
    f(i, 0, n) if(a[i] > a[i-1]) return false;
    return true;
}

int main()
{
    ll n, rev = 0, ans = 0;
    vector <ll> vmin, vmax;
    cin>>n;
    f(i, 0, n)
    {
        ll l, flag = 0;
        cin>>l;
        ll a[l];
        f(i, 0, l) cin>>a[i];
        if(isRev(a, l))
        {
            ll mn = INT_MAX, mx = INT_MIN;
            rev++;
            f(i, 0, l)
            {
                mn = min(mn, a[i]);
                mx = max(mx, a[i]);
            }
            vmax.push_back(mx);
            vmin.push_back(mn);
        }
    }
    sort(vmin.begin(), vmin.end());
    sort(vmax.begin(), vmax.end());
    f(i, 0, rev) ans += vmax.end() - upper_bound(vmax.begin(), vmax.end(), vmin[i]);
    n = n-rev;
    ans += n*n + n*rev*2;
    cout<<ans;
}


#include <bits/stdc++.h>
#define ll long long int
#define f(i, a, b) for(ll i=a; i<b; i++)
using namespace std;

int main()
{
    ll n, rev = 0, ans = 0;
    vector <ll> vmin, vmax;
    cin>>n;
    f(i, 0, n)
    {
        ll l, flag = 0;
        cin>>l;
        ll a[l], mn = INT_MAX, mx = INT_MIN;
        f(i, 0, l) cin>>a[i], mn = min(mn, a[i]), mx = max(mx, a[i]);
        f(i, 1, l) if(a[i] > a[i-1]) flag++;
        if(flag || l == 1) rev++, vmax.push_back(mx), vmin.push_back(mn);
    }
    sort(vmin.begin(), vmin.end());
    sort(vmax.begin(), vmax.end());
    f(i, 0, rev) ans += vmax.end() - upper_bound(vmax.begin(), vmax.end(), vmin[i]);
    n = n-rev;
    ans += n*n + n*rev*2;
    cout<<ans;
}


import sys

readline = sys.stdin.readline

t = int(readline())
for _ in range(t):
    a, b, n, m = map(int, readline().split())
    if a+b < m+n:
        print('No')
    elif a == b:
        print('Yes')
    elif a > b:
        if (a+-b//2 <= n) or (m <= b):
            print('Yes')
        else:
            print('No')
    else:
        if (b-a <= n <= b-a//2) or (a//2 <= m <= a*2):
            print('Yes')
        else:
            print('No')


from sys import stdin, stdout

def main():
    t = int(stdin.readline())
    for _ in range(t):
        v, c, n, m = map(int, stdin.readline().split())
        if v + c < n + m:
            ans = 'No'
        else:
            if v > c:
                if c >= m:
                    ans = 'Yes'
                elif c - (v - n) >= (m - c):
                    ans = 'Yes'
                else:
                    ans = 'No'
            else:
                if v >= m:
                    ans = 'Yes'
                elif v - (c - n) >= (m - v):
                    ans = 'Yes'
                else:
                    ans = 'No'
        stdout.write(ans)
        stdout.write('\n')
    return

if __name__ == '__main__':
    main()


from sys import stdin, stdout

def main():
    t = int(stdin.readline())
    for _ in range(t):
        v, c, n, m = map(int, stdin.readline().split())
        if v + c < n + m:
            ans = 'No'
        else:
            if v > c:
                if c >= m:
                    ans = 'Yes'
                else:
                    ans = 'No'
            else:
                if v >= m:
                    ans = 'Yes'
                else:
                    ans = 'No'
        stdout.write(ans)
        stdout.write('\n')
    return

if __name__ == '__main__':
    main()


from sys import stdin, stdout

def main():
    t = int(stdin.readline())
    for _ in range(t):
        v, c, n, m = map(int, stdin.readline().split())
        if v + c < n + m:
            ans = 'No'
        else:
            if v > c:
                if c >= m:
                    ans = 'Yes'
                elif c - (v - n) == (m - c):
                    ans = 'Yes'
                else:
                    ans = 'No'
            else:
                if v >= m:
                    ans = 'Yes'
                elif v - (c - n) == (m - v):
                    ans = 'Yes'
                else:
                    ans = 'No'
        stdout.write(ans)
        stdout.write('\n')
    return

if __name__ == '__main__':
    main()


t=int(input())
for _ in range(t):
    a,b,n,m=map(int,input().split())
    if(a+b<n+m):
        print("No")
    elif(a<b and m<=a and b>=n):
        print("Yes")
    elif(b<a and m<=b and a>=n):
        print("Yes")
    elif(b>=n and b>=m and a>=n and a>=m):
        print("Yes")
    else:
        print("No")


t=int(input())
for _ in range(t):
    a,b,n,m=map(int,input().split())
    if(a+b<n+m):
        print("No")
    elif(a<=b and m>a):
        print("No")
    elif(b<a and m>b):
        print("No")
    else:
        print("Yes")


t=int(input())
for _ in range(t):
    a,b,n,m=map(int,input().split())
    if(a+b<n+m):
        print("No")
    elif(a<b and m>a):
        print("No")
    elif(b<a and m>b):
        print("No")
    else:
        print("Yes")


t = int(input())
for i in range(t) :
    a,b,n,m = list(map(int,input().split()))
    if (a+b) == 0 :
        print("NO")
    elif (n+m) == 0 :
        print("YES")
    elif (a==b) and (n==m) :
        print("YES")
    elif (a>=b) and (n<m) :
        print("YES")
    elif (a>=b) and (n>=m) :
        print("NO")
    elif (a<b) and (n>=m) :
        print("NO")
    else :
        print("YES")


t = int(input())
for i in range(t) :
    a,b,n,m = list(map(int,input().split()))
    if (a+b) == 0 :
        print("NO")
    elif (n+m) == 0 :
        print("YES")
    elif (a==b) and (n==m) :
        print("YES")
    elif (a>=b) and (n<m) :
        print("YES")
    elif (a>=b) and (n>=m) :
        print("NO")
    elif (a<b) and (n>=m) :
        print("NO")
    else :
        print("NO")


t = int(input())
for i in range(t):
  a,b,n,m=map(int,input().split())
  if n+m>a+b:
    print('NO')
  elif m==0:
    print('YES')
  elif a>b:
    if m<=b:
      print('YES')
    else:
      print('NO')
  else:
      if m<=a:
         print('YES')
      else:
        print('NO')


t = int(input())
for i in range(t) :
    a,b,n,m = list(map(int,input().split()))
    if (a+b) == 0 :
        print("NO")
    elif (n+m) == 0 :
        print("YES")
    elif a==0 or b==0 or n==0 or m==0 :
        print("NO")
    elif (a==b) and (n==m) :
        print("YES")
    elif (a>=b) and (n<m) :
        print("YES")
    elif (a>=b) and (n>=m) :
        print("NO")
    elif (a<b) and (n>=m) :
        print("NO")
    elif (a<b) and (n<m) :
        print("YES")
    else :
        print("NO")


for _ in range(int(input())):
    a,b,n,m=map(int,input().split())
    x=min(a,b)
    y=max(a,b)
    if x<m:
        print("No")
    else:
        y=y+x-m
        if y<n:
            print("No")
        else:
            print("Yes")


for _ in range(int(input())):
    a,b,n,m=map(int,input().split())
    if a>b:
        x=a-b
        if x>=m:
            x-=m
            if a+b-x>=n:
                print("Yes")
            else:
                print("No")
        elif x<m:
            m-=x
            if 2*b>=n+m:
                print("Yes")
            else:
                print("No")
    else:
        y=b-a
        if y>=n:
            y-=n
            if a+b-y>=m:
                print("Yes")
            else:
                print("No")
        elif y<n:
            n-=y
            if 2*a>=n+m:
                print("Yes")
            else:
                print("No")


for _ in range(int(input())):
    a,b,n,m=map(int,input().split())
    if a+b<n+m:
        print("No")
    elif a>b:
        x=a-b
        if x>=m:
            x-=m
            if a+b-x>=n:
                print("Yes")
            else:
                print("No")
        elif x<m:
            m-=x
            if 2*b>=n+m:
                print("Yes")
            else:
                print("No")
    else:
        y=b-a
        if y>=n:
            y-=n
            if a+b-y>=m:
                print("Yes")
            else:
                print("No")
        elif y<n:
            n-=y
            if 2*a>=n+m:
                print("Yes")
            else:
                print("No")


t = int(input())
while t>0:
	t -= 1
	a, b, n, m = input().split()
	a, b, n, m = int(a), int(b), int(n), int(m)

	if a>0 and b>0 and a+b >= n+m:
		print('Yes')
	else:
		print('No')


t = int(input())
while t>0:
	t -= 1
	a, b, n, m = input().split()
	a, b, n, m = int(a), int(b), int(n), int(m)

	if a==0 and b==0 and n==0 and m==0:
		print('Yes')
	if a>0 and b>0 and a+b >= n+m:
		print('Yes')
	else:
		print('No')


t = int(input())
while t>0:
	t -= 1
	a, b, n, m = input().split()
	a, b, n, m = int(a), int(b), int(n), int(m)

	if n+m > a+b:
		print('No')
		continue
	a, b = max(a,b), min(a,b)
	c = max(n - a, 0)
	if b < c+m:
		print('No')
	else:
		print('Yes')


t = int(input())
while t>0:
	t -= 1
	a, b, n, m = input().split()
	a, b, n, m = int(a), int(b), int(n), int(m)

	if a==0 and b==0 and n==0 and m==0:
		print('Yes')
	elif a>=n and a>=m and b>=m:
		print('Yes')
	else:
		print('No')


t = int(input())
while t>0:
	t -= 1
	a, b, n, m = input().split()
	a, b, n, m = int(a), int(b), int(n), int(m)

	if n+m > a+b;
		print('No')
		continue
	a, b = max(a,b), min(a,b)
	c = max(n - a, 0)
	if b <= c+m:
		print('No')
	else:
		print('Yes')


#Consistency is the key :)
#code by: amrit2000
from sys import stdin,stdout
import math
input=stdin.readline
def print(x='',y='',end='\n'):
    if y=='':
        stdout.write(str(x)+end)
    else:
        stdout.write(str(x)+' '+str(y)+end)

def solve():
    a,b,n,m=map(int,input().split())

    if a+b<m+n:
        print('NO')
        return

    if m>n and min(a,b)<m:
        print('NO')
        return
    if max(m,n)<=min(a,b):
        print('YES')
        return
    print('NO')

tt=1
tt=int(input())
for __ in range(tt):
    solve()


#Consistency is the key :)
#code by: amrit2000
from sys import stdin,stdout
import math
input=stdin.readline
def print(x='',y='',end='\n'):
    if y=='':
        stdout.write(str(x)+end)
    else:
        stdout.write(str(x)+' '+str(y)+end)

def solve():
    a,b,n,m=map(int,input().split())

    if a+b<m+n:
        print('NO')
        return

    if min(a,b)<m:
        print('NO')
    else:
        print('YES')

tt=1
tt=int(input())
for __ in range(tt):
    solve()


#Consistency is the key :)
#code by: amrit2000
from sys import stdin,stdout
import math
input=stdin.readline
def print(x='',y='',end='\n'):
    if y=='':
        stdout.write(str(x)+end)
    else:
        stdout.write(str(x)+' '+str(y)+end)

def solve():
    a,b,n,m=map(int,input().split())

    if a+b<m+n:
        print('NO')
        return

    if m>n and min(a,b)<m:
        print('NO')
        return
    print('YES')

tt=1
tt=int(input())
for __ in range(tt):
    solve()


t=int(input())
while(t>0):
    a,b,n,m = map(int, input().split())
    if (a+b)>=(n+m) and m<=min(a,b):
        print("Yes")
    else:
        print("No")

    t-=1


t=int(input())
while(t>0):
    a,b,n,m = map(int, input().split())
    if (a+b)>=(n+m) and m<min(a,b):
        print("Yes")
    else:
        print("No")

    t-=1


t=int(input())
while(t>0):
    a,b,n,m = map(int, input().split())
    if (a+b)>=(n+m):
        print("Yes")
    elif m<min(a,b):
        print("Yes")
    else:
        print("No")

    t-=1


t=int(input())
while(t>0):
    a,b,n,m = map(int, input().split())
    if (a+b)>=(n+m) or m<min(a,b):
        print("Yes")
    else:
        print("No")

    t-=1


n=int(input())
for i in range(n):
     a, b, n, m = map(int, input().split())
     if a+b<n+m:
         print("No")
     elif a<=b and a>=m:
         print("Yes")
     elif a>=b and b>=m:
         print("Yes")
     else:
         print("No")


n=int(input())
for i in range(n):
     a, b, n, m = map(int, input().split())
     if a+b<n+m:
         print("No")
     elif a<=b and a>=m:
         print("Yes")
     else:
         print("No")


n=int(input())
for i in range(n):
     a, b, n, m = map(int, input().split())
     if a+b<n+m:
         print("No")
     elif min(a,b)>=m:
         print("Yes")
     else:
         print("No")


#https://codeforces.com/contest/1372/problem/B
for t in range(int(input())):
    a,b,n,m = map(int,input().split())
    a,b = min(a,b),max(a,b)
    if m<=a and n+m<=a+b:
        print("Yes")
    else:
        print("No")


#https://codeforces.com/contest/1372/problem/B
for t in range(int(input())):
    a,b,n,m = map(int,input().split())
    a,b = min(a,b),max(a,b)
    if m>a and n+m>=b+b:
        print("Yes")
    else:
        print("No")


#https://codeforces.com/contest/1372/problem/B
for t in range(int(input())):
    a,b,n,m = map(int,input().split())
    a,b = min(a,b),max(a,b)
    if (b-a>n and a<m) or (a+b<n+m):
        print("No")
    else:
        print("Yes")


#https://codeforces.com/contest/1372/problem/B
for t in range(int(input())):
    a,b,n,m = map(int,input().split())
    a,b = min(a,b),max(a,b)
    if m<=a and n+m>=a+b:
        print("Yes")
    else:
        print("No")


#https://codeforces.com/contest/1372/problem/B
for t in range(int(input())):
    a,b,n,m = map(int,input().split())
    a,b = min(a,b),max(a,b)
    if m<=a and n+m=<a+b:
        print("Yes")
    else:
        print("No")


for i in range(int(input())):
 a,b,n,m=map(int,input().split())
 if((n+m<=a+b) and (m<min(a,b)) and (n+m)>0):
  print("Yes")
 else:
  print("No")


for i in range(int(input())) :
    a, b, n, m = map(int, input().split())
    if n+m <= a+b and m <= min(a, b) :
        print('YES')
    else :
        print('NO')


tst = int(input())

t=1
while t<=tst:
    v,c,n,m=[int(x) for x in input().split()]
    if(v+c<n+m):
        print("NO")
        continue
    x=min(v,c)
    if m<=x:
        print("YES")
    else:
        print("NO")

    t+=1


tst = int(input())

t=1
while t<=tst:
    v,c,n,m=map(int,input().split())
    if(v+c<n+m):
        print("NO")
    else:
        x=min(v,c)
        if m<=x:
            print("YES")
        else:
            print("NO")

    t+=1


tst = int(input())

t=1
while t<=tst:
    v,c,n,m=map(int,input().split())
    if(v+c<n+m):
        print("NO")
        continue
    x=min(v,c)
    if m<=x:
        print("YES")
    else:
        print("NO")

    t+=1


t=int(input())
for _ in range(t):
    a,b,n,m=map(int,input().split())
    if a==0 and b==0:
        print("No")
    elif a<b and a<m:
        print("No")
    elif b<a and b<m:
        print("No")
    elif a+b<m+n:
        print("No")
    else:
        print("Yes")


t=int(input())
for _ in range(t):
    a,b,n,m=map(int,input().split())
    if a==0 and b==0:
        print("No")
    elif a<b and a<m:
        print("No")
    elif a+b<m+n:
        print("No")
    else:
        print("Yes")


t=int(input())
for _ in range(t):
    a,b,n,m=map(int,input().split())
    if a==0 and b==0:
        print("No")
    elif a<b and a<m:
        print("No")
    elif b<a and b<m:
        print("No")
    elif a==b and a<m:
        print("No")
    elif a+b<m+n:
        print("No")
    else:
        print("Yes")


t=int(input())
for _ in range(t):
    a,b,n,m=map(int,input().split())
    if(a+b>=n+m and a>=m and b>=m):
        print("YES")
    else:
        print("NO")


t=int(input())
for _ in range(t):
    a,b,n,m=map(int,input().split())
    if(a!=0 and b!=0 and a+b>=n+m):
        print("YES")
    else:
        print("NO")


t=int(input())
for _ in range(t):
    a,b,n,m=map(int,input().split())
    if(a==0 and n==0):
        print('NO')
    elif(b==0 and m==0 and a<n):
        print('NO')
    elif(a+b<n+m):
        print('NO')
    else:
        print("YES")


for _ in range(int(input())):
	v,c,n,m=map(int,input().split())
	if (v==0 and c!=0) or (c==0 and v!=0):
		if m!=0:
			print("No")
	elif (v+c)<(n+m):
		print("No")
	else:
		print("Yes")


for _ in range(int(input())):
	v,c,n,m=map(int,input().split())
	if m>c or m>v or n>c or n>v:
		print("No")
	elif (v==0 and c!=0) or (c==0 and v!=0):
		if m!=0:
			print("No")
	elif (v+c)<(n+m):
		print("No")
	else:
		print("Yes")


for _ in range(int(input())):
	v,c,n,m=map(int,input().split())
	if (v+c)<(n+m) or min(v,c)<m:
		print("No")
	else:
		print("Yes")


for i in range(int(input())):
    a,b,n,m = map(int,input().split())
    if min(a,b)>=m and (a+b)>=(m+n):print("Yes")
    else:print('No')


for i in range(int(input())):
    a,b,n,m = map(int,input().split())
    f =True
    if n>=m:
        if a>b:
            if a>=n: a = a-n
            #elif b>=(n-a): b = b-(n-a)
            else: f= False
        else:
            if b>=n: b = b-n
            #elif a>=(n-b): a = a-(n-b)
            else: f=False
        if a>=m: a = a-m
        #elif b>=(m-a): b = b-(m-a)
        else: f= False

    elif m>n:
        if a>b:
            if b>=m: b = b-m
            #elif a>=(m-b): a = a-(m-b)
            else: f=False
        else:
            if a>=m: a = a-m
            #elif b>=(m-a): b = b-(m-a)
            else: f= False

        if(b>=n):b=b-n
        #elif b>=(n-a): b=b-(n-a)
        else:f = False

    if(f):print("Yes")
    else:print("No")


for i in range(int(input())):
    a,b,n,m = map(int,input().split())
    f =False
    if(n>=a and m>=b):f=True
    if(f):print("Yes")
    else:print("No")


import math
import collections
t = int(input())
import math
for i in range(t):
     a,b,n,m = map(int,input().split())
     if n+m>a+b:
          print("No")
          continue
     small = a if a<b else b
     big = b if small == a else a
     if small<m:
          print("No")
          continue

     print("Yes")


import math
import collections
t = int(input())
import math
for i in range(t):
     a,b,n,m = map(int,input().split())
     if n+m>a+b:
          print("No")
          continue
     small = a if a<b else b
     big = b if small == a else a
     if small<m:
          print("No")
          continue
     if big<n:
          print("No")
          continue
     print("Yes")


for _ in range(int(input())):
    a,b,n,m = list(map(int, input().split()))
    if (a+b) >= (n+m) and a > 0 and b > 0:
        print("Yes")
    else:
        print("No")


for _ in range(int(input())):
    a,b,n,m = list(map(int, input().split()))
    if (a+b) >= (n+m) and a > 0 and b > 0 and min(a, b) >= m:
        print("Yes")
    else:
        print("No")


for _ in range(int(input())):
    a,b,n,m = list(map(int, input().split()))
    if (a+b) >= (n+m) and min(a, b) >= m:
        print("Yes")
    else:
        print("No")


t=int(input())
for i in range(t):
    a,b,n,m=map(int,input().split())
    if a>b:
        d=a-b
        a-=min(n,d)
        n-=min(n,d)
    elif a<b:
        d=b-a
        b-=min(n,d)
        n-=min(n,d)
    if a>b:
        if b>=m:
            print("Yes")
        else:
            print("No")
    elif a<b:
        if a>=m:
            print("Yes")
        else:
            print("No")
    else:
        a,b,n,m=a-min(a,n,m),a-min(a,n,m),n-min(a,n,m),m-min(a,n,m)
        if m!=0:
            if a>=m:
                print("Yes")
            else:
                print("No")
        elif n!=0:
            if a+b>=n:
                print("Yes")
            else:
                print("No")
        elif n==0 and m==0:
            print("Yes")
        else:
            print("No")


t=int(input())
for i in range(t):
    a,b,n,m=map(int,input().split())
    if a>b:
        d=a-b
        a-=min(n,d)
        n-=min(n,d)
    elif a<b:
        d=b-a
        b-=min(n,d)
        n-=min(n,d)
    if a>b:
        if b>=m:
            print("Yes")
        else:
            print("No")
    elif a<b:
        if a>=m:
            print("Yes")
        else:
            print("No")
    else:
        if a+b>=n+m:
            print("Yes")
        else:
            print("No")


t=int(input())
for i in range(t):
    a,b,n,m=map(int,input().split())
    if a>b:
        d=a-b
        a-=min(n,d)
        n-=min(n,d)
    elif a<b:
        d=b-a
        b-=min(n,d)
        n-=min(n,d)
    if a>b:
        if b>=m:
            print("Yes")
        else:
            print("No")
    elif a<b:

        if a>=m:
            print("Yes")
        else:
            print("No")
    else:
        n-=b//2
        m-=b//2
        n-=a//2
        m-=a//2
        if n>0:
            n-=a%2+b%2
        elif m>0:
            m-=a%2+b%2
        if n<=0 and m<=0:
            print("Yes")
        else:
            print("No")