//YA HOO
#include <bits/stdc++.h>
#define B begin()
#define E end()
#define RE resize
#define mP make_pair
#define pB push_back
#define X first
#define Y second
#define S size()
#define ST setprecision
const int MAXN = 100 * 1000 + 11;
typedef long long ll;
using namespace std;
int mark[MAXN],D[MAXN],par[MAXN][18],par1[MAXN][18];
vector <pair<int,int> > adj[MAXN],a[MAXN];
vector <pair<int,pair<int,int> > > b,ar_a;
struct Type_DSU
{
	int size[MAXN],par[MAXN];
	void make_set(int x)
	{
		size[x] = 0;
		par[x] = x;
	}
	int findPar(int x)
	{
		return par[x] = (par[x] == x ? x : findPar(par[x]));
	}
	void merge(int x, int y)
	{
		x = findPar(x);
		y = findPar(y);
		if(x == y)
			return;
		if(size[y] > size[x])
			swap(x, y);
		par[y] = x;
		size[x] += (size[x] == size[y]);
		return;
	}
}DSU;
int dfs(int x)
{
	mark[x]=1;
	for(int i=0;i<adj[x].S;i++)
	{
		int pesar=adj[x][i].X;
		if(mark[pesar])
			continue;
		D[pesar]=D[x]+1;
		par[pesar][0]=x;
		par1[pesar][0]=adj[x][i].Y;
		for(int j=1;j<18;j++)
		{
			par[pesar][j]=par[par[pesar][j-1]][j-1];
			par1[pesar][j]=max(par1[pesar][j-1],par1[par[pesar][j-1]][j-1]);
		}
		dfs(pesar);
	}
	return 0;
}
pair<int,int> parent(int x,int d)
{
	int y=0;
	for(int i=0;i<18;i++)
		if((d>>i)&1)
		{
			y=max(y,par1[x][i]);
			x=par[x][i];
		}
	return mP(x,y);
}
int lca(int x,int y)
{
	int z=0;
	if(D[x]>D[y])
		swap(x,y);
	z=parent(y,D[y]-D[x]).Y;
	y=parent(y,D[y]-D[x]).X;
	if(x==y)
	{
		return z;
	}
	for(int i=17;i>=0;i--)
		if(par[x][i]!=par[y][i])
		{
			z=max(z,par1[x][i]);
			z=max(z,par1[y][i]);
			x=par[x][i];
			y=par[y][i];
		}
	z=max(z,par1[x][0]);
	z=max(z,par1[x][0]);
	cout<<"##"<<par[x][0]<<endl;
	return z;
}
int main()
{
	ios_base::sync_with_stdio(false);
	ll n,m,q,s=0,s1=0;
	cin>>n>>m;
	for(int i=0;i<m;i++)
	{
		int t1,t2,t3;
		cin>>t1>>t2>>t3;
		ar_a.pB(mP(t3,mP(t1,t2)));
		adj[t1].pB(mP(t2,t3));
		adj[t2].pB(mP(t1,t3));
	}
	sort(ar_a.B,ar_a.E);
	for(int i=1;i<=n;i++)
		DSU.make_set(i);
	for(int i=0;i<ar_a.S;i++)
	{
		if(DSU.findPar(ar_a[i].Y.X)!=DSU.findPar(ar_a[i].Y.Y))
		{
			DSU.merge(ar_a[i].Y.X,ar_a[i].Y.Y);
			s++;
			s1+=ar_a[i].X;
		}
	}
	dfs(1);
	cin>>q;
	for(int i=0;i<q;i++)
	{
		int t1,t2;
		cin>>t1>>t2;
		if(s<n-2)
			cout<<-1;
		else if(s==n-2)
		{
			if(DSU.findPar(t1)!=DSU.findPar(t2))
				cout<<s1;
			else
				cout<<-1;
		}
		else
		{
			cout<<s1-lca(t1,t2);
		}
		cout<<endl;
	}
	return 0;

}


//YA HOO
#include <bits/stdc++.h>
#define B begin()
#define E end()
#define RE resize
#define mP make_pair
#define pB push_back
#define X first
#define Y second
#define S size()
#define ST setprecision
const int MAXN = 100 * 1000 + 11;
typedef long long ll;
using namespace std;
int mark[MAXN],D[MAXN],par[MAXN][18],par1[MAXN][18];
vector <pair<int,int> > adj[MAXN],adj1[MAXN],a[MAXN];
vector <pair<int,pair<int,int> > > b,ar_a;
struct Type_DSU
{
	int size[MAXN],par2[MAXN];
	void make_set(int x)
	{
		size[x] = 0;
		par2[x] = x;
	}
	int findPar(int x)
	{
		return par2[x] = (par2[x] == x ? x : findPar(par2[x]));
	}
	void merge(int x, int y)
	{
		x = findPar(x);
		y = findPar(y);
		if(x == y)
			return;
		if(size[y] > size[x])
			swap(x, y);
		par2[y] = x;
		size[x] += (size[x] == size[y]);
		return;
	}
}DSU;
int dfs(int x)
{
	mark[x]=1;
	for(int i=0;i<adj[x].S;i++)
	{
		int pesar=adj[x][i].X;
		if(mark[pesar])
			continue;
		D[pesar]=D[x]+1;
		par[pesar][0]=x;
		par1[pesar][0]=adj[x][i].Y;
		for(int j=1;j<18;j++)
		{
			par[pesar][j]=par[par[pesar][j-1]][j-1];
			par1[pesar][j]=max(par1[pesar][j-1],par1[par[pesar][j-1]][j-1]);
		}
		dfs(pesar);
	}
	return 0;
}
pair<int,int> parent(int x,int d)
{
	int y=0;
	for(int i=0;i<18;i++)
		if((d>>i)&1)
		{
			y=max(y,par1[x][i]);
			x=par[x][i];
		}
	return mP(x,y);
}
int lca(int x,int y)
{
	int z=0;
	if(D[x]>D[y])
		swap(x,y);
	z=parent(y,D[y]-D[x]).Y;
	y=parent(y,D[y]-D[x]).X;
	if(x==y)
	{
		return z;
	}
	for(int i=17;i>=0;i--)
		if(par[x][i]!=par[y][i])
		{
			z=max(z,par1[x][i]);
			z=max(z,par1[y][i]);
			x=par[x][i];
			y=par[y][i];
		}
	z=max(z,par1[x][0]);
	z=max(z,par1[y][0]);
	return z;
}
int main()
{
	ios_base::sync_with_stdio(false);
	ll n,m,q,s=0,s1=0;
	cin>>n>>m;
	for(int i=0;i<m;i++)
	{
		int t1,t2,t3;
		cin>>t1>>t2>>t3;
		ar_a.pB(mP(t3,mP(t1,t2)));
		adj1[t1].pB(mP(t2,t3));
		adj1[t2].pB(mP(t1,t3));
	}
	sort(ar_a.B,ar_a.E);
	for(int i=1;i<=n;i++)
		DSU.make_set(i);
	for(int i=0;i<ar_a.S;i++)
	{
		if(DSU.findPar(ar_a[i].Y.X)!=DSU.findPar(ar_a[i].Y.Y))
		{
			DSU.merge(ar_a[i].Y.X,ar_a[i].Y.Y);
			s++;
			s1+=ar_a[i].X;
			adj[ar_a[i].Y.X].pB(mP(ar_a[i].Y.Y,ar_a[i].X));
			adj[ar_a[i].Y.Y].pB(mP(ar_a[i].Y.X,ar_a[i].X));
		}
	}
	dfs(1);
	for(int i=0;i<18;i++)
		par[1][i]=1;
	cin>>q;
	for(int i=0;i<q;i++)
	{
		int t1,t2;
		cin>>t1>>t2;
		if(s<n-2)
			cout<<-1;
		else if(s==n-2)
		{
			if(DSU.findPar(t1)!=DSU.findPar(t2))
				cout<<s1;
			else
				cout<<-1;
		}
		else
		{
			cout<<s1-lca(t1,t2);
		}
		cout<<endl;
	}
	return 0;

}


//YA HOO
#include <bits/stdc++.h>
#define B begin()
#define E end()
#define RE resize
#define mP make_pair
#define pB push_back
#define X first
#define Y second
#define S size()
#define ST setprecision
const int MAXN = 100 * 1000 + 11;
typedef long long ll;
using namespace std;
int mark[MAXN],D[MAXN],par[MAXN][18],par1[MAXN][18];
vector <pair<int,int> > adj[MAXN],a[MAXN];
vector <pair<int,pair<int,int> > > b,ar_a;
struct Type_DSU
{
	int size[MAXN],par2[MAXN];
	void make_set(int x)
	{
		size[x] = 0;
		par2[x] = x;
	}
	int findPar(int x)
	{
		return par2[x] = (par2[x] == x ? x : findPar(par2[x]));
	}
	void merge(int x, int y)
	{
		x = findPar(x);
		y = findPar(y);
		if(x == y)
			return;
		if(size[y] > size[x])
			swap(x, y);
		par2[y] = x;
		size[x] += (size[x] == size[y]);
		return;
	}
}DSU;
int dfs(int x)
{
	mark[x]=1;
	for(int i=0;i<adj[x].S;i++)
	{
		int pesar=adj[x][i].X;
		if(mark[pesar])
			continue;
		D[pesar]=D[x]+1;
		par[pesar][0]=x;
		par1[pesar][0]=adj[x][i].Y;
		for(int j=1;j<18;j++)
		{
			par[pesar][j]=par[par[pesar][j-1]][j-1];
			par1[pesar][j]=max(par1[pesar][j-1],par1[par[pesar][j-1]][j-1]);
		}
		dfs(pesar);
	}
	return 0;
}
pair<int,int> parent(int x,int d)
{
	int y=0;
	for(int i=0;i<18;i++)
		if((d>>i)&1)
		{
			y=max(y,par1[x][i]);
			x=par[x][i];
		}
	return mP(x,y);
}
int lca(int x,int y)
{
	int z=0;
	if(D[x]>D[y])
		swap(x,y);
	z=parent(y,D[y]-D[x]).Y;
	y=parent(y,D[y]-D[x]).X;
	if(x==y)
	{
		return z;
	}
	for(int i=17;i>=0;i--)
		if(par[x][i]!=par[y][i])
		{
			z=max(z,par1[x][i]);
			z=max(z,par1[y][i]);
			x=par[x][i];
			y=par[y][i];
		}
	z=max(z,par1[x][0]);
	z=max(z,par1[y][0]);
	return z;
}
int main()
{
	ios_base::sync_with_stdio(false);
	ll n,m,q,s=0,s1=0;
	cin>>n>>m;
	for(int i=0;i<m;i++)
	{
		int t1,t2,t3;
		cin>>t1>>t2>>t3;
		ar_a.pB(mP(t3,mP(t1,t2)));
		adj[t1].pB(mP(t2,t3));
		adj[t2].pB(mP(t1,t3));
	}
	sort(ar_a.B,ar_a.E);
	for(int i=1;i<=n;i++)
		DSU.make_set(i);
	for(int i=0;i<ar_a.S;i++)
	{
		if(DSU.findPar(ar_a[i].Y.X)!=DSU.findPar(ar_a[i].Y.Y))
		{
			DSU.merge(ar_a[i].Y.X,ar_a[i].Y.Y);
			s++;
			s1+=ar_a[i].X;
		}
	}
	dfs(1);
	for(int i=0;i<18;i++)
		par[1][i]=1;
	cin>>q;
	for(int i=0;i<q;i++)
	{
		int t1,t2;
		cin>>t1>>t2;
		if(s<n-2)
			cout<<-1;
		else if(s==n-2)
		{
			if(DSU.findPar(t1)!=DSU.findPar(t2))
				cout<<s1;
			else
				cout<<-1;
		}
		else
		{
			cout<<s1-lca(t1,t2);
		}
		cout<<endl;
	}
	return 0;

}


//YA HOO
#include <bits/stdc++.h>
#define B begin()
#define E end()
#define RE resize
#define mP make_pair
#define pB push_back
#define X first
#define Y second
#define S size()
#define ST setprecision
const int MAXN = 100 * 1000 + 11;
typedef long long ll;
using namespace std;
int mark[MAXN],D[MAXN],par[MAXN][18],par1[MAXN][18];
vector <pair<int,int> > adj[MAXN],a[MAXN];
vector <pair<int,pair<int,int> > > b,ar_a;
struct Type_DSU
{
	int size[MAXN],par2[MAXN];
	void make_set(int x)
	{
		size[x] = 0;
		par2[x] = x;
	}
	int findPar(int x)
	{
		return par2[x] = (par2[x] == x ? x : findPar(par2[x]));
	}
	void merge(int x, int y)
	{
		x = findPar(x);
		y = findPar(y);
		if(x == y)
			return;
		if(size[y] > size[x])
			swap(x, y);
		par2[y] = x;
		size[x] += (size[x] == size[y]);
		return;
	}
}DSU;
int dfs(int x)
{
	mark[x]=1;
	for(int i=0;i<adj[x].S;i++)
	{
		int pesar=adj[x][i].X;
		if(mark[pesar])
			continue;
		D[pesar]=D[x]+1;
		par[pesar][0]=x;
		par1[pesar][0]=adj[x][i].Y;
		for(int j=1;j<18;j++)
		{
			par[pesar][j]=par[par[pesar][j-1]][j-1];
			par1[pesar][j]=max(par1[pesar][j-1],par1[par[pesar][j-1]][j-1]);
		}
		dfs(pesar);
	}
	return 0;
}
pair<int,int> parent(int x,int d)
{
	int y=0;
	for(int i=0;i<18;i++)
		if((d>>i)&1)
		{
			y=max(y,par1[x][i]);
			x=par[x][i];
		}
	return mP(x,y);
}
int lca(int x,int y)
{
	int z=0;
	if(D[x]>D[y])
		swap(x,y);
	z=parent(y,D[y]-D[x]).Y;
	y=parent(y,D[y]-D[x]).X;
	if(x==y)
	{
		return z;
	}
	for(int i=17;i>=0;i--)
		if(par[x][i]!=par[y][i])
		{
			z=max(z,par1[x][i]);
			z=max(z,par1[y][i]);
			x=par[x][i];
			y=par[y][i];
		}
	z=max(z,par1[x][0]);
	z=max(z,par1[y][0]);
	return z;
}
int main()
{
	ios_base::sync_with_stdio(false);
	ll n,m,q,s=0,s1=0;
	cin>>n>>m;
	for(int i=0;i<m;i++)
	{
		int t1,t2,t3;
		cin>>t1>>t2>>t3;
		ar_a.pB(mP(t3,mP(t1,t2)));
		adj[t1].pB(mP(t2,t3));
		adj[t2].pB(mP(t1,t3));
	}
	sort(ar_a.B,ar_a.E);
	for(int i=1;i<=n;i++)
		DSU.make_set(i);
	for(int i=0;i<ar_a.S;i++)
	{
		if(DSU.findPar(ar_a[i].Y.X)!=DSU.findPar(ar_a[i].Y.Y))
		{
			DSU.merge(ar_a[i].Y.X,ar_a[i].Y.Y);
			s++;
			s1+=ar_a[i].X;
		}
	}
	dfs(1);
	cin>>q;
	for(int i=0;i<q;i++)
	{
		int t1,t2;
		cin>>t1>>t2;
		if(s<n-2)
			cout<<-1;
		else if(s==n-2)
		{
			if(DSU.findPar(t1)!=DSU.findPar(t2))
				cout<<s1;
			else
				cout<<-1;
		}
		else
		{
			cout<<s1-lca(t1,t2);
		}
		cout<<endl;
	}
	return 0;

}


#include <bits/stdc++.h>
using namespace std;

typedef long long i64;
int h[100010];
int stramos[20][100010];
int maxstramos[20][100010];
vector <pair <int, int>> adia[100010];
int uf_g[100010], uf_tata[100010];
i64 costact;
vector <pair <int, pair <int, int>>> wormholes;

int find(int x)
{
    if (uf_tata[uf_tata[x]] != uf_tata[x])
        uf_tata[x] = find(uf_tata[x]);
    return uf_tata[x];
}
void unite(int x, int y, int g)
{
    if (find(x) != find(y)) {
        costact += g;
        adia[x].push_back({ y, g });
        adia[y].push_back({ x, g });
        x = find(x);
        y = find(y);
        if (uf_g[x] < uf_g[y])
            swap(x, y);
        uf_tata[y] = x, uf_g[x] += uf_g[y];
    }
}

void dfs(int nod, int tata = 0)
{
    h[nod] = 1 + h[tata];
    for (int i(1); i < 20; i++) {
        stramos[i][nod] = stramos[i - 1][stramos[i - 1][nod]];
        maxstramos[i][nod] = max(maxstramos[i - 1][nod], maxstramos[i - 1][stramos[i - 1][nod]]);
    }

    for (auto i : adia[nod]) {
        if (i.first == tata)
            continue;
        stramos[0][i.first] = nod;
        maxstramos[0][i.first] = i.second;
        dfs(i.first, nod);
    }
}

int lca(int a, int b)
{
    int ans(0);
    if (h[a] < h[b])
        swap(a, b);

    for (int i(19); i >= 0; i--)
        if (h[a] - (1 << i) >= h[b])
            ans = max(ans, maxstramos[i][a]), a = stramos[i][a];

    if (a == b)
        return ans;

    for (int i(19); i >= 0; i--) {
        if (stramos[i][a] != stramos[i][b]) {
            ans = max(ans, maxstramos[i][a]);
            ans = max(ans, maxstramos[i][b]);
            a = stramos[i][a];
            b = stramos[i][b];
        }
    }

    ans = max(ans, maxstramos[0][a]);
    ans = max(ans, maxstramos[0][b]);
    return ans;
}

int main()
{
    int n, m;
    ios_base :: sync_with_stdio(0);
    cin.tie(0);

    cin >> n >> m;

    wormholes.resize(m);

    for (int i(0); i < m; i++)
        cin >> wormholes[i].second.first >> wormholes[i].second.second >> wormholes[i].first;

    for (int i(1); i <= n; i++)
        uf_g[i] = 1, uf_tata[i] = i;

    sort(wormholes.begin(), wormholes.end());

    for (auto i : wormholes)
        unite(i.second.first, i.second.second, i.first);

    vector <int> roots;

    for (int i(1); i <= n; i++)
        if (find(i) == i)
            roots.push_back(i);

    if (roots.size() > 2) {
        int q;
        cin >> q;
        while (q--)
            cout << "-1\n";
        return 0;
    }

    if (roots.size() == 2) {
        int q, a, b;
        cin >> q;

        while (q--) {
            cin >> a >> b;
            if (find(a) == find(b))
                cout << "-1\n";
            else
                cout << costact << '\n';
        }
        return 0;
    }

    dfs(1);

    int q, a, b;
    cin >> q;

    while (q--) {
        cin >> a >> b;
        cout << costact - lca(a, b) << '\n';
    }

    return 0;
}


//in the name of Allah
//#isart
#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define S second
#define F first
#define MP make_pair

const int N = 1e5 + 10;

int mx[20][N], par[N], pr[20][N], h[N];
vector<pair<int, int> > v[N];
pair<int, pair<int, int> > adj[2 * N];

int get_par(int u){
	if(par[u] == -1)
		return u;
	return par[u] = get_par(par[u]);
}

void dfs(int u, int prt, int w){
	pr[0][u] = prt;
	mx[0][u] = w;
	for(int i = 1; i < 20; i ++){
		if(h[u] >= (1 << i)){
			pr[i][u] = pr[i - 1][pr[i - 1][u]];
			mx[i][u] = max(mx[i - 1][u], mx[i - 1][pr[i - 1][u]]);
		}
	}
	for(auto y : v[u])
		if(y.F != prt){
			h[y.F] = h[u] + 1;
			dfs(y.F, u, y.S);
		}
}

int get_pr(int u, int d){
	for(int i = 19; i >= 0; i --){
		if(d >= (1 << i)){
			d -= (1 << i);
			u = pr[i][u];
		}
	}
	return u;
}

int lca(int u1, int u2){
	if(h[u1] > h[u2])
		swap(u1, u2);
	u2 = get_pr(u2, h[u2] - h[u1]);
	if(u1 == u2)
		return u1;
	for(int i = 19; i >= 0; i --){
		if(pr[i][u1] != pr[i][u2]){
			u1 = pr[i][u1];
			u2 = pr[i][u2];
		}
	}
	return pr[0][u1];
}

int get_mx(int u, int d){
	int val = 0;
	for(int i = 19; i >= 0; i --){
		if(d >= (1 << i)){
			d -= (1 << i);
			val = max(val, mx[i][u]);
		}
	}
	return val;
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	int n, m;
	cin >> n >> m;
	for(int i = 0; i < m; i ++){
		int a, b, c;
		cin >> a >> b >> c;
		a --;
		b --;
		adj[i] = MP(c, MP(a, b));
	}
	sort(adj, adj + m);
	fill(par, par + N, -1);
	int com = n;
	ll sum = 0;
	for(int i = 0; i < m; i ++){
		int u1 = adj[i].S.F, u2 = adj[i].S.S, w = adj[i].F;
		int p1 = get_par(u1), p2 = get_par(u2);
		if(p1 == p2)
			continue;
		com --;
		par[p1] = p2;
		sum += w;
		v[u1].push_back(MP(u2, w));
		v[u2].push_back(MP(u1, w));
	}
	dfs(0, -1, -1);
	int q;
	cin >> q;
	for(int i = 0; i < q; i ++){
		int a, b;
		cin >> a >> b;
		a --;
		b --;
		if(com > 2){
			cout << -1 << '\n';
			continue;
		}
		if(com == 2){
			if(get_par(a) == get_par(b))
				cout << -1 << '\n';
			else
				cout << sum << '\n';
			continue;
		}
		int L = lca(a, b), d1 = h[a] - h[L], d2 = h[b] - h[L];
		int val = max(get_mx(a, d1), get_mx(b,  d2));
		cout << sum - val << '\n';
	}
	return 0;
}


//in the name of Allah
//#isart
#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define S second
#define F first
#define MP make_pair

const int N = 1e5 + 10;

int mx[20][N], par[N], pr[20][N], h[N];
vector<pair<int, int> > v[N];
pair<int, pair<int, int> > adj[2 * N];

int get_par(int u){
	if(par[u] == -1)
		return u;
	return par[u] = get_par(par[u]);
}

void dfs(int u, int prt, int w){
	pr[0][u] = prt;
	mx[0][u] = w;
	for(int i = 1; i < 20; i ++){
		if(h[u] >= (1 << i)){
			pr[i][u] = pr[i - 1][pr[i - 1][u]];
			mx[i][u] = max(mx[i - 1][u], mx[i - 1][pr[i - 1][u]]);
		}
	}
	for(auto y : v[u])
		if(y.F != prt){
			h[y.F] = h[u] + 1;
			dfs(y.F, u, y.S);
		}
}

int get_pr(int u, int d){
	for(int i = 19; i >= 0; i --){
		if(d >= (1 << i)){
			d -= (1 << i);
			u = pr[i][u];
		}
	}
	return u;
}

int lca(int u1, int u2){
	if(h[u1] > h[u2])
		swap(u1, u2);
	u2 = get_pr(u2, h[u2] - h[u1]);
	if(u1 == u2)
		return u1;
	for(int i = 19; i >= 0; i --){
		if(pr[i][u1] != pr[i][u2]){
			u1 = pr[i][u1];
			u2 = pr[i][u2];
		}
	}
	return pr[0][u1];
}

int get_mx(int u, int d){
	int val = 0;
	for(int i = 19; i >= 0; i --){
		if(d >= (1 << i)){
			d -= (1 << i);
			val = max(val, mx[i][u]);
		}
	}
	return val;
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	int n, m;
	cin >> n >> m;
	for(int i = 0; i < m; i ++){
		int a, b, c;
		cin >> a >> b >> c;
		a --;
		b --;
		adj[i] = MP(c, MP(a, b));
	}
	sort(adj, adj + m);
	fill(par, par + N, -1);
	int com = n;
	ll sum = 0;
	for(int i = 0; i < m; i ++){
		int u1 = adj[i].S.F, u2 = adj[i].S.S, w = adj[i].F;
		int p1 = get_par(u1), p2 = get_par(u2);
		if(p1 == p2)
			continue;
		com --;
		par[p1] = p2;
		sum += w;
		v[u1].push_back(MP(u2, w));
		v[u2].push_back(MP(u1, w));
	}
	dfs(0, -1, -1);
	int q;
	cin >> q;
	for(int i = 0; i < q; i ++){
		int a, b;
		cin >> a >> b;
		a --;
		b --;
		if(com > 2){
			cout << -1 << '\n';
			continue;
		}
		if(com == 2){
			if(get_par(a) == get_par(b))
				cout << -1 << '\n';
			else
				cout << sum << '\n';
			continue;
		}
		int L = lca(a, b), d1 = h[a] - h[L], d2 = h[b] - h[L];
		int val = max(get_mx(a, d1), get_mx(b,  d2));
		cout << sum - val << '\n';
	}
	cout << endl;
	system("pause");
	return 0;
}


//in the name of Allah
//#isart
#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define S second
#define F first
#define MP make_pair

const int N = 1e5 + 10;

int mx[20][N], par[N], pr[20][N], h[N];
vector<pair<int, int> > v[N];
pair<int, pair<int, int> > adj[2 * N];

int get_par(int u){
	if(par[u] == -1)
		return u;
	return par[u] = get_par(par[u]);
}

void dfs(int u, int prt, int w){
	pr[0][u] = prt;
	mx[0][u] = w;
	for(int i = 1; i < 20; i ++){
		if(h[u] >= (1 << i)){
			pr[i][u] = pr[i - 1][pr[i - 1][u]];
			mx[i][u] = max(mx[i - 1][u], mx[i - 1][pr[i - 1][u]]);
		}
	}
	for(auto y : v[u])
		if(y.F != prt){
			h[y.F] = h[u] + 1;
			dfs(y.F, u, y.S);
		}
}

int get_pr(int u, int d){
	for(int i = 19; i >= 0; i --){
		if(d >= (1 << i)){
			d -= (1 << i);
			u = pr[i][u];
		}
	}
	return u;
}

int lca(int u1, int u2){
	if(h[u1] > h[u2])
		swap(u1, u2);
	u2 = get_pr(u2, h[u2] - h[u1]);
	if(u1 == u2)
		return u1;
	for(int i = 19; i >= 0; i --){
		if(pr[i][u1] != pr[i][u2]){
			u1 = pr[i][u1];
			u2 = pr[i][u2];
		}
	}
	return pr[0][u1];
}

int get_mx(int u, int d){
	int val = 0;
	for(int i = 19; i >= 0; i --){
		if(d >= (1 << i)){
			d -= (1 << i);
			val = max(val, mx[i][u]);
			u = pr[i][u];
		}
	}
	return val;
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	int n, m;
	cin >> n >> m;
	for(int i = 0; i < m; i ++){
		int a, b, c;
		cin >> a >> b >> c;
		a --;
		b --;
		adj[i] = MP(c, MP(a, b));
	}
	sort(adj, adj + m);
	fill(par, par + N, -1);
	int com = n;
	ll sum = 0;
	for(int i = 0; i < m; i ++){
		int u1 = adj[i].S.F, u2 = adj[i].S.S, w = adj[i].F;
		int p1 = get_par(u1), p2 = get_par(u2);
		if(p1 == p2)
			continue;
		com --;
		par[p1] = p2;
		sum += w;
		v[u1].push_back(MP(u2, w));
		v[u2].push_back(MP(u1, w));
	}
	dfs(0, -1, -1);
	int q;
	cin >> q;
	for(int i = 0; i < q; i ++){
		int a, b;
		cin >> a >> b;
		a --;
		b --;
		if(com > 2){
			cout << -1 << '\n';
			continue;
		}
		if(com == 2){
			if(get_par(a) == get_par(b))
				cout << -1 << '\n';
			else
				cout << sum << '\n';
			continue;
		}
		int L = lca(a, b), d1 = h[a] - h[L], d2 = h[b] - h[L];
		int val = max(get_mx(a, d1), get_mx(b,  d2));
		cout << sum - val << '\n';
	}
	return 0;
}


#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cstdio>
#define N 220000
#define rep(i,x,y) for(i=x;i<=y;++i)
using namespace std;
struct ppp{
 int x,y,k;
}e[N];
int cnt,n,m,i,j,k,bo[N],bo2[N],x,y,size[N],fa[N],d[N],p,qn;
long long ans;
bool cmp(const ppp&a,const ppp&b){
 return a.k<b.k;
}
int get(int x){
 while(fa[x])x=fa[x];
 return x;
}
int main(){
 scanf("%d%d",&n,&m);
 rep(i,1,m)scanf("%d%d%d",&e[i].x,&e[i].y,&e[i].k);
 sort(e+1,e+m+1,cmp);
 rep(i,1,n)size[i]=1;
 p=n;
 rep(i,1,m)if(get(x=e[i].x)!=get(y=e[i].y)){
  x=get(x); y=get(y);
  if(size[x]>size[y])swap(x,y);
  fa[x]=y; size[y]+=size[x];
  d[x]=e[i].k;
  ans+=e[i].k; p--;
 }
 scanf("%d",&qn);
 rep(i,1,qn){
  scanf("%d%d",&x,&y);
  ++cnt; k=-1;
  for(j=x;j;k=max(k,d[j]),j=fa[j])bo[j]=cnt,bo2[j]=k;
  k=-1;
  for(j=y;j;k=max(k,d[j]),j=fa[j])if(bo[j]==cnt)break;
  if(p>2 || p==2 && j)puts("-1");
  else if(p==2)printf("%I64d\n",ans);
  else printf("%I64d\n",ans-max(k,bo2[j]));
 }
}


#include<bits/stdc++.h>
#define ld double
#define ull unsigned long long
#define ll long long
#define pii pair<int,int >
#define iiii pair<int,pii >
#define mp make_pair
#define INF 1000000000
#define MOD 1000000007
#define rep(i,x) for(register int (i)=0;(i)<(x);++(i))
inline int getint(){
	int x=0,p=1;char c=getchar();
	while (c<=32)c=getchar();
	if(c==45)p=-p,c=getchar();
	while (c>32)x=x*10+c-48,c=getchar();
	return x*p;
}
using namespace std;
//ruogu
const int M=20;
const int N=1e5+10;
ll res;
int n,m,cnt,pp[N],w[M][N],par[M][N],dep[N],q;
vector<iiii >E;
vector<int>G[N],V[N];
//
inline void addedge(int x,int y,int z){
    G[x].push_back(y);
    V[x].push_back(z);
}
int fF(int x){
    if(pp[x]==x)return x;
    return pp[x]=fF(pp[x]);
}
bool fU(int x,int y){
    x=fF(x);y=fF(y);
    if(x==y)return false;
    pp[x]=y;
    return true;
}
void dfs(int x,int p,int d,int len){
    par[0][x]=p;
    dep[x]=d;
    w[0][x]=len;
    rep(i,G[x].size()){
        int to=G[x][i],val=V[x][i];
        if(to!=p)dfs(to,x,d+1,val);
    }
}
int lca(int x,int y){
    int ans=0;
    if(dep[x]<dep[y])swap(x,y);
    for(int i=M-1;i>=0;i--)if((dep[x]-dep[y])&(1<<i)){
        ans=max(ans,w[i][x]);
        x=par[i][x];
    }
    if(x==y)return ans;
    for(int i=M-1;i>=0;i--)if(par[i][x]!=par[i][y]){
        ans=max(ans,w[i][x]);
        ans=max(ans,w[i][y]);
        x=par[i][x];
        y=par[i][y];
    }
    ans=max(ans,w[0][x]);
    ans=max(ans,w[0][y]);
    return ans;
}
int main(){
    n=getint();m=getint();
    rep(i,n)pp[i]=i;
    rep(i,m){
        int x=getint()-1,y=getint()-1,z=getint();
        E.push_back(mp(z,mp(x,y)));
    }
    sort(E.begin(),E.end());
    rep(i,m){
        int x=E[i].second.first,y=E[i].second.second,z=E[i].first;
        if(fU(x,y)){
            res+=1ll*z;
            cnt++;
            addedge(x,y,z);
            addedge(y,x,z);
        }
    }
    q=getint();
    if(cnt<n-2){
        rep(i,q)printf("-1\n");
        return 0;
    }
    if(cnt==n-2){
        rep(qqq,q){
            int x=getint()-1,y=getint()-1;
            if(fF(x)==fF(y))printf("-1\n");
            else printf("%I64d\n",res);
        }
        return 0;
    }
    dfs(0,-1,0,0);
    rep(i,M-1)rep(j,n){
        if(par[i][j]==-1)par[i+1][j]=-1;
        else{
            par[i+1][j]=par[i][par[i][j]];
            w[i+1][j]=max(w[i][j],w[i][par[i][j]]);
        }
    }
    rep(qqq,q){
        int x=getint()-1,y=getint()-1;
        printf("%I64d\n",res-1ll*lca(x,y));
    }
	return 0;
}


#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<string>
#include<cstring>
#include<cctype>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<iomanip>
#include<sstream>
#include<cstdlib>
#include<ctime>
#include<list>
#include<deque>
#include<bitset>
#include<fstream>
#define ld double
#define ull unsigned long long
#define ll long long
#define pii pair<int,int >
#define iiii pair<int,pii >
#define mp make_pair
#define INF 1000000000
#define MOD 1000000007
#define rep(i,x) for(register int (i)=0;(i)<(x);++(i))
inline int getint(){
	int x=0,p=1;char c=getchar();
	while (c<=32)c=getchar();
	if(c==45)p=-p,c=getchar();
	while (c>32)x=x*10+c-48,c=getchar();
	return x*p;
}
using namespace std;
//ruogu
const int M=20;
const int N=1e5+10;
ll res;
int n,m,cnt,pp[N],w[M][N],par[M][N],dep[N],q;
vector<iiii >E;
vector<int>G[N],V[N];
//
inline void addedge(int x,int y,int z){
    G[x].push_back(y);
    V[x].push_back(z);
}
int fF(int x){
    if(pp[x]==x)return x;
    return pp[x]=fF(pp[x]);
}
bool fU(int x,int y){
    x=fF(x);y=fF(y);
    if(x==y)return false;
    pp[x]=y;
    return true;
}
void dfs(int x,int p,int d,int len){
    par[0][x]=p;
    dep[x]=d;
    w[0][x]=len;
    rep(i,G[x].size()){
        int to=G[x][i],val=V[x][i];
        if(to!=p)dfs(to,x,d+1,val);
    }
}
int lca(int x,int y){
    int ans=0;
    if(dep[x]<dep[y])swap(x,y);
    for(int i=M-1;i>=0;i--)if((dep[x]-dep[y])&(1<<i)){
        ans=max(ans,w[i][x]);
        x=par[i][x];
    }
    if(x==y)return ans;
    for(int i=M-1;i>=0;i--)if(par[i][x]!=par[i][y]){
        ans=max(ans,w[i][x]);
        ans=max(ans,w[i][y]);
        x=par[i][x];
        y=par[i][y];
    }
    ans=max(ans,w[0][x]);
    ans=max(ans,w[0][y]);
    return ans;
}
int main(){
    n=getint();m=getint();
    rep(i,n)pp[i]=i;
    rep(i,m){
        int x=getint()-1,y=getint()-1,z=getint();
        E.push_back(mp(z,mp(x,y)));
    }
    sort(E.begin(),E.end());
    rep(i,m){
        int x=E[i].second.first,y=E[i].second.second,z=E[i].first;
        if(fU(x,y)){
            res+=1ll*z;
            cnt++;
            addedge(x,y,z);
            addedge(y,x,z);
        }
    }
    q=getint();
    if(cnt<n-2){
        rep(i,q)printf("-1\n");
        return 0;
    }
    if(cnt==n-2){
        rep(qqq,q){
            int x=getint()-1,y=getint()-1;
            if(fF(x)==fF(y))printf("-1\n");
            else printf("%I64d\n",res);
        }
        return 0;
    }
    dfs(0,-1,0,0);
    rep(i,M-1)rep(j,n){
        if(par[i][j]==-1)par[i+1][j]=-1;
        else{
            par[i+1][j]=par[i][par[i][j]];
            w[i+1][j]=max(w[i][j],w[i][par[i][j]]);
        }
    }
    rep(qqq,q){
        int x=getint()-1,y=getint()-1;
        printf("%I64d\n",res-1ll*lca(x,y));
    }
	return 0;
}


#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<string>
#include<cstring>
#include<cctype>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<iomanip>
#include<sstream>
#include<cstdlib>
#include<ctime>
#include<list>
#include<deque>
#include<bitset>
#include<fstream>
#define ld double
#define ull unsigned long long
#define ll long long
#define pii pair<int,int >
#define iiii pair<int,pii >
#define mp make_pair
#define INF 1000000000
#define MOD 1000000007
#define rep(i,x) for(register int (i)=0;(i)<(x);++(i))
inline int getint(){
	int x=0,p=1;char c=getchar();
	while (c<=32)c=getchar();
	if(c==45)p=-p,c=getchar();
	while (c>32)x=x*10+c-48,c=getchar();
	return x*p;
}
using namespace std;
//ruogu
const int M=20;
const int N=1e5+10;
ll res;
int n,m,cnt,pp[N],w[M][N],par[M][N],dep[N],q;
vector<iiii >E;
vector<int>G[N],V[N];
//
inline void addedge(int x,int y,int z){
    G[x].push_back(y);
    V[x].push_back(z);
}
int fF(int x){
    if(pp[x]==x)return x;
    return pp[x]=fF(pp[x]);
}
bool fU(int x,int y){
    x=fF(x);y=fF(y);
    if(x==y)return false;
    pp[x]=y;
    return true;
}
void dfs(int x,int p,int d,int len){
    par[0][x]=p;
    dep[x]=d;
    w[0][x]=len;
    rep(i,G[x].size()){
        int to=G[x][i],val=V[x][i];
        if(to!=p)dfs(to,x,d+1,val);
    }
}
int lca(int x,int y){
    int ans=0;
    if(dep[x]<dep[y])swap(x,y);
    for(int i=M-1;i>=0;i--)if((dep[x]-dep[y])&(1<<i)){
        ans=max(ans,w[i][x]);
        x=par[i][x];
    }
    if(x==y)return ans;
    for(int i=M-1;i>=0;i--)if(par[i][x]!=par[i][y]){
        ans=max(ans,w[i][x]);
        ans=max(ans,w[i][y]);
        x=par[i][x];
        y=par[i][y];
    }
    ans=max(ans,w[0][x]);
    ans=max(ans,w[0][y]);
    return ans;
}
int main(){
    n=getint();m=getint();
    rep(i,n)pp[i]=i;
    rep(i,m){
        int x=getint()-1,y=getint()-1,z=getint();
        E.push_back(mp(z,mp(x,y)));
        addedge(x,y,z);
        addedge(y,x,z);
    }
    sort(E.begin(),E.end());
    rep(i,m){
        int x=E[i].second.first,y=E[i].second.second,z=E[i].first;
        if(fU(x,y)){
            res+=1ll*z;
            cnt++;
        }
    }
    dfs(0,-1,0,0);
    rep(i,M-1)rep(j,n){
        if(par[i][j]==-1)par[i+1][j]=-1;
        else{
            par[i+1][j]=par[i][par[i][j]];
            w[i+1][j]=max(w[i][j],w[i][par[i][j]]);
        }
    }
    q=getint();
    if(cnt<n-2){
        rep(i,q)printf("-1\n");
        return 0;
    }
    rep(qqq,q){
        int x=getint()-1,y=getint()-1;
        if(cnt==n-2){
            if(fF(x)==fF(y))printf("-1\n");
            else printf("%I64d\n",res);
            continue;
        }
        printf("%I64d\n",res-1ll*lca(x,y));
    }
	return 0;
}


#include <cstdio>
#include <vector>
#include <utility>
#include <algorithm>

using namespace std;

#define fst first
#define snd second

const int MAXB = 17;
const int MAXN = 100100;
const long long INF = 1LL << 40;

typedef pair<int, int> PII;
typedef pair<int, long long> PIL;
typedef pair<long long, PII> PLPII;

struct DisjointSet {
  int p[MAXN];

  void init(int n) {
    for (int i = 0; i < n; ++i) {
      p[i] = i;
    }
  }

  int root(int i) { return p[i] = p[i] == i ? i : root(p[i]); }

  bool setp(int i, int j) {
    i = root(i);
    j = root(j);
    if (i != j) {
      p[i] = j;
      return true;
    } else {
      return false;
    }
  }
} ds;

vector<PIL> e[MAXN];
vector<PLPII> ex;
int d[MAXN];
PIL p[MAXN][MAXB];

void dfs(int u, int v) {
  for (int i = 1; i < MAXB; ++i) {
    const PIL& q = p[p[u][i - 1].fst][i - 1];
    p[u][i].fst = q.fst;
    p[u][i].snd = max(p[u][i - 1].snd, q.snd);
  }
  for (vector<PIL>::const_iterator it = e[u].begin(); it != e[u].end(); ++it) {
    int w = it->fst;
    if (w == v) {
      continue;
    }
    d[w] = d[u] + 1;
    p[w][0].fst = u;
    p[w][0].snd = it->snd;
    dfs(w, u);
  }
}

PIL up(int u, int n) {
  long long snd = 0;

  for (int i = 0; n > 0; ++i) {
    if (n % 2 != 0) {
      snd = max(snd, p[u][i].snd);
      u = p[u][i].fst;
    }
    n >>= 1;
  }

  return make_pair(u, snd);
}

long long gao(int u, int v) {
  int l = 0, r = min(d[u], d[v]) + 1, m;

  while (l < r) {
    m = (l + r) / 2;
    if (up(u, d[u] - m).fst == up(v, d[v] - m).fst) {
      l = m + 1;
    } else {
      r = m;
    }
  }
  m = l - 1;

  return max(up(u, d[u] - m).snd, up(v, d[v] - m).snd);
}

int main() {
  int n, m, a, b, c;
  long long sum, ans;

  scanf("%d%d", &n, &m);
  for (int i = 1; i < n; ++i) {
    ex.push_back(make_pair(INF, make_pair(i - 1, i)));
  }
  for (int i = 0; i < m; ++i) {
    scanf("%d%d%d", &a, &b, &c);
    ex.push_back(make_pair(c, make_pair(a - 1, b - 1)));
  }
  sort(ex.begin(), ex.end());
  ds.init(n);
  sum = 0;
  for (vector<PLPII>::const_iterator it = ex.begin(); it != ex.end(); ++it) {
    if (ds.setp(it->snd.fst, it->snd.snd)) {
      sum += it->fst;
      e[it->snd.fst].push_back(make_pair(it->snd.snd, it->fst));
      e[it->snd.snd].push_back(make_pair(it->snd.fst, it->fst));
    }
  }

  d[0] = 0;
  p[0][0] = make_pair(0, 0);
  dfs(0, -1);
  scanf("%d", &m);
  for (int i = 0; i < m; ++i) {
    scanf("%d%d", &a, &b);
    ans = sum - gao(a - 1, b - 1);
    printf("%I64d\n", ans < INF ? ans : -1LL);
  }

  return 0;
}


//I Guds namn
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

const int MAXN = 10 * 1000 + 11, MAXL = 20;

bool mark[MAXN];

int d[MAXN], par[MAXN][MAXL], maxE[MAXN][MAXL];

vector<int> adj[MAXN], wei[MAXN];

vector<pair<int, pair<int, int> > > edge, edges;

struct Type_DSU
{
	int size[MAXN], par[MAXN], h = 0;
	void make_set(int x)
	{
		par[x] = x;
		size[x] = 0;
        h++;
	}
	int findPar(int x)
	{
		return par[x] = (par[x] == x ? x : findPar(par[x]));
	}
	void merge(int x, int y)
	{
		x = findPar(x), y = findPar(y);
		if(x == y)
			return;
		if(size[y] > size[x])
			swap(x, y);
		par[y] = x;
        size[x] += (size[x] == size[y]);
        h--;
		return;
	}
}DSU;

void dfs(int u)
{
    mark[u] = true;
    for(int i = 0;i < adj[u].size();i++)
    {
        int v = adj[u][i], w = wei[u][i];
        if(!mark[v])
        {
            d[v] = d[u] + 1;
            par[v][0] = u;
            maxE[v][0] = w;
            for(int j = 1;j < MAXL;j++)
            {
                par[v][j] = par[par[v][j - 1]][j - 1];
                maxE[v][j] = max(maxE[v][j - 1], maxE[par[v][j - 1]][j - 1]);
            }
            dfs(v);
        }
    }
    return;
}

int parent(int u, int d)
{
    for(int i = 0;i < MAXL;i++)
        if((d >> i) & 1)
            u = par[u][i];
    return u;
}

int maxEdge(int u,int d)
{
    int ret = 0;
    for(int i = 0;i < MAXL;i++)
        if((d >> i) & 1)
        {
            ret = max(ret, maxE[u][i]);
            u = par[u][i];
        }
    return ret;
}

int lca(int u, int v)
{
    if(d[u] > d[v])
        swap(u, v);
    int maxEE = maxEdge(v, d[v] - d[u]);
    v = parent(v, d[v] - d[u]);
    if(u == v)
        return maxEE;
    for(int i = MAXL - 1;i >= 0;i--)
        if(par[u][i] != par[v][i])
        {
            maxEE = max(maxEE, maxE[u][i]);
            u = par[u][i];
            maxEE = max(maxEE, maxE[u][i]);
            v = par[v][i];
        }
    maxEE = max(maxEE, maxE[u][0]);
    maxEE = max(maxEE, maxE[v][0]);
    return maxEE;
}

int main()
{
    int n, m;
    cin >> n >> m;
    for(int i = 1;i <= n;i++)
        DSU.make_set(i);
    for(int i = 1;i <= m;i++)
    {
        int u, v, w;
        cin >> u >> v >> w;
        edge.push_back(make_pair(w, make_pair(u, v)));
    }
    sort(edge.begin(), edge.end());
    long long ans = 0;
    for(int i = 0;i < edge.size();i++)
    {
        int u = edge[i].second.first, v = edge[i].second.second, w = edge[i].first;
        if(DSU.findPar(u) != DSU.findPar(v))
        {
            DSU.merge(u, v);
            ans += w;
            adj[u].push_back(v);
            adj[v].push_back(u);
            wei[u].push_back(w);
            wei[v].push_back(w);
            edges.push_back(edge[i]);
        }
    }
    dfs(1);
    int q;
    cin >> q;
    for(int i = 1;i <= q;i++)
    {
        int a, b;
        cin >> a >> b;
        if(DSU.h > 2)
        {
            cout << -1 << endl;
            continue;
        }
        if(DSU.h == 2)
        {
            if(DSU.findPar(a) == DSU.findPar(b))
            {
                cout << -1 << endl;
                continue;
            }
            cout << ans << endl;
            continue;
        }
        cout << ans - lca(a, b) << endl;
    }
    return 0;
}

/*
2
1
1 2 3
1
1 2
*/

/*
3
1
1 2 3
2
1 2
1 3
*/


//TO CHANGE THE WORLD
#include<bits/stdc++.h>

using namespace std;

#define mp make_pair
#define pb push_back
#define L first
#define R second
const long long maxn=1e5+10, maxl=20;
long long cmp, d[maxn], sum;
pair<long long, long long> par[maxn][maxl];
vector<pair<long long, long long> > adj[maxn];
bool mark[maxn];

struct Type_Dsu
{
	long long par[maxn], h[maxn];
	void make_set(long long x)
	{
		par[x]=x;
		h[x]=0;
	}
	long long findpar(long long x)
	{
		return (x==par[x]?x:findpar(par[x]));
	}
	void merge(long long x, long long y)
	{
		x=findpar(x);
		y=findpar(y);
		if(x==y)
			return;
		cmp--;
		if(h[x]>h[y])
			swap(x,y);
		par[x]=y;
		h[y]+=(h[x]==h[y]);
	}
	bool same(long long x, long long y)
	{
		return (findpar(x)==findpar(y));
	}
}dsu;
struct edge
{
	long long fi, se, w;
};

vector<edge> mmst, ed;
bool comp(edge a, edge b)
{
	if(a.w<b.w)
		return true;
	return false;
}
long long mst(vector <edge> e)
{
	sort(e.begin(), e.end(), comp);
	long long ret;
	for(long long i=0; i<e.size(); i++)
	{
		long long fi=e[i].fi, se=e[i].se, w=e[i].w;
		if(!dsu.same(fi,se))
		{
			dsu.merge(fi,se);
			adj[fi].pb(mp(w,se));
			adj[se].pb(mp(w,fi));
			ret+=w;
		}
	}
	return ret;
}
void dfs1(long long x)
{
	mark[x]=true;
	for(long long i=0; i<adj[x].size(); i++)
	{
		long long child=adj[x][i].R, w=adj[x][i].L;
		if(mark[child])
			continue;
		par[child][0]=mp(x,w);
		d[child]=d[x]+1;
		for(long long j=1; j<maxl; j++)
			par[child][j]=mp(par[par[child][i-1].L][i-1].L,max(par[child][i-1].R, par[par[child][i-1].L][i-1].R));
		dfs1(child);
	}
	return ;
}
pair<long long, long long> findp(long long x, long long d)
{
	long long ret=x, e=0;
	for(long long i=maxl; i>=0; i--)
		if((d>>i)&1)
			{
				ret=par[ret][i].L;
				e=max(e,par[ret][i].R);
			}
	return mp(ret,e);
}
pair<long long, long long> lca(long long x, long long y)
{
	long long e1=0,e2=0;
	if(d[x]>d[y])
		swap(x,y);
	pair<long long, long long> p=findp(y,d[y]-d[x]);
	e2=max(e2,p.R);
	y=p.L;
	if(x==y)
		return mp(x,e2);
	for(long long i=0; i<maxl; i++)
		if(par[x][i].L!=par[y][i].L)
		{
			x=par[x][i].L;
			y=par[y][i].L;
			e1=max(e1,par[x][i].R);
			e2=max(e2,par[y][i].R);
		}
	e1=max(e1,e2);
	e1=max(e1,par[x][0].R);
	return mp(par[x][0].L, max(e1,e2));
}
void input()
{
	long long n,m;
	cin>>n>>m;
	cmp=n;
	for(long long i=0; i<n; i++)
		dsu.make_set(i);
	for(long long i=0; i<m; i++)
	{
		long long fi, se, w;
		fi--; se--;
		cin>>fi>>se>>w;
		dsu.merge(fi,se);
		edge e;
		e.fi=fi;
		e.se=se;
		e.w=w;
		ed.pb(e);
	}
}
void task()
{
	sum=mst(ed);
	dfs1(0);
}
void output()
{
	long long q;
	cin>>q;
	for(long long i=0; i<q; i++)
	{
		long long A,B;
		cin>>A>>B;
		if(cmp>2)
			cout<<-1<<endl;
		if(cmp==2)
			if(dsu.same(A,B))
				cout<<-1<<endl;
			else
				cout<<sum<<endl;
		if(cmp==1)
		{
			cout<<sum-lca(A,B).R<<endl;
		}
	}
	return;
}

int main()
{
	input();
	task();
	output();
	return 0;
}


//I Guds namn
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

const int MAXN = 100 * 1000 + 11, MAXL = 20;

bool mark[MAXN];

int d[MAXN], par[MAXN][MAXL], maxE[MAXN][MAXL];

vector<int> adj[MAXN], wei[MAXN];

vector<pair<int, pair<int, int> > > edge, edges;

struct Type_DSU
{
	int size[MAXN], par[MAXN], h = 0;
	void make_set(int x)
	{
		par[x] = x;
		size[x] = 0;
        h++;
	}
	int findPar(int x)
	{
		return par[x] = (par[x] == x ? x : findPar(par[x]));
	}
	void merge(int x, int y)
	{
		x = findPar(x), y = findPar(y);
		if(x == y)
			return;
		if(size[y] > size[x])
			swap(x, y);
		par[y] = x;
        size[x] += (size[x] == size[y]);
        h--;
		return;
	}
}DSU;

void dfs(int u)
{
    mark[u] = true;
    for(int i = 0;i < adj[u].size();i++)
    {
        int v = adj[u][i], w = wei[u][i];
        if(!mark[v])
        {
            d[v] = d[u] + 1;
            par[v][0] = u;
            maxE[v][0] = w;
            for(int j = 1;j < MAXL;j++)
            {
                par[v][j] = par[par[v][j - 1]][j - 1];
                maxE[v][j] = max(maxE[v][j - 1], maxE[par[v][j - 1]][j - 1]);
            }
            dfs(v);
        }
    }
    return;
}

int parent(int u, int d)
{
    for(int i = 0;i < MAXL;i++)
        if((d >> i) & 1)
            u = par[u][i];
    return u;
}

int maxEdge(int u,int d)
{
    int ret = 0;
    for(int i = 0;i < MAXL;i++)
        if((d >> i) & 1)
        {
            ret = max(ret, maxE[u][i]);
            u = par[u][i];
        }
    return ret;
}

int lca(int u, int v)
{
    if(d[u] > d[v])
        swap(u, v);
    int maxEE = maxEdge(v, d[v] - d[u]);
    v = parent(v, d[v] - d[u]);
    if(u == v)
        return maxEE;
    for(int i = MAXL - 1;i >= 0;i--)
        if(par[u][i] != par[v][i])
        {
            maxEE = max(maxEE, maxE[u][i]);
            u = par[u][i];
            maxEE = max(maxEE, maxE[v][i]);
            v = par[v][i];
        }
    maxEE = max(maxEE, maxE[u][0]);
    maxEE = max(maxEE, maxE[v][0]);
    return maxEE;
}

int main()
{
    int n, m;
    cin >> n >> m;
    for(int i = 1;i <= n;i++)
        DSU.make_set(i);
    for(int i = 1;i <= m;i++)
    {
        int u, v, w;
        cin >> u >> v >> w;
        edge.push_back(make_pair(w, make_pair(u, v)));
    }
    sort(edge.begin(), edge.end());
    long long ans = 0;
    for(int i = 0;i < edge.size();i++)
    {
        int u = edge[i].second.first, v = edge[i].second.second, w = edge[i].first;
        if(DSU.findPar(u) != DSU.findPar(v))
        {
            DSU.merge(u, v);
            ans += w;
            adj[u].push_back(v);
            adj[v].push_back(u);
            wei[u].push_back(w);
            wei[v].push_back(w);
            edges.push_back(edge[i]);
        }
    }
    dfs(1);
    int q;
    cin >> q;
    for(int i = 1;i <= q;i++)
    {
        int a, b;
        cin >> a >> b;
        if(DSU.h > 2)
        {
            cout << -1 << endl;
            continue;
        }
        if(DSU.h == 2)
        {
            if(DSU.findPar(a) == DSU.findPar(b))
            {
                cout << -1 << endl;
                continue;
            }
            cout << ans << endl;
            continue;
        }
        cout << ans - lca(a, b) << endl;
    }
    return 0;
}

/*
2
1
1 2 3
1
1 2
*/

/*
3
1
1 2 3
2
1 2
1 3
*/


//I Guds namn
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

const int MAXN = 10 * 1000 + 11, MAXL = 20;

bool mark[MAXN];

int d[MAXN], par[MAXN][MAXL], maxE[MAXN][MAXL];

vector<int> adj[MAXN], wei[MAXN];

vector<pair<int, pair<int, int> > > edge;

struct Type_DSU
{
	int size[MAXN], par[MAXN], h = 0;
	void make_set(int x)
	{
		par[x] = x;
		size[x] = 0;
        h++;
	}
	int findPar(int x)
	{
		return par[x] = (par[x] == x ? x : findPar(par[x]));
	}
	void merge(int x, int y)
	{
		x = findPar(x), y = findPar(y);
		if(x == y)
			return;
		if(size[y] > size[x])
			swap(x, y);
		par[y] = x;
        size[x] += (size[x] == size[y]);
        h--;
		return;
	}
}DSU;

void dfs(int u)
{
    mark[u] = true;
    for(int i = 0;i < adj[u].size();i++)
    {
        int v = adj[u][i], w = wei[u][i];
        if(!mark[v])
        {
            d[v] = d[u] + 1;
            par[v][0] = u;
            maxE[v][0] = w;
            for(int j = 1;j < MAXL;j++)
            {
                par[v][j] = par[par[v][j - 1]][j - 1];
                maxE[v][j] = max(maxE[v][j - 1], maxE[par[v][j - 1]][j - 1]);
            }
            dfs(v);
        }
    }
    return;
}

int parent(int u, int d)
{
    for(int i = 0;i < MAXL;i++)
        if((d >> i) & 1)
            u = par[u][i];
    return u;
}

int maxEdge(int u,int d)
{
    int ret = 0;
    for(int i = 0;i < MAXL;i++)
        if((d >> i) & 1)
        {
            ret = max(ret, maxE[u][i]);
            u = par[u][i];
        }
    return ret;
}

int lca(int u, int v)
{
    if(d[u] > d[v])
        swap(u, v);
    int maxEE = maxEdge(v, d[v] - d[u]);
    v = parent(v, d[v] - d[u]);
    if(u == v)
        return maxEE;
    for(int i = MAXL - 1;i >= 0;i--)
        if(par[u][i] != par[v][i])
        {
            maxEE = max(maxEE, maxE[u][i]);
            u = par[u][i];
            maxEE = max(maxEE, maxE[u][i]);
            v = par[v][i];
        }
    maxEE = max(maxEE, maxE[u][0]);
    return maxEE;
}

int main()
{
    int n, m;
    cin >> n >> m;
    for(int i = 1;i <= n;i++)
        DSU.make_set(i);
    for(int i = 1;i <= m;i++)
    {
        int u, v, w;
        cin >> u >> v >> w;
        edge.push_back(make_pair(w, make_pair(u, v)));
    }
    sort(edge.begin(), edge.end());
    long long ans = 0;
    for(int i = 0;i < edge.size();i++)
    {
        int u = edge[i].second.first, v = edge[i].second.second, w = edge[i].first;
        if(DSU.findPar(u) != DSU.findPar(v))
        {
            DSU.merge(u, v);
            ans += w;
            adj[u].push_back(v);
            adj[v].push_back(u);
            wei[u].push_back(w);
            wei[v].push_back(w);
        }
    }
    dfs(1);
    int q;
    cin >> q;
    for(int i = 1;i <= q;i++)
    {
        int a, b;
        cin >> a >> b;
        if(DSU.h > 2)
        {
            cout << -1 << endl;
            continue;
        }
        if(DSU.h == 2)
        {
            if(DSU.findPar(a) == DSU.findPar(b))
            {
                cout << -1 << endl;
                continue;
            }
            cout << ans << endl;
            continue;
        }
        cout << ans - lca(a, b) << endl;
    }
    return 0;
}

/*
2
1
1 2 3
1
1 2
*/

/*
3
1
1 2 3
2
1 2
1 3
*/


#include<bits/stdc++.h>
#define ll long long
#define rep(i,x,y) for(i=(x);i<=(y);i++)
using namespace std;
const int mxn=200000;
struct tre{
	int x,y,k;
}e[mxn];
int cnt,n,m,b[mxn],b2[mxn],x,y,sz[mxn],d[mxn],p,q;
ll ans;
int par[mxn];
inline int find(int x){
	while(par[x]!=0)x=par[x];
	return x;
}
inline bool cmp(tre t1,tre t2){
	return t1.k<t2.k;
}
int main(){
	 int i,j,k;
	 scanf("%d%d",&n,&m);
	 rep(i,1,m)scanf("%d%d%d",&e[i].x,&e[i].y,&e[i].k);
	 sort(e+1,e+m+1,cmp);
	 rep(i,1,n)sz[i]=1;
	 p=n;
	 rep(i,1,m){
	 	x=e[i].x,y=e[i].y;
	 	if(find(x)!=find(y)){
	 		x=find(x),y=find(y);
	 		if(sz[x]>sz[y])swap(x,y);
	 		par[x]=y,sz[y]+=sz[x];
	 		d[x]=e[i].k,ans+=e[i].k,--p;
	 	}
	 }
//	 rep(i,1,n)printf("%d ",sz[i]);
//	 puts("");
	 scanf("%d",&q);
	 rep(i,1,q){
	 	scanf("%d%d",&x,&y);
	 	++cnt,k=-1;
	 	for(j=x;j;k=max(k,d[j]),j=par[j]) b[j]=cnt,b2[j]=k;
	 	k=-1;
	 	for(j=y;j;k=max(k,d[j]),j=par[j])if(b[j]==cnt)break;
	 	if(p>2 or p==2 and j)puts("-1");
	 	else if(p==2)printf("%lld\n",ans);
	 	else printf("%lld\n",ans-max(k,b2[j]));
	 }
}


#include<bits/stdc++.h>
#define ll long long
#define rep(i,x,y) for(i=(x);i<=(y);i++)
using namespace std;
const int mxn=200000;
struct tre{
	int x,y,k;
}e[mxn];
int cnt,n,m,b[mxn],b2[mxn],x,y,sz[mxn],d[mxn],p,q;
ll ans;
int par[mxn];
inline int find(int x){return par[x]?par[x]=find(par[x]):x;}
inline bool cmp(tre t1,tre t2){return t1.k<t2.k;}
int main(){
	 int i,j,k;
	 scanf("%d%d",&n,&m);
	 rep(i,1,m)scanf("%d%d%d",&e[i].x,&e[i].y,&e[i].k);
	 sort(e+1,e+m+1,cmp);
	 rep(i,1,n)sz[i]=1;
	 p=n;
	 rep(i,1,m){
	 	x=e[i].x,y=e[i].y;
	 	if(find(x)!=find(y)){
	 		x=find(x),y=find(y);
	 		if(sz[x]>sz[y])swap(x,y);
	 		par[x]=y,sz[y]+=sz[x];
	 		d[x]=e[i].k,ans+=e[i].k,--p;
	 	}
	 }
	 scanf("%d",&q);
	 rep(i,1,q){
	 	scanf("%d%d",&x,&y);
	 	++cnt,k=-1;
	 	for(j=x;j;k=max(k,d[j]),j=par[j]) b[j]=cnt,b2[j]=k;
	 	k=-1;
	 	for(j=y;j;k=max(k,d[j]),j=par[j])if(b[j]==cnt)break;
	 	if(p>2 or p==2 and j)puts("-1");
	 	else if(p==2)printf("%lld\n",ans);
	 	else printf("%lld\n",ans-max(k,b2[j]));
	 }
}


#include<bits/stdc++.h>
#define ll long long
#define rep(i,x,y) for(i=(x);i<=(y);i++)
using namespace std;
const int mxn=200000;
struct tre{
	int x,y,k;
}e[mxn];
int cnt,n,m,b[mxn],b2[mxn],x,y,sz[mxn],d[mxn],p,q;
ll ans;
int par[mxn];
inline int find(int x){
	while(par[x]!=0)x=par[x];
	return x;
}
inline bool cmp(tre t1,tre t2){
	return t1.k<t2.k;
}
int main(){
	 int i,j,k;
	 scanf("%d%d",&n,&m);
	 rep(i,1,m)scanf("%d%d%d",&e[i].x,&e[i].y,&e[i].k);
	 sort(e+1,e+m+1,cmp);
	 rep(i,1,n)sz[i]=1;
	 p=n;
	 rep(i,1,m){
	 	x=e[i].x,y=e[i].y;
	 	if(find(x)!=find(y)){
	 		x=find(x),y=find(y);
	 		if(sz[x]>sz[y])swap(x,y);
	 		par[x]=y,sz[y]+=sz[x];
	 		d[x]=e[i].k,ans+=e[i].k,--p;
	 	}
	 }
	 scanf("%d",&q);
	 rep(i,1,q){
	 	scanf("%d%d",&x,&y);
	 	++cnt,k=-1;
	 	for(j=x;j;k=max(k,d[j]),j=par[j]) b[j]=cnt,b2[j]=k;
	 	k=-1;
	 	for(j=y;j;k=max(k,d[j]),j=par[j])if(b[j]==cnt)break;
	 	if(p>2 or p==2 and j)puts("-1");
	 	else if(p==2)printf("%lld\n",ans);
	 	else printf("%lld\n",ans-max(k,b2[j]));
	 }
}


// God & me
#include <bits/stdc++.h>
#define pb push_back
#define X first
#define Y second
#define int long long
using namespace std;
template <class T, class L> bool smax(T &x,L y){  return x < y ? (x = y, 1) : 0;  }
template <class T, class L> bool smin(T &x,L y){  return y < x ? (x = y, 1) : 0;  }
typedef pair<int, int> pii;

const int maxn = 2e5 + 17, lg = 17;
int n, m, dsu[maxn], par[maxn][lg], mx[maxn][lg], seen[maxn], c_cnt, h[maxn];
vector<pii> g[maxn];
pair<int, pii> e[maxn];
int r(int v){
    return dsu[v] < 0 ? v : dsu[v] = r(dsu[v]);
}
bool mrg(int v, int u){
    return (v = r(v)) == (u = r(u)) ? 0 : (dsu[u] = v, 1);
}
void dfs(int v, int p){
    seen[v] = c_cnt;
    //cerr << v << " : ";
    par[v][0] = p;
    for(int i = 1; i < lg; i++){
	par[v][i] = par[ par[v][i - 1] ][i - 1];
	mx[v][i] = max(mx[v][i - 1], mx[ par[v][i - 1] ][i - 1]);
	//cerr << mx[v][i] << ' ';
    }
    //cerr << '\n';
    for(auto e : g[v])
	if(e.X != p){
	    mx[e.X][0] = e.Y;
	    h[e.X] = h[v] + 1;
	    dfs(e.X, v);
	}
}
int hamid(int v, int u){
    if(h[u] < h[v])  swap(v, u);
    int ans = 0;
    for(int i = 0; i < lg; i++)
	if((h[u] - h[v]) >> i & 1)
	    smax(ans, mx[u][i]), u = par[u][i];
    for(int i = lg - 1; i >= 0; i --)
	if(par[v][i] != par[u][i]){
	    smax(ans, mx[u][i]);
	    smax(ans, mx[v][i]);
	    v = par[v][i], u = par[u][i];
	}
    return v == u ? ans : max({ans, mx[v][0], mx[u][0]});
}
main(){
    ios::sync_with_stdio(0), cin.tie(0);
    memset(dsu, -1, sizeof dsu);
    cin >> n >> m;
    for(int v, u, w, i = 0; i < m; i++)
	cin >> v >> u >> w, e[i] = {w, {v - 1, u - 1}};
    sort(e, e + m);
    int base = 0;
    for(int i = 0; i < m; i++)
	if(mrg(e[i].Y.X, e[i].Y.Y)){
	    g[ e[i].Y.X ].pb({e[i].Y.Y, e[i].X});
	    g[ e[i].Y.Y ].pb({e[i].Y.X, e[i].X});
	    base += e[i].X;
	}
    for(int i = 0; i < n; i++)
	if(!seen[i])
	    c_cnt ++, dfs(i, i);
    int nq;  cin >> nq;
    for(int v, u; nq--;){
	cin >> v >> u, v--, u--;
	if(c_cnt > 2 || c_cnt == 2 && seen[v] == seen[u]){
	    cout << "-1\n";
	    continue;
	}
	if(seen[v] == seen[u])
	    cout << base - hamid(v, u) << '\n';
	else
	    cout << base << '\n';
    }
    return 0;
}


// God & me
#include <bits/stdc++.h>
#define pb push_back
#define X first
#define Y second
#define int long long
using namespace std;
template <class T, class L> bool smax(T &x,L y){  return x < y ? (x = y, 1) : 0;  }
template <class T, class L> bool smin(T &x,L y){  return y < x ? (x = y, 1) : 0;  }
typedef pair<int, int> pii;

const int maxn = 2e5 + 17, lg = 17;
int n, m, dsu[maxn], par[maxn][lg], mx[maxn][lg], seen[maxn], c_cnt, h[maxn];
vector<pii> g[maxn];
pair<int, pii> e[maxn];
int r(int v){
    return dsu[v] < 0 ? v : dsu[v] = r(dsu[v]);
}
bool mrg(int v, int u){
    return (v = r(v)) == (u = r(u)) ? 0 : (dsu[u] = v, 1);
}
void dfs(int v, int p){
    seen[v] = c_cnt;
    //cerr << v << " : ";
    par[v][0] = p;
    for(int i = 1; i < lg; i++){
	par[v][i] = par[ par[v][i - 1] ][i - 1];
	mx[v][i] = max(mx[v][i - 1], mx[ par[v][i - 1] ][i - 1]);
	//cerr << mx[v][i] << ' ';
    }
    //cerr << '\n';
    for(auto e : g[v])
	if(e.X != p){
	    mx[e.X][0] = e.Y;
	    h[e.X] = h[v] + 1;
	    dfs(e.X, v);
	}
}
int hamid(int v, int u){
    if(h[u] < h[v])  swap(v, u);
    int ans = 0;
    for(int i = 0; i < lg; i++)
	if((h[u] - h[v]) >> i & 1)
	    smax(ans, mx[u][i]), u = par[u][i];
    for(int i = lg - 1; i >= 0; i --)
	if(par[v][i] != par[u][i]){
	    smax(ans, mx[u][i]);
	    smax(ans, mx[v][i]);
	    v = par[v][i], u = par[u][i];
	}
    return v == u ? ans : max({ans, mx[v][0], mx[u][0]});
}
main(){
    ios::sync_with_stdio(0), cin.tie(0);
    memset(dsu, -1, sizeof dsu);
    cin >> n >> m;
    for(int v, u, w, i = 0; i < m; i++)
	cin >> v >> u >> w, e[i] = {w, {v - 1, u - 1}};
    sort(e, e + m);
    int base = 0;
    for(int i = 0; i < m; i++)
	if(mrg(e[i].Y.X, e[i].Y.Y)){
	    g[ e[i].Y.X ].pb({e[i].Y.Y, e[i].X});
	    g[ e[i].Y.Y ].pb({e[i].Y.X, e[i].X});
	    base += e[i].X;
	}
    for(int i = 0; i < n; i++)
	if(!seen[i])
	    c_cnt ++, dfs(i, i);
    int nq;  cin >> nq;
    for(int v, u; nq--;){
	cin >> v >> u, v--, u--;
	if(c_cnt > 2 || c_cnt == 2 && seen[v] == seen[u]){
	    cout << "-1\n";
	    continue;
	}
	cout << base - hamid(v, u) << '\n';
    }
    return 0;
}


// God & me
#include <bits/stdc++.h>
#define pb push_back
#define X first
#define Y second
//#define int long long
using namespace std;
template <class T, class L> bool smax(T &x,L y){  return x < y ? (x = y, 1) : 0;  }
template <class T, class L> bool smin(T &x,L y){  return y < x ? (x = y, 1) : 0;  }
typedef pair<int, int> pii;

const int maxn = 1e5 + 17, lg = 17;
int n, m, dsu[maxn], par[maxn][lg], mx[maxn][lg], seen[maxn], c_cnt, h[maxn];
vector<pii> g[maxn];
pair<int, pii> e[maxn];
int r(int v){
    return dsu[v] < 0 ? v : dsu[v] = r(dsu[v]);
}
bool mrg(int v, int u){
    return (v = r(v)) == (u = r(u)) ? 0 : (dsu[u] = v, 1);
}
void dfs(int v, int p){
    seen[v] = c_cnt;
    par[v][0] = p;
    for(int i = 1; i < lg; i++){
	par[v][i] = par[ par[v][i - 1] ][i - 1];
	mx[v][i] = max(mx[v][i - 1], mx[ par[v][i - 1] ][i - 1]);
    }
    for(auto e : g[v])
	if(e.X != p){
	    mx[e.X][0] = e.Y;
	    h[e.X] = h[v] + 1;
	    dfs(e.X, v);
	}
}
int hamid(int v, int u){
    if(h[u] < h[v])  swap(v, u);
    int ans = 0;
    for(int i = 0; i < lg; i++)
	if((h[u] - h[v]) >> i & 1)
	    smax(ans, mx[u][i]), u = par[u][i];
    for(int i = lg - 1; i >= 0; i --)
	if(par[v][i] != par[u][i]){
	    smax(ans, mx[u][i]);
	    smax(ans, mx[v][i]);
	    v = par[v][i], u = par[u][i];
	}
    return ans;
}
int main(){
    ios::sync_with_stdio(0), cin.tie(0);
    memset(dsu, -1, sizeof dsu);
    cin >> n >> m;
    for(int v, u, w, i = 0; i < m; i++)
	cin >> v >> u >> w, e[i] = {w, {v - 1, u - 1}};
    sort(e, e + m);
    int base = 0;
    for(int i = 0; i < m; i++)
	if(mrg(e[i].Y.X, e[i].Y.Y)){
	    g[ e[i].Y.X ].pb({e[i].Y.Y, e[i].X});
	    g[ e[i].Y.Y ].pb({e[i].Y.X, e[i].X});
	    base += e[i].X;
	}
    for(int i = 0; i < n; i++)
	if(!seen[i])
	    c_cnt ++, dfs(i, i);
    int nq;  cin >> nq;
    for(int v, u; nq--;){
	cin >> v >> u, v--, u--;
	if(c_cnt > 2 || c_cnt == 2 && seen[v] == seen[u]){
	    cout << "-1\n";
	    continue;
	}
	cout << base - hamid(v, u) << '\n';
    }
    return 0;
}


//lxrtxdy!
#include <bits/stdc++.h>
typedef long long int ll;
typedef long double ld;
#define pb push_back
#define pii pair < ll , ll >
#define F first
#define S second
#define int long long
#define sync ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#pragma GCC optimize ("Ofast")
#pragma GCC optimize ("unroll-loops")
using namespace std;
/// khodaya komak kon
/// ya navid navid
const int N=2e5+100,M=22;
ll par1[N];
vector <pii> g[N];
ll getpar1(ll v){
    if (par1[v]!=v) return par1[v]=getpar1(par1[v]);

    return v;
}
vector <pair <int,pii> > yal;
ll par[N][M];
ll pr[N][M];
ll hi[N];
ll dfs(ll v,ll p,ll h){
    hi[v]=h;
    for (int i=0;i<g[v].size();i++){
        ll u=g[v][i].F;
        if (u!=p){
            par[u][0]=v;
            pr[u][0]=g[v][i].S;
            dfs(u,v,h+1);
        }
    }
}
ll get(ll v,ll h){
    if (h==-1 || h==0){
        return 0;
    }
    ll ans=0;
    for(int i=0;i<M;i++){
        if (h & (1 << i)){
            ans=max(ans,pr[v][i]);
            v = par[v][i];
        }
    }
    return ans;
}
ll getpar(ll v,ll h){
    if (h==-1 || h==0){
        return v;
    }
    for(int i=0;i<M;i++){
        if (h & (1 << i)) v = par[v][i];
    }
    return v;
}
int lca(int v, int u) {
    if (hi[v]<hi[u]){
        ll y=v;
        v=u;
        u=y;
    }
    v = getpar(v,hi[v]-hi[u]);
    if (v==u){
        return v;
    }
    for(int i=20;i>=0;i--){
        if (par[v][i] != par[u][i]) {
            v=par[v][i];
            u=par[u][i];
        }

    }
    return par[v][0];
}
ll vis[N];
int32_t main(){
    ll n,m;
    cin >> n >> m;
    for (int i=0;i<m;i++){
        ll u,v,w;
        cin >> u >> v >> w;
        yal.pb({w,{u,v}});
    }
    for (int i=0;i<N;i++){
        par1[i]=i;
    }
    sort(yal.begin(),yal.end());
    ll s=0;
    for (int i=0;i<yal.size();i++){
        ll u=yal[i].S.F,v=yal[i].S.S,u1=getpar1(u),v1=getpar1(v),w=yal[i].F;
        if (u1==v1) continue;
        s+=w;
        par1[u1]=v1;
        g[u].pb({v,w});
        g[v].pb({u,w});
    }
    dfs(1,1,1);
    par[1][0]=1;
    for (int i=1;i<M;i++){
        for (int j=1;j<=n;j++){
            par[j][i]=par[par[j][i-1]][i-1];
            pr[j][i]=max(pr[j][i-1],pr[par[j][i-1]][i-1]);
        }
    }
    ll p1=0;
    for (int i=1;i<=n;i++){
        ll x=getpar1(i);
        if (!vis[x]) p1++;
        vis[x]=1;
    }
    ll q;
    cin >> q;
    for (int i=0;i<q;i++){
        ll u,v;
        cin >> u >> v;
        if (p1>2) {
            cout << -1 << endl;
            continue;
        }
        if (p1==2 && par1[u]==par1[v]){
            cout << -1 << endl;
            continue;
        }
        if (p1==2){
            cout << s << endl;
            continue;
        }
      //  cout << u << " " << v << " " << lca(u,v) << " " << par[1][1] << " " << par[2][1] << endl;
        ll f=lca(u,v);
        ll t=get(u,hi[u]-hi[f]);
        ll x=get(v,hi[v]-hi[f]);
        cout << s-max(x,t) << endl;
    }

}

		  	    		   	    	  			 			//hellobig_tq
//orzlxr!//hello


#include <bits/stdc++.h>
using namespace std;

const int EXP = 19;
const int DIM = 200009;

struct str {
    int a, b, c;
} edg[DIM], teg[DIM];

vector<pair<int, int>> ged[DIM];
int fth[DIM], anc[DIM][EXP], mxm[DIM][EXP], lev[DIM];

inline bool cmp(str e1, str e2)
{
    return e1.c < e2.c;
}

inline int getroot(int x)
{
    return fth[x] < 0 ? x : getroot(fth[x]);
}

void dfs(int x, int f, int c)
{
    lev[x] = lev[f] + 1;
    anc[x][0] = f; mxm[x][0] = c;

    for (int i = 1; i < EXP; ++i) {
        anc[x][i] = anc[anc[x][i - 1]][i - 1];
        mxm[x][i] = max(mxm[x][i - 1], mxm[anc[x][i - 1]][i - 1]);
    }

    for (pair<int, int> y : ged[x])
        if (y.first != f) dfs(y.first, x, y.second);
}

int query(int x, int y)
{
    int ans = -1e9;
    if (lev[x] > lev[y])
        swap(x, y);

    for (int i = EXP - 1; i >= 0; --i) {
        if (lev[y] - (1 << i) < lev[x])
            continue;

        ans = max(ans, mxm[y][i]);
        y = anc[y][i];
    }

    for (int i = EXP - 1; i >= 0; --i) {
        if (anc[x][i] == anc[y][i])
            continue;

        ans = max(ans, max(mxm[x][i], mxm[y][i]));
        x = anc[x][i]; y = anc[y][i];
    }

    if (x == y)
        return ans;
    else
        return max(ans, max(mxm[x][0], mxm[y][0]));
}

int main(void)
{
    int n, m;
    cin >> n >> m;

    for (int i = 1; i <= m; ++i)
        cin >> edg[i].a >> edg[i].b >> edg[i].c;

    sort(edg + 1, edg + m + 1, cmp);
    fill(fth + 1, fth + n + 1, -1);

    long long s = 0; int k = 0;
    for (int i = 1; i <= m; ++i) {
        int x = getroot(edg[i].a),
            y = getroot(edg[i].b);

        if (x == y)
            continue;
        if (fth[x] > fth[y])
            swap(x, y);

        ged[edg[i].a].push_back(make_pair(edg[i].b, edg[i].c));
        ged[edg[i].b].push_back(make_pair(edg[i].a, edg[i].c));

        fth[x] += fth[y]; fth[y] = x;
        teg[++k] = edg[i]; s += edg[i].c;
    }

    if (k == n - 1)
        dfs(1, 0, 0);

    int q;
    cin >> q;

    while (q--) {
        int x, y;
        cin >> x >> y;

        if (k < n - 2)
            cout << -1 << "\n";
        else
        if (k == n - 2) {
            int _x = getroot(x), _y = getroot(y);

            if (_x == _y)
                cout << -1 << "\n";
            else
                cout << s << "\n";
        } else
            cout << s - query(x, y) << "\n";
    }

    return 0;
}


//In His Name
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define F first
#define S second
const int MAX=100100,MAXP=32;
ll n,m,q,te,a,b,ans,mx,sum;
bool mark[MAX];
ll par[MAX][MAXP],ma[MAX][MAXP],d[MAX];
vector<pair<ll,pair<ll,ll> > >e;
vector<ll>adj[MAX],w[MAX];
class DSU{
	public: int p[MAX],sz[MAX];
	public:
	void mkset(){
		for(int i=0;i<MAX;i++)
			p[i]=i,sz[i]=0;
	}
	int findpar(int x){
		return (p[x]==x?x:findpar(p[x]));
	}
	void merge(int x,int y){
		x=findpar(x),y=findpar(y);
		if(x==y) return;
		te--;
		if(sz[x]<sz[y]) swap(x,y);
		p[y]=x;
		sz[x]+=(sz[x]==sz[y]);
	}
	bool tof(int x,int y){
		x=findpar(x),y=findpar(y);
		return (x!=y);
	}
}dsu;
void dfs(int v){
	mark[v]=true;
	for(int i=0;i<adj[v].size();i++){
		if(mark[adj[v][i]]) continue;
		int u=adj[v][i];
		d[u]=d[v]+1;
		par[u][0]=v;
		ma[u][0]=w[v][i];
		for(int i=1;i<MAXP;i++){
			par[u][i]=par[par[u][i-1]][i-1];
			ma[u][i]=max(ma[par[u][i-1]][i-1],ma[u][i-1]);
		}
		dfs(u);
	}
	return;
}
pair<ll,ll> parent(int x,int d){
	ll ans=x,mx=0;
	for(int i=0;i<MAXP;i++)
		if(((d>>i)&1)){
			mx=max(mx,ma[ans][i]);
			ans=par[ans][i];
		}
	return make_pair(ans,mx);
}
pair<ll,ll> lca(int x,int y){
	if(d[x]>d[y]) swap(x,y);
	pair<int,int>p=parent(y,d[y]-d[x]);
	ll mx=p.S;
	y=p.F;
	if(x==y) return make_pair(x,mx);
	for(int i=MAXP-1;i>=0;i--)
		if(par[x][i]!=par[y][i]){
			mx=max(mx,max(ma[x][i],ma[y][i]));
			x=par[x][i];
			y=par[y][i];
		}
	mx=max(mx,max(ma[x][0],ma[y][0]));
	return make_pair(par[x][0],mx);
}
int main(){
	ios_base::sync_with_stdio(false);
	dsu.mkset();
	cin>>n>>m;
	for(int i=0;i<m;i++){
		int x,y,we;
		cin>>x>>y>>we;
		e.push_back(make_pair(we,make_pair(x,y)));
	}
	te=n;
	sort(e.begin(),e.end());
	for(int i=0;i<e.size();i++)
		if(dsu.tof(e[i].S.F,e[i].S.S)){
			adj[e[i].S.F].push_back(e[i].S.S);
			w[e[i].S.F].push_back(e[i].F);
			adj[e[i].S.S].push_back(e[i].S.F);
			w[e[i].S.S].push_back(e[i].F);
			sum+=e[i].F;
			dsu.merge(e[i].S.F,e[i].S.S);
		}
	cin>>q;
	dfs(1);
	while(q--){
		cin>>a>>b;
		if(te>2){
			cout<<-1<<endl;
			continue;
		}
		if(te==1)
			cout<<sum-lca(a,b).S<<endl;
		if(te==2){
			if(!dsu.tof(a,b)){
				cout<<-1<<endl;
				continue;
			}
			cout<<sum<<endl;
		}
		mx=0;
	}
	return 0;
}


//In His Name
#include<bits/stdc++.h>
using namespace std;
#define F first
#define S second
const int MAX=100100,MAXP=32;
int n,m,q,te,sum,a,b,mx;
bool mark[MAX];
int par[MAX][MAXP],ma[MAX][MAXP],d[MAX];
vector<pair<int,pair<int,int> > >e;
vector<pair<int,int> >mst;
vector<int>adj[MAX],w[MAX];
class DSU{
	public: int p[MAX],sz[MAX];
	public:
	void mkset(){
		for(int i=0;i<MAX;i++)
			p[i]=i,sz[i]=0;
	}
	int findpar(int x){
		return (p[x]==x?x:findpar(p[x]));
	}
	void merge(int x,int y){
		x=findpar(x),y=findpar(y);
		if(x==y) return;
		if(sz[x]<sz[y]) swap(x,y);
		p[y]=x;
		sz[x]+=(sz[x]==sz[y]);
	}
	bool tof(int x,int y){
		x=findpar(x),y=findpar(y);
		return (x!=y);
	}
}dsu;
void dfs(int v){
	mark[v]=true;
	for(int i=0;i<adj[v].size();i++){
		if(mark[adj[v][i]]) continue;
		int u=adj[v][i];
		d[u]=d[v]+1;
		par[u][0]=v;
		ma[u][0]=w[v][i];
		for(int i=1;i<MAXP;i++){
			par[u][i]=par[par[u][i-1]][i-1];
			ma[u][i]=max(ma[par[u][i-1]][i-1],ma[u][i-1]);
		}
		dfs(u);
	}
	return;
}
int parent(int x,int d){
	int ans=x;
	for(int i=0;i<MAXP;i++)
		if(((d>>i)&1))
			ans=par[ans][i];
	return ans;
}
int lca(int x,int y){
	mx=0;
	if(d[x]>d[y]) swap(x,y);
	mx=ma[y][d[y]-d[x]];
	y=parent(y,d[y]-d[x]);
	if(x==y) return x;
	for(int i=MAXP-1;i>=0;i--)
		if(par[x][i]!=par[y][i]){
			mx=max(mx,max(ma[x][i],ma[y][i]));
			x=par[x][i];
			y=par[y][i];
		}
	mx=max(mx,max(ma[x][0],ma[y][0]));
	return par[x][0];
}
int main(){
	ios_base::sync_with_stdio(false);
	dsu.mkset();
	cin>>n>>m;
	for(int i=0;i<m;i++){
		int x,y,we;
		cin>>x>>y>>we;
		e.push_back(make_pair(we,make_pair(x,y)));
	}
	te=n;
	sort(e.begin(),e.end());
	for(int i=e.size()-1;i>=0;i--)
		if(dsu.tof(e[i].S.F,e[i].S.S)){
			mst.push_back(make_pair(e[i].S.F,e[i].S.S));
			adj[e[i].S.F].push_back(e[i].S.S);
			w[e[i].S.F].push_back(e[i].F);
			adj[e[i].S.S].push_back(e[i].S.F);
			w[e[i].S.F].push_back(e[i].F);
			te--;
			sum+=e[i].F;
			dsu.merge(e[i].S.F,e[i].S.S);
		}
	cin>>q;
	dfs(1);
	while(q--){
		cin>>a>>b;
		if(te>2){
			cout<<-1<<endl;
			continue;
		}
		if(te==1){
			int tmp=lca(a,b);
			cout<<sum-mx<<endl;
		}
		if(te==2){
			if(!dsu.tof(a,b)){
				cout<<-1<<endl;
				continue;
			}
			cout<<sum<<endl;
		}
	}
	return 0;
}


//In His Name
#include<bits/stdc++.h>
using namespace std;
#define F first
#define S second
const int MAX=100100,MAXP=32;
int n,m,q,te,sum,a,b,mx;
bool mark[MAX];
int par[MAX][MAXP],ma[MAX][MAXP],d[MAX];
vector<pair<int,pair<int,int> > >e;
vector<int>adj[MAX],w[MAX];
class DSU{
	public: int p[MAX],sz[MAX];
	public:
	void mkset(){
		for(int i=0;i<MAX;i++)
			p[i]=i,sz[i]=0;
	}
	int findpar(int x){
		return (p[x]==x?x:findpar(p[x]));
	}
	void merge(int x,int y){
		x=findpar(x),y=findpar(y);
		if(x==y) return;
		te--;
		if(sz[x]<sz[y]) swap(x,y);
		p[y]=x;
		sz[x]+=(sz[x]==sz[y]);
	}
	bool tof(int x,int y){
		x=findpar(x),y=findpar(y);
		return (x!=y);
	}
}dsu;
void dfs(int v){
	mark[v]=true;
	for(int i=0;i<adj[v].size();i++){
		if(mark[adj[v][i]]) continue;
		int u=adj[v][i];
		d[u]=d[v]+1;
		par[u][0]=v;
		ma[u][0]=w[v][i];
		for(int i=1;i<MAXP;i++){
			par[u][i]=par[par[u][i-1]][i-1];
			ma[u][i]=max(ma[par[u][i-1]][i-1],ma[u][i-1]);
		}
		dfs(u);
	}
	return;
}
pair<int,int> parent(int x,int d){
	int ans=x,mx=0;
	for(int i=0;i<MAXP;i++)
		if(((d>>i)&1)){
			mx=max(mx,ma[ans][i]);
			ans=par[ans][i];
		}
	return make_pair(ans,mx);
}
pair<int,int> lca(int x,int y){
	if(d[x]>d[y]) swap(x,y);
	pair<int,int>p=parent(y,d[y]-d[x]);
	int mx=p.S;
	y=p.F;
	if(x==y) return make_pair(x,mx);
	for(int i=MAXP-1;i>=0;i--)
		if(par[x][i]!=par[y][i]){
			mx=max(mx,max(ma[x][i],ma[y][i]));
			x=par[x][i];
			y=par[y][i];
		}
	mx=max(mx,max(ma[x][0],ma[y][0]));
	return make_pair(par[x][0],mx);
}
int main(){
	ios_base::sync_with_stdio(false);
	dsu.mkset();
	cin>>n>>m;
	for(int i=0;i<m;i++){
		int x,y,we;
		cin>>x>>y>>we;
		e.push_back(make_pair(we,make_pair(x,y)));
	}
	te=n;
	sort(e.begin(),e.end());
	for(int i=0;i<e.size();i++)
		if(dsu.tof(e[i].S.F,e[i].S.S)){
			adj[e[i].S.F].push_back(e[i].S.S);
			w[e[i].S.F].push_back(e[i].F);
			adj[e[i].S.S].push_back(e[i].S.F);
			w[e[i].S.S].push_back(e[i].F);
			sum+=e[i].F;
			dsu.merge(e[i].S.F,e[i].S.S);
		}
	cin>>q;
	dfs(1);
	while(q--){
		cin>>a>>b;
		if(te>2){
			cout<<-1<<endl;
			continue;
		}
		if(te==1)
			cout<<sum-lca(a,b).S<<endl;
		if(te==2){
			if(!dsu.tof(a,b)){
				cout<<-1<<endl;
				continue;
			}
			cout<<sum<<endl;
		}
		mx=0;
	}
	return 0;
}


//In His Name
#include<bits/stdc++.h>
using namespace std;
#define F first
#define S second
const int MAX=100100,MAXP=32;
int n,m,q,te,sum,a,b,mx;
bool mark[MAX];
int par[MAX][MAXP],ma[MAX][MAXP],d[MAX];
vector<pair<int,pair<int,int> > >e;
vector<int>adj[MAX],w[MAX];
class DSU{
	public: int p[MAX],sz[MAX];
	public:
	void mkset(){
		for(int i=0;i<MAX;i++)
			p[i]=i,sz[i]=0;
	}
	int findpar(int x){
		return (p[x]==x?x:findpar(p[x]));
	}
	void merge(int x,int y){
		x=findpar(x),y=findpar(y);
		if(x==y) return;
		te--;
		if(sz[x]<sz[y]) swap(x,y);
		p[y]=x;
		sz[x]+=(sz[x]==sz[y]);
	}
	bool tof(int x,int y){
		x=findpar(x),y=findpar(y);
		return (x!=y);
	}
}dsu;
void dfs(int v){
	mark[v]=true;
	for(int i=0;i<adj[v].size();i++){
		if(mark[adj[v][i]]) continue;
		int u=adj[v][i];
		d[u]=d[v]+1;
		par[u][0]=v;
		ma[u][0]=w[v][i];
		for(int i=1;i<MAXP;i++){
			par[u][i]=par[par[u][i-1]][i-1];
			ma[u][i]=max(ma[par[u][i-1]][i-1],ma[u][i-1]);
		}
		dfs(u);
	}
	return;
}
int parent(int x,int d){
	int ans=x;
	for(int i=0;i<MAXP;i++)
		if(((d>>i)&1)){
			mx=max(mx,ma[ans][i]);
			ans=par[ans][i];
		}
	return ans;
}
int lca(int x,int y){
	if(d[x]>d[y]) swap(x,y);
	y=parent(y,d[y]-d[x]);
	if(x==y) return x;
	for(int i=MAXP-1;i>=0;i--)
		if(par[x][i]!=par[y][i]){
			mx=max(mx,max(ma[x][i],ma[y][i]));
			x=par[x][i];
			y=par[y][i];
		}
	mx=max(mx,max(ma[x][0],ma[y][0]));
	return par[x][0];
}
int main(){
	ios_base::sync_with_stdio(false);
	dsu.mkset();
	cin>>n>>m;
	for(int i=0;i<m;i++){
		int x,y,we;
		cin>>x>>y>>we;
		e.push_back(make_pair(we,make_pair(x,y)));
	}
	te=n;
	sort(e.begin(),e.end());
	for(int i=e.size()-1;i>=0;i--)
		if(dsu.tof(e[i].S.F,e[i].S.S)){
			adj[e[i].S.F].push_back(e[i].S.S);
			w[e[i].S.F].push_back(e[i].F);
			adj[e[i].S.S].push_back(e[i].S.F);
			w[e[i].S.F].push_back(e[i].F);
			sum+=e[i].F;
			dsu.merge(e[i].S.F,e[i].S.S);
		}
	cin>>q;
	dfs(1);
	while(q--){
		cin>>a>>b;
		if(te>2){
			cout<<-1<<endl;
			continue;
		}
		if(te==1){
			int tmp=lca(a,b);
			cout<<sum-mx<<endl;
		}
		if(te==2){
			if(!dsu.tof(a,b)){
				cout<<-1<<endl;
				continue;
			}
			cout<<sum<<endl;
		}
		mx=0;
	}
	return 0;
}


//In His Name
#include<bits/stdc++.h>
using namespace std;
#define F first
#define S second
const int MAX=100100,MAXP=32;
int n,m,q,te,sum,a,b,mx;
bool mark[MAX];
int par[MAX][MAXP],ma[MAX][MAXP],d[MAX];
vector<pair<int,pair<int,int> > >e;
vector<pair<int,int> >mst;
vector<int>adj[MAX],w[MAX];
class DSU{
	public: int p[MAX],sz[MAX];
	public:
	void mkset(){
		for(int i=0;i<MAX;i++)
			p[i]=i,sz[i]=0;
	}
	int findpar(int x){
		return (p[x]==x?x:findpar(p[x]));
	}
	void merge(int x,int y){
		x=findpar(x),y=findpar(y);
		if(x==y) return;
		if(sz[x]<sz[y]) swap(x,y);
		p[y]=x;
		sz[x]+=(sz[x]==sz[y]);
	}
	bool tof(int x,int y){
		x=findpar(x),y=findpar(y);
		return (x!=y);
	}
}dsu;
void dfs(int v){
	mark[v]=true;
	for(int i=0;i<adj[v].size();i++){
		if(mark[adj[v][i]]) continue;
		int u=adj[v][i];
		d[u]=d[v]+1;
		par[u][0]=v;
		ma[u][0]=w[v][i];
		for(int i=1;i<MAXP;i++){
			par[u][i]=par[par[u][i-1]][i-1];
			ma[u][i]=max(ma[par[u][i-1]][i-1],ma[u][i-1]);
		}
		dfs(u);
	}
	return;
}
int parent(int x,int d){
	int ans=x;
	for(int i=0;i<MAXP;i++)
		if(((d>>i)&1)){
			mx=max(mx,ma[ans][i]);
			ans=par[ans][i];
		}
	return ans;
}
int lca(int x,int y){
	mx=0;
	if(d[x]>d[y]) swap(x,y);
	y=parent(y,d[y]-d[x]);
	if(x==y) return x;
	for(int i=MAXP-1;i>=0;i--)
		if(par[x][i]!=par[y][i]){
			mx=max(mx,max(ma[x][i],ma[y][i]));
			x=par[x][i];
			y=par[y][i];
		}
	mx=max(mx,max(ma[x][0],ma[y][0]));
	return par[x][0];
}
int main(){
	ios_base::sync_with_stdio(false);
	dsu.mkset();
	cin>>n>>m;
	for(int i=0;i<m;i++){
		int x,y,we;
		cin>>x>>y>>we;
		e.push_back(make_pair(we,make_pair(x,y)));
	}
	te=n;
	sort(e.begin(),e.end());
	for(int i=e.size()-1;i>=0;i--)
		if(dsu.tof(e[i].S.F,e[i].S.S)){
			mst.push_back(make_pair(e[i].S.F,e[i].S.S));
			adj[e[i].S.F].push_back(e[i].S.S);
			w[e[i].S.F].push_back(e[i].F);
			adj[e[i].S.S].push_back(e[i].S.F);
			w[e[i].S.F].push_back(e[i].F);
			te--;
			sum+=e[i].F;
			dsu.merge(e[i].S.F,e[i].S.S);
		}
	cin>>q;
	dfs(1);
	while(q--){
		cin>>a>>b;
		if(te>2){
			cout<<-1<<endl;
			continue;
		}
		if(te==1){
			int tmp=lca(a,b);
			cout<<sum-mx<<endl;
		}
		if(te==2){
			if(!dsu.tof(a,b)){
				cout<<-1<<endl;
				continue;
			}
			cout<<sum<<endl;
		}
	}
	return 0;
}


//lxrtxdy!
#include <bits/stdc++.h>
typedef long long int ll;
typedef long double ld;
#define pb push_back
#define pii pair < ll , ll >
#define F first
#define S second
#define int long long
#define sync ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#pragma GCC optimize ("Ofast")
#pragma GCC optimize ("unroll-loops")
using namespace std;
/// khodaya komak kon
/// ya navid navid
const int N=2e5+100,M=22;
ll par1[N];
vector <pii> g[N];
ll getpar1(ll v){
    if (par1[v]!=v) return par1[v]=getpar1(par1[v]);

    return v;
}
vector <pair <int,pii> > yal;
ll par[N][M];
ll pr[N][M];
ll hi[N];
ll dfs(ll v,ll p,ll h){
    hi[v]=h;
    for (int i=0;i<g[v].size();i++){
        ll u=g[v][i].F;
        if (u!=p){
            par[u][0]=v;
            pr[u][0]=g[v][i].S;
            dfs(u,v,h+1);
        }
    }
}
ll get(ll v,ll h){
    if (h==-1 || h==0){
        return 0;
    }
    ll ans=0;
    for(int i=0;i<M;i++){
        if (h & (1 << i)){
            ans=max(ans,pr[v][i]);
            v = par[v][i];
        }
    }
    return ans;
}
ll getpar(ll v,ll h){
    if (h==-1 || h==0){
        return v;
    }
    for(int i=0;i<M;i++){
        if (h & (1 << i)) v = par[v][i];
    }
    return v;
}
int lca(int v, int u) {
    if (hi[v]<hi[u]){
        ll y=v;
        v=u;
        u=y;
    }
    v = getpar(v,hi[v]-hi[u]);
    if (v==u){
        return v;
    }
    for(int i=20;i>=0;i--){
        if (par[v][i] != par[u][i]) {
            v=par[v][i];
            u=par[u][i];
        }

    }
    return par[v][0];
}
ll vis[N];
int32_t main(){
    ll n,m;
    cin >> n >> m;
    for (int i=0;i<m;i++){
        ll u,v,w;
        cin >> u >> v >> w;
        yal.pb({w,{u,v}});
    }
    for (int i=0;i<N;i++){
        par1[i]=i;
    }
    sort(yal.begin(),yal.end());
    ll s=0;
    for (int i=0;i<yal.size();i++){
        ll u=yal[i].S.F,v=yal[i].S.S,u1=getpar1(u),v1=getpar1(v),w=yal[i].F;
        if (u1==v1) continue;
        s+=w;
        par1[u1]=v1;
        g[u].pb({v,w});
        g[v].pb({u,w});
    }
    dfs(1,1,1);
    par[1][0]=1;
    for (int i=1;i<M;i++){
        for (int j=1;j<=n;j++){
            par[j][i]=par[par[j][i-1]][i-1];
            pr[j][i]=max(pr[j][i-1],pr[par[j][i-1]][i-1]);
        }
    }
    ll p1=0;
    for (int i=1;i<=n;i++){
        ll x=getpar1(i);
        if (!vis[x]) p1++;
        vis[x]=1;
    }
    ll q;
    cin >> q;
    for (int i=0;i<q;i++){
        ll u,v;
        cin >> u >> v;
        if (p1>2) {
            cout << -1 << endl;
            continue;
        }
        if (p1==2 && par1[u]==par1[v]){
            cout << -1 << endl;
            continue;
        }
        if (p1==2){
            cout << s << endl;
            continue;
        }
      //  cout << u << " " << v << " " << lca(u,v) << " " << par[1][1] << " " << par[2][1] << endl;
        ll f=lca(u,v);
        ll t=get(u,hi[u]-hi[f]);
        ll x=get(v,hi[v]-hi[f]);
        cout << s-max(x,t) << endl;
    }

}

		  	    		   	    	  			 			//hellobig_tq
//orzlxr!


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define nn 200008
#define lg 18
#define err {puts("-1");exit(0);};
int n;int m;
struct edge{
	int x,y,cost;
}e[nn];
bool operator<(edge a,edge b)
{
	return a.cost<b.cost;
}
int pa[nn][lg+5];int maxi[nn][lg+5];vector<int> v[nn],c[nn];int depth[nn];ll ans;
int bel[nn];int trees[nn];
int fin(int x)
{
	if(bel[x]==x) return x;
	return bel[x]=fin(bel[x]);
}
int lca(int x,int y)
{
	int res=0;
	if(depth[x]<depth[y]) swap(x,y);
	for(int i=lg;i>=0;i--)
	{
		if(depth[pa[x][i]]>=depth[y])
		{
			res=max(res,maxi[x][i]);
			x=pa[x][i];
		}
	}
	if(x==y) return res;
	for(int i=lg;i>=0;i--)
	{
		if(pa[x][i]!=pa[y][i])
		{
			res=max(res,maxi[x][i]);
			res=max(res,maxi[y][i]);
			x=pa[x][i],y=pa[y][i];
		}
	}
	res=max(res,maxi[x][0]);
	res=max(res,maxi[y][0]);
//cout<<pa[x][0]<<"-----\n";
	return res;
}
void dfs(int x,int p)
{
	pa[x][0]=p;depth[x]=depth[p]+1;
	for(int i=0;i<v[x].size();i++)
	{
		int t=v[x][i];if(t==p) continue;
		maxi[t][0]=c[x][i];dfs(t,x);
	}
}
int main()
{
	cin>>n>>m;for(int i=1;i<=m;i++) scanf("%d%d%d",&e[i].x,&e[i].y,&e[i].cost);
	sort(e+1,e+m+1);
	for(int i=1;i<=n;i++) bel[i]=i;
	for(int i=1;i<=m;i++)
	{
		int x=e[i].x,y=e[i].y,cost=e[i].cost;
		if(fin(x)==fin(y)) continue;
		v[x].push_back(y);v[y].push_back(x);
		c[x].push_back(cost);c[y].push_back(cost);
		bel[fin(x)]=fin(y);
		ans+=cost;
	}
	for(int i=1;i<=n;i++) trees[fin(i)]=1;
	int tots=0;for(int i=1;i<=n;i++) tots+=trees[i];
	int T;cin>>T;
	if(tots==1)
	{
		dfs(1,0);
		for(int j=1;j<=lg;j++)
		{
			for(int i=1;i<=n;i++)
			{
				int t=pa[i][j-1];pa[i][j]=pa[t][j-1];
				maxi[i][j]=max(maxi[i][j-1],maxi[t][j-1]);
			}
		}
	}
	while(T--)
	{
		int x,y;scanf("%d%d",&x,&y);
		if(tots==1)
		{
			printf("%lld\n",ans-(lca(x,y)));
		}
		else
		{
			if(fin(x)!=fin(y) and tots==2)
				printf("%lld\n",ans);
			else
				printf("-1\n");
		}
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define nn 200008
#define lg 5
#define err {puts("-1");exit(0);};
int n;int m;
struct edge{
	int x,y,cost;
}e[nn];
bool operator<(edge a,edge b)
{
	return a.cost<b.cost;
}
int pa[nn][lg+5];int maxi[nn][lg+5];vector<int> v[nn],c[nn];int depth[nn];ll ans;
int bel[nn];int trees[nn];
int fin(int x)
{
	if(bel[x]==x) return x;
	return bel[x]=fin(bel[x]);
}
int lca(int x,int y)
{
	int res=0;
	if(depth[x]<depth[y]) swap(x,y);
	for(int i=lg;i>=0;i--)
	{
		if(depth[pa[x][i]]>=depth[y])
		{
			res=max(res,maxi[x][i]);
			x=pa[x][i];
		}
	}
	if(x==y) return res;
	for(int i=lg;i>=0;i--)
	{
		if(pa[x][i]!=pa[y][i])
		{
			res=max(res,maxi[x][i]);
			res=max(res,maxi[y][i]);
			x=pa[x][i],y=pa[y][i];
		}
	}
	res=max(res,maxi[x][0]);
	res=max(res,maxi[y][0]);
//cout<<pa[x][0]<<"-----\n";
	return res;
}
void dfs(int x,int p)
{
	pa[x][0]=p;depth[x]=depth[p]+1;
	for(int i=0;i<v[x].size();i++)
	{
		int t=v[x][i];if(t==p) continue;
		maxi[t][0]=c[x][i];dfs(t,x);
	}
}
int main()
{
	cin>>n>>m;for(int i=1;i<=m;i++) scanf("%d%d%d",&e[i].x,&e[i].y,&e[i].cost);
	sort(e+1,e+m+1);
	for(int i=1;i<=n;i++) bel[i]=i;
	for(int i=1;i<=m;i++)
	{
		int x=e[i].x,y=e[i].y,cost=e[i].cost;
		if(fin(x)==fin(y)) continue;
		v[x].push_back(y);v[y].push_back(x);
		c[x].push_back(cost);c[y].push_back(cost);
		bel[fin(x)]=fin(y);
		ans+=cost;
	}
	for(int i=1;i<=n;i++) trees[fin(i)]=1;
	int tots=0;for(int i=1;i<=n;i++) tots+=trees[i];
	int T;cin>>T;
	if(tots==1)
	{
		dfs(1,0);
		for(int j=1;j<=lg;j++)
		{
			for(int i=1;i<=n;i++)
			{
				int t=pa[i][j-1];pa[i][j]=pa[t][j-1];
				maxi[i][j]=max(maxi[i][j-1],maxi[t][j-1]);
			}
		}
	}
	while(T--)
	{
		int x,y;scanf("%d%d",&x,&y);
		if(tots==1)
		{
			int p=lca(x,y);
			printf("%lld\n",ans-p);
		}
		else
		{
			if(fin(x)!=fin(y) and tots==2)
				printf("%lld\n",ans);
			else
				printf("-1\n");
		}
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define nn 200008
#define lg 5
#define err {puts("-1");exit(0);};
int n;int m;
struct edge{
	int x,y,cost;
}e[nn];
bool operator<(edge a,edge b)
{
	return a.cost<b.cost;
}
int pa[nn][lg+5];int maxi[nn][lg+5];vector<int> v[nn],c[nn];int depth[nn];ll ans;
int bel[nn];int trees[nn];
int fin(int x)
{
	if(bel[x]==x) return x;
	return bel[x]=fin(bel[x]);
}
int lca(int x,int y)
{
	int res=0;
	if(depth[x]<depth[y]) swap(x,y);
	for(int i=lg;i>=0;i--)
	{
		if(depth[pa[x][i]]>=depth[y])
		{
			res=max(res,maxi[x][i]);
			x=pa[x][i];
		}
	}
	if(x==y) return res;
	for(int i=lg;i>=0;i--)
	{
		if(pa[x][i]!=pa[y][i])
		{
			res=max(res,maxi[x][i]);
			res=max(res,maxi[y][i]);
			x=pa[x][i],y=pa[y][i];
		}
	}
	res=max(res,maxi[x][0]);
	res=max(res,maxi[y][0]);
	return res;
}
void dfs(int x,int p)
{
	pa[x][0]=p;
	for(int i=0;i<v[x].size();i++)
	{
		int t=v[x][i];if(t==p) continue;
		maxi[t][0]=c[x][i];dfs(t,x);
	}
}
int main()
{
	cin>>n>>m;for(int i=1;i<=m;i++) scanf("%d%d%d",&e[i].x,&e[i].y,&e[i].cost);
	sort(e+1,e+m+1);
	for(int i=1;i<=n;i++) bel[i]=i;
	for(int i=1;i<=m;i++)
	{
		int x=e[i].x,y=e[i].y,cost=e[i].cost;
		if(fin(x)==fin(y)) continue;
		v[x].push_back(y);v[y].push_back(x);
		c[x].push_back(cost);c[y].push_back(cost);
		bel[fin(x)]=fin(y);
		ans+=cost;
	}
	for(int i=1;i<=n;i++) trees[fin(i)]=1;
	int tots=0;for(int i=1;i<=n;i++) tots+=trees[i];
	int T;cin>>T;
	if(tots==1)
	{
		dfs(1,0);
		for(int j=1;j<=lg;j++)
		{
			for(int i=1;i<=n;i++)
			{
				int t=pa[i][j-1];pa[i][j]=pa[t][j];
				maxi[i][j]=max(maxi[i][j-1],maxi[t][j-1]);
			}
		}
	}
	while(T--)
	{
		int x,y;scanf("%d%d",&x,&y);
		if(tots==1)
		{
			int p=lca(x,y);
			printf("%lld\n",ans-p);
		}
		else
		{
			if(fin(x)!=fin(y) and tots==2)
				printf("%lld\n",ans);
			else
				printf("-1\n");
		}
	}
	return 0;
}


#include <bits/stdc++.h>
typedef long long int ll;
typedef long double ld;
#define pb push_back
#define pii pair < ll , ll >
#define F first
#define S second
#define int long long
#define sync ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#pragma GCC optimize ("Ofast")
#pragma GCC optimize ("unroll-loops")
using namespace std;
/// khodaya komak kon
/// ya navid navid
const int N=2e5+100,M=22;
ll par1[N];
vector <pii> g[N];
ll getpar1(ll v){
    if (par1[v]!=v) return par1[v]=getpar1(par1[v]);

    return v;
}
vector <pair <int,pii> > yal;
ll par[N][M];
ll pr[N][M];
ll hi[N];
ll dfs(ll v,ll p,ll h){
    hi[v]=h;
    for (int i=0;i<g[v].size();i++){
        ll u=g[v][i].F;
        if (u!=p){
            par[u][0]=v;
            pr[u][0]=g[v][i].S;
            dfs(u,v,h+1);
        }
    }
}
ll get(ll v,ll h){
    if (h==-1 || h==0){
        return 0;
    }
    ll ans=0;
    for(int i=0;i<M;i++){
        if (h & (1 << i)){
            ans=max(ans,pr[v][i]);
            v = par[v][i];
        }
    }
    return ans;
}
ll getpar(ll v,ll h){
    if (h==-1 || h==0){
        return v;
    }
    for(int i=0;i<M;i++){
        if (h & (1 << i)) v = par[v][i];
    }
    return v;
}
int lca(int v, int u) {
    if (hi[v]<hi[u]){
        ll y=v;
        v=u;
        u=y;
    }
    v = getpar(v,hi[v]-hi[u]);
    if (v==u){
        return v;
    }
    for(int i=20;i>=0;i--){
        if (par[v][i] != par[u][i]) {
            v=par[v][i];
            u=par[u][i];
        }

    }
    return par[v][0];
}
ll vis[N];
int32_t main(){
    ll n,m;
    cin >> n >> m;
    for (int i=0;i<m;i++){
        ll u,v,w;
        cin >> u >> v >> w;
        yal.pb({w,{u,v}});
    }
    for (int i=0;i<N;i++){
        par1[i]=i;
    }
    sort(yal.begin(),yal.end());
    ll s=0;
    for (int i=0;i<yal.size();i++){
        ll u=yal[i].S.F,v=yal[i].S.S,u1=getpar1(u),v1=getpar1(v),w=yal[i].F;
        if (u1==v1) continue;
        s+=w;
        par1[u1]=v1;
        g[u].pb({v,w});
        g[v].pb({u,w});
    }
    dfs(1,1,1);
    par[1][0]=1;
    for (int i=1;i<M;i++){
        for (int j=1;j<=n;j++){
            par[j][i]=par[par[j][i-1]][i-1];
            pr[j][i]=max(pr[j][i-1],pr[par[j][i-1]][i-1]);
        }
    }
    ll p1=0;
    for (int i=1;i<=n;i++){
        ll x=getpar1(i);
        if (!vis[x]) p1++;
        vis[x]=1;
    }
    ll q;
    cin >> q;
    for (int i=0;i<q;i++){
        ll u,v;
        cin >> u >> v;
        if (p1>2) {
            cout << -1 << endl;
            continue;
        }
        if (p1==2 && par1[u]==par1[v]){
            cout << -1 << endl;
            continue;
        }
        if (p1==2){
            cout << s << endl;
            continue;
        }
      //  cout << u << " " << v << " " << lca(u,v) << " " << par[1][1] << " " << par[2][1] << endl;
        ll f=lca(u,v);
        ll t=get(u,hi[u]-hi[f]);
        ll x=get(v,hi[v]-hi[f]);
        cout << s-max(x,t) << endl;
    }

}

		  	    		   	    	  			 			//hellobig_tq


#include<bits/stdc++.h>
using namespace std;
#define ll long long
const ll N=200010;
struct edge{
	ll x,y,k;
}e[N];
ll n,m,x,y,p,q,cnt,siz[N],fa[N],d[N],b1[N],b2[N];
long long ans;
bool cmp(const edge&a,const edge&b){
	return a.k<b.k;
}
ll get(ll x){
	return !fa[x]?x:fa[x];
}
int main(){
	scanf("%lld%lld",&n,&m);
    for (ll i=1; i<=m; i++)
	    scanf("%lld%lld%lld",&e[i].x,&e[i].y,&e[i].k);
	sort(e+1,e+m+1,cmp);
    for (ll i=1; i<=n; i++)
	    siz[i]=1;
	p=n;
    for (ll i=1; i<=m; i++)
	    if (get(x=e[i].x)!=get(y=e[i].y)){
		    x=get(x);
            y=get(y);
		    if (siz[x]>siz[y])
                swap(x,y);
		    fa[x]=y;
            siz[y]+=siz[x];
		    d[x]=e[i].k;
		    ans+=e[i].k;
            p--;
	    }
	scanf("%lld",&q);
	while (q--){
		scanf("%lld%lld",&x,&y);
		++cnt;
        ll j,k=-1;
		for (j=x; j; j=fa[j]){
            b1[j]=cnt;
            b2[j]=k;
            k=max(k,d[j]);
        }
		k=-1;
		for (j=y; j; j=fa[j]){
            if (b1[j]==cnt) break;
            k=max(k,d[j]);
        }
		if (p>2||p==2&&j) puts("-1"); else
        if (p==2) printf("%lld\n",ans);
		else printf("%lld\n",ans-max(k,b2[j]));
	}
}


#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cstdio>
#define N 220000
#define rep(i,x,y) for(i=x;i<=y;++i)
using namespace std;
#define ll long long
struct ppp{
	ll x,y,k;
}e[N];
ll cnt,n,m,i,j,k,b1[N],b2[N],x,y,siz[N],fa[N],d[N],p,q;
long long ans;
bool cmp(const ppp&a,const ppp&b){
	return a.k<b.k;
}
ll get(ll x){
	return !fa[x]?x:get(fa[x]);
}
int main(){
	scanf("%lld%lld",&n,&m);
    for (ll i=1; i<=m; i++)
	    scanf("%lld%lld%lld",&e[i].x,&e[i].y,&e[i].k);
	sort(e+1,e+m+1,cmp);
    for (ll i=1; i<=n; i++)
	    siz[i]=1;
	p=n;
    for (ll i=1; i<=m; i++)
	    if (get(x=e[i].x)!=get(y=e[i].y)){
		    x=get(x);
            y=get(y);
		    if (siz[x]>siz[y])
                swap(x,y);
		    fa[x]=y;
            siz[y]+=siz[x];
		    d[x]=e[i].k;
		    ans+=e[i].k;
            p--;
	    }
	scanf("%lld",&q);
	while (q--){
		scanf("%lld%lld",&x,&y);
		++cnt;
        ll j,k=-1;
		for (j=x; j; j=fa[j]){
            b1[j]=cnt;
            b2[j]=k;
            k=max(k,d[j]);
        }
		k=-1;
		for (j=y; j; j=fa[j]){
            if (b1[j]==cnt) break;
            k=max(k,d[j]);
        }
		if (p>2||p==2&&j) puts("-1"); else
        if (p==2) printf("%lld\n",ans);
		else printf("%lld\n",ans-max(k,b2[j]));
	}
}


// default code for competitive programming
// ver 3.1415 {{{

// Includes
#include <bits/stdc++.h>

// Defines
#define NAME(x) #x
#define SZ(c) (int)(c).size()
#define ALL(c) (c).begin(), (c).end()
#define FOR(i, e) for( int i = 0 ; i < (e) ; i++ )
#define ITER(it, c) for(__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)
#define REP(i, s, e) for(int i = (s); i <= (e); i++)
#define REPD(i, s, e) for(int i = (s); i >= (e); i--)
#define IOS ios_base::sync_with_stdio( 0 )
#define DEBUG 1
#define fst first
#define snd second
#define PB push_back
#ifdef ONLINE_JUDGE
#define FILE( fn ) \
    freopen(fn".in","r",stdin); \
    freopen(fn".out","w",stdout);
#else
#define FILE( fn )
#endif

#ifdef AKAI
#define debug( ... ) fprintf( stderr , __VA_ARGS__ )
#else
#define debug( ... )
#endif

using namespace std;

// Typedefs
typedef double real;
typedef long long ll;
typedef pair<ll, int> pli;
typedef tuple<ll, int> tli;
typedef pair<int, int> pii;
typedef tuple<int, int> tii;
typedef unsigned long long ull;

// Some common const.
const double EPS = -1e8;
const double Pi = acos(-1);

// Equal for double
bool inline equ(double a, double b)
{return fabs(a - b) < EPS;}

int _R( int& x ) { return scanf( "%d" , &x ); }
int _R( ll& x ) { return scanf( "%" PRId64 , &x ); }
int _R( double& x ) { return scanf( "%lf" , &x ); }
int _R( char* s ) { return scanf( "%s" , s ); }

int R() { return 0; }

template< typename T1 , typename... T2 >
int R( T1& x , T2&... tail )
{
  int tmp = _R( x );
  return tmp + R( tail... );
}

template< typename Iter , typename F >
inline void out( Iter s , Iter e , F of )
{
  bool flag = 0;
  for( Iter it = s ; it != e ; it++ )
  {
    if( flag ) printf( " " );
    else flag = 1;
    of( *it );
  }
  puts( "" );
}

// }}}
// start ~~QAQ~~

const int MAXN = 1e5+10;

struct Edge {
  int u , v;
  ll c;
  bool operator<( const Edge& e ) const {
    return c < e.c;
  }
};

int n , m;
Edge es[ MAXN*2 ];

struct DSJ {
  int mom[ MAXN ] , sz[ MAXN ];
  void init() { REP( i , 1 , n ) mom[ i ] = i; }
  int find( int x ) {
    return mom[ x ] = mom[ x ] == x ? x : find( mom[ x ] );
  }
  void merge( int a , int b ) {
    a = find( a ) , b = find( b );
    mom[ b ] = a;
  }
} dsj;

struct Tree {
  static const int LOG = 22;
  vector< pii> g[ MAXN ];
  int dep[ MAXN ];
  int mom[ LOG ][ MAXN ] , mx[ LOG ][ MAXN ];
  int sz[ MAXN ] , root[ MAXN ];

  inline void adde( Edge& e ) {
    g[ e.u ].push_back( pii( e.v , e.c ) );
    g[ e.v ].push_back( pii( e.u , e.c ) );
  }

  void dfs( int u , int p , int c , int d , int rt ) {
    mom[ 0 ][ u ] = p;
    mx[ 0 ][ u ] = c;
    dep[ u ] = d;
    root[ u ] = rt;
    for( pii e : g[ u ] ) {
      int v , tc;tie( v , tc ) = e;
      if( v == p ) continue ;
      dfs( v , u , tc , d+1 , rt );
    }
  }

  inline void predo() {
    REP( i , 1 , n ) if( !dep[ i ] ) {
      dfs( i , -1 , 0 , 1 , i );
    }
    REP( j , 1 , LOG-1 ) REP( i , 1 , n ) {
      mom[ j ][ i ] = mom[ j-1 ][ mom[ j-1 ][ i ] ];
      mx[ j ][ i ] = max( mx[ j-1 ][ i ] , mx[ j-1 ][ mom[ j-1 ][ i ] ] );
    }
    REP( i , 1 , n ) sz[ root[ i ] ]++;
  }
  inline int lca( int a , int b ) {
    if( dep[ a ] < dep[ b ] ) swap( a , b );
    int jmp = dep[ a ]-dep[ b ];
    FOR( k , LOG ) if( (jmp>>k)&1 )
      a = mom[ k ][ a ];
    if( a == b ) return a;
    REPD( k , LOG-1 , 0 ) if( mom[ k ][ a ] != mom[ k ][ b ] ) {
      a = mom[ k ][ a ];
      b = mom[ k ][ b ];
    }
    return mom[ 0 ][ a ];
  }
  inline int qry( int a , int b ) {
    int anc = lca( a , b );
    int res = 0;
    {
      int jmp = dep[ a ]-dep[ anc ];
      FOR( k , LOG ) if( (jmp>>k)&1 ) {
        res = max( res , mx[ k ][ a ] );
        a = mom[ k ][ a ];
      }
    }
    {
      int jmp = dep[ b ]-dep[ anc ];
      FOR( k , LOG ) if( (jmp>>k)&1 ) {
        res = max( res , mx[ k ][ b ] );
        b = mom[ k ][ b ];
      }
    }
    return res;
  }
} tree;

int main()
{
  R( n , m );
  REP( i , 1 , m ) R( es[ i ].u , es[ i ].v , es[ i ].c );

  sort( es+1 , es+1+m );

  ll mst_cst = 0ll;
  dsj.init();
  REP( i , 1 , m ) {
    Edge& e = es[ i ];
    if( dsj.find( e.u ) != dsj.find( e.v ) ) {
      dsj.merge( e.u , e.v );
      mst_cst += e.c;
      tree.adde( e );
    }
  }
  tree.predo();

  int q;
  R( q );
  while( q-- ) {
    int a , b;R( a , b );
    if( tree.root[ a ] == tree.root[ b ] ) {
      if( tree.sz[ tree.root[ a ] ] < n ) puts( "-1" );
      else printf( "%I64d\n" , mst_cst-tree.qry( a , b ) );
    } else {
      if( tree.sz[ tree.root[ a ] ]+tree.sz[ tree.root[ b ] ] < n ) puts( "-1" );
      else printf( "%I64d\n" , mst_cst );
    }
  }
}


// default code for competitive programming
// ver 3.1415 {{{

// Includes
#include <bits/stdc++.h>

// Defines
#define NAME(x) #x
#define SZ(c) (int)(c).size()
#define ALL(c) (c).begin(), (c).end()
#define FOR(i, e) for( int i = 0 ; i < (e) ; i++ )
#define ITER(it, c) for(__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)
#define REP(i, s, e) for(int i = (s); i <= (e); i++)
#define REPD(i, s, e) for(int i = (s); i >= (e); i--)
#define IOS ios_base::sync_with_stdio( 0 )
#define DEBUG 1
#define fst first
#define snd second
#define PB push_back
#ifdef ONLINE_JUDGE
#define FILE( fn ) \
    freopen(fn".in","r",stdin); \
    freopen(fn".out","w",stdout);
#else
#define FILE( fn )
#endif

#ifdef AKAI
#define debug( ... ) fprintf( stderr , __VA_ARGS__ )
#else
#define debug( ... )
#endif

using namespace std;

// Typedefs
typedef double real;
typedef long long ll;
typedef pair<ll, int> pli;
typedef tuple<ll, int> tli;
typedef pair<int, int> pii;
typedef tuple<int, int> tii;
typedef unsigned long long ull;

// Some common const.
const double EPS = -1e8;
const double Pi = acos(-1);

// Equal for double
bool inline equ(double a, double b)
{return fabs(a - b) < EPS;}

int _R( int& x ) { return scanf( "%d" , &x ); }
int _R( ll& x ) { return scanf( "%" PRId64 , &x ); }
int _R( double& x ) { return scanf( "%lf" , &x ); }
int _R( char* s ) { return scanf( "%s" , s ); }

int R() { return 0; }

template< typename T1 , typename... T2 >
int R( T1& x , T2&... tail )
{
  int tmp = _R( x );
  return tmp + R( tail... );
}

template< typename Iter , typename F >
inline void out( Iter s , Iter e , F of )
{
  bool flag = 0;
  for( Iter it = s ; it != e ; it++ )
  {
    if( flag ) printf( " " );
    else flag = 1;
    of( *it );
  }
  puts( "" );
}

// }}}
// start ~~QAQ~~

const int MAXN = 1e5+10;

struct Edge {
  int u , v;
  ll c;
  bool operator<( const Edge& e ) const {
    return c < e.c;
  }
};

int n , m;
Edge es[ MAXN ];

struct DSJ {
  int mom[ MAXN ] , sz[ MAXN ];
  void init() { REP( i , 1 , n ) mom[ i ] = i; }
  int find( int x ) {
    return mom[ x ] = mom[ x ] == x ? x : find( mom[ x ] );
  }
  void merge( int a , int b ) {
    a = find( a ) , b = find( b );
    mom[ b ] = a;
  }
} dsj;

struct Tree {
  static const int LOG = 22;
  vector< pii> g[ MAXN ];
  int dep[ MAXN ];
  int mom[ LOG ][ MAXN ] , mx[ LOG ][ MAXN ];
  int sz[ MAXN ] , root[ MAXN ];

  inline void adde( Edge& e ) {
    g[ e.u ].push_back( pii( e.v , e.c ) );
    g[ e.v ].push_back( pii( e.u , e.c ) );
  }

  void dfs( int u , int p , int c , int d , int rt ) {
    mom[ 0 ][ u ] = p;
    mx[ 0 ][ u ] = c;
    dep[ u ] = d;
    root[ u ] = rt;
    for( pii e : g[ u ] ) {
      int v , tc;tie( v , tc ) = e;
      if( v == p ) continue ;
      dfs( v , u , tc , d+1 , rt );
    }
  }

  inline void predo() {
    REP( i , 1 , n ) if( !dep[ i ] ) {
      dfs( i , -1 , 0 , 1 , i );
    }
    REP( j , 1 , LOG-1 ) REP( i , 1 , n ) {
      mom[ j ][ i ] = mom[ j-1 ][ mom[ j-1 ][ i ] ];
      mx[ j ][ i ] = max( mx[ j-1 ][ i ] , mx[ j-1 ][ mom[ j-1 ][ i ] ] );
    }
    REP( i , 1 , n ) sz[ root[ i ] ]++;
  }
  inline int lca( int a , int b ) {
    if( dep[ a ] < dep[ b ] ) swap( a , b );
    int jmp = dep[ a ]-dep[ b ];
    FOR( k , LOG ) if( (jmp>>k)&1 )
      a = mom[ k ][ a ];
    if( a == b ) return a;
    REPD( k , LOG-1 , 0 ) if( mom[ k ][ a ] != mom[ k ][ b ] ) {
      a = mom[ k ][ a ];
      b = mom[ k ][ b ];
    }
    return mom[ 0 ][ a ];
  }
  inline int qry( int a , int b ) {
    int anc = lca( a , b );
    int res = 0;
    {
      int jmp = dep[ a ]-dep[ anc ];
      FOR( k , LOG ) if( (jmp>>k)&1 ) {
        res = max( res , mx[ k ][ a ] );
        a = mom[ k ][ a ];
      }
    }
    {
      int jmp = dep[ b ]-dep[ anc ];
      FOR( k , LOG ) if( (jmp>>k)&1 ) {
        res = max( res , mx[ k ][ b ] );
        b = mom[ k ][ b ];
      }
    }
    return res;
  }
} tree;

int main()
{
  R( n , m );
  REP( i , 1 , m ) R( es[ i ].u , es[ i ].v , es[ i ].c );

  sort( es+1 , es+1+m );

  ll mst_cst = 0ll;
  dsj.init();
  REP( i , 1 , m ) {
    Edge& e = es[ i ];
    if( dsj.find( e.u ) != dsj.find( e.v ) ) {
      dsj.merge( e.u , e.v );
      mst_cst += e.c;
      tree.adde( e );
    }
  }
  tree.predo();

  int q;
  R( q );
  while( q-- ) {
    int a , b;R( a , b );
    if( tree.root[ a ] == tree.root[ b ] ) {
      if( tree.sz[ tree.root[ a ] ] < n ) puts( "-1" );
      else printf( "%I64d\n" , mst_cst-tree.qry( a , b ) );
    } else {
      if( tree.sz[ tree.root[ a ] ]+tree.sz[ tree.root[ b ] ] < n ) puts( "-1" );
      else printf( "%I64d\n" , mst_cst );
    }
  }
}


import java.util.*;
// warm-up
public class GuiltyToTheKitchen {

	static void solve() {
		Scanner sc = new Scanner(System.in);
		double n=sc.nextDouble(), no=n, v=sc.nextDouble(), k=0, m1=Double.MAX_VALUE, m2=Double.MAX_VALUE;
		int i=0;
		double[] ing = new double[(int)n];
		double[] vol = new double[(int)n];
		while (no-->0) {
			ing[i] = sc.nextDouble();
			k+=ing[i++];
		}
		no=n; i=0;
		while (no-->0) vol[i++] = sc.nextDouble();
		for (i=0; i<(int)n; i++) m1=Math.min(vol[i]/ing[i], m1);
		System.out.println(Math.min(v, k*m1));
		sc.close();
	}

	public static void main(String args[]) {
		solve();
	}

}


import java.io.BufferedOutputStream;
import java.io.Closeable;
import java.io.DataInputStream;
import java.io.Flushable;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.InputMismatchException;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
	static class TaskAdapter implements Runnable {
		@Override
		public void run() {
			InputStream inputStream = System.in;
			OutputStream outputStream = System.out;
			FastReader in = new FastReader(inputStream);
			Output out = new Output(outputStream);
			AGuiltyToTheKitchen solver = new AGuiltyToTheKitchen();
			solver.solve(1, in, out);
			out.close();
		}
	}

	public static void main(String[] args) throws Exception {
		Thread thread = new Thread(null, new TaskAdapter(), "", 1<<29);
		thread.start();
		thread.join();
	}
	static class AGuiltyToTheKitchen {
		private final int iinf = 1_000_000_000;

		public AGuiltyToTheKitchen() {
		}

		public void solve(int kase, InputReader in, Output pw) {
			int n = in.nextInt();
			double v = in.nextInt();
			double[] a = new double[n], b = new double[n];
			for(int i = 0; i<n; i++) {
				a[i] = in.nextInt();
			}
			for(int i = 0; i<n; i++) {
				b[i] = in.nextInt();
			}
			double x = iinf;
			for(int i = 0; i<n; i++) {
				x = Math.min(x, b[i]/a[i]);
			}
			double ans = 0;
			for(int i = 0; i<n; i++) {
				ans += x*a[i];
			}
			pw.printf("%.4f\n", Math.min(ans, v));
		}

	}

	static class Output implements Closeable, Flushable {
		public StringBuilder sb;
		public OutputStream os;
		public int BUFFER_SIZE;
		public boolean autoFlush;
		public String LineSeparator;

		public Output(OutputStream os) {
			this(os, 1<<16);
		}

		public Output(OutputStream os, int bs) {
			BUFFER_SIZE = bs;
			sb = new StringBuilder(BUFFER_SIZE);
			this.os = new BufferedOutputStream(os, 1<<17);
			autoFlush = false;
			LineSeparator = System.lineSeparator();
		}

		public void print(String s) {
			sb.append(s);
			if(autoFlush) {
				flush();
			}else if(sb.length()>BUFFER_SIZE >> 1) {
				flushToBuffer();
			}
		}

		public void printf(String s, Object... o) {
			print(String.format(s, o));
		}

		private void flushToBuffer() {
			try {
				os.write(sb.toString().getBytes());
			}catch(IOException e) {
				e.printStackTrace();
			}
			sb = new StringBuilder(BUFFER_SIZE);
		}

		public void flush() {
			try {
				flushToBuffer();
				os.flush();
			}catch(IOException e) {
				e.printStackTrace();
			}
		}

		public void close() {
			flush();
			try {
				os.close();
			}catch(IOException e) {
				e.printStackTrace();
			}
		}

	}

	static interface InputReader {
		int nextInt();

	}

	static class FastReader implements InputReader {
		final private int BUFFER_SIZE = 1<<16;
		private DataInputStream din;
		private byte[] buffer;
		private int bufferPointer;
		private int bytesRead;

		public FastReader(InputStream is) {
			din = new DataInputStream(is);
			buffer = new byte[BUFFER_SIZE];
			bufferPointer = bytesRead = 0;
		}

		public int nextInt() {
			int ret = 0;
			byte c = skipToDigit();
			boolean neg = (c=='-');
			if(neg) {
				c = read();
			}
			do {
				ret = ret*10+c-'0';
			} while((c = read())>='0'&&c<='9');
			if(neg) {
				return -ret;
			}
			return ret;
		}

		private boolean isDigit(byte b) {
			return b>='0'&&b<='9';
		}

		private byte skipToDigit() {
			byte ret;
			while(!isDigit(ret = read())&&ret!='-') ;
			return ret;
		}

		private void fillBuffer() {
			try {
				bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
			}catch(IOException e) {
				e.printStackTrace();
				throw new InputMismatchException();
			}
			if(bytesRead==-1) {
				buffer[0] = -1;
			}
		}

		private byte read() {
			if(bytesRead==-1) {
				throw new InputMismatchException();
			}else if(bufferPointer==bytesRead) {
				fillBuffer();
			}
			return buffer[bufferPointer++];
		}

	}
}


import java.io.*;
import java.util.*;

public class MyClass {

    static class FastReader {
        BufferedReader br;
        StringTokenizer st;
        public FastReader() {
            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;
        }
    }

    public static void main(String args[]) {
        FastReader fr = new FastReader();
        int n = fr.nextInt(), v = fr.nextInt();
        int[] a = new int[n], b = new int[n];
        for (int i = 0; i < n; i += 1) a[i] = fr.nextInt();
        for (int i = 0; i < n; i += 1) b[i] = fr.nextInt();
        int sum = 0;
        double t = 1E10;
        for (int i = 0; i < n; i += 1) {
            sum += a[i];
            t = Math.min(t, 1.0 * b[i] / a[i]);
        }
        System.out.println(Math.min(v, sum * t));
    }
}