//**KEEP IT SHORT AND SIMPLE**

#include <bits/stdc++.h>
#define IO ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define FILES freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
#define REP(i,a,b) for (int i = a; i < b; i++)
#define NL '\n'
#define F first
#define S second
using namespace std;
typedef long long ll;
typedef bool bl;
typedef vector < vector<long long> > vvl;
typedef vector <long long> vll;
typedef pair <long long, long long> pll;
typedef set <long long> sll;
typedef map <long long, long long> mll;
typedef map <long long, bool> mlb;
typedef priority_queue<long long, vector<long long>, greater<long long> > minPq;
double pi = 2 * acos(0.0);
int OO = 0x3f3f3f3f;
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
long long lcm(long long a, long long b) { return a * (b / gcd(a, b)); }
ll Summation(ll x)
{
    return (x * (x + 1)) / 2;
}
int main()
{
    IO
    ll a, b, n, l, t, m, Start, End, Mid, r, ans, sum, tempm, befsum;
    cin >> a >> b >> n;
    while(n--)
    {
        cin >> l >> t >> m;
        Start = l;
        End = 1e7;
        ans = -1;
        befsum =  (l - 1) * a + Summation(l - 2) * b;
//        cout << befsum << NL;
        while(Start <= End)
        {
            Mid = (Start + End) >> 1;
            r = a + (Mid - 1) * b;
            sum = Mid * a + Summation(Mid - 1) * b;
            sum -= befsum;
//            cout << Mid << ' ' << sum << ' ' << r << NL;
            if(r <= t && sum <= t * m)
            {
                ans = Mid;
                Start = Mid + 1;
            }
            else
                End = Mid - 1;
        }
        cout << ans << NL;
    }
    return 0;

}


//**KEEP IT SHORT AND SIMPLE**

#include <bits/stdc++.h>
#define IO ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define FILES freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
#define REP(i,a,b) for (int i = a; i < b; i++)
#define NL '\n'
#define F first
#define S second
using namespace std;
typedef long long ll;
typedef bool bl;
typedef vector < vector<long long> > vvl;
typedef vector <long long> vll;
typedef pair <long long, long long> pll;
typedef set <long long> sll;
typedef map <long long, long long> mll;
typedef map <long long, bool> mlb;
typedef priority_queue<long long, vector<long long>, greater<long long> > minPq;
double pi = 2 * acos(0.0);
int OO = 0x3f3f3f3f;
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
long long lcm(long long a, long long b) { return a * (b / gcd(a, b)); }
ll Summation(ll x)
{
    return (x * (x + 1)) / 2;
}
int main()
{
    IO
    ll a, b, n, l, t, m, Start, End, Mid, r, ans, sum, tempm;
    cin >> a >> b >> n;
    while(n--)
    {
        cin >> l >> t >> m;
        Start = l;
        End = 1e7;
        ans = -1;
        while(Start <= End)
        {
            Mid = (Start + End) >> 1;
            r = a + b * (Mid - 1);
            sum = Mid * a + Summation(Mid - 1) - (l > 1 ? (l - 1) * a + b * Summation(l - 2): 0);
//            cout << Mid << ' ' << r << ' ' << sum << NL;
            tempm = min(m, Mid - l + 1);
            if(r > t || sum > t * tempm)
                End = Mid - 1;
            else
            {
                Start = Mid + 1;
                ans = Mid;
            }
        }
        cout << ans << NL;
    }
    return 0;

}


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

int main ()
{
    ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0);

    long long a , b , n ;
    cin >> a >> b >>  n ;

    while(n--)
    {
        long long l , t , m ;
        cin >> l >> t >> m ;

        long long low  =  l , high =  1e12 , mid ;
        long long ans = -1 ;
        long double  last , first , sum;
        first =  a + (l - 1) * b ;

        while(low <= high)
        {
            mid =(low+high) /2 ;
            last = (a + (mid-1) * b) ;
            sum = ((first+last)/ 2) * (mid - l +1 ) ;

            //cout << low << " "<< high << " " << mid << " " << sum << " "<< first << " " << last<< endl;

            if (sum > t * m)
            {
                high = mid - 1;
            }
            else if ( last > t )
            {
                high = mid -1 ;
            }
            else
            {
                if(ans < mid)
                    ans = mid ;
                low = mid +1 ;
            }
        }

        cout << ans << endl;

    }

    return 0 ;
}


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

int main ()
{
    ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0);

    long long a , b , n ;
    cin >> a >> b >>  n ;

    while(n--)
    {
        long long l , t , m ;
        cin >> l >> t >> m ;

        long long low  =  l , high =  1e12 , mid ;
        long long ans = -1 ;
        long long  last , first , sum;
        first =  a + (l - 1) * b ;

        while(low <= high)
        {
            mid =(low+high) /2 ;
            last = (a + (mid-1) * b) ;
            sum = ((first+last)/ 2) * (mid - l +1 ) ;

            //cout << low << " "<< high << " " << mid << " " << sum << " "<< first << " " << last<< endl;

            if (sum > t * m)
            {
                high = mid - 1;
            }
            else if ( last > t )
            {
                high = mid -1 ;
            }
            else if(sum == t* m && last == t)
            {
                ans = mid ;
                break;
            }
            else
            {
                if(ans < mid)
                    ans = mid ;
                low = mid +1 ;
            }
        }

        cout << ans << endl;

    }

    return 0 ;
}


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

int main ()
{
    ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0);

    long long a , b , n ;
    cin >> a >> b >>  n ;

    while(n--)
    {
        long long l , t , m ;
        cin >> l >> t >> m ;

        long long low  =  l , high =  1e12 , mid ;
        long long ans = -1 ;
        long long  last , first , sum;
        first =  a + (l - 1) * b ;

        while(low <= high)
        {
            mid =(low+high) /2 ;
            last = (a + (mid-1) * b) ;
            sum = ((first+last)/ 2) * (mid - l +1 ) ;

            //cout << low << " "<< high << " " << mid << " " << sum << " "<< first << " " << last<< endl;

            if (sum > t * m)
            {
                high = mid - 1;
            }
            else if ( last > t )
            {
                high = mid -1 ;
            }
            else
            {
                if(ans < mid)
                    ans = mid ;
                low = mid +1 ;
            }
        }

        cout << ans << endl;

    }

    return 0 ;
}


#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <math.h>
#include <algorithm>
using namespace std;
#define ls 2*i
#define rs 2*i+1
#define up(i,x,y) for(i=x;i<=y;i++)
#define down(i,x,y) for(i=x;i>=y;i--)
#define mem(a,x) memset(a,x,sizeof(a))
#define w(a) while(a)
#define LL long long
const double pi = acos(-1.0);
#define Len 63
#define mod 19999997
const int INF = 0x3f3f3f3f;

LL a,b,n;
LL l,t,m;

LL cal(LL x)
{
    return a+(x-1)*b;
}

LL getsum(LL r)
{
    return (cal(r)+cal(l))*(r-l+1)/2;
}

int main()
{
    LL i,j,k,maxn;
    w(~scanf("%I64d%I64d%I64d",&a,&b,&n))
    {
        w(n--)
        {
            scanf("%I64d%I64d%I64d",&l,&t,&m);
            if(cal(l)>t)
            {
                printf("-1\n");
                continue;
            }
            LL ll  = l,lr = (t-a)/b+1,mid;
            w(ll<=lr)
            {
                LL mid = (ll+lr)/2;
                if(getsum(mid)<=t*m) ll = mid+1;
                else lr = mid-1;
            }
            printf("%d\n",ll-1);
        }
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll a,b,n;
ll l,t,m;
ll cal(ll x){
    return a+(x-1)*b;
}
ll getSum(ll r){
    return (cal(r)+cal(l))*(r-l+1)/2; //等差数列求和
}
int  main(){
    cin>>a>>b>>n;
    while(n--){
        cin>>l>>t>>m;
        if(cal(l)>t) cout<<"-1"<<endl;
        else{
            ll lb = l,rb = (t-a)/b+1, mid;
            while(lb<=rb){
                mid = (lb+rb)>>1;
                if(getSum(mid)<=t*m) lb = mid+1;
                else                 rb = mid-1;
            }
            cout<<lb-1<<endl;
        }
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef int int__;
#define int long long
#define pb push_back
#define pi pair<int,int>
#define vpi vector<pi>
#define vi vector<int>
#define vvi vector<vi>
#define fi first
#define se second
#define forn(i, n)for(int i=0;i<int__(n);i++)
#define sz(a)int__(a.size())
template <class A, class B> ostream& operator << (ostream& out, const pair<A, B> &a){return out << "(" << a.first << ", " << a.second << ")";}
template<class A>ostream& operator<<(ostream& out,const vector<A>&v){out<<"[";forn(i,sz(v)){if(i)out<<", ";out<<v[i];}return out<<"]";}
#define fast ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
int mod = 1e9 + 7;
int Mod(int x){x%=mod;if(x<0)x+=mod;return x;}
int Pow(int x,int n){if(n==0)return 1;int t=Pow(x,n/2);t=Mod(t*t);if(n&1)t=Mod(t*x);return t;}
#define watch(x) cout << #x << " : " << x << endl;
int gcd(int a,int b){if(b==0)return a;return gcd(b,a%b);}
#define all(x) x.begin(), x.end()
#define db double

int a,b;

int check(int l,int r,int t,int m) {
	int mx = a + (r-1)*b;
	if(t < mx) return 0;

	int sum = ((r-l+1)*(a + (l-1)*b + a + (r-1)*b))/2;

	return sum <= m*t;
}

int lb(int l,int r,int t,int m) {
	int li=l;
	int ans = -1;
	while(li <= r) {
		int mid=(li+r)/2;

		// now check
		if(check(l,mid,t,m)) {
			ans = mid; li=mid+1;
		} else r=mid-1;
	}
	return ans;
}

int__ main() {
	fast;

	int q; cin >> a >> b >> q;
	int maax=3e7 + 10;

	for(int i=0;i<q;i++) {
		int l,t,m; cin >> l >> t >> m;

		int idx = lb(l,maax,t,m);

		cout << idx << endl;
	}
}


#include <bits/stdc++.h>
using namespace std;
typedef int int__;
#define int long long
#define pb push_back
#define pi pair<int,int>
#define vpi vector<pi>
#define vi vector<int>
#define vvi vector<vi>
#define fi first
#define se second
#define forn(i, n)for(int i=0;i<int__(n);i++)
#define sz(a)int__(a.size())
template <class A, class B> ostream& operator << (ostream& out, const pair<A, B> &a){return out << "(" << a.first << ", " << a.second << ")";}
template<class A>ostream& operator<<(ostream& out,const vector<A>&v){out<<"[";forn(i,sz(v)){if(i)out<<", ";out<<v[i];}return out<<"]";}
#define fast ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
int mod = 1e9 + 7;
int Mod(int x){x%=mod;if(x<0)x+=mod;return x;}
int Pow(int x,int n){if(n==0)return 1;int t=Pow(x,n/2);t=Mod(t*t);if(n&1)t=Mod(t*x);return t;}
#define watch(x) cout << #x << " : " << x << endl;
int gcd(int a,int b){if(b==0)return a;return gcd(b,a%b);}
#define all(x) x.begin(), x.end()
#define db double

int a,b;

int check(int l,int r,int t,int m) {
	int mx = a + (r-1)*b;
	if(t < mx) return 0;

	int sum = ((r-l+1)*(a + (l-1)*b + a + (r-1)*b))/2;

	return sum <= m*t;
}

int lb(int l,int r,int t,int m) {
	int ans = -1;
	while(l <= r) {
		int mid=(l+r)/2;

		// now check
		if(check(l,mid,t,m)) {
			ans = mid; l=mid+1;
		} else r=mid-1;
	}
	return ans;
}

int__ main() {
	fast;

	int q; cin >> a >> b >> q;
	const int mx=2e6 + 10;

	for(int i=0;i<q;i++) {
		int l,t,m; cin >> l >> t >> m;

		int idx = lb(l,mx,t,m);

		cout << idx << endl;
	}
}


#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstring>
#include <chrono>
#include <complex>
#define endl "\n"
#define ll long long int
#define vi vector<int>
#define vll vector<ll>
#define vvi vector < vi >
#define pii pair<int,int>
#define pll pair<long long, long long>
#define mod 1000000007
#define inf 1000000000000000001;
#define all(c) c.begin(),c.end()
#define mp(x,y) make_pair(x,y)
#define mem(a,val) memset(a,val,sizeof(a))
#define eb emplace_back
  #define pb push_back
#define f first
#define s second
  #define MAXN 100005
using namespace std;
ll a,b,n,l,t,m;
ll compute(ll r){
	return a*r+b*(r*(r-1)/2);
}
ll calc(ll r){
	return a+b*(r-1);
}
int main()
{
	std::ios::sync_with_stdio(false);

	cin>>a>>b>>n;
	for(int i=0; i<n; i++) {
		ll ans=-1;

		cin>>l>>t>>m;
		ll max=t*m;
		ll z=l;
		ll r=1e9;

		while(l<=r) {
			ll mid=(l+r)/2;
			if(compute(mid)-compute(z-1)<=max&&calc(mid)<=t) {
				ans=mid;
				l=mid+1;
			}
			else{
				r=mid-1;
			}
		}

		cout<<ans<<endl;
	}

	return 0;
}


#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstring>
#include <chrono>
#include <complex>
#define endl "\n"
#define ll long long int
#define vi vector<int>
#define vll vector<ll>
#define vvi vector < vi >
#define pii pair<int,int>
#define pll pair<long long, long long>
#define mod 1000000007
#define inf 1000000000000000001;
#define all(c) c.begin(),c.end()
#define mp(x,y) make_pair(x,y)
#define mem(a,val) memset(a,val,sizeof(a))
#define eb emplace_back
  #define pb push_back
#define f first
#define s second
  #define MAXN 100005
using namespace std;
ll a,b,n,l,t,m;
ll compute(int r){
	return a*r+b*(r*(r-1)/2);
}
ll calc(int r){
	return a+b*(r-1);
}
int main()
{
	std::ios::sync_with_stdio(false);

	cin>>a>>b>>n;
	for(int i=0; i<n; i++) {
		int ans=-1;

		cin>>l>>t>>m;
		ll max=t*m;
		int z=l;
		int r=1e9;

		while(l<=r) {
			int mid=(l+r)/2;
			if(compute(mid)-compute(z-1)<=max&&calc(mid)<=t) {
				ans=mid;
				l=mid+1;
			}
			else{
				r=mid-1;
			}
		}

		cout<<ans<<endl;
	}

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int OO = (int) 1e9;
const double eps = 1e-10;

#define db(x) 			cout<<"> "<<#x<<" = "<<(x)<<'\n'
#define db2(x,y) 		cout<<"> "<<#x<<" = "<<(x)<<", "<<#y<<" = "<<(y)<<'\n'
#define db3(x,y,z) 		cout<<"> "<<#x<<" = "<<(x)<<", "<<#y<<" = "<<(y)<<", "<<#z<<" = "<<(z)<<'\n'
#define db4(x,y,z,u) 	cout<<"> "<<#x<<" = "<<(x)<<", "<<#y<<" = "<<(y)<<", "<<#z<<" = "<<(z)<<", "<<#u<<" = "<<u<<'\n'
#define sz(v) 			int(v.size())
#define trav(a,x) 		for(auto&a:x)
#define rep(i,v)		for(int i=0;i<sz(v);++it)
#define lp(i,n) 		for(int i=0;i<int(n);++i)
#define lpi(i,j,n) 		for(int i=j;i<int(n);++i)
#define lpd(i,j,n) 		for(int i=j;i>=int(n);--i)
#define ndl 			cout<<'\n'
#define all(v) 			v.begin(),v.end()
#define rall(v) 		v.rbegin(),v.rend()
#define present(a,x) 	(a.find(x)!=a.end())
#define cpresent(a,x) 	(find(all(a),x)!=a.end())
#define clr(v, d) 		memset(v,d,sizeof(v))
#define pb 				push_back
#define mp 				make_pair
#define fs 				first
#define sc 				second

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vector<int> > vvi;
typedef map<int, int> mii;
typedef set<int> si;

int n;
ll A,B,l,t,m;

ll sum(ll l, ll r){
	return (r-l+1)*A+(B*(r-1)*r-(l-2)*(l-1))/2;
}

int main() {
	ios::sync_with_stdio(false);
	cout.precision(10);
	cin>>A>>B>>n;
	lp(i,n){
		cin>>l>>t>>m;
		if (A+(l-1)*B>t)
			cout<<"-1\n";
		else{
			ll r=l,mid;
			while(sum(l,r)<=m*t)
				r*=10;
			while(l<r){
				mid=(l+r+1)/2;
				if (max(A+(mid-1)*B,(sum(l,mid)+(sum(l,mid)%m!=0)*m)/m)<=t)
					l=mid;
				else
					r=mid-1;
			}
			cout<<r<<'\n';
		}
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int OO = (int) 1e9;
const double eps = 1e-10;

#define db(x) 			cout<<"> "<<#x<<" = "<<(x)<<'\n'
#define db2(x,y) 		cout<<"> "<<#x<<" = "<<(x)<<", "<<#y<<" = "<<(y)<<'\n'
#define db3(x,y,z) 		cout<<"> "<<#x<<" = "<<(x)<<", "<<#y<<" = "<<(y)<<", "<<#z<<" = "<<(z)<<'\n'
#define db4(x,y,z,u) 	cout<<"> "<<#x<<" = "<<(x)<<", "<<#y<<" = "<<(y)<<", "<<#z<<" = "<<(z)<<", "<<#u<<" = "<<u<<'\n'
#define sz(v) 			int(v.size())
#define trav(a,x) 		for(auto&a:x)
#define rep(i,v)		for(int i=0;i<sz(v);++i)
#define lp(i,n) 		for(int i=0;i<int(n);++i)
#define lpi(i,j,n) 		for(int i=j;i<int(n);++i)
#define lpd(i,j,n) 		for(int i=j;i>=int(n);--i)
#define ndl 			cout<<'\n'
#define all(v) 			v.begin(),v.end()
#define rall(v) 		v.rbegin(),v.rend()
#define present(a,x) 	(a.find(x)!=a.end())
#define cpresent(a,x) 	(find(all(a),x)!=a.end())
#define clr(v, d) 		memset(v,d,sizeof(v))
#define pb 				push_back
#define mp 				make_pair
#define fs 				first
#define sc 				second

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vector<int> > vvi;
typedef map<int, int> mii;
typedef set<int> si;

int n;
ll A,B,l,t,m;

ll sum(ll l, ll r){
	return (r-l+1)*A+B*((r-1)*r-(l-2)*(l-1))/2;
}

int main() {
	ios::sync_with_stdio(false);
	cout.precision(10);
	cin>>A>>B>>n;
	lp(i,n){
		cin>>l>>t>>m;
		if (A+(l-1)*B>t)
			cout<<"-1\n";
		else{
			ll r=l,mid,L=l;
			while(sum(l,r)<=m*t)
				r*=10;
			while(l<r){
				mid=(l+r+1)/2;
				if (max(A+(mid-1)*B,(sum(L,mid)+m-1)/m)<=t)
					l=mid;
				else
					r=mid-1;
			}
			cout<<r<<'\n';
		}
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int OO = (int) 1e9;
const double eps = 1e-10;

#define db(x) 			cout<<"> "<<#x<<" = "<<(x)<<'\n'
#define db2(x,y) 		cout<<"> "<<#x<<" = "<<(x)<<", "<<#y<<" = "<<(y)<<'\n'
#define db3(x,y,z) 		cout<<"> "<<#x<<" = "<<(x)<<", "<<#y<<" = "<<(y)<<", "<<#z<<" = "<<(z)<<'\n'
#define db4(x,y,z,u) 	cout<<"> "<<#x<<" = "<<(x)<<", "<<#y<<" = "<<(y)<<", "<<#z<<" = "<<(z)<<", "<<#u<<" = "<<u<<'\n'
#define sz(v) 			int(v.size())
#define trav(a,x) 		for(auto&a:x)
#define rep(i,v)		for(int i=0;i<sz(v);++it)
#define lp(i,n) 		for(int i=0;i<int(n);++i)
#define lpi(i,j,n) 		for(int i=j;i<int(n);++i)
#define lpd(i,j,n) 		for(int i=j;i>=int(n);--i)
#define ndl 			cout<<'\n'
#define all(v) 			v.begin(),v.end()
#define rall(v) 		v.rbegin(),v.rend()
#define present(a,x) 	(a.find(x)!=a.end())
#define cpresent(a,x) 	(find(all(a),x)!=a.end())
#define clr(v, d) 		memset(v,d,sizeof(v))
#define pb 				push_back
#define mp 				make_pair
#define fs 				first
#define sc 				second

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vector<int> > vvi;
typedef map<int, int> mii;
typedef set<int> si;

int n;
ll A,B,l,t,m;

ll sum(ll l, ll r){
	return (r-l+1)*A+B*((r-1)*r-(l-2)*(l-1))/2;
}

int main() {
	ios::sync_with_stdio(false);
	cout.precision(10);
	cin>>A>>B>>n;
	lp(i,n){
		cin>>l>>t>>m;
		if (A+(l-1)*B>t)
			cout<<"-1\n";
		else{
			ll r=l,mid;
			while(sum(l,r)<=m*t)
				r*=10;
			while(l<r){
				mid=(l+r+1)/2;
				if (max(A+(mid-1)*B,(sum(l,mid)+(sum(l,mid)%m!=0)*m)/m)<=t)
					l=mid;
				else
					r=mid-1;
			}
			cout<<r<<'\n';
		}
	}
	return 0;
}


#include <bits/stdc++.h>
#ifndef ONLINE_JUDGE
#include <unistd.h>
#endif
using namespace std;

// BEGIN template
#define   ff            first
#define   ss            second
#define   mp            make_pair
#define   mt            make_tuple
#define   pb            push_back
#define   eb            emplace_back
#define   pq            priority_queue

#ifdef ONLINE_JUDGE
  #define fastio        std::ios::sync_with_stdio(false)
  #define dbg(X)
  #define _             _
#else
  #define fastio
  #define dbg(X)        cerr << ">>> (" << #X << ") = (" << X << ")\n"
  #define _             << " _ " <<
#endif
#define   sci(X)        scanf("%d",&(X))
#define   sci2(X,Y)     scanf("%d%d",&(X),&(Y))
#define   sci3(X,Y,Z)   scanf("%d%d%d",&(X),&(Y),&(Z))
#define   scl(X)        scanf("%lld",&(X))
#define   scs(X)        scanf("%s",X)
#define   flush         fflush(stdout)
#define   lg(X)         (63 - __builtin_clzll(X))
#define   LG(X)         (lg(X)+((1<<lg(X)) < (X)))
#define    rp(i,L,R)    for (int i = L, __R = R; i <= __R; i++)
#define   rpd(i,R,L)    for (int i = R, __L = L; __L <= i; i--)

#define DP1(type,X)\
  static type dp[X];\
  static bool mark[X];\
  auto& ans = dp[i];\
  if (mark[i]) return ans;\
  mark[i] = true;

#define DP2(type,X,Y)\
  static type dp[X][Y];\
  static bool mark[X][Y];\
  auto& ans = dp[i][j];\
  if (mark[i][j]) return ans;\
  mark[i][j] = true;

#define DP3(type,X,Y,Z)\
  static type dp[X][Y][Z];\
  static bool mark[X][Y][Z];\
  auto& ans = dp[i][j][k];\
  if (mark[i][j][k]) return ans;\
  mark[i][j][k] = true;

#define GET_DP(_1,_2,_3,_4,NAME,...) NAME
#define DP(...) GET_DP(__VA_ARGS__,DP3,DP2,DP1)(__VA_ARGS__)

typedef   long long     ll;
typedef   pair<int,int> ii;
typedef   vector<int>   vi;
typedef   vector<bool>  vb;

#define   oo            0x3f3f3f3f3f3f3f3fLL
#define   MAXLOGN       25
#define   MOD           int(1e9+7)
#define   MAXN          int(20)
// END template

ll A,B;
ll s(ll i) {
  return A+(i-1)*B;
}
ll pa_(ll n) {
  return (n*(n-1))>>1;
}
ll pa(ll l, ll r) {
  return pa_(r)-pa_(l-1);
}
ll sum(ll l, ll r) {
  return (r-l+1)*A+B*pa(l,r);
}
ll howmany(ll l, ll r, ll m) {
  if (r-l+1 <= m) return s(r);
  return (sum(l,r)+m-1)/m;
}
ll find(ll l, ll t, ll m, ll L, ll R) {
  if (R < L) return -1;
  ll mid = (L+R)>>1;
  if (t < howmany(l,mid,m)) return find(l,t,m,L,mid-1);
  ll tmp = find(l,t,m,mid+1,R);
  return tmp < 0 ? mid : tmp;
}

int main() {
  ll n;
  cin >> A >> B >> n;
  rp(i,1,n) {
    ll l,t,m;
    cin >> l >> t >> m;
    cout << find(l,t,m,l,1e8) << endl;
  }
  return 0;
}


#include <bits/stdc++.h>
#ifndef ONLINE_JUDGE
#include <unistd.h>
#endif
using namespace std;

// BEGIN template
#define   ff            first
#define   ss            second
#define   mp            make_pair
#define   mt            make_tuple
#define   pb            push_back
#define   eb            emplace_back
#define   pq            priority_queue

#ifdef ONLINE_JUDGE
  #define fastio        std::ios::sync_with_stdio(false)
  #define dbg(X)
  #define _             _
#else
  #define fastio
  #define dbg(X)        cerr << ">>> (" << #X << ") = (" << X << ")\n"
  #define _             << " _ " <<
#endif
#define   sci(X)        scanf("%d",&(X))
#define   sci2(X,Y)     scanf("%d%d",&(X),&(Y))
#define   sci3(X,Y,Z)   scanf("%d%d%d",&(X),&(Y),&(Z))
#define   scl(X)        scanf("%lld",&(X))
#define   scs(X)        scanf("%s",X)
#define   flush         fflush(stdout)
#define   lg(X)         (63 - __builtin_clzll(X))
#define   LG(X)         (lg(X)+((1<<lg(X)) < (X)))
#define    rp(i,L,R)    for (int i = L, __R = R; i <= __R; i++)
#define   rpd(i,R,L)    for (int i = R, __L = L; __L <= i; i--)

#define DP1(type,X)\
  static type dp[X];\
  static bool mark[X];\
  auto& ans = dp[i];\
  if (mark[i]) return ans;\
  mark[i] = true;

#define DP2(type,X,Y)\
  static type dp[X][Y];\
  static bool mark[X][Y];\
  auto& ans = dp[i][j];\
  if (mark[i][j]) return ans;\
  mark[i][j] = true;

#define DP3(type,X,Y,Z)\
  static type dp[X][Y][Z];\
  static bool mark[X][Y][Z];\
  auto& ans = dp[i][j][k];\
  if (mark[i][j][k]) return ans;\
  mark[i][j][k] = true;

#define GET_DP(_1,_2,_3,_4,NAME,...) NAME
#define DP(...) GET_DP(__VA_ARGS__,DP3,DP2,DP1)(__VA_ARGS__)

typedef   long long     ll;
typedef   pair<int,int> ii;
typedef   vector<int>   vi;
typedef   vector<bool>  vb;

#define   oo            0x3f3f3f3f3f3f3f3fLL
#define   MAXLOGN       25
#define   MOD           int(1e9+7)
#define   MAXN          int(20)
// END template

ll A,B;
ll s(ll i) {
  return A+(i-1)*B;
}
ll pa_(ll n) {
  return (n*(n-1))>>1;
}
ll pa(ll l, ll r) {
  return pa_(r)-pa_(l-1);
}
ll sum(ll l, ll r) {
  return (r-l+1)*A+B*pa(l,r);
}
ll howmany(ll l, ll r, ll m) {
  if (r-l+1 <= m) return s(r);
  return (sum(l,r)+m-1)/m;
}
ll find(ll l, ll t, ll m, ll L, ll R) {
  if (R < L) return -1;
  ll mid = (L+R)>>1;
  if (t < howmany(l,mid,m)) return find(l,t,m,L,mid-1);
  ll tmp = find(l,t,m,mid+1,R);
  return tmp < 0 ? mid : tmp;
}

int main() {
  ll n;
  cin >> A >> B >> n;
  rp(i,1,n) {
    ll l,t,m;
    cin >> l >> t >> m;
    cout << find(l,t,m,l,100) << endl;
  }
  return 0;
}


#include <bits/stdc++.h>
#ifndef ONLINE_JUDGE
#include <unistd.h>
#endif
using namespace std;

// BEGIN template
#define   ff            first
#define   ss            second
#define   mp            make_pair
#define   mt            make_tuple
#define   pb            push_back
#define   eb            emplace_back
#define   pq            priority_queue

#ifdef ONLINE_JUDGE
  #define fastio        std::ios::sync_with_stdio(false)
  #define dbg(X)
  #define _             _
#else
  #define fastio
  #define dbg(X)        cerr << ">>> (" << #X << ") = (" << X << ")\n"
  #define _             << " _ " <<
#endif
#define   sci(X)        scanf("%d",&(X))
#define   sci2(X,Y)     scanf("%d%d",&(X),&(Y))
#define   sci3(X,Y,Z)   scanf("%d%d%d",&(X),&(Y),&(Z))
#define   scl(X)        scanf("%lld",&(X))
#define   scs(X)        scanf("%s",X)
#define   flush         fflush(stdout)
#define   lg(X)         (63 - __builtin_clzll(X))
#define   LG(X)         (lg(X)+((1<<lg(X)) < (X)))
#define    rp(i,L,R)    for (int i = L, __R = R; i <= __R; i++)
#define   rpd(i,R,L)    for (int i = R, __L = L; __L <= i; i--)

#define DP1(type,X)\
  static type dp[X];\
  static bool mark[X];\
  auto& ans = dp[i];\
  if (mark[i]) return ans;\
  mark[i] = true;

#define DP2(type,X,Y)\
  static type dp[X][Y];\
  static bool mark[X][Y];\
  auto& ans = dp[i][j];\
  if (mark[i][j]) return ans;\
  mark[i][j] = true;

#define DP3(type,X,Y,Z)\
  static type dp[X][Y][Z];\
  static bool mark[X][Y][Z];\
  auto& ans = dp[i][j][k];\
  if (mark[i][j][k]) return ans;\
  mark[i][j][k] = true;

#define GET_DP(_1,_2,_3,_4,NAME,...) NAME
#define DP(...) GET_DP(__VA_ARGS__,DP3,DP2,DP1)(__VA_ARGS__)

typedef   long long     ll;
typedef   pair<int,int> ii;
typedef   vector<int>   vi;
typedef   vector<bool>  vb;

#define   oo            0x3f3f3f3f3f3f3f3fLL
#define   MAXLOGN       25
#define   MOD           int(1e9+7)
#define   MAXN          int(20)
// END template

ll A,B;
ll s(ll i) {
  return A+(i-1)*B;
}
ll pa_(ll n) {
  return (n*(n-1))>>1;
}
ll pa(ll l, ll r) {
  return pa_(r)-pa_(l-1);
}
ll sum(ll l, ll r) {
  return (r-l+1)*A+B*pa(l,r);
}
ll howmany(ll l, ll r, ll m) {
  if (r-l+1 <= m) return s(r);
  return (sum(l,r)+m-1)/m;
}
ll find(ll l, ll t, ll m, ll L, ll R) {
  if (R < L) return -1;
  ll mid = (L+R)>>1;
  if (t < howmany(l,mid,m)) return find(l,t,m,L,mid-1);
  ll tmp = find(l,t,m,mid+1,R);
  return tmp < 0 ? mid : tmp;
}

int main() {
  ll n;
  cin >> A >> B >> n;
  rp(i,1,n) {
    ll l,t,m;
    cin >> l >> t >> m;
    cout << find(l,t,m,l,1e10) << endl;
  }
  return 0;
}


#include <bits/stdc++.h>
#ifndef ONLINE_JUDGE
#include <unistd.h>
#endif
using namespace std;

// BEGIN template
#define   ff            first
#define   ss            second
#define   mp            make_pair
#define   mt            make_tuple
#define   pb            push_back
#define   eb            emplace_back
#define   pq            priority_queue

#ifdef ONLINE_JUDGE
  #define fastio        std::ios::sync_with_stdio(false)
  #define dbg(X)
  #define _             _
#else
  #define fastio
  #define dbg(X)        cerr << ">>> (" << #X << ") = (" << X << ")\n"
  #define _             << " _ " <<
#endif
#define   sci(X)        scanf("%d",&(X))
#define   sci2(X,Y)     scanf("%d%d",&(X),&(Y))
#define   sci3(X,Y,Z)   scanf("%d%d%d",&(X),&(Y),&(Z))
#define   scl(X)        scanf("%lld",&(X))
#define   scs(X)        scanf("%s",X)
#define   flush         fflush(stdout)
#define   lg(X)         (63 - __builtin_clzll(X))
#define   LG(X)         (lg(X)+((1<<lg(X)) < (X)))
#define    rp(i,L,R)    for (int i = L, __R = R; i <= __R; i++)
#define   rpd(i,R,L)    for (int i = R, __L = L; __L <= i; i--)

#define DP1(type,X)\
  static type dp[X];\
  static bool mark[X];\
  auto& ans = dp[i];\
  if (mark[i]) return ans;\
  mark[i] = true;

#define DP2(type,X,Y)\
  static type dp[X][Y];\
  static bool mark[X][Y];\
  auto& ans = dp[i][j];\
  if (mark[i][j]) return ans;\
  mark[i][j] = true;

#define DP3(type,X,Y,Z)\
  static type dp[X][Y][Z];\
  static bool mark[X][Y][Z];\
  auto& ans = dp[i][j][k];\
  if (mark[i][j][k]) return ans;\
  mark[i][j][k] = true;

#define GET_DP(_1,_2,_3,_4,NAME,...) NAME
#define DP(...) GET_DP(__VA_ARGS__,DP3,DP2,DP1)(__VA_ARGS__)

typedef   long long     ll;
typedef   pair<int,int> ii;
typedef   vector<int>   vi;
typedef   vector<bool>  vb;

#define   oo            0x3f3f3f3f3f3f3f3fLL
#define   MAXLOGN       25
#define   MOD           int(1e9+7)
#define   MAXN          int(20)
// END template

ll A,B;
ll s(ll i) {
  return A+(i-1)*B;
}
ll pa_(ll n) {
  return (n*(n-1))>>1;
}
ll pa(ll l, ll r) {
  return pa_(r)-pa_(l-1);
}
ll sum(ll l, ll r) {
  return (r-l+1)*A+B*pa(l,r);
}
ll howmany(ll l, ll r, ll m) {
  if (r-l+1 <= m) return s(r);
  return (sum(l,r)+m-1)/m;
}
ll find(ll l, ll t, ll m, ll L, ll R) {
  if (R < L) return -1;
  ll mid = (L+R)>>1;
  if (t < howmany(l,mid,m)) return find(l,t,m,L,mid-1);
  ll tmp = find(l,t,m,mid+1,R);
  return tmp < 0 ? mid : tmp;
}

int main() {
  ll n;
  cin >> A >> B >> n;
  rp(i,1,n) {
    ll l,t,m;
    cin >> l >> t >> m;
    cout << find(l,t,m,l,10) << endl;
  }
  return 0;
}


#include <bits/stdc++.h>
#ifndef ONLINE_JUDGE
#include <unistd.h>
#endif
using namespace std;

// BEGIN template
#define   ff            first
#define   ss            second
#define   mp            make_pair
#define   mt            make_tuple
#define   pb            push_back
#define   eb            emplace_back
#define   pq            priority_queue

#ifdef ONLINE_JUDGE
  #define fastio        std::ios::sync_with_stdio(false)
  #define dbg(X)
  #define _             _
#else
  #define fastio
  #define dbg(X)        cerr << ">>> (" << #X << ") = (" << X << ")\n"
  #define _             << " _ " <<
#endif
#define   sci(X)        scanf("%d",&(X))
#define   sci2(X,Y)     scanf("%d%d",&(X),&(Y))
#define   sci3(X,Y,Z)   scanf("%d%d%d",&(X),&(Y),&(Z))
#define   scl(X)        scanf("%lld",&(X))
#define   scs(X)        scanf("%s",X)
#define   flush         fflush(stdout)
#define   lg(X)         (63 - __builtin_clzll(X))
#define   LG(X)         (lg(X)+((1<<lg(X)) < (X)))
#define    rp(i,L,R)    for (int i = L, __R = R; i <= __R; i++)
#define   rpd(i,R,L)    for (int i = R, __L = L; __L <= i; i--)

#define DP1(type,X)\
  static type dp[X];\
  static bool mark[X];\
  auto& ans = dp[i];\
  if (mark[i]) return ans;\
  mark[i] = true;

#define DP2(type,X,Y)\
  static type dp[X][Y];\
  static bool mark[X][Y];\
  auto& ans = dp[i][j];\
  if (mark[i][j]) return ans;\
  mark[i][j] = true;

#define DP3(type,X,Y,Z)\
  static type dp[X][Y][Z];\
  static bool mark[X][Y][Z];\
  auto& ans = dp[i][j][k];\
  if (mark[i][j][k]) return ans;\
  mark[i][j][k] = true;

#define GET_DP(_1,_2,_3,_4,NAME,...) NAME
#define DP(...) GET_DP(__VA_ARGS__,DP3,DP2,DP1)(__VA_ARGS__)

typedef   long long     ll;
typedef   pair<int,int> ii;
typedef   vector<int>   vi;
typedef   vector<bool>  vb;

#define   oo            0x3f3f3f3f3f3f3f3fLL
#define   MAXLOGN       25
#define   MOD           int(1e9+7)
#define   MAXN          int(20)
// END template

ll A,B;
ll s(ll i) {
  return A+(i-1)*B;
}
ll pa_(ll n) {
  return (n*(n-1))>>1;
}
ll pa(ll l, ll r) {
  return pa_(r)-pa_(l-1);
}
ll sum(ll l, ll r) {
  return (r-l+1)*A+B*pa(l,r);
}
ll howmany(ll l, ll r, ll m) {
  if (r-l+1 <= m) return s(r);
  return (sum(l,r)+m-1)/m;
}
ll find(ll l, ll t, ll m, ll L, ll R) {
  if (R < L) return -1;
  ll mid = (L+R)>>1;
  if (t < howmany(l,mid,m)) return find(l,t,m,L,mid-1);
  ll tmp = find(l,t,m,mid+1,R);
  return tmp < 0 ? mid : tmp;
}

int main() {
  ll n;
  cin >> A >> B >> n;
  rp(i,1,n) {
    ll l,t,m;
    cin >> l >> t >> m;
    cout << find(l,t,m,l,1e6) << endl;
  }
  return 0;
}


#include <bits/stdc++.h>
#ifndef ONLINE_JUDGE
#include <unistd.h>
#endif
using namespace std;

// BEGIN template
#define   ff            first
#define   ss            second
#define   mp            make_pair
#define   mt            make_tuple
#define   pb            push_back
#define   eb            emplace_back
#define   pq            priority_queue

#ifdef ONLINE_JUDGE
  #define fastio        std::ios::sync_with_stdio(false)
  #define dbg(X)
  #define _             _
#else
  #define fastio
  #define dbg(X)        cerr << ">>> (" << #X << ") = (" << X << ")\n"
  #define _             << " _ " <<
#endif
#define   sci(X)        scanf("%d",&(X))
#define   sci2(X,Y)     scanf("%d%d",&(X),&(Y))
#define   sci3(X,Y,Z)   scanf("%d%d%d",&(X),&(Y),&(Z))
#define   scl(X)        scanf("%lld",&(X))
#define   scs(X)        scanf("%s",X)
#define   flush         fflush(stdout)
#define   lg(X)         (63 - __builtin_clzll(X))
#define   LG(X)         (lg(X)+((1<<lg(X)) < (X)))
#define    rp(i,L,R)    for (int i = L, __R = R; i <= __R; i++)
#define   rpd(i,R,L)    for (int i = R, __L = L; __L <= i; i--)

#define DP1(type,X)\
  static type dp[X];\
  static bool mark[X];\
  auto& ans = dp[i];\
  if (mark[i]) return ans;\
  mark[i] = true;

#define DP2(type,X,Y)\
  static type dp[X][Y];\
  static bool mark[X][Y];\
  auto& ans = dp[i][j];\
  if (mark[i][j]) return ans;\
  mark[i][j] = true;

#define DP3(type,X,Y,Z)\
  static type dp[X][Y][Z];\
  static bool mark[X][Y][Z];\
  auto& ans = dp[i][j][k];\
  if (mark[i][j][k]) return ans;\
  mark[i][j][k] = true;

#define GET_DP(_1,_2,_3,_4,NAME,...) NAME
#define DP(...) GET_DP(__VA_ARGS__,DP3,DP2,DP1)(__VA_ARGS__)

typedef   long long     ll;
typedef   pair<int,int> ii;
typedef   vector<int>   vi;
typedef   vector<bool>  vb;

#define   oo            0x3f3f3f3f3f3f3f3fLL
#define   MAXLOGN       25
#define   MOD           int(1e9+7)
#define   MAXN          int(20)
// END template

ll A,B;
ll s(ll i) {
  return A+(i-1)*B;
}
ll pa_(ll n) {
  return (n*(n-1))>>1;
}
ll pa(ll l, ll r) {
  return pa_(r)-pa_(l-1);
}
ll sum(ll l, ll r) {
  return (r-l+1)*A+B*pa(l,r);
}
ll howmany(ll l, ll r, ll m) {
  if (r-l+1 <= m) return s(r);
  return (sum(l,r)+m-1)/m;
}
ll find(ll l, ll t, ll m, ll L, ll R) {
  if (R < L) return -1;
  ll mid = (L+R)>>1;
  if (t < howmany(l,mid,m)) return find(l,t,m,L,mid-1);
  ll tmp = find(l,t,m,mid+1,R);
  return tmp < 0 ? mid : tmp;
}

int main() {
  ll n;
  cin >> A >> B >> n;
  rp(i,1,n) {
    ll l,t,m;
    cin >> l >> t >> m;
    cout << find(l,t,m,l,1e3) << endl;
  }
  return 0;
}


#include <bits/stdc++.h>
#ifndef ONLINE_JUDGE
#include <unistd.h>
#endif
using namespace std;

// BEGIN template
#define   ff            first
#define   ss            second
#define   mp            make_pair
#define   mt            make_tuple
#define   pb            push_back
#define   eb            emplace_back
#define   pq            priority_queue

#ifdef ONLINE_JUDGE
  #define fastio        std::ios::sync_with_stdio(false)
  #define dbg(X)
  #define _             _
#else
  #define fastio
  #define dbg(X)        cerr << ">>> (" << #X << ") = (" << X << ")\n"
  #define _             << " _ " <<
#endif
#define   sci(X)        scanf("%d",&(X))
#define   sci2(X,Y)     scanf("%d%d",&(X),&(Y))
#define   sci3(X,Y,Z)   scanf("%d%d%d",&(X),&(Y),&(Z))
#define   scl(X)        scanf("%lld",&(X))
#define   scs(X)        scanf("%s",X)
#define   flush         fflush(stdout)
#define   lg(X)         (63 - __builtin_clzll(X))
#define   LG(X)         (lg(X)+((1<<lg(X)) < (X)))
#define    rp(i,L,R)    for (int i = L, __R = R; i <= __R; i++)
#define   rpd(i,R,L)    for (int i = R, __L = L; __L <= i; i--)

#define DP1(type,X)\
  static type dp[X];\
  static bool mark[X];\
  auto& ans = dp[i];\
  if (mark[i]) return ans;\
  mark[i] = true;

#define DP2(type,X,Y)\
  static type dp[X][Y];\
  static bool mark[X][Y];\
  auto& ans = dp[i][j];\
  if (mark[i][j]) return ans;\
  mark[i][j] = true;

#define DP3(type,X,Y,Z)\
  static type dp[X][Y][Z];\
  static bool mark[X][Y][Z];\
  auto& ans = dp[i][j][k];\
  if (mark[i][j][k]) return ans;\
  mark[i][j][k] = true;

#define GET_DP(_1,_2,_3,_4,NAME,...) NAME
#define DP(...) GET_DP(__VA_ARGS__,DP3,DP2,DP1)(__VA_ARGS__)

typedef   long long     ll;
typedef   pair<int,int> ii;
typedef   vector<int>   vi;
typedef   vector<bool>  vb;

#define   oo            0x3f3f3f3f3f3f3f3fLL
#define   MAXLOGN       25
#define   MOD           int(1e9+7)
#define   MAXN          int(20)
// END template

ll A,B;
ll s(ll i) {
  return A+(i-1)*B;
}
ll pa_(ll n) {
  return (n*(n-1))>>1;
}
ll pa(ll l, ll r) {
  return pa_(r)-pa_(l-1);
}
ll sum(ll l, ll r) {
  return (r-l+1)*A+B*pa(l,r);
}
bool ok(ll l, ll r, ll t, ll m) {
  return s(r) <= t && sum(l,r) <= m*t;
}
ll find(ll l, ll t, ll m, ll L, ll R) {
  if (R < L) return -1;
  ll r = (L+R)>>1;
  if (!ok(l,r,t,m)) return find(l,t,m,L,r-1);
  ll tmp = find(l,t,m,r+1,R);
  return tmp < 0 ? r : tmp;
}

int main() {
  ll n;
  cin >> A >> B >> n;
  rp(i,1,n) {
    ll l,t,m;
    cin >> l >> t >> m;
    cout << find(l,t,m,l,1e12) << endl;
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int A,B,n,l,t,m;
ll tmp,A1,An;
bool check(int r)
{
	tmp=A+(r-1)*B;
	if(tmp>t) return false;
	A1=A+(l-1)*B;
	An=A+(r-1)*B;
	if((A1+An)*(r-l+1)/2>(ll)m*t) return false;
	return true;
}
int work(int l,int r)
{
	int mid;
	while(r-l>=5)
	{
		mid=(l+r)/2;
		if(check(mid)==true) l=mid;
		else r=mid-1;
	}
	for(;l<=r;l++)
	  if(check(l)==true&&check(l+1)==false)
	    return l;
	return -1;
}
int main()
{
	scanf("%d%d%d",&A,&B,&n);
	int i;
	for(i=1;i<=n;i++)
	{
		scanf("%d%d%d",&l,&t,&m);
		printf("%d\n",work(l,(t-A)/B+2));
	}
	return 0;
}


#include<complex>
#include <vector>
#include <algorithm>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <bitset>
#include <string>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <limits>
#include <climits>
#include <time.h>
#include <cstdio>
#include <numeric>
#define LL long long
#define LD long double
#define rei(x) scanf("%d",&x)
#define reii(x,y) scanf("%d%d",&x,&y)
#define reiii(x,y,z) scanf("%d%d%d",&x,&y,&z)

#define rel(x) scanf("%I64d",&x);
#include<complex>
#include <vector>
#include <algorithm>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <bitset>
#include <string>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <limits>
#include <climits>
#include <time.h>
#include <cstdio>
#include <numeric>
#define LL long long
#define LD long double
#define rei(x) scanf("%d",&x)
#define reii(x,y) scanf("%d%d",&x,&y)
#define reiii(x,y,z) scanf("%d%d%d",&x,&y,&z)

#define rel(x) scanf("%I64d",&x);

#define ref(x) scanf("%lf",&x)
#define rec(x) scanf("%c",&x)
#define res(x) scanf("%s",x)
#define reli(x) scanf("%[^\n]s", x)
#define fi first
#define se second
#define pb push_back
#define f(i, a, b) for(int i = a; i <= b; i++)
#define ms(Arr) memset(Arr,0,sizeof(Arr));
#define CLR(arr,val)  memset(arr,val,sizeof(arr))
#define bit(a , b) (((a)>>(b))&1)
#define fin freopen("in.txt","r",stdin)
#define fout freopen("out.txt","w",stdout)
#define fout1 freopen("out1.txt","w",stdout)
#define ftest freopen("in.txt","w",stdout)
#define lson (id << 1)
#define rson (id << 1 | 1)
#define ls (i << 1)
#define rs (i << 1 | 1)
using namespace std;
const int INF = 0x3f3f3f3f;
const LL mod = 1000000007;
const double e = 2.718281828459;
typedef pair<int, int> Pii;
typedef pair<LL, LL> Pll;

LL gcd(LL a, LL b)
{
	if (!b)return a;
	return gcd(b, a%b);
}

LL gcd1(int a, int b)
{
	if (!b)return a;
	return gcd(b, a%b);
}

LL pow_mod(LL a, LL b, LL c)
{
	LL ans = 1;
	a = a % c;
	while (b > 0)
	{
		if (b % 2 == 1)
			ans = (ans * a) % c;
		b = b / 2;
		a = (a * a) % c;
	}
	return ans;
}

int pow_int(int a, int b)
{
	int ans = 1;
	while (b > 0)
	{
		if (b % 2 == 1)
			ans = ans * a;
		b = b / 2;
		a = a * a;
	}
	return ans;
}

LL pow_llong(LL a, LL b)
{
	LL ans = 1;
	while (b > 0)
	{
		if (b % 2 == 1)
			ans = ans * a;
		b = b / 2;
		a = a * a;
	}
	return ans;
}

int Scan()
{
	int res = 0, flag = 0;
	char ch;
	if ((ch = getchar()) == '-')
	{
		flag = 1;
	}
	else if (ch >= '0' && ch <= '9')
	{
		res = ch - '0';
	}
	while ((ch = getchar()) >= '0' && ch <= '9')
	{
		res = res * 10 + (ch - '0');
	}
	return flag ? -res : res;
}

void Out(int a)
{
	if (a < 0)
	{
		putchar('-');
		a = -a;
	}
	if (a >= 10)
	{
		Out(a / 10);
	}
	putchar(a % 10 + '0');
}

LL jc_mod(LL a, LL b, LL mod)
{
	LL ans = 1;
	b = max(b, a - b);
	for (LL i = a; i > b; i--)
	{
		ans *= i;
		ans %= mod;
	}
	return ans;
}

double lg(double a)
{
	return (log(a) / log(10.0));
}

int lowbit(int x)
{
	return x & -x;
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//long long：-9223372036854775808~9223372036854775807
//int：-2147483648~2147483647
//double：-1.7*10^-308~1.7*10^308，15~16位有效数字
//long double：-1.2*10^-4932~1.2*10^4932，18~19位有效数字
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

inline int read()
{
	int f = 1, x = 0; char ch = getchar();
	while (ch<'0' || ch>'9') { if (ch == '-')f = -1; ch = getchar(); }
	while (ch >= '0'&&ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
	return x*f;
}

/*******************************************************
自定义结构体排序
struct Struct_Name
{
int variable;
bool operator <(const Struct_Name &other)const   //升序排序
{
return variable > other.variable;
}
};
*******************************************************/

//离散化///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//离散化///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//离散化///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#define MAXNN 100010
void lisan(int *x, int n) {
	//首先c++调用一个数组直接打这个数组的类型就好了。
	int data[MAXNN];
	f(i, 1, n)data[i] = x[i];
	sort(data + 1, data + 1 + n);//排序是肯定要的。
	int o = unique(data + 1, data + 1 + n) - data - 1;//给data数组[1..n]去重，黑科技
	f(i, 1, n)x[i] = lower_bound(data + 1, data + 1 + o, x[i]) - data;//找到x[i]在data中的位置再赋值为这个位置，如果不-data就是返回这个值。
}
////////////////////////////////////////////////////end/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////end/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////end/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

///////////////////////////////////三分框架 左闭右开////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////三分框架 左闭右开////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////三分框架 左闭右开////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
LL calc(int M)
{
	return 1;
}
int sanfen(int L, int R)
{
	int M, RM;
	while (L + 1 < R)
	{
		M = (L + R) / 2;
		RM = (M + R) / 2;
		if (calc(M) < calc(RM)) //计算最小值,若为最大值则>
			R = RM;
		else
			L = M;
	}
	return L;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

int CaculateWeekDay(int y, int m, int d)
{
	if (m == 1 || m == 2) {
		m += 12;
		y--;
	}
	int iWeek = (d + 2 * m + 3 * (m + 1) / 5 + y + y / 4 - y / 100 + y / 400) % 7;
	return iWeek;
}

////////////////////////////////////////////////////e nd/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////end/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////end/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

LL a, b, n;
LL get(LL i)
{
	return a + (i - 1) * b;
}

bool judge(LL l, LL r,LL m,LL t)
{
	if (get(r) <= t && (get(l) + get(r)) * (r - l + 1) / 2 <= m * t)
		return true;
	else
		return false;
}

int main()
{

	rel(a);
	rel(b);
	rel(n);
	for (LL i = 1; i <= n; i++)
	{
		LL l, t, m;
		rel(l);
		rel(t);
		rel(m);
		LL L = l, R = (t - a) / b + 10;
		if (!judge(l, l, m, t))
		{
			printf("-1\n");
			continue;
		}
		while (R > L)
		{
			LL mid = (L + R + 1) >> 1;
			if (judge(l, mid, m, t))
				L = mid;
			else
				R = mid - 1;
		}
		printf("%I64d\n", L);
	}
	return 0;
}


#include<bits/stdc++.h>
#define DE cout << "------" << endl
#define mems(a, b) memset(a, (b), sizeof a)
#define maxn 110000
using namespace std;
typedef long long ll;
typedef long double ld;

ll A,B,n;
ll l,m,t;
ll suan(ll x){
    ll aa = 0;
    ll s = (x-l+1)/(2*m);
    aa += s*(A+A+B*(x+x-m-m-1));//cout << s << endl;
    x -= s*2*m;//cout << x << aa<< endl;
    if(x-l+1<=m) aa += (x-1)*B+A;
    else{
        aa += A+(x-1)*B;
        ll t = A+(x-m-1)*B-(m-1)*B;
        if(t>0) aa += t;//cout << "??"<<endl;
    }
   // printf(" %lld %lld\n",x,aa);
    return aa;
}
int main(){
    cin >> A >> B >> n;
    for(int i=1;i<=n;i++){
        cin >> l >> t >> m;
        ll w = A+B*(l-1);
        if(w>t){
            printf("-1\n");
            continue;
        }
        ll L = l;
        ll R = 2e6;
        while(L<R){
           // printf("%lld %lld\n",L,R);
            ll x = (L+R)/2+(L+R)%2;
            if(suan(x)<=t){
                L = x;
            }
            else R=x-1;
        }
        printf("%lld\n",L);
    }
    return 0;
}


#include <cstdio>
using namespace std;
long long a, b, n;
int cal(long long pos) {
	return a + (pos - 1) * b;
}
int main(int argc, char const *argv[]) {
	scanf("%lld %lld %lld", &a, &b, &n);
	for (int i = 0; i < n; ++i) {
		long long l, t, m; scanf("%lld %lld %lld", &l, &t, &m);
		if (cal(l) > t) {
			printf("-1\n");
		} else {
			long long vl = l, vr = (t - a) / b + 1;
			while (vl <= vr) {
				long long vm = (vl + vr) / 2;
				if ((cal(l) + cal(vm)) * (vm - l + 1) / 2 <= t * m) {
					vl = vm + 1;
				} else {
					vr = vm - 1;
				}
			}
			printf("%lld\n", vl - 1);
		}
	}
	return 0;
}


#include<bits/stdc++.h>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int N=1e5;
#define ll long long
int main(){
    ll A,B,n,l,t,m;
    cin>>A>>B>>n;
    for(int i=1;i<=n;i++){
        cin>>l>>t>>m;
        ll  Sl = A+(l-1)*B,Sum = 0,r = -1;
        ll al = l,ar = 1e9;
        while(al<=ar){
            ll mid = (al+ar)/2,ss = (Sl+(mid-1)*B+A)*(mid-l+1)/2;
            if(ss<=m*t&&(mid-1)*B+A<=t)r = mid,al=mid+1;
            else ar = mid-1;
        }
        cout<<r<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define fo(i,n) for(int i=0;i<(n);i++)
typedef long long ll;

ll A, B, N, l, t, m, lo, mid, hi, b;

bool check (ll r) {
	return A+(r-1)*B <= t && A*(r-l+1) + r*(r-1)*B/2 - (l-1)*(l-2)*B/2 <= m*t;
}

int main () {
	scanf("%lld %lld %lld", &A, &B, &N);
	fo(i, N) {
		scanf("%lld %lld %lld\n", &l, &t, &m);
		lo = l, hi = 1e7, b = -1;
		while (lo <= hi) {
			mid = (lo+hi) / 2;
			if (check(mid)) {
				lo = mid + 1;
				b = mid;
			} else {
				hi = mid - 1;
			}
		}
		printf("%lld\n", b);
	}
	return 0;
}


// Infinity on Apr 22, 2018

#include <bits/stdc++.h>

using namespace std;

inline namespace Infinity
{
#define define_pair(classname, x, y) struct classname { int x; int y; \
    classname(int x = 0, int y = 0) : x(x), y(y) {} };
#define define_triple(classname, x, y, z) struct classname { int x; int y; int z; \
    classname(int x = 0, int y = 0, int z = 0) : x(x), y(y), z(z) {} };
#define less_than(classname) bool operator <(const classname &lhs, const classname &rhs)
#define lambda_2crs(type) [](const type &lhs, const type &rhs)
#define compare_2keys(type, var1, op1, var2, op2) lambda_2crs(type) \
    { return lhs.var1 != rhs.var1 ? lhs.var1 op1 rhs.var1 : lhs.var2 op2 rhs.var2; }
#define forever for(;;)

inline namespace Constant
{
constexpr const int eof         = EOF;
constexpr const int maxint      = INT_MAX;
constexpr const long long maxll = LLONG_MAX;
constexpr const double eps      = __DBL_EPSILON__;
} // namespace Infinity::Constant

inline namespace TypeDefine
{
using uint      = unsigned int;
using ll        = long long int;
using llong     = long long int;
using ull       = unsigned long long int;
using ullong    = unsigned long long int;
using ld        = long double;
template<typename T> using ConstRef = const T &;
template<typename T = vector<int>> using Iter = typename T::iterator;
} // namespace Infinity::TypeDefine

inline namespace Miscelleneous
{
inline constexpr int ctoi(const char c)
{ return c - '0'; }

inline constexpr char itoc(const int n)
{ return n + '0'; }

inline int dtoi(const double d)
{ return round(d); }

template<typename T, typename P> inline T ceil(T a, P b)
{ return ::ceil(double(a) / b); }

template<typename T, typename P> inline T floor(T a, P b)
{ return ::floor(double(a) / b); }

template<typename T> inline constexpr bool in(T x, T l, T r)
{ return l <= x && x <= r; }

template<typename T> inline constexpr T tristate(int val, T nag, T zero, T pos)
{ return val > 0 ? pos : val < 0 ? nag : zero; }

template<class T> inline int size(const T &a)
{ return a.size(); }

template<class T> inline void sort(T &a)
{ sort(a.begin(), a.end()); }

template<class T1, class T2> inline void sort(T1 &a, T2 comp)
{ sort(a.begin(), a.end(), comp); }

template<class T1, typename T2> inline int lbound(const T1 &a, const T2 k)
{ return lower_bound(a.begin(), a.end(), k) - a.begin(); }

template<class T1, typename T2> inline int ubound(const T1 &a, const T2 k)
{ return upper_bound(a.begin(), a.end(), k) - a.begin(); }

template<class T1, class T2> inline int count(T1 &a, T2 k)
{ return ubound(a, k) - lbound(a, k); }

template<class T> inline void unique_erase(T &a)
{ a.erase(unique(a.begin(), a.end()), a.end()); }

template<typename T> inline void clear(T &a)
{ memset(a, 0, sizeof a); }

template<typename T> T gcd(T a, T b)
{ while (b) { T t = a % b; a = b; b = t; } return a; }

template<typename T> T lcm(T a, T b)
{ return a / gcd(a, b) * b; }

long long qpow(long long a, long long b, long long c)
{ return b ? qpow(a * a % c, b >> 1, c) * (b & 1 ? a : 1) % c : 1; }

// find (x, y) s.t. a x + b y = gcd(a, b) = d
template<typename T> T exGcd(T a, T b, T &x, T &y)
{ T d = a; if (b) { d = exGcd(b, a % b, y, x); y -= a / b * x; } else { x = 1; y = 0; } return d; }

// returns number of nk's in [l, r]
template<typename T> inline constexpr T mps(T l, T r, T k)
{ return ((r - (r % k + k) % k) - (l + (k - l % k) % k)) / k + 1; }

template<typename T> inline constexpr T sgn(T a)
{ return a ? a > 0 ? 1 : -1 : 0; }

template<typename T> inline constexpr T sq(T a)
{ return a * a; }

template<typename T> inline constexpr bool odd(T a)
{ return bool(a % 2); }

template<typename T> inline constexpr bool even(T a)
{ return !bool(a % 2); }

// simple mod
template<typename T1, typename T2> inline constexpr T1 smod(T1 x, T2 m)
{ return x > m ? x - m : x + m < m ? x + m : x; }

// lengthes of recurring period of replacement `a'
vector<int> repetend(vector<int> a)
{ vector<int> p(a.size(), -1);
  for (unsigned i = 0; i < a.size(); i++) if (p[i] == -1) {
    int c = 1; for (unsigned j = a[i]; j != i; j = a[j]) ++c;
    p[i] = c; for (unsigned j = a[i]; j != i; j = a[j]) p[j] = c;
  } return p; }

template<typename T> inline constexpr T lmiddle(T first, T last)
{ return first + (last - first) / 2; }

template<typename T> inline constexpr T rmiddle(T first, T last)
{ return last - (last - first) / 2; }

// returns the largest value in [l, r) s.t. check(x)
template<typename T, typename F> T dichotomy(T l, T r, F check, T prec = 1)
{ while (r - l > prec) { T m = lmiddle(l, r); (check(m) ? l : r) = m; } return l; }

// returns the smallest value in (l, r] s.t. check(x)
template<typename T, typename F> T dichotomy2(T l, T r, F check, T prec = 1)
{ while (r - l > prec) { T m = rmiddle(l, r); (check(m) ? r : l) = m; } return r; }

template<typename T> bool equals(initializer_list<T> list)
{ for (T item : list) { if (item != *list.begin()) { return false; } } return true; }
} // namespace Infinity::Miscelleneous

inline namespace BattleLab
{
template<typename T>
vector<T> iota(T val = T())
{ vector<T> a(val); iota(a.begin(), a.end(), T()); return a; }

template<typename T>
vector<int> argsort(const vector<T> &a)
{ vector<pair<T, int>> b; for (unsigned i = 0; i < a.size(); i++) { b.emplace_back(a[i], i); }
  stable_sort(b.begin(), b.end()); vector<int> c; for (pair<T, int> p : b) { c.push_back(p.second); }
  return c; }

template<typename T>
vector<T> rearrange(const vector<T> &a, const vector<int>& b)
{ vector<T> c(a.size()); for (unsigned i = 0; i < b.size(); i++) { c[b[i]] = a[i]; } return c; }

//template<typename T>
class Pair : public pair<int, int>
{
public:
    using pair<int, int>::pair;

    Pair operator +(const Pair &p)
    { return {this->first + p.first, this->second + p.second}; }

    Pair operator +=(const Pair &p)
    { return *this = *this + p; }

    bool operator <(const Pair &p)
    { return this->first != p.first ? this->first < p.first : this->second < p.second; }
};

// Array begins

struct
{
    int i, n;
    bool first() const { return i == 0; }
    bool last() const { return i == n - 1; }
} loop;

template<typename T>
class Array : public std::vector<T>
{
public:
    using std::vector<T>::vector;

    Array() = default;

    Array(const vector<T> &a) : vector<T>(a)
    {}

    int length() const
    { return this->size(); }

    T min() const
    { return *min_element(this->begin(), this->end()); }

    T max() const
    { return *max_element(this->begin(), this->end()); }

    int indexOf(T elem)
    { auto it = find(this->begin(), this->end(), elem); return it == this->end() ? -1 : it - this->begin(); }

    template<typename... types> Array<T> &append(types ...args)
    { this->emplace_back(args...); return *this; }

    template<typename... types> Array<T> &prepend(types ...args)
    { insert(this->begin(), args...); return *this; }

    T sum() const
    { T s = T(); for (T val : *this) { s += val; } return s; }

    template<typename P> P sum(P val) const
    { for (T x : *this) { val += x; } return val; }

    Array<T> &add(T increment = 1)
    { for (T &val : *this) { val += increment; } return *this; }

    Array<T> &unique()
    { this->erase(std::unique(this->begin(), this->end()), this->end()); return *this; }

    template<typename F> void foreach(F func)
    { loop.n = length(); for (loop.i = 0; loop.i < loop.n; loop.i++) { func(this->at(loop.i)); } }

    template<typename F> Array<pair<int, int>> segments(F func)
    { Array<pair<int, int>> a; int i = 0; while (i < length()) { int j = i + 1;
      while (j < length() && func(this->at(j - 1), this->at(j))) { j++; }
      a.emplace_back(i, j); i = j; } return a; }

    void iota()
    { std::iota(this->begin(), this->end(), T()); }

#if __cplusplus >= 201402L
#define STL_ALGO_OO(func) \
    template<typename... types> auto func(types ...args) \
    { return std::func(this->begin(), this->end(), args...); }
#define INFINITY_ALGO_OO(func) \
    template<typename... types> auto func(types ...args) \
    { return ::func(*this, args...); }
#define INFINITY_ALGO_OO_OVERRIDE(func, return_type) \
    template<typename... types> return_type func(types ...args) \
    { return ::func(*this, args...); }
#define PAST_PARTICIPLE(stem, pp) \
    template<typename... types> Array<T> pp(types ...args) \
    { Array<T> a = *this; a.stem(args...); return a; }

    STL_ALGO_OO(sort)           PAST_PARTICIPLE(sort, sorted)
    STL_ALGO_OO(stable_sort)    PAST_PARTICIPLE(stable_sort, stable_sorted)
    STL_ALGO_OO(reverse)        PAST_PARTICIPLE(reverse, reversed)
                                PAST_PARTICIPLE(unique, uniqued)
    STL_ALGO_OO(lower_bound)    STL_ALGO_OO(next_permutation)
    STL_ALGO_OO(upper_bound)    STL_ALGO_OO(prev_permutation)
    STL_ALGO_OO(count)          STL_ALGO_OO(count_if)
    STL_ALGO_OO(find)           STL_ALGO_OO(find_if)
    STL_ALGO_OO(fill)           STL_ALGO_OO(partial_sum)
    STL_ALGO_OO(random_shuffle) PAST_PARTICIPLE(random_shuffle, random_shuffled)
    INFINITY_ALGO_OO(lbound)    INFINITY_ALGO_OO(ubound)
    INFINITY_ALGO_OO_OVERRIDE(argsort, Array<int>)

#undef STL_ALGO_OO
#undef INFINITY_ALGO_OO
#undef INFINITY_ALGO_OO_OVERRIDE
#undef PAST_PARTICIPLE
#endif
};

Array<int> getints(unsigned size)
{ Array<int> a(size); for (int &x : a) { scanf("%d", &x); } return a; }

class ints : public Array<int>
{
public:
    using Array<int>::Array;

    void getint()
    { int x; scanf("%d", &x); append(x); }
};

using llongs = Array<long long>;
using doubles = Array<double>;
using PairList = Array<Pair>;
using int2d = Array<Array<int>>;

// Array ends

// String begins

class String : public std::string
{
public:
    using std::string::string;

    String(const std::string &s = "") : std::string(s)
    {}

    String &prepend(char c)
    { this->insert(this->begin(), c); return *this; }

    int len() const
    { return length(); }

    String replace(const String &pattern, const String &replace)
    {
        String t;
        unsigned pos = 0;
        unsigned long long sub = 0;
        while ((sub = this->find(pattern, pos)) != string::npos) {
            t += this->substr(pos, sub - pos) + replace;
            pos = sub + pattern.length();
        }
        t += this->substr(pos);
        return t;
    }

    Array<String> split(char delimiter = ' ') const
    {
        Array<String> a(1);
        for (char c : *this) {
            if (c != delimiter) {
                a.back().push_back(c);
            } else {
                a.emplace_back();
            }
        }
        return a;
    }

    String repeat(int times) const
    { String s; while (--times) { s += *this; } return s; }

    String reversed() const
    { String s{*this}; reverse(s.begin(), s.end()); return s; }

    int toInt(int base = 10) const
    { return stoi(*this, 0, base); }
};

inline String getstr(unsigned size = 0x100000)
{ char s[++size]; scanf("%s", s); return s; }

inline String getln(unsigned size = 0x100000)
{ char s[++size]; scanf("%[^\n]", s); getchar(); return s; }

inline void print(String s)
{ printf("%s", s.data()); }

class StringList : public Array<String>
{
public:
    using Array<String>::Array;

    String join(const String &delimiter)
    {
        String s = this->front();
        for (int i = 1; i < this->length(); i++) {
            s += delimiter + this->at(i);
        }
        return s;
    }

    operator vector<string>()
    { vector<string> vs; for (String s : *this) { vs.push_back(s); } return vs; }
};

inline StringList getstrs(int n, unsigned buffSize = 0x100000)
{ StringList sl; while (n--) { sl.append(getstr(buffSize)); } return sl; }

// String ends

class Division
{
public:
    Division(int n, int k) : lb(n / k), ub(lb + 1), b(n % k) {}
    int operator[](int i) { return i < b ? ub : lb; }
protected:
    int lb, ub, b;
};

struct AdjacencyList : public vector<vector<int>>
{
    using vector<vector<int>>::vector;
    void addDirected(int u, int v) { (*this)[u].push_back(v); }
    void addUndirected(int u, int v) { addDirected(u, v); addDirected(v, u); }
};

class Range
{
protected:
    class RangeIterator
    {
    public:
        RangeIterator(int i = 0, int step = 1) : i(i), step(step), forward(step > 0) {}
        // works fine with c++11 range-based for loop ... only
        bool operator !=(const RangeIterator &it) const { return forward ? i < it.i : i > it.i; }
        int operator *() const { return i; }
        RangeIterator operator ++() { return {i += step, step}; }
    protected:
        int i, step;
        bool forward;
    };
public:
    Range(int to) : from(0), to(to), step(1) {}
    Range(int from, int to, int step = 1) : from(from), to(to), step(step) {}
    RangeIterator begin() const { return {from, step}; }
    RangeIterator end() const { return {to, step}; }
protected:
    int from, to, step;
};

typedef Range range;

class ReversedRange : public Range
{
public:
    ReversedRange(int to) : Range(to - 1, -1, -1) {}
//    ReversedRange(int from, int to, int step = 1) : from(to), to(from + 1), step(step) {}
};

typedef ReversedRange reversed_range;

//#define repeat(x) for (loop.i = 0, loop.n = x; loop.i < loop.n; loop.i++)
} // namespace Infinity::BattleLab

inline namespace IO
{
const char LF = '\n';
const char SP = ' ';

int dbl_prec = 10;
int ldbl_prec = 18;

inline void print(const int n)
{ printf("%d", n); }

inline void print(const unsigned n)
{ printf("%u", n); }

inline void print(const long long n)
{ printf("%lld", n); }

inline void print(const unsigned long long n)
{ printf("%lld", n); }

//inline void print(const int64_t n)
//{ printf("%" PRId64, n); }

//inline void print(const uint64_t n)
//{ printf("%" PRIu64, n); }

inline void print(const double a)
{ printf("%.*f", dbl_prec, a); }

inline void print(const long double a)
{ printf("%.*Lf", ldbl_prec, a); }

inline void print(const char c)
{ printf("%c", c); }

inline void print(const char s[])
{ printf("%s", s); }

inline void print(const string &s)
{ printf("%s", s.data()); }

template<typename T> inline void printsp(const T a)
{ print(a); print(SP); }

template<typename T, typename U> inline void print(const pair<T, U> &p)
{ printSP(p.first); print(p.second); }

template<class T> inline void print(const T a)
{ for (auto i = a.begin(); i != a.end(); i++) { if (i != a.begin()) print(SP); print(*i); } }

template<typename T> inline void print(initializer_list<T> list)
{ for (auto i = list.begin(); i != list.end(); i++) { if (i != list.begin()) print(SP); print(*i); } }

inline void println()
{ print(LF); }

inline void printsp()
{ print(SP); }

template<typename T> inline void println(const T &a)
{ print(a); print(LF); }

template<typename T> inline void println(initializer_list<T> list)
{ for (auto i = list.begin(); i != list.end(); i++) { if (i != list.begin()) print(SP); print(*i); } print(LF); }

template<typename T, typename... types> inline void print(const T &a, const types &...args)
{ print(a); print(args...); }

template<typename... types> inline void println(const types &...args)
{ print(args...); print(LF); }

template<typename... types> inline void printsp(const types &...args)
{ print(args...); print(SP); }

inline void printlnYN(bool b)
{ println(b ? "YES" : "NO"); }

inline void printlnyn(bool b)
{ println(b ? "Yes" : "No"); }

string caseN(int n, bool sharp = true, bool space = true)
{ return string("Case ") + (sharp ? "#" : "") + to_string(n) + ":" + (space ? " " : ""); }

inline int read(int &n)
{ return scanf("%d", &n); }

inline int read(int64_t &n)
{ return scanf("%" SCNd64, &n); }

template<typename T, typename ... types> inline int read(T &n, types &...args)
{ return read(n) == eof ? eof : read(args...) + 1; }

inline char getcc()
{ char c; do c = getchar(); while (isspace(c)); return c; }

inline int getint(int increment = 0)
{ int n; scanf("%d", &n); return n + increment; }

inline int64_t getll(int64_t increment = 0)
{ int64_t n; scanf("%" SCNd64, &n); return n + increment; }

inline double getdbl()
{ double n; scanf("%lf", &n); return n; }

inline Pair getpair()
{ Pair p; scanf("%d%d", &p.first, &p.second); return p; }

inline PairList getpairs(int n)
{ PairList v(n); for (Pair &p : v) { p = getpair(); } return v; }
} // namespace Infinity::IO
} // namespace Infinity

long long a, b, n;

long long sum(int l, int r)
{
    return (a + (l - 1) * b + a + (r - 1) * b) * (r - l + 1) / 2;
}

int main(int, char *[])
{
    a = getint(), b = getint(), n = getint();

    for (int i : range(n)) {
        int l = getint(), t = getint(), m = getint();
        int r1 = dichotomy(l - 1, 10000000, [&](int x) {
            return sum(l, x) <= 1ll * t * m;
        });
        int r2 = dichotomy(l - 1, 10000000, [&](int x) {
            return a + (x - 1) * b <= t;
        });
        println(min(r1, r2) < l ? -1 : min(r1, r2));
    }
    return 0;
}


#include<bits/stdc++.h>
//#define endl "\n"
using namespace std;
int main()
{
	//ios::sync_with_stdio(false);
	//cin.tie(0);
	long long a,b,n;
	cin>>a>>b>>n;
	for (long long z=1; z<=n; z++)
	{
		long long l,t,m;
		cin>>l>>t>>m;
		if (a+(l-1)*b>t)
		{
			cout<<-1<<endl;
			continue;
		}
		long long L=l,R=(t-a)/b+1;
		while (L<=R)
		{
			long long M=(L+R)/2;
			if ((a+(l-1)*b+a+(M-1)*b)*(M-l+1)/2<=t*m) L=M+1;
			else R=M-1;
		}
		L--;
		cout<<L<<endl;
	}
	return 0;
}


#include <cstdio>
using namespace std;
long long a, b, n;
long long cal(long long pos) {
	return a + (pos - 1) * b;
}
int main(int argc, char const *argv[]) {
	scanf("%lld %lld %lld", &a, &b, &n);
	for (int i = 0; i < n; ++i) {
		long long l, t, m; scanf("%lld %lld %lld", &l, &t, &m);
		if (cal(l) > t) {
			printf("-1\n");
		} else {
			long long vl = l, vr = (t - a) / b + 1;
			while (vl <= vr) {
				long long vm = (vl + vr) / 2;
				if ((cal(l) + cal(vm)) * (vm - l + 1) / 2 <= t * m) {
					vl = vm + 1;
				} else {
					vr = vm - 1;
				}
			}
			printf("%lld\n", vl - 1);
		}
	}
	return 0;
}


#include<bits/stdc++.h>
#define DE cout << "------" << endl
#define mems(a, b) memset(a, (b), sizeof a)
#define maxn 110000
using namespace std;
typedef long long ll;
typedef long double ld;

ll A,B,n;
ll l,m,t;
ll suan(ll x){
    ll aa = 0;
    ll s = (x-l+1)/(2*m);
    aa += s*(2*A-2*m*B-B);
    aa += B*s*(x+x-2*m*(s-1));
    x -= s*2*m;//cout << x << aa<< endl;
    if(x-l+1<=m) aa += (x-1)*B+A;
    else{
        aa += A+(x-1)*B;
        ll t = A+(x-m-1)*B-(m-1)*B;
        if(t>0) aa += t;//cout << "??"<<endl;
    }
   // printf(" %lld %lld\n",x,aa);
    return aa;
}
int main(){
    cin >> A >> B >> n;
    for(int i=1;i<=n;i++){
        cin >> l >> t >> m;
        ll w = A+B*(l-1);
        if(w>t){
            printf("-1\n");
            continue;
        }
        ll L = l;
        ll R = 2e6;
        while(L<R){
           // printf("%lld %lld\n",L,R);
            ll x = (L+R)/2+(L+R)%2;
            if(suan(x)<=t){
                L = x;
            }
            else R=x-1;
        }
        printf("%lld\n",L);
    }
    return 0;
}


#include <iostream>

using namespace std;
long long a,b,n,l,m,t,mij,st,dr,last,maxi,sum;
int main()
{
    cin>>a>>b>>n;
    while(n)
    {
        n--;
        cin>>l>>t>>m;
        if(a+b*(l-1)>t)
        {
            cout<<"-1\n";
            continue;
        }

        st=l;
        dr=1e18;
        last=-1;

        while(st<=dr)
        {
            mij=(st+dr)/2;

            maxi=a+b*(mij-1);
            sum=a*(mij-l+1);
            sum+=b*(mij*(mij-1)/2 - (l-1)*(l-2)/2);

            if(maxi<=t && sum<=m*t)
            {
                st=mij+1;
                last=mij;
            }
            else
                dr=mij-1;
        }

        cout<<last<<'\n';
    }
    return 0;
}


#include <iostream>

using namespace std;
long long a,b,n,l,m,t,mij,st,dr,last,maxi,sum;
int main()
{
    cin>>a>>b>>n;
    while(n)
    {
        n--;
        cin>>l>>t>>m;
        if(a+b*(l-1)>t)
        {
            cout<<"-1\n";
            continue;
        }

        st=l;
        dr=1e12;
        last=-1;

        while(st<=dr)
        {
            mij=(st+dr)/2;

            maxi=a+b*(mij-1);
            sum=a*(mij-l+1);
            sum+=b*(mij*(mij-1)/2 - (l-1)*(l-2)/2);

            if(maxi<=t && sum<=m*t)
            {
                st=mij+1;
                last=mij;
            }
            else
                dr=mij-1;
        }

        cout<<last<<'\n';
    }
    return 0;
}


#include <bits/stdc++.h>
 using namespace std;
 #define dude ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
 #define endl "\n"
 #define int  long long
 #define ll  long long
 int mod=1e9+7;
 ll powe(ll x, ll y){ x = x%mod, y=y%(mod-1);ll ans = 1;while(y>0){if (y&1){ans = (1ll * x * ans)%mod;}y>>=1;x = (1ll * x * x)%mod;}return ans;}
 void fun()
 {
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
 }

int fun(int n)
{
    return n*(n+1)/2;
}
signed main()
{
    dude;
    fun();
    int a,b,l,t,m,n;
    cin>>a>>b>>n;

    while(n--)
    {
        cin>>l>>t>>m;
        if(a+b*(l-1)>t)
        {
            cout<<-1<<endl;
            continue;
        }

        int left=l,right=l+10000000000000;

        int mid;
        while(left<=right)
        {
              mid=(left+right)/2;
             int x=a+b*(mid-1);
             int cost=(mid-l+1)*a+(fun(mid-1)-fun(l-2))*b;
             //cout<<mid<<" "<<x<<" "<<cost<<endl;

             if(x>t)
                right=mid-1;
            else if(cost>m*t)
                right=mid-1;
            else if(x<=t)
                left=mid+1;

        }

        cout<<right<<endl;
    }

}


#include <bits/stdc++.h>
 using namespace std;
 #define dude ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
 #define endl "\n"
 #define int  long long
 #define ll  long long
 int mod=1e9+7;
 ll powe(ll x, ll y){ x = x%mod, y=y%(mod-1);ll ans = 1;while(y>0){if (y&1){ans = (1ll * x * ans)%mod;}y>>=1;x = (1ll * x * x)%mod;}return ans;}
 void fun()
 {
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
 }

int fun(int n)
{
    return n*(n+1)/2;
}
signed main()
{
    dude;
    fun();
    int a,b,l,t,m,n;
    cin>>a>>b>>n;

    while(n--)
    {
        cin>>l>>t>>m;
        if(a+b*(l-1)>t)
        {
            cout<<-1<<endl;
            continue;
        }

        int left=l,right=l+100;

        int mid;
        while(left<=right)
        {
              mid=(left+right)/2;
             int x=a+b*(mid-1);
             int cost=(mid-l+1)*a+(fun(mid-1)-fun(l-2))*b;
             //cout<<mid<<" "<<x<<" "<<cost<<endl;

             if(x>t)
                right=mid-1;
            else if(cost>m*t)
                right=mid-1;
            else if(x<=t)
                left=mid+1;

        }

        cout<<right<<endl;
    }

}


#include <iostream>
#include <bits/stdc++.h>
#include <vector>
#include <algorithm>
typedef long long int ll;
using namespace std;
#define P 1000000007
#define rep(i,n) for(i=0;i<n;++i)
#define re(i,a,n) for(i=a;i<n;++i)
#define repr(i,a,n) for(i=a;i>=n;--i)
#define pb push_back
#define mp make_pair
#define fi first
#define se second

ll binpowmod(ll a, ll b, ll m) {
    a %= m;
    ll res = 1;
    while (b > 0) {
        if (b & 1)
            res = res * a % m;
        a = a * a % m;
        b >>= 1;
    }
    return res;
}

ll binpow(ll a, ll b) {
    ll res = 1;
    while (b > 0) {
        if (b & 1)
            res = res * a;
        a = a * a;
        b >>= 1;
    }
    return res;
}

ll modInverse(ll n, ll p)
{
    return binpowmod(n, p-2, p);
}

vector<vector<ll>> adj1,adj2;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll t,m,n,i,f=0,j,ans=0,l,r;
   	ll a,b,x,t1;
    cin >> a >> b >> n ;
    rep(i,n)
    {
      cin >> l >> t >> m ;
      ll l1,r1,mid;
      t1=a+(l-1)*b;
      if(t1>t)
      {
        cout << -1 << endl ;
        continue ;
      }
      x=(t-a)/b+1;
      l1=l,r1=x;
      while(l1<=r1)
      {
          mid=l1+(r1-l1)/2;
          t1=a*(mid-l+1)+b*(((mid-1)*(mid)-(l-1)*(l-2))/2);
          if(t1<=t*m)
          {
            r=mid;
            l1=mid+1;
          }
          else
          {
            r1=mid-1;
          }
      }
      cout << r << endl ;
    }

    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
#include <vector>
#include <algorithm>
typedef long long int ll;
using namespace std;
#define P 1000000007
#define rep(i,n) for(i=0;i<n;++i)
#define re(i,a,n) for(i=a;i<n;++i)
#define repr(i,a,n) for(i=a;i>=n;--i)
#define pb push_back
#define mp make_pair
#define fi first
#define se second

ll binpowmod(ll a, ll b, ll m) {
    a %= m;
    ll res = 1;
    while (b > 0) {
        if (b & 1)
            res = res * a % m;
        a = a * a % m;
        b >>= 1;
    }
    return res;
}

ll binpow(ll a, ll b) {
    ll res = 1;
    while (b > 0) {
        if (b & 1)
            res = res * a;
        a = a * a;
        b >>= 1;
    }
    return res;
}

ll modInverse(ll n, ll p)
{
    return binpowmod(n, p-2, p);
}

vector<vector<ll>> adj1,adj2;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll t,m,n,i,f=0,j,ans=0,l,r;
   	ll a,b,x,t1;
    cin >> a >> b >> n ;
    rep(i,n)
    {
      cin >> l >> t >> m ;
      ll l1,r1,mid;
      t1=a+(l-1)*b;
      if(t1>t)
      {
        cout << -1 << endl ;
        continue ;
      }
      x=(t-a)/b+1;
      l1=l,r1=x;
      while(l1<=r1)
      {
          mid=l1+(r1-l1)/2;
          t1=a*(mid-l+1)+b*(((mid-1)*(mid)-(l-1)*(l))/2);
          if(t1<=t*m)
          {
            r=mid;
            l1=mid+1;
          }
          else
          {
            r1=mid-1;
          }
      }
      cout << r << endl ;
    }

    return 0;
}


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

typedef long long ll;

const int N = int(1e9);

int n;
ll A, B;

ll c_1(ll num) {
	return A + (num - 1) * B;
}

ll s_1(ll num) {
	return (A + c_1(num)) * num / 2;
}

bool check(ll num, ll l, ll t, ll m) {
	//cout << num << " " << c_1(num) << " " << s_1(num) << endl;
	return (c_1(num) <= t) && (s_1(num) - s_1(l) + c_1(l) <= t * m);
}

void solve(ll v, ll t, ll m) {
	ll l = v, r = N;
	while(l != r) {
		ll mid = (l + r) / 2 + (l + r) % 2;
		if(check(mid, v, t, m)) {
			l = mid;
		} else {
			r = mid - 1;
		}
	}
	(check(l, v, t, m)) ? printf("%lld\n", l) : printf("-1\n");
}

int main (int argc, char *argv[])
{
	scanf("%lld%lld%d", &A, &B, &n);
	while(n--) {
		ll l;
		ll t, m;
		scanf("%lld%lld%lld", &l, &t, &m);
		solve(l, t, m);
	}

	return 0;
}


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

typedef long long ll;

const int N = int(1e18);

int n;
ll A, B;

ll c_1(ll num) {
	return A + (num - 1) * B;
}

ll s_1(ll num) {
	return (A + c_1(num)) / 2 * num;
}

bool check(ll num, ll l, ll t, ll m) {
	//cout << num << " " << c_1(num) << " " << s_1(num) << endl;
	return (c_1(num) <= t) && (s_1(num) - s_1(l) + c_1(l) <= t * m);
}

void solve(ll v, ll t, ll m) {
	ll l = v, r = N;
	while(l != r) {
		ll mid = (l + r) / 2 + (l + r) % 2;
		if(check(mid, v, t, m)) {
			l = mid;
		} else {
			r = mid - 1;
		}
	}
	(check(l, v, t, m)) ? printf("%lld\n", l) : printf("-1\n");
}

int main (int argc, char *argv[])
{
	scanf("%lld%lld%d", &A, &B, &n);
	while(n--) {
		ll l;
		ll t, m;
		scanf("%lld%lld%lld", &l, &t, &m);
		solve(l, t, m);
	}

	return 0;
}


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

typedef long long ll;

const int N = int(1e6);

int n;
ll A, B;
ll arr[N], s[N];

void fill() {
	arr[0] = A;
	s[0] = A;
	for(int i = 1; i < N; ++i) {
		arr[i] = arr[i - 1] + B;
		s[i] = s[i - 1] + arr[i];
	}
}

bool check(int num, int l, ll t, ll m) {
	return (arr[num] <= t) && (s[num] - s[l] + arr[l] <= t * m);
}

void solve(int v, ll t, ll m) {
	int l = v, r = N;
	while(l != r) {
		int mid = (l + r) / 2 + (l + r) % 2;
		if(check(mid, v, t, m)) {
			l = mid;
		} else {
			r = mid - 1;
		}
	}
	(check(l, v, t, m)) ? printf("%d\n", l + 1) : printf("-1\n");
}

int main (int argc, char *argv[])
{
	scanf("%lld%lld%d", &A, &B, &n);
	fill();
	while(n--) {
		int l;
		ll t, m;
		scanf("%d%lld%lld", &l, &t, &m);
		l--;
		solve(l, t, m);
	}

	return 0;
}


#include<bits/stdc++.h>
//#include<ctime>
//#include <stdlib.h>
#define fast ios_base:: sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define time ios_base::sync_with_stdio(0);cin.tie(0);
#define dpp(arr,val) memset(arr,val,sizeof (arr))
#define ll long long
#define ull unsigned long long
#define dd double
#define ld long double
#define PQ priority_queue
#define pii pair<int,int>
#define pll pair<ll,ll>
#define S second
#define F first
#define MP make_pair
using namespace std;
ll add(ll a,ll b,ll mod)
{
    return (a%mod+b%mod)%mod;
}
ll sub(ll a,ll b,ll mod)
{
    return (a%mod-b%mod+mod)%mod;

}
ll mul(ll a,ll b,ll mod)
{
    return ((a%mod)*(b%mod))%mod;
}
ll sumodd(ll num,ll mod)
{
    return mul(num,num,mod);
}
ll sumeven(ll num,ll mod)
{
    return mul(num,num+1,mod);
}
ll sum_any_range(ll st,ll en,ll num)
{
    return (num*(st+en)/2);
}
ll gcd(ll a,ll b)
{
    while(b!=0)
    {
        ll a2=a;
        a=b;
        b=a2%b;
    }
    return a;
}
ll lcm(ll a,ll b)
{
    return a/gcd(a,b)*b;
}
string makeitbinary(ll n)
{
    string s;
    while(n)
    {
        s=s+(char)((n%2)+'0');
        n/=2;
    }
    reverse(s.begin(),s.end());
    return s;
}

bool bit(int num,int i)
{
    return((num>>i)&1);
}
ll fastpowermod(ll b,ll p,ll mod)
{
    ll ans=1;
    while(p)
    {
        if (p%2)
        {
            ans=mul(ans,b,mod);
        }
        b=mul(b,b,mod);
        p/=2;
    }
    return ans;
}
ll fastpower(ll b,ll p)
{
    ll ans=1;
    while(p)
    {
        if (p%2)
        {
            ans=ans*b;
        }
        b=b*b;
        p/=2;
    }
    return ans;
}
double fastpower_double(double b,ll p)
{
    double ans=1;
    while(p)
    {
        if (p%2)
        {
            ans=ans*b;
        }
        b=b*b;
        p/=2;
    }
    return ans;
}

ll summation_formula(ll n)
{
    return (n*(n+1)/2);
}
bool lower_vowel(char c)
{
    return (c=='i'||c=='o'||c=='u'||c=='a'||c=='e');
}
string bigint_mini(string s1,string s2)
{
    if (s1.size()>s2.size())
    {
        return s2;
    }
    else if (s2.size()>s1.size())
    {
        return s1;
    }
    for (int i=0; i<s1.size(); i++)
    {
        if ((s1[i]-'0')>(s2[i]-'0'))
        {
            return s2;
        }
        else if ((s2[i]-'0')>(s1[i]-'0'))
        {
            return s1;
        }
    }
    return s1;
}
double polygon_area(int n,vector<double>X,vector<double>Y)
{
    double area=0.0;
    int j=n-1;
    for (int i=0; i<n; i++)
    {
        area+=(X[j] + X[i]) * (Y[j] - Y[i]);
        j=i;
    }
    return abs(area / 2.0);
}
ll sum_of_digits(string s)
{
    ll sum=0;
    for (int i=0; i<s.size(); i++)
    {
        sum+=s[i]-'0';
    }
    return sum;
}
string makeitbase(ll num,ll base)
{
    string s;
    while(num)
    {
        ll mod=num%base;
        s+=(mod+'0');
        num/=base;
    }
    reverse(s.begin(),s.end());
    return s;
}
bool intersect(ll l1,ll r1,ll l2,ll r2)
{
    return(max(l1,l2)<=min(r1,r2));

}
pll find_intersection(ll l1,ll r1,ll l2,ll r2)
{
    return {max(l1,l2),min(r1,r2)};
}
ll sum_ranges(ll l,ll r)
{
    return summation_formula(r)-summation_formula(l);
}
double power_2(double num)
{
    return num*num;
}
bool isPowerOfTwo(int x)
{
    return (x && !(x & (x - 1)));
}
ll modInverse(ll A,ll M)
{
    return fastpowermod(A,M-2,M);
}

ll num_inrange(ll l,ll r)
{
    l=min(l,r);
    r=max(l,r);
    return r-l+1;
}
ll how_many_factor(ll num,ll t)
{
    ll cnt=0;
    while(num!=0&&num%t==0)
    {
        num/=t;
        cnt++;
    }
    return cnt;
}
pair<pll,pll>formula_rotate_left_(ll x,ll y,ll row_len,ll col_len)
{
    ll nx=col_len-y-1;
    ll ny=x;
    return {{nx,ny},{col_len,row_len}};
}
pair<pll,pll> formula_rotate_right_(ll x,ll y,ll row_len,ll col_len)
{
    ll nx=y;
    ll ny=row_len-x-1;
    return {{nx,ny},{col_len,row_len}};
}
pair<pll,pll> horizontal_rotate(ll x,ll y,ll row_len,ll col_len)
{
    ll nx=x;
    ll ny=col_len-y-1;
    return {{nx,ny},{row_len,col_len}};
}

ll mod_big_integer(string s,ll M)
{
    ll num=0;
    for (ll i=0; i<s.size(); i++)
    {
        num=add(mul(num,10,M),(s[i]-'0'),M);
    }
    return num;
}
struct Point
{
    ll x,y;
};
ll overlappingArea(Point l1, Point r1,Point l2, Point r2)
{

    ll area1=abs(l1.x - r1.x)*abs(l1.y - r1.y);

    ll area2=abs(l2.x - r2.x)*abs(l2.y - r2.y);

    ll areaI=(min(r1.x, r2.x)-max(l1.x, l2.x))*(min(r1.y, r2.y)-max(l1.y, l2.y));

    return areaI;
}
bool doOverlap(Point l1, Point r1, Point l2, Point r2)
{
    if (l1.x>r2.x||l2.x>r1.x)
    {
        return false;
    }

    if (l1.y>r2.y||l2.y>r1.y)
    {
        return false;
    }

    return true;
}

Point intersection_botl(Point l1,Point r1,Point l2, Point r2)
{
    Point topr,botl;
    topr.y=min(r1.y,r2.y);
    topr.x=min(r1.x,r2.x);
    botl.y=max(l1.y,l2.y);
    botl.x=max(l1.x,l2.x);
    return botl;
}

Point intersection_topr(Point l1,Point r1,Point l2, Point r2)
{
    Point topr,botl;
    topr.y=min(r1.y,r2.y);
    topr.x=min(r1.x,r2.x);
    botl.y=max(l1.y,l2.y);
    botl.x=max(l1.x,l2.x);
    return topr;
}
ll comp_two(Point l1,Point l2,Point r1,Point r2)
{
    if (doOverlap(l1,r1,l2,r2))
    {
        return overlappingArea(l1,r1,l2,r2);
    }
    else
    {
        return 0;
    }
}
ll geometric_progression_sum(ll endd,ll st,ll k)
{
    return (endd*k-st)/(k-1);
}
ll geometric_progression_sum_mod(ll endd,ll st,ll k,ll M)
{
    return mul(sub(mul(endd,k,M),st,M),modInverse(k-1,M),M);
}
ll makeitnumber(string s)
{
    ll num=1;
    for (int i=0; i<s.size(); i++)
    {
        num+=(s[i]-'0');
        num*=10;
    }
    return num;
}

ll M=1e9+7;
const int sz=1e6+10;
const int OO=0x3f3f3f3f;
bool check(ll A,ll B,ll r,ll l,ll t,ll m)
{

    ll d=(r-l+1);
    ll first=A*d;
    ll d2=((l-1+r-1)*d)/2;
    ll second=d2*B;
    return(first+second<=t*m&&A+(B*(r-1))<=t);

}
int main()
{
    //myf.open("file.txt");
    // freopen("portals.in", "r", stdin);
    //freopen("output.txt", "w", stdout);

    fast;
    ll A,B,n;
    cin>>A>>B>>n;
    for (int i=0;i<n;i++)
    {
     ll l,t,m;
     cin>>l>>t>>m;
     ll st=l;
     ll en=1e18;
     ll last=-1;
     while(st<=en)
     {
         ll mid=st+(en-st)/2;
         if (check(A,B,mid,l,t,m))
         {
           st=mid+1;
           last=mid;
         }
         else {
            en=mid-1;
         }
     }
     cout<<last<<endl;
    }
    return 0;

}


#include<bits/stdc++.h>
//#include<ctime>
//#include <stdlib.h>
#define fast ios_base:: sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define time ios_base::sync_with_stdio(0);cin.tie(0);
#define dpp(arr,val) memset(arr,val,sizeof (arr))
#define ll long long
#define ull unsigned long long
#define dd double
#define ld long double
#define PQ priority_queue
#define pii pair<int,int>
#define pll pair<ll,ll>
#define S second
#define F first
#define MP make_pair
using namespace std;
ll add(ll a,ll b,ll mod)
{
    return (a%mod+b%mod)%mod;
}
ll sub(ll a,ll b,ll mod)
{
    return (a%mod-b%mod+mod)%mod;

}
ll mul(ll a,ll b,ll mod)
{
    return ((a%mod)*(b%mod))%mod;
}
ll sumodd(ll num,ll mod)
{
    return mul(num,num,mod);
}
ll sumeven(ll num,ll mod)
{
    return mul(num,num+1,mod);
}
ll sum_any_range(ll st,ll en,ll num)
{
    return (num*(st+en)/2);
}
ll gcd(ll a,ll b)
{
    while(b!=0)
    {
        ll a2=a;
        a=b;
        b=a2%b;
    }
    return a;
}
ll lcm(ll a,ll b)
{
    return a/gcd(a,b)*b;
}
string makeitbinary(ll n)
{
    string s;
    while(n)
    {
        s=s+(char)((n%2)+'0');
        n/=2;
    }
    reverse(s.begin(),s.end());
    return s;
}

bool bit(int num,int i)
{
    return((num>>i)&1);
}
ll fastpowermod(ll b,ll p,ll mod)
{
    ll ans=1;
    while(p)
    {
        if (p%2)
        {
            ans=mul(ans,b,mod);
        }
        b=mul(b,b,mod);
        p/=2;
    }
    return ans;
}
ll fastpower(ll b,ll p)
{
    ll ans=1;
    while(p)
    {
        if (p%2)
        {
            ans=ans*b;
        }
        b=b*b;
        p/=2;
    }
    return ans;
}
double fastpower_double(double b,ll p)
{
    double ans=1;
    while(p)
    {
        if (p%2)
        {
            ans=ans*b;
        }
        b=b*b;
        p/=2;
    }
    return ans;
}

ll summation_formula(ll n)
{
    return (n*(n+1)/2);
}
bool lower_vowel(char c)
{
    return (c=='i'||c=='o'||c=='u'||c=='a'||c=='e');
}
string bigint_mini(string s1,string s2)
{
    if (s1.size()>s2.size())
    {
        return s2;
    }
    else if (s2.size()>s1.size())
    {
        return s1;
    }
    for (int i=0; i<s1.size(); i++)
    {
        if ((s1[i]-'0')>(s2[i]-'0'))
        {
            return s2;
        }
        else if ((s2[i]-'0')>(s1[i]-'0'))
        {
            return s1;
        }
    }
    return s1;
}
double polygon_area(int n,vector<double>X,vector<double>Y)
{
    double area=0.0;
    int j=n-1;
    for (int i=0; i<n; i++)
    {
        area+=(X[j] + X[i]) * (Y[j] - Y[i]);
        j=i;
    }
    return abs(area / 2.0);
}
ll sum_of_digits(string s)
{
    ll sum=0;
    for (int i=0; i<s.size(); i++)
    {
        sum+=s[i]-'0';
    }
    return sum;
}
string makeitbase(ll num,ll base)
{
    string s;
    while(num)
    {
        ll mod=num%base;
        s+=(mod+'0');
        num/=base;
    }
    reverse(s.begin(),s.end());
    return s;
}
bool intersect(ll l1,ll r1,ll l2,ll r2)
{
    return(max(l1,l2)<=min(r1,r2));

}
pll find_intersection(ll l1,ll r1,ll l2,ll r2)
{
    return {max(l1,l2),min(r1,r2)};
}
ll sum_ranges(ll l,ll r)
{
    return summation_formula(r)-summation_formula(l);
}
double power_2(double num)
{
    return num*num;
}
bool isPowerOfTwo(int x)
{
    return (x && !(x & (x - 1)));
}
ll modInverse(ll A,ll M)
{
    return fastpowermod(A,M-2,M);
}

ll num_inrange(ll l,ll r)
{
    l=min(l,r);
    r=max(l,r);
    return r-l+1;
}
ll how_many_factor(ll num,ll t)
{
    ll cnt=0;
    while(num!=0&&num%t==0)
    {
        num/=t;
        cnt++;
    }
    return cnt;
}
pair<pll,pll>formula_rotate_left_(ll x,ll y,ll row_len,ll col_len)
{
    ll nx=col_len-y-1;
    ll ny=x;
    return {{nx,ny},{col_len,row_len}};
}
pair<pll,pll> formula_rotate_right_(ll x,ll y,ll row_len,ll col_len)
{
    ll nx=y;
    ll ny=row_len-x-1;
    return {{nx,ny},{col_len,row_len}};
}
pair<pll,pll> horizontal_rotate(ll x,ll y,ll row_len,ll col_len)
{
    ll nx=x;
    ll ny=col_len-y-1;
    return {{nx,ny},{row_len,col_len}};
}

ll mod_big_integer(string s,ll M)
{
    ll num=0;
    for (ll i=0; i<s.size(); i++)
    {
        num=add(mul(num,10,M),(s[i]-'0'),M);
    }
    return num;
}
struct Point
{
    ll x,y;
};
ll overlappingArea(Point l1, Point r1,Point l2, Point r2)
{

    ll area1=abs(l1.x - r1.x)*abs(l1.y - r1.y);

    ll area2=abs(l2.x - r2.x)*abs(l2.y - r2.y);

    ll areaI=(min(r1.x, r2.x)-max(l1.x, l2.x))*(min(r1.y, r2.y)-max(l1.y, l2.y));

    return areaI;
}
bool doOverlap(Point l1, Point r1, Point l2, Point r2)
{
    if (l1.x>r2.x||l2.x>r1.x)
    {
        return false;
    }

    if (l1.y>r2.y||l2.y>r1.y)
    {
        return false;
    }

    return true;
}

Point intersection_botl(Point l1,Point r1,Point l2, Point r2)
{
    Point topr,botl;
    topr.y=min(r1.y,r2.y);
    topr.x=min(r1.x,r2.x);
    botl.y=max(l1.y,l2.y);
    botl.x=max(l1.x,l2.x);
    return botl;
}

Point intersection_topr(Point l1,Point r1,Point l2, Point r2)
{
    Point topr,botl;
    topr.y=min(r1.y,r2.y);
    topr.x=min(r1.x,r2.x);
    botl.y=max(l1.y,l2.y);
    botl.x=max(l1.x,l2.x);
    return topr;
}
ll comp_two(Point l1,Point l2,Point r1,Point r2)
{
    if (doOverlap(l1,r1,l2,r2))
    {
        return overlappingArea(l1,r1,l2,r2);
    }
    else
    {
        return 0;
    }
}
ll geometric_progression_sum(ll endd,ll st,ll k)
{
    return (endd*k-st)/(k-1);
}
ll geometric_progression_sum_mod(ll endd,ll st,ll k,ll M)
{
    return mul(sub(mul(endd,k,M),st,M),modInverse(k-1,M),M);
}
ll makeitnumber(string s)
{
    ll num=1;
    for (int i=0; i<s.size(); i++)
    {
        num+=(s[i]-'0');
        num*=10;
    }
    return num;
}

ll M=1e9+7;
const int sz=1e6+10;
const int OO=0x3f3f3f3f;
bool check(ll A,ll B,ll r,ll l,ll t,ll m)
{
    if (A+(B*(r-1))>t){return 0;}
    ll d=(r-l+1);
    ll first=A*d;
    ll d2=((l-1+r-1)*d)/2;
    ll second=d2*B;
    return(first+second<=t*m&&A+(B*(r-1))<=t);

}
int main()
{
    //myf.open("file.txt");
    // freopen("portals.in", "r", stdin);
    //freopen("output.txt", "w", stdout);

    fast;
    ll A,B,n;
    cin>>A>>B>>n;
    for (int i=0;i<n;i++)
    {
     ll l,t,m;
     cin>>l>>t>>m;
     ll st=l;
     ll en=1e12;
     ll last=-1;
     while(st<=en)
     {
         ll mid=st+(en-st)/2;
         if (check(A,B,mid,l,t,m))
         {
           st=mid+1;
           last=mid;
         }
         else {
            en=mid-1;
         }
     }
     cout<<last<<endl;
    }
    return 0;

}


#define _CRT_SECURE_NO_WAR
#include<bits/stdc++.h>
using namespace std;
#define ull long long int
#define ll unsigned long long int
#define file freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout);
#define KingOfWrongAnswer cin.tie(0);std::ios::sync_with_stdio(false);
#define forit(i, a) for ( __typeof( (a).begin() ) i = (a).begin(); i != (a).end(); i++ )
#define mp make_pair
#define pb push_back
#define clr(a,n) memset(a,n,sizeof(a))
#define all(c) c.begin(), c.end()
#define rep(i,n) for(int i=0;i<n;i++)
#define popone(a) __builtin_popcount(a)
#define popzero(a) __builtin_ctz(a)
#define PI = 2.0*acos(0.0)
#define mod 1000000007
#define pi 3.14159265359
//ll gcd (ll x,ll y){if(y==0)return x;return gcd(y,x%y);}
//ll lcm(ll a,ll b){return a/gcd(a,b) * b;}
//int getbit(int n,int i){return ((n>>i)&1)==1;}
//int setbit(int n,int i,int val){return val?(n|(1<<i)):(n&~(1<<i));}
//int graycode(int i){return i^(i>>1);}
//bool power2(int x){return (x && !(x & (x - 1)));}
//ll largest_power(ll n){n = n| (n>>1);n= n| (n>>2);n = n| (n>>4);n = n| (n>>8);return (n+1)>>1;}
//bool cmp(pair<int,int>x,pair<int,int>y){if (x.first==y.first) return x.second>y.second;return x.first<y.first;}
//bool vaild(int x,int y,int n){return (0 <= x && x < n && 0 <= y && y < n);}
//int dx[] = {-1, -1, 1, 1, -2, -2, 2, 2 };
//int dy[] = {2, -2, 2, -2, 1, -1, 1, -1 };
//int dx[] = {0, 0, -1, 1, -1, -1, 1, 1};
//int dy[] = {-1, 1, 0, 0, 1, -1, -1, 1};
//int dx[]= {-1, 1, 0, 0};
//int dy[]= {0, 0, 1, -1};
ll arr[3000066];
ll arr2[3000066];
int a,b,n,l,t,m;
bool check(int mid)
{
    if(arr[mid]-arr[l-1]<=t*m&&arr2[mid]<=t)
        return true;
    return false;
}
int main()
{
    KingOfWrongAnswer
    cin>>a>>b>>n;
    arr[0]=0;
    for(int i=1;i<2e6+66;i++)
        arr2[i]=a+(i-1)*b;
    for(int i=1;i<2e6+66;i++)
        arr[i]=a+(i-1)*b+arr[i-1];
    rep(i,n)
    {
        cin>>l>>t>>m;
        if(arr2[l]>t)
        {
            cout<<-1<<endl;
            continue;
        }
        int st=l,en=2e6+66,ans=-1;
        while(en>=st)
        {
            int mid=(en+st)/2;
            if(check(mid))
                st=mid+1,ans=mid;
            else
                en=mid-1;
        }
        cout<<ans<<endl;
    }
    return 0;
}


#define _CRT_SECURE_NO_WAR
#include<bits/stdc++.h>
using namespace std;
#define ull long long int
#define ll unsigned long long int
#define file freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout);
#define KingOfWrongAnswer cin.tie(0);std::ios::sync_with_stdio(false);
#define forit(i, a) for ( __typeof( (a).begin() ) i = (a).begin(); i != (a).end(); i++ )
#define mp make_pair
#define pb push_back
#define clr(a,n) memset(a,n,sizeof(a))
#define all(c) c.begin(), c.end()
#define rep(i,n) for(int i=0;i<n;i++)
#define popone(a) __builtin_popcount(a)
#define popzero(a) __builtin_ctz(a)
#define PI = 2.0*acos(0.0)
#define mod 1000000007
#define pi 3.14159265359
//ll gcd (ll x,ll y){if(y==0)return x;return gcd(y,x%y);}
//ll lcm(ll a,ll b){return a/gcd(a,b) * b;}
//int getbit(int n,int i){return ((n>>i)&1)==1;}
//int setbit(int n,int i,int val){return val?(n|(1<<i)):(n&~(1<<i));}
//int graycode(int i){return i^(i>>1);}
//bool power2(int x){return (x && !(x & (x - 1)));}
//ll largest_power(ll n){n = n| (n>>1);n= n| (n>>2);n = n| (n>>4);n = n| (n>>8);return (n+1)>>1;}
//bool cmp(pair<int,int>x,pair<int,int>y){if (x.first==y.first) return x.second>y.second;return x.first<y.first;}
//bool vaild(int x,int y,int n){return (0 <= x && x < n && 0 <= y && y < n);}
//int dx[] = {-1, -1, 1, 1, -2, -2, 2, 2 };
//int dy[] = {2, -2, 2, -2, 1, -1, 1, -1 };
//int dx[] = {0, 0, -1, 1, -1, -1, 1, 1};
//int dy[] = {-1, 1, 0, 0, 1, -1, -1, 1};
//int dx[]= {-1, 1, 0, 0};
//int dy[]= {0, 0, 1, -1};
ll arr[3000006];
ll arr2[3000006];
int a,b,n,l,t,m;
bool check(int mid)
{
    if(arr[mid]-arr[l-1]<=t*m&&arr2[mid]<=t)
        return true;
    return false;
}
int main()
{
    KingOfWrongAnswer
    cin>>a>>b>>n;
    arr[0]=0;
    for(int i=1;i<2e6+7;i++)
        arr2[i]=a+(i-1)*b;
    for(int i=1;i<2e6+7;i++)
        arr[i]=a+(i-1)*b+arr[i-1];
    rep(i,n)
    {
        cin>>l>>t>>m;
        if(arr2[l]>t)
        {
            cout<<-1<<endl;
            continue;
        }
        int st=l,en=2e6+7,ans=-1;
        while(en>=st)
        {
            int mid=(en+st)/2;
            if(check(mid))
                st=mid+1,ans=mid;
            else
                en=mid-1;
        }
        cout<<ans<<endl;
    }
    return 0;
}


#define <bits/stdc++.h>
#define fst first
#define snd second
#define N 212345

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;

int n, p[N], d[N];
bool rec[N];

int main()
{
	cin >> n;
	int mx = 0;
	int recs = 0;
	for (int i = 1; i <= n; i++) {
		scanf("%d", &p[i]);
		mx = max(mx, p[i]);
		d[i] = mx;
		if (p[i] == mx)
			rec[i] = true;
	}
	if (n == 1) {
		cout << "1" << endl;
		return 0;
	}
	int mxr = -2;
	int ai = N;
	for (int i = 1; i <= n; i++) {
		int cur = 0; // 7 2 3 1 7 4 5 6
		int mx = 0;
		for (int j = i+1; j <= n; j++) {
			mx = max(mx, p[j]);
			//cout << mx << " " << rec[j] << endl;
			if (mx == p[j] && !rec[j] && p[j] > d[i-1]) {
				cur++;
			}
		}
		//cout << cur << endl;
		if (cur == mxr && ai > p[i]) {
			ai = p[i];
		}
		else if (cur > mxr) {
			mxr = cur;
			ai = p[i];
		}
	}
	cout << ai << endl;
    return 0;
}


#include <bits/stdc++.h>

#define fst first
#define snd second
#define N 212345

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;

int n, x, fmx, smx, d[N], res = 1;

int main()
{
	cin >> n;
	for(int i = 1; i <= n; i++){
	    scanf("%d", &x);
	    if(x > fmx){
	        smx = fmx; fmx = x;
	        d[x] = 1;
	    }
	    else if(x > smx){
	        smx = x;
	        d[fmx]--;
	    }
	}
	for(int i = 1; i <= n; i++){
	    if(d[i] < d[res]) res = i;
	}
	cout << res << endl;
    return 0;
}


#include <bits/stdc++.h>

#define fst first
#define snd second
#define N 212345

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;

int n, p[N], d[N];
bool rec[N];

int main()
{
	cin >> n;
	int mx = 0;
	int recs = 0;
	for (int i = 1; i <= n; i++) {
		scanf("%d", &p[i]);
		mx = (mx, p[i]);
		if (p[i] == mx)
			rec[i] = true;
	}
	if (n == 1) {
		cout << "1" << endl;
		return 0;
	}
	int mxr = -2;
	int ai = N;
	for (int i = 1; i <= n; i++) {
		int cur = 0; // 7 2 3 1 7 4 5 6
		int mx = 0;
		for (int j = 1; j <= n; j++) {
			if (j != i) {
				mx = max(mx, p[j]);
				if (mx == p[j]) {
					cur++;
				}
			}
		}
		//cout << cur << endl;
		if (cur == mxr && ai > p[i]) {
			ai = p[i];
		}
		else if (cur > mxr) {
			mxr = cur;
			ai = p[i];
		}
	}
	cout << ai << endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define mod 1000000007
int a[100005];
int main(){
	int n,flag;
	cin>>n;
	for(int i=0;i<n;i++) scanf("%d",&a[i]);
	if(n==1) cout<<a[0]<<endl;
	else{
	for(int i=1;i<n;i++){
		if(a[i]<a[i-1]){
			flag=a[i-1];
			break;
		}
	}
	cout<<flag<<endl;
}
}


#include<bits/stdc++.h>
using namespace std;
#define mod 1000000007
int a[100005];
int maxx[100005];
int vis[100005];
int main(){
	int n,flag;
	cin>>n;
	int mas=-1111;
	int ans=-1111;
	for(int i=0;i<n;i++){
	 scanf("%d",&a[i]);
	 mas=max(mas,a[i]);
	 maxx[mas]++;
	}
	for(int i=0;i<100005;i++) { if(ans<maxx[i]){ ans=maxx[i]; flag=i;} }
	cout<<flag<<endl;

}


#include<bits/stdc++.h>
using namespace std;
int cnt[100005];
int main(){
	int max1=0,max2=0,n,i,p;
	scanf("%d",&n);
	max1=0,max2=0;
	for(i=0;i<n;i++){
		scanf("%d",&p);
		if(p>max1){
			cnt[p]--;
			max2=max1;
			max1=p;
		}else{
			if(p>max2){
				cnt[max1]++;
				max2=p;
			}else{
				continue;
			}
		}
	}
	int ans=1,num=cnt[1];
	for(i=2;i<=n;i++){
		if(cnt[i]>num){
			ans=i;
			num=cnt[i];
		}
	}
	printf("%d\n",ans);
}


#include <iostream>

using namespace std;
int n,v[200003],t[200003],mare,k,act,usu,sol;
int main()
{
    cin>>n;
    for(int i=1;i<=n;++i)
    {
        cin>>v[i];
        if(v[i]>mare)
        {
            mare=v[i];
            t[++k]=i;
        }
    }
    sol=k;
    t[++k]=n+1;
    mare=0;
    if(n==1)
    {
        cout<<1;
        return 0;
    }
    for(int i=1;i<=k;++i)
    {
        act=0;
        for(int j=t[i]+1;j<t[i+1];++j)
        {
            if(v[j]>mare)
            {
                ++act;
                mare=v[j];
            }
        }
        mare=v[t[i]];
        if(act>usu)
        {
            usu=act;
            sol=v[t[i]];
        }
    }
    cout<<sol;
    return 0;
}


#include <iostream>

using namespace std;
int n,v[100003],m1,m2,sol,nr,act;
int main()
{
    cin>>n;
    for(int i=1;i<=n;++i)
    {
        cin>>v[i];
    }
    if(n==1)
    {
        cout<<1;
        return 0;
    }
    m1=v[1];
    sol=m1;
    nr=0;
    act=0;
    for(int i=2;i<=n;++i)
    {
        if(v[i]<m1&&v[i]<m2) continue;
        if(v[i]<m1&&v[i]>m2)
        {
            m2=v[i];
            ++act;
            continue;
        }
        if(v[i]>m1)
        {
            if(act>nr)
            {
                nr=act;
                sol=m1;
            }
            act=0;
            m2=m1;
            m1=v[i];
        }
    }
    cout<<sol;
    return 0;
}


#include <iostream>

using namespace std;
int n,v[100003],m1,m2,sol,nr,act;
int main()
{
    cin>>n;
    for(int i=1;i<=n;++i)
    {
        cin>>v[i];
    }
    if(n==1)
    {
        cout<<1;
        return 0;
    }
    m1=v[1];
    sol=m1;
    nr=0;
    act=0;
    for(int i=2;i<=n;++i)
    {
        if(v[i]<m1&&v[i]<m2) continue;
        if(v[i]<m1&&v[i]>m2)
        {
            m2=v[i];
            ++act;
            continue;
        }
        if(v[i]>m1)
        {
            if(act>nr)
            {
                nr=act;
                sol=m1;
            }
            act=0;
            m2=m1;
            m1=v[i];
        }
    }
    for(int i=1;i<n;++i)
    {
        if(sol==n&&v[i]==sol&&v[i+1]==n-1)
        {
            sol=n-1;
            break;
        }
    }
    cout<<sol;
    return 0;
}