//#pragma GCC optimize("O3")
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <fstream>
#include <algorithm>
#include <cstring> // Для memset
#define endl '\n'
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const int DIM = 50007;
const LL INF = 999999999999;
int n, k, o, m;
class edge {
public:
    int to, w;
};
vector < edge > v[DIM];
class tran {
public:
    int mat[6][6];
    void init() {
        for (int i = 1; i <= k; i++) {
            for (int j = 1; j <= k; j++) {
                mat[i][j] = INF;
            }
        }
    }
};
tran solve(int L, int R) {
    tran ans;
    if (R - L == 1) {
        ans.init();
        for (int i = 1; i <= k; i++) {
            int val = (L - 1) * k + i - 1;
            for (auto to : v[val]) {
                ans.mat[i][to.to - ((R - 1) * k) + 1] = to.w;
            }
        }
        return ans;
    }
    
    int mid = (L + R) / 2;
    tran m1 = solve(L, mid);
    tran m2 = solve(mid, R);
    for (int l = 1; l <= k; l++) {
        for (int r = 1; r <= k; r++) {
            ans.mat[l][r] = INF;
            for (int m = 1; m <= k; m++)  {
                if (m1.mat[l][m] != INF && m2.mat[m][r] != INF) ans.mat[l][r] = min(ans.mat[l][r], m1.mat[l][m] + m2.mat[m][r]);
            }
        }
    }
    return ans;
}
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> k >> n >> m >> o;
    for (int i = 1; i <= m; i++) {
        int a, b, t;
        cin >> a >> b >> t;
        v[a].push_back({ b, t });
    }
    for (int i = 1; i <= o; i++) {
        int a, b;
        cin >> a >> b;
        int b1 = a / k + 1;
        int b2 = b / k + 1;
        if (b1 >= b2) cout << -1 << endl;
        else {
            int res = solve(b1, b2).mat[a - ((b1 - 1) * k) + 1][b - ((b2 - 1) * k) + 1];
            if (res == INF) cout << -1;
            else cout << res;
            cout << endl;
        }
    }
    return 0;
}
Compilation message (stderr)
toll.cpp: In member function 'void tran::init()':
toll.cpp:34:29: warning: overflow in conversion from 'LL' {aka 'long long int'} to 'int' changes value from '999999999999' to '-727379969' [-Woverflow]
   34 |                 mat[i][j] = INF;
      |                             ^~~
toll.cpp: In function 'tran solve(int, int)':
toll.cpp:60:29: warning: overflow in conversion from 'LL' {aka 'long long int'} to 'int' changes value from '999999999999' to '-727379969' [-Woverflow]
   60 |             ans.mat[l][r] = INF;
      |                             ^~~| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |