DIVISIBLEBY8 - Need Help

Problem Link : Divisible By 8 Practice Coding Problem - CodeChef
What am i doing wrong here ?

#include <bits/stdc++.h>

using namespace std;

#define fastio()                      \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);                    \
    cout.tie(NULL)
#define MOD 1000000007
#define loop(n) for (ll i = 0; i < n; i++)
#define ll long long int

// typedef long long ll;

int main()
{
    fastio();

    int t;
    cin >> t;
    while (t--)
    {
        int n;
        cin >> n;
        string str;
        cin >> str;
        int num;
        int ans;

        if (n == 1)
        {
            cout << 8 << endl;
            continue;
        }
        else if (n == 2)
            num = stoi(str);
        else
            num = stoi(str.substr(n - 3, 3));

        if (num % 8 == 0)
        {
            cout << str << endl;
            continue;
        }
        else
        {
            int rem = num % 8;
            string check1 = (num + rem + "");
            string check2 = (num + rem + "");
            if (check1[check1.size() - 2] == check2[check2.size() - 2])
                ans = num + rem;
            else
                ans = num - rem;

            string res = to_string(ans);
            if (str.size() > 2)
            {
                while (res.size() < 3)
                    res.insert(0, "0");
            }
            res = str.substr(0, n - 3) + res;
            cout << res << endl;
        }
        // cout << ans << endl;
        // string res = str.substr(0, n-3) + to_string(ans);
    }

    return 0;
}

@bonsaidesuyo
heyy plzz refer this solution for better understanding.

#include <bits/stdc++.h>
using namespace std;

int main() {
	int t;
	cin>>t;
	while(t--){
	    int n,v;
	    string s;
	    cin>>n>>s;
	    if(n<=3) v=stoi(s);
	    else v=stoi(s.substr(n-3));
	    if(v%8==0) cout<<s<<endl;
	    else{
	        int ans;
	        if(n==1) ans=0;
	        else if(n==2) ans=stoi(s.substr(n-2,1));
	        else ans=stoi(s.substr(n-3,2));
	        if(ans%4==0) s.back()='8';
	        else if(ans%4==1) s.back()='6';
	        else if(ans%4==2) s.back()='4';
	        else s.back()='2';
	        cout<<s<<endl;
	    }
	}
	return 0;
}