Help me in solving BURGERS2 problem

My issue

not getting logic for the 2nd input

My code

#include <iostream>
using namespace std;

int main() {
	// your code goes here
	int t;
	cin>>t;
    while(t--){
        int x,y,n,r;
        cin>>x>>y>>n>>r;
        if(r<x && r<y)
        {
           cout<<-1<<endl; 
        }
        else
        {
            if((r/y)<=n && (r/x)>=n)
            cout<<n<<" "<<0<<endl;
            else if((r/y)>=n)
            cout<<0<<" "<<n<<endl;
        }
    }
	return 0;
}

Problem Link: BURGERS2 Problem - CodeChef

@klahari_18
plzz refer my c++ solution for better understanding of the logic.

#include <iostream>
using namespace std;

int main() {
	// your code goes here
	int t;
	cin>>t;
	while(t--)
	{
	    int x,y,n,r,i;
	    cin>>x>>y>>n>>r;
	    for( i=n;i>=0;i--)
	    {
	        int val=(i*y)+((n-i)*x);
	        if(val<=r)
	        {
	            cout<<n-i<<" "<<i;
	           break;
	        }
	    }
	    if(i==-1)
	    cout<<i;
	    cout<<endl;
	}
	return 0;
}