package main import ( "bufio" "fmt" "os" "strconv" ) func find(parent []int, i int) int { if parent[i] < 0 { return i } parent[i] = find(parent, parent[i]) return parent[i] } func unite(parent []int, i, j int) { i = find(parent, i) j = find(parent, j) if i == j { return } parent[j] += parent[i] parent[i] = j } func main() { defer flush() N := readInt() Q := readInt() T := make([]int, Q+1) A := make([]int, Q+1) B := make([]int, Q+1) for i := 0; i < Q; i++ { T[i] = readInt() A[i] = readInt() B[i] = readInt() } t := map[int]int{} for i := 0; i < Q; i++ { if T[i] == 3 { t[A[i]] = 0 } } u := map[int][]int{} for k := range t { u[k] = []int{k} } parent := make([]int, N+1) for i := 0; i < N+1; i++ { parent[i] = -1 } for i := 0; i < Q; i++ { if T[i] == 1 { a := find(parent, A[i]) b := find(parent, B[i]) as := u[a] bs := u[b] delete(u, a) delete(u, b) unite(parent, A[i], B[i]) u[find(parent, A[i])] = append(as, bs...) } else if T[i] == 2 { for _, v := range u[find(parent, A[i])] { t[v] += B[i] } } else if T[i] == 3 { println(t[A[i]]) } } } const ( ioBufferSize = 1 * 1024 * 1024 // 1 MB ) var stdinScanner = func() *bufio.Scanner { result := bufio.NewScanner(os.Stdin) result.Buffer(make([]byte, ioBufferSize), ioBufferSize) result.Split(bufio.ScanWords) return result }() func readString() string { stdinScanner.Scan() return stdinScanner.Text() } func readInt() int { result, err := strconv.Atoi(readString()) if err != nil { panic(err) } return result } var stdoutWriter = bufio.NewWriter(os.Stdout) func flush() { stdoutWriter.Flush() } func printf(f string, args ...interface{}) (int, error) { return fmt.Fprintf(stdoutWriter, f, args...) } func println(args ...interface{}) (int, error) { return fmt.Fprintln(stdoutWriter, args...) }