Help me in solving NOSEQ problem

My issue

Judge error on multiple already accepted and new solutions;

My code

// author: imtheonly1
// time: 2023-11-14 18:06:31
// URL: https://www.codechef.com/problems/NOSEQ
// Time Limit: 1000 ms
//
// Powered by CP Editor (https://cpeditor.org)

#include "bits/stdc++.h"
#define ll long long
using namespace std;

void solve() {
    ll n, k, s;
    cin >> n >> k >> s;
    vector<int> a(n);
    for (int i = 0; i < n; i++) {
        if (s % k == 0) {
            a[i] = 0;
            s /= k;
        } else if ((s - 1) % k == 0) {
            a[i] = 1;
            s = (s - 1) / k;
        } else if ((s + 1) % k == 0) {
            a[i] = -1;
            s = (s + 1) / k;
        } else {
            break;
        }
    }
    if (!s) {
        for (int i = 0; i < n; i++) {
            cout << a[i] << ' ';
        }
    } else {
        cout << -2;
    }
    cout << '\n';
}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int tc = 1;
    cin >> tc;
    while (tc--) {
        solve();
    }
    return 0;
}

Problem Link: NOSEQ Problem - CodeChef