Help me in solving SPCP3 problem

My issue

runtime error

My code

#include<iostream>
int main() {
	using namespace std;
	int T,A[1000],B[1000];
	cin>>T;
	for(int i=0;i<T;i++){
	    cin>>A[i]>>B[i];
	}
	
	for(int i=0;i<1000;i++){
	    int k=0;
		while(k<1000){
			if ((A[i]+k)%(B[i]-k)==0){
			  cout<<k<<"\n";}
		
		if((A[i]-k)%(B[i]+k)==0){
			cout<<k<<"\n";
		}
		
		k++;
	        
	      
	    }
	}
	return 0;
}


Problem Link: Marbles Practice Coding Problem - CodeChef

@indeedshakil
plzz refer my c++ code for better understanding

#include <iostream>
using namespace std;

int main() {
	// your code goes here
	int t;
	cin>>t;
	while(t--)
	{
	    int a,b;
	    cin>>a>>b;
	    int a1=a;
	    int b1=b;
	    int cnt=0;
	    while(b>1)
	    {
	        if(a%b==0)
	        {
	            
	            break;
	        }
	        a++;
	        b--;
	        cnt++;
	    }
	  //  cout<<cnt<<endl;
	    a=a1;
	    b=b1;
	    int ans=cnt;
	    cnt=0;
	    while(a>1)
	    {
	        if(a%b==0)
	        {
	       ans=min(ans,cnt);
	        break;
	        }
	        a--;
	        b++;
	        cnt++;
	       
	    }
	   // ans=min(ans,cnt);
	    cout<<ans<<endl;
	}
	return 0;
}