#include <iostream>
using namespace std;
#include <string>
string s , r;
unsigned long long a , b , c , d;

unsigned long long tonum( string s ){
    unsigned long long temp = 0;
    int i = 0;
    while( s[ i ] == '0' )
        i++;
    for( ; i < s.length(); i++ ){
        temp *= 10;
        temp += ( (int)s[ i ] - 48 );
    }
    return temp;
}

unsigned long long convert( string s ){
    unsigned long long temp = 0;
    int i = 0;
    while( s[ i ] == '0' )i++;

    for( ; i < s.length(); i++ ){
        temp *= a;
        if( (int)s[ i ] < 60 )temp += ( (int)s[ i ] - 48 );
        else temp += (( (int)s[ i ] - 65 ) + 11 );
    }

    return temp;
}

string tostring( int x ){
    string t( 1 , (char)x );
    return t;
}

string decimal2roman(int input) {
  const string roman[13] = { "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
  const int decimal[13] =  {1000,  900, 500,  400, 100,   90,  50,   40,  10,   9,    5,    4,   1};
  string romanvalue = "";
  for (int i = 0; i < 13; i++) {
    while (input >= decimal[i]) {
      input -= decimal[i];
      romanvalue += roman[i];
    }
  }
  return romanvalue;
}

int main()
{

    cin>>s;
    a = tonum( s );
    cin>>s;
    if( s[ 0 ] == 'R' ){
        cin>>s;
        c = tonum( s );
        cout<<decimal2roman( c )<<endl;
    }
    else{

        b = tonum( s );
        cin>>s;
        c = tonum( s );
        d = convert( s );

        if( c == 0 ){
            cout<<c<<endl;
            return 0;
        }

        while( d != 0 ){
            r.append( tostring( d % b ) );
            d /= b;
        }

        for( int i = r.length() - 1; i >= 0; i-- ){
            if( (int)r[ i ] < 10 )cout<<(char)(r[ i ] + 48);
            else cout<<(char)(r[ i ] + 54 );
        }
        cout<<endl;

    }

    return 0;
}


#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
using namespace std;

int val(char c)
{
	if ('0' <= c && c <= '9')
		return c - '0';
	else
		return c - 'A' + 10;
}

char symb(int v)
{
	if (v < 10)
		return '0' + v;
	else
		return 'A' + v - 10;
}

long long from(char* buf, int a)
{
	int n = strlen(buf);
	long long num = 0;
	for (int i = 0; i < n; i++)
		num = num * a + val(buf[i]);
	return num;
}

char sym[][3] = {"IV", "XL", "CD", "MP"};

char* rim(long long k)
{
	char* ans = new char[100];
	int pt = 0;
	memset(ans, 0, 100);
	for (int q = 1000, i = 3; i >= 0; q /= 10, i--)
	{
		k %= (10 * q);
		if (k / q == 0)
			continue;
		if (k / q == 4)
		{
			ans[pt++] = sym[i][0], ans[pt++] = sym[i][1];
			continue;
		}
		if (k / q == 9)
		{
			ans[pt++] = sym[i][0], ans[pt++] = sym[i + 1][0];
			continue;
		}

		if (k / q > 4)
			ans[pt++] = sym[i][1], k -= 5 * q;
		for (int j = 0; j < k / q; j++)
			ans[pt++] = sym[i][0];
	}
	return ans;
}

char* to(long long k, int b)
{
	char* ans = new char[100];
	memset(ans, 0, 100);
	if (k == 0)
	{
		ans[0] = '0';
		return ans;
	}
	int i;
	for (i = 0; k > 0; i++)
	{
		ans[i] = symb(k % b);
		k /= b;
	}
	i--;
	for (int j = i; j > i / 2; j--)
		swap(ans[j], ans[i - j]);
	return ans;
}

int main(int argc, char **argv)
{
#ifdef HASFILES
	freopen(TASK".in", "r", stdin);
	freopen(TASK".out", "w", stdout);
#endif
	int a;
	char b[5];
	cin >> a >> b;
	char c[2000];
	cin >> c;
	if (b[0] == 'R')
		cout << rim(from(c, a));
	else
	{
		cout << to(from(c, a), atoi(b));
	}
	cout << endl;
	return 0;
}


#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
using namespace std;

int val(char c)
{
	if ('0' <= c && c <= '9')
		return c - '0';
	else
		return c - 'A' + 10;
}

char symb(int v)
{
	if (v < 10)
		return '0' + v;
	else
		return 'A' + v - 10;
}

long long from(char* buf, int a)
{
	int n = strlen(buf);
	long long num = 0;
	for (int i = 0; i < n; i++)
		num = num * a + val(buf[i]);
	return num;
}

char sym[][3] = {"IV", "XL", "CD", "MP"};

char* rim(long long k)
{
	char* ans = new char[100];
	int pt = 0;
	memset(ans, 0, sizeof(ans));
	for (int q = 1000, i = 3; i >= 0; q /= 10, i--)
	{
		k %= (10 * q);
		if (k / q == 0)
			continue;
		if (k / q == 4)
		{
			ans[pt++] = sym[i][0], ans[pt++] = sym[i][1];
			continue;
		}
		if (k / q == 9)
		{
			ans[pt++] = sym[i][0], ans[pt++] = sym[i + 1][0];
			continue;
		}

		if (k / q > 4)
			ans[pt++] = sym[i][1], k -= 5 * q;
		for (int j = 0; j < k / q; j++)
			ans[pt++] = sym[i][0];
	}
	return ans;
}

char* to(long long k, int b)
{
	char* ans = new char[100];
	int i;
	for (i = 0; k > 0; i++)
	{
		ans[i] = symb(k % b);
		k /= b;
	}
	i--;
	for (int j = i; j > i / 2; j--)
		swap(ans[j], ans[i - j]);
	return ans;
}

int main(int argc, char **argv)
{
#ifdef HASFILES
	freopen(TASK".in", "r", stdin);
	freopen(TASK".out", "w", stdout);
#endif
	int a;
	char b[5];
	cin >> a >> b;
	char c[2000];
	cin >> c;
	if (b[0] == 'R')
		cout << rim(from(c, a));
	else
		cout << to(from(c, a), atoi(b));
	cout << endl;
	return 0;
}


#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
using namespace std;

int val(char c)
{
	if ('0' <= c && c <= '9')
		return c - '0';
	else
		return c - 'A' + 10;
}

char symb(int v)
{
	if (v < 10)
		return '0' + v;
	else
		return 'A' + v - 10;
}

long long from(char* buf, int a)
{
	int n = strlen(buf);
	long long num = 0;
	for (int i = 0; i < n; i++)
		num = num * a + val(buf[i]);
	return num;
}

char sym[][3] = {"IV", "XL", "CD", "MP"};

char* rim(long long k)
{
	char* ans = new char[100];
	int pt = 0;
	memset(ans, 0, sizeof(ans));
	for (int q = 1000, i = 3; i >= 0; q /= 10, i--)
	{
		k %= (10 * q);
		if (k / q == 0)
			continue;
		if (k / q == 4)
		{
			ans[pt++] = sym[i][0], ans[pt++] = sym[i][1];
			continue;
		}
		if (k / q == 9)
		{
			ans[pt++] = sym[i][0], ans[pt++] = sym[i + 1][0];
			continue;
		}

		if (k / q > 4)
			ans[pt++] = sym[i][1], k -= 5 * q;
		for (int j = 0; j < k / q; j++)
			ans[pt++] = sym[i][0];
	}
	return ans;
}

char* to(long long k, int b)
{
	char* ans = new char[100];
	int i;
	for (i = 0; k > 0; i++)
	{
		ans[i] = symb(k % b);
		k /= b;
	}
	i--;
	for (int j = i; j > i / 2; j--)
		swap(ans[j], ans[i - j]);
	return ans;
}

int main(int argc, char **argv)
{
#ifdef HASFILES
	freopen(TASK".in", "r", stdin);
	freopen(TASK".out", "w", stdout);
#endif
	int a;
	char b[5];
	cin >> a >> b;
	char c[2000];
	cin >> c;
	if (b[0] == 'R')
		cout << rim(from(c, a));
	else
		cout << to(from(c, a), atoi(b));
	return 0;
}


#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
using namespace std;

int val(char c)
{
	if ('0' <= c && c <= '9')
		return c - '0';
	else
		return c - 'A' + 10;
}

char symb(int v)
{
	if (v < 10)
		return '0' + v;
	else
		return 'A' + v - 10;
}

long long from(char* buf, int a)
{
	int n = strlen(buf);
	long long num = 0;
	for (int i = 0; i < n; i++)
		num = num * a + val(buf[i]);
	return num;
}

char sym[][3] = {"IV", "XL", "CD", "MP"};

char* rim(long long k)
{
	char* ans = new char[100];
	int pt = 0;
	memset(ans, 0, 100);
	for (int q = 1000, i = 3; i >= 0; q /= 10, i--)
	{
		k %= (10 * q);
		if (k / q == 0)
			continue;
		if (k / q == 4)
		{
			ans[pt++] = sym[i][0], ans[pt++] = sym[i][1];
			continue;
		}
		if (k / q == 9)
		{
			ans[pt++] = sym[i][0], ans[pt++] = sym[i + 1][0];
			continue;
		}

		if (k / q > 4)
			ans[pt++] = sym[i][1], k -= 5 * q;
		for (int j = 0; j < k / q; j++)
			ans[pt++] = sym[i][0];
	}
	return ans;
}

char* to(long long k, int b)
{
	char* ans = new char[100];
	memset(ans, 0, 100);
	int i;
	for (i = 0; k > 0; i++)
	{
		ans[i] = symb(k % b);
		k /= b;
	}
	i--;
	for (int j = i; j > i / 2; j--)
		swap(ans[j], ans[i - j]);
	return ans;
}

int main(int argc, char **argv)
{
#ifdef HASFILES
	freopen(TASK".in", "r", stdin);
	freopen(TASK".out", "w", stdout);
#endif
	int a;
	char b[5];
	cin >> a >> b;
	char c[2000];
	cin >> c;
	if (b[0] == 'R')
		cout << rim(from(c, a));
	else
		cout << to(from(c, a), atoi(b));
	cout << endl;
	return 0;
}


#include<stdio.h>
#include<math.h>
#include<vector>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define eps 1e-8
#define max(a,b) (a>b?a:b)
#define min(a,b) (a<b?a:b)

int main()
{
    long long a;
    scanf("%I64d",&a);
    char numb[2];
    scanf("%s",numb);
    char c[1005];
    scanf("%s",c);
    long long numc=0;
    int pos=0;
    for(int i=0;i<strlen(c);i++)
    {
        if(c[i]!='0')
        {
            pos=i;
            break;
        }
    }
    for(int i=0;i<strlen(c);i++)
    {
        if(c[i]<='9'&&c[i]>='0')
        {
            numc=numc*a+c[i]-'0';
        }
        else if(c[i]<='Z'&&c[i]>='A')
        {
            numc=numc*a+10+c[i]-'A';
        }
        else if(c[i]<='z'&&c[i]>='a')
        {
            numc=numc*a+10+c[i]-'a';
        }
    }
//    printf("%I64d\n",numc);
    if(numb[0]=='R')
    {
        int ttt=0;
        ttt=numc/1000;
        if(ttt==3)
            printf("MMM");
        else if(ttt==2)
            printf("MM");
        else if(ttt==1)
            printf("M");

        numc%=1000;
        ttt=numc/100;
        if(ttt==9)printf("CM");
        else if(ttt==8)printf("DCCC");
        else if(ttt==7)printf("DCC");
        else if(ttt==6)printf("DC");
        else if(ttt==5)printf("D");
        else if(ttt==4)printf("CD");
        else if(ttt==3)printf("CCC");
        else if(ttt==2)printf("CC");
        else if(ttt==1)printf("C");

        numc%=100;
        ttt=numc/10;
        if(ttt==9)printf("XC");
        else if(ttt==8)printf("LXXX");
        else if(ttt==7)printf("LXX");
        else if(ttt==6)printf("LX");
        else if(ttt==5)printf("L");
        else if(ttt==4)printf("XL");
        else if(ttt==3)printf("XXX");
        else if(ttt==2)printf("XX");
        else if(ttt==1)printf("X");

        numc%=10;
        ttt=numc;
        if(ttt==9)printf("IX");
        else if(ttt==8)printf("VIII");
        else if(ttt==7)printf("VII");
        else if(ttt==6)printf("VI");
        else if(ttt==5)printf("V");
        else if(ttt==4)printf("IV");
        else if(ttt==3)printf("III");
        else if(ttt==2)printf("II");
        else if(ttt==1)printf("I");

        printf("\n");
    }
    else
    {
        int b=0;
        for(int i=strlen(numb)-1;i>=0;i--)
        {
            b=b*10+numb[i]-'0';
        }
        char ans[3005];
        int posans=0;
        while(numc)
        {
            int term=numc%b;
            numc/=b;
            if(term<10)
            {
                ans[pos++]='0'+term;
            }
            else
            {
                ans[pos++]='A'+(term-10);
            }
        }
        for(int i=pos-1;i>=0;i--)
        {
            printf("%c",ans[i]);
        }
        printf("\n");
    }
    return 0;
}


#include<stdio.h>
#include<math.h>
#include<vector>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define eps 1e-8
#define max(a,b) (a>b?a:b)
#define min(a,b) (a<b?a:b)

int main()
{
    long long a;
    scanf("%I64d",&a);
    char numb[3];
    scanf("%s",numb);
    char c[1005];
    scanf("%s",c);
    long long numc=0;
    int pos=0;
    for(int i=0;i<strlen(c);i++)
    {
        if(c[i]!='0')
        {
            pos=i;
            break;
        }
    }
    for(int i=0;i<strlen(c);i++)
    {
        if(c[i]<='9'&&c[i]>='0')
        {
            numc=numc*a+c[i]-'0';
        }
        else if(c[i]<='Z'&&c[i]>='A')
        {
            numc=numc*a+10+c[i]-'A';
        }
        else if(c[i]<='z'&&c[i]>='a')
        {
            numc=numc*a+10+c[i]-'a';
        }
    }
//    printf("%I64d\n",numc);
    if(numb[0]=='R')
    {
        int ttt=0;
        ttt=numc/1000;
        if(ttt==3)
            printf("MMM");
        else if(ttt==2)
            printf("MM");
        else if(ttt==1)
            printf("M");

        numc%=1000;
        ttt=numc/100;
        if(ttt==9)printf("CM");
        else if(ttt==8)printf("DCCC");
        else if(ttt==7)printf("DCC");
        else if(ttt==6)printf("DC");
        else if(ttt==5)printf("D");
        else if(ttt==4)printf("CD");
        else if(ttt==3)printf("CCC");
        else if(ttt==2)printf("CC");
        else if(ttt==1)printf("C");

        numc%=100;
        ttt=numc/10;
        if(ttt==9)printf("XC");
        else if(ttt==8)printf("LXXX");
        else if(ttt==7)printf("LXX");
        else if(ttt==6)printf("LX");
        else if(ttt==5)printf("L");
        else if(ttt==4)printf("XL");
        else if(ttt==3)printf("XXX");
        else if(ttt==2)printf("XX");
        else if(ttt==1)printf("X");

        numc%=10;
        ttt=numc;
        if(ttt==9)printf("IX");
        else if(ttt==8)printf("VIII");
        else if(ttt==7)printf("VII");
        else if(ttt==6)printf("VI");
        else if(ttt==5)printf("V");
        else if(ttt==4)printf("IV");
        else if(ttt==3)printf("III");
        else if(ttt==2)printf("II");
        else if(ttt==1)printf("I");

        printf("\n");
    }
    else
    {
        int b=0;
//        printf("%s\n",numb);
        for(int i=0;i<strlen(numb);i++)
        {
            b=b*10+numb[i]-'0';
        }
        char ans[3005];
        int pos=0;
//        printf("!!!\n");
//        printf("%d\n",b);
        while(numc)
        {
//            printf("%I64d\n",numc);
            long long term=numc%b;
            numc/=b;
            if(term<10)
            {
                ans[pos++]='0'+term;
            }
            else
            {
                ans[pos++]='A'+(term-10);
            }
        }
//        printf("%s\n",ans);
        for(int i=pos-1;i>=0;i--)
        {
            printf("%c",ans[i]);
        }
        printf("\n");
    }
    return 0;
}


#include<stdio.h>
#include<math.h>
#include<vector>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define eps 1e-8
#define max(a,b) (a>b?a:b)
#define min(a,b) (a<b?a:b)

int main()
{
    long long a;
    scanf("%I64d",&a);
    char numb[2];
    scanf("%s",numb);
    char c[1005];
    scanf("%s",c);
    long long numc=0;
    int pos=0;
    for(int i=0;i<strlen(c);i++)
    {
        if(c[i]!='0')
        {
            pos=i;
            break;
        }
    }
    for(int i=strlen(c)-1;i>=pos;i--)
    {
        if(c[i]<='9'&&c[i]>='0')
        {
            numc=numc*a+c[i]-'0';
        }
        else if(c[i]<='Z'&&c[i]>='A')
        {
            numc=numc*a+10+c[i]-'A';
        }
        else if(c[i]<='z'&&c[i]>='a')
        {
            numc=numc*a+10+c[i]-'a';
        }
    }
    if(numb[0]=='R')
    {
        int ttt=0;
        ttt=numc/1000;
        if(ttt==3)
            printf("MMM");
        else if(ttt==2)
            printf("MM");
        else if(ttt==1)
            printf("M");

        numc%=1000;
        ttt=numc/100;
        if(ttt==9)printf("CM");
        else if(ttt==8)printf("DCCC");
        else if(ttt==7)printf("DCC");
        else if(ttt==6)printf("DC");
        else if(ttt==5)printf("D");
        else if(ttt==4)printf("CD");
        else if(ttt==3)printf("CCC");
        else if(ttt==2)printf("CC");
        else if(ttt==1)printf("C");

        numc%=100;
        ttt=numc/10;
        if(ttt==9)printf("XC");
        else if(ttt==8)printf("LXXX");
        else if(ttt==7)printf("LXX");
        else if(ttt==6)printf("LX");
        else if(ttt==5)printf("L");
        else if(ttt==4)printf("XL");
        else if(ttt==3)printf("XXX");
        else if(ttt==2)printf("XX");
        else if(ttt==1)printf("X");

        numc%=10;
        ttt=numc;
        if(ttt==9)printf("IX");
        else if(ttt==8)printf("VIII");
        else if(ttt==7)printf("VII");
        else if(ttt==6)printf("VI");
        else if(ttt==5)printf("V");
        else if(ttt==4)printf("IV");
        else if(ttt==3)printf("III");
        else if(ttt==2)printf("II");
        else if(ttt==1)printf("I");

        printf("\n");
    }
    else
    {
        int b=0;
        for(int i=strlen(numb)-1;i>=0;i--)
        {
            b=b*10+numb[i]-'0';
        }
        char ans[3005];
        int posans=0;
        while(numc)
        {
            int term=numc%b;
            numc/=b;
            if(term<10)
            {
                ans[pos++]='0'+term;
            }
            else
            {
                ans[pos++]='A'+(term-10);
            }
        }
        for(int i=0;i<pos;i++)
        {
            printf("%c",ans[i]);
        }
        printf("\n");
    }
    return 0;
}


#include<stdio.h>
#include<math.h>
#include<vector>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define eps 1e-8
#define max(a,b) (a>b?a:b)
#define min(a,b) (a<b?a:b)

int main()
{
    long long a;
    scanf("%I64d",&a);
    char numb[3];
    scanf("%s",numb);
    char c[1005];
    scanf("%s",c);
    long long numc=0;
    int pos=0;
    for(int i=0;i<strlen(c);i++)
    {
        if(c[i]!='0')
        {
            pos=i;
            break;
        }
    }
    for(int i=0;i<strlen(c);i++)
    {
        if(c[i]<='9'&&c[i]>='0')
        {
            numc=numc*a+c[i]-'0';
        }
        else if(c[i]<='Z'&&c[i]>='A')
        {
            numc=numc*a+10+c[i]-'A';
        }
        else if(c[i]<='z'&&c[i]>='a')
        {
            numc=numc*a+10+c[i]-'a';
        }
    }
    if(numc==0)
    {
        printf("0\n");
        return 0;
    }
//    printf("%I64d\n",numc);
    if(numb[0]=='R')
    {
        int ttt=0;
        ttt=numc/1000;
        if(ttt==3)
            printf("MMM");
        else if(ttt==2)
            printf("MM");
        else if(ttt==1)
            printf("M");

        numc%=1000;
        ttt=numc/100;
        if(ttt==9)printf("CM");
        else if(ttt==8)printf("DCCC");
        else if(ttt==7)printf("DCC");
        else if(ttt==6)printf("DC");
        else if(ttt==5)printf("D");
        else if(ttt==4)printf("CD");
        else if(ttt==3)printf("CCC");
        else if(ttt==2)printf("CC");
        else if(ttt==1)printf("C");

        numc%=100;
        ttt=numc/10;
        if(ttt==9)printf("XC");
        else if(ttt==8)printf("LXXX");
        else if(ttt==7)printf("LXX");
        else if(ttt==6)printf("LX");
        else if(ttt==5)printf("L");
        else if(ttt==4)printf("XL");
        else if(ttt==3)printf("XXX");
        else if(ttt==2)printf("XX");
        else if(ttt==1)printf("X");

        numc%=10;
        ttt=numc;
        if(ttt==9)printf("IX");
        else if(ttt==8)printf("VIII");
        else if(ttt==7)printf("VII");
        else if(ttt==6)printf("VI");
        else if(ttt==5)printf("V");
        else if(ttt==4)printf("IV");
        else if(ttt==3)printf("III");
        else if(ttt==2)printf("II");
        else if(ttt==1)printf("I");

        printf("\n");
    }
    else
    {
        int b=0;
//        printf("%s\n",numb);
        for(int i=0;i<strlen(numb);i++)
        {
            b=b*10+numb[i]-'0';
        }
        char ans[3005];
        int pos=0;
//        printf("!!!\n");
//        printf("%d\n",b);
        while(numc)
        {
//            printf("%I64d\n",numc);
            long long term=numc%b;
            numc/=b;
            if(term<10)
            {
                ans[pos++]='0'+term;
            }
            else
            {
                ans[pos++]='A'+(term-10);
            }
        }
//        printf("%s\n",ans);
        for(int i=pos-1;i>=0;i--)
        {
            printf("%c",ans[i]);
        }
        printf("\n");
    }
    return 0;
}


#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <vector>
#include <cstdio>
#include <string>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>

using namespace std;

const int inf = 111111111;

long long a, b, c;
string s, cc;

int str_to_int(string s) {
    int x = 0;
    for (int i = 0; i < s.length(); ++i) {
        x = x * 10 + s[i] - '0';
    }
    return x;
}

string int_to_str(int x) {
    if (x == 0) {
        return "0";
    }
    string ret = "";
    while (x != 0) {
        ret += x % 10 + '0';
        x /= 10;
    }
    reverse(ret.begin(), ret.end());
    return ret;
}

string norm(string s) {
    int poz = 0;
    for (int i = 0; i < s.length(); ++i) {
        if (s[i] != '0') {
            poz = i;
            break;
        }
    }
    return s.substr(poz, s.length() - poz);
}

long long proc1(string s, int k) {
    long long powers[100];
    powers[0] = 1;
    for (int i = 1; i < 100; ++i) {
        powers[i] = powers[i - 1] * k;
    }
    long long res = 0;
    for (int i = 0; i < s.length(); ++i) {
        if (s[i] >= '0' && s[i] <= '9') {
            res += powers[s.length() - i - 1] * (s[i] - '0');
        } else {
            res += powers[s.length() - i - 1] * (s[i] - 'A' + 10);
        }
    }
    return res;
}

string proc2(long long x, int k) {
    if (x == 0) {
        return "0";
    }
    string res = "";
    while (x != 0) {
        int a = x % k;
        x /= k;
        if (a < 10) {
            res += int_to_str(a);
        } else {
            res += 'A' + a - 10;
        }
    }
    reverse(res.begin(), res.end());
    return res;
}

int main() {
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    cin >> a >> s >> cc;
    cc = norm(cc);
    if (s == "R") {
        long long t = proc1(cc, a);
        while (t >= 1000) {
            cout << "M";
            t -= 1000;
        }
        if (t >= 900) {
            cout << "CM";
            t -= 900;
        }
        while (t >= 500) {
            cout << "D";
            t -= 500;
        }
        if (t >= 400) {
            cout << "CD";
            t -= 400;
        }
        while (t >= 100) {
            cout << "C";
            t -= 100;
        }
        if (t >= 90) {
            cout << "XC";
            t -= 90;
        }
        while (t >= 50) {
            cout << "L";
            t -= 50;
        }
        if (t >= 40) {
            cout << "XL";
            t -= 40;
        }
        while (t >= 10) {
            cout << "X";
            t -= 10;
        }
        if (t >= 9) {
            cout << "IX";
            t -= 9;
        }
        while (t >= 5) {
            cout << "V";
            t -= 5;
        }
        if (t >= 4) {
            cout << "IV";
            t -= 4;
        }
        while (t >= 1) {
            cout << "I";
            t -= 1;
        }
    } else {
        b = str_to_int(s);
        long long t = proc1(cc, a);
        cout << proc2(t, b) << endl;
    }
    return 0;
}


#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <vector>
#include <cstdio>
#include <string>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>

using namespace std;

const int inf = 111111111;

long long a, b, c;
string s, cc;

int str_to_int(string s) {
    int x = 0;
    for (int i = 0; i < s.length(); ++i) {
        x = x * 10 + s[i] - '0';
    }
    return x;
}

string int_to_str(int x) {
    if (x == 0) {
        return "0";
    }
    string ret = "";
    while (x != 0) {
        ret += x % 10 + '0';
        x /= 10;
    }
    reverse(ret.begin(), ret.end());
    return ret;
}

string norm(string s) {
    int poz = 0;
    for (int i = 0; i < s.length(); ++i) {
        if (s[i] != '0') {
            poz = i;
            break;
        }
    }
    return s.substr(poz, s.length() - poz);
}

long long proc1(string s, int k) {
    long long powers[100];
    powers[0] = 1;
    for (int i = 1; i < 100; ++i) {
        powers[i] = powers[i - 1] * k;
    }
    long long res = 0;
    for (int i = 0; i < s.length(); ++i) {
        if (s[i] >= '0' && s[i] <= '9') {
            res += powers[s.length() - i - 1] * (s[i] - '0');
        } else {
            res += powers[s.length() - i - 1] * (s[i] - 'A' + 10);
        }
    }
    return res;
}

string proc2(long long x, int k) {
    if (x == 0) {
        return "0";
    }
    string res = "";
    while (x != 0) {
        int a = x % k;
        x /= k;
        if (a < 10) {
            res += int_to_str(a);
        } else {
            res += 'A' + a - 11;
        }
    }
    reverse(res.begin(), res.end());
    return res;
}

int main() {
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    cin >> a >> s >> cc;
    cc = norm(cc);
    if (s == "R") {
        long long t = proc1(cc, a);
        while (t >= 1000) {
            cout << "M";
            t -= 1000;
        }
        if (t >= 900) {
            cout << "CM";
            t -= 900;
        }
        while (t >= 500) {
            cout << "D";
            t -= 500;
        }
        if (t >= 400) {
            cout << "CD";
            t -= 400;
        }
        while (t >= 100) {
            cout << "C";
            t -= 100;
        }
        if (t >= 90) {
            cout << "XC";
            t -= 90;
        }
        while (t >= 50) {
            cout << "L";
            t -= 50;
        }
        if (t >= 40) {
            cout << "XL";
            t -= 40;
        }
        while (t >= 10) {
            cout << "X";
            t -= 10;
        }
        if (t >= 9) {
            cout << "IX";
            t -= 9;
        }
        while (t >= 5) {
            cout << "V";
            t -= 5;
        }
        if (t >= 4) {
            cout << "IV";
            t -= 4;
        }
        while (t >= 1) {
            cout << "I";
            t -= 1;
        }
    } else {
        b = str_to_int(s);
        long long t = proc1(cc, a);
        cout << proc2(t, b) << endl;
    }
    return 0;
}


#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <vector>
#include <cstdio>
#include <string>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>

using namespace std;

const int inf = 111111111;

long long a, b, c;
string s, cc;

int str_to_int(string s) {
    int x = 0;
    for (int i = 0; i < s.length(); ++i) {
        x = x * 10 + s[i] - '0';
    }
    return x;
}

string int_to_str(int x) {
    if (x == 0) {
        return "0";
    }
    string ret = "";
    while (x != 0) {
        ret += x % 10 + '0';
        x /= 10;
    }
    reverse(ret.begin(), ret.end());
    return ret;
}

string norm(string s) {
    int poz = 0;
    for (int i = 0; i < s.length(); ++i) {
        if (s[i] != '0') {
            poz = i;
            break;
        }
    }
    return s.substr(poz, s.length() - poz);
}

long long proc1(string s, int k) {
    long long powers[100];
    powers[0] = 1;
    for (int i = 1; i < 100; ++i) {
        powers[i] = powers[i - 1] * k;
    }
    long long res = 0;
    for (int i = 0; i < s.length(); ++i) {
        if (s[i] >= '0' && s[i] <= '9') {
            res += powers[s.length() - i - 1] * (s[i] - '0');
        } else {
            res += powers[s.length() - i - 1] * (s[i] - 'A' + 11);
        }
    }
    return res;
}

string proc2(long long x, int k) {
    if (x == 0) {
        return "0";
    }
    string res = "";
    while (x != 0) {
        int a = x % k;
        x /= k;
        if (a < 10) {
            res += int_to_str(a);
        } else {
            res += 'A' + a - 11;
        }
    }
    reverse(res.begin(), res.end());
    return res;
}

int main() {
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    cin >> a >> s >> cc;
    cc = norm(cc);
    if (s == "R") {
        long long t = proc1(cc, a);
        while (t >= 1000) {
            cout << "M";
            t -= 1000;
        }
        if (t >= 900) {
            cout << "CM";
            t -= 900;
        }
        while (t >= 500) {
            cout << "D";
            t -= 500;
        }
        if (t >= 400) {
            cout << "CD";
            t -= 400;
        }
        while (t >= 100) {
            cout << "C";
            t -= 100;
        }
        if (t >= 90) {
            cout << "XC";
            t -= 90;
        }
        while (t >= 50) {
            cout << "L";
            t -= 50;
        }
        if (t >= 40) {
            cout << "XL";
            t -= 40;
        }
        while (t >= 10) {
            cout << "X";
            t -= 10;
        }
        if (t >= 9) {
            cout << "IX";
            t -= 9;
        }
        while (t >= 5) {
            cout << "V";
            t -= 5;
        }
        if (t >= 4) {
            cout << "IV";
            t -= 4;
        }
        while (t >= 1) {
            cout << "I";
            t -= 1;
        }
    } else {
        b = str_to_int(s);
        long long t = proc1(cc, a);
        cout << proc2(t, b) << endl;
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <map>
#include <set>
#include <string>
#include <vector>
#include <deque>

#define NAME		""
#define INF			(int)1e9
#define EPS			1e-9
#define m_pi		(3.1415926535897931)
#define SZ(x)		x.size()
#define PB			push_back
#define MP			make_pair
#define fi			first
#define se			second
#define END			cout<<endl
#define FOR(i,a,b)	for(int i=a;i<=b;++i)
#define REP(i,n)	for(int i=0;i<n;++i)
#define ALL(x)		(x.begin(), x.end())
#define REV(x)		reverse(x.begin(), x.end())
#define MAX_N		int(2e2+2)
#define MAX_V		int(1e5)

using namespace std;

typedef deque<int>		DI;
typedef deque<double>	DD;
typedef deque<DI>		DII;
typedef long long		LL;
typedef long double		LD;
typedef	double			D;
typedef pair<int,int>	PII;
typedef pair<D,D>		PDD;

string put(char,int), c, num;
map<int,string> m;
void Rim(int), Dig(LL, int);
LL a, b = 0, n = 0;

int main(){

/*	freopen(NAME".in","r",stdin);
	freopen(NAME".out","w",stdout); //*/

	cin >> a >> c >> num;

	while (num[0] == '0' && SZ(num) > 0) num.erase(0,1);

	int t;
	REV(num);
	REP(i,SZ(num)) t = (num[i] >= 'A') ? (num[i]-'A'+10) : (num[i]-'0'), n += LL(t*pow(a+.0,i));

	if (c[0] == 'R') {
        Rim(n);
	} else {
		REP(i,SZ(c)) b *= 10, b += c[i]-'0';
		Dig(n,b);
	}

	return 0;
}

void Dig(LL x, int k) {
	string s = "";
//	cout << x<<endl;
	while (x > 0) {
	 	s += (k > 9 && x%k > 9) ? char('A' + (x%k)%10) : char('0' + x%k);
//	 	cout << s << ' ';
	 	x /= k;
//	 	cout << x << endl;
	}
	REV(s);
	cout << s << endl;
}

void Rim(int x){
	string s = "";
	m[0]   = "";
	m[1]   = "I";
	m[4]   = "IV";
	m[5]   = "V";
	m[9]   = "IX";
	m[10]  = "X";
	m[40]  = "XL";
	m[50]  = "L";
	m[90]  = "XC";
	m[100] = "C";
	m[400] = "CD";
	m[500] = "D";
	m[900] = "CM";
	m[1000]= "M";

	for (int i = 2; i <= 3; ++ i) {
		m[i] = put('I',i);
		m[i*10] = put('X',i);
		m[i*100] = put('C',i);
		m[i*1000] = put('M',i);
	}

	for (int j = 6; j <= 8; ++ j){
		m[j] = "V"+put('I',j-5);
		m[j*10] = "L"+put('X',j-5);
		m[j*100] = "D"+put('C',j-5);
	}

	s += m[(x/1000)*1000] + m[((x/100)%10)*100] + m[((x/10)%10)*10] + m[x%10];

	cout << s << endl;
}

string put(char c, int k) {
	string res = "";
	REP(i,k) res += c;
	return res;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <map>
#include <set>
#include <string>
#include <vector>
#include <deque>

#define NAME		""
#define INF			(int)1e9
#define EPS			1e-9
#define m_pi		(3.1415926535897931)
#define SZ(x)		x.size()
#define PB			push_back
#define MP			make_pair
#define fi			first
#define se			second
#define END			cout<<endl
#define FOR(i,a,b)	for(int i=a;i<=b;++i)
#define REP(i,n)	for(int i=0;i<n;++i)
#define ALL(x)		(x.begin(), x.end())
#define REV(x)		reverse(x.begin(), x.end())
#define MAX_N		int(2e2+2)
#define MAX_V		int(1e5)

using namespace std;

typedef deque<int>		DI;
typedef deque<double>	DD;
typedef deque<DI>		DII;
typedef long long		LL;
typedef long double		LD;
typedef	double			D;
typedef pair<int,int>	PII;
typedef pair<D,D>		PDD;

string put(char,int), c, num;
map<int,string> m;
void Rim(int), Dig(LL, int);
LL a, b = 0, n = 0;

int main(){

/*	freopen(NAME".in","r",stdin);
	freopen(NAME".out","w",stdout); //*/

	cin >> a >> c >> num;

	while (num[0] == '0' && SZ(num) > 0) num.erase(0,1);

	if (SZ(num) == 0) num += '0';

	int t;
	REV(num);
	REP(i,SZ(num)) t = (num[i] >= 'A') ? (num[i]-'A'+10) : (num[i]-'0'), n += LL(t*pow(a+.0,i));

	if (c[0] == 'R') {
        Rim(n);
	} else {
		if (n == 0) {
			 cout << 0 << endl;
			 return 0;
		}
		REP(i,SZ(c)) b *= 10, b += c[i]-'0';
		Dig(n,b);
	}

	return 0;
}

void Dig(LL x, int k) {
	string s = "";
//	cout << x<<endl;
	while (x > 0) {
	 	s += (k > 9 && x%k > 9) ? char('A' + (x%k) - 10) : char('0' + x%k);
//	 	cout  << x%k << " - "<< s << ' ';
	 	x /= k;
//	 	cout << x << endl;
	}
	REV(s);
	cout << s << endl;
}

void Rim(int x){
	string s = "";
	m[0]   = "";
	m[1]   = "I";
	m[4]   = "IV";
	m[5]   = "V";
	m[9]   = "IX";
	m[10]  = "X";
	m[40]  = "XL";
	m[50]  = "L";
	m[90]  = "XC";
	m[100] = "C";
	m[400] = "CD";
	m[500] = "D";
	m[900] = "CM";
	m[1000]= "M";

	for (int i = 2; i <= 3; ++ i) {
		m[i] = put('I',i);
		m[i*10] = put('X',i);
		m[i*100] = put('C',i);
		m[i*1000] = put('M',i);
	}

	for (int j = 6; j <= 8; ++ j){
		m[j] = "V"+put('I',j-5);
		m[j*10] = "L"+put('X',j-5);
		m[j*100] = "D"+put('C',j-5);
	}

	s += m[(x/1000)*1000] + m[((x/100)%10)*100] + m[((x/10)%10)*10] + m[x%10];

	cout << s << endl;
}

string put(char c, int k) {
	string res = "";
	REP(i,k) res += c;
	return res;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <map>
#include <set>
#include <string>
#include <vector>
#include <deque>

#define NAME		""
#define INF			(int)1e9
#define EPS			1e-9
#define m_pi		(3.1415926535897931)
#define SZ(x)		x.size()
#define PB			push_back
#define MP			make_pair
#define fi			first
#define se			second
#define END			cout<<endl
#define FOR(i,a,b)	for(int i=a;i<=b;++i)
#define REP(i,n)	for(int i=0;i<n;++i)
#define ALL(x)		(x.begin(), x.end())
#define REV(x)		reverse(x.begin(), x.end())
#define MAX_N		int(2e2+2)
#define MAX_V		int(1e5)

using namespace std;

typedef deque<int>		DI;
typedef deque<double>	DD;
typedef deque<DI>		DII;
typedef long long		LL;
typedef long double		LD;
typedef	double			D;
typedef pair<int,int>	PII;
typedef pair<D,D>		PDD;

string put(char,int), c, num;
map<int,string> m;
void Rim(int), Dig(LL, int);
LL a, b = 0, n = 0;

int main(){

/*	freopen(NAME".in","r",stdin);
	freopen(NAME".out","w",stdout); //*/

	cin >> a >> c >> num;

	while (num[0] == '0' && SZ(num) > 0) num.erase(0,1);

	int t;
	REV(num);
	REP(i,SZ(num)) t = (num[i] >= 'A') ? (num[i]-'A'+10) : (num[i]-'0'), n += LL(t*pow(a+.0,i));

	if (c[0] == 'R') {
        Rim(n);
	} else {
		REP(i,SZ(c)) b *= 10, b += c[i]-'0';
		Dig(n,b);
	}

	return 0;
}

void Dig(LL x, int k) {
	string s = "";
//	cout << x<<endl;
	while (x > 0) {
	 	s += (k > 9 && x%k > 9) ? char('A' + (x%k) - 10) : char('0' + x%k);
//	 	cout  << x%k << " - "<< s << ' ';
	 	x /= k;
//	 	cout << x << endl;
	}
	REV(s);
	cout << s << endl;
}

void Rim(int x){
	string s = "";
	m[0]   = "";
	m[1]   = "I";
	m[4]   = "IV";
	m[5]   = "V";
	m[9]   = "IX";
	m[10]  = "X";
	m[40]  = "XL";
	m[50]  = "L";
	m[90]  = "XC";
	m[100] = "C";
	m[400] = "CD";
	m[500] = "D";
	m[900] = "CM";
	m[1000]= "M";

	for (int i = 2; i <= 3; ++ i) {
		m[i] = put('I',i);
		m[i*10] = put('X',i);
		m[i*100] = put('C',i);
		m[i*1000] = put('M',i);
	}

	for (int j = 6; j <= 8; ++ j){
		m[j] = "V"+put('I',j-5);
		m[j*10] = "L"+put('X',j-5);
		m[j*100] = "D"+put('C',j-5);
	}

	s += m[(x/1000)*1000] + m[((x/100)%10)*100] + m[((x/10)%10)*10] + m[x%10];

	cout << s << endl;
}

string put(char c, int k) {
	string res = "";
	REP(i,k) res += c;
	return res;
}


#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <set>
#include <vector>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <cmath>
#include <string>

#define fr(x,y) for(int x=0; x<(y); ++x)
#define fe(x,y,z) for(int x=(y); x<(z); ++x)
#define fw(x,y,z) for(int x=(y); x<=(z); ++x)
#define df(x,y,z) for(int x=(y); x>=(z); --x)
#define mn(x,y) ((x)<(y) ? (x) : (y))
#define mx(x,y) ((x)>(y) ? (x) : (y))
#define ab(x) ((x)<0 ? (-(x)) : (x))
#define MP make_pair
#define PB push_back
#define BIG 1000000000
#define X first
#define Y second
#define dbg(x) if(DEBUG) {cout<<#x<<": "<<(x)<<endl;}
#define dout(x) if(DEBUG) {cout<<x;}
#define dline(x) if(DEBUG) {cout<<x<<endl;}
#define dbgr(x,l) if(DEBUG) {cout<<#x<<": ";fr(innercounter,l) cout<<x[innercounter]<<" ";cout<<endl;}
#define dbge(x,y,z) if(DEBUG) {cout<<#x<<": ";fe(innercounter,y,z) cout<<x[innercounter]<<" ";cout<<endl;}
#define dbgw(x,y,z) if(DEBUG) {cout<<#x<<": ";fw(innercounter,y,z) cout<<x[innercounter]<<" ";cout<<endl;}
#define dbgee(x,l1,l2) if(DEBUG) {cout<<#x<<": "<<endl;fr(icounter,l1){fr(jcounter,l2) cout<<x[icounter][jcounter]<<" ";cout<<endl;}}

bool DEBUG = false;

using namespace std;

int a,b,len;
__int64 c;
char buf[100],ch;
char bas[] = {'I', 'X', 'C', 'M'};
char fiv[] = {'V', 'L', 'D', 'Z'};

int digit(char symbol)
{
if(symbol>='0'&&symbol<='9') return symbol-'0';
return symbol-'A'+10;
}

char symbol(int digit)
{
if(digit>=10) return 'A'+(digit-10);
return '0'+digit;
}

void read()
{
gets(buf);
len = strlen(buf);
fr(i,len)
    c = c*a+digit(buf[i]);
}

void number()
{
len = 0;
while(c>0)
    {
    buf[len++] = symbol(c%b);
    c/=b;
    }
if(len==0) buf[len++] = '\0';
}

void roman()
{
len = 0;
fr(i,4)
    {
    a = c%10;
    dbg(a);
    if(a==9)
        {
        buf[len++] = bas[i+1];
        buf[len++] = bas[i];
        }
    else if(a>=5)
        {
        fr(j,a-5)
            buf[len++] = bas[i];
        buf[len++] = fiv[i];
        }
    else if(a==4)
        {
        buf[len++] = fiv[i];
        buf[len++] = bas[i];
        }
    else
        {
        fr(j,a)
            buf[len++] = bas[i];
        }
    buf[len] = '\0';
//  puts(buf);
    c/=10;
    }
}

int main()
{
#ifdef HOME
freopen("input.txt", "r",stdin);
freopen("output.txt", "w", stdout);
DEBUG = true;
#endif
if(scanf("%d %d", &a, &b)==1)
    {
    scanf("%c", &ch);
    b = -1;
    }
dbg(a);
dbg(b);
scanf("\n");
read();
if(b==-1) roman();
else number();
dbg(c);
buf[len] = '\0';
df(i,len-1,0)
    putchar(buf[i]);

return 0;
}


#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <set>
#include <vector>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <cmath>
#include <string>

#define fr(x,y) for(int x=0; x<(y); ++x)
#define fe(x,y,z) for(int x=(y); x<(z); ++x)
#define fw(x,y,z) for(int x=(y); x<=(z); ++x)
#define df(x,y,z) for(int x=(y); x>=(z); --x)
#define mn(x,y) ((x)<(y) ? (x) : (y))
#define mx(x,y) ((x)>(y) ? (x) : (y))
#define ab(x) ((x)<0 ? (-(x)) : (x))
#define MP make_pair
#define PB push_back
#define BIG 1000000000
#define X first
#define Y second
#define dbg(x) if(DEBUG) {cout<<#x<<": "<<(x)<<endl;}
#define dout(x) if(DEBUG) {cout<<x;}
#define dline(x) if(DEBUG) {cout<<x<<endl;}
#define dbgr(x,l) if(DEBUG) {cout<<#x<<": ";fr(innercounter,l) cout<<x[innercounter]<<" ";cout<<endl;}
#define dbge(x,y,z) if(DEBUG) {cout<<#x<<": ";fe(innercounter,y,z) cout<<x[innercounter]<<" ";cout<<endl;}
#define dbgw(x,y,z) if(DEBUG) {cout<<#x<<": ";fw(innercounter,y,z) cout<<x[innercounter]<<" ";cout<<endl;}
#define dbgee(x,l1,l2) if(DEBUG) {cout<<#x<<": "<<endl;fr(icounter,l1){fr(jcounter,l2) cout<<x[icounter][jcounter]<<" ";cout<<endl;}}

bool DEBUG = false;

using namespace std;

int a,b,len;
__int64 c;
char buf[100],ch;
char bas[] = {'I', 'X', 'C', 'M'};
char fiv[] = {'V', 'L', 'D', 'Z'};

int digit(char symbol)
{
if(symbol>='0'&&symbol<='9') return symbol-'0';
return symbol-'A'+10;
}

char symbol(int digit)
{
if(digit>=10) return 'A'+(digit-10);
return '0'+digit;
}

void read()
{
gets(buf);
len = strlen(buf);
fr(i,len)
    c = c*a+digit(buf[i]);
}

void number()
{
len = 0;
while(c>0)
    {
    buf[len++] = symbol(c%b);
    c/=b;
    }
if(len==0) buf[len++] = '0';
}

void roman()
{
len = 0;
fr(i,4)
    {
    a = c%10;
    dbg(a);
    if(a==9)
        {
        buf[len++] = bas[i+1];
        buf[len++] = bas[i];
        }
    else if(a>=5)
        {
        fr(j,a-5)
            buf[len++] = bas[i];
        buf[len++] = fiv[i];
        }
    else if(a==4)
        {
        buf[len++] = fiv[i];
        buf[len++] = bas[i];
        }
    else
        {
        fr(j,a)
            buf[len++] = bas[i];
        }
    buf[len] = '\0';
//  puts(buf);
    c/=10;
    }
}

int main()
{
#ifdef HOME
freopen("input.txt", "r",stdin);
freopen("output.txt", "w", stdout);
DEBUG = true;
#endif
if(scanf("%d %d", &a, &b)==1)
    {
    scanf("%c", &ch);
    b = -1;
    }
dbg(a);
dbg(b);
scanf("\n");
read();
if(b==-1) roman();
else number();
dbg(c);
buf[len] = '\0';
df(i,len-1,0)
    putchar(buf[i]);

return 0;
}


#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <set>
#include <vector>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <cmath>
#include <string>

#define fr(x,y) for(int x=0; x<(y); ++x)
#define fe(x,y,z) for(int x=(y); x<(z); ++x)
#define fw(x,y,z) for(int x=(y); x<=(z); ++x)
#define df(x,y,z) for(int x=(y); x>=(z); --x)
#define mn(x,y) ((x)<(y) ? (x) : (y))
#define mx(x,y) ((x)>(y) ? (x) : (y))
#define ab(x) ((x)<0 ? (-(x)) : (x))
#define MP make_pair
#define PB push_back
#define BIG 1000000000
#define X first
#define Y second
#define dbg(x) if(DEBUG) {cout<<#x<<": "<<(x)<<endl;}
#define dout(x) if(DEBUG) {cout<<x;}
#define dline(x) if(DEBUG) {cout<<x<<endl;}
#define dbgr(x,l) if(DEBUG) {cout<<#x<<": ";fr(innercounter,l) cout<<x[innercounter]<<" ";cout<<endl;}
#define dbge(x,y,z) if(DEBUG) {cout<<#x<<": ";fe(innercounter,y,z) cout<<x[innercounter]<<" ";cout<<endl;}
#define dbgw(x,y,z) if(DEBUG) {cout<<#x<<": ";fw(innercounter,y,z) cout<<x[innercounter]<<" ";cout<<endl;}
#define dbgee(x,l1,l2) if(DEBUG) {cout<<#x<<": "<<endl;fr(icounter,l1){fr(jcounter,l2) cout<<x[icounter][jcounter]<<" ";cout<<endl;}}

bool DEBUG = false;

using namespace std;

int a,b,len;
__int64 c;
char buf[100],ch;
char bas[] = {'I', 'X', 'C', 'M'};
char fiv[] = {'V', 'L', 'D', 'Z'};

int digit(char symbol)
{
if(symbol>='0'&&symbol<='9') return symbol-'0';
return symbol-'A'+10;
}

char symbol(int digit)
{
if(digit>=10) return 'A'+(digit-10);
return '0'+digit;
}

void read()
{
gets(buf);
len = strlen(buf);
fr(i,len)
    c = c*a+digit(buf[i]);
}

void number()
{
len = 0;
while(c>0)
    {
    buf[len++] = symbol(c%b);
    c/=b;
    }
}

void roman()
{
len = 0;
fr(i,4)
    {
    a = c%10;
    dbg(a);
    if(a==9)
        {
        buf[len++] = bas[i+1];
        buf[len++] = bas[i];
        }
    else if(a>=5)
        {
        fr(j,a-5)
            buf[len++] = bas[i];
        buf[len++] = fiv[i];
        }
    else if(a==4)
        {
        buf[len++] = fiv[i];
        buf[len++] = bas[i];
        }
    else
        {
        fr(j,a)
            buf[len++] = bas[i];
        }
    buf[len] = '\0';
    puts(buf);
    c/=10;
    }
}

int main()
{
#ifdef HOME
freopen("input.txt", "r",stdin);
freopen("output.txt", "w", stdout);
DEBUG = true;
#endif
if(scanf("%d %d", &a, &b)==1)
    {
    scanf("%c", &ch);
    b = -1;
    }
dbg(a);
dbg(b);
scanf("\n");
read();
if(b==-1) roman();
else number();
dbg(c);
buf[len] = '\0';
df(i,len-1,0)
    putchar(buf[i]);
return 0;
}


#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <set>
#include <vector>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <cmath>
#include <string>

#define fr(x,y) for(int x=0; x<(y); ++x)
#define fe(x,y,z) for(int x=(y); x<(z); ++x)
#define fw(x,y,z) for(int x=(y); x<=(z); ++x)
#define df(x,y,z) for(int x=(y); x>=(z); --x)
#define mn(x,y) ((x)<(y) ? (x) : (y))
#define mx(x,y) ((x)>(y) ? (x) : (y))
#define ab(x) ((x)<0 ? (-(x)) : (x))
#define MP make_pair
#define PB push_back
#define BIG 1000000000
#define X first
#define Y second
#define dbg(x) if(DEBUG) {cout<<#x<<": "<<(x)<<endl;}
#define dout(x) if(DEBUG) {cout<<x;}
#define dline(x) if(DEBUG) {cout<<x<<endl;}
#define dbgr(x,l) if(DEBUG) {cout<<#x<<": ";fr(innercounter,l) cout<<x[innercounter]<<" ";cout<<endl;}
#define dbge(x,y,z) if(DEBUG) {cout<<#x<<": ";fe(innercounter,y,z) cout<<x[innercounter]<<" ";cout<<endl;}
#define dbgw(x,y,z) if(DEBUG) {cout<<#x<<": ";fw(innercounter,y,z) cout<<x[innercounter]<<" ";cout<<endl;}
#define dbgee(x,l1,l2) if(DEBUG) {cout<<#x<<": "<<endl;fr(icounter,l1){fr(jcounter,l2) cout<<x[icounter][jcounter]<<" ";cout<<endl;}}

bool DEBUG = false;

using namespace std;

int a,b,len;
__int64 c;
char buf[100],ch;
char bas[] = {'I', 'X', 'C', 'M'};
char fiv[] = {'V', 'L', 'D', 'Z'};

int digit(char symbol)
{
if(symbol>='0'&&symbol<='9') return symbol-'0';
return symbol-'A'+10;
}

char symbol(int digit)
{
if(digit>=10) return 'A'+(digit-10);
return '0'+digit;
}

void read()
{
gets(buf);
len = strlen(buf);
fr(i,len)
    c = c*a+digit(buf[i]);
}

void number()
{
len = 0;
while(c>0)
    {
    buf[len++] = symbol(c%b);
    c/=b;
    }
}

void roman()
{
len = 0;
fr(i,4)
    {
    a = c%10;
    dbg(a);
    if(a==9)
        {
        buf[len++] = bas[i+1];
        buf[len++] = bas[i];
        }
    else if(a>=5)
        {
        fr(j,a-5)
            buf[len++] = bas[i];
        buf[len++] = fiv[i];
        }
    else if(a==4)
        {
        buf[len++] = fiv[i];
        buf[len++] = bas[i];
        }
    else
        {
        fr(j,a)
            buf[len++] = bas[i];
        }
    buf[len] = '\0';
//  puts(buf);
    c/=10;
    }
}

int main()
{
#ifdef HOME
freopen("input.txt", "r",stdin);
freopen("output.txt", "w", stdout);
DEBUG = true;
#endif
if(scanf("%d %d", &a, &b)==1)
    {
    scanf("%c", &ch);
    b = -1;
    }
dbg(a);
dbg(b);
scanf("\n");
read();
if(b==-1) roman();
else number();
dbg(c);
buf[len] = '\0';
df(i,len-1,0)
    putchar(buf[i]);
return 0;
}


#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <set>
#include <vector>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <cmath>
#include <string>

#define fr(x,y) for(int x=0; x<(y); ++x)
#define fe(x,y,z) for(int x=(y); x<(z); ++x)
#define fw(x,y,z) for(int x=(y); x<=(z); ++x)
#define df(x,y,z) for(int x=(y); x>=(z); --x)
#define mn(x,y) ((x)<(y) ? (x) : (y))
#define mx(x,y) ((x)>(y) ? (x) : (y))
#define ab(x) ((x)<0 ? (-(x)) : (x))
#define MP make_pair
#define PB push_back
#define BIG 1000000000
#define X first
#define Y second
#define dbg(x) if(DEBUG) {cout<<#x<<": "<<(x)<<endl;}
#define dout(x) if(DEBUG) {cout<<x;}
#define dline(x) if(DEBUG) {cout<<x<<endl;}
#define dbgr(x,l) if(DEBUG) {cout<<#x<<": ";fr(innercounter,l) cout<<x[innercounter]<<" ";cout<<endl;}
#define dbge(x,y,z) if(DEBUG) {cout<<#x<<": ";fe(innercounter,y,z) cout<<x[innercounter]<<" ";cout<<endl;}
#define dbgw(x,y,z) if(DEBUG) {cout<<#x<<": ";fw(innercounter,y,z) cout<<x[innercounter]<<" ";cout<<endl;}
#define dbgee(x,l1,l2) if(DEBUG) {cout<<#x<<": "<<endl;fr(icounter,l1){fr(jcounter,l2) cout<<x[icounter][jcounter]<<" ";cout<<endl;}}

bool DEBUG = false;

using namespace std;

int a,b,len;
__int64 c;
char buf[10000],ch;
char bas[] = {'I', 'X', 'C', 'M'};
char fiv[] = {'V', 'L', 'D', 'Z'};

int digit(char symbol)
{
if(symbol>='0'&&symbol<='9') return symbol-'0';
return symbol-'A'+10;
}

char symbol(int digit)
{
if(digit>=10) return 'A'+(digit-10);
return '0'+digit;
}

void read()
{
gets(buf);
len = strlen(buf);
fr(i,len)
    c = c*a+digit(buf[i]);
}

void number()
{
len = 0;
while(c>0)
    {
    buf[len++] = symbol(c%b);
    c/=b;
    }
if(len==0) buf[len++] = '0';
}

void roman()
{
len = 0;
fr(i,4)
    {
    a = c%10;
    dbg(a);
    if(a==9)
        {
        buf[len++] = bas[i+1];
        buf[len++] = bas[i];
        }
    else if(a>=5)
        {
        fr(j,a-5)
            buf[len++] = bas[i];
        buf[len++] = fiv[i];
        }
    else if(a==4)
        {
        buf[len++] = fiv[i];
        buf[len++] = bas[i];
        }
    else
        {
        fr(j,a)
            buf[len++] = bas[i];
        }
    buf[len] = '\0';
//  puts(buf);
    c/=10;
    }
}

int main()
{
#ifdef HOME
freopen("input.txt", "r",stdin);
freopen("output.txt", "w", stdout);
DEBUG = true;
#endif
if(scanf("%d %d", &a, &b)==1)
    {
    scanf("%c", &ch);
    b = -1;
    }
dbg(a);
dbg(b);
scanf("\n");
read();
if(b==-1) roman();
else number();
dbg(c);
buf[len] = '\0';
df(i,len-1,0)
    putchar(buf[i]);

return 0;
}


#include <iostream>
#include <string>
#include <algorithm>
#include <vector>

using namespace std;

typedef long long ll;

void convert(int count ,char ten ,  char five, char one){
  if(count >= 9){
    cout << one << ten;
  }else if(count >= 5){
    cout << five;
    for(int i=0;i<count-5;i++){
      cout << one;
    }
  }else if(count == 4){
    cout << one << five;
  }else{
    for(int i=0;i<count;i++){
      cout << one;
    }
  }
  return ;
}

int main(){
  ll a;string s;cin >> a>> s;
  string c;cin >> c;

  ll val = 0;
  for(ll i = 0;i<(ll) c.size();i++){
    val*=a;
    val+= (c[i] - 'A' >= 0 ?  c[i] - 'A'+ 10 : c[i]-'0');
  }

  if(s == "R"){
    for(int i = 0;i<val/1000;i++){
      cout << "M";
    }
    val = val - (val/1000) * 1000;
    convert(val/100,'M','D','C');
    val = val - (val/100)*100;
    convert(val/10,'C', 'L','X');
    val = val - (val/10)*10;
    convert(val,'X','V','I');

  }else{
    ll b = 0;
    if(s.size()==1){
      b = s[0] - '0';
    }else{
      b = (s[0]-'0') * 10 + (s[1] - '0');
    }

    vector<ll> ans;
    while(val > 0){
      ans.push_back(val%b);
      val /=b;
    }
    ans.push_back(0);

    reverse(ans.begin(), ans.end());
    for(ll i = 0;i<(ll) ans.size();i++){
      if(ans[i] >= 10){
	char c = 'A' + ans[i] - 10;
	cout << c;
      }else{
	cout << ans[i] ;
      }
    }
  }
}


#include <iostream>
#include <string>
#include <algorithm>
#include <vector>

using namespace std;

typedef long long ll;

void convert(int count ,char ten ,  char five, char one){
  if(count >= 9){
    cout << one << ten;
  }else if(count >= 5){
    cout << five;
    for(int i=0;i<count-5;i++){
      cout << one;
    }
  }else if(count == 4){
    cout << one << five;
  }else{
    for(int i=0;i<count;i++){
      cout << one;
    }
  }
  return ;
}

int main(){
  ll a;string s;cin >> a>> s;
  string c;cin >> c;

  ll val = 0;
  for(ll i = 0;i<(ll) c.size();i++){
    val*=a;
    val+= (c[i] - 'A' >= 0 ?  c[i] - 'A'+ 10 : c[i]-'0');
  }

  if(s == "R"){
    for(int i = 0;i<val/1000;i++){
      cout << "M";
    }
    val = val - (val/1000) * 1000;
    convert(val/100,'M','D','C');
    val = val - (val/100)*100;
    convert(val/10,'C', 'L','X');
    val = val - (val/10)*10;
    convert(val,'X','V','I');

  }else{
    ll b = 0;
    if(s.size()==1){
      b = s[0] - '0';
    }else{
      b = (s[0]-'0') * 10 + (s[1] - '0');
    }

    vector<ll> ans;
    while(val > 0){
      ans.push_back(val%b);
      val /=b;
    }
    if(!ans.empty()){
      cout << 0 <<endl;
      return 0;
    }

    reverse(ans.begin(), ans.end());
    for(ll i = 0;i<(ll) ans.size();i++){
      if(ans[i] >= 10){
	char c = 'A' + ans[i] - 10;
	cout << c;
      }else{
	cout << ans[i] ;
      }
    }
  }
}


#include <iostream>
#include <string>
#include <algorithm>
#include <vector>

using namespace std;

typedef long long ll;

void convert(int count ,char ten ,  char five, char one){
  if(count >= 9){
    cout << one << ten;
  }else if(count >= 5){
    cout << five;
    for(int i=0;i<count-5;i++){
      cout << one;
    }
  }else if(count == 4){
    cout << one << five;
  }else{
    for(int i=0;i<count;i++){
      cout << one;
    }
  }
  return ;
}

int main(){
  ll a;string s;cin >> a>> s;
  string c;cin >> c;

  ll val = 0;
  for(ll i = 0;i<(ll) c.size();i++){
    val*=a;
    val+= (c[i] - 'A' >= 0 ?  c[i] - 'A'+ 10 : c[i]-'0');
  }

  if(s == "R"){
    for(int i = 0;i<val/1000;i++){
      cout << "M";
    }
    val = val - (val/1000) * 1000;
    convert(val/100,'M','D','C');
    val = val - (val/100)*100;
    convert(val/10,'C', 'L','X');
    val = val - (val/10)*10;
    convert(val,'X','V','I');

  }else{
    ll b = 0;
    if(s.size()==1){
      b = s[0] - '0';
    }else{
      b = (s[0]-'0') * 10 + (s[1] - '0');
    }

    vector<ll> ans;
    while(val > 0){
      ans.push_back(val%b);
      val /=b;
    }

    reverse(ans.begin(), ans.end());
    for(ll i = 0;i<(ll) ans.size();i++){
      if(ans[i] >= 10){
	char c = 'A' + ans[i] - 10;
	cout << c;
      }else{
	cout << ans[i] ;
      }
    }
  }
}


#include <iostream>
#include <string>
#include <algorithm>
#include <vector>

using namespace std;

typedef long long ll;

void convert(int count ,char ten ,  char five, char one){
  if(count >= 9){
    cout << one << ten;
  }else if(count >= 5){
    cout << five;
    for(int i=0;i<count-5;i++){
      cout << one;
    }
  }else if(count == 4){
    cout << one << five;
  }else{
    for(int i=0;i<count;i++){
      cout << one;
    }
  }
  return ;
}

int main(){
  ll a;string s;cin >> a>> s;
  string c;cin >> c;

  ll val = 0;
  for(ll i = 0;i<(ll) c.size();i++){
    val*=a;
    val+= (c[i] - 'A' >= 0 ?  c[i] - 'A'+ 10 : c[i]-'0');
  }

  if(s == "R"){
    for(int i = 0;i<val/1000;i++){
      cout << "M";
    }
    val = val - (val/1000) * 1000;
    convert(val/100,'M','D','C');
    val = val - (val/100)*100;
    convert(val/10,'C', 'L','X');
    val = val - (val/10)*10;
    convert(val,'X','V','I');

  }else{
    ll b = 0;
    if(s.size()==1){
      b = s[0] - '0';
    }else{
      b = (s[0]-'0') * 10 + (s[1] - '0');
    }

    vector<ll> ans;
    while(val > 0){
      ans.push_back(val%b);
      val /=b;
    }
    if(ans.empty()){
      cout << 0 <<endl;
      return 0;
    }

    reverse(ans.begin(), ans.end());
    for(ll i = 0;i<(ll) ans.size();i++){
      if(ans[i] >= 10){
	char c = 'A' + ans[i] - 10;
	cout << c;
      }else{
	cout << ans[i] ;
      }
    }
  }
}


#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <cstdlib>
#include <algorithm>

using namespace std;

string dectob(long long n,int b)
{
    string digits="0123456789ABCDEFGHIJKLMNOPQ";
    string result="";

    while(n>0)
    {
        result+=digits[n%b];
        n/=b;
    }

    reverse(result.begin(),result.end());

    return result;
}

long long btodec(string n,int b)
{
    int i,l=n.size();
    long long res=0;

    for(i=0;i<l;i++)
        if(isalpha(n[i]))
        {
            res*=b;
            res+=(n[i]-'A'+10);
        }
        else
        {
            res*=b;
            res+=(n[i]-'0');
        }

    return res;
}

int main()
{
    int a,bs,d[15]={1000,900,500,450,400,100,90,50,45,40,10,9,5,4,1};
    string sm[15]={"M","CM","D","LD","CD","C","XC","L","VL","XL","X","IX","V","IV","I"};
    string b,c,cn;

    cin >> a >> b >> c;

    long long dec=btodec(c,a);

    if(b=="R")
    {
        while(dec)
        {
            for(int i=0;i<14;i++)
                if(dec/d[i])
                {
                    int v=dec/d[i];

                    for(int j=0;j<v;j++)
                        cn+=sm[i];

                    dec-=(d[i]*v);
                }
        }

        cout << cn << endl;
    }
    else
    {
        bs=atoi(b.c_str());
        cn=dectob(dec,bs);
        cout << cn << endl;
    }

    return 0;
}


#include <iostream>

#define forn(i,n) for (unsigned i = 0; i < n; ++i)

using namespace std;

int min(int a, int b) {
	if (a > b) return b;
	else return a;
}

int main()
{
	int q,k, n, a, b;
	cin >> q;
	forn(_, q) {
		cin >> k >> n >> a >> b;
		if (n * b > k) {
			cout << "-1\n";
			continue;
		}
		cout << min((k - n * b - 1) / (a - b),n) <<endl;
	}
}


#include <iostream>
#include <math.h>

#define forn(i,n) for (unsigned i = 0; i < n; ++i)

using namespace std;

int getY(int k,int a,int b,int x) {
	return (k - a * x) / b;
}

int main()
{
	int q;
	cin >> q;

	forn(_, q) {
		int k, n, a, b,x = 0,y,start = 0,stop;
		cin >> k >> n >> a >> b;

		if (k / a >= n && k % a > 0) {
			cout << n << endl;
			continue;
		}

		if (k / b <= n) {
			if (k / b == n && k % b > 0) cout << 0 << endl;
			else cout << -1 << endl;
			continue;
		}

		stop = k / a;

		forn (r,log(n)) {
			x = (start + stop) / 2;
			y = getY(k, a, b, x);

			if (x+y < n) {
				stop = x;
			}
			else {
				start = x;
			}
		}

		if (x + y >= n) {
			y = getY(k,a,b,x + 1);
			if (x + 1 + y >= n) x++;
		}

		cout << x << endl;
	}
}


#include <iostream>
#include <math.h>

#define forn(i,n) for (unsigned i = 0; i < n; ++i)

using namespace std;

int getY(int k,int a,int b,int x) {
	return (k - a * x) / b;
}

int main()
{
	int q;
	cin >> q;

	forn(_, q) {
		int k, n, a, b,x = 0,y,start = 0,stop;
		cin >> k >> n >> a >> b;

		if (k / a >= n) {
			if (k / a == n && k % a == 0) ;
			else cout << n << endl;
			continue;
		}

		if (k / b <= n) {
			if (k / b == n && k % b > 0) cout << 0 << endl;
			else cout << -1 << endl;
			continue;
		}

		stop = k / a;

		forn (r,log(n)) {
			x = (start + stop) / 2;
			y = getY(k, a, b, x);

			if (x+y < n) {
				stop = x;
			}
			else {
				start = x;
			}
		}

		if (x + y >= n) {
			y = getY(k,a,b,x + 1);
			if (x + 1 + y >= n) x++;
		}

		cout << x << endl;
	}
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
	long long q; cin >> q;
	while(q--)
	{
		long long k, n, a, b, sum = 0;
		cin >> k >> n >> a >> b;
		long long it = n * b;
		if(it >= k)
		{
			cout << -1 << endl;
			continue;
		}
		while(it + a - b < k)
			it += a, it -= b, sum++;
		cout << min(n,sum) << endl;
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
	long long q; cin >> q;
	while(q--)
	{
		long long k, n, a, b, it = LONG_LONG_MAX;
		cin >> k >> n >> a >> b;
		if(n*b > k) it = -1;
		long long sum = (k-(n*b)-1)/(a-b);
		long long answer = min(it,min(n,sum));
		cout << max(-1,answer) << endl;
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
	long long q; cin >> q;
	while(q--)
	{
		long long k, n, a, b, it = LONG_LONG_MAX;
		cin >> k >> n >> a >> b;
		if(n*b > k) it = -1;
		long long sum = (k-(n*b)-1)/(a-b);
		long long answer = min(it,min(n,sum));
		cout << max((long long)-1,answer) << endl;
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
	int q; cin >> q;
	while(q--)
	{
		int k, n, a, b, sum = 0;
		cin >> k >> n >> a >> b;
if(a<b) swap(a,b);
		int it = n * b;
		if(it >= k)
		{
			cout << -1 << endl;
			continue;
		}
		while(it + a - b < k)
			it += a, it -= b, sum++;
		cout << min(n,sum) << endl;
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
	long long q; cin >> q;
	while(q--)
	{
		long long k, n, a, b, sum = 0, it = LONG_LONG_MAX;
		cin >> k >> n >> a >> b;
		if(n*b > k) it = -1;
		long long sum = (k-(n*b)-1)/(a-b);
		cout << min(it,min(n,sum)) << endl;
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
	long long q; cin >> q;
	while(q--)
	{
		long long k, n, a, b, it = LONG_LONG_MAX;
		cin >> k >> n >> a >> b;
		if(n*b > k) it = -1;
		long long sum = (k-(n*b)-1)/(a-b);
		cout << min(it,min(n,sum)) << endl;
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
	long long q; cin >> q;
	while(q--)
	{
		long long k, n, a, b, it = LONG_LONG_MAX;
		cin >> k >> n >> a >> b;
		if(n*b >= k) it = -1;
		long long sum = (k-(n*b)-1)/(a-b);
		long long answer = min(it,min(n,sum));
		cout << max((long long)-1,answer) << endl;
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
	int q; cin >> q;
	while(q--)
	{
		int k, n, a, b, sum = 0;
		cin >> k >> n >> a >> b;
		int it = n * b;
		if(it >= k)
		{
			cout << -1 << endl;
			continue;
		}
		while(it + a - b < k)
			it += a, it -= b, sum++;
		cout << min(n,sum) << endl;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long q;
cin>>q;
while(q--)
{long long k,n,a,b;
double allow;
cin>>k>>n>>a>>b;
double x=(k-b*n)/((a-b)*1.0);
//cout<<x<<" ";
if(x==(int)x)
allow=x-1;
else
allow=x;
//cout<<allow-n<<" ";
if(allow>0)
{if(allow-n>0)
cout<<n;
else if(allow-n==0)
{if((k-(n-1)*a)>b)
 {  if(n==1)
   cout<<1;
   else
   cout<<n-1;
 }
else
cout<<-1;
}
else
cout<<(int)allow;
}
else
cout<<-1;

    cout<<"\n";
}

}


#include<iostream>
using namespace std;
int main()
{long long q;
cin>>q;
while(q--)
{int k,n,a,b;
float allow;
cin>>k>>n>>a>>b;
float x=(k-b*n)/((a-b)*1.0);
//cout<<x<<" ";
if(x==(int)x)
allow=x-1;
else
allow=x;
if(allow>0)
{if(allow-n>0)
cout<<n;
else if(allow-n==0)
cout<<n-1;
else
cout<<(int)allow;
}
else
cout<<-1;

    cout<<"\n";
}

}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long q;
cin>>q;
while(q--)
{long long k,n,a,b;
double allow;
cin>>k>>n>>a>>b;
double x=(k-b*n)/((a-b)*1.0);
//cout<<x<<" ";
if(x==(int)x)
allow=x-1;
else
allow=x;
//cout<<allow-n<<" ";
if(allow>=0)
{if(allow-n>0)
cout<<n;
else if(allow-n==0)
{if((k-(n-1)*a)>b)
 {  if(n==1)
   cout<<1;
   else
   cout<<n-1;
 }
else
cout<<-1;
}
else
cout<<(int)allow;
}
else
cout<<-1;

    cout<<"\n";
}

}


#include<iostream>
using namespace std;
int main()
{long long q;
cin>>q;
while(q--)
{int k,n,a,b;
float allow;
cin>>k>>n>>a>>b;
float x=(k-b*n)/((a-b)*1.0);
//cout<<x<<" ";
if(x==(int)x)
allow=x-1;
else
allow=x;
if(allow>0)
{if(allow-n>=0)
cout<<n;
else
cout<<(int)allow;
}
else
cout<<-1;

    cout<<"\n";
}

}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long q;
cin>>q;
while(q--)
{double k,n,a,b;
double allow;
cin>>k>>n>>a>>b;
double x=(k-b*n);
if(x<0)
cout<<-1;
else {x=x/((a-b)*1.0);
//cout<<x<<" ";
if(x==(int)x)
allow=x-1;
else
allow=(int)x;
//cout<<allow-n<<" ";
if(allow>=0)
{if(allow-n>0)
cout<<n;
else if(allow-n==0)
{if((k-(n-1)*a)>b)
 {  if(n==1)
   cout<<1;
   else
   cout<<n;
 }
else
cout<<-1;
}
else
cout<<(int)allow;
}
else
cout<<-1;
}
    cout<<"\n";
} }


#include<bits/stdc++.h>
using namespace std;
int main()
{ ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    long long q;
cin>>q;
while(q--)
{long long k,n,a,b;
double allow;
cin>>k>>n>>a>>b;
double x=(k-b*n);
if(x<0)
cout<<-1;
else {x=x/((a-b)*1.0);
//cout<<x<<" ";
if(x==(int)x)
allow=x-1;
else
allow=(long long)x;
//cout<<allow-n<<" ";
if(allow>=0)
{
cout<<min((long long)allow,n);
}
else
cout<<-1;
}
    cout<<"\n";
} }


#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long q;
cin>>q;
while(q--)
{double k,n,a,b;
double allow;
cin>>k>>n>>a>>b;
double x=(k-b*n)/((a-b)*1.0);
//cout<<x<<" ";
if(x==(int)x)
allow=x-1;
else
allow=(int)x;
//cout<<allow-n<<" ";
if(allow>=0)
{if(allow-n>0)
cout<<n;
else if(allow-n==0)
{if((k-(n-1)*a)>b)
 {  if(n==1)
   cout<<1;
   else
   cout<<n;
 }
else
cout<<-1;
}
else
cout<<(int)allow;
}
else
cout<<-1;

    cout<<"\n";
}

}


#include<bits/stdc++.h>
using namespace std;
int main()
{ ios_base::sync_with_stdio(false);
cin.tie(NULL);
    long long q;
cin>>q;
while(q--)
{long long k,n,a,b;
double allow;
cin>>k>>n>>a>>b;
double x=(k-b*n)/((a-b)*1.0);
//cout<<x<<" ";
if(x==(int)x)
allow=x-1;
else
allow=x;
if(allow>0)
{if(allow-n>0)
cout<<n;
else if(allow-n==0)
{if((k-(n-1)*a)>b)
   cout<<n-1;
else
cout<<-1;
}
else
cout<<(int)allow;
}
else
cout<<-1;

    cout<<"\n";
}

}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long q;
cin>>q;
while(q--)
{long long k,n,a,b;
double allow;
cin>>k>>n>>a>>b;
double x=(k-b*n);
if(x<0)
cout<<-1;
else {x=x/((a-b)*1.0);
//cout<<x<<" ";
if(x==(int)x)
allow=x-1;
else
allow=(long long)x;
//cout<<allow-n<<" ";
if(allow>=0)
{
cout<<min((long long)allow,n);
}
else
cout<<-1;
}
    cout<<"\n";
} }


#include <iostream>
#include<bits/stdc++.h>
using namespace std;
// #define int long long
void solve(){
    int k,n,a,b ;
    cin >> k >> n >> a >> b ;

    cout << b*n < k ? min(int(ceil((double)(k-n*b)/(a-b))-1),n) << endl : -1 ;
}
signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL) ;
	int q ;
	cin >> q ;
	while(q--){
	    solve() ;
	}
	return 0;
}


#include <iostream>
#include<bits/stdc++.h>
using namespace std;
#define int long long
void solve(){
    int k,n,a,b ;
    cin >> k >> n >> a >> b ;

    cout << min(max(-1ll,( int)(ceil((double)(k-n*b)/(a-b))-1)),n) << endl ;
}
signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL) ;
	int q ;
	cin >> q ;
	while(q--){
	    solve() ;
	}
	return 0;
}


#include <iostream>
#include<bits/stdc++.h>
using namespace std;
#define int long long
void solve(){
    int k,n,a,b ;
    cin >> k >> n >> a >> b ;
    int ans = 0 ;
    if(n*b>=k){
        cout << -1 << endl ;
        return ;
    }
    if(n*a < k){
        cout << n << endl ;
        return ;
    }
     if((k-n*b)%(a-b)==0){
         cout << (k-n*b)/(a-b)-1 ;
     }
     else {
         cout << (k-n*b)/(a-b) ;
     }
     cout << endl ;
    // int start = 0 ,end = n ,mid = n/2 ;
    // while(start < end -1){
    //     mid = start + (end-start)/2 ;
    //     int just_play = mid*a + (n-mid)*b ;
    //     if(just_play<k){
    //         start = mid ;
    //     }
    //     else{
    //         end = mid ;
    //     }
    // }
    // if(start == end-1){
    //     if((mid*a+(n-mid)*b)>=k)
    //         mid-- ;
    // }
    // cout << mid << endl ;
}
signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL) ;
	int q ;
	cin >> q ;
	while(q--){
	    solve() ;
	}
	return 0;
}


#include <iostream>
#include<bits/stdc++.h>
using namespace std;
#define int long long
void solve(){
    int k,n,a,b ;
    cin >> k >> n >> a >> b ;

    cout <<( b*n < k ? min((int )(ceil((double)(k-n*b)/(a-b))-1),n) : -1 ) << endl;
}
signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL) ;
	int q ;
	cin >> q ;
	while(q--){
	    solve() ;
	}
	return 0;
}


#include <iostream>
#include<bits/stdc++.h>
using namespace std;
// #define int long long
void solve(){
    int k,n,a,b ;
    cin >> k >> n >> a >> b ;
    cout << min(int(ceil((double)(k-n*b)/(a-b))-1),n) << endl ;
}
signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL) ;
	int q ;
	cin >> q ;
	while(q--){
	    solve() ;
	}
	return 0;
}


#include <iostream>
#include<bits/stdc++.h>
using namespace std;
// #define int long long
void solve(){
    int k,n,a,b ;
    cin >> k >> n >> a >> b ;

    cout <<( b*n < k ? min(int(ceil((double)(k-n*b)/(a-b))-1),n) : -1 ) << endl;
}
signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL) ;
	int q ;
	cin >> q ;
	while(q--){
	    solve() ;
	}
	return 0;
}


#include <iostream>
#include<bits/stdc++.h>
using namespace std;
// #define int long long
void solve(){
    int k,n,a,b ;
    cin >> k >> n >> a >> b ;
    cout << min(max(-1,int(ceil((double)(k-n*b)/(a-b))-1)),n) << endl ;
}
signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL) ;
	int q ;
	cin >> q ;
	while(q--){
	    solve() ;
	}
	return 0;
}


#include <iostream>
#include<bits/stdc++.h>
using namespace std;
#define int long long
void solve(){
    int k,n,a,b ;
    cin >> k >> n >> a >> b ;
    int ans = 0 ;
    if(n*b>=k){
        cout << -1 << endl ;
        return ;
    }
    if(n*a < k){
        cout << n << endl ;
        return ;
    }
    int diff = k- n*a  ;
    int start = 0 ,end = n ,mid = n/2 ;
    while(start < end -1){
        mid = start + (end-start)/2 ;
        int just_play = mid*a + (n-mid)*b ;
        if(just_play<k){
            start = mid ;
        }
        else{
            end = mid ;
        }
    }
    if(start == end-1){
        if((mid*a+(n-mid)*b)>=k)
            mid-- ;
    }
    cout << mid << endl ;
}
signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL) ;
	int q ;
	cin >> q ;
	while(q--){
	    solve() ;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define fi              first
#define se              second
#define ll             long long
#define pb              push_back
#define mp              make_pair
#define pll             pair<ll,ll>
#define vll              vector<int>
#define mll             map<ll,ll>
#define setbits(x)      __builtin_popcountll(x)
#define zrobits(x)      __builtin_ctzll(x)
#define md             1000000007
#define inf             1e18
#define ps(x,y)         fixed<<setprecision(y)<<x
void hi()
{
	#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	#endif
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
}
void bye()
{
	#ifndef ONLINE_JUDGE
	cerr << "Time elapsed: " << clock() / 1000 << " ms" << endl;
	#endif
}
ll k,n,a,b;
ll fun(ll u , ll l)
{
	while(u>=l)
	{
		ll m=(u+l)/2;
		ll v=m*a+(n-m)*b;
		if(v<k && (m+1)*a+(n-m-1)*b>=k) return m;
		else if(v>=k) u/=2;
		else if(v<k) l*=2;
	}
	return l;
}
void solve()
{
	cin>>k>>n>>a>>b;
	if(b*n<=k) cout<<"-1\n";
	else if((k-1)/a >=n) cout<<n<<"\n";
	else if(a+b*(n-1)>=k) cout<<"0\n";
	else cout<<fun(n,1)<<"\n";
}
int main()
{
	hi();
    ll test_cases; cin>>test_cases;
	for(ll t=1;t<=test_cases;t++)
	{
		solve();
	}
	bye();
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define fi              first
#define se              second
#define ll             long long
#define pb              push_back
#define mp              make_pair
#define pll             pair<ll,ll>
#define vll              vector<int>
#define mll             map<ll,ll>
#define setbits(x)      __builtin_popcountll(x)
#define zrobits(x)      __builtin_ctzll(x)
#define md             1000000007
#define inf             1e18
#define ps(x,y)         fixed<<setprecision(y)<<x
void hi()
{
	#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	#endif
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
}
void bye()
{
	#ifndef ONLINE_JUDGE
	cerr << "Time elapsed: " << clock() / 1000 << " ms" << endl;
	#endif
}
ll k,n,a,b;
ll fun(ll u , ll l)
{
	while(u>=l)
	{
		ll m=(u+l)/2;
		ll v=m*a+(n-m)*b;
		if(v<k && (m+1)*a+(n-m-1)*b>=k) return m;
		else if(v>=k) u/=2;
		else if(v<k) l*=2;
	}
	return (u+l)/2;
}
void solve()
{
	cin>>k>>n>>a>>b;
	if(k/b <= n) cout<<"-1\n";
	else if(k/a >n) cout<<n<<"\n";
	else if(a+b*(n-1)>=k) cout<<"0\n";
	else cout<<fun(n,1)<<"\n";
}
int main()
{
	hi();
    ll test_cases; cin>>test_cases;
	for(ll t=1;t<=test_cases;t++)
	{
		solve();
	}
	bye();
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define fi              first
#define se              second
#define ll             long long
#define pb              push_back
#define mp              make_pair
#define pll             pair<ll,ll>
#define vll              vector<int>
#define mll             map<ll,ll>
#define setbits(x)      __builtin_popcountll(x)
#define zrobits(x)      __builtin_ctzll(x)
#define md             1000000007
#define inf             1e18
#define ps(x,y)         fixed<<setprecision(y)<<x
void hi()
{
	#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	#endif
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
}
void bye()
{
	#ifndef ONLINE_JUDGE
	cerr << "Time elapsed: " << clock() / 1000 << " ms" << endl;
	#endif
}
ll k,n,a,b;
ll fun(ll u , ll l)
{
	while(u>=l)
	{
		ll m=(u+l)/2;
		ll v=m*a+(n-m)*b;
		if(v<k && (m+1)*a+(n-m-1)*b>=k) return m;
		else if(v>=k) u/=2;
		else if(v<k) l*=2;
	}
	return l;
}
void solve()
{
	cin>>k>>n>>a>>b;
	if(b*n>=k) cout<<"-1\n";
	else if(a*n<k) cout<<n<<"\n";
	else if(a+b*(n-1)>=k) cout<<"0\n";
	else cout<<fun(n,1)<<"\n";
}
int main()
{
	hi();
    ll test_cases; cin>>test_cases;
	for(ll t=1;t<=test_cases;t++)
	{
		solve();
	}
	bye();
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define fi              first
#define se              second
#define ll             long long
#define pb              push_back
#define mp              make_pair
#define pll             pair<ll,ll>
#define vll              vector<int>
#define mll             map<ll,ll>
#define setbits(x)      __builtin_popcountll(x)
#define zrobits(x)      __builtin_ctzll(x)
#define md             1000000007
#define inf             1e18
#define ps(x,y)         fixed<<setprecision(y)<<x
void hi()
{
	#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	#endif
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
}
void bye()
{
	#ifndef ONLINE_JUDGE
	cerr << "Time elapsed: " << clock() / 1000 << " ms" << endl;
	#endif
}
ll k,n,a,b;
ll fun(ll u , ll l)
{
	while(u>=l)
	{
		ll m=(u+l)/2;
		ll v=m*a+(n-m)*b;
		if(v<k && (m+1)*a+(n-m-1)*b>=k) return m;
		else if(v>=k) u/=2;
		else if(v<k) l*=2;
	}
	return l;
}
void solve()
{
	cin>>k>>n>>a>>b;
	if(b*n>=k) cout<<"-1\n";
	else if(a*n<k) cout<<n<<"\n";
	else if(a+b*(n-1)>=k) cout<<"0\n";
	else
	{
		ll m=fun(n,1);
		(m*a+(n-m)*b<k)?cout<<m<<"\n":cout<<m--<<"\n";
	}
}
int main()
{
	hi();
    ll test_cases; cin>>test_cases;
	for(ll t=1;t<=test_cases;t++)
	{
		solve();
	}
	bye();
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define fi              first
#define se              second
#define ll             long long
#define pb              push_back
#define mp              make_pair
#define pll             pair<ll,ll>
#define vll              vector<int>
#define mll             map<ll,ll>
#define setbits(x)      __builtin_popcountll(x)
#define zrobits(x)      __builtin_ctzll(x)
#define md             1000000007
#define inf             1e18
#define ps(x,y)         fixed<<setprecision(y)<<x
void hi()
{
	#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	#endif
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
}
void bye()
{
	#ifndef ONLINE_JUDGE
	cerr << "Time elapsed: " << clock() / 1000 << " ms" << endl;
	#endif
}
ll k,n,a,b;
ll fun(ll u , ll l)
{
	while(u>=l)
	{
		ll m=(u+l)/2;
		ll v=m*a+(n-m)*b;
		if(v<k && (m+1)*a+(n-m-1)*b>=k) return m;
		else if(v>=k) u/=2;
		else if(v<k) l*=2;
	}
	return l;
}
void solve()
{
	cin>>k>>n>>a>>b;
	if(b*n>=k) cout<<"-1\n";
	else if(a*n<k) cout<<n<<"\n";
	else if(a+b*(n-1)>=k) cout<<"0\n";
	else
	{
		ll m=fun(n,1);
		while(m*a+(n-m)*b>=k) m--;
		cout<<m<<"\n";
	}
}
int main()
{
	hi();
    ll test_cases; cin>>test_cases;
	for(ll t=1;t<=test_cases;t++)
	{
		solve();
	}
	bye();
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define fi              first
#define se              second
#define ll             long long
#define pb              push_back
#define mp              make_pair
#define pll             pair<ll,ll>
#define vll              vector<int>
#define mll             map<ll,ll>
#define setbits(x)      __builtin_popcountll(x)
#define zrobits(x)      __builtin_ctzll(x)
#define md             1000000007
#define inf             1e18
#define ps(x,y)         fixed<<setprecision(y)<<x
void hi()
{
	#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	#endif
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
}
void bye()
{
	#ifndef ONLINE_JUDGE
	cerr << "Time elapsed: " << clock() / 1000 << " ms" << endl;
	#endif
}
ll k,n,a,b;
ll fun(ll u , ll l)
{
	while(u>=l)
	{
		ll m=(u+l)/2;
		ll v=m*a+(n-m)*b;
		if(v<k && (m+1)*a+(n-m-1)*b>=k) return m;
		else if(v>=k) u/=2;
		else if(v<k) l*=2;
	}
	return l;
}
void solve()
{
	cin>>k>>n>>a>>b;
	if(b*n>=k) cout<<"-1\n";
	else if(a*n<k) cout<<n<<"\n";
	else if(a+b*(n-1)>=k) cout<<"0\n";
	else if(a*n==k) cout<<n-1<<"\n";
	else
	{
		ll m=fun(n,1);
		while((m+1)*a+(n-m-1)*b<k) m++;
		while(m*a+(n-m)*b>=k) m--;
		cout<<m<<"\n";
	}
}
int main()
{
	hi();
    ll test_cases; cin>>test_cases;
	for(ll t=1;t<=test_cases;t++)
	{
		solve();
	}
	bye();
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define fi              first
#define se              second
#define ll             long long
#define pb              push_back
#define mp              make_pair
#define pll             pair<ll,ll>
#define vll              vector<int>
#define mll             map<ll,ll>
#define setbits(x)      __builtin_popcountll(x)
#define zrobits(x)      __builtin_ctzll(x)
#define md             1000000007
#define inf             1e18
#define ps(x,y)         fixed<<setprecision(y)<<x
void hi()
{
	#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	#endif
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
}
void bye()
{
	#ifndef ONLINE_JUDGE
	cerr << "Time elapsed: " << clock() / 1000 << " ms" << endl;
	#endif
}
ll k,n,a,b;
ll fun(ll u , ll l)
{
	while(u>=l)
	{
		ll m=(u+l)/2;
		ll v=m*a+(n-m)*b;
		if(v<k && (m+1)*a+(n-m-1)*b>=k) return m;
		else if(v>=k) u/=2;
		else if(v<k) l*=2;
	}
	return l;
}
void solve()
{
	cin>>k>>n>>a>>b;
	if(k/b <= n) cout<<"-1\n";
	else if((k-1)/a >=n) cout<<n<<"\n";
	else if(a+b*(n-1)>=k) cout<<"0\n";
	else cout<<fun(n,1)<<"\n";
}
int main()
{
	hi();
    ll test_cases; cin>>test_cases;
	for(ll t=1;t<=test_cases;t++)
	{
		solve();
	}
	bye();
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define fi              first
#define se              second
#define ll             long long
#define pb              push_back
#define mp              make_pair
#define pll             pair<ll,ll>
#define vll              vector<int>
#define mll             map<ll,ll>
#define setbits(x)      __builtin_popcountll(x)
#define zrobits(x)      __builtin_ctzll(x)
#define md             1000000007
#define inf             1e18
#define ps(x,y)         fixed<<setprecision(y)<<x
void hi()
{
	#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	#endif
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
}
void bye()
{
	#ifndef ONLINE_JUDGE
	cerr << "Time elapsed: " << clock() / 1000 << " ms" << endl;
	#endif
}
ll k,n,a,b;
ll fun(ll u , ll l)
{
	while(u>=l)
	{
		ll m=(u+l)/2;
		ll v=m*a+(n-m)*b;
		if(v<k && (m+1)*a+(n-m-1)*b>=k) return m;
		else if(v>=k) u/=2;
		else if(v<k) l*=2;
	}
	return l;
}
void solve()
{
	cin>>k>>n>>a>>b;
	if(b*n>=k) cout<<"-1\n";
	else if(a*n<k) cout<<n<<"\n";
	else if(a+b*(n-1)>=k) cout<<"0\n";
	else
	{
		ll m=fun(n,1);
		while((m+1)*a+(n-m-1)*b<k) m++;
		while(m*a+(n-m)*b>=k) m--;
		cout<<m<<"\n";
	}
}
int main()
{
	hi();
    ll test_cases; cin>>test_cases;
	for(ll t=1;t<=test_cases;t++)
	{
		solve();
	}
	bye();
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define fi              first
#define se              second
#define ll             long long
#define pb              push_back
#define mp              make_pair
#define pll             pair<ll,ll>
#define vll              vector<int>
#define mll             map<ll,ll>
#define setbits(x)      __builtin_popcountll(x)
#define zrobits(x)      __builtin_ctzll(x)
#define md             1000000007
#define inf             1e18
#define ps(x,y)         fixed<<setprecision(y)<<x
void hi()
{
	#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	#endif
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
}
void bye()
{
	#ifndef ONLINE_JUDGE
	cerr << "Time elapsed: " << clock() / 1000 << " ms" << endl;
	#endif
}
ll k,n,a,b;
ll fun(ll u , ll l)
{
	while(u>=l)
	{
		ll m=(u+l)/2;
		ll v=m*a+(n-m)*b;
		if(v<k && (m+1)*a+(n-m-1)*b>=k) return m;
		else if(v>=k) u/=2;
		else if(v<k) l*=2;
	}
	return l;
}
void solve()
{
	cin>>k>>n>>a>>b;
	if(k/b <= n) cout<<"-1\n";
	else if(k/a >n) cout<<n<<"\n";
	else if(a+b*(n-1)>=k) cout<<"0\n";
	else cout<<fun(n,1)<<"\n";
}
int main()
{
	hi();
    ll test_cases; cin>>test_cases;
	for(ll t=1;t<=test_cases;t++)
	{
		solve();
	}
	bye();
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define fi              first
#define se              second
#define ll             long long
#define pb              push_back
#define mp              make_pair
#define pll             pair<ll,ll>
#define vll              vector<int>
#define mll             map<ll,ll>
#define setbits(x)      __builtin_popcountll(x)
#define zrobits(x)      __builtin_ctzll(x)
#define md             1000000007
#define inf             1e18
#define ps(x,y)         fixed<<setprecision(y)<<x
void hi()
{
	#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	#endif
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
}
void bye()
{
	#ifndef ONLINE_JUDGE
	cerr << "Time elapsed: " << clock() / 1000 << " ms" << endl;
	#endif
}

void solve()
{
	ll k,n,a,b;
	cin>>k>>n>>a>>b;
	if(b*n>=k) cout<<"-1\n";
	else if(a*n<k) cout<<n<<"\n";
	else
	{
		k-=a*n;
		k*=-1;k++;
		ll dif=a-b;
		ll t=k/(a-b);
		if(k%(a-b)!=0) t++;
		cout<<n-t<<"\n";
	}
}
int main()
{
	hi();
    ll test_cases; cin>>test_cases;
	for(ll t=1;t<=test_cases;t++)
	{
		solve();
	}
	bye();
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define fi              first
#define se              second
#define ll             long long
#define pb              push_back
#define mp              make_pair
#define pll             pair<ll,ll>
#define vll              vector<int>
#define mll             map<ll,ll>
#define setbits(x)      __builtin_popcountll(x)
#define zrobits(x)      __builtin_ctzll(x)
#define md             1000000007
#define inf             1e18
#define ps(x,y)         fixed<<setprecision(y)<<x
void hi()
{
	#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	#endif
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
}
void bye()
{
	#ifndef ONLINE_JUDGE
	cerr << "Time elapsed: " << clock() / 1000 << " ms" << endl;
	#endif
}
ll k,n,a,b;
ll fun(ll u , ll l)
{
	while(u>=l)
	{
		ll m=(u+l)/2;
		ll v=m*a+(n-m)*b;
		if(v<k && (m+1)*a+(n-m-1)*b>=k) return m;
		else if(v>=k) u/=2;
		else if(v<k) l*=2;
	}
	return l;
}
void solve()
{
	cin>>k>>n>>a>>b;
	if(b*n>=k) cout<<"-1\n";
	else if(a*n<k) cout<<n<<"\n";
	else if(a+b*(n-1)>=k) cout<<"0\n";
	else
	{
		ll m=fun(n,1);
		(m*a+(n-m)*b<k)?cout<<m<<"\n":cout<<--m<<"\n";
	}
}
int main()
{
	hi();
    ll test_cases; cin>>test_cases;
	for(ll t=1;t<=test_cases;t++)
	{
		solve();
	}
	bye();
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define fi              first
#define se              second
#define ll             long long
#define pb              push_back
#define mp              make_pair
#define pll             pair<ll,ll>
#define vll              vector<int>
#define mll             map<ll,ll>
#define setbits(x)      __builtin_popcountll(x)
#define zrobits(x)      __builtin_ctzll(x)
#define md             1000000007
#define inf             1e18
#define ps(x,y)         fixed<<setprecision(y)<<x
void hi()
{
	#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	#endif
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
}
void bye()
{
	#ifndef ONLINE_JUDGE
	cerr << "Time elapsed: " << clock() / 1000 << " ms" << endl;
	#endif
}
ll k,n,a,b;
ll fun(ll u , ll l)
{
	while(u>=l)
	{
		ll m=(u+l)/2;
		ll v=m*a+(n-m)*b;
		if(v<k && (m+1)*a+(n-m-1)*b>=k) return m;
		else if(v>=k) u/=2;
		else if(v<k) l*=2;
	}
	return (u+l)/2;
}
void solve()
{
	cin>>k>>n>>a>>b;
	if(b*n>=k) cout<<"-1\n";
	else if(a*n<k) cout<<n<<"\n";
	else if(a+b*(n-1)>=k) cout<<"0\n";
	else cout<<fun(n,1)<<"\n";
}
int main()
{
	hi();
    ll test_cases; cin>>test_cases;
	for(ll t=1;t<=test_cases;t++)
	{
		solve();
	}
	bye();
	return 0;
}


#include<bits/stdc++.h>
#include <cstdlib>
#include <cstdint>
#include <cstddef>
#include <ctime>
using namespace std;
#define fr(i,n)			for(int i=0;i<n;i++)
#define cc 				cout
#define ee				endl
#define ff              first
#define ss              second
#define int             long long
#define pb              push_back
#define mp              make_pair
#define pii             pair<int,int>
#define vi              vector<int>
#define mii             map<int,int>
#define pqb             priority_queue<int>
#define pqs             priority_queue<int,vi,greater<int> >
#define setbits(x)      __builtin_popcountll(x)
#define zrobits(x)      __builtin_ctzll(x)
#define mod             1000000007
#define inf             1e18
#define ps(x,y)         fixed<<setprecision(y)<<x
#define mk(arr,n,type)  type *arr=new type[n];
#define w(xxx)            int xxx; cin>>xxx; while(xxx--)
mt19937                 rng(chrono::steady_clock::now().time_since_epoch().count());
void tk(int* ip, int n) {
	for (int i = 0; i < n; i++) {
		cin >> ip[i];
	}
	return;
}
void tkv(vector<int>& ip, int n) {
	for (int i = 0; i < n; i++) {
		int vsrdgfve;
		cin >> vsrdgfve;
		ip.push_back(vsrdgfve);
	}
	return;
}

void c_p_c()
{
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
}
//////////////////////////////////////GLOBALS/////////////////////////////////////

/////////////////////////////////////Functions////////////////////////////////////
int ss(int n) {
	int sum = 0;
	while (n > 0) {
		int t = n % 10;
		n = n / 10;
		sum += t;
	}
	return sum;
}
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
int32_t main()
{
	c_p_c();
	w(t) {
		int n, k, a, b;
		cin >>  k >> n >> a >> b;
		if (n * b >= k) {cout << -1 << ee;}
		else {
			int diff = k - n * b;
			if (diff % (a - b) == 0) {
				cout << min(n, (diff / (a - b)) - 1) << ee;
			}
			else {
				cout << min(n, diff / (a - b)) << ee;
			}

		}
	}
/////////////////////////////////////////////////////
	return 0;
}


#include<bits/stdc++.h>
#include <cstdlib>
#include <cstdint>
#include <cstddef>
#include <ctime>
using namespace std;
#define fr(i,n)			for(int i=0;i<n;i++)
#define cc 				cout
#define ee				endl
#define ff              first
#define ss              second
// #define int             long long
#define pb              push_back
#define mp              make_pair
#define pii             pair<int,int>
#define vi              vector<int>
#define mii             map<int,int>
#define pqb             priority_queue<int>
#define pqs             priority_queue<int,vi,greater<int> >
#define setbits(x)      __builtin_popcountll(x)
#define zrobits(x)      __builtin_ctzll(x)
#define mod             1000000007
#define inf             1e18
#define ps(x,y)         fixed<<setprecision(y)<<x
#define mk(arr,n,type)  type *arr=new type[n];
#define w(xxx)            int xxx; cin>>xxx; while(xxx--)
mt19937                 rng(chrono::steady_clock::now().time_since_epoch().count());
void tk(int* ip, int n) {
	for (int i = 0; i < n; i++) {
		cin >> ip[i];
	}
	return;
}
void tkv(vector<int>& ip, int n) {
	for (int i = 0; i < n; i++) {
		int vsrdgfve;
		cin >> vsrdgfve;
		ip.push_back(vsrdgfve);
	}
	return;
}

void c_p_c()
{
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
}
//////////////////////////////////////GLOBALS/////////////////////////////////////

/////////////////////////////////////Functions////////////////////////////////////
int ss(int n) {
	int sum = 0;
	while (n > 0) {
		int t = n % 10;
		n = n / 10;
		sum += t;
	}
	return sum;
}
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
int32_t main()
{
	c_p_c();
	w(t) {
		int n, k, a, b;
		cin >>  k >> n >> a >> b;
		if (n * b >= k) {cout << -1 << ee;}
		else if (n > 1 && (n - 1)*b + a > k) {
			cout << 0 << ee;
		}
		else {
			int diff = k - n * b;
			int ans = 0;
			while (diff - (a - b) > 0) {
				ans++;
				diff -= a - b;
			}
			if (ans >= n) {
				cout << n << ee;
			}
			else {
				cout << ans << ee;
			}

		}
	}
/////////////////////////////////////////////////////
	return 0;
}


#include<bits/stdc++.h>
#include <cstdlib>
#include <cstdint>
#include <cstddef>
#include <ctime>
using namespace std;
#define fr(i,n)			for(int i=0;i<n;i++)
#define cc 				cout
#define ee				endl
#define ff              first
#define ss              second
// #define int             long long
#define pb              push_back
#define mp              make_pair
#define pii             pair<int,int>
#define vi              vector<int>
#define mii             map<int,int>
#define pqb             priority_queue<int>
#define pqs             priority_queue<int,vi,greater<int> >
#define setbits(x)      __builtin_popcountll(x)
#define zrobits(x)      __builtin_ctzll(x)
#define mod             1000000007
#define inf             1e18
#define ps(x,y)         fixed<<setprecision(y)<<x
#define mk(arr,n,type)  type *arr=new type[n];
#define w(xxx)            int xxx; cin>>xxx; while(xxx--)
mt19937                 rng(chrono::steady_clock::now().time_since_epoch().count());
void tk(int* ip, int n) {
	for (int i = 0; i < n; i++) {
		cin >> ip[i];
	}
	return;
}
void tkv(vector<int>& ip, int n) {
	for (int i = 0; i < n; i++) {
		int vsrdgfve;
		cin >> vsrdgfve;
		ip.push_back(vsrdgfve);
	}
	return;
}

void c_p_c()
{
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
}
//////////////////////////////////////GLOBALS/////////////////////////////////////

/////////////////////////////////////Functions////////////////////////////////////
int ss(int n) {
	int sum = 0;
	while (n > 0) {
		int t = n % 10;
		n = n / 10;
		sum += t;
	}
	return sum;
}
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
int32_t main()
{
	c_p_c();
	w(t) {
		int n, k, a, b;
		cin >>  k >> n >> a >> b;
		if (n * b >= k) {cout << -1 << ee;}
		else {
			int ans = 0;
			while (k - a >= (n - (ans + 1))*b && k - a > b && ans < n) {
				k -= a;
				if (k == 1) {break;}
				// cout << k << ee;
				ans++;
			}
			cout << ans << ee;
		}
	}
/////////////////////////////////////////////////////
	return 0;
}


#include<bits/stdc++.h>
#include <cstdlib>
#include <cstdint>
#include <cstddef>
#include <ctime>
using namespace std;
#define fr(i,n)			for(int i=0;i<n;i++)
#define cc 				cout
#define ee				endl
#define ff              first
#define ss              second
// #define int             long long
#define pb              push_back
#define mp              make_pair
#define pii             pair<int,int>
#define vi              vector<int>
#define mii             map<int,int>
#define pqb             priority_queue<int>
#define pqs             priority_queue<int,vi,greater<int> >
#define setbits(x)      __builtin_popcountll(x)
#define zrobits(x)      __builtin_ctzll(x)
#define mod             1000000007
#define inf             1e18
#define ps(x,y)         fixed<<setprecision(y)<<x
#define mk(arr,n,type)  type *arr=new type[n];
#define w(xxx)            int xxx; cin>>xxx; while(xxx--)
mt19937                 rng(chrono::steady_clock::now().time_since_epoch().count());
void tk(int* ip, int n) {
	for (int i = 0; i < n; i++) {
		cin >> ip[i];
	}
	return;
}
void tkv(vector<int>& ip, int n) {
	for (int i = 0; i < n; i++) {
		int vsrdgfve;
		cin >> vsrdgfve;
		ip.push_back(vsrdgfve);
	}
	return;
}

void c_p_c()
{
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
}
//////////////////////////////////////GLOBALS/////////////////////////////////////

/////////////////////////////////////Functions////////////////////////////////////
int ss(int n) {
	int sum = 0;
	while (n > 0) {
		int t = n % 10;
		n = n / 10;
		sum += t;
	}
	return sum;
}
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
int32_t main()
{
	c_p_c();
	w(t) {
		int n, k, a, b;
		cin >>  k >> n >> a >> b;
		if (n * b >= k) {cout << -1 << ee;}
		else if (n > 1 && (n - 1)*b + a > k) {
			cout << 0 << ee;
		}
		else {
			int ans = 0;
			while (k - a > ( n - (ans + 1) )*b && ans < n) {
				k -= a; ans++;
				if (k == 1) {break;}
				// cout << k << ee;

			}
			cout << ans << ee;
		}
	}
/////////////////////////////////////////////////////
	return 0;
}


#include<bits/stdc++.h>
#include <cstdlib>
#include <cstdint>
#include <cstddef>
#include <ctime>
using namespace std;
#define fr(i,n)			for(int i=0;i<n;i++)
#define cc 				cout
#define ee				endl
#define ff              first
#define ss              second
#define int             long long
#define pb              push_back
#define mp              make_pair
#define pii             pair<int,int>
#define vi              vector<int>
#define mii             map<int,int>
#define pqb             priority_queue<int>
#define pqs             priority_queue<int,vi,greater<int> >
#define setbits(x)      __builtin_popcountll(x)
#define zrobits(x)      __builtin_ctzll(x)
#define mod             1000000007
#define inf             1e18
#define ps(x,y)         fixed<<setprecision(y)<<x
#define mk(arr,n,type)  type *arr=new type[n];
#define w(xxx)            int xxx; cin>>xxx; while(xxx--)
mt19937                 rng(chrono::steady_clock::now().time_since_epoch().count());
void tk(int* ip, int n) {
	for (int i = 0; i < n; i++) {
		cin >> ip[i];
	}
	return;
}
void tkv(vector<int>& ip, int n) {
	for (int i = 0; i < n; i++) {
		int vsrdgfve;
		cin >> vsrdgfve;
		ip.push_back(vsrdgfve);
	}
	return;
}

void c_p_c()
{
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
}
//////////////////////////////////////GLOBALS/////////////////////////////////////

/////////////////////////////////////Functions////////////////////////////////////
int ss(int n) {
	int sum = 0;
	while (n > 0) {
		int t = n % 10;
		n = n / 10;
		sum += t;
	}
	return sum;
}
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
int32_t main()
{
	c_p_c();
	w(t) {
		int n, k, a, b;
		cin >>  k >> n >> a >> b;
		if (n * b >= k) {cout << -1 << ee;}
		else {
			int diff = k - n * b;
			int ans = 0;
			while (diff - (a - b) > 0) {
				ans++;
				diff -= a - b;
			}
			if (ans >= n) {
				cout << n << ee;
			}
			else {
				cout << ans << ee;
			}

		}
	}
/////////////////////////////////////////////////////
	return 0;
}


#include<bits/stdc++.h>
#include <cstdlib>
#include <cstdint>
#include <cstddef>
#include <ctime>
using namespace std;
#define fr(i,n)			for(int i=0;i<n;i++)
#define cc 				cout
#define ee				endl
#define ff              first
#define ss              second
// #define int             long long
#define pb              push_back
#define mp              make_pair
#define pii             pair<int,int>
#define vi              vector<int>
#define mii             map<int,int>
#define pqb             priority_queue<int>
#define pqs             priority_queue<int,vi,greater<int> >
#define setbits(x)      __builtin_popcountll(x)
#define zrobits(x)      __builtin_ctzll(x)
#define mod             1000000007
#define inf             1e18
#define ps(x,y)         fixed<<setprecision(y)<<x
#define mk(arr,n,type)  type *arr=new type[n];
#define w(xxx)            int xxx; cin>>xxx; while(xxx--)
mt19937                 rng(chrono::steady_clock::now().time_since_epoch().count());
void tk(int* ip, int n) {
	for (int i = 0; i < n; i++) {
		cin >> ip[i];
	}
	return;
}
void tkv(vector<int>& ip, int n) {
	for (int i = 0; i < n; i++) {
		int vsrdgfve;
		cin >> vsrdgfve;
		ip.push_back(vsrdgfve);
	}
	return;
}

void c_p_c()
{
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
}
//////////////////////////////////////GLOBALS/////////////////////////////////////

/////////////////////////////////////Functions////////////////////////////////////
int ss(int n) {
	int sum = 0;
	while (n > 0) {
		int t = n % 10;
		n = n / 10;
		sum += t;
	}
	return sum;
}
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
int32_t main()
{
	c_p_c();
	w(t) {
		int n, k, a, b;
		cin >>  k >> n >> a >> b;
		if (n * b >= k) {cout << -1 << ee;}
		else {
			int diff = k - n * b;
			int ans = 0;
			while (diff - (a - b) > 0) {
				ans++;
				diff -= a - b;
			}
			if (ans >= n) {
				cout << n << ee;
			}
			else {
				cout << ans << ee;
			}

		}
	}
/////////////////////////////////////////////////////
	return 0;
}


#include<bits/stdc++.h>
#include <cstdlib>
#include <cstdint>
#include <cstddef>
#include <ctime>
using namespace std;
#define fr(i,n)			for(int i=0;i<n;i++)
#define cc 				cout
#define ee				endl
#define ff              first
#define ss              second
// #define int             long long
#define pb              push_back
#define mp              make_pair
#define pii             pair<int,int>
#define vi              vector<int>
#define mii             map<int,int>
#define pqb             priority_queue<int>
#define pqs             priority_queue<int,vi,greater<int> >
#define setbits(x)      __builtin_popcountll(x)
#define zrobits(x)      __builtin_ctzll(x)
#define mod             1000000007
#define inf             1e18
#define ps(x,y)         fixed<<setprecision(y)<<x
#define mk(arr,n,type)  type *arr=new type[n];
#define w(xxx)            int xxx; cin>>xxx; while(xxx--)
mt19937                 rng(chrono::steady_clock::now().time_since_epoch().count());
void tk(int* ip, int n) {
	for (int i = 0; i < n; i++) {
		cin >> ip[i];
	}
	return;
}
void tkv(vector<int>& ip, int n) {
	for (int i = 0; i < n; i++) {
		int vsrdgfve;
		cin >> vsrdgfve;
		ip.push_back(vsrdgfve);
	}
	return;
}

void c_p_c()
{
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
}
//////////////////////////////////////GLOBALS/////////////////////////////////////

/////////////////////////////////////Functions////////////////////////////////////
int ss(int n) {
	int sum = 0;
	while (n > 0) {
		int t = n % 10;
		n = n / 10;
		sum += t;
	}
	return sum;
}
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
int32_t main()
{
	c_p_c();
	w(t) {
		int n, k, a, b;
		cin >>  k >> n >> a >> b;
		if (n * b >= k) {cout << -1 << ee;}
		else if (n > 1 && (n - 1)*b + a > k) {
			cout << 0 << ee;
		}
		else {
			int ans = 0;
			while (k - a > b && ans < n) {
				k -= a; ans++;
				if (k == 1) {break;}
				// cout << k << ee;

			}
			cout << ans << ee;
		}
	}
/////////////////////////////////////////////////////
	return 0;
}


#include <bits/stdc++.h>
typedef int OwO; typedef double UwU;
#define int long long
#define double long double

using namespace std;

OwO main(){
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  int q;
  cin >> q;
  while(q--){
    int k,n,a,b;
    cin >> k >> n >> a >> b;
    int turns = k/a;
    if(k%n==0 && k/n==a)
      turns--;
    if(turns>=n){
      cout << n << endl;
      continue;
    }
    bool did = false;
    double quant = (double)(0.0+-b*n+k)/(0.0+a-b);
    if(quant <= 0.000000001)
      cout << -1 << endl;
    else
      cout << (int)floor(quant-0.0000001) << endl;
  }

  return 0;
}


#include <bits/stdc++.h>
typedef int OwO; typedef double UwU;
#define int long long
#define double long double

using namespace std;

OwO main(){
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  int q;
  cin >> q;
  while(q--){
    int k,n,a,b;
    cin >> k >> n >> a >> b;
    int turns = k/a;
    if(k%n==0 && k/n==a)
      turns--;
    if(turns>=n){
      cout << n << endl;
      continue;
    }
    bool did = false;
    double quant = (double)(0.0+-b*n+k)/(0.0+a-b);
    if(quant <= 0.000000001)
      cout << -1 << endl;
    else
      cout << floor(quant-0.0000000000001) << endl;
  }

  return 0;
}


#include <bits/stdc++.h>
typedef int OwO; typedef double UwU;
#define int long long
#define double long double

using namespace std;

OwO main(){
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  int q;
  cin >> q;
  while(q--){
    int k,n,a,b;
    cin >> k >> n >> a >> b;
    k -= n*b;
    if(k<0)
      cout << -1 << endl;
    else{
      a-=b;
      cout << min((k-1)/a, n) << endl;
    }
  }

  return 0;
}