#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 75;
pair<int, int> dist[N][N];
pair<int, int> operator+(pair<int, int> a, pair<int, int> b){
return {a.first+b.first, a.second+b.second};
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
cin >> n >> m;
for(int i = 1; i <= n; ++i){
for(int j = 1; j <= n; ++j){
if(i == j) dist[i][j] = {0, 0};
else dist[i][j] = {1e9, 0};
}
}
while(m--){
int u, v, t;
cin >> u >> v >> t;
dist[u][v] = min(dist[u][v], {t, 1});
}
int k, q;
cin >> k >> q;
for(int l = 1; l <= n; ++l){
for(int i = 1; i <= n; ++i){
for(int j = 1; j <= n; ++j){
pair<int, int> t = dist[i][l]+dist[l][j];
if(t.second > k || t.first >= 1e9) continue;
dist[i][j] = min(dist[i][j], t);
}
}
}
while(q--){
int x, y;
cin >> x >> y;
if(dist[x][y].second > k || dist[x][y].first >= 1e9) cout << -1 << '\n';
else cout << dist[x][y].first << '\n';
}
}