#include <iostream>
#include <stdio.h>
#include <vector>
using namespace std;
typedef long long Int;
int groupid[100111];
Int sum[100111];
int sz[100111];
int n,m;
vector<int> group[100111];
void Union(int a,int b) ///Operation 1
{
int r1=groupid[a];
int r2=groupid[b];
int swp,i;
if (r1==r2)
return;
if (group[r2].size()>group[r1].size()) ///Make sure we're iterating the smaller group
{
swp=r1;
r1=r2;
r2=swp;
}
for (i=0;i<group[r2].size();i++)
{
if ( groupid[ group[r2][i] ]==r2 ) ///Check whether the element is not a leftover
{
group[r1].push_back(group[r2][i]); ///Add it to the new group
groupid[ group[r2][i] ]=r1; ///Remember that it's in the new group
}
}
sum[r1]+=sum[r2]; ///Update new group values
sz[r1]+=sz[r2];
return;
}
void Move(int a,int b) ///Operation 2
{
int r1=groupid[a];
int r2=groupid[b];
if (r1==r2)
return;
sz[r1]--;
sum[r1]-=(Int)a; ///Update old group values
sz[r2]++;
sum[r2]+=(Int)a; ///Update new group values
group[r2].push_back(a); ///Add a to the new group
groupid[a]=r2; ///Update a's group so that we don't mix it with the leftover
return;
}
int main()
{
int i,j;
int cm,a,b;
while(scanf("%d %d",&n,&m)==2)
{
for (i=1;i<=n;i++) ///Initialise every element to be in its own group
{
groupid[i]=i;
sz[i]=1;
sum[i]=i;
group[i].clear();
group[i].push_back(i);
}
for (i=1;i<=m;i++)
{
scanf("%d",&cm);
if (cm==1)
{
scanf("%d %d",&a,&b);
Union(a,b);
}
else if (cm==2)
{
scanf("%d %d",&a,&b);
Move(a,b);
}
else
{
scanf("%d",&a);
printf("%d %lld\n",sz[ groupid[a] ],sum[ groupid[a] ]);
}
}
}
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8c3RkaW8uaD4KI2luY2x1ZGUgPHZlY3Rvcj4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKdHlwZWRlZiBsb25nIGxvbmcgSW50OwoKaW50IGdyb3VwaWRbMTAwMTExXTsKSW50IHN1bVsxMDAxMTFdOwppbnQgc3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yKQogICAgcmV0dXJuOwoKICAgIHN6W3IxXS0tOwogICAgc3VtW3IxXS09KEludClhOyAvLy9VcGRhdGUgb2xkIGdyb3VwIHZhbHVlcwoKICAgIHN6W3IyXSsrOwogICAgc3VtW3IyXSs9KEludClhOyAvLy9VcGRhdGUgbmV3IGdyb3VwIHZhbHVlcwoKICAgIGdyb3VwW3IyXS5wdXNoX2JhY2soYSk7IC8vL0FkZCBhIHRvIHRoZSBuZXcgZ3JvdXAKCiAgICBncm91cGlkW2FdPXIyOyAvLy9VcGRhdGUgYSdzIGdyb3VwIHNvIHRoYXQgd2UgZG9uJ3QgbWl4IGl0IHdpdGggdGhlIGxlZnRvdmVyCgogICAgcmV0dXJuOwp9CgppbnQgbWFpbigpCnsKICAgIGludCBpLGo7CiAgICBpbnQgY20sYSxiOwoKICAgIHdoaWxlKHNjYW5mKCIlZCAlZCIsJm4sJm0pPT0yKQogICAgewogICAgICAgIGZvciAoaT0xO2k8PW47aSsrKSAvLy9Jbml0aWFsaXNlIGV2ZXJ5IGVsZW1lbnQgdG8gYmUgaW4gaXRzIG93biBncm91cAogICAgICAgIHsKICAgICAgICAgICAgZ3JvdXBpZFtpXT1pOwogICAgICAgICAgICBzeltpXT0xOwogICAgICAgICAgICBzdW1baV09aTsKCiAgICAgICAgICAgIGdyb3VwW2ldLmNsZWFyKCk7CiAgICAgICAgICAgIGdyb3VwW2ldLnB1c2hfYmFjayhpKTsKICAgICAgICB9CgogICAgICAgIGZvciAoaT0xO2k8PW07aSsrKQogICAgICAgIHsKICAgICAgICAgICAgc2NhbmYoIiVkIiwmY20pOwoKICAgICAgICAgICAgaWYgKGNtPT0xKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBzY2FuZigiJWQgJWQiLCZhLCZiKTsKCiAgICAgICAgICAgICAgICBVbmlvbihhLGIpOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGVsc2UgaWYgKGNtPT0yKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBzY2FuZigiJWQgJWQiLCZhLCZiKTsKCiAgICAgICAgICAgICAgICBNb3ZlKGEsYik7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgZWxzZQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBzY2FuZigiJWQiLCZhKTsKCiAgICAgICAgICAgICAgICBwcmludGYoIiVkICVsbGRcbiIsc3pbIGdyb3VwaWRbYV0gXSxzdW1bIGdyb3VwaWRbYV0gXSk7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICB9CgogICAgcmV0dXJuIDA7Cn0K