Problem: Baldman is a warp master. He possesses a unique ability — creating wormholes! Given two positions in space, Baldman can make a wormhole which makes it possible to move between them in both directions. Unfortunately, such operation isn't free for Baldman: each created wormhole makes him lose plenty of hair from his head.

Because of such extraordinary abilities, Baldman has caught the military's attention. He has been charged with a special task. But first things first.

The military base consists of several underground objects, some of which are connected with bidirectional tunnels. There necessarily exists a path through the tunnel system between each pair of objects. Additionally, exactly two objects are connected with surface. For the purposes of security, a patrol inspects the tunnel system every day: he enters one of the objects which are connected with surface, walks the base passing each tunnel at least once and leaves through one of the objects connected with surface. He can enter and leave either through the same object, or through different objects. The military management noticed that the patrol visits some of the tunnels multiple times and decided to optimize the process. Now they are faced with a problem: a system of wormholes needs to be made to allow of a patrolling which passes each tunnel exactly once. At the same time a patrol is allowed to pass each wormhole any number of times.

This is where Baldman comes to operation: he is the one to plan and build the system of the wormholes. Unfortunately for him, because of strict confidentiality the military can't tell him the arrangement of tunnels. Instead, they insist that his system of portals solves the problem for any arrangement of tunnels which satisfies the given condition. Nevertheless, Baldman has some information: he knows which pairs of objects he can potentially connect and how much it would cost him (in hair). Moreover, tomorrow he will be told which objects (exactly two) are connected with surface. Of course, our hero decided not to waste any time and calculate the minimal cost of getting the job done for some pairs of objects (which he finds likely to be the ones connected with surface). Help Baldman!

Input

First line of the input contains a single natural number n (2 ≤ n ≤ 100000) — the number of objects on the military base. The second line — one number m (1 ≤ m ≤ 200000) — the number of the wormholes Baldman can make. The following m lines describe the wormholes: each line contains three integer numbers a, b, c (1 ≤ a, b ≤ n, 1 ≤ c ≤ 100000) — the numbers of objects which can be connected and the number of hair Baldman has to spend to make this wormhole.

The next line contains one natural number q (1 ≤ q ≤ 100000) — the number of queries. Finally, the last q lines contain a description of one query each — a pair of numbers of different objects ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi). There could be more than one wormhole between a pair of objects.

Output

Your program should output q lines, one for each query. The i-th line should contain a single integer number — the answer for i-th query: the minimum cost (in hair) of a system of wormholes allowing the optimal patrol for any system of tunnels (satisfying the given conditions) if ai and bi are the two objects connected with surface, or "-1" if such system of wormholes cannot be made.

Examples

Input

2
1
1 2 3
1
1 2


Output

0


Input

3
1
1 2 3
2
1 2
1 3


Output

-1
3

Solution (in JAVA):

Solution:
import java.awt.Point;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;

public class E {

    static StreamTokenizer st;
    static class Sort implements Comparable<Sort> {
        int a, b, c;
        public int compareTo(Sort arg0) {
            return this.c-arg0.c;
        }
    }
    static boolean[]used;
    static ArrayList<Point>[]ages;
    static int[]comp, p, tin, tout;
    static int cnt_comp, L, timer;
    static int[][]up, max_w;
    public static void main(String[] args) throws IOException{
        st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
        PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
        int n = nextInt();
        int m = nextInt();
        Sort[]a = new Sort[m+1];
        for (int i = 1; i <= m; i++) {
            a[i] = new Sort();
            a[i].a = nextInt();
            a[i].b = nextInt();
            a[i].c = nextInt();
        }
        ages = new ArrayList[n+1];
        for (int i = 1; i <= n; i++) {
            ages[i] = new ArrayList<Point>();
        }
        Set<Long>set = new HashSet<Long>();
        Arrays.sort(a, 1, m+1);
        for (int i = 1; i <= m; i++) {
            long hash = (long)a[i].a*(n+1)+a[i].b;
            if (!set.contains(hash)) {
                set.add(hash);
                hash = (long)a[i].b*(n+1)+a[i].a;
                set.add(hash);
                ages[a[i].a].add(new Point(a[i].b, a[i].c));
                ages[a[i].b].add(new Point(a[i].a, a[i].c));
            }
        }
        int q = nextInt();
        cnt_comp = 0;
        used = new boolean[n+1];
        comp = new int[n+1];
        for (int i = 1; i <= n; i++) {
            if (!used[i]) {
                cnt_comp++;
                dfs(i);
            }
        }
        if (cnt_comp > 2) {
            for (int i = 1; i <= q; i++) {
                pw.println(-1);
            }
            pw.close();
            return;
        }
        for (int i = 1; i <= n; i++) {
            ages[i].clear();
        }
        p = new int[n+1];
        for (int i = 1; i <= n; i++) {
            p[i] = i;
        }
        Random rm = new Random();
        long sum = 0;
        for (int i = 1; i <= m; i++) {
            int p1 = get_fath(a[i].a);
            int p2 = get_fath(a[i].b);
            if (p1 != p2) {
                if (rm.nextInt() % 2==0)
                    p[p1] = p2;
                else
                    p[p2] = p1;
                ages[a[i].a].add(new Point(a[i].b, a[i].c));
                ages[a[i].b].add(new Point(a[i].a, a[i].c));
                sum += a[i].c;
            }
        }
        if (cnt_comp==2) {
            for (int i = 1; i <= q; i++) {
                int v1 = nextInt();
                int v2 = nextInt();
                if (comp[v1]==comp[v2])
                    pw.println(-1);
                else 
                    pw.println(sum);
            }
            pw.close();
            return;
        }
        while ((1 << L) <= n)
            L++;
        up = new int[n+1][L+1];
        max_w = new int[n+1][L+1];
        tin = new int[n+1];
        tout = new int[n+1];
        dfs(1, 1, 0);
        for (int i = 1; i <= q; i++) {
            int v1 = nextInt();
            int v2 = nextInt();
            int max = max_w_lca(v1, v2);
            pw.println(sum-max);
        }
        pw.close();
    }
    
    private static int max_w_lca(int a, int b) {
        int max1 = 0, max2 = 0;
        if (!upper(a, b)) {
            for (int i = L; i >= 0; i--) {
                if (!upper(up[a][i], b)) {
                    max1 = Math.max(max1, max_w[a][i]);
                    a = up[a][i];
                }
            }
            max1 = Math.max(max1, max_w[a][0]);
            a = up[a][0];
        }
        if (!upper(b, a)) {
            for (int i = L; i >= 0; i--) {
                if (!upper(up[b][i], a)) {
                    max2 = Math.max(max2, max_w[b][i]);
                    b = up[b][i];
                }
            }
            max2 = Math.max(max2, max_w[b][0]);
        }
        return Math.max(max1, max2);
    }

    private static boolean upper(int a, int b) {
        return tin[a] <= tin[b] && tout[a] >= tout[b];
    }

    private static void dfs(int v, int p, int w) {
        tin[v] = timer++;
        up[v][0] = p;
        max_w[v][0] = w;
        for (int i = 1; i <= L; i++) {
            up[v][i] = up[up[v][i-1]][i-1];
            max_w[v][i] = Math.max(max_w[v][i-1], max_w[up[v][i-1]][i-1]);
        }
        for (Point to : ages[v]) {
            if (to.x==p)
                continue;
            dfs(to.x, v, to.y);
        }
        tout[v] = timer++;
    }

    private static int get_fath(int v) {
        if (v==p[v])
            return v;
        return p[v] = get_fath(p[v]);
    }

    private static void dfs(int v) {
        used[v] = true;
        comp[v] = cnt_comp;
        for (Point to : ages[v]) {
            if (!used[to.x])
                dfs(to.x);
        }
    }

    private static int nextInt() throws IOException{
        st.nextToken();
        return (int) st.nval;
    }

}