Help me in solving DIVISIBLEBY8 problem

My issue

to make the number divisible by 8 the last 3 digit should be divisible , now as you see in my code if the number is already divisible , then no change the i use a while loop change digit at ones place unless it is divisible by 8 the starting digit is 0 in loop, why is this giving a wrong answer.

My code

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

int main() {
	int t;
	cin>>t;
	while(t--)
	{int n;
	    string s;
	    cin>>n;
	    cin>>s;
	    if(n<3)
	    {
	        if(n==1)
	        {
	            cout<<8<<endl;
	         
	        }
	        else{
	        int x1=   0;
	   int x2=   s[s.size()-2]-'0';
	   	   int x3=   s[s.size()-2]-'0';
	   int num=(10*x2)+x1;
int num2=(10*x2)+x3;
if((num2%8)==0)
{
    num=num2;
}
else
{
while((num%8)!=0)
{
    num=num+1;
}
}
	    s[s.size()-1]=num%10+'0';
num=num/10;
s[s.size()-2]=num%10+'0';
for(int i=0;i<s.size();i++)
{
    cout<<s[i];
}
cout<<endl;
	        }
	    }
	    else
	    {
	 int x1=   0;
	   int x2=   s[s.size()-2]-'0';
int x3=   s[s.size()-3]-'0';
int x4=   s[s.size()-1]-'0';
int num=(100*x3)+(10*x2)+x1;
int num2=(100*x3)+(10*x2)+x4;
if((num2%8)==0)
{
    num=num2;
}
else{
while((num%8)!=0)
{
    num=num+1;
}}
s[s.size()-1]=num%10+'0';
num=num/10;
s[s.size()-2]=num%10+'0';
num=num/10;
s[s.size()-3]=num%10+'0';

for(int i=0;i<s.size();i++)
{
    cout<<s[i];
}
cout<<endl;
	  
	}
	}
	return 0;
}


Problem Link: DIVISIBLEBY8 Problem - CodeChef

Hi,
Look, you are implementing one of the correct ways. But your solution is not covering all the requirements the problem asked, such as printing the minimum number of changes. Your solution finds a number that is divisible by 8, but that might not be using the minimum possible number of changes. You get it?

https://www.codechef.com/viewsolution/1025297287
I have solved this problem in python. You may visit here. There, I explained this problem. I think, that would be helpful))