#include <bits/stdc++.h>
using namespace std;
const long long N = 3e5 + 5, inf = 1e18 + 5;
int n;
int m;
int k;
int q;
long long dis[N];
int lastw;
long long ans[N];
int par[N];
int a, b, c;
vector<pair<long long, long long> > adj[N];
vector<int> ver[N];
vector<pair<long long, long long> > adjq[N];
vector<pair<long long, pair<long long, long long> > > edge;
void mdij() {
  set<pair<long long, long long> > st;
  for (long long i = 0; i < n; i++) dis[i] = inf;
  for (long long i = 0; i < k; i++) {
    dis[i] = 0;
    st.insert({0, i});
  }
  while (st.size()) {
    auto u = *st.begin();
    st.erase(u);
    for (auto v : adj[u.second]) {
      if (dis[v.first] > dis[u.second] + v.second) {
        st.erase({dis[v.first], v.first});
        dis[v.first] = dis[u.second] + v.second;
        st.insert({dis[v.first], v.first});
      }
    }
  }
}
int root(int v) { return (v == par[v] ? v : par[v] = root(par[v])); }
void un(int v, int u) {
  int v2 = root(v), u2 = root(u);
  if (v2 == u2) return;
  if (ver[v2].size() > ver[u2].size()) swap(v2, u2);
  par[v2] = u2;
  for (auto x : ver[v2]) {
    for (auto y : adjq[x]) {
      if (ans[y.second] == -1 && root(y.first) == u2) {
        ans[y.second] = lastw;
      }
    }
    ver[u2].push_back(x);
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m >> k >> q;
  for (long long i = 0; i < m; i++) {
    cin >> a >> b >> c;
    a--;
    b--;
    adj[a].push_back({b, c});
    adj[b].push_back({a, c});
    edge.push_back({c, {a, b}});
  }
  mdij();
  for (long long i = 0; i < m; i++) {
    edge[i].first += dis[edge[i].second.first] + dis[edge[i].second.second];
  }
  sort(edge.begin(), edge.end());
  for (long long i = 0; i < q; i++) {
    cin >> a >> b;
    a--;
    b--;
    adjq[a].push_back({b, i});
    adjq[b].push_back({a, i});
    ans[i] = -1;
  }
  for (long long i = 0; i < n; i++) {
    par[i] = i;
    ver[i].push_back(i);
  }
  for (long long i = 0; i < m; i++) {
    lastw = edge[i].first;
    un(edge[i].second.first, edge[i].second.second);
  }
  for (long long i = 0; i < q; i++) {
    cout << ans[i] << '\n';
  }
  return 0;
}
