using System;
using System.Collections.Generic;
using System.Collections;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;
using static System.Math;
using System.Numerics;
static class Program{
	const int mod=(int)1e9+7;
	static List<Tuple<int,int>>[] li;
	static bool[] b;
	static int[][] h;
	static int[][] z;
	static void Main(){
		Sc sc=new Sc();
		int[] s=sc.Ia;
		int n=s[0],m=s[1];
		li=new List<Tuple<int,int>>[n+1];
		h=new int[m][];
		z=new int[n+1][];
		for(int i=1;i<=n;i++){
			li[i]=new List<Tuple<int,int>>();
			z[i]=new int[n+1];
		}
		for(int i=0;i<m;i++){
			h[i]=sc.Ia;
		}
		Array.Sort(h,(u,v)=>u[2]-v[2]);
		Uf uf=new Uf(n+1);
		long c=0;
		for(int i = 0;uf.all!=2;i++) {
			if(!uf.Same(h[i][0],h[i][1])){
				uf.Union(h[i][0],h[i][1]);
				li[h[i][0]].Add(Tuple.Create(h[i][1],h[i][2]));
				li[h[i][1]].Add(Tuple.Create(h[i][0],h[i][2]));
				c+=h[i][2];
			}
		}
		for(int i=1;i<n;i++){
			b=new bool[n+1];
			Fu(i,i,0);
		}
		int q=sc.I;
		StringBuilder sb=new StringBuilder();
		for(int i = 0;i<q;i++) {
			var e=sc.Ia;
			sb.Append((c-z[e[0]][e[1]])+"\n");
		}
		Console.Write(sb);
	}
	static void Fu(int a,int g,int p){
		b[a]=true;
		z[a][g]=z[g][a]=p;
		for(int i=0;i<li[a].Count;i++){
			if(!b[li[a][i].Item1]){Fu(li[a][i].Item1,g,Max(p,li[a][i].Item2));}
		}
	}
}
public class Uf{
	private int[] arr,hi;
	public int all;
	public Uf(int n){
		arr=new int[n];
		hi=new int[n];
		all=n;
		for(int i=0;i<n;i++){arr[i]=-1;}
	}
	public void Union(int a,int b){
		a=Root(a);
		b=Root(b);
		if(a!=b){
			if(hi[a]<hi[b]){
				int v=b;
				b=a;
				a=v;
			}
			arr[a]+=arr[b];
			hi[a]=Math.Max(hi[a],hi[b]+1);
			arr[b]=a;
			all--;
		}
	}
	public bool Same(int a,int b){return Root(a)==Root(b);}
	public int Total(int a){return -arr[Root(a)];}
	public int Root(int a){return arr[a]<0?a:arr[a]=Root(arr[a]);}
}

public class Sc{
	public int I{get{return int.Parse(Console.ReadLine());}}
	public long L{get{return long.Parse(Console.ReadLine());}}
	public double D{get{return double.Parse(Console.ReadLine());}}
	public string S{get{return Console.ReadLine();}}
	public int[] Ia{get{return Array.ConvertAll(Console.ReadLine().Split(),int.Parse);}}
	public long[] La{get{return Array.ConvertAll(Console.ReadLine().Split(),long.Parse);}}
	public double[] Da{get{return Array.ConvertAll(Console.ReadLine().Split(),double.Parse);}}
	public string[] Sa{get{return Console.ReadLine().Split();}}
	public object[] Oa{get{return Console.ReadLine().Split();}}
	public int[] Ia2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),int.Parse);}}
	public int[] Ia3(int a){return Array.ConvertAll((a.ToString()+" "+Console.ReadLine()).Split(),int.Parse);}
	public int[] Ia3(bool a,int b,bool c,int d){return Array.ConvertAll(((a?b.ToString()+" ":"")+Console.ReadLine()+(c?" "+d.ToString():"")).Split(),int.Parse);}
	public long[] La2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),long.Parse);}}
	public long[] La3(int a){return Array.ConvertAll((a.ToString()+" "+Console.ReadLine()).Split(),long.Parse);}
	public long[] La3(bool a,int b,bool c,int d){return Array.ConvertAll(((a?b.ToString()+" ":"")+Console.ReadLine()+(c?" "+d.ToString():"")).Split(),long.Parse);}
	public T[] Arr<T>(int n,Func<T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f();}return a;}
	public T[] Arr<T>(int n,Func<int,T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(i);}return a;}
	public T[] Arr<T>(int n,Func<string[],T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(Console.ReadLine().Split());}return a;}
	public T[] Arr<T>(int n,Func<int,string[],T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(i,Console.ReadLine().Split());}return a;}
}

Measured memory_bytes: 112756