HTML & JS
#include<bits/stdc++.h>
#define endl '\n'
#define maxn 100010
using namespace std;

int l[maxn] , r[maxn] , ans[maxn] , t , n;

vector < int > adj[maxn] , adj2[maxn];

void clean()
{
    for(int i = 1; i <= maxn; i++)
    {
        adj[i].clear();

        adj2[i].clear();
    }
}


int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    cout << setprecision(3) << fixed;

    if(fopen("ArturoTextC.txt" , "r")) freopen("ArturoTextC.txt" , "r" , stdin);

    cin >> t;

    while(t--)
    {
        cin >> n;

        clean();

        for(int i = 0; i < n; i++)
        {
            cin >> l[i] >> r[i];

            adj[l[i]].emplace_back(i);

            adj2[r[i]].emplace_back(i);
        }

        set < int > notclosed;

        set < int > goodindices;

        bool ok1 = false , ok2 = false;

        for(int i = 1; i <= maxn; i++)
        {
            for(int indices : adj[i])
            {
                notclosed.insert(indices);

                goodindices.insert(indices);
            }

            for(int indices : adj2[i])
            {
                notclosed.erase(indices);
            }

            if(notclosed.size() == 0)
            {
                if(goodindices.size() > 1)
                {
                    for(int to : goodindices)
                    {
                        ok1 = true;
                        ans[to] = 1;
                    }
                }
                else
                {
                    for(int to : goodindices)
                    {
                        ok2 = true;
                        ans[to] = 2;
                    }
                }

                goodindices.clear();
            }
        }

        if(ok1 && ok2)
        {
            for(int i = 0; i < n; i++)
            {
                cout << ans[i] << " ";
            }
        }
        else
        {
            if(ok1 == false && ok2 == true)
            {
                ans[0] = 1;

                for(int i = 0; i < n; i++)
                {
                    cout << ans[i] << " ";
                }
            }
            else
            {
                cout << -1;
            }
        }

        cout << endl;
    }

    return 0;
}

Generated URL:

Output:

1 2 
-1
1 2 2 
Report Bug