#include <bits/stdc++.h>

#define rep(n) for(int i=0;i<n;i++)
#define repp(j, n) for(int j=0;j<n;j++)
#define reppp(i, m, n) for(int i=m;i<n;i++)
#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(), c.rend()
#define debug(x) cerr << #x << ": " << x << endl

using namespace std;

typedef long long ll;
typedef pair<ll, ll> Pll;
typedef pair<int, int> Pii;

struct Edge{
    int from, to; ll cost;
    Edge(int from, int to, ll cost){
        this->from = from;
        this->to = to;
        this->cost = cost;
    }
    bool operator<(const Edge &e) const{
        return cost < e.cost;
    }
    bool operator>(const Edge &e) const{
        return cost > e.cost;
    }
};

struct Graph{
    int V,E;
    vector<vector< pair<int, ll> > > edges;

    Graph(){
        cin >> V >> E;
        edges.resize(V);
        int s,t; ll w;
        rep(E){
            cin >> s >> t >> w;
            edges[s].emplace_back(t, w);
            edges[t].emplace_back(s, w);
        }
    }

    ll kruskal(){
        ll ret = 0LL;
        priority_queue<Edge, vector<Edge>, greater<Edge> > que;
        vector<bool> used(V, false);

        used[0] = true;
        for(pair<int, ll> e: edges[0])
            que.push(Edge(0, e.first, e.second));

        while(!que.empty()){
            Edge e = que.top(); que.pop();
            while(!que.empty() && used[e.to]){
                e = que.top(); que.pop();
            }
            if(used[e.to]) break;
            ret += e.cost;
            used[e.to] = true;
            for(pair<int, ll> next: edges[e.to])
                if(!used[next.first]) que.push(Edge(e.to, next.first, next.second));
        }
        return ret;
    }
};

int main(){
    Graph g = Graph();
    cout << g.kruskal() << endl;
}

#include <bits/stdc++.h>

const int MAXN = 100010;
int xs[MAXN], D, n, Q;
int R[MAXN][20];
int main() {
	std::ios_base::sync_with_stdio(false), std::cin.tie(0);
	std::cin >> n;
	for (int i = 1; i <= n; ++i) std::cin >> xs[i];
	std::cin >> D >> Q;
	for (int i = 1; i <= n; ++i)
		R[i][0] = std::upper_bound(xs + 1, xs + 1 + n, xs[i] + D) - xs - 1;
	for (int i = 1; i < 20; ++i)
		for (int j = 1; j <= n; ++j)
			R[j][i] = R[R[j][i - 1]][i - 1];
	while (Q --> 0) {
		int x, y, t = 0; std::cin >> x >> y; if (x > y) std::swap(x, y);
		for (int i = 19; ~i; --i) if (R[x][i] < y) t += 1 << i, x = R[x][i];
		std::cout << t + 1 << '\n';
	}
	return 0;
}
