description: You have an array of integers (initially empty).
  
  You have to perform q queries. Each query is of one of two types: 
  
    * "1 x" — add the element x to the end of the array; 
    * "2 x y" — replace all occurrences of x in the array with y. 
  
  
  
  Find the resulting array after performing all the queries.
  
  Input
  
  The first line contains a single integer q (1 ≤ q ≤ 5 ⋅ 10^5) — the number of queries.
  
  Next q lines contain queries (one per line). Each query is of one of two types: 
  
    * "1 x" (1 ≤ x ≤ 5 ⋅ 10^5); 
    * "2 x y" (1 ≤ x, y ≤ 5 ⋅ 10^5). 
  
  
  
  It's guaranteed that there is at least one query of the first type.
  
  Output
  
  In a single line, print k integers — the resulting array after performing all the queries, where k is the number of queries of the first type.
  
  Examples
  
  Input
  
  
  7
  1 3
  1 1
  2 1 2
  1 2
  1 1
  1 2
  2 1 3
  
  
  Output
  
  
  3 2 2 3 2 
  
  Input
  
  
  4
  1 1
  1 2
  1 1
  2 2 2
  
  
  Output
  
  
  1 2 1 
  
  Input
  
  
  8
  2 1 4
  1 1
  1 4
  1 2
  2 2 4
  2 4 3
  1 2
  2 2 7
  
  
  Output
  
  
  1 3 3 7 
  
  Note
  
  In the first example, the array changes as follows:
  
  [] → [3] → [3, 1] → [3, 2] → [3, 2, 2] → [3, 2, 2, 1] → [3, 2, 2, 1, 2] → [3, 2, 2, 3, 2].
  
  In the second example, the array changes as follows:
  
  [] → [1] → [1, 2] → [1, 2, 1] → [1, 2, 1].
  
  In the third example, the array changes as follows:
  
  [] → [] → [1] → [1, 4] → [1, 4, 2] → [1, 4, 4] → [1, 3, 3] → [1, 3, 3, 2] → [1, 3, 3, 7].
solution: import java.io.*;
  import java.util.*;
  
  public class E {
      static int n;
      static int[][] input;
      public static void main(String[] args) throws IOException{
          FastIO file = new FastIO();
          n = file.nextInt();
          input = new int[n][3];
          int count = 0;
          for (int i=0; i<n; i++){
              int t = file.nextInt();
              if (t == 1){
                  input[i] = new int[] {t, file.nextInt(), count++};
              }else{
                  input[i] = new int[] {t, file.nextInt(), file.nextInt()};
              }
          }
          DSU dsu = new DSU(count);
          HashMap<Integer, Integer> map = new HashMap<>();
          int[] ans = new int[n];
          for (int i=0; i<n; i++){
              if (input[i][0] == 1){
                  if (map.containsKey(input[i][1])){
                      dsu.union(map.get(input[i][1]), input[i][2]);
                  }else{
                      map.put(input[i][1], input[i][2]);
                  }
                  ans[dsu.find(input[i][2])] = input[i][1];
              }else{
                  if (input[i][1] == input[i][2]){
                      continue;
                  }
                  if (map.containsKey(input[i][1])){
                      if (map.containsKey(input[i][2])){
                          dsu.union(map.get(input[i][1]), map.get(input[i][2]));
                      }else{
                          map.put(input[i][2], map.get(input[i][1]));
                      }
                      ans[dsu.find(map.get(input[i][2]))] = input[i][2];
                      map.remove(input[i][1]);
                  }
              }
          }
          for (int i=0; i<count; i++){
              file.print(ans[dsu.find(i)]+ " ");
          }
          file.close();
      }
      static class DSU {
          int[] parent, depth;
          public DSU(int n){
              parent = new int[n];
              depth = new int[n];
              for (int i=0; i<n; i++){
                  depth[i] = 1;
                  parent[i] = i;
              }
          }
          public int find(int a){
              if (parent[a] == a){
                  return a;
              }
              return parent[a] = find(parent[a]); // Path compression
          }
  
          // Keep the tree depth small
          public void union(int a, int b){
              a = find(a);
              b = find(b);
              if (a == b){
                  // Same component
                  return;
              }
              if (depth[a] < depth[b]){
                  // Point the smaller depth to greater depth
                  parent[a] = b;
              }else{
                  parent[b] = a;
                  depth[a] = Math.max(depth[a], depth[b]+1); // When depth[a] = depth[b]. the unioned depth is depth[b]+1
              }
          }
      }
      static class FastIO extends PrintWriter {
          private InputStream stream;
          private byte[] buf = new byte[1<<16];
          private int curChar, numChars;
      
          public FastIO() { this(System.in,System.out); }
          public FastIO(InputStream i, OutputStream o) {
              super(o);
              stream = i;
          }
          public FastIO(String i, String o) throws IOException {
              super(new FileWriter(o));
              stream = new FileInputStream(i);
          }
          public FastIO(String i) throws IOException {
              super(System.out);
              stream = new FileInputStream(i);
          }
          // throws InputMismatchException() if previously detected end of file
          private int nextByte() {
              if (numChars == -1) throw new InputMismatchException();
              if (curChar >= numChars) {
                  curChar = 0;
                  try {
                      numChars = stream.read(buf);
                  } catch (IOException e) {
                      throw new InputMismatchException();
                  }
                  if (numChars == -1) return -1; // end of file
              }
              return buf[curChar++];
          }
          // to read in entire lines, replace c <= ' '
          // with a function that checks whether c is a line break
          public String next() {
              int c; do { c = nextByte(); } while (c <= ' ');
              StringBuilder res = new StringBuilder();
              do { res.appendCodePoint(c); c = nextByte(); } while (c > ' ');
              return res.toString();
          }
          public int nextInt() {
              int c; do { c = nextByte(); } while (c <= ' ');
              int sgn = 1; if (c == '-') { sgn = -1; c = nextByte(); }
              int res = 0;
              do {
                  if (c < '0' || c > '9')
                      throw new InputMismatchException();
                  res = 10*res+c-'0';
                  c = nextByte();
              } while (c > ' ');
              return res * sgn;
          }
          public long nextLong() {
              int c; do { c = nextByte(); } while (c <= ' ');
              int sgn = 1; if (c == '-') { sgn = -1; c = nextByte(); }
              long res = 0;
              do {
                  if (c < '0' || c > '9')
                      throw new InputMismatchException();
                  res = 10*res+c-'0';
                  c = nextByte();
              } while (c > ' ');
              return res * sgn;
          }
          public double nextDouble() { return Double.parseDouble(next()); }
      }
  }