Here is the simplified version of above code with the same issue:-
#include<iostream>
#include<vector>
#include<map>
using namespace std;
const int inf = 2e9;
int nu(vector<int>& suff, int l, int r, int n){
int ans = suff[l] - (r<n?suff[r+1]:0);
return ans%9;
}
void test_case()
{
string s; cin>>s;
int n = s.size();
int w, m; cin>>w>>m;
vector<int> suff(n+1,0); suff[n] = (s[n-1]-'0');
for(int i = n-2;i>=0;i--){
suff[i+1] = suff[i+2] + (s[i]-'0');
}
map<int, pair<int, int>> mp;
for(int i = n-w+1;i>0;i--){
int t= nu(suff, i, i+w-1, n);
mp[t] = {i, mp[t].first};
}
while(m--){
int l,r,k; cin>>l>>r>>k;
int v = nu(suff, l, r, n);
int ans1 = inf, ans2 = inf;
for(int j = 0;j<9;j++){
int l1 = mp[j].first;
int p = (j*v)%9;
int x = (k-p+9)%9;
int l2 = mp[x].first;
if(l2==l1) l2 = mp[x].second;
if(l1>0 and l2>0){
if(l1<ans1){
ans1 = l1;
ans2 = l2;
}else if(l1==ans1){
ans2 = min(ans2, l2);
}
}
}
if(ans1<inf and ans2<inf){
cout<<ans1<<" "<<ans2<<endl;
}else cout<<"-1 -1"<<endl;
}
}
int main() {
int tt = 1;
cin >> tt;
for(int i = 1;i<=tt;i++) {
test_case();
}
return 0;
}