#include <bits/stdc++.h>
#pragma GCC optimize("O2")
using namespace std;
inline long long input() {
  long long n;
  cin >> n;
  return n;
}
string base2(long long n) {
  string a = "";
  while (n >= 2) {
    a += (char)(n % 2 + '0');
    n /= 2;
  }
  a += (char)(n + '0');
  reverse((a).begin(), (a).end());
  return a;
}
const int MAXN = 5e5 + 10;
const int MOD = 1e9 + 7;
const int MOD2 = 998244353;
const long long INF = 8e18;
long long pw(long long a, long long b, long long mod) {
  return (!b ? 1
             : (b & 1 ? (a * pw(a * a % mod, b / 2, mod)) % mod
                      : pw(a * a % mod, b / 2, mod)));
}
long long n, m, k, q, sz[MAXN], par[MAXN], Ans[MAXN], dis[MAXN], king[MAXN];
vector<pair<long long, long long> > G[MAXN];
vector<pair<long long, long long> > M[MAXN];
bool mark[MAXN];
pair<long long, pair<long long, long long> > E[MAXN];
vector<pair<long long, pair<long long, long long> > > E2;
int GET(int x) { return ((x == par[x]) ? x : par[x] = GET(par[x])); }
void unify(long long x, long long y, long long cost) {
  long long v = GET(x), u = GET(y);
  if (v == u) return;
  if (sz[u] > sz[v]) {
    swap(u, v);
  }
  sz[v] += sz[u];
  par[u] = v;
  for (auto now : M[u]) {
    long long rec = GET(now.first);
    if (rec == v) {
      Ans[now.second] = cost;
    } else {
      M[v].push_back(now);
    }
  }
  M[u].clear();
  return;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ;
  cin >> n >> m >> k >> q;
  for (int i = 1; i <= n; i++) dis[i] = 1e17;
  for (int i = 1; i <= m; i++) {
    long long a, b, c;
    cin >> a >> b >> c;
    G[a].push_back(make_pair(b, c));
    G[b].push_back(make_pair(a, c));
    E[i].first = c, E[i].second.first = a, E[i].second.second = b;
  }
  set<pair<long long, long long> > st;
  for (long long i = 1; i <= k; i++) {
    dis[i] = 0ll;
    st.insert(make_pair(0ll, i));
    king[i] = i;
  }
  while (!st.empty()) {
    pair<long long, long long> cu = *st.begin();
    long long v = cu.second;
    st.erase(st.begin());
    if (mark[v]) {
      continue;
    }
    mark[v] = 1;
    for (auto y : G[v]) {
      long long u = y.first, cost = y.second;
      if (dis[u] > dis[v] + cost) {
        dis[u] = dis[v] + cost;
        king[u] = king[v];
        st.insert(make_pair(dis[u], u));
      }
    }
  }
  for (int i = 1; i <= m; i++) {
    long long v = E[i].second.first, u = E[i].second.second, cost = E[i].first;
    if (king[v] != king[u]) {
      E2.push_back(
          make_pair(dis[u] + dis[v] + cost, make_pair(king[v], king[u])));
    }
  }
  sort((E2).begin(), (E2).end());
  int cnt = 1, T = q;
  while (T--) {
    int a, b;
    cin >> a >> b;
    M[a].push_back(make_pair(b, cnt));
    M[b].push_back(make_pair(a, cnt));
    cnt++;
  }
  for (int i = 1; i < MAXN; i++) {
    sz[i] = 1;
    par[i] = i;
  }
  for (auto x : E2) {
    long long v = x.second.first, u = x.second.second, cost = x.first;
    unify(v, u, cost);
  }
  for (int i = 1; i <= q; i++) {
    cout << Ans[i] << "\n";
  }
  return 0;
}
