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

int find_parent(vector<int> par,int i)
{
    if(i==par[i])
        return i;
    par[i]=find_parent(par,par[i]);
    return par[i];
}

void union_(vector<int>& par,int x,int y)
{
    int i=find_parent(par,x);
    int j=find_parent(par,y);
    par[j]=i;
}

int main()
{
    int n;
    cin>>n;
    vector<pair<int,int> > G;
    int days=0;

    vector<int> parent(n+1,0);
    vector<pair<int,int> > to_break;
    vector<pair<int,int> > to_build;
    for(int i=1;i<=n;i++)
        parent[i]=i;

    for(int i=1;i<=n-1;i++)
    {
        int a,b;
        cin>>a>>b;
        int x=find_parent(parent,a);
        int y=find_parent(parent,b);

        if(x!=y)
        {
            union_(parent,x,y);
            G.push_back(make_pair(a,b));
        }
        else
        {
            days++;
            to_break.push_back(make_pair(a,b));
        }
    }

    int sz=to_break.size();
    if(sz==0)
    {
        cout<<sz<<endl;
    }
    else{
            cout<<sz<<endl;

    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            if(find_parent(parent,i)!=find_parent(parent,j))
            {
                to_build.push_back(make_pair(i,j));
                union_(parent,find_parent(parent,i),find_parent(parent,j));
                sz--;
            }
        }
        if(sz==0)
            break;
    }

    for(int i=0;i<to_break.size();i++)
        cout<<to_break[i].first<<" "<<to_break[i].second<<" "<<to_build[i].first<<" "<<to_build[i].second<<endl;
    }
    return 0;
}


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

int find_parent(vector<int> par,int i)         //if this doesn't work try out path compression and union by rank.
{
    if(i==par[i])
        return i;
    par[i]=find_parent(par,par[i]);
    return par[i];
}

void union_(vector<int>& par,int x,int y)
{
    int i=find_parent(par,x);
    int j=find_parent(par,y);
    par[j]=i;
}

int main()
{
    int n;
    cin>>n;
    vector<pair<int,int> > G;
    int days=0;

    vector<int> parent(n+1,0);
    vector<pair<int,int> > to_break;
    vector<pair<int,int> > to_build;
    for(int i=1;i<=n;i++)
        parent[i]=i;

    for(int i=1;i<=n-1;i++)
    {
        int a,b;
        cin>>a>>b;
        int x=find_parent(parent,a);
        int y=find_parent(parent,b);

        if(x!=y)
        {
            union_(parent,x,y);
            G.push_back(make_pair(a,b));
        }
        else
        {
            days++;
            to_break.push_back(make_pair(a,b));
        }
    }
//    cout<<endl;
    int sz=to_break.size();
    if(sz==0)
    {
        cout<<sz<<endl;
    }
    else{
            cout<<sz<<endl;
/*    for(int i=0;i<to_break.size();i++)
    {

        int r=find_parent(parent,to_break[i].first);
        for(int j=n;j>=1;j--)
        {
            int s=find_parent(parent,j);
            if(r!=s)
            {
                cout<<to_break[i].first<<" "<<to_break[i].second<<" "<<to_break[i].first<<" "<<j<<endl;
                union_(parent,r,s);
                break;
            }
        }
//        cout<<to_break[i].first<<" "<<to_break[i].second<<endl;

    }*/
    for(int i=0;i<to_break.size();i++)
    {
        int v=to_break[i].first;
        int r=find_parent(parent,v);
        for(int j=1;j<=n;j++)
        {
            if(j!=v)
            {
                int s=find_parent(parent,j);
                if(r!=s)
                {
                    to_build.push_back(make_pair(v,j));
                    union_(parent,r,s);
                    break;
                }
            }
        }
    }

    for(int i=0;i<to_break.size();i++)
    {
        cout<<to_break[i].first<<" "<<to_break[i].second<<" "<<to_build[i].first<<" "<<to_build[i].second<<endl;
    }

    }
    return 0;
}


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

int find_parent(vector<int> par,int i)         //if this doesn't work try out path compression and union by rank.
{
    if(i==par[i])
        return i;
    par[i]=find_parent(par,par[i]);
    return par[i];
}

void union_(vector<int>& par,int x,int y)
{
    int i=find_parent(par,x);
    int j=find_parent(par,y);
    par[j]=i;
}

int main()
{
    int n;
    cin>>n;
    vector<pair<int,int> > G;
    int days=0;

    vector<int> parent(n+1,0);
    vector<pair<int,int> > to_break;
    vector<pair<int,int> > to_build;
    for(int i=1;i<=n;i++)
        parent[i]=i;

    for(int i=1;i<=n-1;i++)
    {
        int a,b;
        cin>>a>>b;
        int x=find_parent(parent,a);
        int y=find_parent(parent,b);

        if(x!=y)
        {
            union_(parent,x,y);
            G.push_back(make_pair(a,b));
        }
        else
        {
            days++;
            to_break.push_back(make_pair(a,b));
        }
    }

    int sz=to_break.size();
    if(sz==0)
    {
        cout<<sz<<endl;
    }
    else{
            cout<<sz<<endl;
/*   for(int i=0;i<to_break.size();i++)
    {

        int r=find_parent(parent,to_break[i].first);
        for(int j=n;j>=1;j--)
        {
            int s=find_parent(parent,j);
            if(r!=s)
            {
                cout<<to_break[i].first<<" "<<to_break[i].second<<" "<<to_break[i].first<<" "<<j<<endl;
                union_(parent,r,s);
                break;
            }
        }

    }
*/
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            if(parent[i]!=parent[j])
            {
                to_build.push_back(make_pair(i,j));
                sz--;
            }
        }
        if(sz==0)
            break;
    }

    for(int i=0;i<to_break.size();i++)
        cout<<to_break[i].first<<" "<<to_break[i].second<<" "<<to_build[i].first<<" "<<to_build[i].second<<endl;
    }
    return 0;
}


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

int find_parent(vector<int> par,int i)         //if this doesn't work try out path compression and union by rank.
{
    if(i==par[i])
        return i;
    return find_parent(par,par[i]);
}

void union_(vector<int>& par,int x,int y)
{
    int i=find_parent(par,x);
    int j=find_parent(par,y);
    par[j]=i;
}

int main()
{
    int n;
    cin>>n;
    vector<pair<int,int> > G;
    int days=0;

    vector<int> parent(n+1,0);
    vector<pair<int,int> > to_break;
    for(int i=1;i<=n;i++)
        parent[i]=i;

    for(int i=1;i<=n-1;i++)
    {
        int a,b;
        cin>>a>>b;
        int x=find_parent(parent,a);
        int y=find_parent(parent,b);

        if(x!=y)
        {
            union_(parent,x,y);
            G.push_back(make_pair(a,b));
        }
        else
        {
            days++;
            to_break.push_back(make_pair(a,b));
        }
    }
//    cout<<endl;
    int sz=to_break.size();
    if(sz==0)
    {
        cout<<sz<<endl;
    }
    else{
            cout<<sz<<endl;
    for(int i=0;i<to_break.size();i++)
    {
        cout<<to_break[i].first<<" "<<to_break[i].second<<" ";
        int r=find_parent(parent,to_break[i].first);
        for(int j=n;j>=1;j--)
        {
            int s=find_parent(parent,j);
            if(r!=s)
            {
                cout<<to_break[i].first<<" "<<j<<endl;
                union_(parent,r,s);
            }
        }

    }
    }
    return 0;
}


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

int find_parent(vector<int> par,int i)         //if this doesn't work try out path compression and union by rank.
{
    if(i==par[i])
        return i;
    return find_parent(par,par[i]);
}

void union_(vector<int>& par,int x,int y)
{
    int i=find_parent(par,x);
    int j=find_parent(par,y);
    par[j]=i;
}

int main()
{
    int n;
    cin>>n;
    vector<pair<int,int> > G;
    int days=0;

    vector<int> parent(n+1,0);
    vector<pair<int,int> > to_break;
    for(int i=1;i<=n;i++)
        parent[i]=i;

    for(int i=1;i<=n-1;i++)
    {
        int a,b;
        cin>>a>>b;
        int x=find_parent(parent,a);
        int y=find_parent(parent,b);

        if(x!=y)
        {
            union_(parent,x,y);
            G.push_back(make_pair(a,b));
        }
        else
        {
            days++;
            to_break.push_back(make_pair(a,b));
        }
    }
//    cout<<endl;
    int sz=to_break.size();
    if(sz==0)
    {
        cout<<sz<<endl;
    }
    else{
    for(int i=0;i<to_break.size();i++)
    {
        cout<<to_break[i].first<<" "<<to_break[i].second<<" ";
        int r=find_parent(parent,to_break[i].first);
        for(int j=n;j>=1;j--)
        {
            int s=find_parent(parent,j);
            if(r!=s)
            {
                cout<<to_break[i].first<<" "<<j<<endl;
                union_(parent,r,s);
            }
        }

    }
    }
    return 0;
}


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

int find_parent(vector<int> par,int i)         //if this doesn't work try out path compression and union by rank.
{
    if(i==par[i])
        return i;
    par[i]=find_parent(par,par[i]);
    return par[i];
}

void union_(vector<int>& par,int x,int y)
{
    int i=find_parent(par,x);
    int j=find_parent(par,y);
    par[j]=i;
}

int main()
{
    int n;
    cin>>n;
    vector<pair<int,int> > G;
    int days=0;

    vector<int> parent(n+1,0);
    vector<pair<int,int> > to_break;
    vector<pair<int,int> > to_build;
    for(int i=1;i<=n;i++)
        parent[i]=i;

    for(int i=1;i<=n-1;i++)
    {
        int a,b;
        cin>>a>>b;
        int x=find_parent(parent,a);
        int y=find_parent(parent,b);

        if(x!=y)
        {
            union_(parent,x,y);
            G.push_back(make_pair(a,b));
        }
        else
        {
            days++;
            to_break.push_back(make_pair(a,b));
        }
    }
//    cout<<endl;
    int sz=to_break.size();
    if(sz==0)
    {
        cout<<sz<<endl;
    }
    else{
            cout<<sz<<endl;
   for(int i=0;i<to_break.size();i++)
    {

        int r=find_parent(parent,to_break[i].first);
        for(int j=n;j>=1;j--)
        {
            int s=find_parent(parent,j);
            if(r!=s)
            {
                cout<<to_break[i].first<<" "<<to_break[i].second<<" "<<to_break[i].first<<" "<<j<<endl;
                union_(parent,r,s);
                break;
            }
        }
//        cout<<to_break[i].first<<" "<<to_break[i].second<<endl;

    }
    /*
    for(int i=0;i<to_break.size();i++)
    {
        int v=to_break[i].first;
        int r=find_parent(parent,v);
        for(int j=1;j<=n;j++)
        {
            if(j!=v)
            {
                int s=find_parent(parent,j);
                if(r!=s)
                {
                    to_build.push_back(make_pair(v,j));
                    union_(parent,r,s);
                    break;
                }
            }
        }
    }

    for(int i=0;i<to_break.size();i++)
    {
        cout<<to_break[i].first<<" "<<to_break[i].second<<" "<<to_build[i].first<<" "<<to_build[i].second<<endl;
    }*/

    }
    return 0;
}


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

int find_parent(vector<int> par,int i)         //if this doesn't work try out path compression and union by rank.
{
    if(i==par[i])
        return i;
    par[i]=find_parent(par,par[i]);
    return par[i];
}

void union_(vector<int>& par,int x,int y)
{
    int i=find_parent(par,x);
    int j=find_parent(par,y);
    par[j]=i;
}

int main()
{
    int n;
    cin>>n;
    vector<pair<int,int> > G;
    int days=0;

    vector<int> parent(n+1,0);
    vector<pair<int,int> > to_break;
    vector<pair<int,int> > to_build;
    for(int i=1;i<=n;i++)
        parent[i]=i;

    for(int i=1;i<=n-1;i++)
    {
        int a,b;
        cin>>a>>b;
        int x=find_parent(parent,a);
        int y=find_parent(parent,b);

        if(x!=y)
        {
            union_(parent,x,y);
            G.push_back(make_pair(a,b));
        }
        else
        {
            days++;
            to_break.push_back(make_pair(a,b));
        }
    }
//    cout<<endl;
    int sz=to_break.size();
    if(sz==0)
    {
        cout<<sz<<endl;
    }
    else{
            cout<<sz<<endl;
    for(int i=0;i<to_break.size();i++)
    {

        int r=find_parent(parent,to_break[i].first);
        for(int j=n;j>=1;j--)
        {
            int s=find_parent(parent,j);
            if(r!=s)
            {
                cout<<to_break[i].first<<" "<<to_break[i].second<<" "<<to_break[i].first<<" "<<j<<endl;
                union_(parent,r,s);
                break;
            }
        }
//        cout<<to_break[i].first<<" "<<to_break[i].second<<endl;

    }
 /*   for(int i=0;i<to_break.size();i++)
    {
        int v=to_break[i].first;
        int r=find_parent(parent,v);
        for(int j=1;j<=n;j++)
        {
            if(j!=v)
            {
                int s=find_parent(parent,j);
                if(r!=s)
                {
                    to_build.push_back(make_pair(v,j));
                    union_(parent,r,s);
                }
            }
        }
    }

    for(int i=0;i<to_break.size();i++)
    {
        cout<<to_break[i].first<<" "<<to_break[i].second<<" "<<to_build[i].first<<" "<<to_build[i].second<<endl;
    }*/

    }
    return 0;
}


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

int find_parent(vector<int> par,int i)         //if this doesn't work try out path compression and union by rank.
{
    if(i==par[i])
        return i;
    par[i]=find_parent(par,par[i]);
    return par[i];
}

void union_(vector<int>& par,int x,int y)
{
    int i=find_parent(par,x);
    int j=find_parent(par,y);
    par[j]=i;
}

int main()
{
    int n;
    cin>>n;
    vector<pair<int,int> > G;
    int days=0;

    vector<int> parent(n+1,0);
    vector<pair<int,int> > to_break;
    vector<pair<int,int> > to_build;
    for(int i=1;i<=n;i++)
        parent[i]=i;

    for(int i=1;i<=n-1;i++)
    {
        int a,b;
        cin>>a>>b;
        int x=find_parent(parent,a);
        int y=find_parent(parent,b);

        if(x!=y)
        {
            union_(parent,x,y);
            G.push_back(make_pair(a,b));
        }
        else
        {
            days++;
            to_break.push_back(make_pair(a,b));
        }
    }

    int sz=to_break.size();
    if(sz==0)
    {
        cout<<sz<<endl;
    }
    else{
            cout<<sz<<endl;
/*   for(int i=0;i<to_break.size();i++)
    {

        int r=find_parent(parent,to_break[i].first);
        for(int j=n;j>=1;j--)
        {
            int s=find_parent(parent,j);
            if(r!=s)
            {
                cout<<to_break[i].first<<" "<<to_break[i].second<<" "<<to_break[i].first<<" "<<j<<endl;
                union_(parent,r,s);
                break;
            }
        }

    }
*/
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            if(find_parent(parent,i)!=find_parent(parent,j))
            {
                to_build.push_back(make_pair(i,j));
                union_(parent,find_parent(parent,i),find_parent(parent,j));
                sz--;
            }
        }
        if(sz==0)
            break;
    }

    for(int i=0;i<to_break.size();i++)
        cout<<to_break[i].first<<" "<<to_break[i].second<<" "<<to_build[i].first<<" "<<to_build[i].second<<endl;
    }
    return 0;
}


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

int find_parent(vector<int> par,int i)         //if this doesn't work try out path compression and union by rank.
{
    if(i==par[i])
        return i;
    return find_parent(par,par[i]);
}

void union_(vector<int>& par,int x,int y)
{
    int i=find_parent(par,x);
    int j=find_parent(par,y);
    par[j]=i;
}

int main()
{
    int n;
    cin>>n;
    vector<pair<int,int> > G;
    int days=0;

    vector<int> parent(n+1,0);
    vector<pair<int,int> > to_break;
    for(int i=1;i<=n;i++)
        parent[i]=i;

    for(int i=1;i<=n-1;i++)
    {
        int a,b;
        cin>>a>>b;
        int x=find_parent(parent,a);
        int y=find_parent(parent,b);

        if(x!=y)
        {
            union_(parent,x,y);
            G.push_back(make_pair(a,b));
        }
        else
        {
            days++;
            to_break.push_back(make_pair(a,b));
        }
    }
//    cout<<endl;
    int sz=to_break.size();
    if(sz==0)
    {
        cout<<sz<<endl;
    }
    else{
            cout<<sz<<endl;
    for(int i=0;i<to_break.size();i++)
    {

        int r=find_parent(parent,to_break[i].first);
        for(int j=n;j>=1;j--)
        {
            int s=find_parent(parent,j);
            if(r!=s)
            {
                cout<<to_break[i].first<<" "<<to_break[i].second<<" "<<to_break[i].first<<" "<<j<<endl;
                union_(parent,r,s);
                break;
            }
        }
//        cout<<to_break[i].first<<" "<<to_break[i].second<<endl;

    }
    }
    return 0;
}


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

int find_parent(vector<int> par,int i)         //if this doesn't work try out path compression and union by rank.
{
    if(i==par[i])
        return i;
    return (par[i]=find_parent(par,par[i]));
}

void union_(vector<int>& par,int x,int y)
{
    int i=find_parent(par,x);
    int j=find_parent(par,y);
    par[j]=i;
}

int main()
{
    int n;
    cin>>n;
    vector<pair<int,int> > G;
    int days=0;

    vector<int> parent(n+1,0);
    vector<pair<int,int> > to_break;
    for(int i=1;i<=n;i++)
        parent[i]=i;

    for(int i=1;i<=n-1;i++)
    {
        int a,b;
        cin>>a>>b;
        int x=find_parent(parent,a);
        int y=find_parent(parent,b);

        if(x!=y)
        {
            union_(parent,x,y);
            G.push_back(make_pair(a,b));
        }
        else
        {
            days++;
            to_break.push_back(make_pair(a,b));
        }
    }
//    cout<<endl;
    int sz=to_break.size();
    if(sz==0)
    {
        cout<<sz<<endl;
    }
    else{
            cout<<sz<<endl;
    for(int i=0;i<to_break.size();i++)
    {

        int r=find_parent(parent,to_break[i].first);
        for(int j=n;j>=1;j--)
        {
            int s=find_parent(parent,j);
            if(r!=s)
            {
                cout<<to_break[i].first<<" "<<to_break[i].second<<" "<<to_break[i].first<<" "<<j<<endl;
                union_(parent,r,s);
                break;
            }
        }
//        cout<<to_break[i].first<<" "<<to_break[i].second<<endl;

    }
    }
    return 0;
}


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

int find_parent(vector<int> par,int i)         //if this doesn't work try out path compression and union by rank.
{
    if(i==par[i])
        return i;
    par[i]=find_parent(par,par[i]);
    return par[i];
}

void union_(vector<int>& par,int x,int y)
{
    int i=find_parent(par,x);
    int j=find_parent(par,y);
    par[j]=i;
}

int main()
{
    int n;
    cin>>n;
    vector<pair<int,int> > G;
    int days=0;

    vector<int> parent(n+1,0);
    vector<pair<int,int> > to_break;
    vector<pair<int,int> > to_build;
    for(int i=1;i<=n;i++)
        parent[i]=i;

    for(int i=1;i<=n-1;i++)
    {
        int a,b;
        cin>>a>>b;
        int x=find_parent(parent,a);
        int y=find_parent(parent,b);

        if(x!=y)
        {
            union_(parent,x,y);
            G.push_back(make_pair(a,b));
        }
        else
        {
            days++;
            to_break.push_back(make_pair(a,b));
        }
    }
//    cout<<endl;
    int sz=to_break.size();
    if(sz==0)
    {
        cout<<sz<<endl;
    }
    else{
            cout<<sz<<endl;
    /*for(int i=0;i<to_break.size();i++)
    {

        int r=find_parent(parent,to_break[i].first);
        for(int j=n;j>=1;j--)
        {
            int s=find_parent(parent,j);
            if(r!=s)
            {
                cout<<to_break[i].first<<" "<<to_break[i].second<<" "<<to_break[i].first<<" "<<j<<endl;
                union_(parent,r,s);
                break;
            }
        }
//        cout<<to_break[i].first<<" "<<to_break[i].second<<endl;

    }*/
    for(int i=0;i<to_break.size();i++)
    {
        int v=to_break[i].first;
        int r=find_parent(parent,v);
        for(int j=1;j<=n;j++)
        {
            if(j!=v)
            {
                int s=find_parent(parent,j);
                if(r!=s)
                {
                    to_build.push_back(make_pair(v,j));
                    union_(parent,r,s);
                }
            }
        }
    }

    for(int i=0;i<to_break.size();i++)
    {
        cout<<to_break[i].first<<" "<<to_break[i].second<<" "<<to_build[i].first<<" "<<to_build[i].second<<endl;
    }

    }
    return 0;
}


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

int find_parent(vector<int> par,int i)         //if this doesn't work try out path compression and union by rank.
{
    if(i==par[i])
        return i;
    return find_parent(par,par[i]);
}

void union_(vector<int>& par,int x,int y)
{
    int i=find_parent(par,x);
    int j=find_parent(par,y);
    par[j]=i;
}

int main()
{
    int n;
    cin>>n;
    vector<pair<int,int> > G;
    int days=0;

    vector<int> parent(n+1,0);
    vector<pair<int,int> > to_break;
    for(int i=1;i<=n;i++)
        parent[i]=i;

    for(int i=1;i<=n-1;i++)
    {
        int a,b;
        cin>>a>>b;
        int x=find_parent(parent,a);
        int y=find_parent(parent,b);

        if(x!=y)
        {
            union_(parent,x,y);
            G.push_back(make_pair(a,b));
        }
        else
        {
            days++;
            to_break.push_back(make_pair(a,b));
        }
    }
//    cout<<endl;
    int sz=to_break.size();
    if(sz==0)
    {
        cout<<sz<<endl;
    }
    else{
            cout<<sz<<endl;
    for(int i=0;i<to_break.size();i++)
    {

        int r=find_parent(parent,to_break[i].first);
        for(int j=n;j>=1;j--)
        {
            int s=find_parent(parent,j);
            if(r!=s)
            {
                cout<<to_break[i].first<<" "<<to_break[i].second<<" "<<to_break[i].first<<" "<<j<<endl;
                union_(parent,r,s);
            }
        }

    }
    }
    return 0;
}


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

int find_parent(vector<int> par,int i)         //if this doesn't work try out path compression and union by rank.
{
    if(i==par[i])
        return i;
    par[i]=find_parent(par,par[i]);
    return par[i];
}

void union_(vector<int>& par,int x,int y)
{
    int i=find_parent(par,x);
    int j=find_parent(par,y);
    if(i<j)
    par[j]=i;
    else par[i]=j;
}

void dfs(vector<vector<int> > G,int root,vector<bool>& vis)
{
    vis[root]=true;
    for(int i=0;i<G[root].size();i++)
    {
        int v=G[root][i];
        dfs(G,v,vis);
    }

}

int main()
{
    int n;
    cin>>n;
    vector<vector<int> > G;
    G.resize(n+1);
    int days=0;

    vector<int> parent(n+1,0);
 //   vector<bool> vis(n+1,false);
    vector<pair<int,int> > to_break;
    vector<pair<int,int> > to_build;
    for(int i=1;i<=n;i++)
        parent[i]=i;
    int curr_parent;
    for(int i=1;i<=n-1;i++)
    {
        int a,b;
        cin>>a>>b;
        int x=find_parent(parent,a);
        int y=find_parent(parent,b);
        if(i==1)
            curr_parent=x;

        if(x!=y && (x==curr_parent || y==curr_parent))
        {
            union_(parent,x,y);
            G[a].push_back(b);
            G[b].push_back(a);
        }
        else if(x==y)
        {
            days++;
            to_break.push_back(make_pair(a,b));
        }
        else if(x!=y && (x!=curr_parent && y!=curr_parent))
        {
            if(to_break.size()>to_build.size()){
            to_build.push_back(make_pair(a,b));
            parent[b]=a;
            union_(parent,curr_parent,x);}
            else
                union_(parent,x,y);
            G[a].push_back(b);
            G[b].push_back(a);
        }

    }
//    cout<<"CURR PAR"<<curr_parent<<endl;
//    for(int i=1;i<=n;i++)
  //      cout<<parent[i]<<" ";
 //   cout<<endl;
    int sz=to_break.size();
    int diff=sz-to_build.size();
    int p=1;
    while(diff>0)
    {
        if(find_parent(parent,p)!=curr_parent)
        {
            to_build.push_back(make_pair(p,p));
            diff--;
        }
        p++;
    }
    if(sz==0)
    {
        cout<<sz<<endl;
    }
    else{
            cout<<sz<<endl;
            for(int i=0;i<sz;i++)
            {
                cout<<to_break[i].first<<" "<<to_break[i].second<<" "<<to_break[i].first<<" "<<to_build[i].first<<endl;
            }

    }
    return 0;
}


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

int find_parent(vector<int> par,int i)         //if this doesn't work try out path compression and union by rank.
{
    if(i==par[i])
        return i;
    par[i]=find_parent(par,par[i]);
    return par[i];
}

void union_(vector<int>& par,int x,int y)
{
    int i=find_parent(par,x);
    int j=find_parent(par,y);
    par[j]=i;
}

int main()
{
    int n;
    cin>>n;
    vector<pair<int,int> > G;
    int days=0;

    vector<int> parent(n+1,0);
    vector<pair<int,int> > to_break;
    for(int i=1;i<=n;i++)
        parent[i]=i;

    for(int i=1;i<=n-1;i++)
    {
        int a,b;
        cin>>a>>b;
        int x=find_parent(parent,a);
        int y=find_parent(parent,b);

        if(x!=y)
        {
            union_(parent,x,y);
            G.push_back(make_pair(a,b));
        }
        else
        {
            days++;
            to_break.push_back(make_pair(a,b));
        }
    }
//    cout<<endl;
    int sz=to_break.size();
    if(sz==0)
    {
        cout<<sz<<endl;
    }
    else{
            cout<<sz<<endl;
    for(int i=0;i<to_break.size();i++)
    {

        int r=find_parent(parent,to_break[i].first);
        for(int j=n;j>=1;j--)
        {
            int s=find_parent(parent,j);
            if(r!=s)
            {
                cout<<to_break[i].first<<" "<<to_break[i].second<<" "<<to_break[i].first<<" "<<j<<endl;
                union_(parent,r,s);
                break;
            }
        }
//        cout<<to_break[i].first<<" "<<to_break[i].second<<endl;

    }
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int par[1234];
int sz[1234];
int a[1234][2];
bool vis[1234];
vector<int >edj,cmp;
stack<int>stk;
void init(int n){
    for(int i=1;i<=n;i++)par[i]=i;
}
int find(int x){
    if(x==par[x])return x;
    return par[x]=find(par[x]);
}
bool comp(int x,int y){
    x=find(a[x][0]);
    y=find(a[y][0]);
    return (x>y);
}
bool join(int x,int y){
    x=find(x);
    y=find(y);
    if(x==y)return true;
    if(sz[x]>sz[y]){
        par[y]=x;sz[x]+=sz[y]+1;
    }
    else{
        par[x]=y;sz[y]+=sz[x]+1;
    }
    return false;
}
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n;cin>>n;
    init(n);
    int x,y;
    for(int i=1;i<n;i++){
        cin>>x>>y;
        a[i][0]=x;a[i][1]=y;
        if(join(x,y))edj.push_back(i);
    }
    for(int i=1;i<=n;i++){
        x=find(i);
        if(vis[x])continue;
        vis[x]=true;
        cmp.push_back(x);
    }
    int z=edj.size();
    for(int i=0;i<z;i++){
        x=edj[i];
        y=a[x][0];
        x=find(y);
        vis[x]=false;
    }
    int lst=-1,k;
    z=cmp.size();
    for(int i=0;i<z;i++){
        x=cmp[i];
        if(vis[x])stk.push(x);
    }
    z=edj.size();
    sort(edj.begin(),edj.end(),comp);
    cout<<z<<endl;
    for(int i=0;i<z;i++){
        x=a[edj[i]][0];
        y=find(x);
        x=edj[i];
        k=stk.top();stk.pop();
        if(y!=lst && lst!=-1)stk.push(y);
        lst=y;
        cout<<a[x][0]<<" "<<a[x][1]<<" "<<a[x][0]<<" "<<k<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int par[1234];
int sz[1234];
int a[1234][2];
bool vis[1234];
vector<int >edj,cmp;
stack<int>stk;
void init(int n){
    for(int i=1;i<=n;i++)par[i]=i;
}
int find(int x){
    if(x==par[x])return x;
    return par[x]=find(par[x]);
}
bool comp(int x,int y){
    x=find(a[x][0]);
    y=find(a[y][0]);
    return (x>y);
}
bool join(int x,int y){
    x=find(x);
    y=find(y);
    if(x==y)return true;
    if(sz[x]>sz[y]){
        par[y]=x;sz[x]+=sz[y]+1;
    }
    else{
        par[x]=y;sz[y]+=sz[x]+1;
    }
    return false;
}
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n;cin>>n;
    init(n);
    int x,y;
    for(int i=1;i<n;i++){
        cin>>x>>y;
        a[i][0]=x;a[i][1]=y;
        if(join(x,y))edj.push_back(i);
    }
    for(int i=1;i<=n;i++){
        x=find(i);
        if(vis[x])continue;
        vis[x]=true;
        cmp.push_back(x);
    }
    int z=edj.size();
    for(int i=0;i<z;i++){
        x=edj[i];
        y=a[x][0];
        x=find(y);
        vis[x]=false;
    }
    int lst=-1,k;
    z=cmp.size();
    for(int i=0;i<z;i++){
        x=cmp[i];
        if(vis[x]){
            stk.push(x);
//            if(n==39)cout<<x<<endl;
        }
    }
    z=edj.size();
    sort(edj.begin(),edj.end(),comp);
    cout<<z<<endl;
    for(int i=0;i<z;i++){
        x=a[edj[i]][0];
        y=find(x);
        x=edj[i];
        if(y!=lst){
            if(lst!=-1)stk.push(lst);
        }
        k=stk.top();stk.pop();
        lst=y;
        cout<<a[x][0]<<" "<<a[x][1]<<" "<<a[x][0]<<" "<<k;
//        if(n==39)cout<<" : "<<y;
        cout<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int par[1234];
int sz[1234];
int ans[1234];
int siz[1234];
int a[1234][2];
bool vis[1234];
vector<int >edj,cmp;
void init(int n){
    for(int i=1;i<=n;i++)par[i]=i;
}
int find(int x){
    if(x==par[x])return x;
    return par[x]=find(par[x]);
}
bool join(int x,int y){
    x=find(x);
    y=find(y);
    if(x==y)return true;
    if(sz[x]>sz[y]){
        par[y]=x;sz[x]+=sz[y]+1;
    }
    else{
        par[x]=y;sz[y]+=sz[x]+1;
    }
    return false;
}
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n;cin>>n;
    init(n);
    int x,y;
    for(int i=1;i<n;i++){
        cin>>x>>y;
        a[i][0]=x;a[i][1]=y;
        if(join(x,y))edj.push_back(i);
    }
    for(int i=1;i<=n;i++){
        x=find(i);
        if(n==39)cout<<x<<" ";
        if(vis[x])continue;
        vis[x]=true;
        cmp.push_back(x);
    }
    if(n==39)cout<<endl;
    int z=edj.size();
    for(int i=0;i<z;i++){
        x=edj[i];
        y=a[x][0];
        x=find(y);
        vis[x]=false;
    }
    int k=0;
    z=cmp.size();
    for(int i=0;i<z;i++){
        x=cmp[i];
        if(vis[x])ans[k++]=x;
    }
    z=edj.size();
    cout<<z<<endl;
    for(int i=0;i<z;i++){
        x=a[edj[i]][0];
        y=find(x);
        x=ans[siz[y]];
        siz[y]++;
        y=edj[i];
        cout<<a[y][0]<<" "<<a[y][1]<<" "<<a[y][0]<<" "<<x<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int par[1234];
int sz[1234];
int ans[1234];
int siz[1234];
int a[1234][2];
bool vis[1234];
vector<int >edj,cmp;
void init(int n){
    for(int i=1;i<=n;i++)par[i]=i;
}
int find(int x){
    if(x==par[x])return x;
    return par[x]=find(par[x]);
}
bool join(int x,int y){
    x=find(x);
    y=find(y);
    if(x==y)return true;
    if(sz[x]>sz[y]){
        par[y]=x;sz[x]+=sz[y]+1;
    }
    else{
        par[x]=y;sz[y]+=sz[x]+1;
    }
    return false;
}
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n;cin>>n;
    init(n);
    int x,y;
    for(int i=1;i<n;i++){
        cin>>x>>y;
        a[i][0]=x;a[i][1]=y;
        if(join(x,y))edj.push_back(i);
    }
    for(int i=1;i<=n;i++){
        x=find(i);
        if(vis[x])continue;
        vis[x]=true;
        cmp.push_back(x);
    }
    int z=edj.size();
    for(int i=0;i<z;i++){
        x=edj[i];
        y=a[x][0];
        x=find(y);
        vis[x]=false;
    }
    int k=0;
    z=cmp.size();
    for(int i=0;i<z;i++){
        x=cmp[i];
        if(vis[x])ans[k++]=x;
    }
    z=edj.size();
    cout<<z<<endl;
    for(int i=0;i<z;i++){
        x=a[edj[i]][0];
        y=find(x);
        x=ans[siz[y]];
        siz[y]++;
        y=edj[i];
        cout<<a[y][0]<<" "<<a[y][1]<<" "<<a[y][0]<<" "<<x<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int par[1234];
int sz[1234];
int a[1234][2];
bool vis[1234];
vector<int >edj,cmp;
stack<int>stk;
void init(int n){
    for(int i=1;i<=n;i++)par[i]=i;
}
int find(int x){
    if(x==par[x])return x;
    return par[x]=find(par[x]);
}
bool comp(int x,int y){
    x=find(a[x][0]);
    y=find(a[y][0]);
    return (x>y);
}
bool join(int x,int y){
    x=find(x);
    y=find(y);
    if(x==y)return true;
    if(sz[x]>sz[y]){
        par[y]=x;sz[x]+=sz[y]+1;
    }
    else{
        par[x]=y;sz[y]+=sz[x]+1;
    }
    return false;
}
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n;cin>>n;
    init(n);
    int x,y;
    for(int i=1;i<n;i++){
        cin>>x>>y;
        a[i][0]=x;a[i][1]=y;
        if(join(x,y))edj.push_back(i);
    }
    for(int i=1;i<=n;i++){
        x=find(i);
        if(vis[x])continue;
        vis[x]=true;
        cmp.push_back(x);
    }
    int z=edj.size();
    for(int i=0;i<z;i++){
        x=edj[i];
        y=a[x][0];
        x=find(y);
        vis[x]=false;
    }
    int lst=-1,k;
    z=cmp.size();
    for(int i=0;i<z;i++){
        x=cmp[i];
        if(vis[x]){
            stk.push(x);
            if(n==39)cout<<x<<endl;
        }
    }
    z=edj.size();
    sort(edj.begin(),edj.end(),comp);
    cout<<z<<endl;
    for(int i=0;i<z;i++){
        x=a[edj[i]][0];
        y=find(x);
        x=edj[i];
        k=stk.top();stk.pop();
        if(y!=lst && lst!=-1)stk.push(y);
        lst=y;
        cout<<a[x][0]<<" "<<a[x][1]<<" "<<a[x][0]<<" "<<k;
        if(n==39)cout<<" : "<<y;
        cout<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int par[1234];
int sz[1234];
int a[1234][2];
bool vis[1234];
vector<int >edj,cmp;
stack<int>stk;
void init(int n){
    for(int i=1;i<=n;i++)par[i]=i;
}
int find(int x){
    if(x==par[x])return x;
    return par[x]=find(par[x]);
}
bool comp(int x,int y){
    x=find(a[x][0]);
    y=find(a[y][0]);
    return (x>y);
}
bool join(int x,int y){
    x=find(x);
    y=find(y);
    if(x==y)return true;
    if(sz[x]>sz[y]){
        par[y]=x;sz[x]+=sz[y]+1;
    }
    else{
        par[x]=y;sz[y]+=sz[x]+1;
    }
    return false;
}
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n;cin>>n;
    init(n);
    int x,y;
    for(int i=1;i<n;i++){
        cin>>x>>y;
        a[i][0]=x;a[i][1]=y;
        if(join(x,y))edj.push_back(i);
    }
    for(int i=1;i<=n;i++){
        x=find(i);
        if(vis[x])continue;
        vis[x]=true;
        cmp.push_back(x);
    }
    int z=edj.size();
    for(int i=0;i<z;i++){
        x=edj[i];
        y=a[x][0];
        x=find(y);
        vis[x]=false;
    }
    int lst=-1,k;
    z=cmp.size();
    for(int i=0;i<z;i++){
        x=cmp[i];
        if(vis[x])stk.push(x);
    }
    z=edj.size();
    sort(edj.begin(),edj.end(),comp);
    cout<<z<<endl;
    for(int i=0;i<z;i++){
        x=a[edj[i]][0];
        y=find(x);
        k=stk.top();stk.pop();
        if(y!=lst && lst!=-1)stk.push(y);
        lst=y;
        cout<<a[x][0]<<" "<<a[x][1]<<" "<<a[x][0]<<" "<<k<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int par[1234];
int sz[1234];
int a[1234][2];
bool vis[1234];
vector<int >edj,cmp;
stack<int>stk;
void init(int n){
    for(int i=1;i<=n;i++)par[i]=i;
}
int find(int x){
    if(x==par[x])return x;
    return par[x]=find(par[x]);
}
bool comp(int x,int y){
    x=find(a[x][0]);
    y=find(a[y][0]);
    return (x>y);
}
bool join(int x,int y){
    x=find(x);
    y=find(y);
    if(x==y)return true;
    if(sz[x]>sz[y]){
        par[y]=x;sz[x]+=sz[y]+1;
    }
    else{
        par[x]=y;sz[y]+=sz[x]+1;
    }
    return false;
}
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n;cin>>n;
    init(n);
    int x,y;
    for(int i=1;i<n;i++){
        cin>>x>>y;
        a[i][0]=x;a[i][1]=y;
        if(join(x,y))edj.push_back(i);
    }
    for(int i=1;i<=n;i++){
        x=find(i);
        if(vis[x])continue;
        vis[x]=true;
        cmp.push_back(x);
    }
    int z=edj.size();
    for(int i=0;i<z;i++){
        x=edj[i];
        y=a[x][0];
        x=find(y);
        vis[x]=false;
    }
    int lst=-1,k;
    z=cmp.size();
    for(int i=0;i<z;i++){
        x=cmp[i];
        if(vis[x]){
            stk.push(x);
            if(n==39)cout<<x<<endl;
        }
    }
    z=edj.size();
    sort(edj.begin(),edj.end(),comp);
    cout<<z<<endl;
    for(int i=0;i<z;i++){
        x=a[edj[i]][0];
        y=find(x);
        x=edj[i];
        k=stk.top();stk.pop();
        if(y!=lst){
            if(lst!=-1)stk.push(lst);
        }
        lst=y;
        cout<<a[x][0]<<" "<<a[x][1]<<" "<<a[x][0]<<" "<<k;
        if(n==39)cout<<" : "<<y;
        cout<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int par[1234];
int sz[1234];
int a[1234][2];
bool vis[1234];
vector<int >edj,cmp;
stack<int>stk;
void init(int n){
    for(int i=1;i<=n;i++)par[i]=i;
}
int find(int x){
    if(x==par[x])return x;
    return par[x]=find(par[x]);
}
bool comp(int x,int y){
    x=find(a[x][0]);
    y=find(a[y][0]);
    return (x>y);
}
bool join(int x,int y){
    x=find(x);
    y=find(y);
    if(x==y)return true;
    if(sz[x]>sz[y]){
        par[y]=x;sz[x]+=sz[y]+1;
    }
    else{
        par[x]=y;sz[y]+=sz[x]+1;
    }
    return false;
}
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n;cin>>n;
    init(n);
    int x,y;
    for(int i=1;i<n;i++){
        cin>>x>>y;
        a[i][0]=x;a[i][1]=y;
        if(join(x,y))edj.push_back(i);
    }
    for(int i=1;i<=n;i++){
        x=find(i);
        if(vis[x])continue;
        vis[x]=true;
        cmp.push_back(x);
    }
    int z=edj.size();
    for(int i=0;i<z;i++){
        x=edj[i];
        y=a[x][0];
        x=find(y);
        vis[x]=false;
    }
    int lst=-1,k;
    z=cmp.size();
    for(int i=0;i<z;i++){
        x=cmp[i];
        if(vis[x]){
            stk.push(x);
//            if(n==39)cout<<x<<endl;
        }
    }
    z=edj.size();
    sort(edj.begin(),edj.end(),comp);
    cout<<z<<endl;
    for(int i=0;i<z;i++){
        x=a[edj[i]][0];
        y=find(x);
        x=edj[i];
        k=stk.top();stk.pop();
        if(y!=lst){
            if(lst!=-1)stk.push(lst);
        }
        lst=y;
        cout<<a[x][0]<<" "<<a[x][1]<<" "<<a[x][0]<<" "<<k;
//        if(n==39)cout<<" : "<<y;
        cout<<endl;
    }
    return 0;
}


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

vector<int> data(100001);
vector<pair<int,int>> extra_edges;
void init(int n){
    for(int i = 1;i<=n;++i)
        data[i] = i;
}
int make_set(int a){
    if(data[a]==a)
        return a;
    return data[a] = make_set(data[a]);
}
int join_set(int a,int b){
    int a1=a,b1 = b;
    a = make_set(a);
    b=  make_set(b);
    if(a!=b){
        data[b] = a;
    }
    else{
        extra_edges.push_back({a1,b1});
    }
}

int main(){
    int n;
    cin>>n;
    init(n);
    for(int i = 0;i<n-1;++i){
        int a,b;
        cin>>a>>b;
        join_set(a,b);
    }
    set<int> distinct;
    for(int i = 1;i<=n;++i)
        distinct.insert(make_set(i));
    cout<<distinct.size()-1<<"\n";
    for(int i = 0;i<=(int)extra_edges.size()-1;++i){
        int e1 = extra_edges[i].first;
        int e2 = extra_edges[i].second;
        distinct.erase(data[e1]);
        cout<<e1<<" "<<e2<<" "<<e1<<" "<<*distinct.begin()<<"\n";
    }
}


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

vector<int> data(100001);
vector<pair<int,int>> extra_edges;
void init(int n){
    for(int i = 1;i<=n;++i)
        data[i] = i;
}
int make_set(int a){
    if(data[a]==a)
        return a;
    return data[a] = make_set(data[a]);
}
int join_set(int a,int b){
    int a1=a,b1 = b;
    a = make_set(a);
    b=  make_set(b);
    if(a!=b){
        data[b] = a;
    }
    else{
        extra_edges.push_back({a1,b1});
    }
}

int main(){
    int n;
    cin>>n;
    init(n);
    for(int i = 0;i<n-1;++i){
        int a,b;
        cin>>a>>b;
        join_set(a,b);
    }
    set<int> distinct;
    for(int i = 1;i<=n;++i)
        distinct.insert(make_set(i));
    cout<<distinct.size()-1<<"\n";
    for(int i = 0;i<=(int)extra_edges.size()-1;++i){
        int e1 = extra_edges[i].first;
        int e2 = extra_edges[i].second;
        distinct.erase(data[e1]);
        cout<<e1<<" "<<e2<<" "<<e1<<" "<<*distinct.begin()<<"\n";
        distinct.erase(distinct.begin());
    }
}


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

vector<int> data(100001);
vector<pair<int,int>> extra_edges;
void init(int n){
    for(int i = 1;i<=n;++i)
        data[i] = i;
}
int make_set(int a){
    if(data[a]==a)
        return a;
    return data[a] = make_set(data[a]);
}
int join_set(int a,int b){
    int a1=a,b1 = b;
    a = make_set(a);
    b=  make_set(b);
    if(a!=b){
        data[b] = a;
    }
    else{
        extra_edges.push_back({a1,b1});
    }
}

int main(){
    int n;
    cin>>n;
    init(n);
    for(int i = 0;i<n-1;++i){
        int a,b;
        cin>>a>>b;
        join_set(a,b);
    }
    set<int> distinct;
    for(int i = 1;i<=n;++i)
        distinct.insert(data[i]);
    cout<<distinct.size()-1<<"\n";
    for(int i = 0;i<=(int)extra_edges.size()-1;++i){
        int e1 = extra_edges[i].first;
        int e2 = extra_edges[i].second;
        distinct.erase(data[e1]);
        cout<<e1<<" "<<e2<<" "<<e1<<" "<<*distinct.begin()<<"\n";
    }
}


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

vector<int> data(1001);
vector<int> size(1001);

vector<pair<int,int>> extra_edges;
void init(int n){
    for(int i = 1;i<=n;++i) {
        data[i] = i;
        size[i] = 1;
    }
}
int make_set(int a){
    if(data[a]==a)
        return a;
    return data[a] = make_set(data[a]);
}
int join_set(int a,int b){
    int a1=a,b1 = b;
    a = make_set(a);
    b=  make_set(b);
    if(a!=b){
        if(size[a]<size[b])
            swap(a,b);
        data[b] = a;
        size[a] += size[b];
    }
    else{
        extra_edges.push_back({a1,b1});
    }
}

int main(){
    int n;
    cin>>n;
    init(n);
    for(int i = 0;i<n-1;++i){
        int a,b;
        cin>>a>>b;
        join_set(a,b);
    }
    cout<<extra_edges.size()<<"\n";
    int p= make_set(1);
    for(int i = 1;i<=n;++i){
        if(make_set(i)!=p){
            cout<<extra_edges.back().first<<" "<<extra_edges.back().second<<" "<<p<<" "<<i<<"\n";
            data[i] = p;
            extra_edges.pop_back();
        }
    }
}


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

vector<int> data(1001);
vector<int> size(1001);

vector<pair<int,int>> extra_edges;
void init(int n){
    for(int i = 1;i<=n;++i) {
        data[i] = i;
        size[i] = 1;
    }
}
int make_set(int a){
    if(data[a]==a)
        return a;
    return data[a] = make_set(data[a]);
}
int join_set(int a,int b){
    int a1=a,b1 = b;
    a = make_set(a);
    b=  make_set(b);
    if(a!=b){
        if(size[a]<size[b])
            swap(a,b);
        data[b] = a;
        size[a] += size[b];
    }
    else{
        extra_edges.push_back({a1,b1});
    }
}

int main(){
    int n;
    cin>>n;
    init(n);
    for(int i = 0;i<n-1;++i){
        int a,b;
        cin>>a>>b;
        join_set(a,b);
    }
    set<int> distinct;
    for(int i = 1;i<=n;++i)
        distinct.insert(make_set(i));
    cout<<distinct.size()-1<<"\n";
    for(int i = 0;i<=(int)extra_edges.size()-1;++i){
        int e1 = extra_edges[i].first;
        int e2 = extra_edges[i].second;
        distinct.erase(data[e1]);
        cout<<e1<<" "<<e2<<" "<<e1<<" "<<*distinct.begin()<<"\n";
        distinct.erase(distinct.begin());
    }
}


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

vector<int> data(1001);
vector<int> size(1001);

vector<pair<int,int>> extra_edges;
void init(int n){
    for(int i = 1;i<=n;++i) {
        data[i] = i;
        size[i] = 1;
    }
}
int make_set(int a){
    if(data[a]==a)
        return a;
    return data[a] = make_set(data[a]);
}
int join_set(int a,int b){
    int a1=a,b1 = b;
    a = make_set(a);
    b=  make_set(b);
    if(a!=b){
        if(size[a]<size[b])
            swap(a,b);
        data[b] = a;
        size[a] += size[b];
    }
    else{
        extra_edges.push_back({a1,b1});
    }
}

int main(){
    int n;
    cin>>n;
    init(n);
    for(int i = 0;i<n-1;++i){
        int a,b;
        cin>>a>>b;
        join_set(a,b);
    }
    set<int> distinct;
    for(int i = 1;i<=n;++i)
        distinct.insert(data[i]);
    cout<<distinct.size()-1<<"\n";
    for(int i = 0;i<=(int)extra_edges.size()-1;++i){
        int e1 = extra_edges[i].first;
        int e2 = extra_edges[i].second;
        distinct.erase(data[e1]);
        cout<<e1<<" "<<e2<<" "<<e1<<" "<<*distinct.begin()<<"\n";
        distinct.erase(distinct.begin());
    }
}


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

vector<int> data(1001);
vector<int> size(1001);

vector<pair<int,int>> extra_edges;
void init(int n){
    for(int i = 1;i<=n;++i) {
        data[i] = i;
        size[i] = 1;
    }
}
int make_set(int a){
    if(data[a]==a)
        return a;
    return data[a] = make_set(data[a]);
}
int join_set(int a,int b){
    int a1=a,b1 = b;
    a = make_set(a);
    b=  make_set(b);
    if(a!=b){
        if(size[a]<size[b])
            swap(a,b);
        data[b] = a;
        size[a] += size[b];
    }
    else{
        extra_edges.push_back({a1,b1});
    }
}

int main(){
    int n;
    cin>>n;
    init(n);
    for(int i = 0;i<n-1;++i){
        int a,b;
        cin>>a>>b;
        join_set(a,b);
    }
    cout<<extra_edges.size()<<"\n";
    int p= make_set(1);
    for(int i = 1;i<=n;++i){
        if(make_set(i)!=p){
            cout<<extra_edges.back().first<<" "<<extra_edges.back().second<<" "<<p<<" "<<i<<"\n";
            data[make_set(i)] = p;
            extra_edges.pop_back();
        }
    }
}


//In The Name Of Allah//
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <climits>
#include <string>
#include <vector>
#include <queue>
#include <cmath>
#include <map>
#include <set>

using namespace std;

const long long int mod=1e9+7;
const int maxn=1e5+5*1e4;
const int Base=199;
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define Right(x) (x*2+1)
#define Left(x) (2*x)

typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef vector<long long int> vll;
typedef pair<long long int,long long int> pll;

const int MAXN=1e3+100;
int n,E1,E2;
vi adj[MAXN],Comps;
set<int> Vertex;
bool mark[MAXN];
set < pii > Edges,Edges1;
void input()
{
	cin>>n;
	for(int i=1;i<=n-1;i++)
	{
		Vertex.insert(i);
		cin>>E1>>E2;
		adj[E1].pb(E2);
		adj[E2].pb(E1);
		Edges.insert(mp(E1,E2));
	}
	Vertex.insert(n);
}

void DFS(int V, int parent)
{
	mark[V]=1;
	Vertex.erase(V);
	for(int i=0;i<adj[V].size();i++)
	{
		if(mark[adj[V][i]]==0 && adj[V][i]!=0)
			DFS(adj[V][i],V);
		else
			if(adj[V][i]!=parent)
			{
				if(Edges.find(mp(V,adj[V][i]))!=Edges.end())
				{
					Edges.erase(mp(V,adj[V][i]));
					Edges1.insert(mp(V,adj[V][i]));
				}
				if(Edges.find(mp(adj[V][i],V))!=Edges.end())
				{
					Edges.erase(mp(adj[V][i],V));
					Edges1.insert(mp(adj[V][i],V));
				}
			}
	}
}

void Find_components()
{
	while(!Vertex.empty())
	{
		Comps.pb(*Vertex.begin());
		DFS(*Vertex.begin(),0);
	}
	if(Comps.size()==1)
	{
		cout<<0;
		return;
	}
	cout<<Comps.size()-1<<endl;
	for(int i=0;i<Comps.size()-1;i++)
	{
		pii edge=*Edges1.begin();
		Edges1.erase(Edges1.begin());
		cout<<edge.F<<" "<<edge.S<<" "<<Comps[i]<<" "<<Comps[i+1]<<endl;
	}
}
int32_t main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);

	input();
	Find_components();

	return 0;
}

//I will succeed,
//Maybe not immediately,
//BUT DEFIANTLY.


//In The Name Of Allah//
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <climits>
#include <string>
#include <vector>
#include <queue>
#include <cmath>
#include <map>
#include <set>

using namespace std;

const long long int mod=1e9+7;
const int maxn=1e5+5*1e4;
const int Base=199;
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define Right(x) (x*2+1)
#define Left(x) (2*x)

typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef vector<long long int> vll;
typedef pair<long long int,long long int> pll;

const int MAXN=1e3+100;
int n,E1,E2;
vi adj[MAXN],Comps;
set<int> Vertex;
bool mark[MAXN];

void input()
{
	cin>>n;
	for(int i=1;i<=n-1;i++)
	{
		Vertex.insert(i);
		cin>>E1>>E2;
		adj[E1].pb(E2);
		adj[E2].pb(E1);
	}
	Vertex.insert(n);
}

void DFS(int V)
{
	mark[V]=1;
	Vertex.erase(V);
	for(int i=0;i<adj[V].size();i++)
		if(mark[adj[V][i]]==0)
			DFS(adj[V][i]);
}

void Find_components()
{
	while(!Vertex.empty())
	{
		Comps.pb(*Vertex.begin());
		DFS(*Vertex.begin());
	}
	if(Comps.size()==1)
	{
		cout<<0;
		return;
	}
	cout<<Comps.size()-1<<endl;
	for(int i=0;i<Comps.size()-1;i++)
	{
		cout<<E1<<" "<<E2<<" "<<Comps[i]<<" "<<Comps[i+1]<<endl;
	}
}
int32_t main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);

	input();
	Find_components();

	return 0;
}

//I will succeed,
//Maybe not immediately,
//BUT DEFIANTLY.


//In The Name Of Allah//
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <climits>
#include <string>
#include <vector>
#include <queue>
#include <cmath>
#include <map>
#include <set>

using namespace std;

const long long int mod=1e9+7;
const int maxn=1e5+5*1e4;
const int Base=199;
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define Right(x) (x*2+1)
#define Left(x) (2*x)

typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef vector<long long int> vll;
typedef pair<long long int,long long int> pll;

const int MAXN=1e3+100;
int n,E1,E2;
vi adj[MAXN],Comps;
set<int> Vertex;
bool mark[MAXN];

void input()
{
	cin>>n;
	for(int i=1;i<=n-1;i++)
	{
		Vertex.insert(i);
		cin>>E1>>E2;
		adj[E1].pb(E2);
		adj[E2].pb(E1);
	}
	Vertex.insert(n);
}

void DFS(int V)
{
	mark[V]=1;
	Vertex.erase(V);
	for(int i=0;i<adj[V].size();i++)
		if(mark[adj[V][i]]==0)
			DFS(adj[V][i]);
}

void Find_components()
{
	while(!Vertex.empty())
	{
		Comps.pb(*Vertex.begin());
		DFS(*Vertex.begin());
	}
	cout<<(Comps.size()*(Comps.size()-1))/2<<endl;
	for(int i=0;i<Comps.size()-1;i++)
	{
		cout<<Comps[i]<<" "<<adj[Comps[i]][0]<<" "<<Comps[i]<<" "<<Comps[i+1]<<endl;
	}
}
int32_t main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);

	input();
	Find_components();

	return 0;
}

//I will succeed,
//Maybe not immediately,
//BUT DEFIANTLY.


//In The Name Of Allah//
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <climits>
#include <string>
#include <vector>
#include <queue>
#include <cmath>
#include <map>
#include <set>

using namespace std;

const long long int mod=1e9+7;
const int maxn=1e5+5*1e4;
const int Base=199;
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define Right(x) (x*2+1)
#define Left(x) (2*x)

typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef vector<long long int> vll;
typedef pair<long long int,long long int> pll;

const int MAXN=1e3+100;
int n,E1,E2;
vi adj[MAXN],Comps;
set<int> Vertex;
bool mark[MAXN];

void input()
{
	cin>>n;
	for(int i=1;i<=n-1;i++)
	{
		Vertex.insert(i);
		cin>>E1>>E2;
		adj[E1].pb(E2);
		adj[E2].pb(E1);
	}
	Vertex.insert(n);
}

void DFS(int V)
{
	mark[V]=1;
	Vertex.erase(V);
	for(int i=0;i<adj[V].size();i++)
		if(mark[adj[V][i]]==0)
			DFS(adj[V][i]);
}

void Find_components()
{
	while(!Vertex.empty())
	{
		Comps.pb(*Vertex.begin());
		DFS(*Vertex.begin());
	}
	cout<<(Comps.size()*(Comps.size()-1))/2<<endl;
	for(int i=0;i<Comps.size();i++)
		for(int j=i+1;j<Comps.size();j++)
			cout<<E1<<" "<<E2<<" "<<Comps[i]<<" "<<Comps[j]<<endl;
}
int32_t main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);

	input();
	Find_components();

	return 0;
}

//I will succeed,
//Maybe not immediately,
//BUT DEFIANTLY.


#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define LL long long
#define PB push_back
#define POP pop_back()
#define PII pair<int,int>
#define FI first
#define SE second
#define ULL unsigned long long
#define endl '\n'
using namespace std;
const int INF=0x3f3f3f3f;
const double pi=acos(-1),eps=1e-8;
const int maxn=1<<17;
const int N=200+10,M=635,mod=1e9+7;
int n;
int cnt[N];
int c[N];
struct node{
    int k;
    int a[N];
}b[N];
int p[N];
int flag;
int vis[N];
int vs[N];
int C[N];
bool cnm(int now){
    //cout<<"NOW="<<now<<endl;
    if(now==1)return 1;
    int st=0,ed=0;
    for(int i=1;i<=n;i++){
        if(vis[i])continue;
        //cout<<i<<' '<<c[i]<<endl;
        if(c[i]==1){
            if(st)ed=i;
            else st=i;
        }
    }
    //cout<<now<<' '<<st<<' '<<ed<<endl;
    if(ed){
        if(p[1])return 0;
        vis[st]=vis[ed]=1;
        int pos=0;
        for(int i=1;i<n;i++){
            if(vs[i])continue;
            for(int j=1;j<=b[i].k;j++){
                if(b[i].a[j]==st){
                    pos=i;break;
                }
            }
            if(pos){
                for(int j=1;j<=b[i].k;j++){
                    c[b[i].a[j]]--;
                }
                break;
            }
        }
        vs[pos]=1;
        p[now]=st;
        p[1]=ed;
        if(cnm(now-1)){
            return 1;
        }
        vs[pos]=0;
        for(int j=1;j<=b[pos].k;j++){
            c[b[pos].a[j]]++;
        }
        swap(p[now],p[1]);
        swap(st,ed);
        pos=0;
        for(int i=1;i<n;i++){
            if(vs[i])continue;
            for(int j=1;j<=b[i].k;j++){
                if(b[i].a[j]==st){
                    pos=i;break;
                }
            }
            if(pos){
                for(int j=1;j<=b[i].k;j++){
                    c[b[i].a[j]]--;
                }
                break;
            }
        }
        vs[pos]=1;
        return cnm(now-1);
    }
    else{
        vis[st]=1;
        p[now]=st;
        int pos=0;
        for(int i=1;i<n;i++){
            if(vs[i])continue;
            for(int j=1;j<=b[i].k;j++){
                if(b[i].a[j]==st){
                    pos=i;break;
                }
            }
            if(pos){
                for(int j=1;j<=b[i].k;j++){
                    c[b[i].a[j]]--;
                }
                break;
            }
        }
        vs[pos]=1;
        return cnm(now-1);
    }
}
void solve(){
    cin>>n;
    for(int i=1;i<=n;i++)c[i]=0,C[i]=0,vis[i]=vs[i]=0,p[i]=0;
    for(int i=1;i<n;i++){
        scanf("%d",&b[i].k);
        for(int j=1;j<=b[i].k;j++){
            scanf("%d",&b[i].a[j]);
            c[b[i].a[j]]++;
            C[b[i].a[j]]++;
        }
    }
    p[n+1]=0;
    cnm(n);

    if(C[p[1]]>C[p[2]])swap(p[1],p[2]);
    for(int i=1;i<=n;i++)printf("%d ",p[i]);
    printf("\n");
}
int main()
{
    int t;
    cin>>t;
    while(t--){
        solve();
    }
    return 0;
}
/*
1
5
2 4 5
3 1 4 5
3 1 3 5
5 1 2 3 4 5
*/


#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define LL long long
#define PB push_back
#define POP pop_back()
#define PII pair<int,int>
#define FI first
#define SE second
#define ULL unsigned long long
#define endl '\n'
using namespace std;
const int INF=0x3f3f3f3f;
const double pi=acos(-1),eps=1e-8;
const int maxn=1<<17;
const int N=200+10,M=635,mod=1e9+7;
int n;
int c[N];
struct node{
    int k;
    int a[N];
}b[N];
int p[N];
int vis[N];
int vs[N];
int C[N];
int e[N][N];
int mx[N];
bool cnm(int now){
    //cout<<"NOW="<<now<<endl;
    if(now==1)return C[p[1]]<=C[p[2]];
    int st=0,ed=0;
    for(int i=1;i<=n;i++){
        if(vis[i])continue;
        //cout<<i<<' '<<c[i]<<endl;
        if(c[i]==1){
            if(st)ed=i;
            else st=i;
        }
    }
    if(!st)return 0;
    //cout<<now<<' '<<st<<' '<<ed<<endl;
    if(ed){
        if(p[1])return 0;
        vis[st]=vis[ed]=1;
        if(mx[st]<mx[ed])swap(st,ed);
        int pos=0;
        for(int i=1;i<n;i++){
            if(vs[i])continue;
            for(int j=1;j<=b[i].k;j++){
                if(b[i].a[j]==st){
                    pos=i;break;
                }
            }
            if(pos){
                for(int j=1;j<=b[i].k;j++){
                    c[b[i].a[j]]--;
                    mx[b[i].a[j]]+=now;
                }
                break;
            }
        }
        vs[pos]=1;
        p[now]=st;
        p[1]=ed;
        if(cnm(now-1)&&(e[st][p[now+1]]||now==n)){
            vs[pos]=0;
            for(int j=1;j<=b[pos].k;j++){
                c[b[pos].a[j]]++;
                mx[b[pos].a[j]]-=now;
            }
            vis[st]=0,vis[ed]=0;
            return 1;
        }
        //cout<<"NOWCODER "<<now<<endl;
        vs[pos]=0;
        for(int j=1;j<=b[pos].k;j++){
            c[b[pos].a[j]]++;
        }
        swap(p[now],p[1]);
        swap(st,ed);
        pos=0;
        for(int i=1;i<n;i++){
            if(vs[i])continue;
            for(int j=1;j<=b[i].k;j++){
                if(b[i].a[j]==st){
                    pos=i;break;
                }
            }
            if(pos){
                for(int j=1;j<=b[i].k;j++){
                    c[b[i].a[j]]--;
                    mx[b[i].a[j]]+=now;
                }
                break;
            }
        }
        vs[pos]=1;
        int re=cnm(now-1);
        for(int j=1;j<=b[pos].k;j++){
            c[b[pos].a[j]]++;
            mx[b[pos].a[j]]-=now;
        }
        vs[pos]=0;
        vis[st]=vis[ed]=0;
        return re;
    }
    else{
        vis[st]=1;
        p[now]=st;
        int pos=0;
        for(int i=1;i<n;i++){
            if(vs[i])continue;
            for(int j=1;j<=b[i].k;j++){
                if(b[i].a[j]==st){
                    pos=i;break;
                }
            }
            if(pos){
                for(int j=1;j<=b[i].k;j++){
                    c[b[i].a[j]]--;
                    mx[b[i].a[j]]+=now;
                }
                break;
            }
        }
        vs[pos]=1;
        int re=cnm(now-1);
        for(int j=1;j<=b[pos].k;j++){
            c[b[pos].a[j]]++;
            mx[b[pos].a[j]]-=now;
        }
        vis[st]=0;
        vs[pos]=0;
        return re;
    }
}
void solve(){
    cin>>n;
    for(int i=1;i<=n;i++)c[i]=0,C[i]=0,vis[i]=vs[i]=0,p[i]=0;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            e[i][j]=0;
        }
    }
    for(int i=1;i<n;i++){
        scanf("%d",&b[i].k);
        for(int j=1;j<=b[i].k;j++){
            scanf("%d",&b[i].a[j]);
            for(int q=1;q<j;q++){
                e[b[i].a[q]][b[i].a[j]]=e[b[i].a[j]][b[i].a[q]]=1;
            }
            c[b[i].a[j]]++;
            C[b[i].a[j]]++;
        }
    }
    p[n+1]=0;
    cnm(n);
    for(int i=1;i<=n;i++)printf("%d ",p[i]);
    printf("\n");
}
int main()
{
    int t;
    cin>>t;
    while(t--){
        solve();
    }
    return 0;
}
/*
1
5
2 2 3
2 2 4
2 1 4
3 1 4 5
*/


#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define LL long long
#define PB push_back
#define POP pop_back()
#define PII pair<int,int>
#define FI first
#define SE second
#define ULL unsigned long long
#define endl '\n'
using namespace std;
const int INF=0x3f3f3f3f;
const double pi=acos(-1),eps=1e-8;
const int maxn=1<<17;
const int N=100+10,M=635,mod=1e9+7;
int n;
int cnt[N];
int c[N];
struct node{
    int k;
    int a[N];
}b[N];
int p[N];
int flag;
int vis[N];
bool vs[N];
bool check(int st,int ed){
    p[n]=st;
    for(int i=1;i<=n;i++)vis[i]=0,vs[i]=0;
    vis[st]=1;
    vis[ed]=1;
    for(int i=1;i<=n;i++){
        cnt[i]=c[i];
    }
    for(int i=1;i<n;i++){
        int flag=0;
        for(int j=1;j<=b[i].k;j++){
            if(b[i].a[j]==st){
                flag=1;break;
            }
        }
        if(flag){
            vs[i]=1;
            //cout<<i<<endl;
            for(int j=1;j<=b[i].k;j++){
                //cout<<b[i].a[j]<<' '<<cnt[b[i].a[j]]<<endl;
                cnt[b[i].a[j]]--;
            }
        }
    }
    for(int i=n-1;i>1;i--){
        int ans=0;
        for(int j=1;j<=n;j++){
            //cout<<"wcynb "<<i<<' '<<j<<' '<<cnt[j]<<endl;
            if(cnt[j]==1&&j!=st&&j!=ed){
                ans=j;break;
            }
        }
        //cout<<ans<<endl;
        if(ans==0){
            //cout<<i<<' '<<st<<' '<<ed<<endl;
            return 0;
        }
        p[i]=ans;
        vis[ans]=1;
        for(int j=1;j<n;j++){
            if(vs[j])continue;
            int flag=0;
            for(int q=1;q<=b[j].k;q++){
                if(b[j].a[q]==ans){
                    flag=1;break;
                }
            }
            if(flag){
                vs[j]=1;
                for(int q=1;q<=b[j].k;q++){
                    cnt[b[j].a[q]]--;
                }
                break;
            }
        }
    }
    p[1]=ed;
    if(ed==0){
        for(int i=1;i<=n;i++){
            if(vis[i]==0){
                p[1]=i;break;
            }
        }
    }
    for(int i=1;i<=n;i++)printf("%d ",p[i]);
    printf("\n");
    return 1;
}
void solve(){
    cin>>n;
    for(int i=1;i<=n;i++)c[i]=0;
    for(int i=1;i<n;i++){
        scanf("%d",&b[i].k);
        for(int j=1;j<=b[i].k;j++){
            scanf("%d",&b[i].a[j]);
            c[b[i].a[j]]++;
        }
    }
    flag=0;
    int st1=0,st2=0;
    for(int i=1;i<=n;i++){
        if(c[i]==1){
            if(st1)st2=i;
            else st1=i;
        }
    }
    if(!check(st1,st2)){
        check(st2,st1);
    }

}
int main()
{
    int t;
    cin>>t;
    while(t--){
        solve();
    }
    return 0;
}
/*
1
5
2 2 3
2 1 2
2 1 4
2 4 5
*/


#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define LL long long
#define PB push_back
#define POP pop_back()
#define PII pair<int,int>
#define FI first
#define SE second
#define ULL unsigned long long
#define endl '\n'
using namespace std;
const int INF=0x3f3f3f3f;
const double pi=acos(-1),eps=1e-8;
const int maxn=1<<17;
const int N=200+10,M=635,mod=1e9+7;
int n;
int c[N];
struct node{
    int k;
    int a[N];
}b[N];
int p[N];
int vis[N];
int vs[N];
int C[N];
bool cnm(int now){
    //cout<<"NOW="<<now<<endl;
    if(now==1)return C[p[1]]<=C[p[2]];
    int st=0,ed=0;
    for(int i=1;i<=n;i++){
        if(vis[i])continue;
        //cout<<i<<' '<<c[i]<<endl;
        if(c[i]==1){
            if(st)ed=i;
            else st=i;
        }
    }
    if(!st)return 0;
    //cout<<now<<' '<<st<<' '<<ed<<endl;
    if(ed){
        if(p[1])return 0;
        vis[st]=vis[ed]=1;
        int pos=0;
        for(int i=1;i<n;i++){
            if(vs[i])continue;
            for(int j=1;j<=b[i].k;j++){
                if(b[i].a[j]==st){
                    pos=i;break;
                }
            }
            if(pos){
                for(int j=1;j<=b[i].k;j++){
                    c[b[i].a[j]]--;
                }
                break;
            }
        }
        vs[pos]=1;
        p[now]=st;
        p[1]=ed;
        if(cnm(now-1)){
            vs[pos]=0;
            for(int j=1;j<=b[pos].k;j++){
                c[b[pos].a[j]]++;
            }
            vis[st]=0,vis[ed]=0;
            return 1;
        }
        //cout<<"NOWCODER "<<now<<endl;
        vs[pos]=0;
        for(int j=1;j<=b[pos].k;j++){
            c[b[pos].a[j]]++;
        }
        swap(p[now],p[1]);
        swap(st,ed);
        pos=0;
        for(int i=1;i<n;i++){
            if(vs[i])continue;
            for(int j=1;j<=b[i].k;j++){
                if(b[i].a[j]==st){
                    pos=i;break;
                }
            }
            if(pos){
                for(int j=1;j<=b[i].k;j++){
                    c[b[i].a[j]]--;
                }
                break;
            }
        }
        vs[pos]=1;
        int re=cnm(now-1);
        for(int j=1;j<=b[pos].k;j++){
            c[b[pos].a[j]]++;
        }
        vs[pos]=0;
        vis[st]=vis[ed]=0;
        return re;
    }
    else{
        vis[st]=1;
        p[now]=st;
        int pos=0;
        for(int i=1;i<n;i++){
            if(vs[i])continue;
            for(int j=1;j<=b[i].k;j++){
                if(b[i].a[j]==st){
                    pos=i;break;
                }
            }
            if(pos){
                for(int j=1;j<=b[i].k;j++){
                    c[b[i].a[j]]--;
                }
                break;
            }
        }
        vs[pos]=1;
        int re=cnm(now-1);
        for(int j=1;j<=b[pos].k;j++){
            c[b[pos].a[j]]++;
        }
        vis[st]=0;
        vs[pos]=0;
        return re;
    }
}
void solve(){
    cin>>n;
    for(int i=1;i<=n;i++)c[i]=0,C[i]=0,vis[i]=vs[i]=0,p[i]=0;
    for(int i=1;i<n;i++){
        scanf("%d",&b[i].k);
        for(int j=1;j<=b[i].k;j++){
            scanf("%d",&b[i].a[j]);
            c[b[i].a[j]]++;
            C[b[i].a[j]]++;
        }
    }
    p[n+1]=0;
    cnm(n);
    for(int i=1;i<=n;i++)printf("%d ",p[i]);
    printf("\n");
}
int main()
{
    int t;
    cin>>t;
    while(t--){
        solve();
    }
    return 0;
}
/*
1
5
2 2 3
2 2 4
2 1 4
3 1 4 5
*/


#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define LL long long
#define PB push_back
#define POP pop_back()
#define PII pair<int,int>
#define FI first
#define SE second
#define ULL unsigned long long
#define endl '\n'
using namespace std;
const int INF=0x3f3f3f3f;
const double pi=acos(-1),eps=1e-8;
const int maxn=1<<17;
const int N=200+10,M=635,mod=1e9+7;
int n;
int cnt[N];
int c[N];
struct node{
    int k;
    int a[N];
}b[N];
int p[N];
int flag;
int vis[N];
bool vs[N];
bool check(int st,int ed){
    p[n]=st;
    for(int i=1;i<=n;i++)vis[i]=0,vs[i]=0;
    vis[st]=1;
    vis[ed]=1;
    for(int i=1;i<=n;i++){
        cnt[i]=c[i];
    }
    for(int i=1;i<n;i++){
        int flag=0;
        for(int j=1;j<=b[i].k;j++){
            if(b[i].a[j]==st){
                flag=1;break;
            }
        }
        if(flag){
            vs[i]=1;
            //cout<<i<<endl;
            for(int j=1;j<=b[i].k;j++){
                //cout<<b[i].a[j]<<' '<<cnt[b[i].a[j]]<<endl;
                cnt[b[i].a[j]]--;
            }
        }
    }
    for(int i=n-1;i>1;i--){
        int ans=0;
        for(int j=1;j<=n;j++){
            //cout<<"wcynb "<<i<<' '<<j<<' '<<cnt[j]<<endl;
            if(cnt[j]==1&&j!=st&&j!=ed){
                ans=j;//break;
            }
        }
        //cout<<ans<<endl;
        if(ans==0){
            //cout<<i<<' '<<st<<' '<<ed<<endl;
            return 0;
        }
        p[i]=ans;
        vis[ans]=1;
        for(int j=1;j<n;j++){
            if(vs[j])continue;
            int flag=0;
            for(int q=1;q<=b[j].k;q++){
                if(b[j].a[q]==ans){
                    flag=1;break;
                }
            }
            if(flag){
                vs[j]=1;
                for(int q=1;q<=b[j].k;q++){
                    cnt[b[j].a[q]]--;
                }
                break;
            }
        }
    }
    p[1]=ed;
    if(ed==0){
        for(int i=1;i<=n;i++){
            if(vis[i]==0){
                p[1]=i;break;
            }
        }
    }
    if(c[p[2]]<c[p[1]])swap(p[1],p[2]);
    for(int i=1;i<=n;i++)printf("%d ",p[i]);
    printf("\n");
    return 1;
}
void solve(){
    cin>>n;
    for(int i=1;i<=n;i++)c[i]=0;
    for(int i=1;i<n;i++){
        scanf("%d",&b[i].k);
        for(int j=1;j<=b[i].k;j++){
            scanf("%d",&b[i].a[j]);
            c[b[i].a[j]]++;
        }
    }
    flag=0;
    int st1=0,st2=0;
    for(int i=1;i<=n;i++){
        if(c[i]==1){
            if(st1)st2=i;
            else st1=i;
        }
    }
    if(!check(st1,st2)){
        check(st2,st1);
    }

}
int main()
{
    int t;
    cin>>t;
    while(t--){
        solve();
    }
    return 0;
}
/*
1
5
2 4 5
3 1 4 5
3 1 3 5
5 1 2 3 4 5
*/


#include<bits/stdc++.h>
//#include<ext/pb_ds/assoc_container.hpp>
//#include<ext/pb_ds/tree_policy.hpp>
#define int long long
#define fr(i,x,y) for(int i=x;i<y;i++)
#define rfr(i,x,y) for(int i=x;i>=y;i--)
using namespace std;
//using namespace __gnu_pbds;
const int N=202,P=31,MOD=998244353,INF=1e17;
vector<int> mas[N];
int n;
bool tr(int st)
{
	map<int,bool> used;
	map<int,int> pos;
	vector<int> ans;
	fr(i,0,mas[st].size())
	{
		pos[mas[st][i]]=i+1;
		ans.push_back(mas[st][i]);
	}
	used[st]=1;
	bool swaped=0;
	fr(i,2,n)
	{
		bool was2=0;
		fr(j,1,n)
		{
			if(used[j])continue;
			bool can=1;
			int nw=-1;
			map<int,bool> was;
			fr(z,0,mas[j].size())
			{
				//cout<<mas[j][z]<<' ';
				was[mas[j][z]]=1;
				if(pos[mas[j][z]]==0)
				{
					if(nw==-1)
					{
						nw=mas[j][z];
					}
					else
					{
						can=0;
						break;
					}
				}
				else if(pos[mas[j][z]]<=ans.size()-mas[j].size()+1 && !(pos[mas[j][z]]==1 && mas[j].size()==i))
				{
					can=0;
					break;
				}
			}
			//cout<<'\n'<<'\n';
			if(can)
			{
				if(mas[j].size()==i)
				{
					if(!swaped)
					{
						if(was[ans[0]])swap(ans[0],ans[1]);
						swaped=1;
					}
					if(was[ans[0]] || !was[ans[1]])
					{
						can=0;
						break;
					}
				}
				ans.push_back(nw);
				pos[nw]=ans.size();
				was2=1;
				used[j]=1;
				break;
			}
		}
		if(!was2)return 0;
	}
	for(auto j:ans)cout<<j<<' ';
	cout<<'\n';
	return 1;
}
void solve()
{
	cin>>n;
	fr(i,1,n)
	{
		int k;
		cin>>k;
		fr(j,0,k)
		{
			int a;
			cin>>a;
			mas[i].push_back(a);
		}
	}
	fr(i,1,n)
	{
		if(mas[i].size()==2)
			{
				if(tr(i))break;
			}
	}

	fr(i,0,n)
	{
		mas[i].clear();
	}

}
int32_t main()
{
	//freopen("haircut.in","r",stdin);
	//freopen("haircut.out","w",stdout);
	ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	int t=1;
	cin>>t;
	while(t--)
		{
			solve();
			//cout<<'\n';
		}
}


#include<bits/stdc++.h>
//#include<ext/pb_ds/assoc_container.hpp>
//#include<ext/pb_ds/tree_policy.hpp>
#define int long long
#define fr(i,x,y) for(int i=x;i<y;i++)
#define rfr(i,x,y) for(int i=x;i>=y;i--)
using namespace std;
//using namespace __gnu_pbds;
const int N=202,P=31,MOD=998244353,INF=1e17;
vector<int> mas[N];
int n;
bool tr(int st)
{
	map<int,bool> used;
	map<int,int> pos;
	vector<int> ans;
	fr(i,0,mas[st].size())
	{
		pos[mas[st][i]]=i+1;
		ans.push_back(mas[st][i]);
	}
	used[st]=1;
	bool swaped=0;
	fr(i,2,n)
	{
		bool was2=0;
		fr(j,1,n)
		{
			if(used[j])continue;
			bool can=1;
			int nw=-1;
			map<int,bool> was;
			fr(z,0,mas[j].size())
			{
				//cout<<mas[j][z]<<' ';
				was[mas[j][z]]=1;
				if(pos[mas[j][z]]==0)
				{
					if(nw==-1)
					{
						nw=mas[j][z];
					}
					else
					{
						can=0;
						break;
					}
				}
				else if(pos[mas[j][z]]<=ans.size()-mas[j].size()+1 && !(pos[mas[j][z]]==1 && mas[j].size()==i))
				{
					can=0;
					break;
				}
			}
			//cout<<'\n'<<'\n';
			if(can)
			{
				if(mas[j].size()==i)
				{
					if(!swaped)
					{
						if(was[ans[0]])swap(ans[0],ans[1]);
						swaped=1;
					}
					if(was[ans[0]] || !was[ans[1]])
					{
						can=0;
						break;
					}
				}
				ans.push_back(nw);
				pos[nw]=ans.size();
				was2=1;
				used[j]=1;
				break;
			}
		}
		if(!was2)return 0;
	}
	for(auto j:ans)cout<<j<<' ';
	cout<<'\n';
	return 1;
}
void solve()
{
	cin>>n;
	fr(i,1,n)
	{
		int k;
		cin>>k;
		fr(j,0,k)
		{
			int a;
			cin>>a;
			mas[i].push_back(a);
		}
	}
	fr(i,1,n)
	{
		if(mas[i].size()==2)
			{
				if(tr(i))break;
			}
	}

	fr(i,0,n)
	{
		mas[i].clear();
	}

}
int32_t main()
{
	//freopen("haircut.in","r",stdin);
	//freopen("haircut.out","w",stdout);
	//ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	int t=1;
	cin>>t;
	while(t--)
		{
			solve();
			//cout<<'\n';
		}
}


#include<bits/stdc++.h>
//#include<ext/pb_ds/assoc_container.hpp>
//#include<ext/pb_ds/tree_policy.hpp>
#define int long long
#define fr(i,x,y) for(int i=x;i<y;i++)
#define rfr(i,x,y) for(int i=x;i>=y;i--)
using namespace std;
//using namespace __gnu_pbds;
const int N=202,P=31,MOD=998244353,INF=1e17;
vector<int> mas[N];
int n;
bool tr(int st)
{
	map<int,bool> used;
	map<int,int> pos;
	vector<int> ans;
	fr(i,0,mas[st].size())
	{
		pos[mas[st][i]]=i+1;
		ans.push_back(mas[st][i]);
	}
	used[st]=1;
	bool swaped=0;
	fr(i,2,n)
	{
		bool was2=0;
		fr(j,1,n)
		{
			if(used[j])continue;
			bool can=1;
			int nw=-1;
			map<int,bool> was;
			fr(z,0,mas[j].size())
			{
				//cout<<mas[j][z]<<' ';
				was[mas[j][z]]=1;
				if(pos[mas[j][z]]==0)
				{
					if(nw==-1)
					{
						nw=mas[j][z];
					}
					else
					{
						can=0;
						break;
					}
				}
				else if(pos[mas[j][z]]<=ans.size()-mas[j].size()+1 && !(pos[mas[j][z]]<=2 && ans.size()==i))
				{
					can=0;
					break;
				}
			}
			//cout<<'\n'<<'\n';
			if(can)
			{
				if(mas[j].size()==i)
				{
					if(!swaped)
					{
						if(was[ans[0]])swap(ans[0],ans[1]);
						swaped=1;
					}
					if(was[ans[0]] || !was[ans[1]])
					{
						can=0;
						break;
					}
				}
				ans.push_back(nw);
				pos[nw]=ans.size();
				was2=1;
				used[j]=1;
				break;
			}
		}
		if(!was2)return 0;
	}
	for(auto j:ans)cout<<j<<' ';
	cout<<'\n';
	return 1;
}
void solve()
{
	cin>>n;
	fr(i,1,n)
	{
		int k;
		cin>>k;
		fr(j,0,k)
		{
			int a;
			cin>>a;
			mas[i].push_back(a);
		}
	}
	fr(i,1,n)
	{
		if(mas[i].size()==2)
			{
				if(tr(i))break;
			}
	}

	fr(i,0,n)
	{
		mas[i].clear();
	}

}
int32_t main()
{
	//freopen("haircut.in","r",stdin);
	//freopen("haircut.out","w",stdout);
	//ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	int t=1;
	cin>>t;
	while(t--)
		{
			solve();
			//cout<<'\n';
		}
}


#include<bits/stdc++.h>
//#include<ext/pb_ds/assoc_container.hpp>
//#include<ext/pb_ds/tree_policy.hpp>
#define int long long
#define fr(i,x,y) for(int i=x;i<y;i++)
#define rfr(i,x,y) for(int i=x;i>=y;i--)
using namespace std;
//using namespace __gnu_pbds;
const int N=200,P=31,MOD=998244353,INF=1e17;
vector<int> mas[N];
int n;
bool tr(int st)
{
	map<int,bool> in,used;
	vector<int> ans;
	for(auto j:mas[st])
	{
		in[j]=1;
		ans.push_back(j);
	}
	used[st]=1;
	fr(i,2,n)
	{
		bool was2=0;
		fr(j,1,n)
		{
			if(used[j])continue;
			bool can=1;
			int nw=-1;
			map<int,bool> was;
			fr(z,0,mas[j].size())
			{
				//cout<<mas[j][z]<<' ';
				was[mas[j][z]]=1;
				if(!in[mas[j][z]])
				{
					if(nw==-1)
					{
						nw=mas[j][z];
					}
					else
					{
						can=0;
						break;
					}
				}
			}
			//cout<<'\n'<<'\n';
			if(can)
			{
				if(mas[j].size()==i)
				{
					if(was[ans[0]])swap(ans[0],ans[1]);
				}
				ans.push_back(nw);
				in[nw]=1;
				was2=1;
				used[j]=1;
				break;
			}
		}
		if(!was2)return 0;
	}
	for(auto j:ans)cout<<j<<' ';
	cout<<'\n';
	return 1;
}
void solve()
{
	cin>>n;
	fr(i,1,n)
	{
		int k;
		cin>>k;
		fr(j,0,k)
		{
			int a;
			cin>>a;
			mas[i].push_back(a);
		}
	}
	fr(i,1,n)
	{
		if(mas[i].size()==2)
			{
				if(tr(i))break;
			}
	}

	fr(i,0,n)
	{
		mas[i].clear();
	}

}
int32_t main()
{
	//freopen("haircut.in","r",stdin);
	//freopen("haircut.out","w",stdout);
	ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	int t=1;
	cin>>t;
	while(t--)
		{
			solve();
			//cout<<'\n';
		}
}


#include<bits/stdc++.h>
//#include<ext/pb_ds/assoc_container.hpp>
//#include<ext/pb_ds/tree_policy.hpp>
#define int long long
#define fr(i,x,y) for(int i=x;i<y;i++)
#define rfr(i,x,y) for(int i=x;i>=y;i--)
using namespace std;
//using namespace __gnu_pbds;
const int N=202,P=31,MOD=998244353,INF=1e17;
vector<int> mas[N];
int n;
bool tr(int st)
{
	map<int,bool> used;
	map<int,int> pos;
	vector<int> ans;
	fr(i,0,mas[st].size())
	{
		pos[mas[st][i]]=i+1;
		ans.push_back(mas[st][i]);
	}
	used[st]=1;
	bool swaped=0;
	fr(i,2,n)
	{
		bool was2=0;
		fr(j,1,n)
		{
			if(used[j])continue;
			bool can=1;
			int nw=-1;
			map<int,bool> was;
			fr(z,0,mas[j].size())
			{
				//cout<<mas[j][z]<<' ';
				was[mas[j][z]]=1;
				if(pos[mas[j][z]]==0)
				{
					if(nw==-1)
					{
						nw=mas[j][z];
					}
					else
					{
						can=0;
						break;
					}
				}
				else if(pos[mas[j][z]]<ans.size()-mas[j].size()+1 && pos[mas[j][z]]>2)
				{
					can=0;
					break;
				}
			}
			//cout<<'\n'<<'\n';
			if(can)
			{
				if(mas[j].size()==i)
				{
					if(!swaped)
					{
						if(was[ans[0]])swap(ans[0],ans[1]);
						swaped=1;
					}
					if(was[ans[0]] || !was[ans[1]])
					{
						can=0;
						break;
					}
				}
				ans.push_back(nw);
				pos[nw]=ans.size();
				was2=1;
				used[j]=1;
				break;
			}
		}
		if(!was2)return 0;
	}
	for(auto j:ans)cout<<j<<' ';
	cout<<'\n';
	return 1;
}
void solve()
{
	cin>>n;
	fr(i,1,n)
	{
		int k;
		cin>>k;
		fr(j,0,k)
		{
			int a;
			cin>>a;
			mas[i].push_back(a);
		}
	}
	fr(i,1,n)
	{
		if(mas[i].size()==2)
			{
				if(tr(i))break;
			}
	}

	fr(i,0,n)
	{
		mas[i].clear();
	}

}
int32_t main()
{
	//freopen("haircut.in","r",stdin);
	//freopen("haircut.out","w",stdout);
	//ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	int t=1;
	cin>>t;
	while(t--)
		{
			solve();
			//cout<<'\n';
		}
}


#include<bits/stdc++.h>
//#include<ext/pb_ds/assoc_container.hpp>
//#include<ext/pb_ds/tree_policy.hpp>
#define int long long
#define fr(i,x,y) for(int i=x;i<y;i++)
#define rfr(i,x,y) for(int i=x;i>=y;i--)
using namespace std;
//using namespace __gnu_pbds;
const int N=202,P=31,MOD=998244353,INF=1e17;
vector<int> mas[N];
int n;
bool tr(int st)
{
	map<int,bool> used;
	map<int,int> pos;
	vector<int> ans;
	fr(i,0,mas[st].size())
	{
		pos[mas[st][i]]=i+1;
		ans.push_back(mas[st][i]);
	}
	used[st]=1;
	bool swaped=0;
	fr(i,2,n)
	{
		bool was2=0;
		fr(j,1,n)
		{
			if(used[j])continue;
			bool can=1;
			int nw=-1;
			map<int,bool> was;
			fr(z,0,mas[j].size())
			{
				//cout<<mas[j][z]<<' ';
				was[mas[j][z]]=1;
				if(pos[mas[j][z]]==0)
				{
					if(nw==-1)
					{
						nw=mas[j][z];
					}
					else
					{
						can=0;
						break;
					}
				}
				else if(pos[mas[j][z]]<=ans.size()-mas[j].size()+1 )//&& !(pos[mas[j][z]]<=2 && ans.size()==i))
				{
					can=0;
					break;
				}
			}
			//cout<<'\n'<<'\n';
			if(can)
			{
				if(mas[j].size()==i)
				{
					if(!swaped)
					{
						if(was[ans[0]])swap(ans[0],ans[1]);
						swaped=1;
					}
					if(was[ans[0]] || !was[ans[1]])
					{
						can=0;
						break;
					}
				}
				ans.push_back(nw);
				pos[nw]=ans.size();
				was2=1;
				used[j]=1;
				break;
			}
		}
		if(!was2)return 0;
	}
	for(auto j:ans)cout<<j<<' ';
	cout<<'\n';
	return 1;
}
void solve()
{
	cin>>n;
	fr(i,1,n)
	{
		int k;
		cin>>k;
		fr(j,0,k)
		{
			int a;
			cin>>a;
			mas[i].push_back(a);
		}
	}
	fr(i,1,n)
	{
		if(mas[i].size()==2)
			{
				if(tr(i))break;
			}
	}

	fr(i,0,n)
	{
		mas[i].clear();
	}

}
int32_t main()
{
	//freopen("haircut.in","r",stdin);
	//freopen("haircut.out","w",stdout);
	//ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	int t=1;
	cin>>t;
	while(t--)
		{
			solve();
			//cout<<'\n';
		}
}


#include<bits/stdc++.h>
//#include<ext/pb_ds/assoc_container.hpp>
//#include<ext/pb_ds/tree_policy.hpp>
#define int long long
#define fr(i,x,y) for(int i=x;i<y;i++)
#define rfr(i,x,y) for(int i=x;i>=y;i--)
using namespace std;
//using namespace __gnu_pbds;
const int N=202,P=31,MOD=998244353,INF=1e17;
vector<int> mas[N];
int n;
bool tr(int st)
{
	map<int,bool> used;
	map<int,int> pos;
	vector<int> ans;
	fr(i,0,mas[st].size())
	{
		pos[mas[st][i]]=i+1;
		ans.push_back(mas[st][i]);
	}
	used[st]=1;
	bool swaped=0;
	fr(i,2,n)
	{
		bool was2=0;
		fr(j,1,n)
		{
			if(used[j])continue;
			bool can=1;
			int nw=-1;
			map<int,bool> was;
			fr(z,0,mas[j].size())
			{
				//cout<<mas[j][z]<<' ';
				was[mas[j][z]]=1;
				if(pos[mas[j][z]]==0)
				{
					if(nw==-1)
					{
						nw=mas[j][z];
					}
					else
					{
						can=0;
						break;
					}
				}
				else if(pos[mas[j][z]]<=ans.size()-mas[j].size()+1 && pos[mas[j][z]]>2)
				{
					can=0;
					break;
				}
			}
			//cout<<'\n'<<'\n';
			if(can)
			{
				if(mas[j].size()==i)
				{
					if(!swaped)
					{
						if(was[ans[0]])swap(ans[0],ans[1]);
						swaped=1;
					}
					if(was[ans[0]] || !was[ans[1]])
					{
						can=0;
						break;
					}
				}
				ans.push_back(nw);
				pos[nw]=ans.size();
				was2=1;
				used[j]=1;
				break;
			}
		}
		if(!was2)return 0;
	}
	for(auto j:ans)cout<<j<<' ';
	cout<<'\n';
	return 1;
}
void solve()
{
	cin>>n;
	fr(i,1,n)
	{
		int k;
		cin>>k;
		fr(j,0,k)
		{
			int a;
			cin>>a;
			mas[i].push_back(a);
		}
	}
	fr(i,1,n)
	{
		if(mas[i].size()==2)
			{
				if(tr(i))break;
			}
	}

	fr(i,0,n)
	{
		mas[i].clear();
	}

}
int32_t main()
{
	//freopen("haircut.in","r",stdin);
	//freopen("haircut.out","w",stdout);
	ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	int t=1;
	cin>>t;
	while(t--)
		{
			solve();
			//cout<<'\n';
		}
}


#include <bits/stdc++.h>
using namespace std;
vector<vector<int> > v;
int A[202], vis[202];

int check(int idx, vector<vector<int> > v){
    for(int i=0;i<v.size();i++){
        int l = v[i].size();
        bool ok = 1;
        for(int j=0;j<l;j++){
            if(v[i][j] == A[idx]){
                ok = 1;
                break;
            }
        }
        if(ok){
            int cnt = 0, x = 0;
            for(int j=0;j<l;j++){
                if(vis[v[i][j]]) continue;
                cnt++;
                x = v[i][j];
            }
            if(cnt == 1) return x;
        }
    }
    return -1;
}
int main(){
    int t;
    scanf("%d", &t);
    while(t--){
        v.clear();
        int n, k, a;
        scanf("%d", &n);
        for(int i=1;i<=n-1;i++){
            scanf("%d", &k);

            vector<int> x;
            for(int j=1;j<=k;j++){
                scanf("%d", &a);
                x.emplace_back(a);
            }
            v.emplace_back(x);
        }

        for(int i=1;i<=n;i++){
            memset(vis, 0, sizeof(vis));
            A[1] = i;
            vis[i] = 1;
            bool ok = 1;
            for(int j=2;j<=n;j++){
                int x = check(j-1, v);
                if(x == -1){
                    ok = 0;
                    break;
                }
                A[j] = x;
                vis[x] = 1;
            }
            if(ok){
                for(int j=1;j<=n;j++) printf("%d ", A[j]);
                printf("\n");
                break;
            }
        }
    }
}


#include <bits/stdc++.h>
using namespace std;
vector<vector<int> > v;
int A[202], vis[202];

int check(int idx, vector<vector<int> > v){
    for(int i=0;i<v.size();i++){
        int l = v[i].size();
        bool ok = 1;
        for(int j=0;j<l;j++){
            if(v[i][j] == A[idx]){
                ok = 1;
                break;
            }
        }
        if(ok){
            int cnt = 0, x = 0;
            for(int j=0;j<l;j++){
                if(vis[v[i][j]] > 0 && vis[v[i][j]] >= idx-l+2) continue;
                cnt++;
                x = v[i][j];
            }
            if(cnt == 1 && vis[x] == 0) return x;
        }
    }
    return -1;
}
int main(){
    int t;
    scanf("%d", &t);
    while(t--){
        v.clear();
        int n, k, a;
        scanf("%d", &n);
        for(int i=1;i<=n-1;i++){
            scanf("%d", &k);

            vector<int> x;
            for(int j=1;j<=k;j++){
                scanf("%d", &a);
                x.emplace_back(a);
            }
            v.emplace_back(x);
        }

        for(int i=1;i<=n;i++){
            memset(vis, 0, sizeof(vis));
            A[1] = i;
            vis[i] = i;
            bool ok = 1;
            for(int j=2;j<=n;j++){
                int x = check(j-1, v);
                if(x == -1){
                    ok = 0;
                    break;
                }
                A[j] = x;
                vis[x] = j;
            }
            if(ok){
                for(int j=1;j<=n;j++) printf("%d ", A[j]);
                printf("\n");
                break;
            }
        }
    }
}


#include <bits/stdc++.h>
using namespace std;
vector<vector<int> > v;
int A[202], vis[202];

int check(int idx, vector<vector<int> > v){
    for(int i=0;i<v.size();i++){
        int l = v[i].size();
        bool ok = 1;
        for(int j=0;j<l;j++){
            if(v[i][j] == A[idx]){
                ok = 1;
                break;
            }
        }
        if(ok){
            int cnt = 0, x = 0;
            for(int j=0;j<l;j++){
                if(vis[v[i][j]] > 0 && vis[v[i][j]] >= idx-l+2) continue;
                cnt++;
                x = v[i][j];
            }
            if(cnt == 1 && vis[x] == 0) return x;
        }
    }
    return -1;
}
int main(){
    int t;
    scanf("%d", &t);
    while(t--){
        v.clear();
        int n, k, a;
        scanf("%d", &n);
        for(int i=1;i<=n-1;i++){
            scanf("%d", &k);

            vector<int> x;
            for(int j=1;j<=k;j++){
                scanf("%d", &a);
                x.emplace_back(a);
            }
            v.emplace_back(x);
        }

        for(int i=1;i<=n;i++){
            memset(vis, 0, sizeof(vis));
            A[1] = i;
            vis[i] = 1;
            bool ok = 1;
            for(int j=2;j<=n;j++){
                int x = check(j-1, v);
                if(x == -1){
                    ok = 0;
                    break;
                }
                A[j] = x;
                vis[x] = j;
            }
            if(ok){

                for(int j=1;j<=n;j++) printf("%d ", A[j]);
                printf("\n");
                break;
            }
        }
    }
}


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

typedef long long ll;
const int maxn = 205;
typedef pair<int, int>par;
const ll mod = 1e9+7;

bitset<205>mask[maxn], masko[maxn];
vector<int>pos[maxn];
int sol[maxn], soll[maxn];
int n, ok, mark[maxn];
vector<int>ms[maxn];

bool check(){

    for( int i = 1; i <= n; i++ )mark[i] = 0;

    int cont = 0;
    for( int i = 1; i < n; i++ ){
        int cnt = masko[i].count();
        bitset<205>bs;
        for( int j = 1; j <= n; j++ ){
            if( j <= cnt )
                bs[soll[j]] = 1;
            if( j > cnt ){
                bs[ soll[j] ] = 1;
                bs[ soll[j-cnt] ] = 0;
            }

            if( j >= cnt ){
                if( bs == masko[i] ){
                    if( mark[j] ) return false;
                    mark[j] = 1;
                    cont++;
                    break;
                }
            }
        }
    }
    return cont == n-1;
}

void solve( int poss ){
    if( ok )return;
    if( poss == n+1 ){
        if( check() ){
            ok = 1;
            for( int i = 1; i <= n; i++ )
                sol[i] = soll[i];
            return;
        }
    }

    for( int i = 1; i < n; i++ ){
        if( mask[i].count() != 1 )continue;
        for( auto j : ms[i] ){
            if( !mask[i][j] )continue;
            soll[poss] = j;
            for( auto p : pos[j] )
                mask[p][j] = 0;
            solve(poss+1);
            if( ok )return;
            soll[poss] = 0;
            for( auto p : pos[j] )
                mask[p][j] = 1;
        }
    }
}

int main()
{
    cin.tie(0);
    ios_base::sync_with_stdio(0);
    #ifdef acm
    freopen("a.in", "r",stdin);
    #endif // acm

    int test;
    cin >> test;

    while( test-- ){
        cin >> n;
        int c, a;

        vector<int>candidates;
        for( int i = 1; i <= n; i++ )
            pos[i].clear();
        for( int i = 1; i < n; i++ ){
            cin >> c;
            mask[i].reset();
            masko[i].reset();
            ms[i].clear();
            int b = 0;
            if( c == 2 )
                b = 1;
            while( c-- ){
                cin >> a;
                mask[i][a] = masko[i][a] = 1;
                ms[i].push_back(a);
                if( b )candidates.push_back(a);
                pos[a].push_back(i);
            }
        }

        ok = 0;
        for( auto c : candidates ){
            soll[1] = c;
            for( auto p : pos[c] )
                mask[p][c] = 0;
            solve(2);
            for( auto p : pos[c] )
                mask[p][c] = 1;
            if( ok )break;
        }

        for( int i = 1; i <= n; i++ )
            cout << sol[i] << " \n"[i == n];
    }

    return 0;
}


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

typedef long long ll;
const int maxn = 205;
typedef pair<int, int>par;
const ll mod = 1e9+7;

bitset<205>mask[maxn], masko[maxn];
vector<int>pos[maxn];
int sol[maxn], soll[maxn];
int n, ok, mark[maxn];
multiset<int>ms[maxn], msa[maxn];

bool check(){

    for( int i = 1; i <= n; i++ )mark[i] = 0;

    int cont = 0;
    for( int i = 1; i < n; i++ ){
        int cnt = masko[i].count();
        bitset<205>bs;
        for( int j = 1; j <= n; j++ ){
            if( j <= cnt )
                bs[sol[j]] = 1;
            if( j > cnt ){
                bs[ sol[j] ] = 1;
                bs[ sol[j-cnt] ] = 0;
            }

            if( j >= cnt ){
                if( bs == masko[i] ){
                    if( mark[j] ) return false;
                    mark[j] = 1;
                    cont++;
                    break;
                }
            }
        }
    }
    return cont == n-1;
}

int main()
{
    cin.tie(0);
    ios_base::sync_with_stdio(0);
    #ifdef acm
    freopen("a.in", "r",stdin);
    #endif // acm

    int test;
    cin >> test;

    while( test-- ){
        cin >> n;
        int c, a;

        vector<int>candidates;
        for( int i = 1; i <= n; i++ )
            pos[i].clear(), ms[i].clear();
        for( int i = 1; i < n; i++ ){
            cin >> c;
            mask[i].reset();
            masko[i].reset();
            while( c-- ){
                cin >> a;
                masko[i][a] = 1;
                ms[i].insert(a);
                pos[a].push_back(i);
            }
        }

        for( int i = 1; i <= n; i++ ){
            sol[1] = i;
            for( int j = 1; j < n; j++ )
                msa[j] = ms[j];
            for( auto p : pos[i] )
                msa[p].erase(i);

            int ok = 1;
            for( int j = 2; j <= n; j++ ){
                int id = 1, cnt = 0;
                for( int k = 1; k < n; k++ ){
                    if( msa[k].size() == 1 ){
                        id = k;
                        cnt++;
                    }
                }

                if( cnt != 1 ){
                    ok = 0;
                    break;
                }

                sol[j] = *(msa[id].begin());
                for( auto p : pos[ sol[j] ] )
                    msa[p].erase(sol[j]);
            }

            if( !ok )continue;
            if( !check() )continue;
            break;
        }

        for( int i = 1; i <= n; i++ )
            cout << sol[i] << " \n"[i == n];
    }

    return 0;
}


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

typedef long long ll;
const int maxn = 205;
typedef pair<int, int>par;
const ll mod = 1e9+7;

bitset<205>mask[maxn], masko[maxn];
vector<int>pos[maxn];
int sol[maxn], soll[maxn];
int n, ok, mark[maxn];
vector<int>ms[maxn];

bool check(){

    for( int i = 1; i <= n; i++ )mark[i] = 0;

    int cont = 0;
    for( int i = 1; i < n; i++ ){
        int cnt = masko[i].count();
        bitset<205>bs;
        for( int j = 1; j <= n; j++ ){
            if( j <= cnt )
                bs[soll[j]] = 1;
            if( j > cnt ){
                bs[ soll[j] ] = 1;
                bs[ soll[j-cnt] ] = 0;
            }

            if( j >= cnt ){
                if( bs == masko[i] ){
                    if( mark[j] ) return false;
                    mark[j] = 1;
                    cont++;
                    break;
                }
            }
        }
    }
    return cont == n-1;
}

void solve( int poss ){
    if( ok )return;
    if( poss == n+1 ){
        if( check() ){
            ok = 1;
            for( int i = 1; i <= n; i++ )
                sol[i] = soll[i];
            return;
        }
    }

    for( int i = 1; i < n; i++ ){
        if( mask[i].count() != 1 )continue;
        for( auto j : ms[i] ){
            if( !mask[i][j] )continue;
            soll[poss] = j;
            for( auto p : pos[j] )
                mask[p][j] = 0;
            solve(poss+1);
            if( ok )return;
            soll[poss] = 0;
            for( auto p : pos[j] )
                mask[p][j] = 1;
            break;
        }
    }
}

int main()
{
    cin.tie(0);
    ios_base::sync_with_stdio(0);
    #ifdef acm
    freopen("a.in", "r",stdin);
    #endif // acm

    int test;
    cin >> test;

    while( test-- ){
        cin >> n;
        int c, a;

        vector<int>candidates;
        for( int i = 1; i <= n; i++ )
            pos[i].clear();
        for( int i = 1; i < n; i++ ){
            cin >> c;
            mask[i].reset();
            masko[i].reset();
            ms[i].clear();
            int b = 0;
            if( c == 2 )
                b = 1;
            while( c-- ){
                cin >> a;
                mask[i][a] = masko[i][a] = 1;
                ms[i].push_back(a);
                if( b )candidates.push_back(a);
                pos[a].push_back(i);
            }
        }

        ok = 0;
        for( auto c : candidates ){
            soll[1] = c;
            for( auto p : pos[c] )
                mask[p][c] = 0;
            solve(2);
            for( auto p : pos[c] )
                mask[p][c] = 1;
            if( ok )break;
        }

        for( int i = 1; i <= n; i++ )
            cout << sol[i] << " \n"[i == n];
    }

    return 0;
}


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

typedef long long ll;
const int maxn = 205;
typedef pair<int, int>par;
const ll mod = 1e9+7;

bitset<205>mask[maxn], masko[maxn];
vector<int>pos[maxn];
int sol[maxn], soll[maxn];
int n, ok, mark[maxn];
multiset<int>ms[maxn], msa[maxn];

bool check(){

    for( int i = 1; i <= n; i++ )mark[i] = 0;

    int cont = 0;
    for( int i = 1; i < n; i++ ){
        int cnt = masko[i].count();
        bitset<205>bs;
        for( int j = 1; j <= n; j++ ){
            if( j <= cnt )
                bs[sol[j]] = 1;
            if( j > cnt ){
                bs[ sol[j] ] = 1;
                bs[ sol[j-cnt] ] = 0;
            }

            if( j >= cnt ){
                if( bs == masko[i] ){
                    if( mark[j] ) return false;
                    mark[j] = 1;
                    cont++;
                    break;
                }
            }
        }
    }
    return cont == n-1;
}

int main()
{
    cin.tie(0);
    ios_base::sync_with_stdio(0);
    #ifdef acm
    freopen("a.in", "r",stdin);
    #endif // acm

    int test;
    cin >> test;

    while( test-- ){
        cin >> n;
        int c, a;

        vector<int>candidates;
        for( int i = 1; i <= n; i++ )
            pos[i].clear();
        for( int i = 1; i < n; i++ ){
            cin >> c;
            mask[i].reset();
            masko[i].reset();
            while( c-- ){
                cin >> a;
                masko[i][a] = 1;
                ms[i].insert(a);
                pos[a].push_back(i);
            }
        }

        for( int i = 1; i <= n; i++ ){
            sol[1] = i;
            for( int j = 1; j < n; j++ )
                msa[j] = ms[j];
            for( auto p : pos[i] )
                msa[p].erase(i);

            int ok = 1;
            for( int j = 2; j <= n; j++ ){
                int id = 1, cnt = 0;
                for( int k = 1; k < n; k++ ){
                    if( msa[k].size() == 1 ){
                        id = k;
                        cnt++;
                    }
                }

                if( cnt != 1 ){
                    ok = 0;
                    break;
                }

                sol[j] = *(msa[id].begin());
                for( auto p : pos[ sol[j] ] )
                    msa[p].erase(sol[j]);
            }

            if( !ok )continue;
            if( !check() )continue;
            break;
        }

        for( int i = 1; i <= n; i++ )
            cout << sol[i] << " \n"[i == n];
    }

    return 0;
}


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

typedef long long ll;
const int maxn = 205;
typedef pair<int, int>par;
const ll mod = 1e9+7;

bitset<205>mask[maxn], masko[maxn];
vector<int>pos[maxn];
int sol[maxn], soll[maxn];
int n, ok, mark[maxn];
vector<int>ms[maxn];

bool check(){
    for( int i = 1; i <= n; i++ )
        mark[i] = 0;

    for( int i = 2; i <= n; i++ ){
        int ok = 0;
        for( int j = 1; j < n; j++ ){
            if( mark[j] )continue;
            bitset<205>bs;
            int cnt = masko[j].count();
            for( int k = 0; k < cnt; k++ )
                bs[ soll[i-k] ] = 1;
            if( bs == masko[j] ){
                ok = 1;
                mark[j] = 1;
                break;
            }
        }
        if( !ok ) return false;
    }
    return true;
}

void solve( int poss ){
    if( ok )return;
    if( poss == n+1 ){
        if( check() ){
            ok = 1;
            for( int i = 1; i <= n; i++ )
                sol[i] = soll[i];
            return;
        }
    }

    for( int i = 1; i < n; i++ ){
        if( mask[i].count() != 1 )continue;
        for( auto j : ms[i] ){
            if( !mask[i][j] )continue;
            soll[poss] = j;
            for( auto p : pos[j] )
                mask[p][j] = 0;
            solve(poss+1);
            soll[poss] = 0;
            for( auto p : pos[j] )
                mask[p][j] = 1;
        }
    }
}

int main()
{
    cin.tie(0);
    ios_base::sync_with_stdio(0);
    #ifdef acm
    freopen("a.in", "r",stdin);
    #endif // acm

    int test;
    cin >> test;

    while( test-- ){
        cin >> n;
        int c, a;

        vector<int>candidates;
        for( int i = 1; i <= n; i++ )
            pos[i].clear();
        for( int i = 1; i < n; i++ ){
            cin >> c;
            mask[i].reset();
            masko[i].reset();
            ms[i].clear();
            int b = 0;
            if( c == 2 )
                b = 1;
            while( c-- ){
                cin >> a;
                mask[i][a] = masko[i][a] = 1;
                ms[i].push_back(a);
                if( b )candidates.push_back(a);
                pos[a].push_back(i);
            }
        }

        ok = 0;
        for( auto c : candidates ){
            soll[1] = c;
            for( auto p : pos[c] )
                mask[p][c] = 0;
            solve(2);
            for( auto p : pos[c] )
                mask[p][c] = 1;
            if( ok )break;
        }

        for( int i = 1; i <= n; i++ )
            cout << sol[i] << " \n"[i == n];
    }

    return 0;
}