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.util.*;
import java.io.*;
public class b {
public static void main(String[] args) throws IOException
{
    input.init(System.in);
    PrintWriter out = new PrintWriter(System.out);
    int n = input.nextInt(), m = input.nextInt();
    Edge[] es = new Edge[m];
    for(int i = 0; i<m; i++)
    {
        int a = input.nextInt()-1, b = input.nextInt()-1, c = input.nextInt();
        es[i] = new Edge(a, b, c);
    }
    Arrays.sort(es);
    long cost = 0;
    int count = 0;
    DisjointSet ds = new DisjointSet(n);
    ArrayList<Edge>[] g = new ArrayList[n];
    for(int i = 0; i<n; i++) g[i] = new ArrayList<Edge>();
    for(Edge e : es)
    {
        if(ds.find(e.a) == ds.find(e.b)) continue;
        count++;
        cost += e.c;
        ds.union(e.a, e.b);
        Edge fromA = new Edge(e.a, e.b, e.c);
        fromA.to = e.b;
        Edge fromB = new Edge(e.a, e.b, e.c);
        fromB.to = e.a;
        g[e.a].add(fromA);
        g[e.b].add(fromB);
    }
    LCT lct = new LCT(n, Node.MAX);
    b.g = new ArrayList[n];
    b.n = n;
    for(int i = 0; i<n; i++)b.g[i] = new ArrayList<Integer>();

    Queue<Integer> q = new LinkedList<Integer>();
    boolean[] seen = new boolean[n];
    for(int i= 0; i<n; i++)
    {
        if(seen[i]) continue;
        q.add(i);
        seen[i] = true;
        while(!q.isEmpty())
        {
            int at = q.poll();
            for(Edge e : g[at])
            {
                int to = e.to;
                if(seen[to]) continue;
                seen[to] = true;
                lct.link(to, at);
                q.add(to);
                lct.add(to, to, e.c);
                b.g[at].add(to);
            }
        }
    }
    tree();
    binaryLift();
    int Q = input.nextInt();
    for(int  qq= 0; qq<Q; qq++)
    {
        int a = input.nextInt()-1, b = input.nextInt()-1;
        if(count < n-2)out.println(-1);
        else if(count == n-2)
        {
            if(lct.connected(a, b)) out.println(-1);
            else out.println(cost);
        }
        else
        {
            int lca = lca(a,  b);
            long ans = 0;
            if(a != lca)
            {
                int diff = depths[a] - depths[lca];
                ans = Math.max(ans, lct.sum(a, getParent(a, diff - 1)));
            }
            if(b != lca)
            {
                int diff = depths[b] - depths[lca];
                ans = Math.max(ans, lct.sum(b, getParent(b, diff - 1)));
            }
            out.println(cost - ans);
        }
    }
    out.close();
}
static class Node
{
    static int SUM = 0, MIN = 1, MAX = 2;
    Node left, right, parent;
    int size;
    int id;
    boolean revert;
    int type;
    long delta, nodeValue, subtreeValue;
    Node(int i, int t)
    {
        id = i;
        type = t;
    }
    boolean isRoot()
    {
        if(parent == null) return true;
        if(parent.left == this || parent.right == this) return false;
        return true;
    }
    void push()
    {
        if (revert)
        {
            revert = false;
            Node t = left;
            left = right;
            right = t;
            if (left != null) left.revert = !left.revert;
            if (right != null) right.revert = !right.revert;
        }
        if (left != null) left.delta += delta;
        if (right != null) right.delta += delta;
        nodeValue += delta;
        if(type == SUM) subtreeValue += delta * size;
        else subtreeValue += delta;
        delta = 0;
    }
    void update()
    {
        size = 1;
        subtreeValue = nodeValue;
        if (left != null)
        {
            if(type == SUM) subtreeValue += left.subtreeValue + left.delta * left.size;
            else if(type == MIN) subtreeValue = Math.min(subtreeValue, left.subtreeValue + left.delta);
            else if(type == MAX) subtreeValue = Math.max(subtreeValue, left.subtreeValue + left.delta);
            size += left.size;
        }
        if (right != null)
        {
            if(type == SUM) subtreeValue += right.subtreeValue + right.delta * right.size;
            else if(type == MIN) subtreeValue = Math.min(subtreeValue, right.subtreeValue + right.delta);
            else if(type == MAX) subtreeValue = Math.max(subtreeValue, right.subtreeValue + right.delta);
            size += right.size;
        }
    }
}
static int[] depths;
static int[] ps;
static int[][] bl;
static int n;
static ArrayList<Integer>[] g;
static void tree()
{
    ps = new int[n];
    depths = new int[n];
    boolean[] vis = new boolean[n];
    vis[0] = true;
    ps[0] = -1;
    Queue<Integer> q = new LinkedList<Integer>();
    q.add(0);
    ArrayList<Integer>[] tree = new ArrayList[n];
    for(int i = 0; i<n; i++) tree[i] = new ArrayList<Integer>();
    while(!q.isEmpty())
    {
        int at = q.poll();
        for(int e : g[at])
        {
            if(!vis[e])
            {
                tree[at].add(e);
                vis[e] = true;
                q.add(e);
                ps[e] = at;
                depths[e] = 1 + depths[at];
            }
        }
    }
    g = tree;
}
static int lca(int a, int b)
{
    if(depths[a] < depths[b])
    {
        int temp = a; a = b; b = temp;
    }
    int log = 0;
    for(log = 1; (1<<log) <= depths[a]; log++);
    log--;
    for(int i = log; i >= 0; i--)
        if(depths[a] - (1<<i) >= depths[b])
            a = bl[i][a];
    if(a == b) return a;
    for(int i = log; i>=0; i--)
        if(bl[i][a] != -1 && bl[i][a] != bl[i][b])
        {
            a = bl[i][a];
            b = bl[i][b];
        }
    return ps[a];
}
static int dist(int a, int b)
{
    int lca = lca(a, b);
    return depths[a] + depths[b] - 2 * depths[lca];
}
/*
 * Returns the ancestor of at that is distance p away.
 */
static int getParent(int at, int p)
{
    //System.out.println(at+" "+p);
    if(p == 0) return at;
    int log = Integer.numberOfTrailingZeros(Integer.highestOneBit(p));
    return getParent(bl[log][at], p - (1<<log));
}
static void binaryLift()
{
    bl = new int[Integer.numberOfTrailingZeros(Integer.highestOneBit(n)) + 1][n];
    for(int i = 0; i<n; i++) bl[0][i] = ps[i];
    for(int i = 1; i < bl.length; i++)
    {
        Arrays.fill(bl[i], -1);
        for(int j = 0; j<n; j++)
            if(bl[i-1][j] >= 0)
                bl[i][j] = bl[i-1][bl[i-1][j]];
    }
}
static class LCT
{
    Node[] nodes;
    LCT(int n, int type)
    {
        nodes = new Node[n];
        for(int i = 0; i<n; i++) nodes[i] = new Node(i, type);
    }
    void connect(Node ch, Node p, int type)
    {
        if (ch != null) ch.parent = p;
        if(type == 0) p.left = ch;
        else if(type == 1) p.right = ch;
    }
    void rotate(Node x)
    {
        Node p = x.parent, g = p.parent;
        boolean isRootP = p.isRoot();
        boolean leftChildX = (x == p.left);
    
        // create 3 edges: (x.r(l),p), (p,x), (x,g)
        connect(leftChildX ? x.right : x.left, p, leftChildX ? 0 : 1);
        connect(p, x, leftChildX ? 1 : 0);
        connect(x, g, isRootP ? 2 : (p == g.left ? 0 : 1));
        p.update();
    }
    void splay(Node x)
    {
        while (!x.isRoot())
        {
            Node p = x.parent, g = p.parent;
            if (!p.isRoot()) g.push();
            p.push();
            x.push();
            if (!p.isRoot()) rotate((x == p.left) == (p == g.left) ? p/*zig-zig*/ : x/*zig-zag*/);
            rotate(x);
        }
        x.push();
        x.update();
    }
    /*
     * Makes q the root of its tree of splay trees.
     */
    Node access(Node q)
    {
        Node r = null;
        for(Node p = q; p != null; p = p.parent)
        {
            splay(p);
            p.left = r;
            p.update();
            r = p;
        }
        splay(q);
        return r;
    }
    void makeRoot(Node x)
    {
        access(x);
        x.revert = !x.revert;
    }
    /*
     * Makes qq the parent of pp.
     */
    void link(int pp, int qq)
    {
        Node p = nodes[pp], q = nodes[qq];
        //if (connected(p, q)) throw new RuntimeException("error: x and y are already connected");
        makeRoot(p);
        p.parent = q;
    }
    int findRoot(Node p)
    {
        access(p);
        while(p.right != null) p = p.right;
        splay(p);
        return p.id;
    }
    void cut(int pp, int qq)
    {
        Node p = nodes[pp], q = nodes[qq];
        makeRoot(p);
        access(q);
        //if (q.right != p || p.left != null || p.right != null) throw new RuntimeException("error: no edge (x,y)");
        q.right.parent = null;
        q.right = null;
    }
    long sum(int from, int to)
    {
        makeRoot(nodes[from]);
        access(nodes[to]);
        return nodes[to].subtreeValue;
    }
    void add(int from, int to, int delta)
    {
        makeRoot(nodes[from]);
        access(nodes[to]);
        nodes[to].delta += delta;
    }
    boolean connected(int pp, int qq)
    {
        Node p = nodes[pp], q = nodes[qq];
        if(p == q) return true;
        access(p);
        access(q);
        return p.parent != null;
    }
    int lca(int p, int q) {
        //if (findRoot(x) != findRoot(y)) throw new RuntimeException("error: x and y are not connected");
        access(nodes[p]);
        return access(nodes[q]).id;
    }
}
public static class DisjointSet
{
    int[] map; //negative if root, more negative means bigger set; if nonnegative, then it indicates the parent 
    public DisjointSet(int n)
    {
        map = new int[n+1];
        Arrays.fill(map, -1);
    }
    public int find(int x)
    {
        if(map[x] < 0)
            return x;
        else
        {
            map[x] = find(map[x]);
            return map[x];
        }
    }
    public void union(int a, int b)
    {
        int roota = find(a), rootb = find(b);
        if(roota == rootb)
            return;
        if(map[roota] < map[rootb])
        {
            map[roota] += map[rootb]; //add the sizes
            map[rootb] = roota; //connect the smaller to the bigger
        }
        else
        {
            map[rootb] += map[roota];
            map[roota] = rootb;
        }
    }
}
static class Edge implements Comparable<Edge>
{
    int a, b, c, to;
    public Edge(int aa, int bb, int cc)
    {
        a = aa; b = bb; c = cc;
    }
    @Override
    public int compareTo(Edge o) {
        // TODO Auto-generated method stub
        return c - o.c;
    }
}
public static class input {
    static BufferedReader reader;
    static StringTokenizer tokenizer;

    /** call this method to initialize reader for InputStream */
    static void init(InputStream input) {
        reader = new BufferedReader(
                     new InputStreamReader(input) );
        tokenizer = new StringTokenizer("");
    }

    /** get next word */
    static String next() throws IOException {
        while ( ! tokenizer.hasMoreTokens() ) {
            //TODO add check for eof if necessary
            tokenizer = new StringTokenizer(
                   reader.readLine() );
        }
        return tokenizer.nextToken();
    }

    static int nextInt() throws IOException {
        return Integer.parseInt( next() );
    }
    
    static double nextDouble() throws IOException {
        return Double.parseDouble( next() );
    }
    static long nextLong() throws IOException {
        return Long.parseLong( next() );
    }
}
}