#include<bits/stdc++.h>
using namespace std;
#define gc getchar_unlocked
#define fo(i,n) for(int i=0;i<n;i++)
#define Fo(i,k,n) for(int i = k ;i<=n;i++)
#define FO(i,n,k) for(int i = n;i>=k;i--)
#define ll long long
#define si(x) scanf("%d",&x)
#define sl(x) scanf("%lld",&x)
#define ss(s) scanf("%s",s)
#define pi(x) printf("%d\n",x)
#define pl(x) printf("%lld\n",x)
#define ps(s) printf("%s\n",s)
#define deb(x) cout << #x << "=" << x << endl
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define clr(x) memset(x, 0, sizeof(x))
#define mems(a,x) memset(a,x,sizeof(a))
#define sortall(x) sort(all(x))
#define tr(it, a) for(auto it = a.begin(); it != a.end(); it++)
#define PI 3.1415926535897932384626
typedef pair<int, int> pii;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpii;
typedef vector<pl> vpl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count());
int rng(int lim) {
uniform_int_distribution<int> uid(0, lim - 1);
return uid(rang);
}
const int mod = 998244353; //1000000007;
const int N = 3e5, M = N;
//vi g[N];
//int a[N];
ll find(vector<ll> &parent, ll a)
{
return (parent[a] = (parent[a] == a ? a : find(parent, parent[a])));
}
void Union(vector<ll> &parent, vector<ll> &parent2, vector<ll> &rank, vector<ll> &count, vector<ll> &sum, ll a, ll b)
{
a = find(parent, parent2[a]);
b = find(parent, parent2[b]);
if (a == b)
return;
if (rank[a] == rank[b])
{
rank[a]++;
}
if (rank[a] >= rank[b])
{
parent[b] = a;
sum[a] += sum[b];
count[a] += count[b];
}
else
{
parent[a] = b;
sum[b] += sum[a];
count[b] += count[a];
}
}
void move(vector<ll> &parent, vector<ll> &parent2, vector<ll> &count, vector<ll> &sum, ll a, ll b)
{
ll a1 = find(parent, parent2[a]);
b = find(parent, parent2[b]);
if (a == b)
return;
parent2[a] = b;
sum[b] += a;
count[b] += 1;
sum[a1] -= a;
count[a1] -= 1;
}
void solve()
{
ll n, m;
cin >> n >> m;
vector<ll> parent(n + 1), parent2(n + 1), count(n + 1), sum(n + 1), rank(n + 1);
fo(i, n + 1)
{
parent[i] = i;
parent2[i] = i;
count[i] = 1;
sum[i] = i;
rank[i] = 0;
}
fo(i, m)
{
ll x, y, z;
cin >> x >> y;
if (x == 1)
{
cin >> z;
/* code */
Union(parent, parent2, rank, count, sum, y, z);
}
else if (x == 2)
{
cin >> z;
move(parent, parent2, count, sum, y, z);
}
else
{
y = find(parent, parent2[y]);
cout << count[y] << " " << sum[y] << endl;
}
}
}
int main() {
//ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
srand(chrono::high_resolution_clock::now().time_since_epoch().count());
int t = 1;
// cin >> t;
while (t--) {
solve();
}
return 0;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKI2RlZmluZSBnYyBnZXRjaGFyX3VubG9ja2VkCiNkZWZpbmUgZm8oaSxuKSBmb3IoaW50IGk9MDtpPG47aSsrKQojZGVmaW5lIEZvKGksayxuKSBmb3IoaW50IGkgPSBrIDtpPD1uO2krKykKI2RlZmluZSBGTyhpLG4saykgZm9yKGludCBpID0gbjtpPj1rO2ktLSkKI2RlZmluZSBsbCBsb25nIGxvbmcKI2RlZmluZSBzaSh4KSAgIHNjYW5mKCIlZCIsJngpCiNkZWZpbmUgc2woeCkgICBzY2FuZigiJWxsZCIsJngpCiNkZWZpbmUgc3MocykgICBzY2FuZigiJXMiLHMpCiNkZWZpbmUgcGkoeCkgICBwcmludGYoIiVkXG4iLHgpCiNkZWZpbmUgcGwoeCkgICBwcmludGYoIiVsbGRcbiIseCkKI2RlZmluZSBwcyhzKSAgIHByaW50ZigiJXNcbiIscykKI2RlZmluZSBkZWIoeCkgY291dCA8PCAjeCA8PCAiP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gPSBmaW5kKHBhcmVudCwgcGFyZW50MlthXSk7CgogICAgYiA9IGZpbmQocGFyZW50LCBwYXJlbnQyW2JdKTsKCiAgICBpZiAoYSA9PSBiKQogICAgICAgIHJldHVybjsKCiAgICBwYXJlbnQyW2FdID0gYjsKCiAgICBzdW1bYl0gKz0gYTsKICAgIGNvdW50W2JdICs9IDE7CgogICAgc3VtW2ExXSAtPSBhOwogICAgY291bnRbYTFdIC09IDE7Cgp9Cgp2b2lkIHNvbHZlKCkKewogICAgbGwgbiwgbTsKICAgIGNpbiA+PiBuID4+IG07CgogICAgdmVjdG9yPGxsPiBwYXJlbnQobiArIDEpLCBwYXJlbnQyKG4gKyAxKSwgY291bnQobiArIDEpLCBzdW0obiArIDEpLCByYW5rKG4gKyAxKTsKICAgIGZvKGksIG4gKyAxKQogICAgewogICAgICAgIHBhcmVudFtpXSA9IGk7CiAgICAgICAgcGFyZW50MltpXSA9IGk7CiAgICAgICAgY291bnRbaV0gPSAxOwogICAgICAgIHN1bVtpXSA9IGk7CiAgICAgICAgcmFua1tpXSA9IDA7CiAgICB9CgogICAgZm8oaSwgbSkKICAgIHsKICAgICAgICBsbCB4LCB5LCB6OwogICAgICAgIGNpbiA+PiB4ID4+IHk7CiAgICAgICAgaWYgKHggPT0gMSkKICAgICAgICB7CiAgICAgICAgICAgIGNpbiA+PiB6OwogICAgICAgICAgICAvKiBjb2RlICovCiAgICAgICAgICAgIFVuaW9uKHBhcmVudCwgcGFyZW50MiwgcmFuaywgY291bnQsIHN1bSwgeSwgeik7CiAgICAgICAgfQogICAgICAgIGVsc2UgaWYgKHggPT0gMikKICAgICAgICB7CiAgICAgICAgICAgIGNpbiA+PiB6OwogICAgICAgICAgICBtb3ZlKHBhcmVudCwgcGFyZW50MiwgY291bnQsIHN1bSwgeSwgeik7CiAgICAgICAgfQogICAgICAgIGVsc2UKICAgICAgICB7CiAgICAgICAgICAgIHkgPSBmaW5kKHBhcmVudCwgcGFyZW50Mlt5XSk7CiAgICAgICAgICAgIGNvdXQgPDwgY291bnRbeV0gPDwgIiAiIDw8IHN1bVt5XSA8PCBlbmRsOwogICAgICAgIH0KICAgIH0KfQoKaW50IG1haW4oKSB7CiAgICAvL2lvc19iYXNlOjpzeW5jX3dpdGhfc3RkaW8oMCksIGNpbi50aWUoMCksIGNvdXQudGllKDApOwogICAgc3JhbmQoY2hyb25vOjpoaWdoX3Jlc29sdXRpb25fY2xvY2s6Om5vdygpLnRpbWVfc2luY2VfZXBvY2goKS5jb3VudCgpKTsKCiAgICBpbnQgdCA9IDE7CiAgICAvLyBjaW4gPj4gdDsKICAgIHdoaWxlICh0LS0pIHsKICAgICAgICBzb2x2ZSgpOwogICAgfQoKICAgIHJldHVybiAwOwp9CgoKCgo=