Problem:
Score : 400 points

Problem Statement

You have a sequence A composed of N positive integers: A_{1}, A_{2}, \cdots, A_{N}.

You will now successively do the following Q operations:

In the i-th operation, you replace every element whose value is B_{i} with C_{i}.

For each i (1 \leq i \leq Q), find S_{i}: the sum of all elements in A just after the i-th operation.

Constraints

All values in input are integers.

1 \leq N, Q, A_{i}, B_{i}, C_{i} \leq 10^{5}

B_{i} \neq C_{i}

Input

Input is given from Standard Input in the following format:

N
A_{1} A_{2} \cdots A_{N}
Q
B_{1} C_{1}
B_{2} C_{2}
\vdots
B_{Q} C_{Q}

Output

Print Q integers S_{i} to Standard Output in the following format:

S_{1}
S_{2}
\vdots
S_{Q}

Note that S_{i} may not fit into a 32-bit integer.

Sample Input 1

4
1 2 3 4
3
1 2
3 4
2 4

Sample Output 1

11
12
16

Initially, the sequence A is 1,2,3,4.

After each operation, it becomes the following:

2, 2, 3, 4

2, 2, 4, 4

4, 4, 4, 4

Sample Input 2

4
1 1 1 1
3
1 2
2 1
3 5

Sample Output 2

8
4
4

Note that the sequence A may not contain an element whose value is B_{i}.

Sample Input 3

2
1 2
3
1 100
2 100
100 1000

Sample Output 3

102
200
2000

Solution in Java (OpenJDK 1.8.0) (judge verdict: Time Limit Exceeded)



import java.io.*;
import java.util.*;
 
 
class Main{
   public static void main(String[] args) {
      FastScanner sc = new FastScanner();
      out = new PrintWriter(new BufferedOutputStream(System.out));
      int n = sc.nextInt();
      
      int[] arr=new int[n];
      for(int i=0;i<n;i++) {
        arr[i] = sc.nextInt();
      }
      int q=sc.nextInt();
     
      
      SegmentTree tree = new SegmentTree(arr);
     
      for(int i=0;i<q;i++) {
        int b = sc.nextInt();
        int c = sc.nextInt();
        for(int j=0;j<n;j++) {
          if(arr[j] == b) {
            tree.update(j, c);
            arr[j] =c;
          }
        }
        out.println(tree.sum(0,n-1));  
      }
      out.close();
   }
   
  
   static class SegmentTree{
     int[] tree;
     int n;
     public SegmentTree(int[] nums) {
         if (nums.length > 0) {
             n = nums.length;
             tree = new int[n * 2];
             buildTree(nums);
         }
     }
     private void buildTree(int[] nums) {
         for (int i = n, j = 0;  i < 2 * n; i++,  j++)
             tree[i] = nums[j];
         for (int i = n - 1; i > 0; --i)
             tree[i] = tree[i * 2] + tree[i * 2 + 1];
     }
     void update(int pos, int val) {
       pos += n;
       tree[pos] = val;
       while (pos > 0) {
           int left = pos;
           int right = pos;
           if (pos % 2 == 0) {
               right = pos + 1;
           } else {
               left = pos - 1;
           }
           // parent is updated after child is updated
           tree[pos / 2] = tree[left] + tree[right];
           pos /= 2;
       }
     }
    
     
     public long sum(int l, int r) {
       // get leaf with value 'l'
       l += n;
       // get leaf with value 'r'
       r += n;
       long sum = 0;
       while (l <= r) {
           if ((l % 2) == 1) {
              sum += tree[l];
              l++;
           }
           if ((r % 2) == 0) {
              sum += tree[r];
              r--;
           }
           l /= 2;
           r /= 2;
       }
       return sum;
   }
     @Override
     public String toString() {
       return java.util.Arrays.toString(tree);
     }
     
   }
       

   //-----------PrintWriter for faster output---------------------------------
   public static PrintWriter out;
      
   //-----------MyScanner class for faster input----------
   public static class FastScanner {
      BufferedReader br;
      StringTokenizer st;
 
      public FastScanner() {
         br = new BufferedReader(new InputStreamReader(System.in));
      }
 
      String next() {
          while (st == null || !st.hasMoreElements()) {
              try {
                  st = new StringTokenizer(br.readLine());
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
          return st.nextToken();
      }
 
      int nextInt() {
          return Integer.parseInt(next());
      }
 
      long nextLong() {
          return Long.parseLong(next());
      }
 
      double nextDouble() {
          return Double.parseDouble(next());
      }
 
      String nextLine(){
          String str = "";
      try {
         str = br.readLine();
      } catch (IOException e) {
         e.printStackTrace();
      }
      return str;
      }

   }
}