Yet another education system reform has been carried out in Berland recently. The innovations are as follows:An academic year now consists of n days. Each day pupils study exactly one of m subjects, besides, each subject is studied for no more than one day. After the lessons of the i-th subject pupils get the home task that contains no less than ai and no more than bi exercises. Besides, each subject has a special attribute, the complexity (ci). A school can make its own timetable, considering the following conditions are satisfied:  the timetable should contain the subjects in the order of the complexity's strict increasing;  each day, except for the first one, the task should contain either k times more exercises, or more by k compared to the previous day (more formally: let's call the number of home task exercises in the i-th day as xi, then for each i (1 < i ≤ n): either xi = k + xi - 1 or xi = k·xi - 1 must be true);  the total number of exercises in all home tasks should be maximal possible. All limitations are separately set for each school.It turned out that in many cases ai and bi reach 1016 (however, as the Berland Minister of Education is famous for his love to half-measures, the value of bi - ai doesn't exceed 100). That also happened in the Berland School №256. Nevertheless, you as the school's principal still have to work out the timetable for the next academic year...

The first line contains three integers n, m, k (1 ≤ n ≤ m ≤ 50, 1 ≤ k ≤ 100) which represent the number of days in an academic year, the number of subjects and the k parameter correspondingly. Each of the following m lines contains the description of a subject as three integers ai, bi, ci (1 ≤ ai ≤ bi ≤ 1016, bi - ai ≤ 100, 1 ≤ ci ≤ 100) — two limitations to the number of exercises on the i-th subject and the complexity of the i-th subject, correspondingly. Distinct subjects can have the same complexity. The subjects are numbered with integers from 1 to m.  Please do not use the %lld specificator to read or write 64-bit numbers in С++. It is preferred to use the cin stream or the %I64d specificator.

If no valid solution exists, print the single word "NO" (without the quotes). Otherwise, the first line should contain the word "YES" (without the quotes) and the next n lines should contain any timetable that satisfies all the conditions. The i + 1-th line should contain two positive integers: the number of the subject to study on the i-th day and the number of home task exercises given for this subject. The timetable should contain exactly n subjects.

#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <cstring>
#include <list>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <numeric>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <iterator>
#include <complex>
#include <stack>
#include <queue>
#include <ctime>
using namespace std;
static const double EPS = 1e-8;
typedef long long ll;
typedef unsigned long long ull;
typedef complex <double> pt;
typedef complex <ll> pti;

int main() {
	//ifstream fin("input.txt", ios::in);
	//ofstream fout("output.txt", ios::out);
	//FILE *fpin=fopen("input.txt", "r");
	//FILE *fpout=fopen("output.txt", "w");
	istream &fin=cin;
	ostream &fout=cout;
	FILE *fpin=stdin;
	FILE *fpout=stdout;

	int n, m, k;
	fin >> n >> m >> k;
	vector <ll> a(m), b(m);
	vector <int> c(m);
	for(int i=0; i<m; i++) {
		fin >> a[i] >> b[i] >> c[i];
	}

	vector <vector <ll> > vvl(m, vector <ll>(4));
	for(int i=0; i<m; i++) {
		vvl[i][0]=c[i];
		vvl[i][1]=a[i];
		vvl[i][2]=b[i];
		vvl[i][3]=i+1;
	}
	sort(vvl.begin(), vvl.end());

	ll maxval=-1;
	ll dp[102][102][102];
	memset(dp, -1, sizeof(dp));
	int prev[102][102][102][3];
	for(int d=0; d<m; d++) {
		vector <ll> &vl=vvl[d];
		for(int diff=0; diff<=vl[2]-vl[1]; diff++) {
			ll curval=vl[1]+diff;
			if(dp[d][diff][1]==-1) dp[d][diff][1]=curval;
			for(int num=1; num<=d+1; num++) {
				if(dp[d][diff][num]==-1) continue;
				if(num==n) maxval=max(maxval, dp[d][diff][num]);
				for(int nd=d+1; nd<m; nd++) {
					vector <ll> &vl2=vvl[nd];
					if(vl2[0]==vl[0]) continue;
					if(curval+k>=vl2[1] && curval+k<=vl2[2]) {
						ll ndiff=curval+k-vl2[1];
						ll nval=vl2[1]+ndiff;
						if(dp[nd][ndiff][num+1]<dp[d][diff][num]+nval) {
							dp[nd][ndiff][num+1]=dp[d][diff][num]+nval;
							prev[nd][ndiff][num+1][0]=d;
							prev[nd][ndiff][num+1][1]=diff;
							prev[nd][ndiff][num+1][2]=num;
						}
					}
					if(curval*k>=vl2[1] && curval*k<=vl2[2]) {
						ll ndiff=curval*k-vl2[1];
						ll nval=vl2[1]+ndiff;
						if(dp[nd][ndiff][num+1]<dp[d][diff][num]+nval) {
							dp[nd][ndiff][num+1]=dp[d][diff][num]+nval;
							prev[nd][ndiff][num+1][0]=d;
							prev[nd][ndiff][num+1][1]=diff;
							prev[nd][ndiff][num+1][2]=num;
						}
					}
				}
			}
		}
	}

	if(maxval==-1) {
		fout << "NO" << endl;
		return 0;
	}

	for(int d=0; d<m; d++) {
		vector <ll> vl=vvl[d];
		for(int diff=0; diff<=vl[2]-vl[1]; diff++) {
			if(dp[d][diff][n]==maxval) {
				fout << "YES" << endl;
				vector <vector <ll> > vvl2(n);
				int num=n;
				for(int j=0; j<n; j++) {
					vvl2[j].push_back(vl[3]);
					vvl2[j].push_back(vl[1]+diff);
					int pd=prev[d][diff][num][0];
					int pdiff=prev[d][diff][num][1];
					int pnum=prev[d][diff][num][2];
					d=pd; diff=pdiff; num=pnum;
					if(d>=0) vl=vvl[d];
				}
				reverse(vvl2.begin(), vvl2.end());
				for(int j=0; j<n; j++) {
					fout << vvl2[j][0] << " " << vvl2[j][1] << endl;
				}
				return 0;
			}
		}
	}

}

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

using namespace std;

int n , m ;
long long k ;
long long dp[60][60][200] ;
pair <int , int> par[60][60][200];

struct state
{
    long long l , r , c , id;
    state () {};
} a[100];

bool cmp(state i , state j)
{
    return i.c < j.c;
};

int main()
{
scanf("%d%d%I64d",&n,&m,&k);
for (int i = 1 ; i <= m ; i ++ )
{
    scanf("%I64d %I64d %I64d",&a[i].l,&a[i].r,&a[i].c);
    a[i].id = i;
};
sort(a + 1 , a + m + 1 , cmp);
for (int i = 1 ; i <= m ; i ++ )
    for (int dt = 0 ; dt <= a[i].r - a[i].l ; dt ++ )
        dp[1][i][dt] = dt + a[i].l;
for (int i = 2 ; i <= n ; i ++ )
    for (int j = 1 ; j <= m ; j ++ )
        for (int dt = 0 ; dt <= a[j].r - a[j].l ; dt ++ )
        {
            dp[i][j][dt] = -2e18;
            for (int p = j - 1 ; p >= 1 ; --p )
            {
                if ( a[p].c == a[j].c )
                    continue;
                long long cur_ex = a[j].l + dt;
                if ( a[p].l <= cur_ex - k && cur_ex - k <= a[p].r && dp[i - 1][p][cur_ex - k - a[p].l] + cur_ex > dp[i][j][dt] )
                {
                    dp[i][j][dt] = dp[i - 1][p][cur_ex - k - a[p].l] + cur_ex;
                    par[i][j][dt] = make_pair(p , 0);
                };
                if ( cur_ex % k == 0 && a[p].l <= cur_ex / k && cur_ex / k <= a[p].r && dp[i - 1][p][cur_ex / k - a[p].l] + cur_ex > dp[i][j][dt] )
                {
                    dp[i][j][dt] = dp[i - 1][p][cur_ex / k - a[p].l] + cur_ex;
                    par[i][j][dt] = make_pair(p , 1);
                };
            };
        };
long long answer = 0;
int start = -1 ;
long long d = -1 ;
for (int i = 1 ; i <= m ; i ++ )
    for (int dt = 0 ; dt <= a[i].r - a[i].l ; dt ++ )
        if ( dp[n][i][dt] > answer )
        {
            answer = dp[n][i][dt];
            start = i;
            d = dt;
        };

if ( !answer )
    printf("NO\n");
else
{
    printf("YES\n");
    vector <pair<int , long long> > Ans;
    for (int i = n ; i >= 1 ; i -- )
    {
        Ans.push_back(make_pair(a[start].id , a[start].l + d) );
        int new_v = par[i][start][d].first;
        if ( par[i][start][d].second == 0 )
            d = (a[start].l + d - k) - a[new_v].l;
        else
            d = ((a[start].l + d) / k) - a[new_v].l;
        start = new_v;
    };
    for (int i = Ans.size() - 1 ; i >= 0 ; i -- )
        printf("%d %I64d\n",Ans[i].first,Ans[i].second);
};
return 0;
}

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

using namespace std;

typedef long long ll;

struct prze
{
    ll tr, a, b, nr;
};

struct pop
{
    int j, l;
};

ll dp[50][50][101];
pop ost[50][50][101];

bool por(prze a, prze b)
{
    if (a.tr < b.tr)
        return true;
    if (a.tr > b.tr)
        return false;
    return (a.nr < b.nr);
}

int main()
{
    ios_base::sync_with_stdio(0);
    int n, m, k;
    cin >> n >> m >> k;
    vector<prze> p(m);
    for (int i = 0; i < m; ++i)
    {
        cin >> p[i].a >> p[i].b >> p[i].tr;
        p[i].nr = i;
    }
    sort(p.begin(), p.end(), por);
    for (int i = 0; i < 50; ++i)
    {
        for (int j = 0; j < 50; ++j)
        {
            for (int l = 0; l < 101; ++l)
            {
                dp[i][j][l] = -1;
                ost[i][j][l] = {-1, -1};
            }
        }
    }
    for (int i = 0; i < m; ++i)
        for (ll j = p[i].a; j <= p[i].b; ++j)
            dp[0][i][j - p[i].a] = j;
    for (int i = 1; i < n; ++i)
    {
        for (int j = 0; j < m; ++j)
        {
            for (ll l = p[j].a; l <= p[j].b; ++l)
            {
                ll r = l - p[j].a;
                for (int o = 0; o < j; ++o)
                {
                    if (p[o].tr >= p[j].tr)
                        continue;
                    ll lt;
                    if (l % k == 0)
                    {
                        lt = l / k;
                        if (lt >= p[o].a && lt <= p[o].b && dp[i - 1][o][lt - p[o].a] != -1)
                        {
                            if (dp[i - 1][o][lt - p[o].a] + l > dp[i][j][r])
                            {
                                dp[i][j][r] = dp[i - 1][o][lt - p[o].a] + l;
                                ost[i][j][r] = {o, lt - p[o].a};
                            }
                        }
                    }
                    lt = l - k;
                    if (lt >= p[o].a && lt <= p[o].b && dp[i - 1][o][lt - p[o].a] != -1)
                    {
                        if (dp[i - 1][o][lt - p[o].a] + l > dp[i][j][r])
                        {
                            dp[i][j][r] = dp[i - 1][o][lt - p[o].a] + l;
                            ost[i][j][r] = {o, lt - p[o].a};
                        }
                    }
                }
            }
        }
    }
    ll wyn = -1;
    int a = n - 1, b, c;
    for (int i = 0; i < m; ++i)
    {
        for (ll j = 0; j <= p[i].b - p[i].a; ++j)
        {
            if (dp[n - 1][i][j] > wyn)
            {
                wyn = dp[n - 1][i][j];
                b = i;
                c = j;
            }
        }
    }
    if (wyn == -1)
    {
        cout << "NO";
        return 0;
    }
    cout << "YES" << endl;
    vector<ll> x, y;
    while (a != -1)
    {
        x.push_back(p[b].nr);
        y.push_back(c + p[b].a);
        int nowy_a = a - 1;
        int nowy_b = ost[a][b][c].j;
        int nowy_c = ost[a][b][c].l;
        a = nowy_a;
        b = nowy_b;
        c = nowy_c;
    }
    for (int i = n - 1; i >= 0; --i)
        cout << x[i] + 1 << " " << y[i] << endl;
    return 0;
}

# include <iostream>
# include <string>
# include <vector>
# include <math.h>
# include <algorithm>
# include <string.h>
# include <stack>
# include <queue>
# include <sstream>
# include <set>
# include <map>
# include <stdio.h>
using namespace std;
int main()
{
    long long n, m, kkk;
    cin >> n >> m >> kkk;
    vector<pair<long long, pair<long long, long long> > >a(m), b;
    for (long long i = 0; i < m; i++)
    {
        cin >> a[i].second.first >> a[i].second.second >> a[i].first;
    }
    b = a;
    sort(a.begin(), a.end());
    vector<long long> or_ind(m);
    vector<bool> used(m, false);
    for (long long i = 0; i < m; i++)
    {
        for (long long j = 0; j < m; j++)
            if (!used[j] && a[i] == b[j])
            {
                         or_ind[i] = j;
                         used[j] = true;
            }
    }
    map<long long, long long> f[55][55];
    map<long long, pair<long long, long long> > prev[55][55];
    for (long long i = 0; i < m; i++)
    {
        for (long long j = a[i].second.first; j <= a[i].second.second; j++)
        {
            f[0][i][j] = j;
            prev[0][i][j] = make_pair(-1, -1);
        }
    }
    for (long long i = 1; i < n; i++)
    {
        for (long long j = i; j < m; j++)
        {
            for (long long op = a[j].second.first; op <= a[j].second.second; op++)
                for (long long k = i - 1; k < j; k++)
                {
                    if (a[k].first == a[j].first)
                       break;
                    long long temp = f[i - 1][k][op - kkk];
                    if (temp && temp + op > f[i][j][op])
                    {
                       f[i][j][op] = temp + op;
                       prev[i][j][op] = make_pair(k, op - kkk);
                    }
                    if (op % kkk == 0)
                    {
                        long long we = op / kkk;
                        temp = f[i - 1][k][we];
                        if (temp && temp + op > f[i][j][op])
                        {
                           f[i][j][op] = temp + op;
                           prev[i][j][op] = make_pair(k, we);
                        }
                    }
                }
        }
    }
    long long ma = -1;
    for (long long i = 0; i < m; i++)
    {
        for (map<long long, long long>::iterator it = f[n - 1][i].begin(); it != f[n - 1][i].end(); it++)
        {
            ma = max(ma, (*it).second);
        }
    }
    for (long long i = 0; i < m; i++)
    {
        for (map<long long, long long>::iterator it = f[n - 1][i].begin(); it != f[n - 1][i].end(); it++)
        {
            if ((*it).second != ma)
               continue;
            long long f = (*it).first;
            vector<pair<long long, long long> > ans;
            long long num = f, cur = i;
            long long ind = n - 1;
            while (!(num == -1 && cur == -1))
            {
                  ans.push_back(make_pair(cur, num));
                  pair<long long, long long> t = prev[ind--][cur][num];
                  cur = t.first;
                  num = t.second;
            }
            reverse(ans.begin(), ans.end());
            cout << "YES" << endl;
            for (long long j = 0; j < ans.size(); j++)
            {
                cout << or_ind[ans[j].first] + 1 << " " << ans[j].second << endl;
            }
            //system("pause");
            return 0;
        }
    }
    cout << "NO";
    //system("pause");
    return 0;
}

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

#define pb push_back
#define For(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += ((begin) > (end)?-1:1))
#define all( v ) (v).begin(),(v).end()
template<typename T> using V = vector<T>;
typedef long long int ll;
typedef unsigned long long int ull;
/***********************************************/
/*      ____________
 *     /            \
 *    /  /\      /\  \
 *   /  /  \    /  \  \
 *   \                /
 *    \     \___/    /
 *     \____________/
 */
const ll mod = 1000000007;
struct sb{
	size_t c,ind;
	ull a,b;
};
bool cmp(sb a,sb b){
	return a.c < b.c;
}
struct s{
	ull sub;
	size_t lastsub;
	int before;
	bool can;
}DP [51][101][51];

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	size_t N,M;
	ull K;
	cin>>N>>M>>K;
	V<sb> subs(M);
	For(i,0,M)cin>>subs[i].a>>subs[i].b>>subs[i].c,subs[i].ind = i;
	sort(all(subs),cmp);

	pair<size_t,ull> ans;

	For(i,0,M){
		For(j,subs[i].a,subs[i].b+1){
			size_t l = j - subs[i].a;
			DP[i][l][1].sub = j;
			DP[i][l][1].lastsub = 0;
			DP[i][l][1].before = -1;
			DP[i][l][1].can = true;
			For(k,2,N+1){
				DP[i][l][k].sub = 0;
				DP[i][l][k].lastsub = 0;
				DP[i][l][k].before = -1;
				DP[i][l][k].can = false;
				For(z,i,0){
					if(subs[z].c == subs[i].c)
						continue;
					if(j%K == 0 && j/K >= subs[z].a && (j/K-subs[z].a) <= subs[z].b - subs[z].a){
						if(DP[z][j/K - subs[z].a][k-1].can && DP[z][j/K - subs[z].a][k-1].sub + j >= DP[i][l][k].sub){
							DP[i][l][k].sub = DP[z][j/K - subs[z].a][k-1].sub + j;
							DP[i][l][k].lastsub = j/K - subs[z].a;
							DP[i][l][k].can = true;
							DP[i][l][k].before = z;
						}
					}
					if(j >= K + subs[z].a && (j - K - subs[z].a) <= subs[z].b - subs[z].a){
						if(DP[z][j - K - subs[z].a][k-1].can && DP[z][j - K - subs[z].a][k-1].sub + j >= DP[i][l][k].sub){
							DP[i][l][k].sub = DP[z][j - K - subs[z].a][k-1].sub + j;
							DP[i][l][k].lastsub = j - K - subs[z].a;
							DP[i][l][k].can = true;
							DP[i][l][k].before = z;
						}
					}
				}
				//cout<<i<<' '<<l<<' '<<k<<":\n";
				//cout<<boolalpha<<DP[i][l][k].can<<' ';
				//cout<<DP[i][l][k].sub<<' '<<DP[i][l][k].before<<' '<<DP[i][l][k].lastsub<<endl;
			}
			if(DP[i][l][N].can && DP[i][l][N].sub >= DP[ans.first][ans.second][N].sub)
				ans = {i,l};
		}
	}

	//cout<<ans.first<<' '<<ans.second<<' '<<DP[ans.first][ans.second][N].sub<<endl;

	if(!DP[ans.first][ans.second][N].can){
		cout<<"NO"<<endl;
		return 0;
	}
	cout<<"YES"<<endl;

	V<pair<size_t,ull> > res;
	For(i,N+1,1){
		res.pb({subs[ans.first].ind+1,subs[ans.first].a + ans.second});
		ans = {DP[ans.first][ans.second][i].before,DP[ans.first][ans.second][i].lastsub};
	}

	For(i,N,0)cout<<res[i].first<<' '<<res[i].second<<endl;
	return 0;
}
/**/

#include<bits/stdc++.h>
using namespace std;
struct subject
{
    long long a,b,c,idx;
};
subject ar[51];
long long dp[51][51][101];  //dp[i][j][k]= max no of exercises if i study the ith subject on the jth day such that x[i]=a[i]+k
pair<long long,long long> v[51][51][101];
stack<pair<long long,long long> >st;
bool cmp(subject x,subject y)
{
    if(x.c!=y.c)
    return x.c<y.c;
    else
    return x.a<y.a;
}
int main()
{
    long long int n,m,kk;
    cin>>n>>m>>kk;
    for(int i=1;i<=m;i++)
    {
        cin>>ar[i].a>>ar[i].b>>ar[i].c;
        ar[i].idx=i;
    }
    sort(ar+1,ar+m+1,cmp);
    for(int i=1;i<=m;i++)
    {
        for(int k=0;k<=ar[i].b-ar[i].a;k++)
        dp[i][1][k]=ar[i].a+k;
    }
    for(int j=2;j<=n;j++)
    {
        for(int i=j;i<=m;i++)
        {
            for(long long int k=0;k<=ar[i].b-ar[i].a;k++)
            {
                    for(int l=j-1;l<i;l++)
                    {
                        if(ar[l].a<=ar[i].a+k-kk && ar[i].a+k-kk<=ar[l].b && ar[i].c>ar[l].c)
                        {
                            long long int z=(ar[i].a+k-kk)-ar[l].a;
                            if(dp[l][j-1][z]>0 && dp[i][j][k]<=dp[l][j-1][z]+ar[i].a+k)
                            {
                                dp[i][j][k]=dp[l][j-1][z]+ar[i].a+k;
                                v[i][j][k]=make_pair(l,z);
                            }
                        }
                        if((ar[i].a+k)%kk==0 && ar[l].a<=(ar[i].a+k)/kk && (ar[i].a+k)/kk<=ar[l].b && ar[i].c>ar[l].c)
                        {
                            long long int z=(ar[i].a+k)/kk-ar[l].a;
                            if(dp[l][j-1][z]>0 && dp[i][j][k]<=dp[l][j-1][z]+ar[i].a+k)
                            {
                                dp[i][j][k]=dp[l][j-1][z]+ar[i].a+k;
                                v[i][j][k]=make_pair(l,z);
                            }
                        }
                    }
            }
        }
        long long ma=0;
        for(int i=j;i<=m;i++)
        {
            for(int k=0;k<=ar[i].b-ar[i].a;k++)
            ma=max(ma,dp[i][j][k]);
        }
        if(ma==0)
        {
            cout<<"NO\n";
            return 0;
        }
    }
    long long ans=0;
    long long int x=n,y,z,c=n;
    for(int i=1;i<=m;i++)
        {
            for(int k=0;k<=ar[i].b-ar[i].a;k++)
            {
                if(ans<dp[i][c][k])
                {
                    ans=dp[i][c][k];
                    x=i;
                    z=k;
                }
            }
        }
        cout<<"YES"<<endl;
    for(int i=c;i>=1;i--)
    {
        st.push(make_pair(ar[x].idx,ar[x].a+z));
        int abc=x;
        x=v[x][i][z].first;
        z=v[abc][i][z].second;
    }
    while(!st.empty())
    {
        cout<<st.top().first<<" ";
        cout<<st.top().second<<endl;
        st.pop();
    }
}

#include<stdio.h>
#include<conio.h>
const long maxN=55;
struct subject{long long a,b,c,cs;};
long long f[maxN][maxN][maxN*2],sl[maxN];
long n,m,k,d[maxN];
subject t[maxN];
int nhap();
int xuli();
int inkq();
int main()
{
    nhap();
    xuli();
    inkq();
}
int nhap()
{
    scanf("%d%d%d",&n,&m,&k);
    long i,j;
    subject tg;
    for (i=1;i<=m;i++)
    {
       scanf("%I64d%I64d%d",&t[i].a,&t[i].b,&t[i].c);
       t[i].cs=i;
    }
    for (i=m;i>=2;i--)
    for (j=1;j<=i-1;j++)
    if (t[j].c>t[j+1].c)
    {
         tg=t[j];t[j]=t[j+1];t[j+1]=tg;
    }
}
long long max(long long a,long long b)
{
     if (a>b) return a; else return b;
 }
int xuli()
{
    long i,j,p,q;
    long long m1,m2;
    for (i=1;i<=n;i++)
    for (p=i;p<=m;p++)
    for (j=0;j<=t[p].b-t[p].a;j++)
    {
        if (i==1)
        {
            f[i][p][j]=j+t[p].a;
        }
        else
        for (q=1;q<p;q++)
        if (t[q].c<t[p].c)
        {
            m1=j+t[p].a-k-t[q].a;
            if ((j+t[p].a)%k==0) m2=(j+t[p].a)/k-t[q].a;else m2=-1;
            if ((m1>=0)&&(m1<=t[q].b-t[q].a)&&(f[i-1][q][m1]>0)) f[i][p][j]=max(f[i][p][j],f[i-1][q][m1]+j+t[p].a);
            if ((m2>=0)&&(m2<=t[q].b-t[q].a)&&(f[i-1][q][m2]>0)) f[i][p][j]=max(f[i][p][j],f[i-1][q][m2]+j+t[p].a);
        }
    }
}
int landg(long n,long m,long long x,long long kq)
{
    if (n==0) return 0;
    long p,j;
    for (p=1;p<=m;p++)
    for (j=0;j<=t[p].b-t[p].a;j++)
    {
        if ((j+t[p].a+k==x)||((j+t[p].a)*k==x))
        if (f[n][p][j]==kq)
        {
            d[n]=t[p].cs;
            sl[n]=j+t[p].a;
            landg(n-1,p-1,j+t[p].a,kq-j-t[p].a);
            return 0;
        }
    }
}
int inkq()
{
    long i,j,p;
    long long kq=0;
    for (p=1;p<=m;p++)
    for (j=0;j<=t[p].b-t[p].a;j++)
    if (f[n][p][j]>kq) kq=f[n][p][j];
    if (kq==0)
    {
       printf("NO");
       return 0;
    }
    printf("YES\n");
    for (p=1;p<=m;p++)
    for (j=0;j<=t[p].b-t[p].a;j++)
    if (f[n][p][j]==kq)
    {
         d[n]=t[p].cs;
         sl[n]=j+t[p].a;
         landg(n-1,p-1,j+t[p].a,kq-j-t[p].a);
    }
    for (i=1;i<=n;i++)
    printf("%d %I64d\n",d[i],sl[i]);
 //   getch();
}

// Friendship is Magic
#pragma comment(linker, "/STACK:32000000")

#include <memory.h>
#include <string.h>
#include <cstdio>
#include <cmath>
#include <iostream>
#include <sstream>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>

using namespace std;

typedef long long LL;

struct subj
{
    LL a, b;
    int c;
    int num;
    friend bool operator < (const subj &x, const subj &y)
    {
        return x.c < y.c;
    }
};

int n, m, K;
subj a[55];
int diff[55];
LL dp[55][55][105];
int from_cnt[55][55][105];
int from_subj[55][55][105];

void test(int i, int j, int k, LL val, int from_k, int from_j)
{
    if (dp[i][j][k] >= val) return;
    dp[i][j][k] = val;
    from_cnt[i][j][k] = from_k;
    from_subj[i][j][k] = from_j;
}

void do_solve() {
    cin >> n >> m >> K;
    for (int i = 0; i < m; i++)
    {
        cin >> a[i].a >> a[i].b >> a[i].c;
        a[i].num = i+1;
    }
    sort(a, a+m);
    memset(dp, -1, sizeof(dp));
    for (int i = 0; i < m; i++)
        diff[i] = a[i].b - a[i].a;
    for (int i = 0; i < m; i++)
        for (int j = 0; j <= diff[i]; j++)
            dp[0][i][j] = a[i].a + j;

    for (int i = 0; i + 1 < n; i++)
        for (int j = 0; j < m; j++)
            for (int k = 0; k <= diff[j]; k++)
                if (dp[i][j][k] >= 0)
                {
                    for (int z = j + 1; z < m; z++) if (a[z].c > a[j].c)
                    {
                        // +k
                        LL x = a[j].a + k + K;
                        if (x >= a[z].a && x <= a[z].b)
                        {
                            test(i+1, z, x - a[z].a, dp[i][j][k] + x, k, j);
                        }
                        // *k
                        x = (a[j].a + k) * K;
                        if (x >= a[z].a && x <= a[z].b)
                        {
                            test(i+1, z, x - a[z].a, dp[i][j][k] + x, k, j);
                        }
                    }
                }
    LL best = -1;
    int lastsubj = -1, lastdiff;
    for (int i = 0; i < m; i++)
        for (int j = 0; j <= diff[i]; j++)
            if (dp[n-1][i][j] > best)
            {
                best = dp[n-1][i][j];
                lastsubj = i;
                lastdiff = j;
            }
    if (lastsubj == -1)
    {
        cout << "NO";
        return;
    }
    cout << "YES" << endl;
    vector<LL> cts;
    vector<int> subjs;
    for (int i = n-1; i >= 0; i--)
    {
        cts.push_back(a[lastsubj].a + lastdiff);
        subjs.push_back(lastsubj);
        int t = from_subj[i][lastsubj][lastdiff];
        lastdiff = from_cnt[i][lastsubj][lastdiff];
        lastsubj = t;
    }
    for (int i = n-1; i >= 0; i--) cout << a[subjs[i]].num << " " << cts[i] << endl;
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    do_solve();
    return 0;
}

#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

// ----------------------------------------Defines and Template-------------------------------------//
#define int long long
#define ll long long
#define double long double
#define f first
#define ss second
#define mp make_pair
#define pb push_back
#define all(v) v.begin(),v.end()
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vi vector<int>
#define vii vector<vi>
#define vl vector<ll>
#define vpii vector<pii>
#define vpll vector<pll>
#define print(arr) for (auto it = arr.begin(); it != arr.end(); ++it) cout << *it << " "; cout << endl;

#define min3(a, b, c) min(a, min(b, c))
#define max3(a, b, c) max(a, max(b, c))
#define alla(a, n) a, a + n
#define gcd(a, b) __gcd(a, b)
#define lcm(a, b) (a * b)/gcd(a, b)

typedef tree<
int,
null_type,
less<int>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
//order_of_key (k) : Number of items strictly smaller than k .
//find_by_order(k) : K-th element in a set (counting from zero).

#define setitr set <int, greater <int> > :: iterator
#define RE(i,n) for(int i=1; i<=n; i++)
#define REP(i,j,n) for(int i=j; i<=n; i++)
#define REB(i,j,n) for(int i=j; i>=n; i--)
#define FO(i,n) for(int i=n; i>0; i--)

const int mod = 1e9+7;
const int INF = 1e18;
const int LGN = 30;

//-------------------------------------------------Template Ends------------------------------------------------------/

int dp[52][52][103],n,m,k;  //exercise
pii up[52][52][103]; //subject, ind
int a[53], b[54], c[54], in[54];
//dp[i][j][k] - Answer from 1 to i ending with subject j on day i doing total of a[j]+k exercises.

void soln(int i, int j, int k)
{
    if(i==0)
    {
        return;
    }
    soln(i-1,up[i][j][k].f, up[i][j][k].ss);
    cout << in[j] << " " << a[j]+k << "\n";
}

void solve_case()
{
    int i,j,t,k1;
    cin >> n >> m >> k1;
    vector<pair<pair<int,int>, pair<int, int> > > arr;
    RE(tt, m)
    {
        cin >> i >> j >> t;
        arr.pb(mp(mp(t,tt),mp(i,j)));
    }
    sort(all(arr));
    REP(i,0,m-1)
    {
        a[i+1] = arr[i].ss.f;
        b[i+1] = arr[i].ss.ss;
        c[i+1] = arr[i].f.f;
        in[i+1] = arr[i].f.ss; //Order has to be retained for output :(
    }
    REP(i,0,n) REP(j,0,m) REP(k,0,100) { dp[i][j][k] = -INF; up[i][j][k] = mp(-1,-1);}
    for(i=1; i<=m; i++)
    {
        for(j=0; j+a[i]<=b[i]; j++)
        {
            dp[1][i][j] = j+a[i];
            up[1][i][j] = mp(i,j);
        }
    }
    for(i=2; i<=n; i++)
    {
        for(j=1; j<=m; j++)
        {
            for(k=0; k+a[j]<=b[j]; k++)
            {

                for(int l=1; l<j; l++)
                {
                    int pos1 = k+a[j] - k1;     //type 1: prev+k
                    int pos2 = k+a[j];         //type 2: prev*k
                    if(c[l]==c[j]) continue;  //complexity strictly increasing.
                    //dont be a stupid fool and know the diff b/w i and j.
                    if(pos1>=a[l] && pos1<=b[l])
                    {
                        if((dp[i-1][l][pos1-a[l]]+a[j]+k) > dp[i][j][k])
                        {
                            dp[i][j][k] = dp[i-1][l][pos1-a[l]] + a[j]+k;
                            up[i][j][k] = mp(l,pos1-a[l]);
                        }
                    }
                    if(pos2%k1==0)
                    {
                        pos2 = (pos2/k1);
                        if(pos2>=a[l] && pos2<=b[l])
                        {
                            if((dp[i-1][l][pos2-a[l]]+a[j]+k) > dp[i][j][k])
                            {
                                dp[i][j][k] = dp[i-1][l][pos2-a[l]] + a[j]+k;
                                up[i][j][k] = mp(l,pos2-a[l]);
                            }
                        }
                    }
                }
            }
        }
    }

    /*
    for(i=1; i<=n; i++)
    {
        cout << i << ": \n";
        for(j=1; j<=m; j++)
        {
            cout << j << ": ";
            for(k=0; k+a[j]<=b[j]; k++)
            {
                cout << dp[i][j][k] << " ";
            }
            cout << "\n";
        }
    }
    */

    int ans = -INF;
    pii ind = mp(-1,-1);
    for(i=1; i<=m; i++)
    {
        for(j=0; j<=100; j++)
        {
            if(dp[n][i][j] > ans)
            {
                ans = dp[n][i][j];
                ind = mp(i,j);
            }
        }
    }
    if(ans<0) { cout << "NO\n"; return; }
    cout << "YES\n";
    soln(n, ind.f, ind.ss);
}

signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    //freopen("input.txt", "r", stdin);
    int Testcases = 1;
    //cin >> Testcases;
    while(Testcases--)
    {
        solve_case();
    }
    return 0;
}

#include <bits/stdc++.h>
#define X first
#define Y second

using namespace std;
using LL = long long;

const LL  IMPOSSIBLE = -1;

const int MAXN = 50;
const int MAXDIFF = 111;

LL dp[MAXN][MAXN][MAXDIFF];
int prv[MAXN][MAXN][MAXDIFF][3];

vector< pair<LL, LL> > a;
vector< pair<int, int> > c;

void printRes(int i, int j, int k) {
  if (i < 0 || j < 0 || k < 0)
    return;
  printRes(prv[i][j][k][0], prv[i][j][k][1], prv[i][j][k][2]);
  cout << c[i].Y + 1 << " " << a[c[i].Y].X + k << endl;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  memset(dp , IMPOSSIBLE, sizeof dp );
  memset(prv, IMPOSSIBLE, sizeof prv);

  int n, m, kk;
  cin >> n >> m >> kk;

  a.resize(m);
  c.resize(m);

  for (int i = 0; i < m; ++i) {
    auto& x = a[i];
    auto& y = c[i];
    cin >> x.X >> x.Y;
    x.Y -= x.X;

    cin >> y.X;
    y.Y = i;
  }

  sort(c.begin(), c.end());

  for (int i = 0; i < m; ++i) {
    auto cur = a[c[i].Y];

    for (int ad = 0; ad <= cur.Y; ++ad) {
      dp[i][0][ad] = ad + cur.X;
    }

    for (int j = 1; j < n; ++j) {
      for (int ad = 0; ad <= cur.Y; ++ad) {
        LL cval = cur.X + ad;
        LL t = cval / kk;
        LL tt = cval - kk;
        LL& curDp = dp[i][j][ad];
        int* curPrv = prv[i][j][ad];

        for (int k = 0; k < i && c[k].X < c[i].X; ++k) {
          auto pr = a[c[k].Y];

          if (cval % kk == 0 && t >= pr.X && t <= pr.X + pr.Y) {
            int it = t - pr.X;
            LL dpVal = dp[k][j - 1][it];
            if (dpVal != IMPOSSIBLE && dpVal + cval > curDp) {
              curDp = dpVal + cval;

              curPrv[0] = k;
              curPrv[1] = j - 1;
              curPrv[2] = it;
            }
          }

          if (tt >= pr.X && tt <= pr.X + pr.Y) {
            int it = tt - pr.X;
            LL dpVal = dp[k][j - 1][it];
            if (dpVal != IMPOSSIBLE && dpVal + cval > curDp) {
              curDp = dpVal + cval;

              curPrv[0] = k;
              curPrv[1] = j - 1;
              curPrv[2] = it;
            }
          }
        }
      }
    }
  }

  LL res = -1;
  int ri = 0;
  int rad = 0;

  for (int i = 0; i < m; ++i)
    for (int ad = 0; ad < MAXDIFF; ++ad)
      if (res < dp[i][n - 1][ad]) {
        res = dp[i][n - 1][ad];
        ri = i;
        rad = ad;
      }

  if (res == -1)
    cout << "NO" << endl;
  else {
    cout << "YES" << endl;
    printRes(ri, n - 1, rad);
  }
}

#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <cstring>

using namespace std;

#define F first
#define PB(v,x) v.push_back(x)
#define size(X) (int)X.size()
#define S second
#define show(X) cerr << #X << " = " << X << endl;
#define getrandom(min,max) \
    ((rand()%(int)(((max)+1)-(min)))+min)
#define foreach(var, container) \
    for(typeof((container).begin()) var = (container).begin(); \
            var != (container).end(); \
            ++var)
#define SetBit(value, place) ((value) | (1 << (place)))
#define ClearBit(value, place) ((value) & (~(1 << (place))))
#define ToggleBit(value, place) ((value) ^ (1 << (place)))
#define GetBit(value, place) (((value) >> (place)) & 1)
#define GetPrecentage(Numerator, Denominator)   ((Numerator)*100.0/(Denominator))
#define GI ({int t;scanf("%d",&t);t;})
#define GL ({int t;scanf("%I64d",&t);t;})
#define GD ({double t;scanf("%lf",&t);t;})
#define is istringstream
#define os ostringstream
#define lb lower_bound
#define ub upper_bound
#define bs binary_search
#define REP(i,n) for (int i=0,_n=(n); i < _n; i++)
#define REV(i,n) for (int i=(n)-1; i >= 0; i--)
#define FOR(i,a,b) for (int _b=(b), i=(a); i <= _b; i++)
#define ROR(i,a,b) for(int i=(a),_b=(b);i>=_b;i--)
#define ALL(c) (c).begin(), (c).end()
#define SORT(c) sort(ALL(c))
#define CLEAR(x) memset(x,0,sizeof x);
#define REVERSE(c) reverse(ALL(c))
#define UNIQUE(c) SORT(c),(c).resize(unique(ALL(c))-(c).begin())
#define INF (ll)1e17
#define eps 1.0e-11
#define PI(x) printf("%d ",x)
template <class X> void printa(X a,int n)
{
    REP(i,n) cerr << a[i] << " " ;
    cerr << endl;
}

template <class X> void printv(X v)
{
    foreach(it,v) cerr << *it << " " ;
    cerr << endl;
}
typedef pair<int,int> pii;
typedef long long ll;
typedef pair<ll,ll> pll;

const ll MAXN = 120;
ll n,m,k;
ll d[MAXN][MAXN][MAXN];
ll mark[MAXN][MAXN][MAXN];
pll parent[MAXN][MAXN][MAXN];
pll ans[MAXN];
struct sub{
    ll c,a,b,i;
    sub(){;}
    friend istream &operator >> (istream &cin , sub&x)
    {
        cin >> x.a >> x.b >> x.c;
        return cin;
    }
    bool operator < (const sub &x) const { return c < x.c; }
}subj[MAXN];

void input()
{
    ios::sync_with_stdio(false);
    cin >> n >> m >> k;
    REP(i,m)
    {
        cin >> subj[i];
        subj[i].i = i;
    }
    sort(subj,subj+m);
}

ll memz(ll i,ll j,ll t)
{

    if(mark[i][j][t])
        return d[i][j][t];
    ll q = subj[i].a + t;
    mark[i][j][t] = 1;
    if(q > subj[i].b || i<j)
    {
        d[i][j][t] = -1*INF;
        return -1*INF;
    }
    if(j==0)
    {
        d[i][j][t] = q;
        return q;
    }
    ll p1 = q;
    ll p2 = q-k;
    bool tt = true;
    REV(z,i) if(subj[z].c < subj[i].c && z>=j-1)
    {
        if(p2>=subj[z].a && p2<=subj[z].b && d[i][j][t] < memz(z,j-1,p2-subj[z].a) + q)
        {
            d[i][j][t] = memz(z,j-1,p2-subj[z].a) + q;
            parent[i][j][t].F = z;
            parent[i][j][t].S = p2-subj[z].a;
            tt = false;
        }
        if((p1/k)>=subj[z].a && (p1/k)<=subj[z].b && !(p1%k) && d[i][j][t] < memz(z,j-1,(p1/k)-subj[z].a)+q)
        {
            d[i][j][t] = memz(z,j-1,(p1/k)-subj[z].a)+q;
            parent[i][j][t].F = z;
            parent[i][j][t].S = p1/k-subj[z].a;
            tt = false;
        }
    }
    if(tt)
    {
        d[i][j][t] = -1*INF;
        return d[i][j][t];
    }
    return d[i][j][t];
}

int main()
{
    input();
    ll MAX = 0;
    ll s = -1;
    ll s2,s3;
    memset(parent,-1,sizeof parent);
    REP(i,m) REP(j,101) if(memz(i,n-1,j) > MAX)
    {
        MAX = memz(i,n-1,j);
        s = i;
        s2 = n-1;
        s3 = j;
    }
    if(MAX == 0)
    {
        cout << "NO" << endl;
        return 0;
    }
    cout << "YES" << endl;
    int sum = 0;
    while(s!=-1)
    {
        ans[sum] = pll(subj[s].i + 1,s3 + subj[s].a);
        ++sum;
        ll p = s;
        s = parent[s][s2][s3].F;
        s3 = parent[p][s2][s3].S;
        s2--;
    }
    REV(i,sum)
        cout << ans[i].F << " " << ans[i].S << endl;
    return 0;
}

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <cstring>
#include <vector>
#include <deque>
#include <cctype>
#include <algorithm>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <iomanip>
#include <sstream>
#include <cmath>
#include <bitset>
#include <unordered_map>
#include <unordered_set>
#include <functional>
#include <fstream>
#include <utility>

#define s scanf
#define p printf
#define pf push_front
#define pp push_back
#define mp make_pair
#define se second
#define fi first
#define OO 1e9
#define EPS 1e-9
#define __ 1000000007
#define V(a) vector <a>
#define P(a,b) pair <a,b>
#define II pair <int, int>
#define ALL(a) a.begin(),a.end()
#define mm(a,b) memset(a , b , sizeof a)
#define max(a,b) (((a > b)) ? (a) : (b))
#define min(a,b) (((a < b)) ? (a) : (b))
#define PI 3.14159265358979323846264338327950

typedef unsigned long long int ull;
typedef long long int ll; using namespace std;

int X[] = { 1, 0, -1, 0, 1, -1, -1, 1 };
int Y[] = { 0, 1, 0, -1, 1, 1, -1, -1 };
void MN(ll &a, ll b) { if (a > b) a = b; }
void MX(ll &a, ll b) { if (a < b) a = b; }
void MN(int &a, ll b) { if (a > b) a = b; }
void MX(int &a, ll b) { if (a < b) a = b; }
void MN(double &a, ll b) { if (a > b) a = b; }
void MX(double &a, ll b) { if (a < b) a = b; }

            /*                                Listen!                                */

ll n,m,k,c,a,b,dp[55][123][55],L_[55][123][55],W_[55][123][55],C_[55][123][55];
V(P(int,P(int,P(ll,ll))))sub; // (fi.complexity) - (se.fi.index - (se.se.fi.a - se.se.se.b))
ll solve(int last, int where, int N){
    if (N == 0) return 0;
    if (dp[last][where][N] != -1) return dp[last][where][N];
    ll LAST = sub[last].se.se.fi + where; dp[last][where][N] = -1e20;
    for (int i = last+1; i < m; i ++){
        if (sub[i].fi == sub[last].fi) continue;
        ll c1 = LAST * k , c2 = LAST + k;
        ll A = sub[i].se.se.fi , B = sub[i].se.se.se;
        if (A <= c1 && c1 <= B){
            ll rec = solve(i,c1-A,N-1);
            if (c1+rec > dp[last][where][N]){
                dp[last][where][N] = c1+rec;
                L_[last][where][N] = i;
                W_[last][where][N] = c1-A;
                C_[last][where][N] = c1;
            }
        }
        if (A <= c2 && c2 <= B){
            ll rec = solve(i,c2-A,N-1);
            if (c2+rec > dp[last][where][N]){
                dp[last][where][N] = c2+rec;
                L_[last][where][N] = i;
                W_[last][where][N] = c2-A;
                C_[last][where][N] = c2;
            }
        }
    } //cout << last << " " << where << " " << N << " " << dp[last][where][N] << endl;
    return dp[last][where][N];
}
void ________________________________(int last, int where, int N){
    if (N == 0) return;
    cout << sub[L_[last][where][N]].se.fi << " " << C_[last][where][N]; p("\n");
    ________________________________(L_[last][where][N],W_[last][where][N],N-1);
}
int main() {

    s("%d%d%d" , &n,&m,&k); mm(dp,-1);
    for (int i = 1 ; i <= m ; i ++){
        cin >> a >> b >> c;
        sub.pp(mp(c,mp(i,mp(a,b))));
    } sort(ALL(sub)); ll maxi = 0 , l , w;
    for (int i = 0 ; i < m ; i ++){
        for (int ii = 0 ; ii + sub[i].se.se.fi <= sub[i].se.se.se ; ii ++){
            ll calc = sub[i].se.se.fi + ii + solve(i,ii,n-1);
            if (calc > maxi){
                maxi = calc; l = i; w = ii;
            }
        }
    } if (maxi <= 0)p("NO\n");
    else {
        p("YES\n"); cout << sub[l].se.fi << " " << sub[l].se.se.fi + w; p("\n");
        ________________________________(l,w,n-1);
    }

	return 0;
}

#include <bits/stdc++.h>
#define int long long
#define fi first
#define se second
#define pb push_back
#define fastcin() ios_base::sync_with_stdio(false); cin.tie(0)
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef pair<int, int> ii;

int n, m, k, dp[100][100][200], cnt;
ii trace[100][100][200], res[100];
vector<int> com;

struct subject {
	int a, b, c, id;
	bool operator < (subject oth) {
		return c < oth.c;
	}
}S[100];

bool in(int x, int i) {
	return (x >= S[i].a and x <= S[i].b);
}

signed main()
{
	cin >> n >> m >> k;
	for(int i = 1, a, b, c; i <= m; i++) cin >> a >> b >> c, S[i] = {a, b, c, i};
	sort(S+1, S+m+1);
	for(int i = 0; i <= n; i++) for(int j = 1; j <= m; j++) for(int u = 0; u <= 100; u++) dp[i][j][u] = -1e18;
	for(int i = 1; i <= n; i++)
	{
		for(int j = 1; j <= m; j++)
			for(int u = 0; u <= S[j].b - S[j].a; u++)
			{
				if(i == 1)
				{
					dp[i][j][u] = u + S[j].a;
					trace[i][j][u] = {0, 0};
					continue;
				}
				for(int t = 1; t < j; t++)
				{
					if(S[t].c == S[j].c) continue;
					int d = (S[j].a + u) / k, diff = (in(S[j].a + u - k, t) ? dp[i-1][t][S[j].a + u - k - S[t].a] + u + S[j].a : -1e18),
						divs = ((S[j].a + u) % k == 0 and in(d, t) ? dp[i-1][t][d - S[t].a] + u + S[j].a : -1e18);
					if(dp[i][j][u] < diff)
						dp[i][j][u] = diff, trace[i][j][u] = {t, S[j].a + u - k - S[t].a};
					if(dp[i][j][u] < divs)
						dp[i][j][u] = divs, trace[i][j][u] = {t, d - S[t].a};
				}
			}
	}
	int ans = -1e18, sub, hw;
	for(int j = 1; j <= m; j++) for(int u = 0; u <= S[j].b - S[j].a; u++)
		if(ans < dp[n][j][u])
		{
			ans = dp[n][j][u];
			sub = j, hw = u;
		}
	if(ans <= 0) cout << "NO";
	else
	{
		int cur = n;
		while(1)
		{
 			res[cur] = {S[sub].id, hw + S[sub].a};
			if(cur == 1) break;
			int tmp = sub;
			sub = trace[cur][tmp][hw].fi, hw = trace[cur][tmp][hw].se;
			cur--;
		}
		cout << "YES\n";
		for(int i = 1; i <= n; i++) cout << res[i].fi << ' ' << res[i].se << '\n';
	}
}

#include <iostream>
#include <vector>

using namespace std;
using ll = long long;

struct sub{
    int i, c;
    ll a, b;
    sub(int i, ll a, ll b, int c): i(i), a(a), b(b), c(c){}
};

struct DPdata{
    ll exercise, parentI, parentJ;
    DPdata(){
        exercise = -1;
        parentI = -1;
        parentJ = -1;
    }
};

int main(){
    int n, m, k, c;
    ll a, b;
    scanf(" %d %d %d", &n, &m, &k);
    vector<vector<sub>> subs(100, vector<sub>());
    vector<ll> map(m); // maps index of a subject to its lower complexity bound
    for(int i = 0; i < m; ++i){
        scanf(" %lld %lld %d", &a, &b, &c);
        --c;
        subs[c].push_back(sub(i, a, b, c));
        map[i] = a;
    }
    ll mostExe = 0;
    pair<int, int> best;
    vector<vector<vector<DPdata>>> dp(m, vector<vector<DPdata>>(101, vector<DPdata>(n, DPdata{})));
    for(int c1 = 0; c1 < 100; ++c1){
        for(sub& s1 : subs[c1]){
            for(ll e1 = s1.a; e1 <= s1.b; ++e1){
                dp[s1.i][e1 - s1.a][0].exercise = e1;
                for(int str = 0; str < n - 1; ++str){
                    ll next1 = e1*k, next2 = e1 + k;
                    for(int c2 = c1 + 1; c2 < 100; ++c2){
                        for(sub& s2 : subs[c2]){
                            if(dp[s1.i][e1 - s1.a][str].exercise == -1) continue;
                            if(s2.a <= next1 && next1 <= s2.b){
                                if(dp[s2.i][next1 - s2.a][str + 1].exercise < dp[s1.i][e1 - s1.a][str].exercise + next1){
                                    /*
                                    if(s2.i == 1){
                                        printf("old: %d\n", dp[s2.i][next1 - s2.a][str + 1].exercise);
                                        printf("node: %d, edge: %d\n", dp[s1.i][e1 - s1.a][str].exercise, next1);
                                        printf("this: %d, %d, %d\n", s2.i, next1 - s2.a, str + 1);
                                        printf("parent: %d, %d, %d\n", s1.i, e1 - s1.a, str);
                                    }
                                    //*/
                                    dp[s2.i][next1 - s2.a][str + 1].exercise = dp[s1.i][e1 - s1.a][str].exercise + next1;
                                    dp[s2.i][next1 - s2.a][str + 1].parentI = s1.i;
                                    dp[s2.i][next1 - s2.a][str + 1].parentJ = e1 - s1.a;
                                    /*
                                    if(s2.i == 1){
                                        printf("new: %d = %d\n\n", dp[s1.i][e1 - s1.a][str].exercise + next1, dp[s2.i][next1 - s2.a][str + 1].exercise);
                                    }
                                    //*/
                                    //printf("%d %d %d: %d %d %d\n", s2.i, next1 - s2.a, str + 1, dp[s1.i][e1 - s1.a][str].exercise + next1, s1.i, e1 - s1.a);
                                }
                            }
                            if(s2.a <= next2 && next2 <= s2.b){
                                if(dp[s2.i][next2 - s2.a][str + 1].exercise < dp[s1.i][e1 - s1.a][str].exercise + next2){
                                    /*
                                    if(s2.i == 1){
                                        printf("old: %d\n", dp[s2.i][next2 - s2.a][str + 1].exercise);
                                        printf("node: %d, edge: %d\n", dp[s1.i][e1 - s1.a][str].exercise, next2);
                                        printf("this: %d, %d, %d\n", s2.i, next2 - s2.a, str + 1);
                                        printf("parent: %d, %d, %d\n", s1.i, e1 - s1.a, str);
                                    }
                                    //*/
                                    dp[s2.i][next2 - s2.a][str + 1].exercise = dp[s1.i][e1 - s1.a][str].exercise + next2;
                                    dp[s2.i][next2 - s2.a][str + 1].parentI = s1.i;
                                    dp[s2.i][next2 - s2.a][str + 1].parentJ = e1 - s1.a;
                                    /*
                                    if(s2.i == 1){
                                        printf("new: %d = %d\n\n", dp[s1.i][e1 - s1.a][str].exercise + next2, dp[s2.i][next2 - s2.a][str + 1].exercise);
                                    }
                                    //*/
                                }
                            }
                        }
                    }
                }
                if(dp[s1.i][e1 - s1.a][n - 1].exercise > mostExe){
                    mostExe = dp[s1.i][e1 - s1.a][n - 1].exercise;
                    best = {s1.i, e1 - s1.a};
                }
            }
        }
    }
    /*
    for(int c1 = 0; c1 < 6; ++c1){
        printf("complexity %d:\n", c1 + 1);
        for(sub& s1 : subs[c1]){
            printf("problem %d:\n", s1.i + 1);
            for(ll e1 = 0; e1 <= s1.b - s1.a; ++e1){
                printf("e %d:  ", s1.a + e1);
                for(int i = 0; i < n; ++i){
                    printf("%d(%d, %d) ", dp[s1.i][e1][i].exercise, dp[s1.i][e1][i].parentI, dp[s1.i][e1][i].parentJ);
                    //printf("%d ", dp[s1.i][e1][i].exercise);
                }
                printf("\n");
            }
        }
    }
    //*/

    if(mostExe == 0){
        printf("NO\n");
    }else{
        vector<pair<int, int>> stack;
        stack.push_back(best);
        for(int str = n - 1; str > 0; --str){
            best = {dp[best.first][best.second][str].parentI, dp[best.first][best.second][str].parentJ};
            stack.push_back(best);
        }
        printf("YES\n");
        for(int str = 0; str < n; ++str){
            printf("%d %lld\n", stack.back().first + 1, map[stack.back().first] + stack.back().second);
            stack.pop_back();
        }
    }
}

#include<iostream>
#include<sstream>
#include<vector>
#include<algorithm>
#include<set>
#include<map>
#include<queue>
#include<complex>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cassert>
using namespace std;

#define rep(i,n) for(int i=0;i<(int)n;i++)
#define each(i,c) for(__typeof(c.begin()) i=c.begin();i!=c.end();i++)
#define pb push_back
#define mp make_pair
#define all(c) c.begin(),c.end()
#define dbg(x) cerr<<__LINE__<<": "<<#x<<" = "<<(x)<<endl

typedef long long ll;
typedef pair<ll,ll> pi;
const int inf=(int)1e9;
const double EPS=1e-9, INF=1e12;

int n, m, x;
ll a[50], b[50], c[50];
ll prev[50][50][101], dp[50][50][101];

int main(){
	memset(dp, -1, sizeof(dp));
	cin >> n >> m >> x;
	rep(i, m){
		cin >> a[i] >> b[i] >> c[i];

		for(ll j = a[i]; j <= b[i]; j++){
			prev[0][i][j - a[i]] = -1;
			dp[0][i][j - a[i]] = j;
		}
	}

	rep(i, n) rep(p, m) rep(j, 101) rep(k, m){
		if(dp[i][p][j] < 0) continue;
		if(c[p] >= c[k]) continue;

		if(a[k] <= j + a[p] + x && j + a[p]+ x <= b[k]){
			ll nxt = j + a[p] + x;
			ll sum = dp[i][p][j] + nxt;
			if(sum > dp[i + 1][k][nxt - a[k]]){
				dp[i + 1][k][nxt- a[k]] = sum;
				prev[i + 1][k][nxt - a[k]] = p * 2;
			}
		}
		if(a[k] <= (j + a[p]) * x && (j + a[p]) * x <= b[k]){
			ll nxt = (j + a[p]) * x;
			ll sum = dp[i][p][j] + nxt;
			if(sum > dp[i + 1][k][nxt - a[k]]){
				dp[i + 1][k][nxt- a[k]] = sum;
				prev[i + 1][k][nxt - a[k]] = p * 2 + 1;
			}
		}
	}

	ll s, t, sum = -1;
	rep(i, m) rep(j, 101){
		if(dp[n - 1][i][j] > sum){
			sum = dp[n - 1][i][j];
			s = i;
			t = j + a[i];
		}
	}
	if(sum >= 0){
		cout << "YES" << endl;
		vector<pi> ans;
		ans.pb(mp(s, t));

		rep(i, n - 1){
			int type = prev[n - 1 - i][s][t - a[s]] % 2;
			s = prev[n - 1 - i][s][t - a[s]] / 2;
			if(type) t /= x;
			else t -= x;
			ans.pb(mp(s, t));
		}
		reverse(all(ans));
		rep(i, n) cout << ans[i].first + 1 << " " << ans[i].second << endl;
		return 0;
	}
	cout << "NO" << endl;
	return 0;
}

#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <string.h>

using namespace std;

const double pi = acos(-1.0);
const double eps = 1E-7;
const int inf = 123456789;

typedef long long int64;
typedef unsigned long long uint64;
#define two(X) (1<<(X))
#define twoL(X) (((int64)(1))<<(X))
#define contain(S,X) (((S)&two(X))!=0)
#define containL(S,X) (((S)&twoL(X))!=0)
#define sqr(x) ((x)*(x))
typedef pair<int,int> ipair;
#define SIZE(A) ((int)A.size())
#define MP(A,B) make_pair(A,B)
#define PB(X) push_back(X)
#define ME(a) memset((a), 0, sizeof((a)))
#define MM(a, b) memcpy((a), (b), sizeof((a)))
#define FOR(i,n) for (int (i) = 0; (i) < (n); ++(i))
#define REP(i,a,b) for (int (i) = (a); (i) < (b); ++(i))

const int maxN = 50+5;
const int Len = 101;
long long dp[maxN][maxN][120];
long long a[maxN], b[maxN], c[maxN];
int father1[maxN][maxN][120], father2[maxN][maxN][120];
int up[maxN];
int len[maxN];
int n, m, k;

void dfs(int x) {
	if (up[x] >0) return;
	up[x] = 1;
	FOR(i,m) if (c[i] >c[x]) {
		dfs(i);
		up[x] = max(up[x], up[i]+1);
	}
}
void init() {
	scanf("%d%d%d", &n, &m, &k);
	FOR(i,m) {
		scanf("%I64d%I64d%I64d", a+i, b+i, c+i);
		len[i] = b[i]-a[i]+1;
	}
	ME(up);
	FOR(i,m) dfs(i);
}
bool hzz(long long a, long long b) {
	if ((a-b) == k) return true;
	if (a/b == k && (a%b == 0)) return true;
	return false;
}
void outans(int i, int j, int u) {
	if (i == 0) {
		printf("%d %I64d\n", j+1, a[j]+u);
		return;
	}
	outans(i-1, father1[i][j][u], father2[i][j][u]);
	printf("%d %I64d\n", j+1, dp[i][j][u]-dp[i-1][father1[i][j][u]][father2[i][j][u]]);
}
void work() {
	memset(dp, -1, sizeof(dp));
	FOR(j,m) FOR(u,len[j]) {
		dp[0][j][u] = a[j]+u;
	}
	FOR(i,n-1) {
		FOR(j1,m) if (up[j1] >= n-i)
		FOR(u1,len[j1]) if (dp[i][j1][u1] > 0)
		FOR(j2,m) if (c[j2] > c[j1] && up[j2] >= n-i-1)
		FOR(u2,len[j2]) if (hzz(u2+a[j2], u1+a[j1])) {
			if (dp[i][j1][u1] + u2+a[j2] > dp[i+1][j2][u2]) {
				//	cout<<u2+a[j2]<<"y"<<u1+a[j1]<<endl;
					dp[i+1][j2][u2] = dp[i][j1][u1] + u2+a[j2];
					father1[i+1][j2][u2] = j1;
					father2[i+1][j2][u2] = u1;
			}
		}
	}

	long long ans = -1;
	int bj, bu;
	FOR(j,m) FOR(u,len[j]) {
		if (dp[n-1][j][u] > ans) {
			ans = dp[n-1][j][u];
			bj = j;
			bu = u;
		}
	}
	if (ans == -1) {
		puts("NO");
	} else {
		puts("YES");
//		printf("%I64d\n", ans);
		outans(n-1, bj, bu);
	}
}
int main() {
//	freopen("a.in", "r", stdin);
	init();
	work();
	//while (1>0) {}
	return 0;
}

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

ifstream in("test.in");
ofstream out("test.out");

const int NMAX = 55;
const int DMAX = 105;

struct str {
    long long a, b; int c, id;
} arr[NMAX];

long long dp[NMAX][NMAX][DMAX];

bool cmp(str s1, str s2)
{
    return s1.c < s2.c;
}

void reset(int k)
{

}

void update(long long &x, long long y)
{
    x = max(x, y);
}

void solve(int nr, int i, int j, int p)
{
    if (nr == 1) {
        cout << arr[i].id << " " << arr[i].a + j << endl; return; }

    for (int k = 1; arr[k].c < arr[i].c; ++k) {
    for (int l = 0; l <= arr[k].b - arr[k].a; ++l) {
        if ((arr[k].a + l) * p == arr[i].a + j and dp[nr][i][j] == dp[nr - 1][k][l] + (arr[k].a + l) * p) {
            solve(nr - 1, k, l, p); cout << arr[i].id << " " << arr[i].a + j << endl; return; }
        if ((arr[k].a + l) + p == arr[i].a + j and dp[nr][i][j] == dp[nr - 1][k][l] + (arr[k].a + l) + p) {
            solve(nr - 1, k, l, p); cout << arr[i].id << " " << arr[i].a + j << endl; return; }
    } }
}

int main(void)
{
    int m, n, p;
    cin >> m >> n >> p;

    for (int i = 1; i <= n; ++i)
        cin >> arr[i].a >> arr[i].b >> arr[i].c, arr[i].id = i;
    sort(arr + 1, arr + n + 1, cmp);

    for (int nr = 0; nr < NMAX; ++nr)
    for (int i = 0; i < NMAX; ++i)
    for (int j = 0; j < DMAX; ++j)
        dp[nr][i][j] = numeric_limits<long long> :: min() / 2;

    for (int i = 1; i <= n; ++i)
    for (int j = 0; j <= arr[i].b - arr[i].a; ++j)
        dp[1][i][j] = arr[i].a + j;

    for (int nr = 2, l = 0; nr <= m; ++nr, l ^= 1) {
    for (int i = 1; i <= n; ++i) {
    for (int j = 1; arr[j].c < arr[i].c; ++j) {
    for (int k = 0; k <= arr[j].b - arr[j].a; ++k) {
        if (dp[nr - 1][j][k] < 0)
            continue;
        if ((arr[j].a + k) * p >= arr[i].a and (arr[j].a + k) * p <= arr[i].b)
            update(dp[nr][i][(arr[j].a + k) * p - arr[i].a], dp[nr - 1][j][k] + (arr[j].a + k) * p);
        if ((arr[j].a + k) + p >= arr[i].a and (arr[j].a + k) + p <= arr[i].b)
            update(dp[nr][i][(arr[j].a + k) + p - arr[i].a], dp[nr - 1][j][k] + (arr[j].a + k) + p);
    } } } }

    pair<int, int> ps(0, 0);
    long long ans = numeric_limits<long long> :: min() / 2;

    for (int i = 1; i <= n; ++i)
    for (int j = 0; j <= arr[i].b - arr[i].a; ++j)
        if (ans < dp[m][i][j]) ans = dp[m][i][j], ps = make_pair(i, j);

    if (ans < 0)
        cout << "NO\n";
    else {
        cout << "YES\n";
        solve(m, ps.first, ps.second, p);
    }

    return 0;
}

#include <iostream>
#include <vector>
#include <string>
#include <cmath>
#include <algorithm>
using namespace std;

pair<long long,long long> limits[200],temp[200];
pair<long long,long long> Comp[200];
long long dif[200];
long long n,m,k;

long long dp[200][200][200];
long long uv[200][200][200];
long long ci[200][200][200];

long long isin(long long v,long long in) {
	if(v<limits[in].first||v>limits[in].second)
		return -1;
	return (v-limits[in].first);
}

int main()
{
	//freopen("in.txt","r",stdin);
	cin>>n>>m>>k;
	long long i,j,l,v,d,tt;
	long long mxd,mxv=-1,mxl;

	for(i=0;i<m;i++) {
		cin>>temp[i].first>>temp[i].second;
		cin>>Comp[i].first;
		Comp[i].second=i;
	}
	sort(Comp,Comp+m);
	for(i=0;i<m;i++) {
		limits[i]=temp[Comp[i].second];
		dif[i]=limits[i].second-limits[i].first+1;
	}

	memset(dp,-1,sizeof dp);
	for(i=0;i<n;i++) {
		for(j=0;j<m;j++) {
			for(l=0;l<dif[j];l++) {
				v=l+limits[j].first;
				if(i==0) {
					dp[i][j][l]=v;
				}
				else if(j==0){
					continue;
				}
				else {
					for(d=0;d<j;d++) {
						if(Comp[d].first==Comp[j].first)
							continue;
						if((tt=isin(v-k,d))>=0) {
							if(dp[i-1][d][tt]>=0&&(dp[i-1][d][tt]+v)>dp[i][j][l]) {
								dp[i][j][l]=dp[i-1][d][tt]+v;
								uv[i][j][l]=tt;
								ci[i][j][l]=d;
							}
						}
						if(v%k==0&&(tt=isin(v/k,d))>=0) {
							if(dp[i-1][d][tt]>=0&&(dp[i-1][d][tt]+v)>dp[i][j][l]) {
								dp[i][j][l]=dp[i-1][d][tt]+v;
								uv[i][j][l]=tt;
								ci[i][j][l]=d;
							}
						}
					}
				}
				if(i==(n-1)) {
					if(mxv<dp[i][j][l]) {
						mxv=dp[i][j][l];
						mxd=j;
						mxl=l;
					}
				}
			}
		}
	}

	if(mxv>=0) {
		cout<<"YES"<<endl;
		//cout<<mxv<<endl;
		vector<pair<long long,long long> > res;
		res.clear();
		i=n-1;
		j=mxd;
		l=mxl;
		while(i>=0) {
			v=limits[j].first+l;
			res.push_back(make_pair(Comp[j].second,v));
			long long nj=ci[i][j][l];
			l=uv[i][j][l];
			j=nj;
			i--;
		}
		for(i=res.size()-1;i>=0;i--) {
			cout<<res[i].first+1<<" "<<res[i].second<<endl;
		}
	}
	else {
		cout<<"NO"<<endl;
	}

	return 0;
}

#include <iostream>
#include <cstring>
#include <cstdio>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <limits.h>
#include <utility>
#include <sstream>
#include <algorithm>

using namespace std;
#define LL long long
struct NODE{
    LL a, b, c;
    int num;
};
NODE p[55];
int n, m;
LL K;
LL f[55][55][110];
int cmp(NODE a, NODE b)
{
    if(a.c == b.c){
        return a.num < b.num;
    }else return a.c < b.c;
}
void print_path(int dep, int j, int k)
{
    if(!dep) return ;
    LL tmp = k + p[j].a;
    for(int i = 1; i < j; i++){
        if(tmp - K >= p[i].a && tmp - K <= p[i].b && f[dep][j][k] == f[dep - 1][i][tmp - K - p[i].a] + tmp){
            print_path(dep - 1, i, tmp - K - p[i].a);
            break;
        }
        if(tmp % K == 0 && tmp / K >= p[i].a && tmp / K <= p[i].b && f[dep][j][k] == f[dep - 1][i][tmp / K - p[i].a] + tmp){
            print_path(dep - 1, i, tmp / K - p[i].a);
            break;
        }
    }
    //printf("%d: ", dep);
    printf("%d %I64d\n", p[j].num, tmp);
}
int main()
{
    //freopen("input.txt", "r", stdin);
    scanf("%d %d %I64d", &n, &m, &K);
    for(int i = 1; i <= m; i++){
        scanf("%I64d %I64d %I64d", &p[i].a, &p[i].b, &p[i].c);
        p[i].num = i;
    }
    sort(p + 1, p + 1 + m, cmp);
    memset(f, 0, sizeof(f));
//    for(int i = 1; i <= m; i++)
//        printf("%d: %d %d %d\n", p[i].num, p[i].a, p[i].b, p[i].c);
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= m; j++){
            for(LL k = p[j].a; k <= p[j].b; k++){
                if(i == 1) f[i][j][k - p[j].a] = k;
                else if(f[i - 1][j][k - p[j].a]){
                    //printf("f[%d][%d][%d] = %d\n", i - 1, j, k - p[j].a, f[i - 1][j][k - p[j].a]);
                    for(int l = j + 1; l <= m; l++){
                        if(p[l].c > p[j].c){
                            LL tmp;
                            if(k + K >= p[l].a && k + K <= p[l].b){
                                tmp = k + K - p[l].a;
                                f[i][l][tmp] = max(f[i][l][tmp], f[i - 1][j][k - p[j].a] + k + K);
                            }
                            if(k * K >= p[l].a && k * K <= p[l].b){
                                tmp = k * K - p[l].a;
                                f[i][l][tmp] = max(f[i][l][tmp], f[i - 1][j][k - p[j].a] + k * K);
                            }
                        }
                    }
                }
            }
        }
    }
    LL Maxx = 0;
    int id, jd;
    for(int j = n; j <= m; j++){
        for(int i = 0; i <= p[j].b - p[j].a; i++){
            //printf("f[%d][%d][%d] = %d\n", n, j, i, f[n][j][i]);
            if(Maxx < f[n][j][i]){
                Maxx = f[n][j][i];
                id = j;
                jd = i;
            }
        }
    }
    //printf("%d\n", f[1][1][4]);
    if(Maxx){
        //printf("%d %d %d\n", Maxx, id, jd);
        printf("YES\n");
        print_path(n, id, jd);
    }else printf("NO\n");
    return 0;
}

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<queue>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#define INFINITE (int)pow(10,15)
#define FOR(i,n) for(int i=0;i<n;i++)
#define FORN(i,a,n) for(int i=a;i<=n;i++)

using namespace std;

class myData{
    public:
        __int64 v;
        int c;
        int id;
        myData(__int64 v, int u, int c):v(v), id(u), c(c){}
        myData(){};
        bool operator < (const myData& m)const;
    private:
};
bool myData::operator<(const myData& m) const{
    if (c < m.c)    return true;
    return false;
}
//int dp[1000000];
//vector<myData>md;
/*int sp(int a,int s,int ma){
    int j;
    for(j=0;j<L;j++)
        if(m[j] < out[a])   break;
    return j;
}*/
int n,m,k,mdc=0;
myData mc[5050];
__int64 dp[5050][51];
int route[5050][51],seq[51];            //
int pro_c[5050], pro_m[5050];
map<__int64, int> hash;

void update(int p, int a, int b, __int64 v){
    dp[a][b] = v;
    route[a][b] = p;
}
int getNum(){
    for(int i=mdc-1;i>=0;){
        int j;
        for(j=i;j>=0&&mc[j].c == mc[i].c;j--);
        for(int m=i;m>j;m--){
            __int64 ff = mc[m].v + k;
            if(hash.find(ff) != hash.end())   pro_c[m] = hash[ff];
            else    pro_c[m] = -1;
            ff = mc[m].v * k;
            if(hash.find(ff) != hash.end())   pro_m[m] = hash[ff];
            else    pro_m[m] = -1;
        }
        for(int m=i;m>j;m--)
            hash[mc[m].v] = m;
        i = j;          //assign once for each
    }
    int ans = 0;
    memset(dp, -1, sizeof(dp));
    for(int i=0;i<mdc;i++)  dp[i][1] = mc[i].v;
    for(int i=0;i<mdc;i++){
        if(dp[i][n] > dp[ans][n])   ans = i;
        for(int j=1;j<n;j++){
            if(dp[i][j] < 0)    break;
            if(pro_c[i]>0 && dp[pro_c[i]][j+1] < dp[i][j] + mc[pro_c[i]].v)  update(i, pro_c[i], j+1 ,dp[i][j] + mc[pro_c[i]].v);
            if(pro_m[i]>0 && dp[pro_m[i]][j+1] < dp[i][j] + mc[pro_m[i]].v)  update(i, pro_m[i], j+1 ,dp[i][j] + mc[pro_m[i]].v);
        }
    }
    if(dp[ans][n] == -1)    cout <<"NO";
    else{
        cout <<"YES"<<endl;
        for(int i=0;i<n;i++){
            seq[n-1-i] = ans;
            ans = route[ans][n-i];
            //printf("seq[%d] = %d, id = %d\n", n-1-i,seq[n-1-i], mc[seq[n-1-i]].id);
        }
        for(int i=0;i<n;i++)
            printf("%d %I64d\n", mc[seq[i]].id, mc[seq[i]].v);
    }
    return 0;
}
int main(){
    //cout <<"t"<<endl;
    scanf("%d %d %d", &n, &m, &k);
    FOR(i,m){
        __int64 a,b;
        int c;
        scanf("%I64d %I64d %d", &a,&b,&c);
        for(__int64 j=a;j<=b;j+=1)
            mc[mdc++] = myData(j,i+1,c);
    }
    sort(mc, mc + mdc);
    //FOR(i,mdc)
    //    printf("%I64d %d %d\n", mc[i].v, mc[i].id, mc[i].c);
    getNum();

}

#include <bits/stdc++.h>

using namespace std;

const int MOD=1e9+7;

//const int MOD=998244353;

struct Subject{
    int pos, c;
    long long a,b;
    Subject(int _pos, long long _a, long long _b, int _c):pos(_pos),a(_a),b(_b),c(_c){}
};

struct Pe{
    long long val, pval;
    int i,j;
    Pe(int _i, int _j, long long _val, long long _pval):val(_val),pval(_pval),i(_i),j(_j){}

};

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int n,m,k;
    cin>>n>>m>>k;

    vector<Subject> vs;
    for(int i=0; i<m; i++){
        long long a,b;
        int c;
        cin>>a>>b>>c;
        vs.push_back(Subject(i,a,b,c));
    }
    sort(vs.begin(),vs.end(),[](auto s1, auto s2){return s1.c<s2.c;});

    vector<vector<map<long long,long long>>> dp(n+1,vector<map<long long,long long>>(m));
    vector<vector<map<long long,Pe>>> preds(n+1, vector<map<long long,Pe>>(m));

    for(int i=1; i<=n; i++){
        for(int j=0; j<m; j++){
            for(long long kv=vs[j].a; kv<=vs[j].b; kv++){
                if(i==1) dp[i][j][kv]=kv;
                else{
                    long long bv=0;
                    for(int j2=0; j2<j; j2++){
                        if(vs[j2].c<vs[j].c){
                            auto it=dp[i-1][j2].find(kv-k);
                            if(it!=dp[i-1][j2].end() && kv+it->second>bv){
                                bv=kv+it->second;
                                dp[i][j][kv]=bv;
                                Pe pe(i-1,j2,kv,kv-k);
                                preds[i][j].insert({bv,pe});
                            }
                            if(!(kv%k)){
                                auto it=dp[i-1][j2].find(kv/k);
                                if(it!=dp[i-1][j2].end() && kv+it->second>bv){
                                    bv=kv+it->second;
                                    dp[i][j][kv]=bv;
                                    Pe pe(i-1,j2,kv,kv/k);
                                    preds[i][j].insert({bv,pe});
                                }
                            }

                        }
                    }
                }

            }

        }
    }

    long long tot=0;
    int pj=-1;
    long long pk=-1;
    for(int j=0; j<m; j++){
        for(auto p:dp[n][j]){
            if(p.second>tot){
                tot=p.second;
                pk=p.first;
                pj=j;
            }
        }
    }
    if(!tot){
        cout<<"NO";
        return 0;
    }
    cout<<"YES\n";
    vector<pair<int,long long>> ans;

    for(int i=n; i; i--){
        ans.push_back({pj,pk});
        for(int j=0;  vs[j].c<vs[pj].c; j++){
            auto it=dp[i-1][j].find(pk-k);
            if(it!=dp[i-1][j].end() && tot==it->second+pk){
                tot-=pk;
                pj=j;
                pk=it->first;
                break;
            }
            if(!(pk%k)){
                auto it=dp[i-1][j].find(pk/k);
                if(it!=dp[i-1][j].end() && tot==it->second+pk){
                    tot-=pk;
                    pj=j;
                    pk=it->first;
                    break;
                }
            }

        }

    }
    reverse(ans.begin(),ans.end());
    for(auto p:ans){
        cout<<vs[p.first].pos+1<<' '<<p.second<<'\n';
    }

    return 0;
}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int n,m,K;
class cls
{
	public:
		ll a,b;
		int c,id;
		bool operator <(const cls &x)const
		{
			return c<x.c;
		}
}c[55];
ll dp[55][110][55];
class trans
{
	public:
		int a,b,c;
		bool operator <(const trans &x)const
		{
			return a<x.a;
		}
		bool operator !=(const trans &x)
		{
			return a!=x.a||b!=x.b||c!=x.c;
		}
}zy[55][110][55];
pair<ll,trans> mxpos;
int main()
{
	scanf("%d%d%d",&n,&m,&K);
	for(int i=1;i<=m;i++)scanf("%lld%lld%d",&c[i].a,&c[i].b,&c[i].c),c[i].id=i;
	sort(c+1,c+m+1);
	memset(dp,255,sizeof(dp));
	dp[0][0][0]=0;
	mxpos=make_pair(-1ll,(trans){-1,-1,-1});
	for(int T=1;T<=n;T++)
	for(int i=1;i<=m;i++)
	{
		for(int j=0;j<i;j++)
		{
			if(c[j].c>=c[i].c)break;
			for(int k=0;k<=c[j].b-c[j].a;k++)
			{
				if(dp[j][k][T-1]==-1)continue;
				for(int l=0;l<=c[i].b-c[i].a;l++)
				{
					if(c[j].a+k+K==c[i].a+l||(c[j].a+k)*K==c[i].a+l||j==0)
					{
						if(dp[i][l][T]<dp[j][k][T-1]+c[i].a+l)
						{
							zy[i][l][T]=(trans){j,k,T-1};
							dp[i][l][T]=dp[j][k][T-1]+c[i].a+l;
						}
					}
				}
			}
		}
		if(T==n)for(int j=0;j<=c[i].b-c[i].a;j++)mxpos=max(mxpos,make_pair(dp[i][j][T],(trans){i,j,T}));
	}
	if(mxpos.first==-1)puts("NO");
	else
	{
		puts("YES");
		vector<pair<int,ll> > ans;
//		ans.push_back(make_pair(mxpos.second.first,mxpos.second.second+c[mxpos.second.first].a));
		trans np=mxpos.second;
		for(;np!=(trans){0,0,0};np=zy[np.a][np.b][np.c])ans.push_back(make_pair(np.a,np.b+c[np.a].a));
		for(int i=ans.size()-1;i>=0;i--)printf("%d %lld\n",c[ans[i].first].id,ans[i].second);
	}
	return 0;
}

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

typedef long long ll;

const int Maxn = 52;
const int Maxk = 105;

struct sub {
       ll a, b;
       int c, id;
       bool operator <(const sub &s) const {
            return c < s.c;
       }
};

int n, m, k;
vector <sub> V;
ll res[Maxn][Maxk][Maxn];
pair <int, int> p[Maxn][Maxk][Maxn];

void Add(int i1, int i2, int i3, int j1, int j2, int j3, ll have)
{
    if (i2 >= 0 && i2 <= V[i1].b - V[i1].a && res[i1][i2][i3] != -1 &&
        (res[j1][j2][j3] == -1 || res[i1][i2][i3] + have > res[j1][j2][j3])) {
                         res[j1][j2][j3] = res[i1][i2][i3] + have;
                         p[j1][j2][j3] = make_pair(i1, i2);
    }
}

void Print(int i, int j, int k)
{
     if (k > 1) Print(p[i][j][k].first, p[i][j][k].second, k - 1);
     cout << V[i].id << " " << V[i].a + ll(j) << endl;
}

int main()
{
    cin >> n >> m >> k;
    for (int i = 1; i <= m; i++) {
        sub s; s.id = i;
        cin >> s.a >> s.b >> s.c;
        V.push_back(s);
    }
    sort(V.begin(), V.end());
    fill((ll*)res, (ll*)res + Maxn * Maxk * Maxn, -1);
    for (int i = 0; i < m; i++)
       for (int j = 0; j <= V[i].b - V[i].a; j++) {
           ll have = V[i].a + ll(j);
           res[i][j][1] = have;
           for (int l = 2; l <= n; l++)
              for (int pr = 0; pr < i && V[pr].c < V[i].c; pr++) {
                  if (have % ll(k) == 0LL) Add(pr, have / ll(k) - V[pr].a, l - 1, i, j, l, have);
                  Add(pr, have - ll(k) - V[pr].a, l - 1, i, j, l, have);
              }
       }
    int bi = 0, bj = 0;
    for (int i = 0; i < m; i++)
       for (int j = 0; j <= V[i].b - V[i].a; j++)
          if (res[i][j][n] > res[bi][bj][n]) {
                           bi = i; bj = j;
          }
    if (res[bi][bj][n] == -1) cout << "NO\n";
    else {
         cout << "YES\n";
         Print(bi, bj, n);
    }
    return 0;
}

#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cctype>
#include <ctime>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <functional>
#include <numeric>
#include <algorithm>
#include <list>
#include <map>
#include <set>
#include <iterator>
#include <utility>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <exception>
#define FOR(i, n, m) for (i = (n); i < (m); i++)
#define FORR(i, n, m) for (i = (n); i >= (m); i--)
#define LLI long long int
#define ULL unsigned long long
#define PII pair < int, int >
#define VI vector < int >
#define VS vector < string >
#define SS stringstream
#define FS first
#define SN second
#define PB push_back
#define BE begin
#define EN end
#define CL(v, val) memset(v, val, sizeof v)
#define ALL(v) v.begin(), v.end()
#define INF 0x3f3f3f3f
#define ISDIG(c) (c >= '0' && c <= '9')
#define MAXN 50
#define MAXK 100
using namespace std;

int n, m, k;
int suc[MAXN][MAXN][MAXK + 1];
LLI dp[MAXN][MAXN][MAXK + 1];
struct node
{
    LLI a, b;
    int c, i;

    node()
    {
    }

    bool operator<(const node &b) const
    {
        return c < b.c;
    }
} v[MAXN];

LLI go(int i, int j, int l)
{
    int ii, jj;
    LLI temp1, temp2, temp3;

    if (i == n) {
        return 0LL;
    }

    if (j == m) {
        return (LLI) (-INF);
    }

    LLI& best = dp[i][j][l];
    if (best != -1LL) {
        return best;
    }

    best = (LLI) (-INF);
    FOR (ii, j + 1, m) {
        if (v[j].c >= v[ii].c) {
            continue;
        }

        temp1 = v[j].a + (LLI) (l) + (LLI) (k);
        temp2 = (v[j].a + (LLI) (l)) * (LLI) (k);

        if (temp1 >= v[ii].a && temp1 <= v[ii].b) {
            jj = (int) (temp1 - v[ii].a);
            temp3 = go(i + 1, ii, jj);

            if (temp3 >= 0LL) {
                best = max(best, temp3 + temp1);
            }
        }

        if (temp2 >= v[ii].a && temp2 <= v[ii].b) {
            jj = (int) (temp2 - v[ii].a);
            temp3 = go(i + 1, ii, jj);

            if (temp3 >= 0LL) {
                best = max(best, temp3 + temp2);
            }
        }
    }

    return best;
}

void printSol(int i, int j, int l)
{
    int ii, jj;
    LLI temp1, temp2, temp3;

    if (i == n) {
        return;
    }

    FOR (ii, j + 1, m) {
        if (v[j].c >= v[ii].c) {
            continue;
        }

        temp1 = v[j].a + (LLI) (l) + (LLI) (k);
        temp2 = (v[j].a + (LLI) (l)) * (LLI) (k);

        if (temp1 >= v[ii].a && temp1 <= v[ii].b) {
            jj = (int) (temp1 - v[ii].a);
            temp3 = go(i + 1, ii, jj);

            if (temp3 >= 0LL) {
                if (dp[i][j][l] == (temp3 + temp1)) {
                    cout << v[ii].i << " " << temp1 << endl;
                    printSol(i + 1, ii, jj);
                    return;
                }
            }
        }

        if (temp2 >= v[ii].a && temp2 <= v[ii].b) {
            jj = (int) (temp2 - v[ii].a);
            temp3 = go(i + 1, ii, jj);

            if (temp3 >= 0LL) {
                if (dp[i][j][l] == (temp3 + temp2)) {
                    cout << v[ii].i << " " << temp2 << endl;
                    printSol(i + 1, ii, jj);
                    return;
                }
            }
        }
    }
}

int main()
{
    int i, bi;
    LLI j, bj, ans, temp;

    cin >> n >> m >> k;
    FOR (i, 0, m) {
        cin >> v[i].a >> v[i].b >> v[i].c;
        v[i].i = i + 1;
    }

    sort(v, v + m);

    ans = (LLI) (-INF);
    memset(dp, -1LL, sizeof dp);
    bi = -1;
    bj = -1LL;
    FOR (i, 0, m) {
        FOR (j, v[i].a, v[i].b + 1) {
            temp = go(1, i, (int) (j - v[i].a));
            if (temp >= 0LL && j + temp > ans) {
                ans = temp + j;
                bi = i;
                bj = j;
            }
        }
    }

    if (bi == -1) {
        puts("NO");
    }
    else {
        puts("YES");
        cout << v[bi].i << " " << bj << endl;

        printSol(1, bi, (int) (bj - v[bi].a));
    }

    return 0;
}

#include<iostream>
#include<cstring>
using namespace std;
typedef long long ll;
int n,m,k;
ll a[60],b[60];
int c[100];
ll mem[60][60][110];
ll nxt[60][60][110],nxta[60][60][110];
ll dp(int i,int cnt,ll amt) {
	if(amt<a[i] || amt>b[i]) return -2;
	if(cnt==1) return amt;
	int nrm=amt-a[i];
	if(mem[i][cnt][nrm]!=-1) {
		return mem[i][cnt][nrm];
	}
	ll mx=-2,t;
	for(int j=0;j<m;j++) {
		if(c[j]>c[i]) {
			t=dp(j,cnt-1,amt+k);
			if(t>mx) {
				mx=t;
				nxt[i][cnt][nrm]=j;
				nxta[i][cnt][nrm]=amt+k;
			}
			t=dp(j,cnt-1,amt*k);
			if(t>mx) {
				mx=t;
				nxt[i][cnt][nrm]=j;
				nxta[i][cnt][nrm]=amt*k;
			}
		}
	}
	if(mx!=-2) {
		mx+=amt;
	}
	return mem[i][cnt][nrm]=mx;
}
int main() {
	memset(mem,-1,sizeof(mem));
	cin>>n>>m>>k;
	for(int i=0;i<m;i++) {
		cin>>a[i]>>b[i]>>c[i];
	}
	ll mx=-2,mxi,mxa;
	for(int i=0;i<m;i++) {
		for(ll f=a[i];f<=b[i];f++) {
			ll t=dp(i,n,f);
			if(t>mx) {
				mx=t;
				mxi=i;
				mxa=f;
			}
		}
	}
	if(mx==-2) {
		cout<<"NO";
		return 0;
	}
	cout<<"YES"<<endl;
	for(int i=n;i;i--) {
		cout<<mxi+1<<' '<<mxa<<endl;
		ll ti=nxt[mxi][i][mxa-a[mxi]],ta=nxta[mxi][i][mxa-a[mxi]];
		mxi=ti;
		mxa=ta;
	}
}

//By Lin
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

int		n,m,kk,from[55][55][105][2];
long long 		dp[55][55][105];;
struct	Subject{
	long long	a,b;
	int	c,num,L;
}data[55];
bool	cmp( Subject a , Subject b ) {
	return a.c < b.c;
}
bool	check(long long  &a , long long b )
{
	if ( b > a ) { a = b;  return true; }
	return false;
}
void	writ(int i ,int j ,int k )
{
	if  ( i == 0 ) return;
	writ( i-1 , from[i][j][k][0] , from[i][j][k][1] );
	printf("%d %I64d\n" , data[j].num , data[j].a + k );
}

int main()
{
	scanf("%d%d%d" , &n ,&m ,&kk );
	for (int i = 0; i<m; i++)
	{
		scanf("%I64d%I64d%d" , &data[i].a , &data[i].b , &data[i].c );
		data[i].num = i+1;
		data[i].L = data[i].b - data[i].a;
	}
	sort( data , data+m , cmp );
	for (int i = 1; i<=n; i++)
		for (int j = 0; j<m; j++)
			for (int k = 0; k<=data[j].L; k++)
			{
				long long  today = data[j].a + k;
				if ( i == 1 ) dp[i][j][k] = today;
				else
				{
					for (int t = 0; t<j; t++)
					{
						if ( data[t].c == data[j].c ) break;
						long long 	w = data[j].a + k - kk - data[t].a;
						if ( w >= 0 && w <= data[t].L && dp[i-1][t][w] &&  check(dp[i][j][k],dp[i-1][t][w]+today) ){
							from[i][j][k][0] = t;
							from[i][j][k][1] = (int)w;
						}
						w = data[j].a + k;
						if ( w%kk != 0 ) continue;
						w = w/kk - data[t].a;
						if ( w >= 0 && w <= data[t].L && dp[i-1][t][w] && check(dp[i][j][k],dp[i-1][t][w]+today) ){
							from[i][j][k][0] = t;
							from[i][j][k][1] = (int)w;
						}
					}
				}
			}
	int	g = -1 , h = -1;
	long long ans = 0;
	for ( int j = 0; j<m; j++)
		for (int k = 0; k<=data[j].L; k++)
			if ( dp[n][j][k]>ans ) {
				ans = dp[n][j][k];
				g = j;
				h = k;
			}
	if ( g == -1 ) printf("NO\n");
	else
	{
		printf("YES\n");
		writ( n , g , h );
	}
	return 0;
}

/*
*Rainto96
*Beijing University of Posts and Telecommunications School of Software Engineering
*http://blog.csdn.net/u011775691
*/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cstring>
#include <string>
#include <cmath>
#include <set>
#include <map>
#include <vector>
#include <climits>
using namespace std;
#define pb push_back
#define ALL(x) x.begin(),x.end()
#define VINT vector<ll>
#define PII pair<ll,ll>
#define MP(x,y) make_pair((x),(y))
#define ll long long
#define ull unsigned ll
#define MEM0(x)  memset(x,0,sizeof(x))
#define MEM(x,val) memset((x),val,sizeof(x))
#define scan(x) scanf("%I64d",&(x))
#define scan2(x,y) scanf("%I64d%I64d",&(x),&(y))
#define scan3(x,y,z) scanf("%I64d%I64d%I64d",&(x),&(y),&(z))
#define scan4(x,y,z,k) scanf("%I64d%I64d%I64d%I64d",&(x),&(y),&(z),&(k))
#define Max(a,b) a=max(a,b)
#define Min(a,b) a=min(a,b)
#define rep(x,y) for(ll (x)=0;(x)<(y);(x)++ )
using namespace std;
struct Cls{
        ll a,b,c;
        ll preid;
        bool operator<(Cls s)const{
                return c<s.c;
        }
}cls[77];
ll dp[66][66][111];
ll pre[66][66][111][3];
ll num[66][66][111];

void output(ll i,ll j,ll k){
        //cout<<i<<" "<<j<<" "<<k<<endl;
        //printf("%I64d %I64d %I64d\n",i,j,k);
        if(i==-1) return;
        ll prei=pre[i][j][k][0];
        ll prej=pre[i][j][k][1];
        ll prek=pre[i][j][k][2];
        output(prei,prej,prek);
        printf("%I64d %I64d\n",cls[j].preid,k+cls[j].a);
}
int main(){
	#ifndef ONLINE_JUDGE
		freopen("in.txt","r",stdin);
	#endif
	ll n,m,K;
	scan3(n,m,K);
	for(ll i=1;i<=m;i++){
                ll a,b,c;
                scan3(a,b,c);
                cls[i]=(Cls){a,b,c,i};
	}
	MEM(pre,-1);
	MEM(dp,-1);
	sort(cls+1,cls+1+m);
	//for(ll i=0;i<=100;i++) dp[0][0][i]=0;
	for(ll j=1;j<=m;j++){
                for(ll k=cls[j].a;k<=cls[j].b;k++){
                        dp[1][j][k-cls[j].a] = k;
                }
	}
	for(ll i=1;i<n;i++){
                for(ll j=0;j<=m;j++){
                        for(ll k=0;k<=100;k++){
                                if(dp[i][j][k]==-1) continue;
                                for(ll t=j+1;t<=m;t++){
                                        if(cls[t].c<=cls[j].c) continue;

                                        ll plus2=k+cls[j].a +K;
                                        ll times2=(k+cls[j].a)*K;
                                        if(plus2>=cls[t].a && plus2<=cls[t].b
                                           && dp[i][j][k]+plus2>dp[i+1][t][plus2-cls[t].a] ){
                                                dp[i+1][t][plus2-cls[t].a]= dp[i][j][k]+plus2;
                                                pre[i+1][t][plus2-cls[t].a][0]=i;
                                                pre[i+1][t][plus2-cls[t].a][1]=j;
                                                pre[i+1][t][plus2-cls[t].a][2]=k;
                                                num[i+1][t][plus2-cls[t].a]=plus2;
                                        }

                                        if(times2>=cls[t].a && times2<=cls[t].b
                                           && dp[i][j][k]+times2>dp[i+1][t][times2-cls[t].a]){
                                                dp[i+1][t][times2-cls[t].a]=dp[i][j][k]+times2;
                                                pre[i+1][t][times2-cls[t].a][0]=i;
                                                pre[i+1][t][times2-cls[t].a][1]=j;
                                                pre[i+1][t][times2-cls[t].a][2]=k;
                                                num[i+1][t][times2-cls[t].a]=times2;
                                        }
                                }
                        }
                }
	}
        ll maxn=0;
        ll sti,stj,stk;
	for(ll j=m;j>=1;j--){
                for(ll k=100;k>=0;k--){
//                        if(dp[n][j][k]){
//                                puts("YES");
//                                output(n,j,k);
//                                return 0;
//                        }
                        if(maxn<dp[n][j][k]){
                                maxn=dp[n][j][k];
                                sti=n;
                                stj=j;
                                stk=k;
                        }
                }
	}

        if(maxn==0){
                puts("NO");
        }else{
                puts("YES");
                //cout<<maxn<<endl;
                output(sti,stj,stk);
        }

        return 0;
}

#include <bits/stdc++.h>
#define L first
#define R second
#define MP make_pair
#define PB push_back

using namespace std;

typedef pair<int, int> pie;
typedef pair<long long, long long> Pie;
typedef long long LL;

const int maxn = 5 * 10 + 10;
pair<pair<Pie, pie>, int> an[maxn];
LL a[maxn], b[maxn], dp[maxn][2 * maxn][maxn];
int c[maxn], d[maxn], num[maxn];
pie par[maxn][2 * maxn][maxn];

bool cmp(pair<pair<Pie, pie>, int> a, pair<pair<Pie, pie>, int> b){
	return a.L.R.L < b.L.R.L;
}

void go(int i, int j, int l){
	if(l == 0)
		return;
	go(par[i][j][l].L, par[i][j][l].R, l - 1);
	cout << num[i] + 1 << " " << a[i] + j << endl;
}

main(){
	int n, m;
	LL k;
	cin >> m >> n >> k;
	for(int i = 0;i < n;i ++)
		cin >> an[i].L.L.L >> an[i].L.L.R >> an[i].L.R.L,
		an[i].L.R.R = an[i].L.L.R - an[i].L.L.L,
		an[i].R = i;
	sort(an, an + n, cmp);
	for(int i = 0;i < n;i ++)
		a[i] = an[i].L.L.L,
		b[i] = an[i].L.L.R,
		c[i] = an[i].L.R.L,
		d[i] = an[i].L.R.R,
		num[i] = an[i].R;
	memset(dp, -1, sizeof dp);
	for(int i = 0;i < n;i ++){
		for(LL j = 0;j <= d[i];j ++){
			dp[i][j][0] = 0;
			dp[i][j][1] = a[i] + j;
			LL an1 = -1;
			if((a[i] + j) % k == 0)
				an1 = (a[i] + j) / k;
			LL an2 = a[i] + j - k;
			for(int l = 2;l <= m;l ++){
				for(int o = 0;o < i;o ++){
					if(c[i] == c[o])
						continue;
					if(a[o] <= an1 && an1 <= b[o] && dp[o][an1 - a[o]][l - 1] > dp[i][j][l])
						dp[i][j][l] = dp[o][an1 - a[o]][l - 1],
						par[i][j][l] = MP(o, an1 - a[o]);
					if(a[o] <= an2 && an2 <= b[o] && dp[o][an2 - a[o]][l - 1] > dp[i][j][l])
						dp[i][j][l] = dp[o][an2 - a[o]][l - 1],
						par[i][j][l] = MP(o, an2 - a[o]);
				}
				if(dp[i][j][l] != -1)
					dp[i][j][l] += a[i] + j;
			}
		}
	}
	LL res = -1;
	pair<int, int> source = MP(0, 0);
	for(int i = 0;i < n;i ++)
		for(LL j = 0;j <= d[i];j ++)
			if(dp[i][j][m] > res)
				res = dp[i][j][m],
				source = MP(i, j);
	if(res == -1)
		cout << "NO\n";
	else
		cout << "YES\n",
		go(source.L, source.R, m);
	return 0;
}

#include <iostream>
#include <vector>
#include <stdio.h>
#include <algorithm>
#include <string.h>

using namespace std;

int n, m, k;

long long gm[101][101][100];
int nxt[101][101][100];
long long ds[101][101][100];

struct subj {
	int c, n;
	long long a, b;
};

vector <subj> s;

bool operator < (const subj& x, const subj& y)
{
	return x.c < y.c;
}

long long getmax(int x, long long d, int left)
{
	if (left == 0) return d;
	int p = (int) (d - s[x].a);
	if (ds[x][p][left]) return gm[x][p][left];
	long long res = -1;
	long long tr;
	for (int i = 0; i < x; i++) {
		if (s[i].c == s[x].c) continue;
		if (s[i].a <= d - k && d - k <= s[i].b && (tr = getmax(i, d - k, left - 1)) > res) {
			res = tr;
			nxt[x][p][left] = i;
			ds[x][p][left] = d - k;
		}

		if (d % k == 0 && s[i].a <= d / k && d / k <= s[i].b && (tr = getmax(i, d / k, left - 1)) > res) {
			res = tr;
			nxt[x][p][left] = i;
			ds[x][p][left] = d / k;
		}

	}

	if (!ds[x][p][left]) ds[x][p][left] = -1;
	if (res != -1) res += d;
	return gm[x][p][left] = res;
}

void trace(int x, long long d, int left)
{
	if (left) {
		int x2 = nxt[x][d - s[x].a][left];
		long long d2 = ds[x][d - s[x].a][left];
		trace(x2, d2, left - 1);
	}
	cout << s[x].n << " " << d << endl;
}

int main()
{
	cin >> n >> m >> k;

	s.resize(m);
	for (int i = 0; i < m; i++) {
		cin >> s[i].a >> s[i].b >> s[i].c;
		s[i].n = i + 1;
	}

	sort(s.begin(), s.end());

	long long res = -1, tr;
	long long ri, rj;
	for (int i = 0; i < m; i++)
		for (long long j = s[i].a; j <= s[i].b; j++)
			if ((tr = getmax(i, j, n - 1)) > res) {
				res = tr;
				ri = i;
				rj = j;
			}

	if (res > -1) {
		cout << "YES" << endl;
		trace(ri, rj, n - 1);
	}
	else cout << "NO" << endl;

	return 0;
}

#pragma comment (linker, "/STACK:74000000")
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <memory>
#include <cctype>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
using namespace std;

#define FOR(i,a,b) for(int (i) = (a); (i) < (b); ++(i))
#define RFOR(i,a,b) for(int (i) = (a)-1; (i) >= (b); --(i))
#define CLEAR(a) memset((a),0,sizeof(a))
#define INF 1000000000
#define PB push_back
#define ALL(c) (c).begin(), (c).end()
#define pi 2*acos(0.0)
#define SQR(a) (a)*(a)
#define MP make_pair

typedef long long Int;

struct node
{
        Int a, b;
        int i, c;
};

struct parent
{
        int i, j, k;
        Int d;
};

int n, m, k;
vector <node> A;
Int DP[51][51][101];
parent P[51][51][101];

bool Cmp(node a, node b)
{
        return a.c < b.c;
}

int main()
{
        cin >> n >> m >> k;
        FOR (i,0,m)
        {
                node t;
                Int a, b;
                int c;
                cin >> a >> b >> c;
                t.a = a;
                t.b = b;
                t.c = c;
                t.i = i;
                A.PB(t);
        }
        sort(ALL(A), Cmp);
        FOR (i,0,51)
                FOR (j,0,51)
                        FOR (k,0,101)
                        {
                                DP[i][j][k] = -100000000000000000ll;
                                P[i][j][k].i = -1;
                        }

        FOR (i,0,A.size())
                for (Int j = A[i].a; j<=A[i].b; j++)
                {
                        P[1][i][j-A[i].a].i = 0;
                        P[1][i][j-A[i].a].j = i;
                        P[1][i][j-A[i].a].d = j;
                        DP[1][i][j-A[i].a] = j;
                }

        FOR (i,1,n)
                FOR (j,0,A.size())
                {
                        for (Int l = A[j].a; l <= A[j].b; l++)
                        {
                                int d = (int)(l-A[j].a);
                                if (DP[i][j][d] < 0)
                                        continue;
                                FOR (t,j+1,A.size())
                                        if (A[t].c > A[j].c)
                                        {
                                                if (l+k >= A[t].a && l+k <= A[t].b)
                                                        if (DP[i+1][t][l+k-A[t].a] < DP[i][j][d]+l+k)
                                                        {
                                                                DP[i+1][t][l+k-A[t].a] = DP[i][j][d]+l+k;
                                                                P[i+1][t][l+k-A[t].a].i = i;
                                                                P[i+1][t][l+k-A[t].a].j = j;
                                                                P[i+1][t][l+k-A[t].a].k = d;
                                                                P[i+1][t][l+k-A[t].a].d = l+k;

                                                        }
                                                if (l*(Int)k >= A[t].a && l*(Int)k <= A[t].b)
                                                        if (DP[i+1][t][l*(Int)k-A[t].a] < DP[i][j][d]+l*(Int)k)
                                                        {
                                                                DP[i+1][t][l*(Int)k-A[t].a] = DP[i][j][d]+l*(Int)k;
                                                                P[i+1][t][l*(Int)k-A[t].a].i = i;
                                                                P[i+1][t][l*(Int)k-A[t].a].j = j;
                                                                P[i+1][t][l*(Int)k-A[t].a].k = d;
                                                                P[i+1][t][l*(Int)k-A[t].a].d = l*(Int)k;
                                                        }
                                        }
                        }
                }

        Int r = -100000000000000000ll;
        int x, y, z;

        FOR (i,0,A.size())
                FOR (j,0,101)
                        if (DP[n][i][j] > r)
                        {
                                r = DP[n][i][j];
                                x = n;
                                y = i;
                                z = j;
                        }
        if (r < 0)
        {
                cout << "NO" << endl;
                return 0;
        }

        cout << "YES" << endl;
        vector < pair<int, Int> > R;
        while (x > 0)
        {
                R.PB(MP(A[y].i+1, P[x][y][z].d));
                int xx, yy, zz;
                xx = P[x][y][z].i;
                yy = P[x][y][z].j;
                zz = P[x][y][z].k;
                x = xx;
                y = yy;
                z = zz;
        }
        reverse(ALL(R));
        FOR (i,0,R.size())
                cout << R[i].first << ' ' << R[i].second << endl;

        return 0;
}

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

using namespace std;

const int N = 55;
const int M = 128;

struct node
{
    long long a, b;
    int c, id;
    bool operator < (const node & sec) const
    {
        return c < sec.c;
    }
} dat[N];

struct ansnode
{
    int id;
    long long val;
} end, pre[N][N][M];

long long dp[N][N][M];

inline long long min(long long a, long long b)
{
    return a < b ? a : b;
}

void putans(ansnode cur, int num)
{
    if(cur.id == -1) return;
    putans(pre[num][cur.id][cur.val] , num - 1);
    printf("%d %I64d\n", dat[cur.id].id ,cur.val + dat[cur.id].a);
}

int main()
{
    int n,m;
    long long d;
    while(scanf("%d %d %I64d",&n,&m,&d) == 3)
    {
        memset(dp, -1, sizeof(dp));
        memset(pre, -1, sizeof(pre));

        for(int i = 1; i <= m; i++)
        {
            scanf("%I64d %I64d %d", &dat[i].a , &dat[i].b , &dat[i].c);
            dat[i].id = i;
        }
        sort(dat + 1, dat + m + 1);
        for(int i = 1; i <= m; i++)
        {
            int limit = min(100 , dat[i].b - dat[i].a);
            for(int j = 0; j <= limit ; j++)
            {
                dp[1][i][j] = dat[i].a + j;
            }
        }

        for(int i = 1; i < n; i++)
        {
            for(int j = 1; j <= m; j++)
            {
                int limit = min(100, dat[j].b - dat[j].a);
                for(int k = 0; k <= limit; k++)
                {
                    if(dp[i][j][k] == -1) continue;
                    for(int l = j + 1; l <= m; l++)
                    {
                        if(dat[j].c == dat[l].c) continue;
                        long long cur = k + dat[j].a , nxt;
                        nxt = cur + d;
                        if(nxt <= dat[l].b && nxt >= dat[l].a)
                        {
                            if(dp[i + 1][l][nxt - dat[l].a] < dp[i][j][k] + nxt)
                            {
                                dp[i + 1][l][nxt - dat[l].a] = dp[i][j][k] + nxt;
                                pre[i + 1][l][nxt - dat[l].a].id = j;
                                pre[i + 1][l][nxt - dat[l].a].val = k;
                            }
                        }
                        nxt = cur * d;
                        if(nxt <= dat[l].b && nxt >= dat[l].a)
                        {
                            if(dp[i + 1][l][nxt - dat[l].a] < dp[i][j][k] + nxt)
                            {
                                dp[i + 1][l][nxt - dat[l].a] = dp[i][j][k] + nxt;
                                pre[i + 1][l][nxt - dat[l].a].id = j;
                                pre[i + 1][l][nxt - dat[l].a].val = k;
                            }
                        }
                    }
                }
            }
        }
        long long ans = -1;
        for(int i = 1; i <= m; i++)
        {
            for(int j = 0; j <= 100; j++)
            {
                if(ans < dp[n][i][j])
                {
                    ans = dp[n][i][j];
                    end.id = i;
                    end.val = j;
                }
            }
        }
        if(ans != -1)
        {
            printf("YES\n");
            putans(end, n);
        }
        else
            printf("NO\n");
    }
    return 0;
}

/// in the name of ALLAH

//#include <bits\stdc++.h>
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <stdio.h>
#include <set>
#include <map>
#include <cmath>
#include <stack>
#include <deque>
#include <sstream>
#include <string>
#include <fstream>
#include <utility>

#define bits(a) __builtin_popcount(a)
#define ll long long
#define FB 0xFF
int dx[] = {1 , -1 , 0 , 0 };
int dy[] = {0 ,  0 , 1 , -1};
const int mod = (int)1e9 + 7;
const int oo = 1<<30;
const ll loo = (ll)1<<60;
const double pi = 3.14159265359;
const int N = (int) 1e2 + 2;
const double ABS = 1e-15;

using namespace std;
ll n , m , k;
pair<pair<int , ll> , pair<ll , int> > pr[N];
map<ll , ll>memo[N][N];
int fir;
ll ans = -loo , firval;
ll solve(int idx , int rm , ll val) {
    if(!rm)
        return 0;
    if(idx == m)
        return -loo;
    if(memo[idx][rm][val] != 0)
        return memo[idx][rm][val];
    int nextIdx = idx;
    while(pr[nextIdx].first.first == pr[idx].first.first)
        nextIdx++;
    ll res = solve(idx+1 , rm , val);
    if(rm == n) {
        for(ll i = pr[idx].second.first; i <= pr[idx].first.second; i++) {
            ll t = solve(nextIdx , rm-1 , i) + i ;
            if(t > ans) {
                ans = t;
                fir = idx;
                firval = i;
            }
            res = max(res , t);
        }
    } else {
        if(pr[idx].second.first <= val + k && pr[idx].first.second >= val + k) {
            ll t = solve(nextIdx , rm-1 , val+k) + val+k;
            res = max(res , t);
        }
        if(pr[idx].second.first <= val * k && pr[idx].first.second >= val * k) {
            ll t = solve(nextIdx , rm-1 , val*k)+ (val*k);
            res = max(res , t);
        }
    }
    return memo[idx][rm][val] = res;
}
vector<pair<int , ll> > res;

void build(int idx , int rm , ll v) {
    if(idx == m || rm == 0)
        return;
    ll r = memo[idx][rm][v];
    if(r == solve(idx+1 , rm  , v))
        return build(idx+1 , rm , v);
    int nextIdx = idx;
    while(pr[nextIdx].first.first == pr[idx].first.first)
        nextIdx++;
    if(rm == n) {
        build(nextIdx , rm - 1 , v);
        res.push_back(make_pair(pr[idx].second.second , v));
        return;
    } else {
        if(r == solve(nextIdx , rm - 1 , v + k) + v + k) {
            build(nextIdx , rm - 1 , v+k);
            res.push_back(make_pair(pr[idx].second.second , v+k));
            return;
        }
        if(r == solve(nextIdx , rm - 1 , v * k) + v * k) {
            build(nextIdx , rm - 1 , v*k);
            res.push_back(make_pair(pr[idx].second.second , v*k));
            return;
        }

    }
}

int main() {
    scanf("%d%d%d" , &n , &m , &k);
    for(int i = 0,a; i < m; i ++) {
        scanf("%I64d %I64d %d" , &pr[i].second.first , &pr[i].first.second , &pr[i].first.first);
        pr[i].second.second = i + 1;
    }
    sort(pr , pr + m);
    ll foo = solve(0 , n , 0);
    if(foo < 0)
        puts("NO\n");
    else {
        if(n > 1)
            build(fir , n , firval);
        else
            res.push_back(make_pair(pr[fir].second.second , firval));
        puts("YES\n");
        for(int i = res.size() - 1; i >= 0; i --) {
            printf("%d %I64d\n" , res[i].first , res[i].second);
        }
    }
}

#include <iostream>
#include <cstdio>
#include <vector>
#include <list>
#include <cmath>
#include <fstream>
#include <algorithm>
#include <string>
#include <queue>
#include <set>
#include <map>
#include <complex>
#include <iterator>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <stack>
#include <climits>
#include <deque>
#include <bitset>
#include <cassert>
#include <ctime>
using namespace std;

typedef long long ll;
typedef pair<int,int> pii;
const int dy[]={-1,0,1,0},dx[]={0,1,0,-1};
// adjust problem by problem
const double EPS=1e-8;
const double PI=acos(-1.0);
#ifdef __GNUC__
int popcount(int n){return __builtin_popcount(n);}
int popcount(ll n){return __builtin_popcountll(n);}
#endif
#ifndef __GNUC__
template<class T> int popcount(T val){
  val = val - ((val >> 1) & 0x55555555);
  val = (val & 0x33333333) + ((val >> 2) & 0x33333333);
  val = (val + (val >> 4)) & 0x0f0f0f0f;
  val += val >> 8;
  val += val >> 16;
  return (int)(val & 0x0000003f);
}
#endif
template<class T>int SIZE(T a){return a.size();}
template<class T>string IntToString(T num){string res;stringstream ss;ss<<num;return ss.str();}
template<class T>T StringToInt(string str){T res=0;for(int i=0;i<SIZE(str);i++)res=(res*10+str[i]-'0');return res;}
template<class T>T gcd(T a,T b){if(b==0)return a;return gcd(b,a%b);}
template<class T>T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T> void PrintSeq(T &a,int sz){for(int i=0;i<sz;i++){cout<<a[i];if(sz==i+1)cout<<endl;else cout<<' ';}}
bool EQ(double a,double b){return abs(a-b)<EPS;}
void fastStream(){cin.tie(0);std::ios_base::sync_with_stdio(0);}
vector<string> split(string str,char del){
  vector<string> res;
  for(int i=0,s=0;i<SIZE(str);i++){
    if(str[i]==del){if(i-s!=0)res.push_back(str.substr(s,i-s));s=i+1;}
    else if(i==SIZE(str)-1){res.push_back(str.substr(s));}
  }
  return res;
}

typedef pair<ll,ll> pll;
int N,M,K;
map<ll,ll> dp[51][51];
map<ll,pair<ll,pll> > tos[51][51];
const ll INF=1LL<<60;
pair<pll,pll> elms[51];

ll dfs(int pos,int need,ll prv){
  if(dp[pos][need].count(prv))
    return dp[pos][need][prv];
  ll res=need==0?0:-INF;
  if(pos==M){

  }
  else{
    if(pos-1>=0
       &&elms[pos-1].first.first==elms[pos].first.first){
      res=dfs(pos+1,need,prv);
    }
    else{
      // 何番目の要素を取るか
      for(int i=pos;i<M;i++){
        if(need==N){
          ll a=elms[i].second.first;
          ll b=elms[i].second.second;
          if(need-1>=0){
            for(ll j=a;j<=b;j++)
              if(dfs(i+1,need-1,j)!=-INF)
                res=max(res,dfs(i+1,need-1,j)+j);
          }
        }
        else{
          ll a=elms[i].second.first;
          ll b=elms[i].second.second;
          // +kか*kかして次の要素を探す
          if(a<=prv+K&&prv+K<=b&&need-1>=0
             &&dfs(i+1,need-1,prv+K)!=-INF)
            res=max(res,dfs(i+1,need-1,prv+K)+prv+K);
          if(a<=prv*K&&prv*K<=b&&need-1>=0
             &&dfs(i+1,need-1,prv*K)!=-INF)
            res=max(res,dfs(i+1,need-1,prv*K)+prv*K);
        }
      }
    }
  }
  return dp[pos][need][prv]=res;
}

void rec(int pos,int need,ll prv){
  if(need==0)return;
  ll res=need==0?0:-INF;
  pair<pii,ll> nxt;
  if(pos==M){
  }
  else{
    if(pos-1>=0
       &&elms[pos-1].first.first==elms[pos].first.first
       &&dp[pos+1][need][prv]!=-INF){
      rec(pos+1,need,prv);
    }
    else{
      // 何番目の要素を取るか
      for(int i=pos;i<M;i++){
        if(need==N){
          ll a=elms[i].second.first;
          ll b=elms[i].second.second;
          if(need-1>=0){
            for(ll j=a;j<=b;j++){
              if(dp[i+1][need-1][j]!=-INF
                 &&dp[i+1][need-1][j]+j>res){
                res=dp[i+1][need-1][j]+j;
                nxt=pair<pii,ll>(pii(i+1,need-1),j);
              }
            }
          }
        }
        else{
          ll a=elms[i].second.first;
          ll b=elms[i].second.second;
          // +kか*kかして次の要素を探す
          if(a<=prv+K&&prv+K<=b&&need-1>=0
             &&dfs(i+1,need-1,prv+K)!=-INF
             &&dfs(i+1,need-1,prv+K)+prv+K>res){
            res=dfs(i+1,need-1,prv+K)+prv+K;
            nxt=pair<pii,ll>(pii(i+1,need-1),prv+K);
          }
          if(a<=prv*K&&prv*K<=b&&need-1>=0
             &&dfs(i+1,need-1,prv*K)!=-INF
             &&dfs(i+1,need-1,prv*K)+prv*K>res){
            res=dfs(i+1,need-1,prv*K)+prv*K;
            nxt=pair<pii,ll>(pii(i+1,need-1),prv*K);
          }
        }
      }
      const int idx=nxt.first.first-1;
      cout<<elms[idx].first.second+1<<" ";
      cout<<nxt.second<<endl;
      rec(nxt.first.first,nxt.first.second,nxt.second);
    }
  }
}

int main(){
  //ifstream cin("in.txt");
  cin>>N>>M>>K;
  for(int i=0;i<M;i++){
    ll a,b,c;
    cin>>a>>b>>c;
    elms[i]=make_pair(pll(c,i),pll(a,b));
  }
  sort(elms,elms+M);
  ll res=dfs(0,N,0);
  if(res==-INF)cout<<"NO"<<endl;
  else{
    cout<<"YES"<<endl;
    rec(0,N,0);
  }
  return 0;
}

#include <cstdio>
#include <iostream>
#include <sstream>
#include <string>
#include <cstring>
#include <algorithm>
#include <set>
#include <map>
#include <deque>
#include <queue>
#include <vector>
#include <cmath>
#include <ctime>
#include <cassert>

using namespace std;

typedef long long ll;

template<typename T> int size(T & a) { return (int) a.size(); }
template<typename T> T sqr(T a) {return a * a; }

#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define _(a, b) memset((a), (b), sizeof(a))
#define REP(i, a, b) for(int i=(a); i<(b); ++i)
#define all(a) a.begin(),a.end()

struct pos {
    ll a, b;
    int c;
    int id;
    pos(ll a= 0, ll b= 0, int c = 0, int id=0):
    a(a),b(b),c(c),id(id){ }
    bool operator < (const pos& o) const {
        if (c != o.c)
            return c < o.c;
        return id < o.id;
    }
};

ll dm[52][52][110];
int ps[52][52][110];
int pl[52][52][110];
pos subj[100];

ll A[100],B[100];
int C[100];

bool update(ll &g, ll r) {
    if (g < r) {
        g = r;
        return true;
    }
    return false;
}
int main(){
#ifdef air
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif

    int n,m,k;
    cin >> n >> m >> k;
    REP(i,0,m){
        ll a, b;
        int c;
        cin >> a >> b >> c;
        A[i] = a;
        B[i] = b;
        C[i] = c;
        subj[i] = pos(a,b,c,i);
    }
    sort(subj, subj + m);
    _(dm,255);
    dm[0][0][0] = 0;

    REP(day,0,n) REP(s,0,m) REP(l,0,(int)(subj[s].b-subj[s].a+1)) {
        ll r = dm[day][s][l];
        if (r == -1) continue;
        REP(ns,(day==0?s:s+1),m) {
            if (day != 0 && subj[ns].c == subj[s].c)
                continue;
            for(ll ta = subj[ns].a; ta <= subj[ns].b; ++ta) {
                ll g = subj[s].a + l;
                if (day == 0 ||
                     ((ta - g == k || ta == k*g))) {
                        if (update(dm[day + 1][ns][int(ta - subj[ns].a)], r + ta)){
                            ps[day + 1][ns][int(ta - subj[ns].a)] = s;
                            pl[day + 1][ns][int(ta - subj[ns].a)] = l;
                        }
                    }
            }
        }
    }
    int ts = -1, tl = -1;
    ll res = -1;
    for(int i=0;i<m;++i)
        for(int j=0;j<=100;++j){
            ll r = dm[n][i][j];
            if (r != -1) {
                if (r > res) {
                    res = r;
                    ts = i;
                    tl = j;
                }
            }
        }
    if(res == -1){
        cout << "NO";
        return 0;
    }

    vector<pair<int, ll> > path ;
    int td = n;
    while (td > 0) {
        path.pb(mp(subj[ts].id, subj[ts].a + tl));
        int _ts = ps[td][ts][tl];
        int _tl = pl[td][ts][tl];
        -- td;
        ts = _ts;
        tl = _tl;
    }
    cout << "YES" << endl;
    reverse(all(path));
    REP(i,0,size(path)){
        cout << path[i].first + 1 << " " << path[i].second << endl;
    }
#ifdef air
    printf("\n%.3lf\n", clock() * 1.0 / CLOCKS_PER_SEC);
#endif

}

#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <algorithm>
#include <set>
#include <iomanip>
#include <queue>
#include <cmath>
#include <sstream>
using namespace std;

const int INF = 20000000;
typedef long long ll;
const ll mod = 1e9 + 7;

ll dp[51][50][101][3];

struct lesson
{
    ll a;
    ll b;
    ll c;
    int num;
    bool operator() (const lesson &A,const lesson &B)
    {
        return A.c < B.c;
    }
};

int main(){
    ios::sync_with_stdio(false);
    int n,m,k;
    cin >> n >> m >> k;
    vector< lesson > A(m);
    for(int i=0;i<m;++i)
    {
        cin >> A[i].a >> A[i].b >> A[i].c;
        A[i].num = i+1;
    }
    sort(A.begin(),A.end(),lesson());
    for(int z=0;z<=n;++z)
    {
        for(int i=0;i<m;++i)
        {
            for(ll j=0;j<=100;++j)
            {
                dp[z][i][j][1] = dp[z][i][j][2] = -1;
                if(z>0) dp[z][i][j][0] = -2000000000000000000ll;
                else if(j<=A[i].b-A[i].a)dp[z][i][j][0] = j+A[i].a;
                else dp[z][i][j][0] = -2000000000000000000ll;
            }
        }
    }
    for(int i=0;i<m;++i)
    {
        for(ll j=0;j<=A[i].b-A[i].a;++j)
        {
          // cout << (dp[0][i][j][0]==-20000000000000000ll?-1:dp[0][i][j][0]) << ' ';
        }
     //   cout << endl;
    }
    for(int z=1;z<n;++z)
    {
        for(int i=m-2;i>=0;--i)
        {
            for(int j=i+1;j<m;++j)
            {
                if(A[i].c==A[j].c) continue;
                for(ll p=0; p<=A[j].b-A[j].a; ++p)
                {
                    ll temp;
                    if((0ll+A[j].a+p)%k==0)
                    {
                        temp = (p+A[j].a)/k;
                        temp -= A[i].a;
                        if(temp>=0 && temp<=100)
                        {
                            if(dp[z][j][p][0]< A[j].a + p + dp[z-1][i][temp][0])
                            {
                                dp[z][j][p][0] = A[j].a + p + dp[z-1][i][temp][0];
                                dp[z][j][p][1] = i;
                                dp[z][j][p][2] = temp;
                            }
                        }

                    }
                    temp = (p+A[j].a)-k;
                    temp -= A[i].a;
                    if(temp>=0 && temp<=100)
                    {
                        if(dp[z][j][p][0]<A[j].a + p + dp[z-1][i][temp][0])
                        {
                            dp[z][j][p][0] = A[j].a + p + dp[z-1][i][temp][0];
                            dp[z][j][p][1] = i;
                            dp[z][j][p][2] = temp;
                        }
                    }
                }
            }
        }
    }

    ll Max = -2;
    int i,o,ii=1000,oo=1000;
    for(i=0;i<m;++i)
    {
        for(o=0;o<=100;++o)
        {
            if(Max<dp[n-1][i][o][0])
            {
                Max = dp[n-1][i][o][0];
                ii = i;oo = o;
               // cout << Max << endl;
            }
        }
    }
    if(Max>0)
    {
        cout << "YES\n";
        vector< pair<int,int> > ansss;
        ansss.push_back( make_pair(ii,oo));
        int l = n-1;
        int cur = dp[n-1][ii][oo][1],curo = dp[n-1][ii][oo][2];
        while(cur!=-1)
        {
            ansss.push_back( make_pair(cur,curo));
            ii = dp[l-1][cur][curo][1];
            oo = dp[l-1][cur][curo][2];
            cur = ii;
            curo = oo;
            --l;
        }

        for(int i=n-1;i>=0;--i)
        {
            cout << A[ansss[i].first].num << ' ' << A[ansss[i].first].a + ansss[i].second << endl;
        }
    }
    else
    {
        cout << "NO";

    }
    return 0;
}

#include <iostream>
#include <cstring>
#include <cstdio>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <limits.h>
#include <utility>
#include <sstream>
#include <algorithm>

using namespace std;
#define LL long long
struct NODE{
    LL a, b, c;
    int num;
};
NODE p[55];
int n, m;
LL K;
LL f[55][55][110];
int cmp(NODE a, NODE b)
{
    if(a.c == b.c){
        return a.num < b.num;
    }else return a.c < b.c;
}
void print_path(int dep, int j, int k)
{
    if(!dep) return ;
    LL tmp = k + p[j].a;
    for(int i = 1; i < j; i++){
        if(tmp - K >= p[i].a && tmp - K <= p[i].b && f[dep][j][k] == f[dep - 1][i][tmp - K - p[i].a] + tmp){
            print_path(dep - 1, i, tmp - K - p[i].a);
            break;
        }
        if(tmp % K == 0 && tmp / K >= p[i].a && tmp / K <= p[i].b && f[dep][j][k] == f[dep - 1][i][tmp / K - p[i].a] + tmp){
            print_path(dep - 1, i, tmp / K - p[i].a);
            break;
        }
    }
    //printf("%d: ", dep);
    printf("%d %I64d\n", p[j].num, tmp);
}
int main()
{
    //freopen("input.txt", "r", stdin);
    scanf("%d %d %I64d", &n, &m, &K);
    for(int i = 1; i <= m; i++){
        scanf("%I64d %I64d %I64d", &p[i].a, &p[i].b, &p[i].c);
        p[i].num = i;
    }
    sort(p + 1, p + 1 + m, cmp);
    memset(f, 0, sizeof(f));
//    for(int i = 1; i <= m; i++)
//        printf("%d: %d %d %d\n", p[i].num, p[i].a, p[i].b, p[i].c);
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= m; j++){
            for(LL k = p[j].a; k <= p[j].b; k++){
                if(i == 1) f[i][j][k - p[j].a] = k;
                else if(f[i - 1][j][k - p[j].a]){
                    //printf("f[%d][%d][%d] = %d\n", i - 1, j, k - p[j].a, f[i - 1][j][k - p[j].a]);
                    for(int l = j + 1; l <= m; l++){
                        if(p[l].c > p[j].c){
                            LL tmp;
                            if(k + K >= p[l].a && k + K <= p[l].b){
                                tmp = k + K - p[l].a;
                                f[i][l][tmp] = max(f[i][l][tmp], f[i - 1][j][k - p[j].a] + k + K);
                            }
                            if(k * K >= p[l].a && k * K <= p[l].b){
                                tmp = k * K - p[l].a;
                                f[i][l][tmp] = max(f[i][l][tmp], f[i - 1][j][k - p[j].a] + k * K);
                            }
                        }
                    }
                }
            }
        }
    }
    LL Maxx = 0;
    int id, jd;
    for(int j = n; j <= m; j++){
        for(int i = 0; i <= p[j].b - p[j].a; i++){
            //printf("f[%d][%d][%d] = %d\n", n, j, i, f[n][j][i]);
            if(Maxx < f[n][j][i]){
                Maxx = f[n][j][i];
                id = j;
                jd = i;
            }
        }
    }
    //printf("%d\n", f[1][1][4]);
    if(Maxx){
        //printf("%d %d %d\n", Maxx, id, jd);
        printf("YES\n");
        print_path(n, id, jd);
    }else printf("NO\n");
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
/* YOU DO NOT TELL ME WHETHER MY SOLUTION IS RIGHT, SKYNET!!! */
#define forr(n) for (int i=0;i<n;i++)
#define pb push_back
#define len size()
#define mems(a,v) memset(a,v,(int)sizeof a)
#define all(v) v.begin(),v.end()
#define sz(v)  ((int)((v).size()))
#define rall(v) v.rbegin(),v.rend()
#define mp(x,y) make_pair(x,y)
#define fr first
#define se second
//#define p printf
#define sc scanf
#define V vector
#define nl cout<<"\n";
#define deb(s) cout<<"here"<<s<<endl;
#define PI (double)3.14159265358979323846264338
int X[]={1,0,-1,0},Y[]={0,1,0,-1};
string DIR="SENW";
#define MAX 1000000007
#define MOD 1000000009
#define md(x,y) ((x+y)%y)
#define oo (int)1e9
typedef pair<int,int> ii;
typedef unsigned long long ULL;
typedef long long LL;
typedef int I;
ULL summ(ULL n){return ((n*n)+n)/2;}
ULL gcd (ULL a,ULL b){ULL c;while(a!=0){c=a;a=b%a;b=c;}return b;}
double dist(LL x, LL y ,LL xx,LL yy){return sqrt((x-xx)*(x-xx)+ (y-yy)*(y-yy));}
///Actual solution starts here////////////////////Sherbi7y///////////////////////////////
//map< pair<ii,int>  ,LL> ss;
LL dp[55][111][55][55];
int k,M,N;
LL a[55],b[55], c[55],d[55];
bool success;
LL make(int ind, LL las,int kk,int m)
{
    if(m==N){
        success=1;
        return 0;
    }
    if(ind ==M)
        return 0;
    if(dp[ind][las][kk][m]!= -1)return dp[ind][las][kk][m];

    if(N-m > M-ind)
        return 0;
    LL maxx=make(ind+1,las,kk,m);
    if(m==0)
        for(LL i=b[ind];i>=a[ind];i--)
                maxx=max(maxx,i+make(ind+1,i-a[ind],ind,1));

    else if(c[ind]>c[kk])
    {
        LL h=(las+a[kk])*k;
        if(h>=a[ind]&&h<=b[ind])
            maxx=max(maxx,h+make(ind+1,h-a[ind],ind,m+1));
        h=(las+a[kk])+k;
        if(h>=a[ind]&&h<=b[ind])
            maxx=max(maxx,h+make(ind+1,h-a[ind],ind,m+1));
    }
    return dp[ind][las][kk][m]=maxx;

}
string smake(int ind, LL las,int kk,int m)
{
    if(m==N)
        return "";
    if(ind ==M)
        return "";
    if(N-m > M-ind)
        return "";
    int p=-1;
    LL maxx=dp[ind+1][las][kk][m];
    if(m==0)
    {
        for(LL i=a[ind];i<=b[ind];i++)
            {
                if (maxx< i+dp[ind+1][i-a[ind]][ind][1])
                {
                    maxx=i+dp[ind+1][i-a[ind]][ind][1];
                    p=i-a[ind];
                }
            }
            if(p==-1) return ""+smake(ind+1,las,kk,m);
            string s;
            s+=(char)ind;
            s+=(char)p;
            return s+smake(ind+1, p, ind,1);
    }

    else if(c[ind]>c[kk])
    {
        LL h=(las+a[kk])*k;
        if(h>=a[ind]&&h<=b[ind])
        {
            if(maxx<h+dp[ind+1][h-a[ind]][ind][m+1]){
                maxx=h+dp[ind+1][h-a[ind]][ind][m+1];
                p=1;
            }
        }
        LL hh=(las+a[kk])+k;
        if(hh>=a[ind]&&hh<=b[ind])
        {
            if(maxx<hh+dp[ind+1][hh-a[ind]][ind][m+1]){
                maxx=hh+dp[ind+1][hh-a[ind]][ind][m+1];
                p=2;
            }
        }
        if(p==-1) return ""+smake(ind+1,las,kk,m);
        else if(p==1) {string s;
            s+=(char)ind;
            s+=(char)(h-a[ind]);
            return s+smake(ind+1,h-a[ind],ind,m+1);}
        else {
            string s;
            s+=(char)ind;
            s+=(char)(hh-a[ind]);
           return s+smake(ind+1,hh-a[ind],ind,m+1);}
    }
    return ""+smake(ind+1,las,kk,m);

}
int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);
    cout.tie(0);//Better safe than sorry?
    #ifndef ONLINE_JUDGE
        freopen("a1.in","r",stdin);//freopen("A2.out","w",stdout);
    #endif
    while(cin>>N>>M>>k)
    {
        mems(dp,-1);
        success=0;
        vector<vector<LL> > v(M,vector<LL>(4,0));
        forr(M)
        {
            cin>>v[i][1]>>v[i][2]>> v[i][0];
            v[i][3]=i;
        }
        sort(all(v));
        forr(M)
        {
            c[i]=v[i][0];
            a[i]=v[i][1];
            b[i]=v[i][2];
            d[i]=v[i][3];
        }
        LL T=make(0,0,0,0);
        if(!success)
            cout<<"NO\n";
        else
        {
            string S = smake(0,0,0,0);
            cout<<"YES\n";
            for(int i=0;i<S.len;i+=2)
            cout<<d[S[i]]+1<<" "<<a[S[i]]+S[i+1]<<"\n";
            cout<<endl;
        }

    }
    return 0;
}

#include <bits/stdc++.h>
#define err(args...) {}
#ifdef DEBUG
#include "_debug.cpp"
#endif
using namespace std;
using ll = long long;
template <typename T> using lim = numeric_limits<T>;
struct subj {
    ll a, b;
    int c, i;
    int size() {
        return b - a + 1;
    }
};
const int N = 50, M = 50, VAL = 100;
subj s[M+1];
ll opt[N+1][M+1][VAL+1] = {};
struct state {
    int n, i, val;
} pred[N+1][M+1][VAL+1];
map<ll, int> opt_m[N+1][M+1];
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int n, m, k;
    cin >> n >> m >> k;
    for(int i = 1; i <= m; i++) {
        cin >> s[i].a >> s[i].b >> s[i].c;
        s[i].i = i;
    }
    sort(s + 1, s + 1 + m, [](auto& s1, auto& s2) {
        return s1.c < s2.c;
    });
    for(int nn = 1; nn <= n; nn++) {
        for(int i = 1; i <= m; i++) {
            for(int val1 = 0; val1 < s[i].size(); val1++) {
                ll& ans = opt[nn][i][val1];
                ll ival = s[i].a + val1;
                if(nn == 1) {
                    ans = ival;
                } else {
                    ans = 0;
                    for(int j = 1; j < i; j++) {
                        if(s[j].c < s[i].c and opt_m[nn - 1][j].count(ival)) {
                            int val2 = opt_m[nn - 1][j][ival];
                            if(opt[nn - 1][j][val2]
                            and ans < ival + opt[nn - 1][j][val2]) {
                                ans = ival + opt[nn - 1][j][val2];
                                pred[nn][i][val1] = {nn - 1, j, val2};
                            }
                        }
                    }
                }
                if(ans and ans > opt[nn][i][opt_m[nn][i][k + ival]]) opt_m[nn][i][k + ival] = val1;
                if(ans and ans > opt[nn][i][opt_m[nn][i][k * ival]]) opt_m[nn][i][k * ival] = val1;
            }
        }
    }
    int opti = 1, optval = 0;
    for(int i = 1; i <= m; i++) {
        for(int val = 0; val < s[i].size(); val++) {
            if(opt[n][opti][optval] < opt[n][i][val]) {
                opti = i, optval = val;
            }
        }
    }
    if(opt[n][opti][optval]) {
        cout << "YES" << endl;
        vector<pair<int, ll>> ans;
        state curr = {n, opti, optval};
        while(curr.n > 0) {
            ans.push_back({s[curr.i].i, s[curr.i].a + curr.val});
            curr = pred[curr.n][curr.i][curr.val];
        }
        reverse(ans.begin(), ans.end());
        for(auto [x, y] : ans) {
            cout << x << " " << y << endl;
        }
        return 0;
    }
    cout << "NO" << endl;
    return 0;
}

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct TSubject
{
	__int64 a, b;
	int nmb, c;
} sb[100];
bool operator < (const TSubject &x, const TSubject &y)
{
	return x.c < y.c;
}
int n, m, k, cur = 0;
__int64 d[50][101][51];
pair<int, int> p[50][101][51], state;
int main()
{
	cin >> n >> m >> k;
	for (int i = 0; i < m; ++i)
	{
		cin >> sb[i].a >> sb[i].b >> sb[i].c;
		sb[i].nmb = i + 1;
	}
	sort(sb, sb + m);
	state = make_pair(0, 0);
	for (int i = 0; i < m; ++i)
		for (int j = 0; j <= sb[i].b - sb[i].a; ++j)
		{
			d[i][j][1] = sb[i].a + j;
			for (int z = 2; z <= n; ++z)
			{
				d[i][j][z] = -1;
				for (int last = i - 1; last >= 0; --last)
					if (sb[last].c < sb[i].c)
						for (int t = 0; t <= 1; ++t)
						{
							__int64 temp = sb[i].a + j;
							bool yes = (t == 0) ? temp % k == 0 : temp >= k;
							temp = (t == 0) ? temp / k : temp - k;
							temp -= sb[last].a;
							if (!yes) continue;
							if (temp >= 0 && temp <= sb[last].b - sb[last].a
								&& d[last][temp][z - 1] != -1
								&& d[last][temp][z - 1] + sb[i].a + j > d[i][j][z])
							{
								d[i][j][z] = d[last][temp][z - 1] + sb[i].a + j;
								p[i][j][z] = make_pair(last, temp);
							}
						}
			}
			if (d[i][j][n] > d[state.first][state.second][n])
				state = make_pair(i, j);
		}
	if (d[state.first][state.second][n] == -1)
	{
		cout << "NO";
		return 0;
	}
	vector<pair<int, __int64> > ans;
	for (int i = n; i >= 1; --i)
	{
		ans.push_back(make_pair(sb[state.first].nmb, sb[state.first].a + state.second));
		state = p[state.first][state.second][i];
	}
	cout << "YES" << endl;
	for (int i = n - 1; i >= 0; --i)
		cout << ans[i].first << ' ' << ans[i].second << endl;
	return 0;
}

#include <cstdio>
#include <algorithm>
#include <utility>
#include <stack>
using namespace std;
const int MAXN=50;
int c[MAXN], d[MAXN], psub[MAXN][MAXN][101], phw[MAXN][MAXN][101];
long long f[MAXN][MAXN][101]={0}, a[MAXN], b[MAXN];
struct day
{
    int sub;
    int d;
};
int main()
{
    int n, m, k, i, j, jj, u, v;
    long long t, tt, tmp, max_sum=0;
    day last_day, day0, day1;
    scanf("%d%d%d", &n, &m, &k);
    for(i=0; i<m; ++i)
    {
        scanf("%I64d%I64d%d", a+i, b+i, c+i);
        d[i]=b[i]-a[i];
    }
    for(j=0; j<m; ++j)
        for(u=0; u<=d[j]; ++u)
            f[0][j][u]=a[j]+u;
    for(i=0; i<n-1; ++i)
        for(j=0; j<m; ++j)
            for(u=0; u<=d[j]; ++u)
                for(jj=0; jj<m; ++jj)
                    if(f[i][j][u] && c[jj]>c[j])
                    {
                        t=a[j]+u;
                        tt=t+k;
                        if(a[jj]<=tt && tt<=b[jj])
                        {
                            tmp=f[i][j][u]+tt;
                            if(f[i+1][jj][tt-a[jj]]<tmp)
                            {
                                v=tt-a[jj];
                                f[i+1][jj][v]=tmp;
                                psub[i+1][jj][v]=j;
                                phw[i+1][jj][v]=u;
                            }
                        }
                        tt=t*k;
                        if(tt/k==t && a[jj]<=tt && tt<=b[jj])
                        {
                            tmp=f[i][j][u]+tt;
                            if(f[i+1][jj][tt-a[jj]]<tmp)
                            {
                                v=tt-a[jj];
                                f[i+1][jj][v]=tmp;
                                psub[i+1][jj][v]=j;
                                phw[i+1][jj][v]=u;
                            }
                        }
                    }
    for(j=0; j<m; ++j)
        for(u=0; u<=d[j]; ++u)
            if(max_sum<f[n-1][j][u])
            {
                last_day.sub=j;
                last_day.d=u;
                max_sum=f[n-1][j][u];
            }
    if(max_sum)
    {
        puts("YES");
        stack<day> ans;
        ans.push(last_day);
        day0=last_day;
        for(i=n-1; i>0; --i)
        {
            day1.sub=psub[i][day0.sub][day0.d];
            day1.d=phw[i][day0.sub][day0.d];
            ans.push(day1);
            day0=day1;
        }
        while(!ans.empty())
        {
            day1=ans.top();
            t=a[day1.sub]+day1.d;
            printf("%d %I64d\n", day1.sub+1, t);
            ans.pop();
        }
    }
    else
        puts("NO");
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef double db;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define rep(i,l,r) for(int i=(l);i<(r);++i)
#define per(i,l,r) for(int i=(r)-1;i>=(l);--i)
#define dd(x) cout << #x << " = " << x << ", "
#define de(x) cout << #x << " = " << x << endl
//-------
const int N = 55;
const int M = 5007;
int n, m, K, idv[N];
vector<ll> V;
struct Node {
	ll a, b;
	int c, id;
	void in() {
		scanf("%lld%lld%d", &a, &b, &c);
	}
	bool operator<(const Node &p) const {
		return c < p.c;
	}
} nd[N];
ll dp[N][N][M];
pii go[N][N][M];
void solve() {
	memset(dp, -1, sizeof(dp));
	memset(go, -1, sizeof(go));
	dp[0][0][0] = 0;
	int t = 0;
	for (int ni = 0, nj; ni < n; ni = nj) {
		++t, nj = ni + 1;
		while (nj < n && nd[ni].c == nd[nj].c) ++nj;
		rep(i, 0, m + 1) rep(j, 0, sz(V))
			dp[t][i][j] = dp[t - 1][i][j], go[t][i][j] = mp(0, 0);
		rep(k, ni, nj) {
			rep(j, idv[k], idv[k] + nd[k].b - nd[k].a + 1) {
				if (dp[t][1][j] < dp[t - 1][0][0] + V[j]) {
					dp[t][1][j] = dp[t - 1][0][0] + V[j];
					go[t][1][j] = mp(3, k);
				}
			}
			rep(i, 0, m) {
				int p;
				// +
				p = lower_bound(all(V), nd[k].a - K) - V.begin();
				for (int j = p; j < sz(V) && V[j] + K <= nd[k].b; ++j) {
					if (dp[t - 1][i][j] == -1) continue;
					int j2 = idv[k] + V[j] + K - nd[k].a;
					assert(V[j2] == V[j] + K);
					if (dp[t][i + 1][j2] < dp[t - 1][i][j] + V[j2]) {
						dp[t][i + 1][j2] = dp[t - 1][i][j] + V[j2];
						go[t][i + 1][j2] = mp(1, k);
					}
				}
				// *
				p = lower_bound(all(V), (nd[k].a + K - 1) / K) - V.begin();
				for (int j = p; j < sz(V) && V[j] * K <= nd[k].b; ++j) {
					if (dp[t - 1][i][j] == -1) continue;
					int j2 = idv[k] + V[j] * K - nd[k].a;
					assert(V[j2] == V[j] * K);
					if (dp[t][i + 1][j2] < dp[t - 1][i][j] + V[j2]) {
						dp[t][i + 1][j2] = dp[t - 1][i][j] + V[j2];
						go[t][i + 1][j2] = mp(2, k);
					}
				}
			}
		}
//		rep(i, 0, m + 1) rep(j, 0, sz(V)) if (~dp[t][i][j])
//			printf("dp[%d][%d][%lld] = %lld\n", t, i, V[j], dp[t][i][j]);
	}
	int p = 0;
	rep(i, 0, sz(V)) if (dp[t][m][p] < dp[t][m][i])
		p = i;
	if (dp[t][m][p] == -1) {
		puts("NO");
		return ;
	}
	vector<pair<int, ll> > ans;
	for (int i = t, j = m, k = p; i > 0; --i) {
		pii g = go[i][j][k];
		if (g.fi == 0) {
		} else if (g.fi == 1) {
			ans.pb(mp(nd[g.se].id, V[k]));
			--j, k = lower_bound(all(V), V[k] - K) - V.begin();
		} else if (g.fi == 2) {
			ans.pb(mp(nd[g.se].id, V[k]));
			--j, k = lower_bound(all(V), V[k] / K) - V.begin();
		} else {
			ans.pb(mp(nd[g.se].id, V[k]));
			break;
		}
	}
	reverse(all(ans));
	puts("YES");
	rep(i, 0, sz(ans))
		printf("%d %lld\n", ans[i].fi, ans[i].se);
}
int main() {
	scanf("%d%d%d", &m, &n, &K);
	rep(i, 0, n) nd[i].in(), nd[i].id = i + 1;
	V.pb(0);
	rep(i, 0, n)
		for (ll j = nd[i].a; j <= nd[i].b; ++j)
			V.pb(j);
	sort(all(V)); V.erase(unique(all(V)), V.end());
	sort(nd, nd + n);
	rep(i, 0, n) idv[i] = lower_bound(all(V), nd[i].a) - V.begin();
	solve();
	return 0;
}

#pragma comment(linker, "/STACK:16777216")

#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <unordered_map>
using namespace std;

#define FOR(i, n) for (int i = 0; i < (int) (n); i++)
#define RFOR(i, n) for (int i = (int) (n) - 1; i >= 0; i--)
#define CL(x, v) memset(x, v, sizeof(x))
#define ALL(x) x.begin(), x.end()
#define PB push_back
#define MP make_pair

typedef long long LL;
typedef vector<int> VI;
typedef vector<string> VS;
typedef pair<int, int> PII;

const int N = 50 + 5;
const int M = 100 + 5;

int ttc = 0;
char tt[N * N * M];

int n, m, k;
LL a[N], b[N];
int c[N];

struct T
{
    int n, c;
    LL x;

    T()
    {
        n = c = x = 0;
    }

    T(int _n, int _c, LL _x)
    {
        n = _n;
        c = _c;
        x = _x;
    }

    void * operator new (size_t size)
    {
        void * res = (void *) (tt + ttc);
        ttc += sizeof(T);
        return res;
    }
};

struct H
{
    size_t operator () (const T & t) const
    {
        return t.n * 13 + t.c * 17 + t.x * 37;
    }
};

struct C
{
    bool operator () (const T & a, const T & b) const
    {
        return a.n == b.n && a.c == b.c && a.x == b.x;
    }
};

unordered_map<T, bool, H, C> vs;
unordered_map<T, LL, H, C> dp;
unordered_map<T, int, H, C> pr1;
unordered_map<T, LL, H, C> pr2;

LL D(T t)
{
    if (t.n == n) return 0;

    bool & vis = vs[t];
    LL & res = dp[t];
    int & pre1 = pr1[t];
    LL & pre2 = pr2[t];

    if (vis) return res;

    vis = 1;
    res = -1;

    FOR(i, m) if (c[i] > t.c)
        if (!t.n)
        {
            for (LL x = a[i]; x <= b[i]; x++)
            {
                T f(t.n + 1, c[i], x);
                LL cur = D(f);
                if (cur != -1)
                {
                    cur += x;
                    if (cur > res)
                    {
                        res = cur;
                        pre1 = i;
                        pre2 = x;
                    }
                }
            }
        }
        else
        {
            if (a[i] <= t.x + k && t.x + k <= b[i])
            {
                T f(t.n + 1, c[i], t.x + k);
                LL cur = D(f);
                if (cur != -1)
                {
                    cur += t.x + k;
                    if (cur > res)
                    {
                        res = cur;
                        pre1 = i;
                        pre2 = t.x + k;
                    }
                }
            }

            if (a[i] <= t.x * k && t.x * k <= b[i])
            {
                T f(t.n + 1, c[i], t.x * k);
                LL cur = D(f);
                if (cur != -1)
                {
                    cur += t.x * k;
                    if (cur > res)
                    {
                        res = cur;
                        pre1 = i;
                        pre2 = t.x * k;
                    }
                }
            }
        }

    return res;
}

int main()
{
#ifdef LOCAL_BUILD
    freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
#endif

    scanf("%d %d %d", &n, &m, &k);
    FOR(i, m) scanf("%I64d %I64d %d", &a[i], &b[i], &c[i]);

    T t(0, 0, 0);
    LL x = D(t);
    if (x != -1)
    {
        printf("YES\n");
        while (t.n < n)
        {
            printf("%d %I64d\n", pr1[t] + 1, pr2[t]);
            t = T(t.n + 1, c[pr1[t]], pr2[t]);
        }
    }
    else
        printf("NO\n");

    return 0;
}

#include <bits/stdc++.h>
#include <unordered_map>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef map<int,int> ump;
#define N (100)
#define maxn (N + 5)
#define pii pair<int,int>
#define mp make_pair
const ll mod = 1000000007;
const ll INF = (1e9);
ll dp[maxn][maxn][maxn];
struct sub{
	ll ai,bi,ci,id;
}s[maxn];
struct chose{
	ll id,cnt;
	chose(){}
	chose(ll x,ll y):id(x),cnt(y){}
}pre[55][55][maxn],opt[maxn];
bool cmp(sub a,sub b){
	return a.ci < b.ci;
}
int main(){
	int n,m;ll d;scanf("%d%d%lld",&n,&m,&d);
	for(int i = 1;i <= m;i++){
		scanf("%lld%lld%lld",&s[i].ai,&s[i].bi,&s[i].ci);s[i].id = i;
	}
	sort(s + 1,s + 1 + m,cmp);
	for(ll i = 1;i <= m;i++){
		for(ll j = s[i].ai;j <= s[i].bi;j++){
			dp[1][i][j - s[i].ai] = j;
		}
	}
	for(ll i = 1;i < n;i++){
		for(ll j = i;j <= m && (m - j) >= (n - i);j++){
			for(ll k = s[j].ai;k <= s[j].bi;k++){
				ll cnt = dp[i][j][k - s[j].ai];
				if(cnt){
					for(ll jj = j + 1;jj <= m;jj++){
						if(s[j].ci == s[jj].ci)continue;
						if(k * d >= s[jj].ai && k * d <= s[jj].bi){
							if(dp[i + 1][jj][k * d - s[jj].ai] < cnt + k * d){
								dp[i + 1][jj][k * d - s[jj].ai] = cnt + k * d;
								pre[i + 1][jj][k * d - s[jj].ai] = chose(j,k - s[j].ai);
							}
						}
						if(k + d >= s[jj].ai && k + d <= s[jj].bi){
							if(dp[i + 1][jj][k + d - s[jj].ai] < cnt + k + d){
								dp[i + 1][jj][k + d - s[jj].ai] = cnt + k + d;
								pre[i + 1][jj][k + d - s[jj].ai] = chose(j,k - s[j].ai);
							}
						}
					}
				}
			}
		}
	}
	ll ans = 0;
	ll jj = 0,kk = 0;
	for(int i = n;i <= m;i++){
		for(ll j = s[i].ai;j <= s[i].bi;j++){
			if(ans < dp[n][i][j - s[i].ai]){
				ans = dp[n][i][j - s[i].ai];
				jj = i;kk = j - s[i].ai;
			}
		}
	}
	if(ans){
		printf("YES\n");
		int on = 0;
		chose now = chose(jj,kk);
		for(int i = n;i >= 1;i--){
			//printf("%lld %lld\n",s[jj].id,kk + s[jj].ai);
			opt[++on] = now;
			now = pre[i][now.id][now.cnt];
			//ll jj1 = pre[i][jj][kk].id,kk1 = pre[i][jj][kk].cnt;
			//jj = jj1;kk = kk1;
		}
		for(int i = n;i >= 1;i--){
			printf("%lld %lld\n",s[opt[i].id].id,opt[i].cnt + s[opt[i].id].ai);
		}
	}
	else{
		printf("NO\n");
	}
    return 0;
}

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

using namespace std;

#pragma comment(linker, "/STACK:160000000")

struct subj {
	long long l, r;
	int ind, c, n;

	subj() {}

	subj(long long _l, long long _r, int _c, int _ind) {
		l = _l;
		r = _r;
		c = _c;
		ind = _ind;
		n = r - l + 1;
	}

	bool operator < (const subj & other) const {
		return c < other.c;
	}

	bool inside(long long q) {
		return q >= l && q <= r;
	}

};

vector <subj> a;

int dp[60][200][100][3];
long long cnt[60][200][100];
vector < pair<int, long long> > ans;

int main() {
#ifndef ONLINE_JUDGE
	freopen("in", "r", stdin);
	freopen("out", "w", stdout);
#endif
	int n, m, k;
	scanf("%d%d%d", &n, &m, &k);
	for (int i = 0; i < m; ++i) {
		long long aa, bb;
		int cc;
		scanf("%I64d%I64d%d", &aa, &bb, &cc);
		a.push_back(subj(aa, bb, cc, i));
	}

	sort(a.begin(), a.end());
	pair <int, int> res(-1, -1);
	long long cnt_res = 0;

	for (int i = 0; i < m; ++i) {
		for (int j = 0; j < a[i].n; ++j) {
			dp[i][j][0][0] = 1;
			dp[i][j][1][0] = 1;
			cnt[i][j][1] = a[i].l + j;
			if (n == 1 && (res.first == -1 || cnt[i][j][n] > cnt[res.first][res.second][n])) {
				res = make_pair(i, j);
			}
			for (int t = 0; t < i; ++t) {
				if (a[t].c < a[i].c) {
//						cerr << i << " " << j << " " << a[i].l + j - k << endl;
					if (a[t].inside(a[i].l + j - k)) {
						for (int q = 0; q < n; ++q) {
							if (dp[t][a[i].l + j - k - a[t].l][q][0] && cnt[i][j][q + 1] < cnt[t][a[i].l + j - k - a[t].l][q] + a[i].l + j) {
								cnt[i][j][q + 1] = cnt[t][a[i].l + j - k - a[t].l][q] + a[i].l + j;
								dp[i][j][q + 1][0] = 1;
								dp[i][j][q + 1][1] = t;
								dp[i][j][q + 1][2] = 0;
								if (q + 1 == n && (res.first == -1 || cnt[i][j][n] > cnt[res.first][res.second][n])) {
									res = make_pair(i, j);
								}
							}
						}
					}
//						cerr << i << " " << j << " " << (a[i].l + j) / k << endl;
					if ((a[i].l + j) % k == 0 && a[t].inside((a[i].l + j) / k)) {
						for (int q = 0; q < n; ++q) {
							if (dp[t][(a[i].l + j) / k - a[t].l][q][0] && cnt[i][j][q + 1] < cnt[t][(a[i].l + j) / k - a[t].l][q] + a[i].l + j) {
								cnt[i][j][q + 1] = cnt[t][(a[i].l + j) / k - a[t].l][q] + a[i].l + j;
								dp[i][j][q + 1][0] = 1;
								dp[i][j][q + 1][1] = t;
								dp[i][j][q + 1][2] = 1;
								if (q + 1 == n && (res.first == -1 || cnt[i][j][n] > cnt[res.first][res.second][n])) {
									res = make_pair(i, j);
								}
							}
						}
					}
				}
			}
		}
	}

//	cerr << dp[0][0][1][0];
//	cerr << dp[1][2][2][0];
//	cerr << dp[2][0][3][0];

//	cerr << dp[0][0][1][1];
//	cerr << dp[1][2][2][1];
//	cerr << dp[2][0][3][1];

	if (res.first == -1) {
		printf("NO\n");
		return 0;
	}

	printf("YES\n");
	int i = res.first;
	int j = res.second;
	for (int t = n; t > 0; --t) {
		ans.push_back(make_pair(a[i].ind, a[i].l + j));
		int nj;
		if (dp[i][j][t][2] == 0)
			nj = a[i].l + j - k - a[dp[i][j][t][1]].l;
		else
			nj = (a[i].l + j) / k - a[dp[i][j][t][1]].l;
		i = dp[i][j][t][1];
		j = nj;
	}
	for (int t = 0; t < n; ++t) {
		printf("%d %I64d\n", ans.back().first + 1, ans.back().second);
		ans.pop_back();
	}

	return 0;
}

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<queue>
#include<map>
using namespace std;
#define LL long long
struct node{
        LL a,b,c;
        node(){}
        node(LL x,LL y,LL z){
                a=x,b=y,c=z;
        }
        bool operator <(node p)const{
                if (a==p.a&&b==p.b) return c<p.c;
                if (a==p.a) return b<p.b;
                return a<p.a;
        }
};
map<node,LL> mp;
map<node,node> nxt;
node p[60];
int n,m,k;
LL cpl[60],a[60],b[60];
LL dp[60][202][60];
int fit(LL a,LL b){
        return a<=p[b].b&&a>=p[b].a;
}
LL fnd(LL x,LL y,LL z){
        if (x==n) return 0;
        if (dp[x][y][z]!=-1) return dp[x][y][z];
        int ok=0;
        LL ret=-2;
        for (int i=z+1;i<m;i++){
        //      printf("%lld %d\n",z,i);
                if (p[i].c>p[z].c){
                        LL tmp=-2,t1=y+k+p[z].a;
                        if (fit(t1,i)){
                                tmp=fnd(x+1,t1-p[i].a,i);
                                if (tmp!=-2) tmp+=t1;
                                if (tmp>ret){
                                        ret=tmp;
                                        nxt[node(x,y,z)]=node(x+1,t1-p[i].a,i);
                                }
                        }
                        tmp=-2;
                        t1=k*(y+p[z].a);
                        if (fit(t1,i)){
                                tmp=fnd(x+1,t1-p[i].a,i);
                                if (tmp!=-2) tmp+=t1;
                                if (tmp>ret){
                                        ret=tmp;
                                        nxt[node(x,y,z)]=node(x+1,t1-p[i].a,i);
                                }
                        }
                }
        }
        return dp[x][y][z]=ret;
}
bool cmp(node a,node b){
        return a.c<b.c;
}
int main(){
    memset(dp,-1,sizeof(dp));
        scanf("%d%d%d",&n,&m,&k);
        for (int i=0;i<m;i++){
                scanf("%lld%lld%lld",a+i,b+i,cpl+i);
        }
        for (int i=0;i<m;i++){
                p[i]=node(a[i],b[i],cpl[i]);
        }
        sort(p,p+m,cmp);
//      for (int i=0;i<m;i++){
        //      printf("%lld %lld %lld\n",p[i].a,p[i].b,p[i].c);
//      }
        LL f[60];
        for (int i=0;i<m;i++){
                for (int j=0;j<m;j++){
                        if (p[i].a==a[j]&&p[i].b==b[j]&&p[i].c==cpl[j]){
                                f[i]=j;
                                break;
                        }
                }
        }
        LL ans=-1;
        node cur;
        for (LL i=0;i+n<=m;i++){
        //      printf("%d\n",i);
                for (LL j=p[i].a;j<=p[i].b;j++){
                        LL tmp=fnd(1,j-p[i].a,i);
                        if (tmp!=-2) tmp+=j;
                        if (ans<tmp){
                                ans=tmp;
                                cur=node(1,j-p[i].a,i);
                        }
                }
        }
        if (ans==-1){
                puts("NO");
        }
        else{
                puts("YES");
                for (LL i=0;i<n;i++){
                        printf("%lld %lld\n",f[cur.c]+1,cur.b+p[cur.c].a);
                        cur=nxt[cur];
                }
        }
        return 0;
}

#include <map>
#include <string>
#include <vector>
#include <math.h>
#include <set>
#include <algorithm>
#include <list>
#include <iostream>
#include <map>
#include <utility>
#define ll long long
using namespace std;
int n,m,k;

map<pair<int,pair<int,ll> > ,pair<int,ll> > next;
map<pair<int,pair<int,ll> > ,ll > maxhw;
vector <pair<int,pair<ll,ll> > > subjects;

ll findit(int prevdiff,ll prev,int left)
{
    if(left==0)
        return 0;

    pair<int,pair<int,ll> > hashval;
    hashval=make_pair(prevdiff,make_pair(left,prev));

    if(next.find(hashval)!=next.end())
            return maxhw[hashval];

    next[hashval]=make_pair(-1,-1);
    ll retval=-1;
    for(int j=0;j<m;j++)
    {
        ll MINN=subjects[j].second.first;
        ll MAXX=subjects[j].second.second;
        for(int b=0;b<=1;b++)
        {
            ll v;
            if(b==0)
                v=prev*k;
            else
                v=prev+k;
            if(!(v>=MINN&&v<=MAXX))
                continue;

            if(subjects[j].first<=prevdiff)
                continue;
            ll curr=findit(subjects[j].first,v,left-1);

            if(curr!=-1)
            {
                if(retval<curr+v)
                {
                    next[hashval]=make_pair(j,v);
                    retval=curr+v;
                }
            }
        }
    }
    maxhw[hashval]=retval;
    return retval;

}

int main()
{
    cin>>n>>m>>k;
    for(int i=0;i<m;i++)
    {
        ll a,b;
        int c;
        cin>>a>>b>>c;
        subjects.push_back(make_pair(c,make_pair(a,b)));
    }

    ll opt=-1;
    int istart;
    ll jstart;
    for(int i=0;i<m;i++)
    {
        for(ll j=subjects[i].second.first;j<=subjects[i].second.second;j++)
        {
            ll numhws=findit(subjects[i].first,j,n-1);
            if(numhws!=-1 && numhws+j>opt)
            {
                istart=i;
                jstart=j;
                opt=numhws+j;
            }
        }

    }

    if(opt==-1)
    {
        printf("NO\n");
    }
    else
    {
        printf("YES\n");
        int pos=istart;
        ll value=jstart;
        cout << pos+1 << " " << value << endl;
        for(int k=1;k<=n-1;k++)
        {
            pair<int,pair<int,ll> > hashval=make_pair(subjects[pos].first,make_pair(n-k,value));
            pos=next[hashval].first;
            value=next[hashval].second;
            cout << pos+1 << " " << value << endl;
        }
    }

}

#include <iostream>
#include <algorithm>

using namespace std;

const int maxn = 105;

long long dp[maxn][maxn][maxn];
long long a[maxn], b[maxn], c[maxn], number[maxn];
long long best[maxn][maxn][maxn];
long long N, M, K;

int solve(int i, int j, int k) {
	long long many = 0;
	if (i != 1)
		if (best[i][j][k] > 0) {
			long long newt = a[j] + k;
			newt -= K;
			newt -= a[best[i][j][k]];
			solve(i - 1, best[i][j][k], newt);
			many = dp[i][j][k] - dp[i - 1][best[i][j][k]][newt];
		}
		else {
			long long newt = a[j] + k;
			newt /= K;
			newt -= a[-best[i][j][k]];
			solve(i - 1, -best[i][j][k], newt);
			many = dp[i][j][k] - dp[i - 1][-best[i][j][k]][newt];
		}
	else
		many = dp[i][j][k];
	cout << number[j] << " " << many << "\n";
	return 0;
}

int main() {

	cin >> N >> M >> K;
	for (int i = 1; i <= M; ++i)
		cin >> a[i] >> b[i] >> c[i], number[i] = i;

	for (int i = 1; i <= M; ++i)
		for (int j = i + 1; j <= M; ++j)
			if (c[i] > c[j])
				swap(a[i], a[j]), swap(b[i], b[j]), swap(c[i], c[j]), swap(number[i], number[j]);

	for (int i = 1; i <= M; ++i)
		for (int j = 0; j <= b[i] - a[i]; ++j)
			dp[1][i][j] = a[i] + j;

	for (int i = 2; i <= N; ++i)
		for (int j = i; j <= M; ++j)
			for (int l = 0; l <= b[j] - a[j]; ++l)
				for (int k = i - 1; k < j && c[k] < c[j]; ++k)
				 {
						long long poz = a[j] + l - K;
						if (poz >= a[k] && poz <= b[k] && dp[i - 1][k][poz - a[k]] != 0) {
							poz -= a[k];
							if (dp[i - 1][k][poz] + a[j] + l > dp[i][j][l])
								dp[i][j][l] = dp[i - 1][k][poz] + a[j] + l,
								best[i][j][l] = k;
						}
						poz = a[j] + l;
						if (poz % K)
							continue;
						poz /= K;
						if (poz >= a[k] && poz <= b[k] && dp[i - 1][k][poz - a[k]] != 0) {
							poz -= a[k];
							if (dp[i - 1][k][poz] + a[j] + l > dp[i][j][l])
								dp[i][j][l] = dp[i - 1][k][poz] + a[j] + l,
								best[i][j][l] = -k;
						}
				}

	int pos = 0;
	int row = 1;
	for (int i = 1; i <= M; ++i)
		for (int l = 0; l <= b[i] - a[i]; ++l)
			if (dp[N][i][l] > dp[N][row][pos])
				row = i, pos = l;

	if (dp[N][row][pos] == 0) {
		cout << "NO";
		return 0;
	}
	cout << "YES\n";
	solve(N, row, pos);
}

#include <iostream>
#include <cstdio>
#include <cstring>

#include <algorithm>
#define INF 1ll << 63
using namespace std;
long long f[52][52][105];
long long path[52][52][105][2];

struct node{
	long long a , b , c;
	int id;
	bool operator < (const node & p) const
	{
		if(c != p.c)
		{
			return c < p.c;
		}
		else if(a != p.a)
		{
			return a < p.c;
		}
		return b < p.b;
	}
};
node r[55];

void print(int n , int j , long long x)
{
	if(path[n][j][x][0] != j)
	{
		int u = path[n][j][x][0];
		long long v = path[n][j][x][1];
		print(n - 1 , u , v);
	}
	cout << r[j].id << " " << x + r[j].a << endl;
}

int main()
{
	//freopen("data.in" , "r" ,stdin);
	int n , m , k;
	cin >> n >> m >> k;
	for(int i = 0 ; i < m ;i++)
	{
		cin >> r[i].a >>r[i].b >> r[i].c;
		r[i].id = i + 1;
	}
	sort(r , r + m);

	int last_j = 0;
	long long last_x = 0;
	long long Max = -INF;
	for(int i = 0 ; i < n ;i++)
	{
		for(int j = 0 ; j < m ;j++)
		{
			long long u = r[j].b - r[j].a;
			for(long long x = 0 ; x <= u ; x++)
			{
				if(i == 0)
				{
					f[i][j][x] = x + r[j].a;
					path[i][j][x][0] = j;
					//path[i][j][x][1] = x;
				}
				else
				{
					long long ans = -INF;
					long long v = x + r[j].a;

					for(int p = i - 1 ; p < j ; p++)
					{
						if(r[j].c  == r[p].c) continue;
						if(v % k == 0 && v / k >= r[p].a && v / k <= r[p].b)
						{
							if(ans < f[i - 1][p][v / k - r[p].a] + v)
							{
								ans = f[i - 1][p][v / k - r[p].a] + v;
								path[i][j][x][0] = p;
								path[i][j][x][1] = v / k - r[p].a;

							}

						}
						if(v - k >= r[p].a && v - k <= r[p].b)
						{
							if(ans < f[i - 1][p][v - k - r[p].a] + v)
							{
								ans  = f[i - 1][p][v - k - r[p].a] + v;
								path[i][j][x][0] = p;
								path[i][j][x][1] = v - k - r[p].a;

							}

						}

					}
					if(ans < 0)
					{
						path[i][j][x][0] = j;
					}

					f[i][j][x] = ans;
				}
				if(i == n - 1)
				{
					if(Max < f[i][j][x])
					{
						Max = f[i][j][x];
						last_j = j;
						last_x = x;
					}
				}
			}
		}
	}

	//cout << Max << endl;
	if(Max > 0)
	{
		cout << "YES" << endl;
		print(n - 1 , last_j , last_x);
	}
	else
	{
		cout << "NO" << endl;
	}
//	while(1);
	return 0;
}

#include<cstdio>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<list>
#include<stack>
#include<queue>
#include<complex>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<iostream>
#include<fstream>
#include<utility>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int ui;
typedef short int si;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

const double Pi = acos(-1.0);
const ld Eps=1e-8;

struct sub
{
	ll a;
	ll b;
	int c;
};

sub make(ll a, ll b, ll c)
{
	sub res;
	res.a = a;
	res.b = b;
	res.c = c;
	return res;
}

const int MAXN = 50, MAXK = 100, MAXP = 101;
ll d[MAXN][MAXK][MAXP] = {0};
sub s[MAXN];
pll pr[MAXN][MAXK][MAXP];

bool cmp(sub& a, sub& b)
{
	return a.c < b.c;
}

int main()
{
	int n, m, k, ind;
	ll a, b, c, dif, to;
	scanf("%d%d%d", &n, &m, &k);
	for(int i = 0; i<m; i++)
	{
		scanf("%I64d%I64d%I64d", &a, &b, &c);
		s[i] = make(a, b, c);
	}
	for(int i = 0; i<m; i++)
	{
		dif = s[i].b-s[i].a;
		for(int p = 0; p<=dif; p++)
			d[0][i][p] = s[i].a+p;
	}

	for(int l = 0; l<n-1; l++)
	{
		for(int i = 0;i<m; i++)
		{
			dif = s[i].b-s[i].a;
			for(int j = 0; j<m; j++)
				if(s[i].c<s[j].c)
				{
						for(int p = 0; p<=dif; p++)
							if(d[l][i][p])
							{
								to = s[i].a+p+k;
								ind = to-s[j].a;
								if(s[j].a <= to && to <= s[j].b && d[l+1][j][ind] < d[l][i][p]+to)
								{
									d[l+1][j][ind] = d[l][i][p]+to;
									pr[l+1][j][ind] = make_pair(i, p);
								}
								to = (s[i].a+p)*k;
								ind = to-s[j].a;
								if(to/k == s[i].a + p && s[j].a <= to && to <= s[j].b && d[l+1][j][ind] < d[l][i][p]+to)
								{
									d[l+1][j][ind] = d[l][i][p]+to;
									pr[l+1][j][ind] = make_pair(i, p);
								}
							}
				}
		}
	}
	ll ans = 0;
	ll ai, ap, si;
	for(int i = 0; i<m; i++)
		for(int p = 0; p<MAXP; p++)
			if(ans<d[n-1][i][p])
			{
				ans = d[n-1][i][p];
				ai = i;
				ap = p;
			}
	if(ans)
	{
		puts("YES");
		stack<pll> f;
		pll it;
		f.push(make_pair(ai, ap));
		for(int i = n-1; i>0; i--)
		{
			it = pr[i][ai][ap];
			f.push(it);
			ai = it.first;
			ap = it.second;
		}
		for(int i = 0; i<n; i++, f.pop())
		{
			si =  f.top().first;
			printf("%I64d %I64d\n", si+1, s[si].a + f.top().second);
		}
	}
	else
		puts("NO");
	return 0;
}

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <cstdlib>
#include <numeric>
using namespace std;

#define X first
#define Y second
#define MP make_pair
#define PB push_back
#define EB emplace_back
typedef long long LL;
typedef double D;
typedef long double LD;
typedef vector<int> VI;
typedef pair<int,int> PII;
#define REP(i,n) for (int i=0;i<(n);++i)
#define FOR(i,a,b) for (VAR(i,a);i<=(b);++i)
#define FORD(i,a,b) for(VAR(i,a);i>=(b);--i)
#define FORE(e,v) for(VAR(e,(v).begin());e!=(v).end();++e)
#define VAR(a,b) __typeof(b) a=(b)
#define SIZE(a) ((int)(a).size())
#define ALL(x) (x).begin(),(x).end()
#define CLR(x,a) memset(x,a,sizeof(x))

const int M = 53;

LL a[M],b[M];
int c[M];

LL t[M][M][103];
int pop[M][M][103];
LL inf = -1;

int main() {
		int n,m,k;
    cin >> n >> m >> k;
    vector<PII> tt;
    REP(i,m) {
        cin>>a[i]>>b[i]>>c[i];
        tt.PB(MP(c[i],i));
    }
    sort(ALL(tt));

    REP(u,m) REP(ile,n+1) REP(cc,103) t[u][ile][cc] = inf;

    REP(u, m) {
        int i = tt[u].Y;

        REP(cc, b[i]-a[i]+1)
            t[u][1][cc] = a[i] + cc;

        FOR(ile, 1, n-1)
        REP(cc, b[i]-a[i]+1)
        if(t[u][ile][cc] > inf) {
            LL x = a[i] + cc;
            FOR(e, u+1, m-1) if(tt[e].X > c[i]) {
                int j = tt[e].Y;
                if(x + k >= a[j] && x + k <= b[j] && t[e][ile+1][x+k-a[j]] < t[u][ile][cc] + x+k) {
                        t[e][ile+1][x+k-a[j]] = t[u][ile][cc] + x + k;
                        pop[e][ile+1][x+k-a[j]] = u + u;
                }
                if(x * k >= a[j] && x * k <= b[j] && t[e][ile+1][x*k-a[j]] < t[u][ile][cc] + x*k) {
                    t[e][ile+1][x*k-a[j]] = t[u][ile][cc] + x*k;
                    pop[e][ile+1][x*k-a[j]] = u + u + 1;
                }
            }
        }
    }
    LL ret = inf;
    int U, CC;
    REP(u, m) {
        int i = tt[u].second;
        REP(cc, b[i]-a[i]+1) if(ret < t[u][n][cc]) {
            ret = t[u][n][cc];
            U = u;
            CC = cc;
        }
    }
    if(ret == inf) {
        cout << "NO\n";
        return 0;
    }
    cout << "YES\n";
    vector<pair<int, LL> > rett;
    while(n) {
        int i = tt[U].Y;
        LL x = a[i] + CC;
        rett.PB(MP(i+1, x));
        int p = pop[U][n][CC];
        n--;
        U = p/2;
        if(p%2) x /= k;
        else x -= k;
        CC = x - a[tt[U].Y];
    }
    reverse(ALL(rett));
    FORE(i,rett) cout << i->X << " " << i->Y << "\n";
    return 0;
}

#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif
#include <iostream>
#include <cmath>
#include <iomanip>
#include <string>
#include <cstring>
#include <cstdio>
#include <math.h>
#include <algorithm>
#include <stdio.h>
#include <climits>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <sstream>
#include <map>
#include <ctime>
#include <cstdlib>
#include <list>
#include <bitset>
#include <functional>
#include <numeric>
#include <utility>

using namespace std;

typedef unsigned long long ull;
typedef long long ll;

int dx[] = { 1, 0, -1, 0 };
int dy[] = { 0, 1, 0, -1 };

// start + (end - start) / 2;
/*
long long power(long long b, long long Pow){
if (b == 0) return 0;
else if (Pow == 1) return b;
else if (Pow % 2  == 0.0) return power(b*b, Pow / 2);
else return b*power(b*b, Pow / 2);
}*/
/*
int gcd(int a, int b){
if (b == 0)
return a;
return gcd(b, a % b);
}
*/

#define OO 1e32
int n, m, k;
vector < pair < pair < int, int > , pair < ll, ll> > > v;
ll dp[55][110][55][55];
bool f = 1;
ll solve(int idx, ll x, int cnt, int last){
    if (cnt == n)return 0;
    if (idx == m)return OO;
    int y = x - v[last].second.first;
    ll &ret = dp[idx][x - v[last].second.first][cnt][last];
    if (ret != -1)
        return ret;
    ret = solve(idx + 1, x, cnt, last);
    if (v[idx].first.first > v[last].first.first){
        if (x * k >= v[idx].second.first && x * k <= v[idx].second.second){
            ret = max(ret, solve(idx + 1, k * x, cnt + 1, idx) + x * k);
        }
        if (x + k >= v[idx].second.first && x + k <= v[idx].second.second){
            ret = max(ret, solve(idx + 1, k + x, cnt + 1, idx) + x + k);
        }
    }
    return ret;
}
void print(int idx, ll x, int cnt, int last){
    if (cnt == n){
        f = 0;
        return;
    }
    if (idx == m)return;
    ll ret = dp[idx][x - v[last].second.first][cnt][last];
    if (ret == solve(idx + 1, x, cnt, last) && f){
        print(idx + 1, x, cnt, last);
    }
    if (v[idx].first.first > v[last].first.first && f){
        if (x * k >= v[idx].second.first && x * k <= v[idx].second.second){
            if (ret == solve(idx + 1, k * x, cnt + 1, idx) + x * k){
                cout << v[idx].first.second << " " << x * k << "\n";
                print(idx + 1, k * x, cnt + 1, idx);
            }
        }
        if (x + k >= v[idx].second.first && x + k <= v[idx].second.second){
            if (ret == solve(idx + 1, k + x, cnt + 1, idx) + x + k){
                cout << v[idx].first.second << " " << x + k << "\n";
                print(idx + 1, k + x, cnt + 1, idx);
            }
        }
    }
    return;
}
int main() {
    cin.tie(0);
    std::ios::sync_with_stdio(false);
    cin >> n >> m >> k;
    v.resize(m);
    memset(dp, -1, sizeof dp);
    for (int i = 0; i < m; i++){
        cin >> v[i].second.first >> v[i].second.second >> v[i].first.first;
        v[i].first.second = i + 1;
    }
    sort(v.begin(), v.end());
    ll mx = -1;
    ll x = 0, I = 0;
    for (int i = 0; i < m; i++){
        for (ll j = v[i].second.first; j <= v[i].second.second; j++){
            if (mx < solve(i + 1, j, 1, i) + j){
                mx = max(mx, solve(i + 1, j, 1, i) + j);
                I = i;
                x = j;
            }
        }
    }
    if (mx != -1){
        cout << "YES\n";
        cout << v[I].first.second << " " << x << "\n";
        print(I + 1, x, 1, I);
    }
    else
        cout << "NO\n";
}

#include <stdio.h>
#include <stdlib.h>

#define MAX_N 51
#define MAX_M 51
#define MAX_LEN 101

typedef struct path_info_st {
	int m;
	long long ht;
} path_info_st, *path_info_t;

long long A[MAX_M], B[MAX_M];
int C[MAX_M], L[MAX_M];
long long dp[MAX_N][MAX_M][MAX_LEN];
long long help1[MAX_M][MAX_LEN];
long long help2[MAX_M][MAX_LEN];
path_info_st path_data[MAX_N][MAX_M][MAX_LEN];
path_info_t path[MAX_N];
bool can_pick[MAX_M];
int N, M, K;

int main() {
	long long i, j, k, r, p, q, t, t1, t2, t3, t4, t5, t6;
	scanf("%d %d %d", &N, &M, &K);
	for( i=0;i<M;i++ )
		scanf("%I64d %I64d %d", &(A[i]), &(B[i]), &(C[i]));
	for( i=0;i<M;i++ )
		L[i] = (int)(B[i] - A[i]);

	for( i=0;i<M;i++ ) {
		t = L[i];
		t1 = A[i];

		for( j=0;j<=t;j++ ) {
			t2 = t1 + j;
			help1[i][j] = t2 / K;
		}

		for( j=0;j<=t;j++ ) {
			t2 = t1 + j;
			help2[i][j] = t2 - K;
		}
	}

	for( i=0;i<M;i++ ) {
		can_pick[i] = true;
		t1 = A[i];
		t = L[i];
		for( j=0;j<=t;j++ )
			dp[0][i][j] = t1 + j;
	}

	for( i=1;i<N;i++ ) {
		for( j=0;j<M;j++ ) {
			t = L[j];
			t1 = A[j];
			for( k=0;k<M;k++ ) {
				if( C[k] < C[j] ) {
					t3 = A[k];
					t4 = B[k];
				for( r=0;r<=t;r++ ) {
					t2 = t1 + r;
					p = help1[j][r];
					q = help2[j][r];
						if( p >= t3 && p <= t4 && (t2 % K) == 0 ) {
							t5 = dp[i][j][r];
							t6 = dp[i-1][k][p-t3];
							if( t6 > 0 && (0 == t5 || t5 < t2 + t6) ) { // t6 > 0 means there is a path to i-1 day set subject k and issues total t6 home task
								dp[i][j][r] = t2 + t6;
								path_data[i][j][r].m = k;
								path_data[i][j][r].ht = p-t3;
							}
						}
						if( q >= t3 && q <= t4 ) {
							t5 = dp[i][j][r];
							t6 = dp[i-1][k][q-t3];
							if( t6 > 0 && ( 0 == t5 || t5 < t2 + t6 ) ) {  // t6 > 0 means there is a path to i-1 day set subject k and issues total t6 home task
								dp[i][j][r] = t2 + t6;
								path_data[i][j][r].m = k;
								path_data[i][j][r].ht = q-t3;
							}
						}
					}
				}
			}
		}
	}

	k = 0;
	for( i=0;i<M;i++ ) {
		t = L[i];
		for( j=0;j<=t;j++ ) {
			t2 = dp[N-1][i][j];
			if( k < t2 ) {
				k = t2; t3 = i; t4 = j;
			}
		}
	}

	if( 0 == k ) {
		printf("NO\n");
		return 0;
	}

	k = 0, i = 1;
	printf("YES\n");
	if( N > 1 ) {
		path_info_t pi = &(path_data[N-1][t3][t4]);
		for( i=1;i<N-1;i++ ) {
			path[k++] = pi;
			pi = &(path_data[N-i-1][pi->m][pi->ht]);
		}
		path[k++] = pi;

		for( i=k-1;i>=0;i-- )
			printf("%d %I64d\n", (int)(path[i]->m + 1), (path[i]->ht + A[path[i]->m]));
	}
	printf("%d %I64d\n", (int)(t3 + 1), (t4 + A[t3]));

	return 0;
}

#include<bits/stdc++.h>
using namespace std;
struct st {
    int id,c;
    unsigned long long a,b,num;
};
bool cmp(st a,st b) {
    return a.c<b.c;
}
st jilu[100];
unsigned long long dp[55][105][55];
int fi[55][105][55],fj[55][105][55];
stack<pair<unsigned long long,int> >ss;
int main() {
    int n,m;
    unsigned long long kk;
    scanf("%d%d%I64u",&n,&m,&kk);
    for(int i=0; i<m; i++) {
        int tmp;
        scanf("%I64u%I64u%d",&jilu[i].a,&jilu[i].b,&jilu[i].c);
        jilu[i].num=jilu[i].b-jilu[i].a;
        jilu[i].id=i+1;
    }
    sort(jilu,jilu+m,cmp);
    for(int k=1; k<=n; k++)  {
        {
            for(int i=0; i<m; i++)
                for(int j=0; j<=jilu[i].num; j++) {
                    if(i==0) {
                        if(k==1)
                            dp[i][j][k]=j+jilu[i].a;
                    } else if(k==1) {
                        dp[i][j][k]=j+jilu[i].a;
                    } else {
                        long long t=jilu[i].a+j-kk;
                        for(int w=0; w<i; w++) {
                            if(jilu[w].c>=jilu[i].c)break;
                            if(jilu[w].a>t||jilu[w].b<t)continue;
                            long long tt=t-jilu[w].a;
                            if(dp[w][tt][k-1]) {
                                if(dp[i][j][k]<jilu[i].a+j+dp[w][tt][k-1]) {
                                    dp[i][j][k]=jilu[i].a+j+dp[w][tt][k-1];
                                    fi[i][j][k]=w;
                                    fj[i][j][k]=tt;
                                }
                            }
                        }
                        t=jilu[i].a+j;
                        if(t%kk==0) {
                            t/=kk;
                            for(int w=0; w<i; w++) {
                                if(jilu[w].c>=jilu[i].c)break;
                                if(jilu[w].a>t||jilu[w].b<t)continue;
                                long long tt=t-jilu[w].a;
                                if(dp[w][tt][k-1]) {
                                    if(dp[i][j][k]<jilu[i].a+j+dp[w][tt][k-1]) {
                                        dp[i][j][k]=jilu[i].a+j+dp[w][tt][k-1];
                                        fi[i][j][k]=w;
                                        fj[i][j][k]=tt;
                                    }
                                }
                            }
                        }
                    }
                }
        }
    }
    long long ans=0;
    int idi,idj;
    for(int i=0; i<m; i++) {
        for(int j=0; j<=jilu[i].num; j++) {
            if(ans<dp[i][j][n]) {
                ans=dp[i][j][n];
                idi=i;
                idj=j;
            }
        }
    }
    if(ans==0) {
        puts("NO");
        return 0;
    }
    while(n--) {
        int n_idi=fi[idi][idj][n+1];
        int n_idj=fj[idi][idj][n+1];
        if(n!=0)
            ss.push(make_pair(dp[idi][idj][n+1]-dp[n_idi][n_idj][n],jilu[idi].id));
        else
            ss.push(make_pair(dp[idi][idj][n+1],jilu[idi].id));
        idi=n_idi;
        idj=n_idj;
    }
    puts("YES");
    while(!ss.empty()) {
        pair<unsigned long long,int>t=ss.top();
        ss.pop();
        printf("%d %I64u\n",t.second,t.first);
    }
}

#include <cstdio>
#include <iostream>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <functional>
using namespace std;

#pragma comment(linker,"/STACK:100000000")

const long long inf = 0x3f3f3f3f3f3f3f3f;

long long a[55], b[55];
int c[55];
int ind[55];
long long f[55][55][105];
int g[55][55][105];
bool t[55][55][105];
int ans1[55];
long long ans2[55];

bool cmp(int i, int j)
{
    return c[i] < c[j];
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    int n, m;
    long long k;
    int i, j, u, y;
    scanf("%d%d%I64d", &n, &m, &k);
    for (i = 1; i <= m; ++i)
    {
        scanf("%I64d%I64d%d", &a[i], &b[i], &c[i]);
        ind[i] = i;
    }
    sort(ind, ind + m + 1, cmp);
    for (i = 1; i <= m; ++i)
    {
        for (j = 1; j <= n; ++j)
        {
            for (u = 0; u <= b[ind[i]] - a[ind[i]]; ++u)
            {
                f[i][j][u] = -inf;
                if (j == 1)
                {
                    f[i][j][u] = a[ind[i]] + u;
                    continue;
                }
                for (y = i - 1; y > 0; --y)
                    if (c[ind[i]] > c[ind[y]])
                    {
                        if (b[ind[y]] >= a[ind[i]] + u - k && a[ind[i]] + u - k >= a[ind[y]] && f[i][j][u] < f[y][j - 1][a[ind[i]] + u - k - a[ind[y]]] + a[ind[i]] + u)
                        {
                            f[i][j][u] = f[y][j - 1][a[ind[i]] + u - k - a[ind[y]]] + a[ind[i]] + u;
                            g[i][j][u] = y;
                            t[i][j][u] = 0;
                        }
                        if ((a[ind[i]] + u)%k == 0 && b[ind[y]] >= (a[ind[i]] + u)/k && (a[ind[i]] + u)/k >= a[ind[y]] && f[i][j][u] < f[y][j - 1][(a[ind[i]] + u)/k - a[ind[y]]] + a[ind[i]] + u)
                        {
                            f[i][j][u] = f[y][j - 1][(a[ind[i]] + u)/k - a[ind[y]]] + a[ind[i]] + u;
                            g[i][j][u] = y;
                            t[i][j][u] = 1;
                        }
                    }
            }
        }
    }
    int mxi = -1;
    int mxu = -1;
    long long mx = -1;
    for (i = 1; i <= m; ++i)
        for (j = 0; j <= b[ind[i]] - a[ind[i]]; ++j)
            if (f[i][n][j] > mx)
            {
                mx = f[i][n][j];
                mxi = i;
                mxu = j;
            }
    if (mxi == -1)
    {
        printf("NO\n");
        return 0;
    }
    printf("YES\n");
    i = mxi;
    u = mxu;
    for (j = n; j > 0; --j)
    {
        ans1[j] = ind[i];
        ans2[j] = a[ind[i]] + u;
        int ni = g[i][j][u];
        long long nu = a[ind[i]] + u;
        if (t[i][j][u] == 1)
            nu = nu/k - a[ind[ni]];
        else
            nu = nu - k - a[ind[ni]];
        i = ni;
        u = nu;
    }
    for (i = 1; i <= n; ++i)
        printf("%d %I64d\n", ans1[i], ans2[i]);
    return 0;
}

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

typedef long long ll;

struct state {
    ll a, b;
    int c, id;
    bool operator<(const state&e) const {
        return c < e.c;
    }
} v[50];

int n, m, k;
ll dp[50][50][101];

ll calc(int lst, int w, int add) {
    if (w == n)
        return 0;
    ll &res = dp[lst][w][add];
    if (res != -1)
        return res;
    res = -1e18;
    ll p = v[lst].a + add;
    for (int i = lst + 1; i < m; ++i)
        if (v[i].c > v[lst].c) {
            if (p + k >= v[i].a && p + k <= v[i].b)
                res = max(res, calc(i, w + 1, p + k - v[i].a) + p + k);
            if (p * k >= v[i].a && p * k <= v[i].b)
                res = max(res, calc(i, w + 1, p * k - v[i].a) + p * k);
        }
    return res;
}

void path(int lst, int w, int add) {
    if (w == n)
        return;
    ll best = calc(lst, w, add), p = v[lst].a + add;
    for (int i = lst + 1; i < m; ++i)
        if (v[i].c > v[lst].c) {
            if (p + k >= v[i].a && p + k <= v[i].b && calc(i, w + 1, p + k - v[i].a) + p + k == best) {
                printf("%d %lld\n", v[i].id + 1, p + k);
                path(i, w + 1, p + k - v[i].a);
                break;
            }
            if (p * k >= v[i].a && p * k <= v[i].b && calc(i, w + 1, p * k - v[i].a) + p * k == best) {
                printf("%d %lld\n", v[i].id + 1, p * k);
                path(i, w + 1, p * k - v[i].a);
                break;
            }
        }
}

int main() {
//  freopen("a", "r", stdin);
    scanf("%d%d%d", &n, &m, &k);
    for (int i = 0; i < m; ++i) {
        scanf("%lld%lld%d", &v[i].a, &v[i].b, &v[i].c);
        v[i].id = i;
    }
    sort(v, v + m);
    memset(dp, -1, sizeof dp);
    int str = -1, add = 0;
    ll res = 0;
    for (int i = 0; i < m; ++i)
        for (ll j = v[i].a; j <= v[i].b; ++j) {
            ll temp = calc(i, 1, j - v[i].a) + j;
            if (temp > res) {
                res = temp;
                str = i;
                add = j - v[i].a;
            }
        }
    if (str == -1) {
        puts("NO");
        return 0;
    }
    puts("YES");
    printf("%d %lld\n", v[str].id + 1, v[str].a + add);
    path(str, 1, add);
    return 0;
}

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cassert>
#define LL long long
#define MAXN 51
using namespace std;

LL a[MAXN], b[MAXN], c[MAXN], id[MAXN], f[MAXN][MAXN][MAXN][2 * MAXN];
LL kq, k;
int n, m;

void sort(int i, int j) {
     int l = i, r = j;
     LL p = c[(l + r) >> 1];
     do {
         while (c[l] < p) l++;
         while (c[r] > p) r--;
         if (l <= r) {
            swap(c[l], c[r]);
            swap(a[l], a[r]);
            swap(b[l], b[r]);
            swap(id[l], id[r]);
            l++;
            r--;
         }
     }
     while (l <= r);
     if (l < j) sort(l, j);
     if (r > i) sort(i, r);
}

LL go(int at, int num, int lastPos, LL lastVal) {
    if (at == m - 1) {
       if (num == n - 1) return lastVal + a[lastPos];
       return 0;
    }
    LL &ans = f[at][num][lastPos][lastVal];
    if (ans != -1) return ans;
    ans = 0;
    LL can = go(at + 1, num, lastPos, lastVal);
    if (can != 0) ans = can;
    if (num < n - 1) {
       lastVal += a[lastPos];
       if (c[at + 1] > c[lastPos] && lastVal + k >= a[at + 1] && lastVal + k <= b[at + 1]) {
          can = go(at + 1, num + 1, at + 1, lastVal + k - a[at + 1]);
          if (can != 0) ans = max(ans, can + lastVal);
       }
       if (c[at + 1] > c[lastPos] && lastVal * k >= a[at + 1] && lastVal * k <= b[at + 1]) {
          can = go(at + 1, num + 1, at + 1, lastVal * k - a[at + 1]);
          if (can != 0) ans = max(ans, can + lastVal);
       }
    }
    return ans;
}

void truy(int at, int num, int lastPos, LL lastVal) {
     if (at == m - 1) {
        cout << id[lastPos] << ' ' << lastVal + a[lastPos] << endl;
        return;
     }
     LL ans = f[at][num][lastPos][lastVal];
     LL can = go(at + 1, num, lastPos, lastVal);
     if (can != 0 && ans == can)
        truy(at + 1, num, lastPos, lastVal);
     else {
          lastVal += a[lastPos];
          cout << id[lastPos] << ' ' << lastVal << endl;
          if (c[at + 1] > c[lastPos] && lastVal + k >= a[at + 1] && lastVal + k <= b[at + 1] && ans == go(at + 1, num + 1, at + 1, lastVal + k - a[at + 1]) + lastVal)
             truy(at + 1, num + 1, at + 1, lastVal + k - a[at + 1]);
          else
              truy(at + 1, num + 1, at + 1, lastVal * k - a[at + 1]);
     }
}

int main() {
    //freopen("c.inp", "r", stdin);
    //freopen("c.out", "w", stdout);

    cin >> n >> m >> k;
    for (int i = 0; i < m; i++) {
        cin >> a[i] >> b[i] >> c[i];
        id[i] = i + 1;
    }
    sort(0, m - 1);
    memset(f, -1, sizeof(f));
    kq = -1;
    for (int i = 0; i < m; i++)
        for (LL j = a[i]; j <= b[i]; j++)
            kq = max(kq, go(i, 0, i, j - a[i]));
    if (kq > 0) {
       cout << "YES" << endl;
       for (int i = 0; i < m; i++)
           for (LL j = a[i]; j <= b[i]; j++)
               if (kq == go(i, 0, i, j - a[i])) {
                  truy(i, 0, i, j - a[i]);
                  return 0;
               }
    }
    else cout << "NO" << endl;
}

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <cstdio>
#include <stdlib.h>
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <queue>
#include <set>
#include <algorithm>
#include <time.h>
using namespace std;

class node
{
    public:
        long long a, b, c;
        int ind;
        node()
        {
            a = b = c = 0LL;
            ind = 0;
        }
        node(long long aa, long long bb, long long cc)
        {
            a = aa, b = bb, c = cc;
        }
};

bool cmp (const node &a, const node &b)
{
    if (a.c != b.c)
        return a.c < b.c;
    if (a.a != b.a)
        return a.a < b.a;
    return a.b < b.b;
}

long long k;
int n, m;
vector <node> v;
node dp [55][55][110];
bool vis[55][55][110] = {false};
long long s[55];

node solve (int ind, int cur, int d)
{
    if (ind == n)
    {
        node q = node (0LL, 0LL, 0LL);
        return q;
    }

    if (cur > m)
    {
        node q = node (-(1LL<<60), 0LL, 0LL);
        return q;
    }

    if (vis [ind][cur][d])
        return dp[ind][cur][d];

    int cind = ind-1;
    int ccur = cur-1;

    node ret = node (-(1LL<<60), 0LL, 0LL);

    for (long long i = cur; i<m;i++)
    {
        if (!cur)
        {
            for (long long j = 0;j<=100LL;j++)
            {
                long long nn = j + v[i].a;
                if (nn > v[i].b)
                    break;
                node r = solve (ind+1, i+1, j);
                if (r.a + nn > ret.a)
                {
                    ret = r;
                    ret.a += nn;
                    ret.b |= (1LL << i);
                    s[i] = nn;
                }
            }
        }
        else
        {
            if (v[i].c == v[ccur].c)
                continue;
            long long sum = v[ccur].a + (long long)(d);
            long long nn = sum + k;
            if (nn >= v[i].a && nn <= v[i].b)
            {
                node r = solve (ind+1, i+1, nn - v[i].a);
                if (r.a + nn > ret.a)
                {
                    ret = r;
                    ret.a += nn;
                    ret.b |= (1LL << i);
                }
            }
            nn = sum*k;
            if (nn >= v[i].a && nn <= v[i].b)
            {
                node r = solve (ind+1, i+1, nn - v[i].a);
                if (r.a + nn > ret.a)
                {
                    ret = r;
                    ret.a += nn;
                    ret.b |= (1LL << i);
                    ret.c |= (1LL << i);
                }
            }

        }
    }

    vis [ind][cur][d] = true;
    return dp[ind][cur][d] = ret;
}

int main ()
{
    scanf ("%d %d %I64d", &n, &m, &k);

    for (int i=0;i<m;i++)
    {
        node q;
        scanf ("%I64d %I64d %I64d", &q.a, &q.b, &q.c);
        q.ind = i+1;
        v.push_back(q);
    }

    sort (v.begin(), v.end(), cmp);

    memset (vis, 0, sizeof(vis));

    node ans = solve (0, 0, 0);

    vector <node> pr;

    if (ans.a < 0LL)
        cout <<"NO" << endl;
    else
    {
        cout << "YES" << endl;
        long long b = ans.b;
        long long o = ans.c;
        long long cur = -1LL;
        for (long long i = 0LL;i<=(long long)(m);i++)
        {
            if ((1LL << i) & b)
            {
                if (cur == -1)
                    cur = s[i];
                else
                {
                    if ((1LL << i) & o)
                        cur *= k;
                    else
                        cur += k;
                }

                cout << v[i].ind << " " << cur << endl;
            }
        }
    }

    return 0;
}

#include<stdio.h>
#include<iostream>
#include<vector>
#include<queue>
#include<algorithm>
#include<sstream>
#include<set>
#include<map>
#include<stack>
#include<cmath>
#include <map>
#include<cstdlib>
#include<cstring>
#include<string>
#include<set>
#include<cassert>
using namespace std;

#define DEBUG //on-off switch for prlling statements

// Input macros
#define s(n)                        scanf("%d",&n)
#define sc(n)                       scanf("%c",&n)
#define sl(n)                       scanf("%lld",&n)
#define sf(n)                       scanf("%lf",&n)
#define ss(n)                       scanf("%s",n)

// Useful constants
#define INF                        (ll)1000000000
#define EPS                         1e-9

// Useful hardware instructions
#define bitcount                    __builtin_popcount
#define gcd                         __gcd

// Useful container manipulation / traversal macros
#define forall(i,a,b)               for(ll i=a;i<b;i++)
#define foreach(v, c)               for( typeof( (c).begin()) v = (c).begin();  v != (c).end(); ++v)
#define all(a)                      a.begin(), a.end()
#define in(a,b)                     ( (b).find(a) != (b).end())

#define fill(a,v)                    memset(a, v, sizeof a)
#define sz(a)                       ((ll)(a.size()))

// Some common useful functions
#define maX(a,b)                     ( (a) > (b) ? (a) : (b))
#define miN(a,b)                     ( (a) < (b) ? (a) : (b))

#define ll long long int
#define llu long long unsigned
#define ld long
 #define mod 1000000007
#define F first
#define S second
#define pb push_back
#define B 29
#define size1 340
#define llu long long unsigned
#define ld long

ll dp[51][51][201][3];
typedef struct type
{
    ll a;
    int sz;
    int c;
    int ind;
}type;
type sub[51];
int compare(const type& a, const type& b)
{
    return a.c < b.c;
}

int main()
{
    int n,m,mul;
    s(n);
    s(m);
    s(mul);

    for(int i=1;i<=m;i++)
    {
        sl(sub[i].a);
        ll x;
        sl(x);
        sub[i].sz= x-sub[i].a+1;
        s(sub[i].c);
        sub[i].ind=i;
    }
    sort(sub+1,sub+m+1,compare);
    for(int j=1;j<=m;j++)
    {
        for(int k=0;k<sub[j].sz;k++)
        {
            dp[1][j][k][0] = sub[j].a+k;
            dp[1][j][k][1] = -1;
            dp[1][j][k][2]= -1;
        }
    }
    for(int i=2;i<=n;i++)
    {
        for(int j=i;j<=m;j++)
        {
            for(int k=0;k<sub[j].sz;k++)
            {
                ll val=sub[j].a+k;
                ll oneway=-1;
                if(val%mul == 0)
                 oneway=val/mul;
                ll twoway=val-mul;
                dp[i][j][k][0]=0;
                dp[i][j][k][1]=-1;
                dp[i][j][k][2]= -1;
                ll maxi=0;
                int idx=-1;
                int off=-1;
                for(int l=1;l<j;l++)
                {
                    if(sub[l].c == sub[j].c)
                    break;
                    ll off1=oneway-sub[l].a;
                    ll off2=twoway-sub[l].a;
                    if(off1 >=0 && off1 < sub[l].sz && dp[i-1][l][off1][0])
                    {
                        if(maxi < dp[i-1][l][off1][0])
                        {
                            maxi=dp[i-1][l][off1][0];
                            idx=l;
                            off=off1;
                        }

                    }
                    if(off2 >=0 && off2 < sub[l].sz && dp[i-1][l][off2][0])
                    {
                        if(maxi < dp[i-1][l][off2][0])
                        {
                            maxi=dp[i-1][l][off2][0];
                            idx=l;
                            off=off2;
                        }
                    }
                }
                if(idx == -1)
                continue;
                dp[i][j][k][0] = maxi+sub[j].a+k;
                dp[i][j][k][1]= idx;
                dp[i][j][k][2]=off;
            }
        }
    }
    int idx=-1;
    ll maxi=0;
    int off=-1;
    int flag=0;

    for(int j=1;j<=m;j++)
    {
        for(int k=0;k<sub[j].sz;k++)
        {

            if(maxi < dp[n][j][k][0])
            {
                maxi=dp[n][j][k][0];
                idx=j;
                off=k;
                flag=1;
            }
        }
    }
    if(flag == 0)
    {
        printf("NO\n");
        return 0;
    }
    int val1;
    printf("YES\n");
    vector< pair<int,ll> > ans;
    for(int x=n;x>=1;x--)
    {
        ans.pb(make_pair(sub[idx].ind,sub[idx].a+off));
    //  printf("%d %d\n",idx,off);
        int val1=dp[x][idx][off][1];
        off=dp[x][idx][off][2];
        idx=val1;
    }
    for(int i=ans.size()-1;i>=0;i--)
    {
        printf("%d %lld\n",ans[i].F,ans[i].S);
    }
}

#include <stdio.h>
#include <stdlib.h>

#define MAX_N 51
#define MAX_M 51
#define MAX_LEN 101

typedef struct path_info_st {
	int m;
	long long ht;
} path_info_st, *path_info_t;

long long A[MAX_M], B[MAX_M];
int C[MAX_M], L[MAX_M];
long long dp[MAX_N][MAX_M][MAX_LEN];
//long long help1[MAX_M][MAX_LEN];
//long long help2[MAX_M][MAX_LEN];
path_info_st path_data[MAX_N][MAX_M][MAX_LEN];
path_info_t path[MAX_N];
bool can_pick[MAX_N][MAX_M];
int N, M, K;

int main() {
	long long i, j, k, r, p, q, t, t1, t2, t3, t4, t5, t6;
	scanf("%d %d %d", &N, &M, &K);
	for( i=0;i<M;i++ )
		scanf("%I64d %I64d %d", &(A[i]), &(B[i]), &(C[i]));
	for( i=0;i<M;i++ )
		L[i] = (int)(B[i] - A[i]);

	/*for( i=0;i<M;i++ ) {
		t = L[i];
		t1 = A[i];

		for( j=0;j<=t;j++ ) {
			t2 = t1 + j;
			help1[i][j] = t2 / K;
		}

		for( j=0;j<=t;j++ ) {
			t2 = t1 + j;
			help2[i][j] = t2 - K;
		}
	}*/

	for( i=0;i<M;i++ ) {
		t1 = A[i];
		t = L[i];
		for( j=0;j<=t;j++ )
			dp[0][i][j] = t1 + j;

		can_pick[0][i] = true;
	}

	for( i=1;i<N;i++ ) {
		for( j=0;j<M;j++ ) {
			t = L[j];
			t1 = A[j];
			for( k=0;k<M;k++ ) {
				if( C[k] < C[j] && can_pick[i-1][k] ) {
					t3 = A[k];
					t4 = B[k];
				for( r=0;r<=t;r++ ) {
					t2 = t1 + r;
					/*p = help1[j][r];
					q = help2[j][r];*/
					p = t2 / K;
					q = t2 - K;
						if( p >= t3 && p <= t4 && (t2 % K) == 0 ) {
							t5 = dp[i][j][r];
							t6 = dp[i-1][k][p-t3];
							if( t6 > 0 && (0 == t5 || t5 < t2 + t6) ) { // t6 > 0 means there is a path to i-1 day set subject k and issues total t6 home task
								dp[i][j][r] = t2 + t6;
								path_data[i][j][r].m = k;
								path_data[i][j][r].ht = p-t3;
								can_pick[i][j] = true;
							}
						}
						if( q >= t3 && q <= t4 ) {
							t5 = dp[i][j][r];
							t6 = dp[i-1][k][q-t3];
							if( t6 > 0 && ( 0 == t5 || t5 < t2 + t6 ) ) {  // t6 > 0 means there is a path to i-1 day set subject k and issues total t6 home task
								dp[i][j][r] = t2 + t6;
								path_data[i][j][r].m = k;
								path_data[i][j][r].ht = q-t3;
								can_pick[i][j] = true;
							}
						}
					}
				}
			}
		}
	}

	k = 0;
	for( i=0;i<M;i++ ) {
		t = L[i];
		for( j=0;j<=t;j++ ) {
			t2 = dp[N-1][i][j];
			if( k < t2 ) {
				k = t2; t3 = i; t4 = j;
			}
		}
	}

	if( 0 == k ) {
		printf("NO\n");
		return 0;
	}

	k = 0, i = 1;
	printf("YES\n");
	if( N > 1 ) {
		path_info_t pi = &(path_data[N-1][t3][t4]);
		for( i=1;i<N-1;i++ ) {
			path[k++] = pi;
			pi = &(path_data[N-i-1][pi->m][pi->ht]);
		}
		path[k++] = pi;

		for( i=k-1;i>=0;i-- )
			printf("%d %I64d\n", (int)(path[i]->m + 1), (path[i]->ht + A[path[i]->m]));
	}
	printf("%d %I64d\n", (int)(t3 + 1), (t4 + A[t3]));

	return 0;
}

#include <stdio.h>
#include<queue>
#include <algorithm>
#include <iostream>
#include <vector>
#include <string>
#include <set>
#define mp make_pair
#ifdef WIN32
	#define LLD "%I64d"
#else
	#define LLD "%lld"
#endif
using namespace std;
const double eps = 1e-6;
struct subj {
	long long sup, inf;
	int skill;
	int id;
	void read() {scanf("%lld%lld%d", &inf, &sup, &skill);}
	bool operator<(subj s) const {
		return skill < s.skill;
	}
};
subj a[60];
long long d[60][102][60];
pair<int, int> p[60][102][60];
void print(int y, int x, int n) {
	if (n > 1)
		print(p[y][x][n].first, p[y][x][n].second, n - 1);
	printf("%d %lld\n", a[y].id, a[y].inf + x);
}

int main() {
	//freopen("input.txt", "r", stdin);
	int n, m, k;
	scanf("%d%d%d", &n, &m, &k);
	for (int i=1; i<=m; i++) {a[i].read(); a[i].id = i;}
	sort(a + 1, a + m + 1);
	for (int j=1; j<=m; j++) {
		for (int i=0; i<=a[j].sup-a[j].inf; i++)
			d[j][i][1] = a[j].inf + i;
	}
	for (int i=1; i<=m; i++) {
		for (int j=0; j<=a[i].sup - a[i].inf; j++) {
			for (int z=1; z <= i; z++) {
				for (int x = 1; x < i; x++) {
						long long y = a[i].inf + j - k - a[x].inf;
						if (0 <= y && a[x].inf + y <= a[x].sup && a[i].skill > a[x].skill) {
							if (d[x][y][z-1] > 0 && d[x][y][z-1] + a[i].inf + j > d[i][j][z]) {
								d[i][j][z] = d[x][y][z-1] + a[i].inf + j;
								p[i][j][z] = mp(x, y);
							}
						}
						if ((a[i].inf + j) % k == 0) {
							long long y = (a[i].inf + j) / k - a[x].inf;
							if (0 <= y && a[x].inf + y <= a[x].sup && a[i].skill > a[x].skill) {
							if (d[x][y][z-1] > 0 && d[x][y][z-1] + a[i].inf + j > d[i][j][z]) {
								d[i][j][z] = d[x][y][z-1] + a[i].inf + j;
								p[i][j][z] = mp(x, y);
							}
						}
					}
				}
			}
		}
	}
	long long ans = 0;
	int y=-1, x=-1;
	for (int i=1; i<=m; i++) {
		for (int j=0; j<=100; j++) {
			if (d[i][j][n] > ans) {
				ans = d[i][j][n];
				y = i, x = j;
			}
		}
	}
	if (y == -1 || x == -1)
		puts("NO");
	else {
		puts("YES");
		print(y, x, n);
	}
	//printf("%lld", ans);
	return 0;
}

#define MOD 998244353
#define fast ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define foru(i, k, n) for (ll i = k; i < n; i++)
#define ford(i, k, n) for (ll i = k; i >= n; i--)
#define pb push_back
#define mp make_pair
#define has(set, x) set.find(x) != set.end()
#define nohas(set, x) set.find(x) == set.end()
#define inc(mpp, x) {if(has(mpp, x)) mpp[x]++; else mpp.insert(mp(x,1));}
#define decc(mpp, x) {if(has(mpp, x)) {if(mpp[x] > 1) mpp[x]--; else mpp.erase(x);}}
#define F first
#define S second
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vvi vector<vi>
#define um unordered_map
#define us unordered_set
#include <iostream>
#include <list>
#include <queue>
#include <vector>
#include <map>
#include <string>
#include <unordered_map>
#include <iomanip>
#include <set>
#include <unordered_set>
#include <fstream>
#include <algorithm>
#include <chrono>
#include <stack>
#include <deque>
#include <queue>
#include <bitset>
#include <numeric>
using namespace std;
const int INF = 1 << 29;
typedef long long ll;
typedef unsigned long long ull;
inline int two(int n) { return 1 << n; }
ll extgcd(ll a, ll b, ll& x, ll& y) { if (b == 0) { x = 1; y = 0; return a; } else { int g = extgcd(b, a%b, y, x); y -= a / b * x; return g; } }
ll modpow(ll a, ll b) { ll res = 1; a %= MOD; for (; b; b >>= 1) { if (b & 1)res = res * a%MOD; a = a * a%MOD; }return res; }

struct lesson {
	ll a, b, c,ind;
	lesson() {
		a = b = c = 0;
		ind = 0;
	}
};
struct {
	bool operator()(const lesson& x, const lesson& y) {
		if (x.c != y.c)return x.c <= y.c;
		else if (x.a != y.a)return x.a <= y.a;
		else if(x.b!=y.b)return x.b <= y.b;
		else return x.ind <= y.ind;
	}
}lessonCmp;
const int sz = 51, len = 101;
ll dp[sz][sz][len]; //amt, starts, pos
int to[sz][sz][len], way[sz][sz][len];
ll n, m, k;
lesson les[sz];
int main() {
	fast;
	ifstream in("input.txt");
	ofstream out("output.txt");
	cin >> n >> m >> k;
	foru(i, 0, m) {
		cin >> les[i].a >> les[i].b >> les[i].c;
		les[i].ind = i;
	}
	sort(les, les + m, lessonCmp);
	foru(amt, 1, n + 1) {
		ford(i, m - 1, 0) {
			foru(co, les[i].a, les[i].b + 1) {
				if (amt == 1) {
					dp[amt][i][co - les[i].a] = co;
					to[amt][i][co - les[i].a] = -1;
				}
				else {
					foru(j, i + 1, m) {
						if (les[j].c <= les[i].c)continue;
						if (co + k >= les[j].a&&co + k <= les[j].b) {
							if (dp[amt][i][co - les[i].a] < dp[amt - 1][j][co + k - les[j].a] + co&& dp[amt - 1][j][co + k - les[j].a]) {
								dp[amt][i][co - les[i].a] = dp[amt - 1][j][co + k - les[j].a] + co;
								to[amt][i][co - les[i].a] = j;
								way[amt][i][co - les[i].a] = 1;
							}
						}
						if (co * k >= les[j].a&&co * k <= les[j].b) {
							if (dp[amt][i][co - les[i].a] < dp[amt - 1][j][co * k - les[j].a] + co&& dp[amt - 1][j][co * k - les[j].a]) {
								dp[amt][i][co - les[i].a] = dp[amt - 1][j][co * k - les[j].a] + co;
								to[amt][i][co - les[i].a] = j;
								way[amt][i][co - les[i].a] = 2;
							}
						}
					}
				}
			}
		}
	}
	ll mxval = 0,bsti,bstco;
	foru(i, 0, m) {
		foru(co, les[i].a, les[i].b + 1) {
			if (mxval < dp[n][i][co - les[i].a]) {
				mxval = dp[n][i][co - les[i].a];
				bsti = i;
				bstco = co;
			}
		}
	}
	if (!mxval) {
		cout << "NO" << endl;
	}
	else {
		cout << "YES" << endl;
		ford(i, n, 1) {
			cout << les[bsti].ind + 1 << " " << bstco<<endl;
			ll tmpco = bstco;
			if (way[i][bsti][bstco - les[bsti].a] == 1)bstco += k;
			else bstco *= k;
			bsti = to[i][bsti][tmpco - les[bsti].a];
		}
	}
	return 0;
}

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

using namespace std;

#define p2(x) (x)*(x)
#define All(S) S.begin(),S.end()
#define isin(S, x) S.count(x)
#define pi acos(-1.0)
#define eps 1e-9
typedef long long ll;

ll dp[52][52][102];
ll parent[52][52][102];
char rout[52][52][102];
int k, n, m;
struct subject
{
    ll a, b, c;
    int inx;
    subject() {}
    subject(ll A, ll B, ll C): a(A), b(B), c(C) {}
    bool operator < (const subject &R) const
    {
        return c < R.c;
    }
};
vector<subject> subj;
void print(int i, int j, ll l)
{
    if(parent[i][j][l] != -1)
    {
        int ii = i-1;
        int jj = parent[i][j][l];
        ll df;
        if(rout[i][j][l] == 1)
            df = subj[j].a + l - k - subj[jj].a;
        else if(rout[i][j][l] == 2)
            df = ((subj[j].a + l) / k) - subj[jj].a;
        else
            df = -1;
        if(ii > 0 && jj >= ii && df >= 0)
            print(ii, jj, df);
    }
    cout << subj[j].inx << ' ' << l + subj[j].a << endl;
}

int main()
{
    //freopen("f.in", "r", stdin);
    cin >> n >> m >> k;
    subj = vector<subject>(m+1);
    for(int i = 1; i <= m; ++i)
    {
        cin >> subj[i].a >> subj[i].b >> subj[i].c;
        subj[i].inx = i;
    }
    subj[0] = subject(0,0,-100);
    memset(dp, 0, sizeof dp);
    memset(parent, -1, sizeof parent);
    memset(rout, -1, sizeof rout);
    sort(subj.begin(), subj.end());
    ll mx = -1, I = -1, J = -1, L = -1;
    int dif = subj[1].b - subj[1].a;
    for(int i = 0; i <= dif; ++i)
        dp[1][1][i] = i + subj[1].a;
    for(int i = 1; i <= n; ++i)
        for(int j = i; j <= m; ++j)
        {
            dif = subj[j].b - subj[j].a;
            for(int l = 0; l <= dif; ++l)
            {
                if(i == 1)
                {
                    dp[i][j][l] = l + subj[j].a;
                    continue;
                }
                for(int t = 1; t < j; ++t)
                    if(subj[t] < subj[j])
                    {
                        ll tot = (l + subj[j].a) - k;
                        if(tot >= subj[t].a && tot <= subj[t].b && dp[i-1][t][tot-subj[t].a] > 0 &&
                                dp[i][j][l] < dp[i-1][t][tot-subj[t].a] + l + subj[j].a)
                        {
                            dp[i][j][l] = dp[i-1][t][tot-subj[t].a] + l + subj[j].a;
                            parent[i][j][l] = t;
                            rout[i][j][l] = 1;
                        }
                        if((l+subj[j].a) % k != 0) continue;
                        tot = (l+subj[j].a) / k;
                        if(tot >= subj[t].a && tot <= subj[t].b && dp[i-1][t][tot-subj[t].a] > 0 &&
                                dp[i][j][l] < dp[i-1][t][tot-subj[t].a] + l + subj[j].a)
                        {
                            dp[i][j][l] = dp[i-1][t][tot-subj[t].a] + l + subj[j].a;
                            parent[i][j][l] = t;
                            rout[i][j][l] = 2;
                        }
                    }
            }
        }

    for(int j = n; j <= m; ++j)
    {
        dif = subj[j].b - subj[j].a;
        for(int l = 0; l <= dif; ++l)
            if(dp[n][j][l] > 0 && mx < dp[n][j][l])
            {
                mx = dp[n][j][l];
                J = j;
                L = l;
            }
    }
    if(mx == -1)
        cout << "NO" << endl;
    else
    {
        cout << "YES" << endl;
        //cout << J << "           " << L << endl;
        print(n, J, L);
    }
    return 0;
}

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <array>
#include <vector>

template<typename T> bool ckmax(T& a, const T& b){return a<b?a=b,1:0;}

using ll = long long;

const int MN = 55, MD = 105;

int N, M, K;
struct Subj
{
	public:
		ll a;int d, c, id;
		bool operator < (const Subj& o) const
		{
			return c<o.c;
		}
} a[MN];

ll dp[MN][MN][MD];
int pr[MN][MN][MD];

std::vector<std::array<ll, 2> > f;
void ans(int n, int d)
{
	printf("YES\n");
	for(int i=N;i;--i)
	{
		int v=pr[i][n][d];
		f.push_back({a[n].id, a[n].a+d});
		n=(v>>8&0xff)-1;
		d=v&0xff;
	}
	std::reverse(f.begin(), f.end());
	for(auto x:f) printf("%lld %lld\n", x[0], x[1]);
}

int main()
{
	scanf("%d%d%d", &N, &M, &K);
	for(int i=0;i<M;++i)
	{
		ll a,b; int c;
		scanf("%lld%lld%d", &a, &b, &c);
		::a[i]={a,(int)(b-a),c,i+1};
	}
	std::sort(a, a+M);
	memset(dp, -1, sizeof dp);
	for(int i=0,j;i<M;i=j)
	{
		for(j=i;j<M&&a[i].c==a[j].c;++j)
		{
			if(i)
				for(int x=0;x<i;++x)
					for(int k=0;k<=a[x].d;++k)
						for(int v=1;v<N;++v)
							if(~dp[v][x][k])
							{
								ll nk;

								nk = (a[x].a+k)*K-a[j].a;
								if(0<=nk&&nk<=a[j].d)
									if(ckmax(dp[v+1][j][nk], dp[v][x][k]+nk+a[j].a))
										pr[v+1][j][nk]=x+1<<8|k;

								nk = (a[x].a+k)+K-a[j].a;
								if(0<=nk&&nk<=a[j].d)
									if(ckmax(dp[v+1][j][nk], dp[v][x][k]+nk+a[j].a))
										pr[v+1][j][nk]=x+1<<8|k;
							}
			for(int k=0;k<=a[j].d;++k)
				dp[1][j][k]=a[j].a+k, pr[1][j][k]=0;
		}
		//printf("%d %d\n", i, j);
	}
	int n=-1, d=-1;
	ll best=-1;
	for(int i=0;i<M;++i)
		for(int j=0;j<=a[i].d;++j)
			if(ckmax(best, dp[N][i][j]))
				n=i, d=j;
	if(~best)
		ans(n,d);
	else
		printf("NO\n");
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
struct st{
    int id,c;
    unsigned long long a,b,num;
};
bool cmp(st a,st b){
    return a.c<b.c;
}
st jilu[100];
unsigned long long dp[55][105][55];
int fi[55][105][55],fj[55][105][55];
stack<pair<unsigned long long,int> >ss;
int main()
{
    int n,m;
    unsigned long long kk;
    scanf("%d%d%I64u",&n,&m,&kk);
    for(int i=0;i<m;i++){
        int tmp;
        scanf("%I64u%I64u%d",&jilu[i].a,&jilu[i].b,&jilu[i].c);
        jilu[i].num=jilu[i].b-jilu[i].a;
        jilu[i].id=i+1;
    }
    sort(jilu,jilu+m,cmp);
    for(int i=0;i<m;i++){
        for(int k=1;k<=n;k++){
            for(int j=0;j<=jilu[i].num;j++){
                if(i==0){
                    if(k==1)
                        dp[i][j][k]=j+jilu[i].a;
                }
                else if(k==1){
                    dp[i][j][k]=j+jilu[i].a;
                }
                else{
                    long long t=jilu[i].a+j-kk;
                    for(int w=0;w<i;w++){
                        if(jilu[w].c>=jilu[i].c)break;
                        if(jilu[w].a>t||jilu[w].b<t)continue;
                        long long tt=t-jilu[w].a;
                        if(dp[w][tt][k-1]){
                            if(dp[i][j][k]<jilu[i].a+j+dp[w][tt][k-1]){
                                dp[i][j][k]=jilu[i].a+j+dp[w][tt][k-1];
                                fi[i][j][k]=w;
                                fj[i][j][k]=tt;
                            }
                        }
                    }
                    t=jilu[i].a+j;
                    if(t%kk==0){
                        t/=kk;
                        for(int w=0;w<i;w++){
                            if(jilu[w].c>=jilu[i].c)break;
                            if(jilu[w].a>t||jilu[w].b<t)continue;
                            long long tt=t-jilu[w].a;
                            if(dp[w][tt][k-1]){
                                if(dp[i][j][k]<jilu[i].a+j+dp[w][tt][k-1]){
                                    dp[i][j][k]=jilu[i].a+j+dp[w][tt][k-1];
                                    fi[i][j][k]=w;
                                    fj[i][j][k]=tt;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    long long ans=0;
    int idi,idj;
    for(int i=0;i<m;i++){
        for(int j=0;j<=jilu[i].num;j++){
            if(ans<dp[i][j][n]){
                ans=dp[i][j][n];
                idi=i;
                idj=j;
            }
        }
    }
    if(ans==0){
        puts("NO");
        return 0;
    }
    while(n--){
        int n_idi=fi[idi][idj][n+1];
        int n_idj=fj[idi][idj][n+1];
        if(n!=0)
            ss.push(make_pair(dp[idi][idj][n+1]-dp[n_idi][n_idj][n],jilu[idi].id));
        else
            ss.push(make_pair(dp[idi][idj][n+1],jilu[idi].id));
        idi=n_idi;idj=n_idj;
    }
    puts("YES");
    while(!ss.empty()){
        pair<unsigned long long,int>t=ss.top();
        ss.pop();
        printf("%d %I64u\n",t.second,t.first);
    }
}

#include <iostream>
#include <stdio.h>
#include <sstream>
#include <memory.h>
#include <cassert>
#include <time.h>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <deque>

using namespace std;

#define pb push_back
#define mp make_pair
#define _(a,b) memset((a),b,sizeof(a))
#define all(a) (a).begin(),(a).end()
#define sz(a) (int)(a).size()

typedef long long lint;
typedef unsigned long long ull;
typedef pair < int , int > pii;

const int inf = 2000000000;
const lint linf = 2000000000000000000LL;
const double eps = 1e-9;

void prepare()
{
#ifdef _DEBUG
	freopen("input.txt","r",stdin);
#endif
}

struct vert
{
	lint a,b;
	int c;
	int id;
};

bool cmp(vert a,vert b)
{
	return a.c < b.c;
}

int n,m,k;
lint dp[55][55][105];
int type[55][55][105];
int pred[55][55][105];
vert p[55];

int ans[55];
lint ans2[55];

bool solve()
{
	cin >> n >> m >> k;
	for (int i = 0; i < m; i ++)
	{
		cin >> p[i].a >> p[i].b >> p[i].c;
		p[i].id = i;
	}
	sort(p,p+m,cmp);

	_(dp,0);
	for (int i = 0; i < m; i++)
	{
		for (int j = 0; j < (int)(p[i].b - p[i].a + 1); j++)
			dp[1][i][j] = (lint)j + p[i].a;
	}

	for (int i = 1; i < n; i ++)
	{
		for (int j = 0; j < m; j ++)
		{
			for (int w = 0; w < (int)(p[j].b - p[j].a + 1); w++)
			{
				if (dp[i][j][w])
				{
					for (int to = j + 1; to < m; to ++)
					{
						if (p[to].c > p[j].c)
						{
							lint x = p[j].a + w;
							x *= k;
							if (p[to].a <= x && x <= p[to].b &&
								dp[i][j][w] + x > dp[i+1][to][x-p[to].a])
							{
								dp[i+1][to][x-p[to].a] = dp[i][j][w] + x;
								pred[i+1][to][x-p[to].a] = j;
								type[i+1][to][x-p[to].a] = 1;
							}
							x /= k;
							x += k;
							if (p[to].a <= x && x <= p[to].b &&
								dp[i][j][w] + x > dp[i+1][to][x-p[to].a])
							{
								dp[i+1][to][x-p[to].a] = dp[i][j][w] + x;
								pred[i+1][to][x-p[to].a] = j;
								type[i+1][to][x-p[to].a] = 2;
							}
						}
					}
				}
			}
		}
	}

	lint mx = -1;
	int v,r;
	for (int i = 0; i < m; i ++)
	{
		for (int j = 0; j < (int)(p[i].b - p[i].a + 1); j++)
		{
			if (dp[n][i][j] > mx)
			{
				mx = dp[n][i][j];
				v = i;
				r = j;
			}
		}
	}
	if (mx <= 0)
		cout << "NO\n";
	else
	{
		cout << "YES\n";
		for (int i = n; i >= 1; i--)
		{
			ans[i] = p[v].id;
			ans2[i] = r + p[v].a;

			int nv,nr;
			nv = pred[i][v][r];
			if (type[i][v][r] == 1)
				nr = ans2[i] / k;
			else
				nr = ans2[i] - k;
			v = nv;
			nr = nr - p[v].a;
			r = nr;
		}

		for (int i = 1; i <= n; i++)
			cout << ans[i] + 1 << " " << ans2[i] << endl;
	}
	return false;
}

int main()
{
	prepare();
	while (solve());
	return 0;
}

#pragma comment(linker, "/stack:64000000")

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cstring>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <algorithm>
#include <sstream>
#include <cassert>

using namespace std;

#define REP(i, n) for (int (i) = 0; (i) < (n); (i)++)
#define FOR(i, a, b) for (int (i) = (a); (i) < (b); (i)++)
#define sz(v) (int)(v).size()
#define all(v) (v).begin(), (v).end()
#define mp make_pair
#define pb push_back

typedef long long LL;

struct Lesson {
	long long L, R;
	int D, N;

	Lesson() {}

	Lesson(long long L, long long R, int D, int N): L(L), R(R), D(D), N(N) {}

	bool operator<(const Lesson& lesson) const {
		return D < lesson.D;
	}
};

struct Prev {
	int num, exc;

	Prev() {}

	Prev(int num, int exc): num(num), exc(exc) {}
};

long long dp[55][55][111];
Prev pr[55][55][111];

int main() {
#ifndef ONLINE_JUDGE
	#define NAME "test"
	freopen(NAME".in", "r", stdin);
	freopen(NAME".out", "w", stdout);
	long long start = clock();
#endif
	int need, m, k;
	scanf("%d %d %d", &need, &m, &k);
	vector <Lesson> v(m);
	REP(i, m) {
		long long L, R;
		int D;
		cin >> L >> R >> D;
		v[i] = Lesson(L, R, D, i + 1);
	}
	sort(all(v));
	memset(dp, 0, sizeof(dp));
	memset(pr, -1, sizeof(pr));
	REP(i, m) REP(j, (int)(v[i].R - v[i].L) + 1) dp[1][i][j] = v[i].L + j;
	for (int done = 1; done < need; done++) {
		for (int cur = 0; cur < m; cur++) {
			for (int exc = 0; exc <= (int)(v[cur].R - v[cur].L); exc++) {
				long long did = v[cur].L + exc;
				if (dp[done][cur][exc] == 0) continue;
				for (int nxt = cur + 1; nxt < m; nxt++) {
					if (v[nxt].D > v[cur].D) {
						long long val[2];
						val[0] = did + k;
						if (did > (long long)(1e16 + 10)) {
							val[1] = -1;
						} else {
							val[1] = did * k;
						}
						REP(j, 2) {
							if (v[nxt].L <= val[j] && val[j] <= v[nxt].R) {
								int t = (int)(val[j] - v[nxt].L);
								if (dp[done + 1][nxt][t] < dp[done][cur][exc] + val[j]) {
									dp[done + 1][nxt][t] = dp[done][cur][exc] + val[j];
									pr[done + 1][nxt][t] = Prev(cur, exc);
								}
							}
						}
					}
				}
			}
		}
	}
	int B = -1, C = -1;
	REP(i, 111) REP(j, 111) {
		if (dp[need][i][j] > 0 && (B == -1 || dp[need][i][j] > dp[need][B][C])) {
			B = i, C = j;
		}
	}
	if (B != -1) {
		vector <pair <int, long long> > res(need);
		for (int A = need; A >= 1; A--) {
			res[A - 1] = mp(v[B].N, v[B].L + C);
			int b = B, c = C;
			B = pr[A][b][c].num;
			C = pr[A][b][c].exc;
		}
		cout << "YES\n";
		REP(i, sz(res)) {
			cout << res[i].first << " " << res[i].second << "\n";
		}
	} else {
		cout << "NO\n";
	}
    return 0;
}

#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif
#include <iostream>
#include <cmath>
#include <iomanip>
#include <string>
#include <cstring>
#include <cstdio>
#include <math.h>
#include <algorithm>
#include <stdio.h>
#include <climits>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <sstream>
#include <map>
#include <ctime>
#include <cstdlib>
#include <list>
#include <bitset>
#include <functional>
#include <numeric>
#include <utility>

using namespace std;

typedef unsigned long long ull;
typedef long long ll;

int dx[] = { 1, 0, -1, 0 };
int dy[] = { 0, 1, 0, -1 };

// start + (end - start) / 2;
/*
long long power(long long b, long long Pow){
if (b == 0) return 0;
else if (Pow == 1) return b;
else if (Pow % 2  == 0.0) return power(b*b, Pow / 2);
else return b*power(b*b, Pow / 2);
}*/
/*
int gcd(int a, int b){
if (b == 0)
return a;
return gcd(b, a % b);
}
*/
#define OO 1e32
int n, m, k;
vector < pair < pair < int, int > , pair < ll, ll> > > v;
ll dp[55][110][55][55];
bool f = 1;
ll solve(int idx, ll x, int cnt, int last){
    if (cnt == n)return 0;
    if (idx == m)return OO;
    int y = x - v[last].second.first;
    ll &ret = dp[idx][x - v[last].second.first][cnt][last];
    if (ret != -1)
        return ret;
    ret = solve(idx + 1, x, cnt, last);
    if (v[idx].first.first > v[last].first.first){
        if (x * k >= v[idx].second.first && x * k <= v[idx].second.second){
            ret = max(ret, solve(idx + 1, k * x, cnt + 1, idx) + x * k);
        }
        if (x + k >= v[idx].second.first && x + k <= v[idx].second.second){
            ret = max(ret, solve(idx + 1, k + x, cnt + 1, idx) + x + k);
        }
    }
    return ret;
}
void print(int idx, ll x, int cnt, int last){
    if (cnt == n)return;
    if (idx == m)return;
    ll ret = dp[idx][x - v[last].second.first][cnt][last];
    if (ret == solve(idx + 1, x, cnt, last)){
        print(idx + 1, x, cnt, last);
    }
    else if (v[idx].first.first > v[last].first.first){
        if (x * k >= v[idx].second.first && x * k <= v[idx].second.second){
            if (ret == solve(idx + 1, k * x, cnt + 1, idx) + x * k){
                cout << v[idx].first.second << " " << x * k << "\n";
                print(idx + 1, k * x, cnt + 1, idx);
            }
        }
        if (x + k >= v[idx].second.first && x + k <= v[idx].second.second){
            if (ret == solve(idx + 1, k + x, cnt + 1, idx) + x + k){
                cout << v[idx].first.second << " " << x + k << "\n";
                print(idx + 1, k + x, cnt + 1, idx);
            }
        }
    }
    return;
}
int main() {
    cin.tie(0);
    std::ios::sync_with_stdio(false);
    cin >> n >> m >> k;
    v.resize(m);
    memset(dp, -1, sizeof dp);
    for (int i = 0; i < m; i++){
        cin >> v[i].second.first >> v[i].second.second >> v[i].first.first;
        v[i].first.second = i + 1;
    }
    sort(v.begin(), v.end());
    ll mx = -1;
    ll x = 0, I = 0;
    for (int i = 0; i < m; i++){
        for (ll j = v[i].second.first; j <= v[i].second.second; j++){
            if (mx < solve(i + 1, j, 1, i) + j){
                mx = max(mx, solve(i + 1, j, 1, i) + j);
                I = i;
                x = j;
            }
        }
    }
    if (mx != -1){
        cout << "YES\n";
        cout << v[I].first.second << " " << x << "\n";
        print(I + 1, x, 1, I);
    }
    else
        cout << "NO\n";
}

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef __int64 big;
struct W
{
    big s,e;
    int w,id;
}a[500];
int pre1[51][120][155];
int pre2[51][120][155];
big f[61][120][155];
int K,n,m;
int ww[120][120],tt[120];
bool cmp(const W &a,const W &b){return a.w<b.w;}
void dfs(int dep,int i,int j)
{
    if(dep==1)return ;
    dfs(dep-1,pre1[dep][i][j],pre2[dep][i][j]);
    printf("%d %I64d\n",a[pre2[dep][i][j]].id,pre1[dep][i][j]+a[pre2[dep][i][j]].s);
}
int main()
{
    int i,j,k,p;
    big val;
    scanf("%d%d%d",&n,&m,&K);
    for(i=1;i<=m;i++)
    scanf("%I64d%I64d%d",&a[i].s,&a[i].e,&a[i].w),a[i].id=i;
    sort(a+1,a+1+m,cmp);
    for(i=1;i<=m;i++)tt[a[i].id]=i;
    for(j=1;j<=m;j++)
    for(k=0;k<=a[j].e-a[j].s;k++)
    {
        f[1][k][j]=max(f[1][k][j],k+a[j].s);
        ww[k][j]=a[j].id;
    }
    for(i=1;i<n;i++)
    for(j=1;j<=m;j++)
    for(k=0;k<=a[j].e-a[j].s;k++)
    if(f[i][k][j])
    {
        for(p=j+1;p<=m;p++)
        if(a[p].w>a[j].w)
        {
            val=k+a[j].s;
            if(val*K<=a[p].e&&val*K>=a[p].s)
            {
                if(f[i+1][val*K-a[p].s][p]<f[i][k][j]+val*K)
                {
                    f[i+1][val*K-a[p].s][p]=f[i][k][j]+val*K;
                    pre1[i+1][val*K-a[p].s][p]=k;
                    pre2[i+1][val*K-a[p].s][p]=j;
                }
            }
            if(val+K <=a[p].e&&val+K>=a[p].s)
            {
                if(f[i+1][val+K-a[p].s][p]<f[i][k][j]+val+K)
                {
                    f[i+1][val+K-a[p].s][p]=f[i][k][j]+val+K;
                    pre1[i+1][val+K-a[p].s][p]=k;
                    pre2[i+1][val+K-a[p].s][p]=j;
                }
            }
        }
    }
    big ans=-1;
    int ii,jj;
    for(i=100;i>=0;i--)
    for(j=0;j<=100;j++)
    if(ans<f[n][i][j]&&f[n][i][j])
    {
        ii=i;
        jj=j;
        ans=f[n][i][j];
    }
    if(ans==-1)puts("NO");
    else
    {
        puts("YES");
        dfs(n,ii,jj);
        printf("%d %I64d\n",a[jj].id,ii+a[jj].s);
    }
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
const int maxn = 60;
struct tipo{
	__int64 a, b, c;
	int id;
};
tipo sub[maxn];
__int64 k;
int n, m;
__int64 dp[maxn][maxn][105][maxn];
bool vis[maxn][maxn][105][maxn];
__int64 f(int pos, int prev, int sume_prev, int cont){
	if(cont == 0) return 0;
	if(pos > n){
		return LLONG_MIN;
	}
	if(vis[pos][prev][sume_prev][cont]) return dp[pos][prev][sume_prev][cont];
	__int64 tomo = LLONG_MIN;
	if(prev == 0){
		for(int i = 0; i <= sub[pos].b - sub[pos].a; ++i){
			__int64 vn = f(pos + 1, pos, i, cont - 1);
			if(vn == LLONG_MIN) continue;
			tomo = max(sub[pos].a + i + vn, tomo);
		}
	}
	else{
		__int64 sumprev = sub[prev].a + sume_prev;
		__int64 nexts = sumprev + k, vn; int dif;
		if(nexts >= sub[pos].a and nexts <= sub[pos].b and sub[prev].c < sub[pos].c){
			dif = nexts - sub[pos].a;
			vn = f(pos + 1, pos, dif, cont - 1);
			if(vn != LLONG_MIN)
				tomo = max(sub[pos].a + dif + vn, tomo);
		}
		nexts = sumprev*k;
		if(nexts >= sub[pos].a and nexts <= sub[pos].b and sub[prev].c < sub[pos].c){
			dif = nexts - sub[pos].a;
			vn = f(pos + 1, pos, dif, cont - 1);
			if(vn != LLONG_MIN)
				tomo = max(sub[pos].a + dif + vn, tomo);
		}
	}
	vis[pos][prev][sume_prev][cont] = 1;
	__int64 notomo = f(pos + 1, prev, sume_prev, cont);
	return dp[pos][prev][sume_prev][cont] = max(notomo, tomo);
}
void rec(int pos, int prev, int sume_prev, int cont){
	if(cont == 0) return;
	if(pos > n) return;
	__int64 tomo = LLONG_MIN;
	vector<int> next(4);
	if(prev == 0){
		for(int i = 0; i <= sub[pos].b - sub[pos].a; ++i){
			__int64 vn = f(pos + 1, pos, i, cont - 1);
			if(vn == LLONG_MIN) continue;
			if(sub[pos].a + i + vn > tomo){
				tomo = max(sub[pos].a + i + vn, tomo);
				next[0] = pos + 1;
				next[1] = pos;
				next[2] = i;
				next[3] = cont - 1;
			}
		}
	}
	else{
		__int64 sumprev = sub[prev].a + sume_prev;
		__int64 nexts = sumprev + k, vn; int dif;
		if(nexts >= sub[pos].a and nexts <= sub[pos].b and sub[prev].c < sub[pos].c){
			dif = nexts - sub[pos].a;
			vn = f(pos + 1, pos, dif, cont - 1);
			if(vn != LLONG_MIN){
				if(sub[pos].a + dif + vn > tomo){
					tomo = max(sub[pos].a + dif + vn, tomo);
					next[0] = pos + 1;
					next[1] = pos;
					next[2] = dif;
					next[3] = cont - 1;
				}
			}
		}
		nexts = sumprev*k;
		if(nexts >= sub[pos].a and nexts <= sub[pos].b and sub[prev].c < sub[pos].c){
			dif = nexts - sub[pos].a;
			vn = f(pos + 1, pos, dif, cont - 1);
			if(vn != LLONG_MIN){
				if(sub[pos].a + dif + vn > tomo){
					tomo = max(sub[pos].a + dif + vn, tomo);
					next[0] = pos + 1;
					next[1] = pos;
					next[2] = dif;
					next[3] = cont - 1;
				}
			}
		}
	}
	__int64 notomo = f(pos + 1, prev, sume_prev, cont);
	if(notomo > tomo)
		rec(pos + 1, prev, sume_prev, cont);
	else{
		cout << sub[pos].id << " " << sub[pos].a + next[2] << endl;
		rec(next[0],next[1],next[2],next[3]);
	}
}
bool cmp(tipo a, tipo b){
	return a.c < b.c;
}
int main(){
	cin >> m >> n >> k;
	sub[0].a = sub[0].b = sub[0].c = 0, sub[0].id = 0;
	for(int i = 1; i <= n; ++i){
		cin >> sub[i].a >> sub[i].b >> sub[i].c;
		sub[i].id = i;
	}
	sort(sub + 1, sub + n + 1, cmp);
	memset(vis,0,sizeof(vis));
	__int64 can = f(1,0,0,m);
	if(can != LLONG_MIN){
		puts("YES");
		rec(1,0,0,m);
	}
	else puts("NO");
	return 0;
}

#include <stdio.h>
#include <stdlib.h>

#define MAX_N 51
#define MAX_M 51
#define MAX_LEN 101

typedef struct path_info_st {
	int m;
	long long ht;
} path_info_st, *path_info_t;

long long A[MAX_M], B[MAX_M];
int C[MAX_M], L[MAX_M];
long long dp[MAX_N][MAX_M][MAX_LEN];
long long help1[MAX_M][MAX_LEN];
long long help2[MAX_M][MAX_LEN];
path_info_st path_data[MAX_N][MAX_M][MAX_LEN];
path_info_t path[MAX_N];
bool can_pick[MAX_N][MAX_M];
int N, M, K;

int main() {
	long long i, j, k, r, p, q, t, t1, t2, t3, t4, t5, t6;
	scanf("%d %d %d", &N, &M, &K);
	for( i=0;i<M;i++ )
		scanf("%I64d %I64d %d", &(A[i]), &(B[i]), &(C[i]));
	for( i=0;i<M;i++ )
		L[i] = (int)(B[i] - A[i]);

	for( i=0;i<M;i++ ) {
		t = L[i];
		t1 = A[i];

		for( j=0;j<=t;j++ ) {
			t2 = t1 + j;
			help1[i][j] = t2 / K;
		}

		for( j=0;j<=t;j++ ) {
			t2 = t1 + j;
			help2[i][j] = t2 - K;
		}
	}

	for( i=0;i<M;i++ ) {
		t1 = A[i];
		t = L[i];
		for( j=0;j<=t;j++ )
			dp[0][i][j] = t1 + j;

		can_pick[0][i] = true;
	}

	for( i=1;i<N;i++ ) {
		for( j=0;j<M;j++ ) {
			//can_pick[j] = false;
			t = L[j];
			t1 = A[j];
			for( k=0;k<M;k++ ) {
				if( C[k] < C[j] && can_pick[i-1][k] ) {
					t3 = A[k];
					t4 = B[k];
				for( r=0;r<=t;r++ ) {
					t2 = t1 + r;
					p = help1[j][r];
					q = help2[j][r];
						if( p >= t3 && p <= t4 && (t2 % K) == 0 ) {
							t5 = dp[i][j][r];
							t6 = dp[i-1][k][p-t3];
							if( t6 > 0 && (0 == t5 || t5 < t2 + t6) ) { // t6 > 0 means there is a path to i-1 day set subject k and issues total t6 home task
								dp[i][j][r] = t2 + t6;
								path_data[i][j][r].m = k;
								path_data[i][j][r].ht = p-t3;
								can_pick[i][j] = true;
							}
						}
						if( q >= t3 && q <= t4 ) {
							t5 = dp[i][j][r];
							t6 = dp[i-1][k][q-t3];
							if( t6 > 0 && ( 0 == t5 || t5 < t2 + t6 ) ) {  // t6 > 0 means there is a path to i-1 day set subject k and issues total t6 home task
								dp[i][j][r] = t2 + t6;
								path_data[i][j][r].m = k;
								path_data[i][j][r].ht = q-t3;
								can_pick[i][j] = true;
							}
						}
					}
				}
			}
			/*if( can_pick[j] == false ) {
				printf("\n");
			}*/
		}
	}

	k = 0;
	for( i=0;i<M;i++ ) {
		t = L[i];
		for( j=0;j<=t;j++ ) {
			t2 = dp[N-1][i][j];
			if( k < t2 ) {
				k = t2; t3 = i; t4 = j;
			}
		}
	}

	if( 0 == k ) {
		printf("NO\n");
		return 0;
	}

	k = 0, i = 1;
	printf("YES\n");
	if( N > 1 ) {
		path_info_t pi = &(path_data[N-1][t3][t4]);
		for( i=1;i<N-1;i++ ) {
			path[k++] = pi;
			pi = &(path_data[N-i-1][pi->m][pi->ht]);
		}
		path[k++] = pi;

		for( i=k-1;i>=0;i-- )
			printf("%d %I64d\n", (int)(path[i]->m + 1), (path[i]->ht + A[path[i]->m]));
	}
	printf("%d %I64d\n", (int)(t3 + 1), (t4 + A[t3]));

	return 0;
}

#include<stdio.h>
#include<iostream>
#include<vector>
#include<queue>
#include<algorithm>
#include<sstream>
#include<set>
#include<map>
#include<stack>
#include<cmath>
#include <map>
#include<cstdlib>
#include<cstring>
#include<string>
#include<set>
#include<cassert>
using namespace std;

#define DEBUG //on-off switch for prlling statements

// Input macros
#define s(n)                        scanf("%d",&n)
#define sc(n)                       scanf("%c",&n)
#define sl(n)                       scanf("%lld",&n)
#define sf(n)                       scanf("%lf",&n)
#define ss(n)                       scanf("%s",n)

// Useful constants
#define INF                        (ll)1000000000
#define EPS                         1e-9

// Useful hardware instructions
#define bitcount                    __builtin_popcount
#define gcd                         __gcd

// Useful container manipulation / traversal macros
#define forall(i,a,b)               for(ll i=a;i<b;i++)
#define foreach(v, c)               for( typeof( (c).begin()) v = (c).begin();  v != (c).end(); ++v)
#define all(a)                      a.begin(), a.end()
#define in(a,b)                     ( (b).find(a) != (b).end())

#define fill(a,v)                    memset(a, v, sizeof a)
#define sz(a)                       ((ll)(a.size()))

// Some common useful functions
#define maX(a,b)                     ( (a) > (b) ? (a) : (b))
#define miN(a,b)                     ( (a) < (b) ? (a) : (b))

#define ll long long int
#define llu long long unsigned
#define ld long
 #define mod 1000000007
#define F first
#define S second
#define pb push_back
#define B 29
#define size1 340
#define llu long long unsigned
#define ld long

ll dp[51][51][201][3];
typedef struct type
{
	ll a;
	int sz;
	int c;
	int ind;
}type;
type sub[51];
int compare(const type& a, const type& b)
{
	return a.c < b.c;
}

int main()
{
	int n,m,mul;
	s(n);
	s(m);
	s(mul);

	for(int i=1;i<=m;i++)
	{
		sl(sub[i].a);
		ll x;
		sl(x);
		sub[i].sz= x-sub[i].a+1;
		s(sub[i].c);
		sub[i].ind=i;
	}
	sort(sub+1,sub+m+1,compare);
	for(int j=1;j<=m;j++)
	{
		for(int k=0;k<sub[j].sz;k++)
		{
			dp[1][j][k][0] = sub[j].a+k;
			dp[1][j][k][1] = -1;
			dp[1][j][k][2]= -1;
		}
	}

	for(int i=2;i<=n;i++)
	{
		for(int j=i;j<=m;j++)
		{
			for(int k=0;k<sub[j].sz;k++)
			{
				ll val=sub[j].a+k;
				ll oneway=-1;
				if(val%mul == 0)
				 oneway=val/mul;
				ll twoway=val-mul;
				dp[i][j][k][0]=0;
				dp[i][j][k][1]=-1;
				dp[i][j][k][2]= -1;
				ll maxi=0;
				int idx=-1;
				int off=-1;
				for(int l=1;l<j;l++)
				{
					if(sub[l].c == sub[j].c)
					break;
					ll off1=oneway-sub[l].a;
					ll off2=twoway-sub[l].a;
					if(off1 >=0 && off1 < sub[l].sz && dp[i-1][l][off1][0])
					{
						if(maxi < dp[i-1][l][off1][0])
						{
							maxi=dp[i-1][l][off1][0];
							idx=l;
							off=off1;
						}

					}
					if(off2 >=0 && off2 < sub[l].sz && dp[i-1][l][off2][0])
					{
						if(maxi < dp[i-1][l][off2][0])
						{
							maxi=dp[i-1][l][off2][0];
							idx=l;
							off=off2;
						}
					}
				}
				if(idx == -1)
				continue;
				dp[i][j][k][0] = maxi+sub[j].a+k;
				dp[i][j][k][1]= idx;
				dp[i][j][k][2]=off;
			}
		}
	}
	int idx=-1;
	ll maxi=0;
	int off=-1;
	int flag=0;

	for(int j=1;j<=m;j++)
	{
		for(int k=0;k<sub[j].sz;k++)
		{

			if(maxi < dp[n][j][k][0])
			{
				maxi=dp[n][j][k][0];
				idx=j;
				off=k;
				flag=1;
			}
		}
	}
	if(flag == 0)
	{
		printf("NO\n");
		return 0;
	}

	printf("YES\n");
	vector< pair<int,ll> > ans;
	for(int x=n;x>=1;x--)
	{
		ans.pb(make_pair(sub[idx].ind,sub[idx].a+off));
	//	printf("%d %d\n",idx,off);
		int val1=dp[x][idx][off][1];
		off=dp[x][idx][off][2];
		idx=val1;
	}
	for(int i=ans.size()-1;i>=0;i--)
	{
		printf("%d %lld\n",ans[i].F,ans[i].S);
	}
}

/*
 * Author: tender
 * Created Time:  2011/10/14 23:58:53
 * File Name: c.cpp
 */
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <map>
#include <queue>
#include <string>
#include <vector>

using namespace std;
const double pi = acos(-1.0);
long long dp[52][52][102];
struct node1 {
    int i, k;
}pre[52][52][102];
struct node {
    long long a, b;
    int c;
    int idx;
    bool operator < (const node &aa) const {
        return c < aa.c;
    }
}ex[55];
void print(int i, int j, int k){
    if (j > 1) print(pre[i][j][k].i, j - 1, pre[i][j][k].k);
    printf("%d %I64d\n", ex[i].idx, ex[i].a + k);
}
long long _sum;
void _func(int i, int j, int k) {
    _sum += dp[i][j][k];
    if (j > 1)
        _func(pre[i][j][k].i, j - 1, pre[i][j][k].k);
}
int main() {
    //freopen("2.txt", "w", stdout);
    int n, m, K;
    scanf("%d%d%d", &n, &m, &K);
    for (int i = 1; i <= m; i++) {
        scanf("%I64d%I64d%d", &ex[i].a, &ex[i].b, &ex[i].c);
        ex[i].idx = i;
    }
    sort(ex + 1, ex + m + 1);
    for (int i = 1; i <= m; i++)
        for (int j = 1; j <= n; j++) {
            long long end = ex[i].b - ex[i].a;
            for (int k = 0; k <= end; k++) {
                if (j == 1)
                    dp[i][j][k] = ex[i].a + k;
                else {
                    long long _max = 0, t = k + ex[i].a;
                    int j1 = j - 1;
                    for (int i1 = i - 1; i1 > 0; i1--) {
                        if (ex[i1].c >= ex[i].c) continue;
                        long long k1 = t - K;
                        if (k1 >= ex[i1].a && k1 <= ex[i1].b) {
                            if (_max < dp[i1][j1][k1 - ex[i1].a]) {
                                _max = dp[i1][j1][k1 - ex[i1].a];
                                pre[i][j][k].i = i1; pre[i][j][k].k = k1 - ex[i1].a;
                            }
                        }
                        //_max = max(_max, dp[i1][j1][k1 - ex[i1].a]);
                        k1 = t / K;
                        if (k1 * K == t && k1 >= ex[i1].a && k1 <= ex[i1].b) {
                            if (_max < dp[i1][j1][k1 - ex[i1].a]) {
                                _max = dp[i1][j1][k1 - ex[i1].a];
                                pre[i][j][k].i = i1; pre[i][j][k].k = k1 - ex[i1].a;
                            }
                        }
                        //_max = max(_max, dp[i1][j1][k1 - ex[i1].a]);
                    }
                    if (_max != 0)
                        dp[i][j][k] = _max + t;
                }
            }
        }
    //for (int i = 1; i <= m; i++)
        //for (int j = 1; j <= n; j++) {
            //long long end = ex[i].b - ex[i].a;
            //for (int k = 0; k <= end; k++)
                //printf("dp[%d][%d][%I64d] = %I64d ", i, j, k + ex[i].a, dp[i][j][k]);
            //printf("\n");
        //}
    //for (int i = 1; i <= m; i++)
        //for (int j = 1; j <= n; j++) {
            //long long end = ex[i].b - ex[i].a;
            //for (int k = 0; k <= end; k++)
                //printf("pre[%d][%d][%I64d] = %d %d %d ", i, j, k + ex[i].a, pre[i][j][k].i, j - 1, pre[i][j][k].k);
            //printf("\n");
        //}
    long long _max = 0;
    int i1, k1;
    for (int i = n; i <= m; i++) {
        long long end = ex[i].b - ex[i].a;
        for (int k = 0; k <= end; k++) {
            if (_max < dp[i][n][k]) {
                _max = dp[i][n][k];
                i1 = i; k1 = k;
            }
        }
        //_max = max(_max, dp[i][n][k]);
    }
    if (_max == 0)
        printf("NO\n");
    else {
        printf("YES\n");
        //printf("%I64d\n", _max);
        //printf("i1 = %d k1 = %d\n", i1, k1);
        print(i1, n, k1);
    }
    return 0;
}

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

template <typename T>
inline void read(T &x){
    x = 0;int fu = 1;
    char c = getchar();
    while(c > 57 || c < 48){
        if(c == 45) fu = -1;
        c = getchar();
    }
    while(c <= 57 && c >= 48){
        x = (x << 3) + (x << 1) + c - 48;
        c = getchar();
    }
    x *= fu;
}
template <typename T>
inline void fprint(T x){
    if(x < 0) putchar(45), x = -x;
    if(x > 9) fprint(x / 10);
    putchar(x % 10 + 48);
}
template <typename T>
inline void fprint(T x, char ch){
    fprint(x);putchar(ch);
}
#define int long long
int n, m, k;

int dp[55][55][105];
typedef pair <int, int> pi;

pi pre[55][55][105];

struct Subject {
    int l, r, val, id;
    inline bool operator < (const Subject &tmp) const{
        return val < tmp.val;
    }
}a[55];

inline void output(int x, int y, int z){
    if(y == 1) {printf("%lld %lld\n", a[x].id, a[x].l + z);return;}
    pi cur = pre[x][y][z];
    output(cur.first, y - 1, cur.second);
    printf("%lld %lld\n", a[x].id, a[x].l + z);
}

signed main(){
    read(n);read(m);read(k);
    for (register int i = 1;i <= m;i ++){read(a[i].l);read(a[i].r);read(a[i].val);a[i].id = i;}
    sort(a + 1, a + m + 1);
    for (register int i = 1;i <= m;i ++){
        for (register int j = 1;j <= n && j <= i;j ++){
            for (register int p = a[i].l;p <= a[i].r;p ++){
                int dis = p - a[i].l;
                if(j == 1) {dp[i][j][dis] = p;continue;}
                for (register int q = 1;q < i;q ++){
                    if(a[q].val == a[i].val) continue;
                    if(p - k >= a[q].l && p - k <= a[q].r && dp[q][j - 1][p - k - a[q].l] && dp[q][j - 1][p - k - a[q].l] + p > dp[i][j][dis]) dp[i][j][dis] = dp[q][j - 1][p - k - a[q].l] + p, pre[i][j][dis] = make_pair(q, p - k - a[q].l);
                    if(p % k == 0 && (p / k) >= a[q].l && (p / k) <= a[q].r && dp[q][j - 1][(p / k) - a[q].l] && dp[q][j - 1][(p / k) - a[q].l] + p > dp[i][j][dis]) dp[i][j][dis] = dp[q][j - 1][(p / k) - a[q].l] + p, pre[i][j][dis] = make_pair(q, (p / k) - a[q].l);
                }
            }
        }
    }
    pi pos;
    int ans = -0x3f3f3f3f;
    for (register int i = n;i <= m;i ++){
        for (register int p = a[i].r;p >= a[i].l;p --){
            int dis = p - a[i].l;
            if(dp[i][n][dis] && dp[i][n][dis] > ans) pos = make_pair(i, dis), ans = dp[i][n][dis];
        }
    }
    if(ans == -0x3f3f3f3f) printf("NO\n");
    else{
        printf("YES\n");
        output(pos.first, n, pos.second);
    }

    return 0;
}

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cmath>
using namespace std;

#define e -1e18
int n,m,k,c[55],p[55],u[55][55][105],pr[55][55][105],w[55][55][105];
__int64 a[55],b[55],r[55][55][105];

bool f(int x,int y)
{
    return c[x]<c[y];
}

__int64 g(int s,int l,__int64 v)
{
    int d,i,x,y;
    __int64 t1,t2,cur=e;
    d=p[s];
    if(s==m)return int (l!=n)*e;
    if(v<a[d]||v>b[d])return e;
    if(w[s][l][v-a[d]])return r[s][l][v-a[d]];
    w[s][l][v-a[d]]=1;
    x=y=0;
    for(i=s+1;i<=m;i++)
    {
        if(c[p[i]]<=c[d])continue;
        t1=g(i,l+1,v+k)+v;
        t2=g(i,l+1,v*k)+v;
        if(cur<t1)
        {
            cur=t1;
            x=0;
            y=i;
        }
        if(cur<t2)
        {
            cur=t2;
            x=1;
            y=i;
        }
    }
    r[s][l][v-a[d]]=cur;
    u[s][l][v-a[d]]=x;
    pr[s][l][v-a[d]]=y;
    return cur;
}

void o(int s,int l,__int64 v)
{
    int d=p[s];
    if(s==m)return;
    printf("%d %I64d\n",d+1,v);
    if(u[s][l][v-a[d]])o(pr[s][l][v-a[d]],l+1,v*k);
    else o(pr[s][l][v-a[d]],l+1,v+k);
}
int main()
{
    //freopen("a.txt","r",stdin);
    int i,x;
    __int64 y,j,sum,cur;
    scanf("%d%d%d",&n,&m,&k);
    for(i=0;i<m;i++)
    {
        scanf("%I64d%I64d%d",a+i,b+i,c+i);
        p[i]=i;
    }
    p[m]=m;
    c[m]=101;
    sort(p,p+m,f);
    memset(w,0,sizeof(w));
    sum=e;
    x=y=0;
    for(i=0;i<m;i++)
        for(j=a[p[i]];j<=b[p[i]];j++)
        {
            cur=g(i,0,j);
            if(sum<cur)
            {
                sum=cur;
                x=i;
                y=j;
            }
        }
    if(sum<0)printf("NO\n");
    else
    {
        printf("YES\n");
        o(x,0,y);
    }
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n,m,kk;
ll mp[109];
pair<ll,pair<ll,ll> > p[100009];
pair<ll,pair<ll,pair<ll,ll> > > p2[100009];
ll inf=9000000000000000009ll;
ll dp[59][59][59][109];
ll bt(ll id,ll K,ll last,ll more){
	if(id==m){
		if(K==n)return 0;
		else return -inf;
	}
	ll &ret=dp[id][K][last][more];
	if(ret!=-1)return ret;
	ret=bt(id+1,K,last,more);
	if(K==0){
		for(ll i=0;i<=p[id].second.second-p[id].second.first;i++){
			ret=max(ret,bt(id+1,1,id,i)+p[id].second.first+i);
		}
	}
	else{
		if(p[last].first<p[id].first){
			ll x=(p[last].second.first+more)*kk;
			ll xx=(p[last].second.first+more)+kk;
			if(x>=p[id].second.first && x<=p[id].second.second)ret=max(ret,bt(id+1,K+1,id,x-p[id].second.first)+x);
			if(xx>=p[id].second.first && xx<=p[id].second.second)ret=max(ret,bt(id+1,K+1,id,xx-p[id].second.first)+xx);
		}
	}
	return ret;
}
void path(ll id,ll K,ll last,ll more){
	if(id==m)return;
	ll ret=bt(id,K,last,more);
	ll ans1=bt(id+1,K,last,more);
	if(ans1==ret){
		path(id+1,K,last,more);
		return;
	}
	if(K==0){
		for(ll i=0;i<=p[id].second.second-p[id].second.first;i++){
            ll ans2=bt(id+1,1,id,i)+p[id].second.first+i;
			if(ans2==ret){
				cout<<mp[id]<<" "<<p[id].second.first+i<<endl;
				path(id+1,1,id,i);
				return;
			}
        }
	}
	else{
		if(p[last].first<p[id].first){
            ll x=(p[last].second.first+more)*kk;
            ll xx=(p[last].second.first+more)+kk;
            if(x>=p[id].second.first && x<=p[id].second.second){
				ll ans3=bt(id+1,K+1,id,x-p[id].second.first)+x;
				if(ans3==ret){
					cout<<mp[id]<<" "<<x<<endl;
					path(id+1,K+1,id,x-p[id].second.first);
					return;
				}
			}
            if(xx>=p[id].second.first && xx<=p[id].second.second){
				ll ans4=bt(id+1,K+1,id,xx-p[id].second.first)+xx;
				if(ans4==ret){
					cout<<mp[id]<<" "<<xx<<endl;
					path(id+1,K+1,id,xx-p[id].second.first);
					return;
				}
			}
        }
	}
}
int main(){
	memset(dp,-1,sizeof(dp));
	cin>>n>>m>>kk;
	for(int i=0;i<m;i++){
		ll a,b,c;
		cin>>a>>b>>c;
		p[i]={c,{a,b}};
		p2[i]={c,{a,{b,i}}};
	}
	sort(p,p+m);
	sort(p2,p2+m);
	for(int i=0;i<m;i++){
		mp[i]=p2[i].second.second.second+1;
	}
	ll xxx=bt(0,0,0,0);
	if(xxx<=0)cout<<"NO"<<endl,exit(0);
	else cout<<"YES"<<endl;
	path(0,0,0,0);
}

#include <iostream>
#include <ctime>
#include <fstream>
#include <cmath>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <complex>
#include <utility>
#include <cctype>

using namespace std;

#define FORALL(i,a,b) for(int i=(a);i<=(b);++i)
#define FOR(i,n) for(int i=0;i<(n);++i)
#define FORB(i,a,b) for(int i=(a);i>=(b);--i)

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef complex<ld> vec;

typedef pair<int,int> pii;
typedef map<int,int> mii;

#define pb push_back
#define mp make_pair

#define INF 1000000000000000010ll
#define MAXN 52
#define MAXM 52
#define MAXD 102
ll A[MAXM];
ll B[MAXM];
ll C[MAXM];

ll dp[MAXN][MAXM][MAXD];
int subj[MAXN][MAXM][MAXD];	// choice
int hw[MAXN][MAXM][MAXD];	// the choice of how much homework to assign
bool seen[MAXN][MAXM][MAXD];

int N,M,K;

// update an answer with a better (higher) one
void check(ll& a1, int& s1, int& h1,
		   ll a2, int s2, int h2) {
	if (a2 <= a1) return;
	a1 = a2; s1 = s2; h1 = h2;
}

// return max amount of homework
ll solve(int n, int m, int x) {
	ll& ans = dp[n][m][x];
	if (seen[n][m][x]) return ans;
	seen[n][m][x] = true;

	if (n==0) return ans = 0;

	ans = -INF;
	int& ans_subj = subj[n][m][x];
	int& ans_hw = hw[n][m][x];

	ll amt = A[m] + x, aplus = amt + K, atimes = amt*K;

	// pick next subject
	FOR(h,M) {
		if (C[h] <= C[m]) continue;
		ll ans_plusk = ((A[h]<=aplus&&aplus<=B[h])?(solve(n-1, h, aplus-A[h])+ (aplus)):-INF);
		ll ans_times = ((A[h]<=atimes&&atimes<=B[h])?(solve(n-1,h,atimes-A[h])+ (atimes)):-INF);

		check(ans,ans_subj,ans_hw, ans_plusk,h,aplus-A[h]);
		check(ans,ans_subj,ans_hw, ans_times,h,atimes-A[h]);
	}

	return ans;
}

int main(){
	memset(seen,0,sizeof(seen));

	scanf("%d%d%d",&N,&M,&K);
	FOR(j,M) cin >> A[j] >> B[j] >> C[j];

	ll ans=-INF; int asubj=0; int ahw=0;
	FOR(j,M) FORALL(x,0,B[j]-A[j]) {
		ll ans2 = solve(N-1,j,x) + (A[j]+x);
		int subj2 = j;
		int hw2 = x;
		check(ans,asubj,ahw,ans2,subj2,hw2);
	}

	if (ans < 0) {
		cout << "NO" << endl;
		return 0;
	}

	cout << "YES" << endl;
	int n = N, m = asubj, x = ahw;
	while(n) {
		assert(x>=0 && x <= (B[m]-A[m]));
		cout << (m+1) << " " << (x + A[m]) << endl;
		int newm = subj[n-1][m][x];
		int newx = hw[n-1][m][x];
		n = n-1;
		m = newm;
		x = newx;
	}

	return 0;
}

#include <stdio.h>
#include<queue>
#include <algorithm>
#include <iostream>
#include <vector>
#include <string>
#include <set>
#define mp make_pair
#ifdef WIN32
	#define LLD "%I64d"
#else
	#define LLD "%lld"
#endif
using namespace std;
const double eps = 1e-6;
struct subj {
	long long sup, inf;
	int skill;
	int id;
	void read() {scanf("%lld%lld%d", &inf, &sup, &skill);}
	bool operator<(subj s) const {
		return skill < s.skill;
	}
};
subj a[60];
long long d[60][102][60];
pair<int, int> p[60][102][60];
void print(int y, int x, int n) {
	if (n > 1)
		print(p[y][x][n].first, p[y][x][n].second, n - 1);
	printf("%d %lld\n", a[y].id, a[y].inf + x);
}

int main() {
	//freopen("input.txt", "r", stdin);
	int n, m, k;
	scanf("%d%d%d", &n, &m, &k);
	for (int i=1; i<=m; i++) {a[i].read(); a[i].id = i;}
	sort(a + 1, a + m + 1);
	for (int j=1; j<=m; j++) {
		for (int i=0; i<=a[j].sup-a[j].inf; i++)
			d[j][i][1] = a[j].inf + i;
	}
	for (int i=1; i<=m; i++) {
		for (int j=0; j<=a[i].sup - a[i].inf; j++) {
			for (int z=1; z <= i; z++) {
				for (int x = 1; x < i; x++) {
						long long y = a[i].inf + j - k - a[x].inf;
						if (0 <= y && a[x].inf + y <= a[x].sup && a[i].skill > a[x].skill) {
							if (d[x][y][z-1] > 0 && d[x][y][z-1] + a[i].inf + j > d[i][j][z]) {
								d[i][j][z] = d[x][y][z-1] + a[i].inf + j;
								p[i][j][z] = mp(x, y);
							}
						}
						if ((a[i].inf + j) % k == 0) {
							long long y = (a[i].inf + j) / k - a[x].inf;
							if (0 <= y && a[x].inf + y <= a[x].sup && a[i].skill > a[x].skill) {
							if (d[x][y][z-1] > 0 && d[x][y][z-1] + a[i].inf + j > d[i][j][z]) {
								d[i][j][z] = d[x][y][z-1] + a[i].inf + j;
								p[i][j][z] = mp(x, y);
							}
						}
					}
				}
			}
		}
	}
	long long ans = 0;
	int y=-1, x=-1;
	for (int i=1; i<=m; i++) {
		for (int j=0; j<=100; j++) {
			if (d[i][j][n] > ans) {
				ans = d[i][j][n];
				y = i, x = j;
			}
		}
	}
	if (y == -1 || x == -1)
		puts("NO");
	else {
		puts("YES");
		print(y, x, n);
	}
	//printf("%lld", ans);
	return 0;
}

#include <bits/stdc++.h>
#include <unordered_map>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef map<int,int> ump;
#define N (100)
#define maxn (N + 5)
#define pii pair<int,int>
#define mp make_pair
const ll mod = 1000000007;
const ll INF = (1e9);
ll dp[maxn][maxn][maxn];
struct sub{
	ll ai,bi,ci,id;
}s[maxn];
struct chose{
	ll id,cnt;
	chose(){}
	chose(ll x,ll y):id(x),cnt(y){}
}pre[55][55][maxn],opt[maxn];
bool cmp(sub a,sub b){
	return a.ci < b.ci;
}
int main(){
	int n,m;ll d;scanf("%d%d%lld",&n,&m,&d);
	for(int i = 1;i <= m;i++){
		scanf("%lld%lld%lld",&s[i].ai,&s[i].bi,&s[i].ci);s[i].id = i;
	}
	sort(s + 1,s + 1 + m,cmp);
	for(ll i = 1;i <= m;i++){
		for(ll j = s[i].ai;j <= s[i].bi;j++){
			dp[1][i][j - s[i].ai] = j;
		}
	}
	for(ll i = 1;i < n;i++){
		for(ll j = i;j <= m && (m - j) >= (n - i);j++){
			for(ll k = s[j].ai;k <= s[j].bi;k++){
				ll cnt = dp[i][j][k - s[j].ai];
				if(cnt){
					for(ll jj = j + 1;jj <= m;jj++){
						if(s[j].ci == s[jj].ci)continue;
						if(k * d >= s[jj].ai && k * d <= s[jj].bi){
							if(dp[i + 1][jj][k * d - s[jj].ai] < cnt + k * d){
								dp[i + 1][jj][k * d - s[jj].ai] = cnt + k * d;
								pre[i + 1][jj][k * d - s[jj].ai] = chose(j,k - s[j].ai);
							}
						}
						if(k + d >= s[jj].ai && k + d <= s[jj].bi){
							if(dp[i + 1][jj][k + d - s[jj].ai] < cnt + k + d){
								dp[i + 1][jj][k + d - s[jj].ai] = cnt + k + d;
								pre[i + 1][jj][k + d - s[jj].ai] = chose(j,k - s[j].ai);
							}
						}
					}
				}
			}
		}
	}
	ll ans = 0;
	ll jj = 0,kk = 0;
	for(int i = n;i <= m;i++){
		for(ll j = s[i].ai;j <= s[i].bi;j++){
			if(ans < dp[n][i][j - s[i].ai]){
				ans = dp[n][i][j - s[i].ai];
				jj = i;kk = j - s[i].ai;
			}
		}
	}
	if(ans){
		printf("YES\n");
		int on = 0;
		chose now = chose(jj,kk);
		for(int i = n;i >= 1;i--){
			//printf("%lld %lld\n",s[jj].id,kk + s[jj].ai);
			opt[++on] = now;
			now = pre[i][now.id][now.cnt];
			//ll jj1 = pre[i][jj][kk].id,kk1 = pre[i][jj][kk].cnt;
			//jj = jj1;kk = kk1;
		}
		for(int i = n;i >= 1;i--){
			printf("%lld %lld\n",s[opt[i].id].id,opt[i].cnt + s[opt[i].id].ai);
		}
	}
	else{
		printf("NO\n");
	}
    return 0;
}

/*
  - Author : Ahmed Araby
  - submitted by an Alien so it's not and ordinary code,
    don't till any one.
*/
#include <bits/stdc++.h>
/*#include <iostream>
#include <vector>
#include <map>
#include <unordered_map>
#include <queue>
#include <algorithm>
#include <math.h>*/
#define ll long long
#define pi pair<int , int>
#define pl pair<long long , long long>
using namespace std;

void run()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
}
struct info
{
    ll a , b;
    int i , c;
    info(){}
    info(ll aa, ll bb , int cc , int ii)
    {
        a=aa;
        b=bb;
        c=cc;
        i=ii;
    }
    bool operator <(info &obj) const
    {
        return c<obj.c;
    }
};
int n , m , k;
vector<info> v;
ll mem[55][55][105];
pair<int , int> path[55][55][105];
ll solve(int i , int p , int x)
{
    if(i==n)
        return 0;
    else if(p==m-1)
        return -LONG_LONG_MAX;
    ll &ret=mem[i][p][x];
    if(ret!=-1)
        return ret;
    ret=-LONG_LONG_MAX;
    // choose proper subject
    for(int j=p+1; j<m; j++)  // considered as forth dimension
    {
        if(v[j].c<=v[p].c) // violate the condition of strict increasing
            continue;
        ll a=v[j].a;
        ll b=v[j].b;
        // change from before
        ll prv=v[p].a;
        prv+=x; // How I went forward from previous "a"
        ll t1=-LONG_LONG_MAX , t2=-LONG_LONG_MAX , tok=0;
        // previous choice determine what I'm going to do
        // check if I can put thin number of exercises
        if(prv+k>=a && prv+k<=b)
            t1=solve(i+1, j , (k+prv)-a)+(k+prv);  // take care of sign when we get X
        if(prv*k>=a && prv*k<=b)
            t2=solve(i+1 , j , (k*prv)-a)+(k*prv);
        if(t1>=t2)
            tok=k+prv;
        else{
            t1=t2;
            tok=k*prv;
        }
        if(t1>ret)
        {
            ret=t1;
            // mark you choice  , store index of subject and added value
            int dif=tok-a;
            path[i][p][x]={j , dif};
        }
    }
    return ret;
}
void build(int i, int p , int x)  // theses parameters belong to  the previous choice
{
    while(i!=n)
    {
        int index=path[i][p][x].first;
        int dif=path[i][p][x].second;
        cout<<v[index].i<<" "<<v[index].a+dif<< endl;
        // move forward
        p=index;
        x=dif;
        i++;
    }
    return ;
}
int main()
{
	run();
	//freopen("input.txt" ,"r" , stdin);
	//freopen("output.txt" , "w", stdout);
	cin>>n>>m>>k;
	for(int i=0; i<=n; i++)
        for(int j=0; j<=m; j++)
        for(int q=0; q<=100; q++)
        mem[i][j][q]=-1;

	for(int i=0; i<m; i++)
    {
        ll a , b;
        int c;
        cin>>a>>b>>c;
        v.push_back(info(a, b , c , i+1));
    }
    sort(v.begin() , v.end());
    int p;
    ll c , maxi=-LONG_LONG_MAX;
    for(int i=0; i<=m-n; i++)  // choose the first day  , (m-n) number of subject I can mess and still able to have N subjects , we cant mess the (m-n) th subject
    {
        ll a=v[i].a;
        ll b=v[i].b;
        ll tmp=0;
        for(; a<=b; a++)
        {
            int x=a-v[i].a;
            tmp=solve(1 ,i ,x)+a;
            if(tmp>maxi)
            {
                maxi=tmp;
                p=i;
                c=a;
            }
        }
    }
    if(maxi>0)
    {
        cout<<"YES"<<endl;
        //cout<<maxi<< endl;
        cout<<v[p].i<<" "<<c<< endl;
        // build
        int x=c-v[p].a;
        build(1 , p , x); // same parameter that we get our solution with
    }
    else
        cout<<"NO"<< endl;
	return 0;
}

#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <climits>
#include <cstring>

using namespace std;

#define mp make_pair
#define pp push_back
#define Sort(x) sort(x.begin(), x.end())
#define rep(i, x, y) for(int i = x; i < y; ++i)
#define Rep(i, x, y) for(int i = x; i <= y; ++i)
#define dRep(i, x, y) for(int i = x;i >= y; --i)
#define vi vector<int>
#define vvi vector<vector<int> >
#define ll long long
#define all(v) v.begin(),v.end()
#define ii pair<int, int>
#define mem(x, v) memset(x, v, sizeof(x))
#define nl '\n'
#define MOD 1000000007

ll days, courses, k;
int LIS[51];
vector<ll>lower, upper;
vi complexity;

ll dp[51][51][101];
ii nxt[51][51][101];

ll solve(int index,int rem, ll cur)
{

    if(cur < lower[index] || cur > upper[index])
    {
        return -2;
    }
    else if(!rem)
    {
        return cur;
    }
    else if(dp[index][rem][upper[index] - cur] != -1)
    {
        if(dp[index][rem][upper[index] - cur] == -2)
            return -2;
        return dp[index][rem][upper[index] - cur] + cur;
    }
    else
    {
        ll ret = -2;
        rep(i, index + 1, courses - rem + 1)
        {
            if(complexity[i] > complexity[index])
            {
                ll res1, res2;
                res1 = res2 = -2;
                if(cur + k >= lower[i] && cur + k <= upper[i])
                    res1 = solve(i, rem - 1, cur + k);
                if(cur * k >= lower[i] && cur * k <= upper[i])
                    res2 = solve(i, rem - 1, cur * k);
                if(res1 > ret)
                {
                    nxt[index][rem][upper[index] - cur].first = i;
                    nxt[index][rem][upper[index] - cur].second = 0;
                }
                ret = max(res1 , ret);
                if(res2 > ret)
                {
                    nxt[index][rem][upper[index] - cur].first = i;
                    nxt[index][rem][upper[index] - cur].second = 1;
                }
                ret = max(res2 , ret);

            }
        }

        dp[index][rem][upper[index] - cur] = ret;

        if(ret == -2)
        {
            return ret;
        }
        else
        {
            return ret + cur;
        }
    }

}

int calcLIS(int index)
{
    if(index == courses)
    {
        return 0;
    }
    else if(LIS[index] != -1)
    {
        return LIS[index];
    }
    else
    {
        LIS[index] = 1;
        rep(i, index + 1, courses)
        {
            if(complexity[i] > complexity[index] && !(k * upper[index] < lower[i] && upper[index] + k < lower[i]) && !(k * lower[index] > upper[i] && lower[index] + k > upper[i]) )
            {
                LIS[index] = max(LIS[index], calcLIS(i) + 1);
            }
        }

        return LIS[index];
    }

}

int main()
{
    vi indexes;
    //freopen("input.in", "r", stdin);
    while(cin >> days >> courses >> k)
    {
        memset(dp, -1, 262701 * sizeof(dp[0][0][0]));
        lower = vector<ll>(courses);
        upper = vector<ll>(courses);
        complexity = vi(courses);
        memset(LIS, -1, sizeof(LIS));
        indexes.clear();
        rep(i, 0, courses)
        {
            cin >> lower[i] >> upper[i] >> complexity[i];
            indexes.pp(i + 1);
        }

        rep(i, 0, courses)
        {
            rep(j, i + 1, courses)
            {
                if(complexity[i] > complexity[j])
                {
                    swap(complexity[i], complexity[j]);
                    swap(lower[i], lower[j]);
                    swap(upper[i], upper[j]);
                    swap(indexes[i], indexes[j]);
                }
            }
        }

        bool valid = false;
        rep(i, 0, courses)
        {
            if(calcLIS(i) >= days)
            {
                valid = true;
            }
        }

        if(valid)
        {
            ll res = -2;
            ll temp = -2;
            ll start, val;
            rep(i, 0, courses)
            {
                if(LIS[i] >= days)
                {
                    for(ll j = upper[i]; j >= lower[i]; j--)
                    {
                        temp = max(res, solve(i, days - 1, j));
                        if(temp > res)
                        {
                            start = i;
                            val = j;
                        }
                        res = max(res, temp);
                    }
                }
            }

            if(res == -2)
                valid = 0;
            else
            {
                cout << "YES" << nl;
                int tmp;
                while(days--)
                {
                    cout << indexes[start] << " " << val << nl;
                    if(days)
                    {
                        tmp = start;
                        start = nxt[start][days][upper[start] - val].first;
                        if(nxt[tmp][days][upper[tmp] - val].second)
                        {
                            val *= k;
                        }
                        else
                        {
                            val += k;
                        }
                    }
                }
            }
        }
        if(!valid)
        {
            cout << "NO" << nl;
        }

    }
    return 0;
}

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

using namespace std;
#define MP make_pair
typedef long long LL;
const int MAXN = 100;
const int MAXM = 200;

int n,m,k;

struct NODE {
	LL a,b,c;
	int id;
}nd[MAXM];

bool cmp(const NODE& a, const NODE& b) {
	if (a.c == b.c)
		return a.id < b.id;
	else
	return a.c < b.c;
}

LL dp[53][53][103];
pair<int,int> back[53][53][103];

void doDP() {
	for (int i = 0; i < m; i++) {
		for(LL j = nd[i].a; j <= nd[i].b; j++) {
			dp[1][i][j - nd[i].a] = j;
			back[1][i][j - nd[i].a ] = MP<int,int>(0,0);
		}
	}

	for (int i = 1; i < n; i++) {
		for (int j = 0; j < m; j++) {
			for (int l = 0; l <= (int)(nd[j].b - nd[j].a); l++) if (dp[i][j][l]) {
				LL x,y;
				x = nd[j].a + l + k;
				y = (nd[j].a + l )*k;
				for (int p = j + 1; p <= m; p++) if (nd[p].c > nd[j].c) {
					if (x >= nd[p].a && x <= nd[p].b) {
						if (dp[i+1][p][x - nd[p].a]  <  dp[i][j][l] + x) {
							dp[i+1][p][x - nd[p].a] = dp[i][j][l] + x;
							back[i+1][p][x - nd[p].a] = MP<int,int>(j,l);
						//	cout<<"update + i = "<<i <<" j = "<<j<<" p = "<<p<<"ans = "<<dp[i+1][p][x - nd[p].a]<<endl;
						}
					}

					if (y >= nd[p].a && y <= nd[p].b) {
						if (dp[i+1][p][y - nd[p].a] < dp[i][j][l] + y) {
							dp[i+1][p][y - nd[p].a] = dp[i][j][l] + y;
							back[i+1][p][y - nd[p].a] = MP<int,int>(j,l);
					//		cout<<"update * i = "<<i <<" p = "<<p<<" abs(y) = "<<y - nd[p].a<<"ans = "<<dp[i+1][p][y - nd[p].a]<<endl;
						}
					}
				}
			}
		}
	}

	int rpi,rpj;
	rpi = rpj = -1;
	LL ans = 0;

	for (int i = 0; i < m; i++)
		for (int j = 0; j <= (int)(nd[i].b - nd[i].a); j++) {
		//	cout<<i<<" "<<j<< " "<<dp[n][i][j]<<endl;
				if (dp[n][i][j] > ans) {
					//cout<<ans<<endl;
					ans = dp[n][i][j];
					rpi = i;
					rpj = j;
				}
		}

	if (ans == 0)
		printf("NO\n");
	else {
		puts("YES");
		vector<pair<int,int> > res;
		for (int i = n ; i > 0; i--) {
			int j,l;
			res.push_back(MP<int,int>(rpi,rpj));
			j = back[i][rpi][rpj].first;
			l = back[i][rpi][rpj].second;
			rpi = j;
			rpj = l;
		}
		reverse(res.begin(),res.end());
		for (int i = 0; i < res.size(); i++) {
			cout<<nd[res[i].first].id+1<<" "<<res[i].second + nd[res[i].first].a<<endl;
		}
	}
}

void solve() {
	scanf("%d %d %d",&n,&m,&k);
	for (int i = 0; i < m; i++) {
		scanf("%I64d %I64d %I64d",&nd[i].a,&nd[i].b,&nd[i].c);
		nd[i].id = i;
	}
	sort(nd,nd+m,cmp);
	doDP();
}

int main() {
	int T = 1;
	while (T--) {
		solve();
	}
return 0;
}

#include <bits/stdc++.h>
#define int long long
#define fi first
#define se second
#define pb push_back
#define fastcin() ios_base::sync_with_stdio(false); cin.tie(0)
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef pair<int, int> ii;

int n, m, k, dp[100][100][200], cnt;
ii trace[100][100][200], res[100];
vector<int> com;

struct subject {
	int a, b, c, id;
	bool operator < (subject oth) {
		return c < oth.c;
	}
}S[100];

bool in(int x, int i) {
	return (x >= S[i].a and x <= S[i].b);
}

signed main()
{
	cin >> n >> m >> k;
	for(int i = 1, a, b, c; i <= m; i++) cin >> a >> b >> c, S[i] = {a, b, c, i};
	sort(S+1, S+m+1);
	for(int i = 0; i <= n; i++) for(int j = 1; j <= m; j++) for(int u = 0; u <= 100; u++) dp[i][j][u] = -1e18;
	for(int i = 1; i <= n; i++)
	{
		for(int j = 1; j <= m; j++)
			for(int u = 0; u <= S[j].b - S[j].a; u++)
			{
				if(i == 1)
				{
					dp[i][j][u] = u + S[j].a;
					trace[i][j][u] = {0, 0};
					continue;
				}
				for(int t = 1; t < j; t++)
				{
					if(S[t].c == S[j].c) continue;
					int d = (S[j].a + u) / k, diff = (in(S[j].a + u - k, t) ? dp[i-1][t][S[j].a + u - k - S[t].a] + u + S[j].a : -1e18),
						divs = ((S[j].a + u) % k == 0 and in(d, t) ? dp[i-1][t][d - S[t].a] + u + S[j].a : -1e18);
					if(dp[i][j][u] < diff)
						dp[i][j][u] = diff, trace[i][j][u] = {t, S[j].a + u - k - S[t].a};
					if(dp[i][j][u] < divs)
						dp[i][j][u] = divs, trace[i][j][u] = {t, d - S[t].a};
				}
			}
	}
	int ans = -1e18, sub, hw;
	for(int j = 1; j <= m; j++) for(int u = 0; u <= S[j].b - S[j].a; u++)
		if(ans < dp[n][j][u])
		{
			ans = dp[n][j][u];
			sub = j, hw = u;
		}
	if(ans <= 0) cout << "NO";
	else
	{
		int cur = n;
		while(1)
		{
 			res[cur] = {S[sub].id, hw + S[sub].a};
			if(cur == 1) break;
			int tmp = sub;
			sub = trace[cur][tmp][hw].fi, hw = trace[cur][tmp][hw].se;
			cur--;
		}
		cout << "YES\n";
		for(int i = 1; i <= n; i++) cout << res[i].fi << ' ' << res[i].se << '\n';
	}
}

#include <stdlib.h>
#include <time.h>
#include <vector>
#include <math.h>
#include <algorithm>
#include <sstream>
#include <iostream>
#include <fstream>
#include <string>
#include <string.h>
#include <map>
#include <stack>
#include <set>
#include <queue>
#include <utility>

using namespace std;

const int M=50+10;
typedef long long int ll;

ll dp[M][M][120];

int id[M][M][120];
ll val[M][M][120];

struct NODE{
	ll a;
	ll b;
	int c;
	int idx;
	bool operator < (const NODE& x) const
	{
		return c<x.c;
	}

}node[M];

int main()
{
	//freopen("input.txt","r",stdin);
	//freopen("output.txt","w",stdout);

	int n,m,K;
	cin>>n>>m>>K;

	for (int i=1; i<=m; i++)
	{
		cin>>node[i].a>>node[i].b>>node[i].c;
		node[i].idx=i;
	}

	sort(node+1, node+m+1);

	/*for (int i=1; i<=m; i++)
	{
		cout<<node[i].c<<endl;
	}*/

	for (int j=1; j<=m; j++)
	{
		for (int k=0; node[j].a+k<=node[j].b; k++)
		{
			dp[1][j][k]=node[j].a+k;
		}
	}

	for (int i=1; i<n; i++)
	{
		for (int j=i; j<=m; j++)
		{
			for (int k=0; node[j].a+k<=node[j].b; k++)
			{
				ll num=node[j].a+k;
				for (int nextj=j+1; nextj<=m; nextj++)
				{
					if (node[nextj].c!=node[j].c && dp[i][j][k]!=0)
					{
						if (num+K>=node[nextj].a && num+K<=node[nextj].b)
						{
							if (dp[i+1][nextj][num+K-node[nextj].a] < dp[i][j][k]+num+K)
							{
								dp[i+1][nextj][num+K-node[nextj].a] = dp[i][j][k]+num+K;
								id[i+1][nextj][num+K-node[nextj].a] = j;
								val[i+1][nextj][num+K-node[nextj].a] = k;
							}
						}
						if (num*K>=node[nextj].a && num*K<=node[nextj].b)
						{
							if (dp[i+1][nextj][num*K-node[nextj].a] < dp[i][j][k]+num*K)
							{
								dp[i+1][nextj][num*K-node[nextj].a] = dp[i][j][k]+num*K;
								id[i+1][nextj][num*K-node[nextj].a] = j;
								val[i+1][nextj][num*K-node[nextj].a] = k;
							}
						}
					}
				}
			}

		}
	}

	ll ans=0;
	int finalid=-1;
	int finalval=-1;
	for (int j=1; j<=m; j++)
	{
		for (int k=0; node[j].a+k<=node[j].b; k++)
		{
			if (dp[n][j][k]>ans)
			{
				ans=dp[n][j][k];
				finalid=j;
				finalval=k;
			}
		}
	}

	if (ans==0)
	{
		cout<<"NO"<<endl;
	}
	else
	{
		cout<<"YES"<<endl;

		vector<pair<int, ll> > res;
		res.push_back(make_pair(finalid, node[finalid].a+finalval));
		for (int i=n-1; i>=1; i--)
		{
			int tmpid= id[i+1][finalid][finalval];
			ll tmpval= val[i+1][finalid][finalval];
			res.push_back(make_pair(tmpid, node[tmpid].a+tmpval));

			finalid=tmpid;
			finalval=tmpval;
		}

		int len=res.size();
		for (int i=len-1; i>=0; i--)
		{
			int r1=node[res[i].first].idx;
			ll r2=res[i].second;
			cout<<r1<<" "<<r2<<endl;
		}
	}

	return 0;

}

#include <fstream>
#include <iostream>
#include <string>
#include <complex>
#include <cmath>
#include <set>
#include <vector>
#include <map>
#include <queue>
#include <cstdio>
#include <stack>
#include <algorithm>
#include <list>
#include <bitset>
#include <ctime>
#include <cassert>
#include <climits>
#include <iomanip>

using namespace std;

//```
#define ONLINE_JUDGE1

const double epsilon = 1e-7;
#define LL long long
#define ULL unsigned long long
#define MOD1 666013
#define MOD2 666019
#define MOD3 1999999973
#define MOD4 1000000007
#define base 73

typedef vector<int> vi;
typedef vector<long long> vl;
typedef vector<bool> vb;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<string> vs;
typedef pair<int, int> ii;
typedef pair<long long, long long> ll;
typedef vector<ii> vii;
typedef vector<ll> vll;

#define all(V) V.begin(), V.end()
#define allr(V) V.rbegin(), V.rend()
#define for_c_it(container, it) for (auto it : container)
#define present(container, element) (container.find(element) != container.end())
#define sz(a) int((a).size())
#define pb push_back
#define mp make_pair
#define zeroes(x) ((x ^ (x - 1)) & x)
#define BASE 10000

void add(int &A, int B, int MOD) {
	A = (A + B) % MOD;
}

class Subject {
public:
	LL a, b;
	int c, pos;
	Subject() : a(0), b(0), c(0) {}
};

bool operator<(const Subject S1,const Subject &S2) {
	return S1.c < S2.c;
}

int N, M, K;
vector<Subject> V;
LL dp[50][101][51];
ii sol[50][101][51], state;

int main() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
#endif
	int i, j, z, last, t;
	scanf("%d %d %d\n", &N, &M, &K);
	V.resize(M);
	for (i = 0; i < M; ++i) {
		scanf("%I64d %I64d %d", &V[i].a, &V[i].b, &V[i].c);
		V[i].pos = i + 1;
	}
	sort(all(V));
	state = mp(0, 0);
	for (i = 0; i < M; ++i) {
		for (j = 0; j <= V[i].b - V[i].a; ++j) {
			dp[i][j][1] = V[i].a + j;
			for (z = 2; z <= N; ++z) {
				dp[i][j][z] = -1;
				for (last = i - 1; last >= 0; --last) {
					if (V[last].c < V[i].c) {
						for(t = 0; t < 2; ++t) {
							LL now = V[i].a + j;
							if (!(t == 0 ? now % K == 0 : now >= K)) {
								continue;
							}
							now = (t == 0 ? now / K : now - K);
							now -= V[last].a;
							if (now >= 0 && now <= V[last].b - V[last].a
								&& dp[last][now][z - 1] != -1
								&& dp[last][now][z - 1] + V[i].a + j > dp[i][j][z]) {
									dp[i][j][z] = dp[last][now][z - 1] + V[i].a + j;
									sol[i][j][z] = mp(last, now);
							}
						}
					}
				}
			}
			if (dp[i][j][N] > dp[state.first][state.second][N]) {
				state = mp(i, j);
			}
		}
	}
	if (dp[state.first][state.second][N] == -1) {
		printf("NO");
	}
	else {
		printf("YES\n");
		vector< pair<int, LL> > ans;
		while (N) {
			ans.pb(mp(V[state.first].pos, V[state.first].a + state.second));
			state = sol[state.first][state.second][N];
			--N;
		}
		for (auto pp = ans.rbegin(); pp != ans.rend(); ++pp) {
			printf("%d %I64d\n", pp -> first, pp -> second);
		}
	}
	return 0;
}

#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <cstring>

using namespace std;

#define F first
#define PB(v,x) v.push_back(x)
#define size(X) (int)X.size()
#define S second
#define show(X) cerr << #X << " = " << X << endl;
#define getrandom(min,max) \
	((rand()%(int)(((max)+1)-(min)))+min)
#define foreach(var, container) \
	for(typeof((container).begin()) var = (container).begin(); \
			var != (container).end(); \
			++var)
#define SetBit(value, place) ((value) | (1 << (place)))
#define ClearBit(value, place) ((value) & (~(1 << (place))))
#define ToggleBit(value, place) ((value) ^ (1 << (place)))
#define GetBit(value, place) (((value) >> (place)) & 1)
#define GetPrecentage(Numerator, Denominator)	((Numerator)*100.0/(Denominator))
#define GI ({int t;scanf("%d",&t);t;})
#define GL ({int t;scanf("%I64d",&t);t;})
#define GD ({double t;scanf("%lf",&t);t;})
#define is istringstream
#define os ostringstream
#define lb lower_bound
#define ub upper_bound
#define bs binary_search
#define REP(i,n) for (int i=0,_n=(n); i < _n; i++)
#define REV(i,n) for (int i=(n)-1; i >= 0; i--)
#define FOR(i,a,b) for (int _b=(b), i=(a); i <= _b; i++)
#define ROR(i,a,b) for(int i=(a),_b=(b);i>=_b;i--)
#define ALL(c) (c).begin(), (c).end()
#define SORT(c) sort(ALL(c))
#define CLEAR(x) memset(x,0,sizeof x);
#define REVERSE(c) reverse(ALL(c))
#define UNIQUE(c) SORT(c),(c).resize(unique(ALL(c))-(c).begin())
#define INF (ll)1e17
#define eps 1.0e-11
#define PI(x) printf("%d ",x)
template <class X> void printa(X a,int n)
{
	REP(i,n) cerr << a[i] << " " ;
	cerr << endl;
}

template <class X> void printv(X v)
{
	foreach(it,v) cerr << *it << " " ;
	cerr << endl;
}
typedef pair<int,int> pii;
typedef long long ll;
typedef pair<ll,ll> pll;

const ll MAXN = 120;
ll n,m,k;
ll d[60][60][MAXN];
ll mark[60][60][MAXN];
pll parent[60][60][MAXN];
struct sub{
	ll c,a,b,i;
	sub(){;}
	friend istream &operator >> (istream &cin , sub&x)
	{
		cin >> x.a >> x.b >> x.c;
		return cin;
	}
	bool operator < (const sub &x) const { return c < x.c; }
}subj[MAXN];

void input()
{
	cin >> n >> m >> k;
	REP(i,m)
	{
		cin >> subj[i];
		subj[i].i = i;
	}
	sort(subj,subj+m);
}

ll memz(ll i,ll j,ll t)
{

	if(mark[i][j][t])
		return d[i][j][t];
	ll q = subj[i].a + t;
	mark[i][j][t] = 1;
	if(q > subj[i].b)
	{
		d[i][j][t] = -1*INF;
		return -1*INF;
	}
	if(j==0)
	{
		d[i][j][t] = q;
		return q;
	}
	if(i<j)
	{
		d[i][j][t] = -1*INF;
		return d[i][j][t];
	}
	ll p1 = q;
	ll p2 = q-k;
	bool tt = true;
	REV(z,i) if(subj[z].c < subj[i].c && z>=j-1)
	{
		if(p2>=subj[z].a && p2<=subj[z].b && d[i][j][t] < memz(z,j-1,p2-subj[z].a) + q)
		{
			d[i][j][t] = memz(z,j-1,p2-subj[z].a) + q;
			parent[i][j][t].F = z;
			parent[i][j][t].S = p2-subj[z].a;
			tt = false;
		}
		if((p1/k)>=subj[z].a && (p1/k)<=subj[z].b && !(p1%k) && d[i][j][t] < memz(z,j-1,(p1/k)-subj[z].a)+q)
		{
			d[i][j][t] = memz(z,j-1,(p1/k)-subj[z].a)+q;
			parent[i][j][t].F = z;
			parent[i][j][t].S = p1/k-subj[z].a;
			tt = false;
		}
	}
	if(tt)
	{
		d[i][j][t] = -1*INF;
		return d[i][j][t];
	}
	return d[i][j][t];
}

int main()
{
	input();
	ll MAX = 0;
	ll s = -1;
	ll s2,s3;
	memset(parent,-1,sizeof parent);
	REP(i,m) REP(j,101) if(memz(i,n-1,j) > MAX)
	{
		MAX = memz(i,n-1,j);
		s = i;
		s2 = n-1;
		s3 = j;
	}
	if(MAX == 0)
	{
		cout << "NO" << endl;
		return 0;
	}
	cout << "YES" << endl;
	vector<pll> ans;
	while(s!=-1)
	{
		PB(ans,pll(subj[s].i + 1,s3 + subj[s].a));
		ll p = s;
		s = parent[s][s2][s3].F;
		s3 = parent[p][s2][s3].S;
		s2--;
	}
	reverse(ans.begin(),ans.end());
	REP(i,size(ans))
		cout << ans[i].F << " " << ans[i].S << endl;
	return 0;
}

#include <iostream>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <string>
#include <sstream>
#include <stack>
#include <queue>
#include <map>
#include <numeric>
#include <cstdlib>
#include <cstring>
#include <utility>
#include <functional>
#include <cstdio>
#include <ctime>
using namespace std ;
#define eps 1e-10
#define ll long long
#define ld long double
#define ull unsigned long long
#define REP(i,k,n) for(int i=k;i<n;i++)
#define REV(i,k,n) for(int i=k;i>=n;i--)
#define SZ(x) ((int)x.size())
#define CLR(c,v) memset(c,v,sizeof (c))
////////////////////////////////
ll max (ll a,ll b) { return a > b ? a : b ; }
ll min (ll a,ll b) { return a < b ? a : b ; }
bool Eq (double a,double b) { return fabs (a - b) <= eps ; }
ll ABS (ll x) { return x < 0 ? - x : x ; }
//////////////////////
const int NN = 52 , MaxVal = 1000 * 1000 + 2 , p  = 43 , o = 83  ;
const ll  INF = (1LL << 59) , MOD2 = 1000000007 ;
/////////////////////
int xx [] = {-1,-1,-1,0,0,1,1,1} ;
int yy [] = {-1,0,1,-1,1,-1,0,1} ;
///////////////////////////
int bit (int x) { return x == 0 ? 0 : 1 + bit (x & (x - 1)) ; }

ll a [NN] , b [NN] , c [NN] ;
int n,m ;
ll k ;

ll dp [NN][NN * 2][NN] ; // cur , pre , dist , used ;
bool v[NN][NN * 2][NN] ;
struct path {
	char pre,dist,used;
	ll cost ;
	path () {} ;
	path (char Pre,char Dist,char Used,ll Cost) {
		pre = Pre ;
		dist = Dist ;
		used = Used ;
		cost = Cost ;
	}
};

path nex [NN][NN * 2][NN] ;

ll rec (int pos,int dist,int used) {
	if (used == 0) {
		return 0 ;
	}
	if (pos == m) {
		return - INF ;
	}
	ll &res = dp [pos][dist][used] ;
	if (v [pos][dist][used]) return res ;
	v [pos][dist][used] = true ;
	res = -INF ;

	if (used == n) {

		res = rec (pos + 1,dist,used) ;
		nex [pos][dist][used] = path (pos + 1,dist,used,0) ;

		for (ll i = a [pos] ; i <= b [pos]; i ++) {
			ll aux = rec (pos,i - a[pos],used - 1) + i ;
			if (aux > res) {
				res = aux ;
				nex [pos][dist][used] = path (pos,(ll)(i - a[pos]),used - 1,i) ;
			}
		}
	}else {
		ll exer =  a [pos] + dist ;
		REP (i,pos + 1,m) if (c [i] > c [pos]) {
			if (exer + k >= a [i] && exer + k <= b [i]) {
				ll aux = rec (i,ll(exer + k - a [i]),used - 1) + exer + k ;
				if (aux > res) {
					res = aux ;
					nex [pos][dist][used] = path (i,ll(exer + k - a [i]),used - 1,exer + k) ;
				}
			}
			if (exer * k >= a [i] && exer * k <= b [i]) {
				ll aux = rec (i,exer * k - a [i],used - 1) + exer * k ;
				if (aux > res) {
					res = aux ;
					nex [pos][dist][used] = path (i,exer * k - a [i],used - 1,exer * k) ;
				}
			}
		}
	}
	return res ;
}

int main () {
	//////////////////
#ifdef HOMEPC
	freopen ("in.in","r",stdin) ;
#endif
    ////////////////// Code ///////////////////
	scanf ("%d%d%d",&n,&m,&k) ;
	REP (i,0,m) {
		cin >> a [i] >> b [i] >> c [i] ;
	}
	vector <int> index (m) ;
	REP (i,0,m) index [i] = i + 1;
	REP (i,0,m) REP (j,i + 1,m) if (c [i] > c [j]) {
		swap (a[i],a[j]) ;
		swap (b[i],b[j]) ;
		swap (c[i],c[j]) ;
		swap (index [i],index [j]) ;
	}
	CLR (v,0) ;
	ll ans = rec (0,0,n) ;
	if (ans <= 0) {
		cout << "NO" << endl ;
	}else {
		cout << "YES" << endl ;
		int pos = 0 , pre = 0 , dist = 0 , used = n ;
		while (used > 0) {
			path p = nex [pos][dist][used] ;
			if (p.used + 1 == used) {
				cout << index [p.pre] << ' ' << p.cost << endl ;
			}
			pos = p.pre , dist = p.dist , used = p.used ;
		}
	}
    return 0 ;
}

#include <sstream>
#include <iostream>
#include <cstdio>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <utility>
#include <queue>
#include <deque>
#pragma comment(linker, "/STACK:16777216")

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector< pair<int, int> > vii;

#define foru(i,a,b) for(int i = int(a); i <= int(b); ++i)
#define ford(i,b,a) for(int i = int(b); i >= int(a); --i)
#define rep(i, n) for(int i = 0; i < int(n); ++i)
#define all(a) a.begin(),a.end()
#define size(a) int(a.size())
#define fill(a,x) memset(a, (x), sizeof(a))
#define mp(x,y) make_pair((x), (y))
#define pb(x) push_back((x))
#define fr first
#define sc second
#define tr(container, it) for(typeof(container.begin()) it = container.begin(); it != container.end(); it++)

int read() {   int x;   scanf("%d",&x);   return x;   }
int read(int &x) {  scanf("%d",&x);     return x;   }
int readln() {  int x;  scanf("%d\n",&x);   return x;   }
int readln(int &x) {   scanf("%d\n",&x);   return x;   }

const int MAX_N = 52;
const int MAX_D = 102;

struct subject {
    int c, id;
    ll a, b;
    subject (ll aa, ll bb, int cc, int i) {
        a = aa;
        b = bb;
        c = cc;
        id = i;
    }
    bool operator < (const subject &x) const {
        return c < x.c;
    }
};

int n, m, p;
vector<subject> sub;
ll f[MAX_N][MAX_N][MAX_D];
int trace[MAX_N][MAX_N][MAX_D][2];
ll res = 0;
int ri, rj, rk;

void update(int i, int j, int k, int ii, int kk) {
    if (kk < 0 || kk > sub[ii].b-sub[ii].a || sub[i].c == sub[ii].c) return;
    ll tmp = f[ii][j-1][kk]+sub[i].a+ll(k);
    if (f[ii][j-1][kk] < 1 || tmp <= f[i][j][k]) return;
    f[i][j][k] = tmp;
    trace[i][j][k][0] = ii;
    trace[i][j][k][1] = kk;
}

void print_res(int i, int j, int k) {
    if (i < 1 || j < 1) return;
    print_res(trace[i][j][k][0], j-1, trace[i][j][k][1]);
    //printf("%d %I64d\n", sub[i].id, sub[i].a+ll(k));
    cout << sub[i].id << " " << sub[i].a+ll(k) << endl;
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("119C.in", "r", stdin);
    freopen("119C.out", "w", stdout);
#endif
    scanf("%d%d%d", &n, &m, &p);
    sub.pb(subject(0,0,-1,0));
    foru(i,1,m) {
        ll a, b, c;
        //scanf("%lld%lld%d", &a, &b, &c);
        cin >> a >> b >> c;
        sub.pb(subject(a, b, c, i));
    }

    sort(all(sub));
    //foru(i,1,m) cout << sub[i].id << " " << sub[i].a << " "<< sub[i]. b<< " " << sub[i].c << endl;

    fill(f, 0);
    fill(trace, 0);
    foru(i,1,m) {
        foru(k,0,sub[i].b-sub[i].a) {
            f[i][1][k] = sub[i].a+k;
            int j = 1;
            if (j == n && f[i][j][k] > res) {
                res = f[i][j][k];
                ri = i; rj = j; rk = k;
            }
        }
        foru(j,2,min(i,n))
            foru(k,0,sub[i].b-sub[i].a) {
                foru(ii,1,i-1) {
                    update(i,j,k, ii, sub[i].a+k-p-sub[ii].a);
                    if ((sub[i].a+k)%p == 0)
                        update(i,j,k, ii, (sub[i].a+k)/p-sub[ii].a);
                }
                if (j == n && f[i][j][k] > res) {
                    res = f[i][j][k];
                    ri = i; rj = j; rk = k;
                }
            }
    }

    if (res < 1) {
        cout << "NO";
        return 0;
    }

    cout << "YES" << endl;
    print_res(ri, rj, rk);
    return 0;
}

//#pragma GCC optimize("Ofast")
//#pragma GCC optimize ("unroll-loops")
//#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define all(x) (x).begin(), (x).end()
#define ff first
#define ss second
#define setbits(x) __builtin_popcount(x)
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<db, db> pdd;
typedef vector<int> vi;
#define m1 make_pair
#define in insert
#define pb push_back
#define IOS                           \
    std::ios::sync_with_stdio(false); \
    cin.tie(NULL);                    \
    cout.tie(NULL);
#define endl "\n"
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

inline ll gcd(ll a, ll b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}
inline ll power(ll a, ll n, ll m)
{
    if (n == 0)
        return 1;
    ll p = power(a, n / 2, m);
    p = (p * p) % m;
    if (n % 2)
        return (p * a) % m;
    else
        return p;
}
const ll MOD = 1000000007;
const int N = int(3e5) + 999;
#define int long long int

struct inp{
	int a,b,c,index;
	bool operator< (const inp &v) const{
		return c<v.c;
	}
};

int n,m,k;

vector<inp> v;
pair<int,int> par[51][101][51];
int dp[51][101][51];

int32_t main(){
    IOS;

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

    cin>>n>>m>>k;

    v.resize(m);

    for(int i=0;i<m;i++){
    	cin>>v[i].a>>v[i].b>>v[i].c;
    	v[i].index = i+1;
    }
    sort(all(v));

    pair<int,int> lans = {0,0};

    for(int i=0;i<m;i++){

    	for(int diff=0;diff<=v[i].b-v[i].a;diff++){

    		int exc = diff + v[i].a;

    		dp[i][diff][1] = exc;

    		for(int place = 2;place<=n;place++){

    			dp[i][diff][place] = -1;
    			for(int last = i-1;last>=0;last--){

    				if(v[last].c==v[i].c)
    					continue;
    				if(exc%k==0){
    					int tmp = exc/k;
    					if(tmp>=v[last].a and tmp<=v[last].b){
    						tmp-=v[last].a;
    						if(dp[i][diff][place]<dp[last][tmp][place-1]+exc and dp[last][tmp][place-1]!=-1){
    							dp[i][diff][place] = dp[last][tmp][place-1]+exc;
    							par[i][diff][place] = {last,tmp};
    						}
    					}
    				}

    				if(exc>=k){
    					int tmp = exc-k;
    					if(tmp>=v[last].a and tmp<=v[last].b){
    						tmp-=v[last].a;
    						if(dp[i][diff][place]<dp[last][tmp][place-1]+exc and dp[last][tmp][place-1]!=-1){
    							dp[i][diff][place] = dp[last][tmp][place-1]+exc;
    							par[i][diff][place] = {last,tmp};
    						}
    					}
    				}
    			}
    		}

    		if(dp[i][diff][n]>dp[lans.ff][lans.ss][n]){
    			lans = {i,diff};

    		}
    	}
    }

    if(dp[lans.ff][lans.ss][n]==-1){
    	cout<<"NO"<<endl;
    	return 0;
    }

    cout<<"YES"<<endl;
    vector<pair<int,int>> ans;

    while(n>=1){
    	ans.pb({v[lans.ff].index,lans.ss+v[lans.ff].a});
    	lans = par[lans.ff][lans.ss][n];
    	n--;
    }

    reverse(all(ans));

    for(auto it:ans){
    	cout<<it.ff<<" "<<it.ss<<endl;
    }

    return 0;
}

#include <stdio.h>
#include <stdlib.h>

#define MAX_N 51
#define MAX_M 51
#define MAX_LEN 101

typedef struct path_info_st {
	int m;
	long long ht;
} path_info_st, *path_info_t;

long long A[MAX_M], B[MAX_M];
int C[MAX_M], L[MAX_M];
long long dp[MAX_N][MAX_M][MAX_LEN];
long long help1[MAX_M][MAX_LEN];
long long help2[MAX_M][MAX_LEN];
path_info_st path_data[MAX_N][MAX_M][MAX_LEN];
path_info_t path[MAX_N];

int N, M, K;

int main() {
	long long i, j, k, r, p, q, t, t1, t2, t3, t4, t5, t6;
	scanf("%d %d %d", &N, &M, &K);
	for( i=0;i<M;i++ )
		scanf("%I64d %I64d %d", &(A[i]), &(B[i]), &(C[i]));
	for( i=0;i<M;i++ )
		L[i] = (int)(B[i] - A[i]);

	for( i=0;i<M;i++ ) {
		t = L[i];
		t1 = A[i];

		for( j=0;j<=t;j++ ) {
			t2 = t1 + j;
			help1[i][j] = t2 / K;
		}

		for( j=0;j<=t;j++ ) {
			t2 = t1 + j;
			help2[i][j] = t2 - K;
		}
	}

	for( i=0;i<M;i++ ) {
		t1 = A[i];
		t = L[i];
		for( j=0;j<=t;j++ ) {
			dp[0][i][j] = t1 + j;
			path_data[0][i][j].m = -1;
			path_data[0][i][j].ht = -1;
		}
	}

	bool can_set_to_this_day;
	for( i=1;i<N;i++ ) {
		for( j=0;j<M;j++ ) {
			t = L[j];
			t1 = A[j];
			for( r=0;r<=t;r++ ) {
				t2 = t1 + r;
				p = help1[j][r];
				q = help2[j][r];
				/*p = t2 / K;
				q = t2 - K;*/
				for( k=0;k<M;k++ ) {
					if( C[k] < C[j] ) {
						t3 = A[k];
						t4 = B[k];
						if( p >= t3 && p <= t4 && (t2 % K) == 0 ) {
							t5 = dp[i][j][r];
							t6 = dp[i-1][k][p-t3];
							if( t6 > 0 && (0 == t5 || t5 < t2 + t6) ) { // t6 > 0 means there is a path to i-1 day set subject k and issues total t6 home task
								dp[i][j][r] = t2 + t6;
								path_data[i][j][r].m = k;
								path_data[i][j][r].ht = p-t3;
							}
						}
						if( q >= t3 && q <= t4 ) {
							t5 = dp[i][j][r];
							t6 = dp[i-1][k][q-t3];
							if( t6 > 0 && ( 0 == t5 || t5 < t2 + t6 ) ) {  // t6 > 0 means there is a path to i-1 day set subject k and issues total t6 home task
								dp[i][j][r] = t2 + t6;
								path_data[i][j][r].m = k;
								path_data[i][j][r].ht = q-t3;
							}
						}
					}
				}
			}
		}
	}

	k = 0;
	for( i=0;i<M;i++ ) {
		t = L[i];
		for( j=0;j<=t;j++ ) {
			t2 = dp[N-1][i][j];
			if( k < t2 ) {
				k = t2; t3 = i; t4 = j;
			}
		}
	}

	if( 0 == k ) {
		printf("NO\n");
		return 0;
	}

	k = 0, i = 1;
	printf("YES\n");
	if( N > 1 ) {
		path_info_t pi = &(path_data[N-1][t3][t4]);
		for( i=1;i<N-1;i++ ) {
			path[k++] = pi;
			pi = &(path_data[N-i-1][pi->m][pi->ht]);
		}
		path[k++] = pi;

		for( i=k-1;i>=0;i-- )
			printf("%d %I64d\n", (int)(path[i]->m + 1), (path[i]->ht + A[path[i]->m]));
	}
	printf("%d %I64d\n", (int)(t3 + 1), (t4 + A[t3]));

	return 0;
}

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

int main()
{
    ios_base::sync_with_stdio(false);

    ll n,m,k;
    cin>>n>>m>>k;

    vector < pair < ll, pair <ll, pair <ll,ll> > > > vec(m);

    for (ll i=0;i<m;i++)
    {
        cin>>vec[i].second.first>>vec[i].second.second.first>>vec[i].first;
        vec[i].second.second.second=i+1;
    }

    sort(vec.begin(),vec.end());

    ll dp[m][n+1][101];
    pair < ll, pair <ll,ll> > point[m][n+1][101];

    for (ll i=0;i<m;i++)
    {
        for (ll j=0;j<=n;j++)
        {
            for (ll l=0;l<=100;l++)
            {
                dp[i][j][l]=LLONG_MIN;
                point[i][j][l]={-1,{-1,-1}};
            }
        }
    }

    for (ll i=0;i<m;i++)
    {
        ll lmt=vec[i].second.second.first-vec[i].second.first;

        for (ll j=0;j<=lmt;j++)
        {
            dp[i][1][j]=vec[i].second.first+j;
            point[i][1][j]={i,{1,j}};
        }
    }

    for (ll i=0;i<m;i++)
    {
        for (ll j=2;j<=n;j++)
        {
            if (j>(i+1))    continue;

            ll temp=vec[i].second.second.first-vec[i].second.first;

            for (ll diff=0;diff<=temp;diff++)
            {
                ll mx=LLONG_MIN;
                ll index=-1;
                ll hw=-1;
                ll orig=vec[i].first;

                for (ll ind=0;ind<i;ind++)
                {
                    ll l=vec[ind].second.first,r=vec[ind].second.second.first;
                    ll key=diff+vec[i].second.first;
                    ll found=vec[ind].first;

                    if (orig==found)    continue;

                    ll tmp=LLONG_MIN;

                    if (key%k==0)
                    {
                        ll ky=key/k;

                        if (ky<=r && ky>=l)
                        {
                            ll t1=dp[ind][j-1][ky-l];

                            if (t1>mx)
                            {
                                mx=t1;
                                index=ind;
                                hw=ky-l;
                            }
                        }
                    }
                    if (key>=k)
                    {
                        ll ky=key-k;

                        if (ky<=r && ky>=l)
                        {
                            ll t1=max(tmp,dp[ind][j-1][ky-l]);

                            if (t1>mx)
                            {
                                mx=t1;
                                index=ind;
                                hw=ky-l;
                            }
                        }
                    }

                    mx=max(mx,tmp);
                }

                dp[i][j][diff]=mx+vec[i].second.first+diff;
                point[i][j][diff]={index,{j-1,hw}};

                //cout<<i<<" "<<j<<" "<<diff<<" "<<dp[i][j][diff]<<endl;
            }
        }
    }

    ll mx=LLONG_MIN;
    ll strt=-1;
    pair <ll, pair <ll,ll> > start;
    for (ll i=0;i<m;i++)
    {
        for (ll j=0;j<=100;j++)
        {
            ll t1=dp[i][n][j];

            if (t1>mx)
            {
                mx=t1;
                strt=i;
                start={i,{n,j}};
            }
        }
    }

    if (mx>0)
    {
        cout<<"YES"<<endl;
        ll cnt=n;
        ll prev=-1;

        vector < pair <ll,ll> > v(n);
        ll index=n-1;

        while (strt!=prev)
        {
            ll ind=vec[start.first].second.second.second;
            v[index]={ind,vec[start.first].second.first+start.second.second};
            //cout<<index<<" "<<v[index].first<<" "<<v[index].second<<endl;
            index--;
            prev=strt;

            start=point[start.first][start.second.first][start.second.second];
            strt=start.first;
        }

        for (ll i=0;i<n;i++)    cout<<v[i].first<<" "<<v[i].second<<endl;
    }
    else    cout<<"NO"<<endl;
}

/*
ID: keyvank2
TASK: combo
LANG: C++
*/

#include <bits/stdc++.h>

#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define MAXINT ((~0) ^ (1 << 31))
#define MAXLL ((~0) ^ ((ll)1 << 63))
#define INF ((int)2e9)
#define INFL ((ll)9e18)
#define FOR(i,j,k) for(long long i = j; i < k; i++)
#define REP(i,j,k) for(long long i = j; i >= k; i--)
#define setmax(i) const int maxn = (int) i;
#define setmod(i) const int MOD = (int) i;
#define all(a) a.begin(),a.end()

using namespace std;

//ifstream fin("");
//ofstream fout("");
//#define cin fin
//#define cout fout

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<ld,ld> pdd;
typedef pair<pii,int> ppi;
typedef pair<pll,ll> ppl;
typedef pair<int,pii> pip;
typedef pair<ll,pll> plp;
typedef pair<pii,pii> ppp;

template<class T> inline T pow2(T a) {return a*a;}
template<class T> inline bool mineq(T& a, T b) { if(a < b) {a=b;return true;} return false;}

//srand (time(NULL));

setmax(111);

struct node
{
    ll l,r,c,id;
    bool operator < (const node& a) const
    {
	return c < a.c;
    }
} num[maxn];

ll n,m,k;

struct nodedp
{
    ll val,now;
    pll pr;
    bool done;
} sdp[maxn][maxn][maxn];

ll dp(ll p, ll v, ll cnt)
{

    ll diff = v - num[p].l;

    if(v < num[p].l || v > num[p].r) return -1;
    if(cnt == 1) return v;
    if(sdp[p][diff][cnt].done) return sdp[p][diff][cnt].val;

    ll maxi = -1, now;
    FOR(t,0,p)
    {
	if(num[p].c == num[t].c) continue;

	now = dp(t, v - k, cnt - 1);
	if(now != -1 && now + v > maxi)
	{
	    maxi = now + v;
	    sdp[p][diff][cnt].pr = mp(t,v-k);
	}

	if(v % k == 0)
	{
	    now = dp(t,v/k,cnt-1);
	    if(now != -1 && maxi < now + v)
	    {
		maxi = now + v;
		sdp[p][diff][cnt].pr = mp(t,v/k);
	    }
	}
    }

    sdp[p][diff][cnt].val = maxi;
    sdp[p][diff][cnt].done = true;
    return maxi;
}

void print(ll i, ll j, ll cnt)
{
    if(cnt == 0) return;
    pll par = sdp[i][j-num[i].l][cnt].pr;
    print(par.ff,par.ss,cnt-1);
    cout << num[i].id+1 << " " << j << endl;
}

int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);

    cin >> n >> m >> k;
    FOR(i,0,m)
    {
	cin >> num[i].l >> num[i].r >> num[i].c;
	num[i].id = i;
    }

    sort(num,num+m);

    ll starti = -1, maxi = -1, startj = -1;;
    FOR(i,0,m)
    {
	FOR(j,num[i].l,num[i].r+1)
	{
	    ll now = dp(i,j,n);
	    if(now > maxi)
	    {
		maxi = now;
		starti = i;
		startj = j;
	    }
	}
    }

    if(starti == -1)
    {
	cout << "NO";
    }
    else
    {
	cout << "YES" << endl;
	print(starti,startj,n);
    }
}

#include<iostream>
#include<sstream>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
typedef vector<int>VI;typedef vector<VI>VVI;
typedef vector<string>VS;
typedef pair<int,int>PII;
#define FOR(i,n) for((i)=0;(i)<(n);(i)++)
#define FORN(i,n) for((i)=(n)-1;(i)>=0;(i)--)
#define BE(a) ((a).begin()),((a).end())
#define SI(a) ((a).size())
#define PB push_back
#define MP make_pair
#define FORIT(i,a) for((i)=(a).begin();(i)!=(a).end();(i)++)
#define CLR(a,v) memset((a),(v),sizeof(a))
template <class Ty, class Tx>
Ty cast(const Tx &x) { Ty y; stringstream ss(""); ss<<x; ss.seekg(0); ss>>y; return y; }

int n, m, k;
int c[55];
long long a[55], b[55];
long long memo[55][55][105];
int order[55];
int move[55][55][105];
long long movea[55][55][105];
bool seen[55][55][105];

long long doit(int day, int last, int d) {
    long long & ret = memo[day][1 + last][d];
    if (seen[day][1 + last][d]) return ret;
    seen[day][1 + last][d] = true;
    if (day >= n) return ret = 1;
    ret = 0;
    int i;
    long long cur, z;
    for (i = last + 1; i < m; i++) if (last < 0 || c[order[i]] > c[order[last]]) {
        if (day) {
            if (last < 0 || a[order[last]] + d + k >= a[order[i]] && a[order[last]] + d + k <= b[order[i]])
            if (doit(day + 1, i, a[order[last]] + d + k - a[order[i]])) {
                cur = a[order[last]] + d + k + doit(day + 1, i, a[order[last]] + d + k - a[order[i]]);
                if (!ret || cur > ret) {
                    ret = cur;
                    move[day][1 + last][d] = i;
                    movea[day][1 + last][d] = a[order[last]] + d + k - a[order[i]];
                }
            }
            if (last < 0 || (a[order[last]] + d) * k >= a[order[i]] && (a[order[last]] + d) * k <= b[order[i]])
            if (doit(day + 1, i, (a[order[last]] + d) * k - a[order[i]])) {
                cur = (a[order[last]] + d) * k + doit(day + 1, i, (a[order[last]] + d) * k - a[order[i]]);
                if (!ret || cur > ret) {
                    ret = cur;
                    move[day][1 + last][d] = i;
                    movea[day][1 + last][d] = (a[order[last]] + d) * k - a[order[i]];
                }
            }
        } else {
            for (z = 0; z <= b[order[i]] - a[order[i]]; z++) {
                if (doit(day + 1, i, z)) {
                    cur = z + doit(day + 1, i, z);
                    if (!ret || cur > ret) {
                        ret = cur;
                        move[day][1 + last][d] = i;
                        movea[day][1 + last][d] = z;
                    }
                }
            }
        }
    }
    return ret;
}

int main() {
    int i, j, tmp;
    long long z;
    CLR(seen,0);
    cin >> n >> m >> k;
    FOR (i,m) cin >> a[i] >> b[i] >> c[i];
    FOR (i,m) order[i] = i;
    FORN (i,m) FOR (j,i) if (c[order[j]] > c[order[i]]) {
        tmp = order[i];
        order[i] = order[j];
        order[j] = tmp;
    }
    if (!doit(0, -1, 0)) cout << "NO" << endl;
    else {
        cout << "YES" << endl;
        j = -1;
        z = 0;
        FOR (i,n) {
            tmp = move[i][1 + j][z];
            z = movea[i][1 + j][z];
            j = tmp;
            cout << (order[j] + 1) << " " << (a[order[j]] + z) << endl;
        }
    }
    return 0;
}

#pragma comment(linker, "/STACK:64000000")
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cassert>
#include <ctime>
#include <deque>

#define forn(i, n) for (int i = 0; i < int(n); i ++)
#define ford(i, n) for (int i = int(n) - 1; i >= 0; i --)
#define mp make_pair
#define pb push_back
#define x first
#define y second
#define pi 3.1415926535897932
#define ll long long
#define ld long double

using namespace std;

struct item{
    ll a, b;
    int c, num;
    item(){}
};

bool cmp(const item& a, const item& b){
    return a.c < b.c;
}

item a[110];
ll d[60][60][110];
pair<int, int> pr[60][60][110];
int n, m, k1;
vector <pair<int, ll> > ans;

void rest(int j1, int k1){
    cout << "YES" << endl;
    ford(i, n){
        int j2 = pr[i][j1][k1].first, k2 = pr[i][j1][k1].second;
        ans.pb(mp(j1, a[j1].a + k1));
        j1 = j2;
        k1 = k2;
    }
    ford(i, n)
        cout << a[ans[i].first].num + 1 << ' ' << ans[i].second << endl;
}

int main(){
#ifndef ONLINE_JUDGE
    freopen ("input.txt","rt",stdin);
    freopen ("output.txt","wt",stdout);
#endif
    cin >> n >> m >> k1;
    ll minn = -(1ll << 61ll);
    forn(i, m){
        ll t1, t2, t3;
        cin >> t1 >> t2 >> t3;
        item k;
        k.a = t1;
        k.b = t2;
        k.c = t3;
        k.num = i;
        a[i] = k;
    }
    sort(a, a + m, cmp);
    memset(pr, -1, sizeof(pr));
    forn(i, 60)
        forn(j, 60)
            forn(k, 110){
                d[i][j][k] = minn;
                pr[i][j][k] = mp(-1, -1);
    }
    ll s = 1;
    forn(i, 16)
        s *= 10ll;
    forn(i, m)
        forn(j, a[i].b - a[i].a + 1)
            d[0][i][j] = max(a[i].a + j, d[0][i][j]);
    forn(i, n - 1)
        for (int j = i; j < m; j ++)
            forn(k, a[j].b - a[j].a + 1)
                for (int z = j + 1; z < m; z ++){
                    if (a[j].c == a[z].c) continue;
                    ll l = k + a[j].a + k1;
                    if (l >= a[z].a && l <= a[z].b)
                        if (d[i + 1][z][l - a[z].a] < d[i][j][k] + l){
                            d[i + 1][z][l - a[z].a] = d[i][j][k] + l;
                            pr[i + 1][z][l - a[z].a] = mp(j, k);
                        }
                    if (k + a[j].a < s && 1ll * k1 * (k + a[j].a) >= a[z].a && k1 * (k + a[j].a) <= a[z].b){
                        ll l = k1 * (k + a[j].a);
                        if (d[i + 1][z][l - a[z].a] < d[i][j][k] + l){
                            d[i + 1][z][l - a[z].a] = d[i][j][k] + l;
                            pr[i + 1][z][l - a[z].a] = mp(j, k);
                        }
                    }
                }
    ll maxx = 0;
    int maxj = -1, maxk = -1;
    forn(j, m)
        forn(k, a[j].b - a[j].a + 1)
            if (maxx < d[n - 1][j][k]){
                maxx = d[n - 1][j][k];
                maxj = j;
                maxk = k;
            }
    if (maxj == -1){
        cout << "NO" << endl;
    } else rest(maxj, maxk);
    return 0;
}

#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long llong;
const int Max=55,MaxGap=105;
struct course{
    llong a,b;
    int id,c;
    bool in(const llong &x){return a<=x && x<=b;}
};
bool operator <(const course &x,const course &y){
    return x.c<y.c;
}
int pre[Max][Max][MaxGap][2];
llong dp[Max][Max][MaxGap];
course cr[Max];
int n,m,l;
inline void set(int *a,int x,int y){
    a[0]=x,a[1]=y;
}
void dfs(int i,int j,llong v){
    int k=v-cr[j].a;
    if(i>=n || pre[i][j][k][0]>=0) return;
    llong s1=v+l;
    llong s2=v*l;
    for(int t=m-1;t>j && cr[t].c>cr[j].c;--t){
        if(cr[t].in(s1) && dp[i][j][k]+s1>dp[i+1][t][s1-cr[t].a]){
            dfs(i+1,t,s1);
            pre[i+1][t][s1-cr[t].a][0]=j;
            pre[i+1][t][s1-cr[t].a][1]=k;
            dp[i+1][t][s1-cr[t].a]=dp[i][j][k]+s1;
        }
        if(cr[t].in(s2) && dp[i][j][k]+s2>dp[i+1][t][s2-cr[t].a]){
            dfs(i+1,t,s2);
            pre[i+1][t][s2-cr[t].a][0]=j;
            pre[i+1][t][s2-cr[t].a][1]=k;
            dp[i+1][t][s2-cr[t].a]=dp[i][j][k]+s2;
        }
    }
}
int main(){
    scanf("%d %d %d",&n,&m,&l);
    for(int i=0;i<m;++i){
        scanf("%I64d %I64d %d",&cr[i].a,&cr[i].b,&cr[i].c);
        cr[i].id=i+1;
    }
    sort(cr,cr+m);
    memset(pre,-1,sizeof(pre));
    memset(dp,0,sizeof(dp));
    for(int j=0;j<m;++j){
        for(int k=0,ek=cr[j].b-cr[j].a;k<=ek;++k){
            dfs(1,j,cr[j].a+k);
            set(pre[1][j][k],0,0);
        }
    }/*
    for(int i=1;i<n;++i){
        for(int j=i-1;j<m;++j){
            for(int k=0,ek=cr[j].b-cr[j].a;k<=ek;++k){
                if(pre[i][j][k][0]==-1) continue;
                llong s1=cr[j].a+k+l;
                llong s2=(cr[j].a+k)*l;
                for(int t=m-1;t>j && cr[t].c>cr[j].c;--t){
                    if(cr[t].in(s1)) set(pre[i+1][t][s1-cr[t].a],j,k);
                    if(cr[t].in(s2)) set(pre[i+1][t][s2-cr[t].a],j,k);
                }
            }
        }
    }*/
    int u=-1,v=-1;
    for(int j=n-1;j<m;++j){
        for(int k=0,ek=cr[j].b-cr[j].a;k<=ek;++k){
            if(pre[n][j][k][0]>=0 && (u==-1 || dp[n][u][v]<dp[n][j][k])){
                u=j,v=k;
            }
        }
    }
    if(u==-1) puts("NO");
    else {
        puts("YES");
        llong out[n+1][2],tmp;
        for(int i=n;i>=1;--i){
            out[i][0]=cr[u].id;
            out[i][1]=v+cr[u].a;
            tmp=u;
            u=pre[i][tmp][v][0];
            v=pre[i][tmp][v][1];
        }
        for(int i=1;i<=n;++i)
            printf("%I64d %I64d\n",out[i][0],out[i][1]);
    }
    return 0;
}

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

#define fi first
#define se second

const int maxN = 111;

struct Node {
    ll a, b;
    int c, ii;
    Node() {
        a = b = c = ii = 0;
    }
    Node(ll aa, ll bb, ll cc, int i) {
        a = aa;
        b = bb;
        c = cc;
        ii = i;
    }
};

bool operator < (Node a, Node b) {
    return a.c < b.c;
}
const int maxM = 55;
ll dp[maxM][maxM][maxN];
int p1[maxM][maxM][maxN];
ll p2[maxM][maxM][maxN];

vector<Node> A;
void upd(int i, int j, ll w, ll cur, int pp1, int pp2) {
    if (A[i].a <= cur && cur <= A[i].b) {
        if (dp[i][j][cur - A[i].a] < w + cur) {
            dp[i][j][cur - A[i].a] = w + cur;
            p1[i][j][cur - A[i].a] = pp1;
            p2[i][j][cur - A[i].a] = pp2;
        }
    }
}

int main() {
    #define task "a"
    //freopen(task".in", "r", stdin);
    //freopen(task".out", "w", stdout);
    int n, m;
    ll kk;
    cin >> n >> m >> kk;
    swap(n, m);
    A = vector<Node>(n + 1);
    for (int i = 1; i <= n; i++) {
        ll a, b;
        int c;
        cin >> a >> b >> c;
        A[i] = Node(a, b, c, i);
    }
    sort(A.begin(), A.end());
    for (int i = 1; i <= n; i++) {
        for (int l = i + 1; l <= n; l++) {
            if (A[i].c <= A[l].c) {
                for (int j = 0; j < m; j++) {
                    for (int k = 0; k <= A[i].b - A[i].a; k++) {
                        if (j == 1) {
                            dp[i][j][k] = A[i].a + k;
                        }
                        if (dp[i][j][k]) {
                            ll cur = A[i].a + k;
                            ll cur2 = cur + kk;
                            ll cur1 = cur * kk;
                            //cout << i << " " << j << " " << k << " "  << dp[i][j][k] << " " << endl;
                            upd(l, j + 1, dp[i][j][k], cur1, i, k);
                            upd(l, j + 1, dp[i][j][k], cur2, i, k);
                        }
                    }
                }
            }
        }
    }
    pair<ll, int> mx = {0, 0};
    for (int k = 0; k < maxN; k++) {
        mx = max({dp[n][m][k], k}, mx);
    }
    if (mx.fi == 0) {
        cout << "NO" << endl;
        return 0;
    }
    cout << "YES" << endl;
    vector<pair<int, ll> > ans;
    int k = mx.se;
    while(m) {
        ans.push_back({A[n].ii, A[n].a + k});
        int n2 = p1[n][m][k];
        int k2 = p2[n][m][k];
        n = n2;
        k = k2;
        m--;
    }
    reverse(ans.begin(), ans.end());
    for (auto x : ans) {
        cout << x.fi << " " << x.se << endl;
    }
    return 0;
}

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <streambuf>
#include <string>
#include <utility>
#include <vector>

#define r(i, n) rb(i, 0, n)
#define rb(i, b, n) rbc(i, b, n, <)
#define re(i, n) rbe(i, 0, n)
#define rbe(i, b, n) rbc(i, b, n, <=)
#define rbc(i, b, n, c) for(int i = (b); i c ((int)(n)); i++)

#define ri r(i, n)
#define rj r(j, n)
#define rk r(k, n)
#define rim r(i, m)
#define rjm r(j, m)
#define rkm r(k, m)

#define p(x) cout << x << " "
#define pl cout << endl
#define pn(x) cout << x << endl
#define pv(v) ri { p(v[i]); } pl;
#define pm(m) ri { rjm { p(m[i][j]); } pl; } pl;
#define pp(x) " "#x" " << x
#define ppn(x) pn(pp(x))
#define ppp(x) p(pp(x))

#define in(x) cin >> x

#define s(v) ((int) v.size())
#define all(v) v.begin(), v.end()

using namespace std;

typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<string> vs;

const int COL = 100 * 50 + 1;

  ull dp[51][COL][51] = {{{0}}};
  int what[51][COL][51];
  pair<int, int> from[51][COL][51];

int main() {
  ull n, m, K;
  cin >> n >> m >> K;
  vector< pair<int, pair< pair<ull, ull>, int> > > in;
  int curind = 1;
  map <ull, int> ind;
  r(i, m) {
    ull a, b, c;
    cin >> a >> b >> c;
    in.push_back(make_pair((int) (c - 1), make_pair(make_pair(a, b), i)));
    for (ull j = a; j <= b; j++) {
      if (!ind[j]) {
        ind[j] = curind;
        curind++;
      }
    }
  }

  sort(all(in));
  /*
  r(i, 3) {
    cout << in[i].first << ' ';
  }//*/

  ull res = 0;
  int bi, bj;
  r(i, s(in)) {
    //ppn(i);
    rb(len, 1, n + 1) {
      //ppn(len);
        ull a = in[i].second.first.first,
            b = in[i].second.first.second;
        //ppn(a); ppn(b);
        for (ull k = a; k <= b; k++) {
          //ppn(k);
          int index = ind[k];
          //ppn(index);
          if (len == 1 && index && k > dp[i][index][len]) {
            dp[i][index][len] = k;
            what[i][index][len] = in[i].first;
            from[i][index][len] = make_pair(-1, -1);
          }
          if (dp[i][index][n] > res) {
            res = dp[i][index][n];
            bi = i;
            bj = index;
          }
          if (len == 1) continue;
          if (k > K) {
            int other = ind[k - K];
            if (other) {
              for (int l = i - 1; l >= 0; l--) {
                if (dp[l][other][len - 1] != 0 &&
                    dp[l][other][len - 1] + k > dp[i][index][len]) {
                  dp[i][index][len] = dp[l][other][len - 1] + k;
                  what[i][index][len] = in[i].first;
                  from[i][index][len] = make_pair(l, other);
                }
              }
            }
          }
          if (k % K == 0) {
            int other = ind[k / K];
            if (other) {
              for (int l = i - 1; l >= 0; l--) {
                if (dp[l][other][len - 1] != 0 &&
                    dp[l][other][len - 1] + k > dp[i][index][len]) {
                  dp[i][index][len] = dp[l][other][len - 1] + k;
                  what[i][index][len] = in[i].first;
                  from[i][index][len] = make_pair(l, other);
                }
              }
            }
          }
          if (dp[i][index][n] > res) {
            res = dp[i][index][n];
            bi = i;
            bj = index;
          }
        }
    }
  }
  if (res != 0) {
    cout << "YES" << endl;
    stack<pair<int, ull> > st;
    int len = n;
    while (len > 0) {
      st.push(make_pair(what[bi][bj][len], dp[bi][bj][len]));
      int nbi = from[bi][bj][len].first;
      int nbj = from[bi][bj][len].second;
      len--;
      bi = nbi;
      bj = nbj;
    }
    ull last = 0;
    while (!st.empty()) {
      pair<int, ull> cur = st.top(); st.pop();
      cout << cur.first + 1 << ' ' << cur.second - last << endl;
      last = cur.second;
    }
  } else {
    cout << "NO" << endl;
  }
  //cout << res << endl;
  /*
r(len, 10) {
  ppn(len);
  r(i, 10) {
    r(j, 25) {
      cout << dp[i][j][len] << ' ';
    }
    cout << endl;
  }
  cout << endl;
}
cout << endl;//*/
}

#include <iostream>
#include <fstream>
#include <string>
#include <string.h>
#include <stdio.h>
#include <map>
#include <vector>
#include <queue>
#include <algorithm>
#include <set>
#include <math.h>
#include <bitset>

#define pb push_back

using namespace std;

typedef long long ll;
typedef pair<ll, ll> llll;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef pair<ii, ii> iiii;
typedef pair<double, int> di;
typedef vector<int> vi;
typedef vector<di> vdi;
typedef vector<vi> vvi;
typedef vector<ii> vii;
typedef vector<iiii> viiii;
typedef vector<vii> vvii;

#define INF 1000000000000000000ll

viiii subjs;
int n,m,k;

ll dp[51][51][51][101];

ll recur(int d, int s, int lasts, int lastk){
    if(d==n) return dp[d][s][lasts][lastk]=0;
    if(s==m) return dp[d][s][lasts][lastk]=-INF;
    dp[d][s][lasts][lastk]=recur(d, s+1, lasts, lastk);
    int len = subjs[s].second.second - subjs[s].second.first + 1;
    ll a;
    for(int i=0; i<len; i++){
        a = subjs[s].second.first + i;
        if(d==0 || (subjs[s].first.first>subjs[lasts].first.first &&
                        (a==k+(lastk+subjs[lasts].second.first)||a==k*(lastk+subjs[lasts].second.first))))
            dp[d][s][lasts][lastk]=max(dp[d][s][lasts][lastk], a+recur(d+1, s+1, s, i));

    }
    return dp[d][s][lasts][lastk];
}

void recons(int d, int s, int lasts, int lastk){
    if(d==n || s==m) return;
    ll ta = dp[d][s+1][lasts][lastk], tb=-INF, ti;

    int len = subjs[s].second.second - subjs[s].second.first + 1;
    ll a;
    for(int i=0; i<len; i++){
        a = subjs[s].second.first + i;
        if(d==0 || (subjs[s].first.first>subjs[lasts].first.first &&
                    (a==k+(lastk+subjs[lasts].second.first)||a==k*(lastk+subjs[lasts].second.first)))) {
            if(a+dp[d+1][s+1][s][i]>=tb){
                tb = a+dp[d+1][s+1][s][i];
                ti = i;
            }
        }
    }
    if(ta>=tb) recons(d, s+1, lasts, lastk);
    else {
        printf("%d %I64d\n", subjs[s].first.second, subjs[s].second.first + ti);
        recons(d+1, s+1, s, ti);
    }
}

int main() {
    int c;
    ll a,b;
    scanf("%d %d %d", &n, &m, &k);

    for(int i=0; i<m; i++){
        scanf("%I64d %I64d %d", &a, &b, &c);
        subjs.pb(iiii(ii(c,i+1),llll(a,b)));
    }
    sort(subjs.begin(), subjs.end());

    ll res = recur(0, 0, 0, 0);

    if(res<0) printf("NO\n");
    else{
        printf("YES\n");
        recons(0,0,0,0);

    }

    return 0;

}

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cassert>
#define LL long long
#define MAXN 51
using namespace std;

LL a[MAXN], b[MAXN], c[MAXN], id[MAXN], f[MAXN][MAXN][MAXN][2 * MAXN];
LL kq, k;
int n, m;

void sort(int i, int j) {
     int l = i, r = j;
     LL p = c[(l + r) >> 1];
     do {
         while (c[l] < p) l++;
         while (c[r] > p) r--;
         if (l <= r) {
            swap(c[l], c[r]);
            swap(a[l], a[r]);
            swap(b[l], b[r]);
            swap(id[l], id[r]);
            l++;
            r--;
         }
     }
     while (l <= r);
     if (l < j) sort(l, j);
     if (r > i) sort(i, r);
}

LL go(int at, int num, int lastPos, LL lastVal) {
    if (at == m - 1) {
       if (num == n - 1) return lastVal + a[lastPos];
       return -1000000000 * -1000000000;
    }
    LL &ans = f[at][num][lastPos][lastVal];
    if (ans != -1) return ans;
    ans = go(at + 1, num, lastPos, lastVal);
    if (num < n) {
       lastVal += a[lastPos];
       if (c[at + 1] > c[lastPos] && lastVal + k >= a[at + 1] && lastVal + k <= b[at + 1])
          ans = max(ans, go(at + 1, num + 1, at + 1, lastVal + k - a[at + 1]) + lastVal);
       if (c[at + 1] > c[lastPos] && lastVal * k >= a[at + 1] && lastVal * k <= b[at + 1])
          ans = max(ans, go(at + 1, num + 1, at + 1, lastVal * k - a[at + 1]) + lastVal);
    }
    if (ans < 0) ans = -1000000000 * -1000000000;
    return ans;
}

void truy(int at, int num, int lastPos, LL lastVal) {
     if (at == m - 1) {
        cout << id[lastPos] << ' ' << lastVal + a[lastPos] << endl;
        return;
     }
     LL ans = f[at][num][lastPos][lastVal];
     if (ans == go(at + 1, num, lastPos, lastVal))
        truy(at + 1, num, lastPos, lastVal);
     else {
          lastVal += a[lastPos];
          cout << id[lastPos] << ' ' << lastVal << endl;
          if (c[at + 1] > c[lastPos] && lastVal + k >= a[at + 1] && lastVal + k <= b[at + 1] && ans == go(at + 1, num + 1, at + 1, lastVal + k - a[at + 1]) + lastVal)
             truy(at + 1, num + 1, at + 1, lastVal + k - a[at + 1]);
          else
              truy(at + 1, num + 1, at + 1, lastVal * k - a[at + 1]);
     }
}

int main() {
    //freopen("c.inp", "r", stdin);
    //freopen("c.out", "w", stdout);

    cin >> n >> m >> k;
    for (int i = 0; i < m; i++) {
        cin >> a[i] >> b[i] >> c[i];
        id[i] = i + 1;
    }
    sort(0, m - 1);
    memset(f, -1, sizeof(f));
    kq = -1000000000 * -1000000000;
    for (int i = 0; i < m; i++)
        for (LL j = a[i]; j <= b[i]; j++)
            kq = max(kq, go(i, 0, i, j - a[i]));
    if (kq > 0) {
       cout << "YES" << endl;
       for (int i = 0; i < m; i++)
           for (LL j = a[i]; j <= b[i]; j++)
               if (kq == go(i, 0, i, j - a[i])) {
                  truy(i, 0, i, j - a[i]);
                  return 0;
               }
    }
    else cout << "NO" << endl;
}

/*
  - Author : Ahmed Araby
  - submitted by an Alien so it's not and ordinary code,
    don't till any one.
*/
#include <bits/stdc++.h>
/*#include <iostream>
#include <vector>
#include <map>
#include <unordered_map>
#include <queue>
#include <algorithm>
#include <math.h>*/
#define ll long long
#define pi pair<int , int>
#define pl pair<long long , long long>
using namespace std;

void run()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
}
struct info
{
    ll a , b;
    int i , c;
    info(){}
    info(ll aa, ll bb , int cc , int ii)
    {
        a=aa;
        b=bb;
        c=cc;
        i=ii;
    }
    bool operator <(info &obj) const
    {
        return c<obj.c;
    }
};
int n , m , k;
vector<info> v;
ll mem[55][55][105];
pair<int , int> path[55][55][105];
ll solve(int i , int p , int x)
{
    if(i==n)
        return 0;
    else if(p==m-1)
        return -LONG_LONG_MAX;
    ll &ret=mem[i][p][x];
    if(ret!=-1)
        return ret;
    ret=-LONG_LONG_MAX;
    // choose proper subject
    for(int j=p+1; j<m; j++)
    {
        ll a=v[j].a;
        ll b=v[j].b;
        // change from before
        ll prv=v[p].a;
        prv+=x; // How I went forward from previous "a"
        ll t1=-LONG_LONG_MAX , t2=-LONG_LONG_MAX , tok=0;
        // previous choice determine what I'm going to do
        // check if I can put thin number of exercises
        if(prv+k>=a && prv+k<=b)
            t1=solve(i+1, j , (k+prv)-a)+(k+prv);  // take care of sign when we get X
        if(prv*k>=a && prv*k<=b)
            t2=solve(i+1 , j , (k*prv)-a)+(k*prv);
        if(t1>=t2)
            tok=k+prv;
        else{
            t1=t2;
            tok=k*prv;
        }
        if(t1>ret)
        {
            ret=t1;
            // mark you choice  , store index of subject and added value
            int dif=tok-a;
            path[i][p][x]={j , dif};
        }
    }
    return ret;
}
void build(int i, int p , int x)  // theses parameters belong to  the previous choice
{
    while(i!=n)
    {
        int index=path[i][p][x].first;
        int dif=path[i][p][x].second;
        cout<<v[index].i<<" "<<v[index].a+dif<< endl;
        // move forward
        p=index;
        x=dif;
        i++;
    }
    return ;
}
int main()
{
	run();
	//freopen("input.txt" ,"r" , stdin);
	//freopen("output.txt" , "w", stdout);
	cin>>n>>m>>k;
	for(int i=0; i<=n; i++)
        for(int j=0; j<=m; j++)
        for(int q=0; q<=100; q++)
        mem[i][j][q]=-1;

	for(int i=0; i<m; i++)
    {
        ll a , b;
        int c;
        cin>>a>>b>>c;
        v.push_back(info(a, b , c , i+1));
    }
    sort(v.begin() , v.end());
    int p;
    ll c , maxi=-LONG_LONG_MAX;
    for(int i=0; i<m; i++)  // choose the first day
    {
        ll a=v[i].a;
        ll b=v[i].b;
        ll tmp=0;
        for(; a<=b; a++)
        {
            int x=a-v[i].a;
            tmp=solve(1 ,i ,x)+a;
            if(tmp>maxi)
            {
                maxi=tmp;
                p=i;
                c=a;
            }
        }
    }
    if(maxi>0)
    {
        cout<<"YES"<<endl;
        //cout<<maxi<< endl;
        cout<<v[p].i<<" "<<c<< endl;
        // build
        int x=c-v[p].a;
        build(1 , p , x); // same parameter that we get our solution with
    }
    else
        cout<<"NO"<< endl;
	return 0;
}

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

using namespace std;
#define MP make_pair
typedef long long LL;
const int MAXN = 100;
const int MAXM = 200;

int n,m,k;

struct NODE {
	LL a,b,c;
	int id;
}nd[MAXM];

bool cmp(const NODE& a, const NODE& b) {
	if (a.c == b.c)
		return a.id < b.id;
	else
	return a.c < b.c;
}

LL dp[53][53][103];
pair<int,int> back[53][53][103];

void doDP() {
	for (int i = 0; i < m; i++) {
		for(LL j = nd[i].a; j <= nd[i].b; j++) {
			dp[1][i][j - nd[i].a] = j;
			back[1][i][j - nd[i].a ] = MP<int,int>(0,0);
		}
	}

	for (int i = 1; i < n; i++) {
		for (int j = 0; j < m; j++) {
			for (int l = 0; l <= (int)(nd[j].b - nd[j].a); l++) if (dp[i][j][l]) {
				LL x,y;
				x = nd[j].a + l + k;
				y = (nd[j].a + l )*k;
				for (int p = j + 1; p <= m; p++) if (nd[p].c > nd[j].c) {
					if (x >= nd[p].a && x <= nd[p].b) {
						if (dp[i+1][p][x - nd[p].a]  <  dp[i][j][l] + x) {
							dp[i+1][p][x - nd[p].a] = dp[i][j][l] + x;
							back[i+1][p][x - nd[p].a] = MP<int,int>(j,l);
						//	cout<<"update + i = "<<i <<" j = "<<j<<" p = "<<p<<"ans = "<<dp[i+1][p][x - nd[p].a]<<endl;
						}
					}

					if (y >= nd[p].a && y <= nd[p].b) {
						if (dp[i+1][p][y - nd[p].a] < dp[i][j][l] + y) {
							dp[i+1][p][y - nd[p].a] = dp[i][j][l] + y;
							back[i+1][p][y - nd[p].a] = MP<int,int>(j,l);
					//		cout<<"update * i = "<<i <<" p = "<<p<<" abs(y) = "<<y - nd[p].a<<"ans = "<<dp[i+1][p][y - nd[p].a]<<endl;
						}
					}
				}
			}
		}
	}

	int rpi,rpj;
	rpi = rpj = -1;
	LL ans = 0;

	for (int i = 0; i < m; i++)
		for (int j = 0; j <= (int)(nd[i].b - nd[i].a); j++) {
		//	cout<<i<<" "<<j<< " "<<dp[n][i][j]<<endl;
				if (dp[n][i][j] > ans) {
					//cout<<ans<<endl;
					ans = dp[n][i][j];
					rpi = i;
					rpj = j;
				}
		}

	if (ans == 0)
		printf("NO\n");
	else {
		puts("YES");
		vector<pair<int,int> > res;
		for (int i = n ; i > 0; i--) {
			int j,l;
			res.push_back(MP<int,int>(rpi,rpj));
			j = back[i][rpi][rpj].first;
			l = back[i][rpi][rpj].second;
			rpi = j;
			rpj = l;
		}
		reverse(res.begin(),res.end());
		for (int i = 0; i < res.size(); i++) {
			cout<<nd[res[i].first].id+1<<" "<<res[i].second + nd[res[i].first].a<<endl;
		}
	}
}

void solve() {
	scanf("%d %d %d",&n,&m,&k);
	for (int i = 0; i < m; i++) {
		scanf("%d %d %d",&nd[i].a,&nd[i].b,&nd[i].c);
		nd[i].id = i;
	}
	sort(nd,nd+m,cmp);
	doDP();
}

int main() {
	int T = 1;
	while (T--) {
		solve();
	}
return 0;
}

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

const int maxn = 105;
struct pp {
    long long a, b, c; int index;
    bool operator < (const pp &a) const {
        return c < a.c;
    }
} sub[maxn];
pair<int, int> parent[maxn][maxn][maxn];
long long dp[maxn][maxn][maxn];
long long k;
int n, m;

//d = day, s = sub, o = offset, type 1 = +, type 2 = x
long long dfs(int d, int s, int o) {
    long long val = sub[s].a + o;
    /*if (o > b[i] - a[i] || o < 0) {
        return -2;
    } */
    if (dp[d][s][o] != -1) {
        return dp[d][s][o];
    } if (d == n) {
        return dp[d][s][o] = val;
    }
    long long ans = -2; int nxt = -2, type = -2;
    for (int ns = 0; ns <= m; ns++) {
        if (sub[ns].c < sub[s].c) {
            continue;
        }
        long long v1 = val + k;
        if (sub[ns].a <= v1 && v1 <= sub[ns].b) {
            long long sum = dfs(d + 1, ns, v1 - sub[ns].a);
            if (sum < 0) continue;
            sum += val;
            if (sum > ans) {
                ans = sum;
                nxt = ns;
                type = 1;
            }
        }
        long long v2 = val * k;
        if (sub[ns].a <= v2 && v2 <= sub[ns].b) {
            long long sum = dfs(d + 1, ns, v2 - sub[ns].a);
            if (sum < 0) continue;
            sum += val;
            if (sum > ans) {
                ans = sum;
                nxt = ns;
                type = 2;
            }
        }
    }
    parent[d][s][o] = {nxt, type};
    return dp[d][s][o] = ans;
}

int main() {
    freopen("input.in", "r", stdin);
    cin >> n >> m >> k;
    for (int i = 1; i <= m; i++) {
        cin >> sub[i].a >> sub[i].b >> sub[i].c;
        sub[i].index = i;
    }
    sort(sub + 1, sub + n + 1);
    memset(dp, -1, sizeof(dp));

    long long ans = -2; int d = -1, nm = -1;
    for (int i = m; i >= 1; i--) {
        for (long long j = sub[i].a; j <= sub[i].b; j++) {
            long long diff = j - sub[i].a;
            if (ans < dfs(1, i, diff)) {
                ans = dfs(1, i, diff);
                nm = i;
                d = diff;
            }
        }
    }

    if (ans < 0) {
        cout << "NO" << endl;
    } else {
        cout << "YES" << endl;
        for (int day = 1; day <= n; day++) {
            long long om = nm;
            long long v = sub[om].a + d;
            cout << sub[om].index << ' ' << v << endl;
            int t = parent[day][om][d].second;
            nm = parent[day][om][d].first;
            if (t == 1) {
                d = (v + k) - sub[nm].a;
            } else {
                d = (v * k) - sub[nm].a;
            }
        }
    }
}

#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <utility>
using namespace std;
#define N 55
#define D 105
#define DEBUG 0
long long c[N];
struct cmp{
   bool operator() (long long a, long long b){
      return c[a]<c[b];
   }
};
int main(){
   long long n,m,k;
   long long a[N],b[N],ind[N];
   long long dp[N][N][D][3];
   cin>>n>>m>>k;
   for(long long i=0; i<m; ++i){
      cin>>a[i]>>b[i]>>c[i];
      ind[i]=i;
   }
   sort(ind,ind+m,cmp());
   if(DEBUG)for(long long i=0; i<m; i++)cout<<ind[i]<<" ";
   if(DEBUG)cout<<endl;
   memset(dp,-1,sizeof(dp));
   for(long long i=0; i<m; i++){
      for(long long p=0; p<=(long long)(b[ind[i]]-a[ind[i]]); p++){
         dp[i][1][p][0]=a[ind[i]]+p;
         dp[i][1][p][1]=i;
         dp[i][1][p][2]=p;
      }
   }
   long long maxVal,maxInd,maxH;
   for(long long i=1; i<m; i++){
      for(long long j=2; j<=min(n,i+1); j++){
         for(long long p=0; p<=(long long)(b[ind[i]]-a[ind[i]]); p++){

            maxVal=-1;
            maxInd=-1;
            maxH=-1;
            long long cur=a[ind[i]]+p,req1=cur-k,req2=((cur%k)?-1:(cur/k));
            for(long long s=i-1; s>=0; s--){
               if(DEBUG)cout<<"hello "<<s<<" "<<req1<<" "<<req2<<" "<<a[ind[s]]<<" "<<b[ind[s]]<<" "<<dp[s][j-1][(long long)(req1-a[ind[s]])][0]<<endl;
               if(DEBUG)cout<<"MAXVAL "<<maxVal<<endl;
               if(req1>0 && req1<=b[ind[s]] && req1>=a[ind[s]]){
                  if(dp[s][j-1][(long long)(req1-a[ind[s]])][0]>maxVal){
                     maxInd=s;
                     maxVal=dp[s][j-1][(long long)(req1-a[ind[s]])][0];
                     maxH=(req1-a[ind[s]]);
                  }
               }
               if(req2>0 && req2<=b[ind[s]] && req2>=a[ind[s]]){
                  if(dp[s][j-1][(long long)(req2-a[ind[s]])][0]>maxVal){
                     maxInd=s;
                     maxVal=dp[s][j-1][(long long)(req2-a[ind[s]])][0];
                     maxH=(req2-a[ind[s]]);
                  }
               }
            }
            if(DEBUG)cout<<"FUCK "<<maxVal<<" "<<maxH<<endl;
            dp[i][j][p][0]=(maxVal!=-1)?(maxVal+cur):maxVal;
            dp[i][j][p][1]=maxInd;
            dp[i][j][p][2]=maxH;
            if(DEBUG)cout<<i<<" "<<j<<" "<<a[ind[i]]+p<<" "<<dp[i][j][p][0]<<endl;
         }
      }
   }
   maxVal=-1;maxInd=-1;maxH=-1;
   for(long long i=0; i<m; i++){
      for(long long j=0; j<=(b[ind[i]]-a[ind[i]]); j++)
         if(maxVal<dp[i][n][j][0]){
            maxVal=dp[i][n][j][0];
            maxInd=i;
            maxH=j;
         }
   }
   if(maxVal!=-1){
      long long j=n;
      vector< pair<long long,long long> > v;
      vector< pair<long long,long long> >::iterator it;
      cout<<"YES"<<endl;
      while(true){
         if(DEBUG)cout<<"mind mh "<<maxInd<<" "<<maxH<<endl;
         v.push_back(make_pair(ind[maxInd]+1,a[ind[maxInd]]+maxH));
         long long temp=maxInd;
         maxInd=dp[temp][j][maxH][1];
         maxH=dp[temp][j][maxH][2];
         j--;
         if(j==0)break;
      }

      for(it=--v.end(),j=n;j>0;it--,j--){
         cout<<(*it).first<<" "<<(*it).second<<endl;
      }
   }
   else{
      cout<<"NO"<<endl;
   }
   return 0;
}

#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define int long long
using namespace std;
const int oo = 1e18;

pair <pair<int, int>, pair<int, int>> a[105];

int dp[51][51][51][101];
int trace[51][51][51][101];
int n,m,k;

int solve(int num, int i, int j, int x)
{
    if (num == n) return 0;
    if ((n - num) > (m - i + 1)) return -oo;
    int &res = dp[num][i][j][x];
    int &t = trace[num][i][j][x];
    if (res != -oo) return res;
    res++;
    //======================== skip
    if (res < solve(num, i + 1, j, x))
    {
        res = dp[num][i + 1][j][x];
        t = 0;
    }
    //==`====================== num == 0
    if (num == 0)
    {
        for (int val = a[i].se.fi; val <= a[i].se.se; val++)
        {
            int xx = val - a[i].se.fi;
            if (res < solve(num + 1, i + 1, i, xx) + val)
            {
                res = dp[num + 1][i + 1][i][xx] + val;
                t = val;
            }
        }
    }
    if (j != 0 && a[i].fi.fi != a[j].fi.fi)
    {
        //======================== accept x + k
        x += a[j].se.fi;
        if (a[i].se.fi <= (x + k) && (x + k) <= a[i].se.se)
        {
            int xx = x + k - a[i].se.fi;
            if (res < solve(num + 1, i + 1, i, xx) + (x + k))
            {
                res = dp[num + 1][i + 1][i][xx] + (x + k);
                t = (x + k);
            }
        }
        //======================== accept x + a[j].se.fi * k
        if (a[i].se.fi <= (x * k) && (x * k) <= a[i].se.se)
        {
            int xx = (x * k) - a[i].se.fi;
            if (res < solve(num + 1, i + 1, i, xx) + (x * k))
            {
                res = dp[num + 1][i + 1][i][xx] + (x * k);
                t = (x * k);
            }
        }
    }
    return res;
}

signed main()
{
    //freopen("file.inp","r",stdin);
    cin >> n >> m >> k;
    for (int i = 1; i <= m; i++)
    {
        cin >> a[i].se.fi >> a[i].se.se >> a[i].fi.fi;
        a[i].fi.se = i;
    }
    sort(a + 1,a + m + 1);
    for (int num = 0; num <= n; num++)
    for (int i = 1; i <= m; i++)
    for (int j = 0; j <= m; j++)
    for (int x = 0; x <= 100; x++)
    {
        dp[num][i][j][x] = -oo;
    }
    if (solve(0, 1, 0, 0) <= 0) return cout << "NO\n", 0;
    cout << "YES\n";
    int num = 0;
    int j = 0;
    int x = 0;
    for (int i = 1; i <= m; i++)
    {
        int t = trace[num][i][j][x];
        if (t != 0)
        {
            cout << a[i].fi.se << " " << t << "\n";
            x = t - a[i].se.fi;
            j = i;
            num++;
        }
    }
    return 0;
}

#pragma comment(linker, "/stack:64000000")

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cstring>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <algorithm>
#include <sstream>
#include <cassert>

using namespace std;

#define REP(i, n) for (int (i) = 0; (i) < (n); (i)++)
#define FOR(i, a, b) for (int (i) = (a); (i) < (b); (i)++)
#define sz(v) (int)(v).size()
#define all(v) (v).begin(), (v).end()
#define mp make_pair
#define pb push_back

typedef long long LL;

struct Lesson {
	long long L, R;
	int D, N;

	Lesson() {}

	Lesson(long long L, long long R, int D, int N): L(L), R(R), D(D), N(N) {}

	bool operator<(const Lesson& lesson) const {
		return D < lesson.D;
	}
};

struct Prev {
	int num, exc;

	Prev() {}

	Prev(int num, int exc): num(num), exc(exc) {}
};

long long dp[55][55][111];
Prev pr[55][55][111];

int main() {
#ifndef ONLINE_JUDGE
	#define NAME "test"
	freopen(NAME".in", "r", stdin);
	freopen(NAME".out", "w", stdout);
#endif
	int need, m, k;
	scanf("%d %d %d", &need, &m, &k);
	vector <Lesson> v(m);
	REP(i, m) {
		long long L, R;
		int D;
		cin >> L >> R >> D;
		v[i] = Lesson(L, R, D, i + 1);
	}
	sort(all(v));
	memset(dp, 0, sizeof(dp));
	memset(pr, -1, sizeof(pr));
	for (int done = 1; done < need; done++) {
		for (int cur = 0; cur < m; cur++) {
			for (int exc = 0; exc <= (int)(v[cur].R - v[cur].L); exc++) {
				long long did = v[cur].L + exc;
				if (done == 1) {
					dp[done][cur][exc] = did;
				}
				if (dp[done][cur][exc] == 0) continue;
				for (int nxt = cur + 1; nxt < m; nxt++) {
					if (v[nxt].D > v[cur].D) {
						for (long long j = v[nxt].L; j <= v[nxt].R; j++) {
							if (j % did == 0 && j / did == k || j - did == k) {
								int t = (int)(j - v[nxt].L);
								if (dp[done + 1][nxt][t] < dp[done][cur][exc] + j) {
									dp[done + 1][nxt][t] = dp[done][cur][exc] + j;
									pr[done + 1][nxt][t] = Prev(cur, exc);
								}
							}
						}
					}
				}
			}
		}
	}
	int B = -1, C = -1;
	REP(i, 111) REP(j, 111) {
		if (dp[need][i][j] > 0 && (B == -1 || dp[need][i][j] > dp[need][B][C])) {
			B = i, C = j;
		}
	}
	if (B != -1) {
		vector <pair <int, long long> > res(need);
		for (int A = need; A >= 1; A--) {
			res[A - 1] = mp(v[B].N, v[B].L + C);
			int b = B, c = C;
			B = pr[A][b][c].num;
			C = pr[A][b][c].exc;
		}
		cout << "YES\n";
		REP(i, sz(res)) {
			cout << res[i].first << " " << res[i].second << "\n";
		}
	} else {
		cout << "NO\n";
	}
    return 0;
}

#include <bits/stdc++.h>
#define endl "\n"
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using tl = tuple<ll, ll, int, int>;

constexpr int N = 1e2+5;

ll a, b, c, ans = 0, val, cur;
int n, m, k, pred;
bool f = true;
map<ll, ll> dp[N][N];
tl arr[N];
vector<pii> pr;

void Solve()
{
  cin >> n >> m >> k;
  for (int i = 1; i <= m; i++)
  {
    cin >> a >> b >> c;
    arr[i] = make_tuple(a, b, c, i);
  }
  sort(arr+1, arr+m+1, [&](const auto& a, const auto& b){return get<2>(a)<get<2>(b);});
  for (int i = 1; i <= m;)
  {
    pred = i-1;
    f = true;
    while(i<=m&&get<2>(arr[i])==get<2>(arr[i+1])||f)
    {
      auto [a, b, c, id] = arr[i];
      for (ll j = a; j <= b; j++)
        dp[i][1][j] = max(j, dp[pred][1][j]);
      for (int j = 2; j <= min(i, n); j++)
      {
        for (auto [ck, ans] : dp[pred][j-1])
        {
          if (a<=ck+k && b>=ck+k)
            dp[i][j][ck+k] = max(dp[i][j][ck+k], ans+ck+k);
          if (a<=ck*k && b>=ck*k)
            dp[i][j][k*ck] = max(dp[i][j][k*ck], ans+ck*k);
        }
      }
      if (!f)
        for (int j = 1; j <= min(i, n); j++)
          for (auto [ck, ans] : dp[i][j])
              dp[i+1][j][ck] = max(dp[i][j][ck], ans);
      f = false;
      i++;
    }
    for (int j = 1; j <= min(i-1, n); j++)
      for (auto [ck, ans] : dp[i-1][j])
          dp[i][j][ck] = max(dp[i][j][ck], ans);
  }
  for (auto [el, an] : dp[m][n])
    if (ans<an)
    {
      ans = an;
      val = el;
    }
  if (ans==0)
  {
    cout << "NO";
    return;
  }
  cur = m;
  while (ans)
  {
    auto [a, b, c, id] = arr[cur];
    if (a<=val && val<=b)
    {
      ans-=val;
      pr.emplace_back(id, val);
      if (dp[cur-1][n-pr.size()][val-k]==ans)
        val-=k;
      else val/=k;
    }
    cur--;
  }
  cout << "YES" << endl;
  reverse(pr.begin(), pr.end());
  for (auto [i, v] : pr)
    cout << i << " " << v << endl;
}

int main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  Solve();
  return 0;
}

#pragma comment (linker, "/STACK:74000000")
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <memory>
#include <cctype>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
using namespace std;

#define FOR(i,a,b) for(int (i) = (a); (i) < (b); ++(i))
#define RFOR(i,a,b) for(int (i) = (a)-1; (i) >= (b); --(i))
#define CLEAR(a) memset((a),0,sizeof(a))
#define INF 1000000000
#define PB push_back
#define ALL(c) (c).begin(), (c).end()
#define pi 2*acos(0.0)
#define SQR(a) (a)*(a)
#define MP make_pair

typedef long long Int;

struct node
{
        Int a, b;
        int i, c;
};

struct parent
{
        int i, j, k;
        Int d;
};

int n, m, k;
vector <node> A;
Int DP[51][51][101];
parent P[51][51][101];

bool Cmp(node a, node b)
{
        return a.c < b.c;
}

int main()
{
        cin >> n >> m >> k;
        FOR (i,0,m)
        {
                node t;
                Int a, b;
                int c;
                cin >> a >> b >> c;
                t.a = a;
                t.b = b;
                t.c = c;
                t.i = i;
                A.PB(t);
        }
        sort(ALL(A), Cmp);
        FOR (i,0,51)
                FOR (j,0,51)
                        FOR (k,0,101)
                        {
                                DP[i][j][k] = -100000000000000000ll;
                                P[i][j][k].i = -1;
                        }

        FOR (i,0,A.size())
                for (Int j = A[i].a; j<=A[i].b; j++)
                {
                        P[1][i][j-A[i].a].i = 0;
                        P[1][i][j-A[i].a].j = i;
                        P[1][i][j-A[i].a].d = j;
                        DP[1][i][j-A[i].a] = j;
                }

        FOR (i,1,n)
                FOR (j,0,A.size())
                {
                        for (Int l = A[j].a; l <= A[j].b; l++)
                        {
                                int d = l-A[j].a;
                                if (DP[i][j][d] < 0)
                                        continue;
                                FOR (t,j+1,A.size())
                                        if (A[t].c > A[j].c)
                                        {
                                                if (l+k >= A[t].a && l+k <= A[t].b)
                                                        if (DP[i+1][t][l+k-A[t].a] < DP[i][j][d]+l+k)
                                                        {
                                                                DP[i+1][t][l+k-A[t].a] = DP[i][j][d]+l+k;
                                                                P[i+1][t][l+k-A[t].a].i = i;
                                                                P[i+1][t][l+k-A[t].a].j = j;
                                                                P[i+1][t][l+k-A[t].a].k = d;
                                                                P[i+1][t][l+k-A[t].a].d = l+k;

                                                        }
                                                if (l*(Int)k >= A[t].a && l*(Int)k <= A[t].b)
                                                        if (DP[i+1][t][l*(Int)k-A[t].a] < DP[i][j][d]+l*(Int)k)
                                                        {
                                                                DP[i+1][t][l*(Int)k-A[t].a] = DP[i][j][d]+l*(Int)k;
                                                                P[i+1][t][l*(Int)k-A[t].a].i = i;
                                                                P[i+1][t][l*(Int)k-A[t].a].j = j;
                                                                P[i+1][t][l*(Int)k-A[t].a].k = d;
                                                                P[i+1][t][l*(Int)k-A[t].a].d = l*(Int)k;
                                                        }
                                        }
                        }
                }

        Int r = -100000000000000000ll;
        int x, y, z;

        FOR (i,0,A.size())
                FOR (j,0,101)
                        if (DP[n][i][j] > r)
                        {
                                r = DP[n][i][j];
                                x = n;
                                y = i;
                                z = j;
                        }
        if (r < 0)
        {
                cout << "NO" << endl;
                return 0;
        }

        cout << "YES" << endl;
        vector < pair<int, Int> > R;
        while (x > 0)
        {
                R.PB(MP(y+1, P[x][y][z].d));
                int xx, yy, zz;
                xx = P[x][y][z].i;
                yy = P[x][y][z].j;
                zz = P[x][y][z].k;
                x = xx;
                y = yy;
                z = zz;
        }
        reverse(ALL(R));
        FOR (i,0,R.size())
                cout << R[i].first << ' ' << R[i].second << endl;

        return 0;
}

#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <map>
#include <stack>
#include <cmath>
#include <queue>
#include <map>
#include <string>
#include <cstdlib>
#include <bitset>
#include <set>
#include <list>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long LL;
typedef pair<int, LL> P;
typedef unsigned int uint;
#define sign puts("SIGN");
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define rep(i, j, k) for (int i = int(j); i <= int(k); ++ i)
#define dwn(i, j, k) for (int i = int(j); i >= int(k); -- i)
#define lson rt << 1, l, m
#define rson rt << 1 | 1, m + 1, r
const int MOD = 1e9 + 7;
int n, m, K;
struct Node {
	LL a, b, c, id;
	void read() {
		cin >> a >> b >> c;
	}
}a[51];
int cmp(Node a, Node b) {
	return a.c < b.c;
}
LL dp[51][51][101];
P f[51][51][101];
int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);

	cin >> n >> m >> K;
	rep(i, 1, m) a[i].read(), a[i].id = i;
	sort(a + 1, a + 1 + m, cmp);

	memset(dp, -1, sizeof(dp));
	rep(i, 1, m)
	for (long long j = a[i].a; j <= a[i].b; ++ j)
		dp[1][i][j - a[i].a] = j;

	P p;
	LL ans = -1;
	rep(i, 2, n) {
		rep(j, 1, m) {
			for (long long k = a[j].a; k <= a[j].b; ++ k) {
				rep(l, 1, j - 1)
				if (a[l].c < a[j].c) {
					LL t1;
					t1 = k - (LL)K;
					if (t1 >= a[l].a && t1 <= a[l].b
						&& dp[i - 1][l][t1 - a[l].a] != -1) {
						if (dp[i][j][k - a[j].a] < dp[i - 1][l][t1 - a[l].a] + k) {
							dp[i][j][k - a[j].a] = dp[i - 1][l][t1 - a[l].a] + k;
							f[i][j][k - a[j].a] = P(l, t1 - a[l].a);
						}
					}
					if (k % K) continue;
					t1 = k / (LL)K;
					if (t1 >= a[l].a && t1 <= a[l].b
						&& dp[i - 1][l][t1 - a[l].a] != -1) {
						if (dp[i][j][k - a[j].a] < dp[i - 1][l][t1 - a[l].a] + k) {
							dp[i][j][k - a[j].a] = dp[i - 1][l][t1 - a[l].a] + k;
							f[i][j][k - a[j].a] = P(l, t1 - a[l].a);
						}
					}
				}
				if (i == n) {
					if (dp[i][j][k - a[j].a] > ans) {
						ans = dp[i][j][k - a[j].a];
						p = mp(j, k - a[j].a);
					}
				}
  			}
		}
	}
	// cout << ans << endl;
	if (ans == -1) {
		puts("NO");
		return 0;
	}
	else {
		// cout << ans << endl;
		puts("YES");
		vector<P> vec;
		vec.pb(p);
		for (int i = n; i >= 2; -- i) {
			p = f[i][p.F][p.S];
			vec.pb(p);
			// cout << p.F << " " << p.S << endl;
		}
		reverse(begin(vec), end(vec));
		for (auto i: vec) {
			cout << a[i.F].id << " " << a[i.F].a + (LL)i.S << endl;
 		}
	}

}

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <string>
#include <cstring>
#include <cstdlib>
#include <set>
#include <map>
using namespace std;
#define forn(i, n) for (int i = 0; i < (int)(n); i++)
#define fore(i, b, e) for (int i = (int)(b); i <= (int)(e); i++)
typedef pair<int, int> pii;
typedef long long ll;
typedef long double ld;
#define se second
#define fi first
#define pb push_back
#define mp make_pair
const int maxn = 105;
const ll inf = (ll)1e17;

struct les
{
	int a, b, c, i;
	bool operator< (const les& q) const
	{
		return c < q.c;
	}
	les(){};
	les(int A, int B, int C, int I):a(A),b(B),c(C),i(I){};
};

ll d[55][55][110];
int anc[55][55][110];
int n, m, u;
les a[maxn];

bool cmp(les a, les b)
{
	return a.i < b.i;
}
void scan()
{
	cin >> n >> m >> u;
	forn(i, m)
	{
		cin >> a[i].a >> a[i].b >> a[i].c;
		a[i].i = i;
	}
	sort(a, a+m);
}
void dp()
{
	forn(i, 55) forn(j, 55) forn(p, 110)
		d[i][j][p] = -1;
	forn(j, m) forn(k, a[j].b - a[j].a + 1)
	{
		//cout << 0 << " " << j << " " << k << endl;
		d[0][j][k] = a[j].a + k;
	}

	forn(i, n) forn(j, m) forn(k, a[j].b - a[j].a + 1)
	{
		if (d[i][j][k] == -1)
			continue;
		ll curc = a[j].a + k, nxtc;
		//cout << i << " " << j << " " << k << "   " << curc << endl;

		forn(I, 2)
		{
			if (I == 0)
				nxtc = curc + u;
			else
				nxtc = curc * u;
			//cout << nxtc << endl;
			for (int p = j + 1; p < m; p++) if (a[j].c < a[p].c)
				if (nxtc >= a[p].a && nxtc <= a[p].b)
					if (d[i+1][p][nxtc - a[p].a] < d[i][j][k] + nxtc)
					{
						//cout << i+1 << " " << p << " " << nxtc - a[p].a << endl;
						d[i+1][p][nxtc - a[p].a] = d[i][j][k] + nxtc,
						anc[i+1][p][nxtc - a[p].a] = (p-j) * (I?1:(-1));
					}
		}
		//cout << endl;
	}
}
void getans()
{
	ll mx = -1, J=0, K=9;
	forn(j, m) forn(k, a[j].b - a[j].a + 1)
		if (d[n-1][j][k] != -1)
			mx = max(mx, d[n-1][j][k]), J=j, K=k;

	if (mx == -1)
	{
		cout << "NO\n";
		return;
	}
	cout << "YES\n";
	vector<pair<int, pair<int, ll> > > res; //i, num, amt
	for (int i = n-1; i >= 0; i--)
	{
		res.pb(mp(a[J].c, mp(a[J].i+1, K+a[J].a)));
		ll curc = K + a[J].a, nxtc;
		//cout << J << " " << K << " " << curc << endl;
		if (anc[i][J][K] > 0)
			nxtc = curc / u;
		else
			nxtc = curc - u;
		if (i)
		{
			J -= abs(anc[i][J][K]);
			K = nxtc - a[J].a;
		}
	}
	sort(res.begin(), res.end());
	forn(i, res.size())
		cout << res[i].se.fi << " " << res[i].se.se << endl;
}

int main()
{
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);

	scan();
	dp();
	getans();

	return 0;
}

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
#include<utility>
#include<map>
using namespace std;
#define max(x,y) (((x)>(y))?(x):(y))
#define min(x,y) (((x)<(y))?(x):(y))
#define abs(x) (((x)>0)?(x):(-(x)))
#define INF (1<<29)
#define eps (1e-8)
#define ll long long

struct map_node{
    ll ex;
    int id,c,day;

    bool operator<(const map_node &d)const{
        if (id!=d.id) return id<d.id;
        else if (c!=d.c) return c<d.c;
        else if (day!=d.day) return day<d.day;
        else return ex<d.ex;
    }

    map_node(){}
    map_node(int id,int c,ll ex,int day):id(id),c(c),ex(ex),day(day){}
};

struct node{
    ll ex;
    int id,c,day;

    bool operator<(const node &d)const{
        return ex<d.ex;
    }

    node(){}
    node(int id,int c,ll ex,int day):id(id),c(c),ex(ex),day(day){}
    node(map_node d){
        id=d.id; c=d.c; day=d.day; ex=d.ex;
    }
};

map_node con(node d){
    map_node tmp;
    tmp.id=d.id; tmp.c=d.c; tmp.day=d.day; tmp.ex=d.ex;
    return tmp;
}

struct node2{
    ll a,b; int c,id;

    void input(int x){
        scanf("%I64d%I64d%d",&a,&b,&c);
        id=x;
    }

    bool operator<(const node2 &d)const{
        return c<d.c;
    }
} sub[105];

int n,m,k;
map<map_node,map_node> par;
map<map_node,bool> visit;
priority_queue<node> q;

int main(){
    scanf("%d%d%d",&n,&m,&k);
    for (int i=0;i<m;++i){
        sub[i].input(i);
        for (ll j=sub[i].a;j<=sub[i].b;++j) q.push(node(i,sub[i].c,j,1));
    }

    map_node ans; ans.id=-1;
    while (!q.empty()){
        node tmp=q.top(); q.pop();
        map_node tmp2=con(tmp);
        if (visit.find(tmp2)==visit.end()){
            visit[tmp2]=1;
            //printf("%d\n",tmp.day);
            if (tmp.day==n && ans.id==-1){ans=tmp2; break;}
            for (int i=0;i<m;++i){
                if (sub[i].c>tmp.c){
                    ll x=tmp.ex+(ll)k;
                    if (x<0ll) continue;
                    if (x>=sub[i].a && x<=sub[i].b){
                        map_node next=map_node(i,sub[i].c,x,tmp.day+1);
                        if (visit.find(next)==visit.end()){
                            par[next]=tmp2; q.push(next);
                        }
                    }
                    x=tmp.ex*(ll)k;
                    if (x<0ll) continue;
                    if (x>=sub[i].a && x<=sub[i].b){
                        map_node next=map_node(i,sub[i].c,x,tmp.day+1);
                        if (visit.find(next)==visit.end()){
                            par[next]=tmp2; q.push(next);
                        }
                    }
                }
            }
        }
    }

    if (ans.id==-1) puts("NO");
    else{
        puts("YES");

        map_node tans[105];
        for (int i=n-1;i>=0;--i){
            tans[i]=ans;
            if (i!=0) ans=par[ans];
        }
        ll sum=0ll;
        for (int i=0;i<n;++i){
            printf("%d %I64d\n",tans[i].id+1,tans[i].ex-sum);
        }
    }
    return 0;
}

/*
 * Author: FreeIdea
 * Created Time:  2011/10/14 23:46:03
 * File Name: C.cpp
 */
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <vector>
using namespace std;
const int maxint = -1u>>1;
template <class T> bool get_max(T& a, const T &b) {return b > a? a = b, 1: 0;}
template <class T> bool get_min(T& a, const T &b) {return b < a? a = b, 1: 0;}
typedef long long lint;
const int maxn = 50 + 2;
lint f[maxn][maxn][102];  // �� i��β�ĳ���Ϊj������
int n, m, d;

struct Sub {
    lint a, b, c;
    int idx;
    bool operator < (const Sub &A) const {
        return c < A.c;
    }
}sub[maxn];

struct Sol {
    int a, b, c;
    Sol() {
    }
    Sol(int _a, int _b, int _c) : a(_a), b(_b), c(_c) {}
}sol[maxn][maxn][102], final;

void output(Sol s) {
    if(s.a == 0)  return;
    output(sol[s.a][s.b][s.c]);
    printf("%d", sub[s.a].idx);
    lint tmp = sub[s.a].a + s.c;
    printf(" %I64d\n", tmp);
}

int main() {
    scanf("%d%d%d", &m, &n, &d);
    for(int i = 1; i <= n; i++) {
        scanf("%I64d%I64d%I64d", &sub[i].a, &sub[i].b, &sub[i].c);
        sub[i].idx = i;
    }
    sort(sub + 1, sub + 1 + n);
    memset(f, -1, sizeof(f));
    for(int i = 0; i <= 100; i++) {
        f[0][0][i] = 0;
    }
    bool found = false;
    for(int i = 0; i <= n; i++) {
        for(int j = 0; j <= min(i, m); j++) {
            for(int k = 0; k <= 100; k++) {
                if(f[i][j][k] == -1) continue;
                if(j == m) {
                    found = true;
                    final = Sol(i, j, k);
                }
                for(int l = i + 1; l <= n; l++) {
                    if(sub[l].c > sub[i].c) {
                        lint tmp = sub[i].a + k + d;
                        if(sub[l].a <= tmp && tmp <= sub[l].b) {
                            if(get_max(f[l][j + 1][tmp - sub[l].a], f[i][j][k] + tmp)) {
                                sol[l][j + 1][tmp - sub[l].a] = Sol(i, j, k);
                            }
                        }

                        tmp = (sub[i].a + k) * d;
                        if(sub[l].a <= tmp && tmp <= sub[l].b) {
                            if(get_max(f[l][j + 1][tmp - sub[l].a], f[i][j][k] + tmp)) {
                                sol[l][j + 1][tmp - sub[l].a] = Sol(i, j, k);
                            }
                        }
                    }
                }
            }
        }
    }
    if(!found) {
        printf("NO\n");
    }
    else {
        printf("YES\n");
        output(final);
    }
    return 0;
}

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <utility>
#include <stack>
#include <cstring>
#include <math.h>
#include<cstdio>
#include<deque>
#include<sstream>
#define pb push_back
#define mem(a,b) memset(a,b,sizeof(a))
#define sz(v) ((int)v.size())
#define rep(i,m) for(int i=0;i<(int)(m);i++)
#define oo ((int) 1e9)
#define mp make_pair
#define pi 2 * acos(0);
#define eps 1e-6

using namespace std;
long long a, b;
int c, n, m, k;
map <pair <long long, pair<int, int > >, long long > dp;
vector < pair <pair< int, long long > , pair < long long, int > > > p;
bool f = 0;
vector<pair<int, long long > > v;
long long calc(int ind, long long  pre, int cnt){
    if (cnt == n){
        f = 1;
        return 0;
    }

    if (dp.find(mp(pre, mp(ind, cnt))) != dp.end())
        return dp[mp(pre, mp(ind, cnt))];
    long long r = -2e14;

    for (int i = ind; i < m; i++){

        if ((p[i].first.second <= pre * k && p[i].second.first >= k * pre)
            || (p[i].first.second <= pre + k && p[i].second.first >= k + pre)){

            if (p[i].first.second <= pre * k && p[i].second.first >= k * pre)
                r = max(r, calc(i + 1, pre * k, cnt + 1) + (pre*k));

            if (p[i].first.second <= pre + k && p[i].second.first >= k + pre)
                r = max(r, calc(i + 1, pre + k, cnt + 1) + (pre + k));
        }
        else
            return -2e14;
    }
    return dp[mp(pre, mp(ind, cnt))]=r;
}
void pth(int ind, long long  pre, int cnt){
    if (cnt == n)
        return ;

    long long r = -2e14 ,rr = 2e14,gg=0;
    int x;
    for (int i = ind; i < m; i++){
        if (p[i].first.second <= pre * k && p[i].second.first >= k * pre){
            rr = calc(i + 1, pre * k, cnt + 1) + (pre*k);
            if (rr > r){
                r = rr;
                gg = pre*k;
                x = i;
            }
        }

        if (p[i].first.second <= pre + k && p[i].second.first >= k + pre){
            rr = calc(i + 1, pre + k, cnt + 1) + (pre + k);
            if (rr > r){
                r = rr;
                gg = pre+k;
                x = i;
            }
        }

    }
    v.push_back(mp(p[x].second.second, gg));
    pth(x + 1, gg, cnt + 1);
}
int  main()
{
    //freopen("test.txt" , "rt" , stdin);
    //freopen ("myfile.txt","w",stdout);

    cin >> n >> m >> k;
    for (int i = 0; i < m; i++){
        cin >> a >> b >> c;
        p.push_back(mp(mp(c,a), mp(b,i+1)));
    }
    sort(p.begin(), p.end());
    long long res = 0,yy;
    int xx;
    for (int i = m - n; i >= 0; i--){
        for (long long j = p[i].second.first; j >= p[i].first.second; j--){
            if (res <= calc(i + 1, j, 1) + j)
                res = calc(i + 1, j, 1) + j, xx = i, yy = j;;
            //cout << j << endl;
        }
    }
    if (!f)
        puts("NO");
    else
    {
        puts("YES");
        v.push_back(mp(p[xx].second.second, yy));
        pth(xx + 1, yy, 1);
        for (int i = 0; i < v.size(); i++)
            cout << v[i].first << " " << v[i].second << endl;

    }
    //cout << res << endl;
    return 0;
}

#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <map>
#include <set>
#include <string.h>
using namespace std;

#define sz(x) int((x).size())
#define inf 1000000000
#define pb push_back
#define FOR(i,a,b) for(i=a;i<=b;i++)
#define REP(i,a,b) for(i=a;i<b;i++)
#define pi 2*acos(0.0)
#define sqr(a) (a)*(a)
#define mp make_pair
typedef vector<int> vint;
typedef long long ll;
#define y1 kjdh

int n,m,k,i,q[101];

int c,day,pr1[101][101][101],pr2[101][101][101],num[101];
vector< pair<int,ll> >uns;
int I;

ll l[101],r[101],U,dp[101][101][101],qq,J,j;

int main()
{
   // freopen("input.txt","r",stdin);

	cin>>n>>m>>k;
	FOR(i,1,m)cin>>l[i]>>r[i]>>q[i],num[i]=i;

	FOR(i,1,m)
	FOR(j,i+1,m)
	if(q[i]>q[j])
	{
		swap(l[i],l[j]);
		swap(r[i],r[j]);
		swap(q[i],q[j]);
		swap(num[i],num[j]);
	}

	memset(dp,-1,sizeof(dp));
	U=-1;

	FOR(i,1,m)
	FOR(j,l[i],r[i])
	{
		dp[i][1][j-l[i]]=j;
		FOR(c,1,i-1)
			if(q[i]>q[c])
			{
				FOR(qq,l[c],r[c])
					if(qq+k==j || qq*k==j)
						FOR(day,2,n)
						if(dp[c][day-1][qq-l[c]]+j>dp[i][day][j-l[i]])
						{
						dp[i][day][j-l[i]]=max(dp[i][day][j-l[i]],dp[c][day-1][qq-l[c]]+j);

						pr1[i][day][j-l[i]]=c;
						pr2[i][day][j-l[i]]=qq-l[c];

						}
			}
		U=max(U,dp[i][n][j-l[i]]);
	}

	if(U==-1){cout<<"NO"<<endl;return 0;}

	cout<<"YES"<<endl;

	uns.clear();

	FOR(i,1,m)
	FOR(j,l[i],r[i])
	if(dp[i][n][j-l[i]]==U)
	{
		j-=l[i];
		for(;;)
		{

			uns.pb(mp(num[i],j+l[i]));//cout<<num[i]<<" "<<j+l[i]<<endl;
			if(n==1)break;

			I=i;J=j;
			i=pr1[I][n][J];
			j=pr2[I][n][J];
			n--;

		}
		for(i=sz(uns)-1;i>=0;i--)cout<<uns[i].first<<" "<<uns[i].second<<endl;
		return 0;
	}

}

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<cmath>
#include<algorithm>
using namespace std;

#define F(i,n) for(i=0;i<n;i++)
#define FF(i,n) for(i=1;i<=n;i++)
#define LL long long
#define inf (1LL<<62)

#define update(a, b, c, d) if(a<b) {(a) = (b); (c)  = (d);}

struct node{
    LL a, b, c;
}b[100];

LL a[55][55][105];  //[day][course][exe]
int n, m, k;
int p[55][55][105]; // course*1000 + exe
int ansu[55], ansv[55];

int main(){
    int i, j, y, nj;
    LL x,nx;
    while(scanf("%d%d%d",&n,&m,&k)!=EOF){
        F(i,m) scanf("%lld%lld%lld",&b[i].a, &b[i].b, &b[i].c);
        F(i,n+1)F(j,m+1)F(x,101) a[i][j][x] = -inf;
        F(j, m)F(x, b[j].b-b[j].a+1) a[1][j][x] = b[j].a + x;
        for(i=1;i<n;i++)F(j,m)F(x, b[j].b-b[j].a+1)if(a[i][j][x]>0){
            F(nj, m)if(b[nj].c>b[j].c){
                nx = b[j].a + x + k - b[nj].a;
                if(nx>=0&&nx<b[nj].b-b[nj].a+1)
                      update(a[i+1][nj][nx], a[i][j][x]+b[nj].a+x,  p[i+1][nj][nx], j*1000 + x);
                nx = (b[j].a + x)*k - b[nj].a;
                if(nx>=0&&nx<b[nj].b-b[nj].a+1)
                      update(a[i+1][nj][nx], a[i][j][x]+b[nj].a+x,  p[i+1][nj][nx], j*1000 + x);
            }
        }
        LL ans = 0;
        int u, v;
        F(j,m)F(x, b[j].b-b[j].a+1)if(ans<a[n][j][x]) ans = a[n][j][x], u = j, v = x;
        if(ans==0) printf("NO\n");
        else {
            printf("YES\n");
            for(i=n;i>=1;i--){
                ansu[i] = u, ansv[i] = v;
                x = p[i][u][v]/1000, y = p[i][u][v]%1000;
                u = x, v = y;
            }
            FF(i,n) printf("%d %lld\n", ansu[i]+1, ansv[i]+b[ansu[i]].a);
        }
    }
    return 0;
}

#include <vector>
#include <cstring>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <queue>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <stdlib.h>
#include <ctime>
#include <string>
#include <complex>
#include <memory.h>
#ifdef __ASD__
#include <windows.h>
#endif
using namespace std;
#define all(a) a.begin(),a.end()
#define forn(i,n) for(int i=0;i<(n);++i)
#define fornn(i,n) for(i=0;i<(n);++i)
#define lng long long
#define SQ(a) ((a)*(a))
#define forv(i,v) for(int i=0;i<(int)v.size();++i)
#define mp make_pair
#define pb push_back
#define ABS(a) ((a)<0?-(a):(a))
#define iinf 1000000000
#define linf SQ(1LL*iinf)
#define left asdleft
#define prev asdprev
#define hash asdhash
#define eps 1e-8
#define y1 asdy1
#define y2 asdy2
#define EQ(a,b) abs((a)-(b))<eps
#define PI 3.1415926535897932384626433832795
#define DEG2RAD PI/180
#define ulng unsigned lng
const double dinf = 1e200;
void mytimer(string task){
#ifdef __ASD__
    static LARGE_INTEGER prev;  LARGE_INTEGER cur,freq; QueryPerformanceCounter(&cur);  QueryPerformanceFrequency(&freq);   if(task!="")        cout<<task<<" took "<<(cur.QuadPart-prev.QuadPart)*1.0/freq.QuadPart<<endl; prev=cur;
#endif
}
void ext(int c){
#ifdef __ASD__
    exit(c);
#endif
}

int gcd(int a,int b){
    return a?gcd(b%a,a):b;
}

struct item{
    lng a;
    int l;
    int c;
    void read(){
        lng t;
        cin>>a>>t>>c;
        l=t-a+1;
    }
    bool operator<(const item &b)const{
        return c<b.c;
    }
};

int M,N,K;
item items[60];

lng dp[60][60][110];
pair<int,int> prev[60][60][110];

#define taska "four"
int main(){
#ifdef __ASD__
    freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
#else
    //freopen(taska".in","r",stdin);freopen(taska".out","w",stdout);
#endif

    //ios_base::sync_with_stdio(false);

    cin>>M>>N>>K;
    forn(i,N)
        items[i].read();
    sort(items,items+N);

    forn(n,N){
        forn(v,items[n].l)
            dp[n][1][v]=items[n].a+v;
        for(int m=2;m<=M;++m){
            forn(v,items[n].l){
                forn(nn,n){
                    if(items[nn].c==items[n].c)
                        continue;

                    lng val;

                    val=items[n].a+v-K;
                    val-=items[nn].a;
                    if(val>=0&&val<items[nn].l){
                        int vv=(int)val;
                        if(dp[nn][m-1][vv]>0){
                            lng t=dp[nn][m-1][vv]+items[n].a+v;
                            if(t>dp[n][m][v]){
                                dp[n][m][v]=t;
                                prev[n][m][v]=mp(nn,vv);
                            }
                        }
                    }

                    lng tt=items[n].a+v;
                    if(tt%K)
                        continue;

                    val=tt/K;
                    val-=items[nn].a;
                    if(val>=0&&val<items[nn].l){
                        int vv=(int)val;
                        if(dp[nn][m-1][vv]>0){
                            lng t=dp[nn][m-1][vv]+items[n].a+v;
                            if(t>dp[n][m][v]){
                                dp[n][m][v]=t;
                                prev[n][m][v]=mp(nn,vv);
                            }
                        }
                    }
                }
            }
        }
    }

    pair<lng,pair<int,int> > res(0,mp(0,0));

    forn(n,N)
        forn(s,items[n].l)
            res=max(res,mp(dp[n][M][s],mp(n,s)));

    if(res.first==0){
        cout<<"NO";
        return 0;
    }

    cout<<"YES"<<endl;

    pair<int,int> p=res.second;
    vector<pair<int,lng> > resv;
    int m=M;
    while(true){
        resv.pb(mp(p.first+1,items[p.first].a+p.second));
        if(m==1)
            break;
        p=prev[p.first][m][p.second];
        --m;
    }

    reverse(all(resv));

    forv(i,resv)
        cout<<resv[i].first<<' '<<resv[i].second<<'\n';

    return 0;
}

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

using namespace std;

struct lesson
{
    int c;
    long long a, b;
    int num;
    void read()
    {
        cin >> a >> b >> c;
    }
    void print()
    {
        cout << a << " " << b << " " << c << "\n";
    }
};

long long dp[102][102][102];
int last_p[102][102][102];
int last_work_p[102][102][102];
lesson les[102];
int n;

bool comp(lesson h, lesson k)
{
    return h.c < k.c;
}

void print_ans(int m, int p, int j)
{
    if (p >= 2)
    {
        if (last_p[m][p][j] <= 0)
        {
            cout << "NO";
            exit(0);
        }
        print_ans(last_p[m][p][j], p - 1, last_work_p[m][p][j]);
    }
    if (p == 1)
    {
        cout << "YES\n";
    }
    cout << les[m].num << " " << les[m].a + j << "\n";
}

int main()
{
    int m, k;
    cin >> n >> m >> k;
    for (int i = 1; i <= m; i++)
    {
        les[i].read();
        les[i].num = i;
    }
    sort(les + 1, les + m + 1, comp);
    for (int i = 1; i <= m; i++)
    {
        for (int j = 0; j <= n; j++)
        {
            for (int z = 0; z <= les[i].b - les[i].a; z++)
            {
                dp[i][j][z] = 0;
            }
        }
    }
    for (int i = 1; i <= m; i++)
    {
        for (int j = 0; j <= les[i].b - les[i].a; j++)
        {
            dp[i][1][j] = les[i].a + j;
        }
    }

    for (int i = 2; i <= m; i++)
    {
        for (int j = 2; j <= n; j++)
        {
            for (int z = 0; z <= les[i].b - les[i].a; z++)
            {
                for (int r = 1; r <= i - 1; r++)
                {
                    if ((les[i].a + z >= les[r].a + k) && (les[i].a + z <= les[r].b + k))
                    {
                        long long f = dp[r][j - 1][((les[i].a + z) - k) - les[r].a] + les[i].a + z;
                        if (f > dp[i][j][z] && les[i].c > les[r].c)
                        {
                            dp[i][j][z] = f;
                            last_p[i][j][z] = r;
                            last_work_p[i][j][z] = ((les[i].a + z) - k) - les[r].a;
                        }
                    }
                    if ((les[i].a + z >= les[r].a * k) && (les[i].a + z <= les[r].b * k) && ((les[i].a + z) % k == 0))
                    {
                        long long f = dp[r][j - 1][((les[i].a + z) / k) - les[r].a] + les[i].a + z;
                        if (f > dp[i][j][z] && les[i].c > les[r].c)
                        {
                            dp[i][j][z] = f;
                            last_p[i][j][z] = r;
                            last_work_p[i][j][z] = ((les[i].a + z) / k) - les[r].a;
                        }
                    }
                }
            }
        }
    }
    long long mx = 0;
    int last = 1;
    int last_work = 0;
    for (int i = n; i <= m; i++)
    {
        for (int j = 0; j <= les[i].b - les[i].a; j++)
        {
            if (mx < dp[i][n][j])
            {
                mx = dp[i][n][j];
                last = i;
                last_work = j;
            }
        }
    }
    print_ans(last, n, last_work);
    return 0;
}

#include <algorithm>
#include <iostream>
#include <cstdio>
#include <stack>

using namespace std;

struct SUB
{
    int a, b, c; int num;

    SUB(){}
    SUB(int _a, int _b, int _c, int _num)
    {
        a = _a; b = _b; c = _c; num = _num;
    }

    bool operator<(const SUB &p)const
    {
        return c < p.c;
    }
}sub[100];

int N, M, K;

int day[100][200], sum[100][200];
int fx[100][200], fy[100][200];
int mxx, mxy, mxs;

stack<int> Ax, Ay;

int main()
{
    scanf("%d %d %d", &M, &N, &K);

    for(int Ni = 0; Ni < N; Ni++)
    {
        scanf("%d %d %d", &sub[Ni].a, &sub[Ni].b, &sub[Ni].c);
        sub[Ni].num = Ni+1;
    }

    sort(sub, sub+N);

    for(int Ni = 0; Ni < N; Ni++)
    {
        for(int i = 0; sub[Ni].a+i <= sub[Ni].b; i++)
        {
            day[Ni][i] = 1; sum[Ni][i] = sub[Ni].a+i;
            fx[Ni][i] = Ni; fy[Ni][i] = i;

            int p = sub[Ni].a+i;

            for(int Nj = 0; Nj < Ni; Nj++)
            {
                int t;

                t = p-K;
                if( sub[Nj].a <= t && t <= sub[Nj].b )
                {
                    t -= sub[Nj].a;

                    if( day[Ni][i] < day[Nj][t]+1 || day[Ni][i] == day[Nj][t]+1 && sum[Ni][i] < sum[Nj][t]+p )
                    {
                        day[Ni][i] = day[Nj][t]+1; sum[Ni][i] = sum[Nj][t]+p;
                        fx[Ni][i] = Nj; fy[Ni][i] = t;
                    }
                }

                if( p%K == 0 )
                {
                    t = p/K;
                    if( sub[Nj].a <= t && t <= sub[Nj].b )
                    {
                        t -= sub[Nj].a;

                        if( day[Ni][i] < day[Nj][t]+1 || day[Ni][i] == day[Nj][t]+1 && sum[Ni][i] < sum[Nj][t]+p )
                        {
                            day[Ni][i] = day[Nj][t]+1; sum[Ni][i] = sum[Nj][t]+p;
                            fx[Ni][i] = Nj; fy[Ni][i] = t;
                        }
                    }
                }
            }
        }
    }

    for(int Ni = 0; Ni < N; Ni++)
    {
        for(int i = 0; sub[Ni].a+i <= sub[Ni].b; i++)
        {
            if( day[Ni][i] < M ) continue;

            int p = 0; int t = 0;
            int px = Ni, py = i, tx, ty;

            while(1)
            {
                p += sub[px].a+py;
                tx = fx[px][py]; ty = fy[px][py];
                px = tx; py = ty; t++;
                if( t == M ) break;
            }

            if( mxs < p ){ mxs = p; mxx = Ni; mxy = i; }
        }
    }

    if( mxs == 0 ){ puts("NO"); return 0; }

    int px = mxx, py = mxy, tx, ty; int t = 0;

    while(1)
    {
        Ax.push(sub[px].num); Ay.push(sub[px].a+py);

        tx = fx[px][py]; ty = fy[px][py];
        px = tx; py = ty; t++;
        if( t == M ) break;
    }

    puts("YES");

    while( !Ax.empty() && !Ay.empty() )
    {
        printf("%d %d\n", Ax.top(), Ay.top());
        Ax.pop(); Ay.pop();
    }

    return 0;
}

#include <bits/stdc++.h>
#define endl "\n"
using namespace std;
using ll = long long;
using pii = pair<ll, ll>;
using tl = tuple<ll, ll, int, int>;

constexpr int N = 1e2+5;

ll a, b, c, ans = 0, val, cur;
int n, m, k, pred, cr;
bool f = true;
map<ll, pair<ll, ll>> dp[N][N];
tl arr[N];
vector<pii> pr;

void Solve()
{
  cin >> n >> m >> k;
  for (int i = 1; i <= m; i++)
  {
    cin >> a >> b >> c;
    arr[i] = make_tuple(a, b, c, i);
  }
  sort(arr+1, arr+m+1, [&](const auto& a, const auto& b){return get<2>(a)<get<2>(b);});
  for (int i = 1; i <= m;)
  {
    pred = i-1;
    cr = i;
    f = true;
    while(cr<=m&&get<2>(arr[i])==get<2>(arr[cr]))
    {
      auto [a, b, c, id] = arr[cr];
      for (ll j = a; j <= b; j++)
        dp[i][1][j] = {j, 0};
      for (int j = 2; j <= min(i, n); j++)
      {
        for (auto [ck, ans] : dp[pred][j-1])
        {
          if (a<=ck+k && b>=ck+k)
            dp[cr][j][ck+k] = {ans.first+ck+k, ck};
          if (a<=ck*k && b>=ck*k)
            dp[cr][j][ck*k] = {ans.first+ck*k, ck};
        }
      }
      for (int j = 1; j <= min(cr, n); j++)
        for (auto [ck, ans] : dp[cr-1][j])
          if (dp[cr][j][ck].first<=ans.first)
            dp[cr][j][ck] = ans;
      cr++;
    }
    i = cr;
  }
  for (auto [el, an] : dp[m][n])
    if (ans<=an.first)
    {
      ans = an.first;
      val = el;
    }
  if (ans==0)
  {
    cout << "NO";
    return;
  }
  cur = m;
  while (pr.size()<n)
  {
    auto [a, b, c, id] = arr[cur];
    if (a<=val && val<=b)
    {
      ans-=val;
      pr.emplace_back(id, val);
      val = dp[cur][n-pr.size()+1][val].second;
      while(get<2>(arr[cur-1])==get<2>(arr[cur]))
        cur--;
    }
  }
  cout << "YES" << endl;
  reverse(pr.begin(), pr.end());
  for (auto [i, v] : pr)
    cout << i << " " << v << endl;
}

int main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  Solve();
  return 0;
}

#include <cstdio>
#include <cstring>
#include <algorithm>

#define MAXN 55
#define MAXD 105

using namespace std;

int n,m,k;
struct homework{
    long long l,r;
    int c,loc;
    bool operator < (const homework &a)const{
        return c < a.c;
    }
}   h[MAXN];
long long f[MAXN][MAXN][MAXD];
int g[MAXN][MAXN][MAXD];
int gg[MAXN][MAXN][MAXD];

void printans(int x,int y,int z){
    if (x>1){
        printans(x-1,g[x][y][z],gg[x][y][z]);
    }
    printf("%d %I64d\n",h[y].loc,h[x].l+(long long)z);
}

int main(){
  //  freopen("c.in","r",stdin);
    int i,j,x,y,z,ji;
    long long p,q,ans,jp;
    while (scanf("%d%d%I64d",&n,&m,&k)!=EOF){
        for (i=1;i<=m;i++){
            scanf("%I64d%I64d%d",&h[i].l,&h[i].r,&h[i].c);
            h[i].loc = i;
        }
        sort(h+1,h+m+1);
        memset(f,-1,sizeof(f));
        for (i=1;i<=m;i++)
            for (p=h[i].l;p<=h[i].r;p++)
                f[1][i][p-h[1].l] = p;
        for (i=1;i<n;i++){
            for (j=1;j<=m;j++)
                for (p=h[j].l;p<=h[j].r;p++)
                    if (f[i][j][p-h[j].l]>=0){
                        for (x=j+1;x<=m;x++)
                            if (h[j].c<h[x].c){
                                q = p + k;
                                if (h[x].l<=q && q<=h[x].r){
                                    if (f[i+1][x][q-h[x].l]<f[i][j][p-h[j].l]+q){
                                        f[i+1][x][q-h[x].l] = f[i][j][p-h[j].l] + q;
                                        g[i+1][x][q-h[x].l] = j;
                                        gg[i+1][x][q-h[x].l] = p - h[j].l;
                                    }
                                }
                                q = p * k;
                                if (h[x].l<=q && q<=h[x].r){
                                    if (f[i+1][x][q-h[x].l]<f[i][j][p-h[j].l]+q){
                                        f[i+1][x][q-h[x].l] = f[i][j][p-h[j].l] + q;
                                        g[i+1][x][q-h[x].l] = j;
                                        gg[i+1][x][q-h[x].l] = p - h[j].l;
                                    }
                                }
                            }
                    }
        }
        ans = -1;
        for (i=1;i<=m;i++)
            for (p=h[i].l;p<=h[i].r;p++){
                if (f[n][i][p-h[i].l]>ans){
                    ans = f[n][i][p-h[i].l];
                    ji = i;
                    jp = p - h[i].l;
                }
            }
        //printf("f = %I64d\n",f[1][2][7]);
       // printf("ans = %I64d %d %d\n",ans,ji,jp);
        if (ans<0) printf("NO\n");
            else{
                printf("YES\n");
                printans(n,ji,jp);
            }
    }
    return 0;
}

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <utility>
#include <vector>
#define LL long long
const int LMT=55;
using namespace std;
struct cricu
{
    LL a,b;
    int tag,c;
    bool operator<(const cricu &x)const
    {
        return c<x.c;
    }
}cri[LMT];
struct line
{
    bool wid[LMT<<1];
    int len;
}dp[LMT][LMT];
pair<int,int> pre[LMT][LMT][LMT];
pair<int,LL>ans;
LL k,sum[LMT][LMT][LMT];
vector<pair<int,LL> >vec;
int n,m;
int main()
{
    LL have=0;
    int d;
    ans=make_pair(-1,0);
    cin>>n>>m>>k;
    for(int i=1;i<=m;i++)
    {
      cin>>cri[i].a>>cri[i].b>>cri[i].c;
      cri[i].tag=i;
    }
    sort(cri+1,cri+1+m);
    for(int i=1;i<=m;i++)
    {
        for(int j=1;j<=n;j++)
          dp[i][j].len=(int)(cri[i].b-cri[i].a+1);
        memset(dp[i][1].wid,1,sizeof(dp[i][1].wid));
        for(int j=0;j<dp[i][1].len;j++)
            sum[i][1][j]=cri[i].a+j;
    }
    for(int i=1;i<=m;i++)
    {
        for(int j=2;j<=i&&j<=n;j++)
          for(int ii=i-1;ii>=j-1;ii--)
          if(cri[ii].c<cri[i].c)
            for(int t=0;t<dp[ii][j-1].len;t++)
            if(dp[ii][j-1].wid[t])
            {
               if((cri[ii].a+t)+k>=cri[i].a&&cri[ii].a+t+k<=cri[i].b)
               {
                   d=(int)((cri[ii].a+t)+k-cri[i].a);
                   dp[i][j].wid[d]|=1;
                   if(sum[i][j][d]<sum[ii][j-1][t]+(cri[ii].a+t)+k)
                   {
                       sum[i][j][d]=sum[ii][j-1][t]+(cri[ii].a+t)+k;
                       pre[i][j][d].first=ii;
                       pre[i][j][d].second=t;
                   }
               }
               if((cri[ii].a+t)*k>=cri[i].a&&(cri[ii].a+t)*k<=cri[i].b)
               {
                   d=(int)((cri[ii].a+t)*k-cri[i].a);
                   dp[i][j].wid[d]|=1;
                   if(sum[i][j][d]<sum[ii][j-1][t]+(cri[ii].a+t)*k)
                   {
                       sum[i][j][d]=sum[ii][j-1][t]+(cri[ii].a+t)*k;
                       pre[i][j][d].first=ii;
                       pre[i][j][d].second=t;
                   }
               }
            }
            for(int t=dp[i][n].len-1;t>=0;t--)
            if(dp[i][n].wid[t])
            {
                if(have<sum[i][n][t])
                {
                    have=sum[i][n][t];
                    ans.second=t;
                    ans.first=i;
                }
            }
    }
    if(ans.first==-1)printf("NO\n");
    else
    {
        int have=n;
        pair<int,LL> x=ans;
        printf("YES\n");
        while(x.first&&have)
        {
            vec.push_back(make_pair(x.first,x.second+cri[x.first].a));
            x=pre[x.first][have][x.second];
            have--;
        }
        while(!vec.empty())
        {
            cout<<cri[vec.back().first].tag<<" "<<vec.back().second<<endl;
            vec.pop_back();
        }
    }
    return 0;
}

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<string>
#include<map>
#include<vector>
#include<algorithm>
#define NMAX 110
#define MMAX 110
#define DMAX 110
using namespace std;

typedef long long LL;

struct S{
        LL a,b,c,df;
        void eat(){
                scanf("%lld %lld %lld",&a,&b,&c);
                df = b-a+1;
        }
};

struct F{
        LL n,m,d;
        void out(){
                printf("(%lld,%lld,%lld)\n",n,m,d);
        }
};

LL nn,mm,kk;
LL dp[NMAX][MMAX][DMAX];
F f[NMAX][MMAX][DMAX];
S sj[MMAX];

bool scmp(S a,S b){
        return a.c < b.c;
}

int main(){
        scanf("%lld %lld %lld",&nn,&mm,&kk);
        for(LL i=0;i<mm;i++){
                sj[i].eat();
        }
        sort(sj,sj+mm,scmp);
        for(LL i=0;i<mm;i++){
                for(LL j=0;j<sj[i].df;j++){
                        dp[1][i][j] = sj[i].a+j;
                        f[1][i][j].n = -1;
                }
        }
        for(LL i=2;i<=nn;i++){
                for(LL j=0;j<mm;j++){
                        for(LL jl=0;jl<j;jl++){
                                for(LL x = 0;x<sj[j].df;x++){
                                        LL sta[3],st=0;
                                        LL &cur = dp[i][j][x];
                                        LL profit = x+sj[j].a;
                                        LL m = x+sj[j].a;
                                        if(m>=kk){
                                                sta[st++] = m-kk;
                                        }
                                        if(m%kk==0 && m){
                                                sta[st++] = m/kk;
                                        }
                                        for(LL t=0;t<st;t++){
                                                LL ml = sta[t];
                                                if(ml<sj[jl].a)continue;
                                                if(ml>sj[jl].b)continue;
                                                LL kl = ml-sj[jl].a;
                                                LL &next = dp[i-1][jl][kl];
                                                if(next == 0)continue;
                                                if(cur < profit + next){
                                                        cur = profit + next;
                                                        F ft;
                                                        ft.n = i-1;ft.m = jl;ft.d = kl;
                                                        f[i][j][x] = ft;
                                                }
                                        }
                                }
                        }
                        /*
                        for(LL x = 0;x<sj[j].df;x++){
                                printf("dp[%lld][%lld][%lld]=%lld\n",i,j,x,dp[i][j][x]);
                        }*/
                }
        }
        LL maxd = 0;
        F mf;
        for(LL j=0;j<mm;j++){
                for(LL x=0;x<sj[j].df;x++){
                        if(maxd < dp[nn][j][x]){
                                maxd = max(maxd,dp[nn][j][x]);
                                mf.n = nn;
                                mf.m = j;
                                mf.d = x;
                        }
                }
        }
        //printf("%lld\n",maxd);
        //f[3][3][2].out();
        if(maxd>0){
                puts("YES");
                F sf[NMAX];
                LL tf = 0;
                while(1){
                        if(mf.n<0)break;
                        sf[tf++]=mf;
                        mf = f[mf.n][mf.m][mf.d];
                }
                for(LL i=tf-1;i>=0;i--){
                        printf("%lld %lld\n",sf[i].m+1,sf[i].d+sj[sf[i].m].a);
                }
        }
        else{
                puts("NO");
        }
        //printf("%lld\n",maxd);
        return 0;
}

#include <iostream>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <string>
#include <sstream>
#include <stack>
#include <queue>
#include <map>
#include <numeric>
#include <cstdlib>
#include <cstring>
#include <utility>
#include <functional>
#include <cstdio>
#include <ctime>
using namespace std ;
#define eps 1e-10
#define ll long long
#define ld long double
#define ull unsigned long long
#define REP(i,k,n) for(int i=k;i<n;i++)
#define REV(i,k,n) for(int i=k;i>=n;i--)
#define SZ(x) ((int)x.size())
#define CLR(c,v) memset(c,v,sizeof (c))
////////////////////////////////
ll max (ll a,ll b) { return a > b ? a : b ; }
ll min (ll a,ll b) { return a < b ? a : b ; }
bool Eq (double a,double b) { return fabs (a - b) <= eps ; }
ll ABS (ll x) { return x < 0 ? - x : x ; }
//////////////////////
const int NN = 52 , MaxVal = 1000 * 1000 + 2 , p  = 43 , o = 83 , INF = (1 << 28) ;
//const ll  INF = 12345678901LL , MOD2 = 1000000007 ;
/////////////////////
int xx [] = {-1,-1,-1,0,0,1,1,1} ;
int yy [] = {-1,0,1,-1,1,-1,0,1} ;
///////////////////////////
int bit (int x) { return x == 0 ? 0 : 1 + bit (x & (x - 1)) ; }

int a [NN] , b [NN] , c [NN] ;
int n,m,k ;

int dp [NN][NN][NN * 2][NN] ; // cur , pre , dist , used ;
struct path {
	char pos,pre,dist,used;
	int cost ;
	path () {} ;
	path (char Pos,char Pre,char Dist,char Used,int Cost) {
		pos = Pos ;
		pre = Pre ;
		dist = Dist ;
		used = Used ;
		cost = Cost ;
	}
};

path nex [NN][NN][NN * 2][NN] ;

int rec (int pos,int pre,int dist,int used) {
	if (used == 0) {
		return 0 ;
	}
	if (pos == m) {
		return -(INF * 2) ;
	}

	int &res = dp [pos][pre][dist][used] ;
	if (res != -1) return res ;
	res = -INF ;

	int exer = used == n ? -1 : a [pre] + dist ;

	res = rec (pos + 1,pre,dist,used) ;
	nex [pos][pre][dist][used] = path (pos + 1,pre,dist,used,0) ;

	if (exer == -1) {
		REP (i,a [pos],b [pos] + 1) {
			int aux = rec (pos + 1,pos,i - a[pos],used - 1) + i ;
			if (aux > res) {
				res = aux ;
				nex [pos][pre][dist][used] = path (pos + 1,pos,i - a[pos],used - 1,i) ;
			}
		}
	}else {
		if (c [pos] > c [pre]) {
			if (exer + k >= a [pos] && exer + k <= b [pos]) {
				int aux = rec (pos + 1,pos,exer + k - a [pos],used - 1) + exer + k ;
				if (aux > res) {
					res = aux ;
					nex [pos][pre][dist][used] = path (pos + 1,pos,exer + k - a [pos],used - 1,exer + k) ;
				}
			}
			if (exer * k >= a [pos] && exer * k <= b [pos]) {
				int aux = rec (pos + 1,pos,exer * k - a [pos],used - 1) + exer * k ;
				if (aux > res) {
					res = aux ;
					nex [pos][pre][dist][used] = path (pos + 1,pos,exer * k - a [pos],used - 1,exer * k) ;
				}
			}
		}
	}
	return res ;
}

int main () {
	//////////////////
#ifdef HOMEPC
	freopen ("in.in","r",stdin) ;
#endif
    ////////////////// Code ///////////////////
	scanf ("%d%d%d",&n,&m,&k) ;
	REP (i,0,m) {
		scanf ("%d%d%d",&a[i],&b[i],&c[i]) ;
	}
	REP (i,0,m) REP (j,i + 1,m) if (c [i] > c [j]) {
		swap (a[i],a[j]) ;
		swap (b[i],b[j]) ;
		swap (c[i],c[j]) ;
	}
	CLR (dp,-1) ;
	int ans = rec (0,0,0,n) ;
	if (ans <= 0) {
		cout << "NO" << endl ;
	}else {
		cout << "YES" << endl ;
		int pos = 0 , pre = 0 , dist = 0 , used = n ;
		while (used > 0) {
			path p = nex [pos][pre][dist][used] ;
			if (p.used + 1 == used) {
				cout << pos + 1 << ' ' << p.cost << endl ;
			}
			pos = p.pos , pre = p.pre , dist = p.dist , used = p.used ;
		}
	}
    return 0 ;
}

#include <iostream>
#include <algorithm>

using namespace std;

struct Subject
{
    long long a, b, c;
    int num;
    void scan(int n)
    {
        num = n;
        scanf("%I64d%I64d%I64d", &a, &b, &c);
    }
    bool operator<(const Subject & sub)
    {
        return c < sub.c;
    }
    bool good(long long x)
    {
        return a <= x && x <= b;
    }
    int get(long long x)
    {
        return x - a;
    }
}subj[146];

long long dp[146][146][146];
pair<int, int> prv[146][146][146];

void printAns(int i, int j, int k)
{
    if(j != 1)
        printAns(prv[i][j][k].first, j - 1, prv[i][j][k].second);
    printf("%d %I64d\n", subj[i].num + 1, subj[i].a + k);
}

int main()
{
    int n, m, q;
    scanf("%d%d%d", &n, &m, &q);
    for(int i = 0; i < m; i++)
        subj[i].scan(i);
    sort(subj, subj + m);
    memset(dp, -1, sizeof(dp));
    for(int i = 0; i < m; i++)
        for(int j = 0; j <= subj[i].b - subj[i].a; j++)
            dp[i][1][j] = subj[i].a + j;
    for(int i = 0; i < m; i++)
        for(int j = 0; j < n; j++)
            for(int k = 0; k <= subj[i].b - subj[i].a; k++)
                if(dp[i][j][k] != -1)
                {
                    for(int l = i + 1; l < m; l++)
                    {
                        if(subj[l].good(subj[i].a + k + q) && dp[l][j + 1][subj[l].get(subj[i].a + k + q)] < dp[i][j][k] + subj[i].a + k + q)
                        {
                            dp[l][j + 1][subj[l].get(subj[i].a + k + q)] = dp[i][j][k] + subj[i].a + k + q;
                            prv[l][j + 1][subj[l].get(subj[i].a + k + q)] = make_pair(i, k);
                        }
                        if(subj[l].good((subj[i].a + k) * q) && dp[l][j + 1][subj[l].get((subj[i].a + k) * q)] < dp[i][j][k] + (subj[i].a + k) * q)
                        {
                            dp[l][j + 1][subj[l].get((subj[i].a + k) * q)] = dp[i][j][k] + (subj[i].a + k) * q;
                            prv[l][j + 1][subj[l].get((subj[i].a + k) * q)] = make_pair(i, k);
                        }
                    }
                }
    long long ans = -1;
    for(int i = 0; i < m; i++)
        for(int j = 0; j <= 100; j++)
            ans = max(ans, dp[i][n][j]);
    if(ans != -1)
    {
        for(int i = 0; i < m; i++)
            for(int j = 0; j <= 100; j++)
                if(dp[i][n][j] == ans)
                {
                    printf("YES\n");
                    printAns(i, n, j);
                    return 0;
                }
    }
    printf("NO");
}

#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;

struct subj{
	ll index, a, b, c;
	subj() {
		index = a = b = c = 0;
	}
};
bool operator <(subj d1, subj d2) {
	if(d1.c != d2.c) return d1.c < d2.c;
	return d1.index < d2.index;
}
const int MAXN = 50;
const int MAXM = 50;
const int MAXK = 100;

ll n, m, k;
subj s[MAXM + 10];
ll dp[MAXN+ 10][MAXM + 10][MAXK + 10]; // tedade problems!
ll par[MAXN + 10][MAXM + 10][MAXK + 10]; // vase m e!
ll paro[MAXN + 10][MAXM + 10][MAXK + 10];
void print(int i, int j, int o) {
	if(i == 0) {
		cout << s[j].index << ' ' << s[j].a + o << endl;
		return;
	}
	print(i - 1, par[i][j][o], paro[i][j][o]);
	cout << s[j].index << ' ' << s[j].a + o << endl;
}
int main() {
	ios::sync_with_stdio(false);
	cin >> n >> m >> k;
	for(int i = 0; i < m; i++) {
		cin >> s[i].a >> s[i].b >> s[i].c;
		s[i].index = i + 1;
	}
	sort(s, s + m);
	//for(int i = 0; i < m; i++)
	//	cerr << s[i].index << ' ' << s[i].c << endl;
	for(int j = 0; j < m; j++)
		for(int o = 0; o <= s[j].b - s[j].a; o++)
			dp[0][j][o] = o + s[j].a;
	for(int i = 1; i < n; i++)
		for(int j = 0; j < m; j++)
			for(int o = 1; o <= s[j].b - s[j].a; o++) {
				for(int p = j - 1; p >= 0; p--) {
					if(s[j].c == s[p].c) continue;
					if((o + s[j].a) % k == 0  && ((o + s[j].a) / k) - s[p].a >= 0 && ((o + s[j].a) / k) - s[p].a <= s[j].b - s[j].a && dp[i - 1][p][((o + s[j].a) / k) - s[p].a] != 0) {
						if(dp[i - 1][p][((o + s[j].a) / k) - s[p].a] + o + s[j].a > dp[i][j][o]) {
							par[i][j][o] = p;
							paro[i][j][o] = ((o + s[j].a) / k) - s[p].a;
						}
						dp[i][j][o] = max(dp[i][j][o], dp[i - 1][p][((o + s[j].a) / k) - s[p].a] + o + s[j].a);
					}
					if(o + s[j].a - k - s[p].a >= 0 && o + s[j].a - k - s[p].a <= s[j].b - s[j].a &&  dp[i - 1][p][o + s[j].a - k - s[p].a] != 0) {
						if(dp[i - 1][p][o + s[j].a - k - s[p].a] + o + s[j].a > dp[i][j][o]) {
							par[i][j][o] = p;
							paro[i][j][o] = o + s[j].a - k - s[p].a;
						}
						dp[i][j][o] = max(dp[i][j][o], dp[i - 1][p][o + s[j].a - k - s[p].a] + o + s[j].a);
					}
				}
			}
	int maxm = 0, maxo = 0;
	for(int j = 0; j < m; j++)
		for(int o = 0; o <= s[j].b - s[j].a ; o++) {
			//cerr << dp[
			if(dp[n - 1][j][o] > dp[n - 1][maxm][maxo]) {
				maxm = j;
				maxo = o;
			}
		}
	if(dp[n - 1][maxm][maxo] == 0) {
		cout << "NO" << endl;
		//system("pause");
		return 0;
	}
	cout << "YES" << endl;
	print(n - 1, maxm, maxo);
	//system("pause");
	return 0;
}

#include <bits/stdc++.h>
#define err(args...) {}
#ifdef DEBUG
#include "_debug.cpp"
#endif
using namespace std;
using ll = long long;
template <typename T> using lim = numeric_limits<T>;
struct subj {
    ll a, b;
    int c;
    int size() {
        return b - a + 1;
    }
};
const int N = 50, M = 50, VAL = 100;
subj s[M+1];
bool can[N+1][M+1][VAL+1];
struct state {
    int n, m, val;
} pred[N+1][M+1][VAL+1];
map<ll, set<pair<int, int>>> can_m[N+1];
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int n, m, k;
    cin >> n >> m >> k;
    for(int i = 1; i <= m; i++) {
        cin >> s[i].a >> s[i].b >> s[i].c;
    }
    sort(s + 1, s + 1 + m, [](auto& s1, auto& s2) {
        return s1.c < s2.c;
    });
    for(int nn = 1; nn <= n; nn++) {
        for(int i = 1; i <= m; i++) {
            for(int val1 = 0; val1 < s[i].size(); val1++) {
                bool& ans = can[nn][i][val1];
                if(nn == 1) {
                    ans = true;
                } else {
                    ans = false;
                    if(can_m[nn - 1].count(s[i].a + val1)) {
                        for(auto [j, val2] : can_m[nn - 1][s[i].a + val1]) {
                            if(s[j].c < s[i].c) {
                                ans = true;
                                pred[nn][i][val1] = {nn - 1, j, val2};
                                break;
                            }
                        }
                    }
                }
                if(ans) {
                    can_m[nn][k + s[i].a + val1].insert({i, val1});
                    can_m[nn][k * (s[i].a + val1)].insert({i, val1});
                }
            }
        }
    }
    for(int i = 1; i <= m; i++) {
        for(int val = 0; val < s[i].size(); val++) {
            if(can[n][m][val]) {
                cout << "YES" << endl;
                vector<pair<int, ll>> ans;
                state curr = {n, m, val};
                while(curr.n > 0) {
                    ans.push_back({curr.m, s[curr.m].a + curr.val});
                    curr = pred[curr.n][curr.m][curr.val];
                }
                reverse(ans.begin(), ans.end());
                for(auto [x, y] : ans) {
                    cout << x << " " << y << endl;
                }
                return 0;
            }
        }
    }
    cout << "NO" << endl;
    return 0;
}

#include <cstdio>
#include <memory.h>
#include <cstring>
#include <vector>
#include <deque>
#include <cstdlib>
#include <queue>
#include <algorithm>
#include <cmath>
#include <cassert>
#include <functional>
#include <iostream>
#include <set>
#include <list>
#include <map>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <bitset>
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
using namespace std;

typedef long long ll;
typedef unsigned long long llu;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;
typedef pair<string, int> psi;
const ll MOD = 1e9 + 7;
const long double PI = 3.141592653589793238462643383279502884197;

priority_queue<int, vector<int>, greater<int> > pq;
vector<int> v;

int n, m;
int kk;
struct node {
	ll a, b;
	int c, id;
} vec[50];

ll dp[50][51][101][50];
char tra[50][51][101][50];

ll go(int o, int k, int val, int pre) {
	if (o == m) {
		if (k == n) return 0;
		return -90000000000000;
	}
	if (dp[o][k][val][pre] != -1) return dp[o][k][val][pre];

	ll mav = -90000000000000, t;
	t = go(o + 1, k, val, pre);
	if (t > mav) {
		mav = t;
		tra[o][k][val][pre] = 0;
	}

	if (vec[o].c > vec[pre].c) {
		ll tt = vec[pre].a + val;
		if (tt * kk >= vec[o].a && tt * kk <= vec[o].b) {
			t = go(o + 1, k + 1, tt * kk - vec[o].a, o) + tt * kk;
			if (t > mav) {
				mav = t;
				tra[o][k][val][pre] = 1;
			}
		}
		if (tt + kk >= vec[o].a && tt + kk <= vec[o].b) {
			t = go(o + 1, k + 1, tt + kk - vec[o].a, o) + tt + kk;
			if (t > mav) {
				mav = t;
				tra[o][k][val][pre] = 2;
			}
		}
	}

	return dp[o][k][val][pre] = mav;
}

int main() {
	memset(dp, -1, sizeof(dp));
	scanf("%d %d %d", &n, &m, &kk);
	for (int i = 0; i < m; i++) {
		scanf("%lld %lld %d", &vec[i].a, &vec[i].b, &vec[i].c);
		vec[i].id = i + 1;
	}
	sort(vec, vec + m, [](node v1, node v2) { return v1.c < v2.c; });

	ll mav = -90000000000000;
	int o, k, val, pre;
	for (int i = 1; i <= m; i++)
		for (int j = 0; j <= vec[i - 1].b - vec[i - 1].a; j++) {
			ll t = go(i, 1, j, i - 1) + j + vec[i - 1].a;
			if (t > mav) {
				mav = t;
				o = i, k = 1, val = j, pre = i - 1;
			}
		}

	if (mav < -80000000000000) return !printf("NO");

	printf("YES\n");
	printf("%d %lld\n", vec[o - 1].id, vec[o - 1].a + val);
	for (int i = o; i < m; i++) {
		ll tt = vec[pre].a + val;

		if (tra[i][k][val][pre] == 1) {
			tt *= kk;
			printf("%d %lld\n", vec[i].id, tt);
			pre = i;
			val = tt - vec[i].a;
			k++;
		}
		else if (tra[i][k][val][pre] == 2) {
			tt += kk;
			printf("%d %lld\n", vec[i].id, tt);
			pre = i;
			val = tt - vec[i].a;
			k++;
		}
	}
}

#include <bits/stdc++.h>

using namespace std;

const int MOD=1e9+7;

//const int MOD=998244353;

struct Subject{
    int pos, c;
    long long a,b;
    Subject(int _pos, long long _a, long long _b, int _c):pos(_pos),a(_a),b(_b),c(_c){}
};

struct Pe{
    long long val;
    int i,j;
    Pe(int _i, int _j, long long _val):val(_val), i(_i), j(_j){}

};

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int n,m,k;
    cin>>n>>m>>k;

    vector<Subject> vs;
    for(int i=0; i<m; i++){
        long long a,b;
        int c;
        cin>>a>>b>>c;
        vs.push_back(Subject(i,a,b,c));
    }
    sort(vs.begin(),vs.end(),[](auto s1, auto s2){return s1.c<s2.c;});

    vector<vector<map<long long,long long>>> dp(n+1,vector<map<long long,long long>>(m));
    vector<vector<map<long long,Pe>>> preds(n+1, vector<map<long long,Pe>>(m));

    for(int i=1; i<=n; i++){
        for(int j=0; j<m; j++){
            for(long long kv=vs[j].a; kv<=vs[j].b; kv++){
                if(i==1) dp[i][j][kv]=kv;
                else{
                    long long bv=0;
                    for(int j2=0; j2<i; j2++){
                        if(vs[j2].c<vs[j].c){
                            auto it=dp[i-1][j2].find(kv-k);
                            if(it!=dp[i-1][j2].end() && kv+it->second>bv){
                                bv=kv+it->second;
                                dp[i][j][kv]=bv;
                                Pe pe(i-1,j2,kv-k);
                                preds[i][j].insert({kv,pe});
                            }
                            if(!(kv%k)){
                                auto it=dp[i-1][j2].find(kv/k);
                                if(it!=dp[i-1][j2].end() && kv+it->second>bv){
                                    bv=kv+it->second;
                                    dp[i][j][kv]=bv;
                                    Pe pe(i-1,j2,kv/k);
                                    preds[i][j].insert({kv,pe});
                                }
                            }

                        }
                    }
                }

            }

        }
    }

    long long tot=0;
    int pj=-1;
    long long pk=-1;
    for(int j=0; j<m; j++){
        for(auto p:dp[n][j]){
            if(p.second>tot){
                tot=p.second;
                pk=p.first;
                pj=j;
            }
        }
    }
    if(!tot){
        cout<<"NO";
        return 0;
    }
    cout<<"YES\n";
    vector<pair<int,long long>> ans;

    for(int i=n; i; i--){
        ans.push_back({pj,pk});

        if(i!=1){
            auto pe = preds[i][pj].at(pk);
           pj=pe.i;
            pk=pe.val;
        }

    }
    reverse(ans.begin(),ans.end());
    for(auto p:ans){
        cout<<p.first+1<<' '<<p.second<<'\n';
    }

    return 0;
}

#include <iostream>
#include <cstdio>
#include <cstring>

#include <algorithm>

using namespace std;
long long f[52][52][102];
int path[52][52][102][2];

struct node{
	long long a , b , c;
	int id;
	bool operator < (const node & p) const
	{
		return c < p.c;
	}
};
node r[52];

void print(int n , int j , long long x)
{
	if(path[n][j][x][0] != j)
	{
		int u = path[n][j][x][0];
		long long v = path[n][j][x][1];
		print(n - 1 , u , v);
	}
	cout << r[j].id << " " << x + r[j].a << endl;
}

int main()
{
//	freopen("data.in" , "r" ,stdin);
	int n , m , k;
	cin >> n  >> m >> k;
	for(int i = 0 ; i < m ;i++)
	{
		cin >> r[i].a >>r[i].b >> r[i].c;
		r[i].id = i + 1;
	}
	sort(r , r + m);

	int last_j = 0;
	long long last_x = 0;
	long long Max = -(1ll << 16);
	memset(f , 0 ,sizeof(f));
	for(int i = 0 ; i < n ;i++)
	{
		for(int j = i ; j < m ;j++)
		{
			long long u = r[j].b - r[j].a;
			for(int x = 0 ; x <= u ; x++)
			{
				long long ans = -(1ll << 16);

				if(i == 0)
				{
					f[i][j][x] = x + r[j].a;
					path[i][j][x][0] = j;
					path[i][j][x][1] = x + r[j].a;
				}
				else
				{
					long long v = x + r[j].a;

					for(int p = i - 1 ; p < j ; p++)
					{
						if(r[j].c  == r[p].c) continue;
						if(v % k == 0 && v / k >= r[p].a && v / k <= r[p].b)
						{
							if(ans < f[i - 1][p][v / k - r[p].a] + v)
							{
								ans = f[i - 1][p][v / k - r[p].a] + v;
								path[i][j][x][0] = p;
								path[i][j][x][1] = v / k - r[p].a;
							}

						}
						if(v - k >= r[p].a && v - k <= r[p].b)
						{
							if(ans < f[i - 1][p][v - k - r[p].a] + v)
							{
								ans  = f[i - 1][p][v - k - r[p].a] + v;
								path[i][j][x][0] = p;
								path[i][j][x][1] = v - k - r[p].a;
							}

						}
					}
					f[i][j][x] = ans;
				}
				if(i == n - 1)
				{
					if(Max < f[i][j][x])
					{
						Max = f[i][j][x];
						last_j = j;
						last_x = x;
					}
				}
			}
		}
	}

	//cout << Max << endl;
	if(Max > 0)
	{
		cout << "YES" << endl;
		print(n - 1 , last_j , last_x);
	}
	else
	{
		cout << "NO" << endl;
	}
//	while(1);
	return 0;
}

#define _CRT_SECURE_NO_WARNINGS
#pragma comment (linker, "/STACK:16777216")
#include <iostream>
#include <algorithm>
#include <numeric>
#include <vector>
#include <queue>
#include <list>
#include <stack>
#include <string>
#include <fstream>
#include <math.h>
#include <limits>
#include <set>
#include <map>
#include <sstream>
#include <stdio.h>
#include <time.h>
#include <memory.h>
#include <cassert>
#include <complex>
using namespace std;

///////////////// macros and typedefs ///////////////////
#define rep(i, n) for (int i = 0, _n = (n); i < _n; ++i)
#define repd(i, n) for (int i = (n)-1; i >= 0; --i)
#define _fill(a, x) memset((a), (x), sizeof((a)))
#define DEB(k) cerr<<"debug: "#k<<"="<<k<<endl;
#define all(c) (c).begin(), (c).end()
#define mp(a, b) make_pair(a, b)
#define l(c) (int)((c).size())
#define sqr(a) ((a)*(a))
#define inf 0x7f7f7f7f
#define pb push_back
#define ppb pop_back
#define x first
#define y second
typedef long long ll;
typedef vector<int> vi;
typedef vector<double> vd;
typedef pair<int,int> pi;

#define infl 1000000000000000000LL

struct subj {
   int id;
   int comp;
   ll a, b;
};

bool cmp(const subj& A, const subj& B) {
   if (A.comp != B.comp) return A.comp < B.comp;
   if (A.b != B.b) return A.b < B.b;
   return A.a < B.a;
}

int n, m, k;
subj d[50];
ll dp[51][51][102];

ll go(int i, int j, int add) {
   if (j == m) return -infl;
   if (dp[i][j][add] != -1) return dp[i][j][add];
   ll ret = go(i, j+1, add);
   if (add == 101) {
      if (j == m) return -infl;
      ll diff = d[j].b-d[j].a+1;
      rep(it, diff)
         ret = max(ret, go(i+1, j, it));
   }
   else {
      ll sum = d[j].a + add;
      if (j == m-1) return i == n ? sum : -infl;
      for (int j2 = j+1; j2 < m; j2++) if (d[j2].comp > d[j].comp) {
         ll val = sum + k;
         if (val == 0) val = 101;
         if (d[j2].a <= val && val <= d[j2].b)
            ret = max(ret, go(i+1, j2, val-d[j2].a) + sum);
         val = sum * k;
         if (val == 0) val = 101;
         if (d[j2].a <= val && val <= d[j2].b)
            ret = max(ret, go(i+1, j2, val-d[j2].a) + sum);
      }
   }
   return dp[i][j][add] = ret;
}

void print(int i, int j, int add) {
   ll ret = dp[i][j][add];
   if (ret == go(i, j+1, add)) {
      print(i, j+1, add);
      return;
   }
   if (add == 101) {
      ll diff = d[j].b-d[j].a+1;
      rep(it, diff)
         if (ret == go(i+1, j, it)) {
            print(i+1, j, it);
            return;
         }
   }
   else {
      ll sum = d[j].a + add;
      cout << d[j].id+1 << " " << sum << endl;
      if (i == n) return;
      for (int j2 = j+1; j2 < m; j2++) if (d[j2].comp > d[j].comp) {
         ll val = sum + k;
         if (val == 0) val = 101;
         if (d[j2].a <= val && val <= d[j2].b)
            if (go(i+1, j2, val-d[j2].a) + sum == ret) {
               print(i+1, j2, val-d[j2].a);
               return;
            }
         val = sum * k;
         if (val == 0) val = 101;
         if (d[j2].a <= val && val <= d[j2].b)
            if (go(i+1, j2, val-d[j2].a) + sum == ret) {
               print(i+1, j2, val-d[j2].a);
               return;
            }
      }
   }
}

void solution()
{
   cin >> n >> m >> k;
   rep(i, m) {
      cin >> d[i].a >> d[i].b >> d[i].comp;
      d[i].id = i;
   }
   sort(d, d+m, cmp);
   _fill(dp, -1);
   ll ret = go(0, 0, 101);
   if (ret < 0) puts("NO");
   else {
      puts("YES");
      //cerr << ret << endl;
      print(0, 0, 101);
   }
}

int main()
{
#ifndef ONLINE_JUDGE
   freopen("my.in", "rt", stdin);
   //freopen("my.out", "wt", stdout);
#endif
   solution();
#ifndef ONLINE_JUDGE
   double totalTime = clock()*1./CLOCKS_PER_SEC;
   fprintf(stderr, "Time: %.2lf sec\n", totalTime);
#endif
   return 0;
}

#include<bits/stdc++.h>
#define REP(i,n) for (int i = 1; i <= n; i++)
#define mod 1000000007
#define pb push_back
#define ff first
#define ss second
#define ii pair<lli,lli>
#define vi vector<int>
#define vii vector<ii>
#define lli long long int
#define endl '\n'
const double PI = 3.141592653589793238460;
typedef std::complex<double> Complex;
typedef std::valarray<Complex> CArray;

using namespace std;
const lli INF = LONG_MAX;
struct subject{
	lli a , b;
	int idx , c;
};
subject sb[101];

bool comp(subject a , subject b)
{
	return a.c < b.c;
}

lli dp[51][51][101];
ii pre[51][51][101];

int main()
{
	lli n , m , k;
	cin>>n>>m>>k;

	REP(i , m) cin>>sb[i].a>>sb[i].b>>sb[i].c , sb[i].idx = i;
	sort(sb+1 , sb+1+m , comp);

	memset(dp , -1 , sizeof dp);
	for(int j=1;j<=m;j++)
	{
		for(int dx=0;dx<=sb[j].b - sb[j].a; dx++)
		dp[1][j][dx] = sb[j].a + dx;
	}

	ii finalRes = {0 , -1};
	lli maximum = -1;

	for(int i=2;i<=n;i++)
	{
//		cout<<"for "<<i<<endl;
		for(int j=i;j<=m;j++)
		{
//			cout<<"\tfor j = "<<j<<endl;

			for(int dx=0;dx<=sb[j].b-sb[j].a;dx++)
			{
				lli mx = -1;
				ii tmp = {0 , -1};

				lli d1 = -1 , d2 = -1;
				if((sb[j].a + dx) % k == 0)
				d2 = (sb[j].a + dx) / k;

				if(sb[j].a + dx > k)
				d1 = sb[j].a + dx - k;

				for(int q=1;q<j;q++)
				{
					if(d1 > 0 && (d1 >= sb[q].a && d1 <= sb[q].b))
					{
						if(mx < dp[i-1][q][d1-sb[q].a])
						mx = dp[i-1][q][d1-sb[q].a] , tmp = {q , d1-sb[q].a};
					}

					if(d2 > 0 && (d2 >= sb[q].a && d2 <= sb[q].b))
					{
						if(mx < dp[i-1][q][d2-sb[q].a])
						mx = dp[i-1][q][d2-sb[q].a] , tmp = {q , d2-sb[q].a};
					}
				}

//				cout<<"\t\tmx = "<<mx<<" from course "<<tmp.ff<<" with homework = "<<sb[j].a + dx<<endl;
				if(mx == -1)
				dp[i][j][dx] = -1;
				else
				{
					dp[i][j][dx] = mx + sb[j].a + dx;
					pre[i][j][dx] = tmp;

					if(i == n && dp[i][j][dx] > maximum)
					finalRes = {j , dx} , maximum = dp[i][j][dx];
				}
			}
		}
	}

	if(finalRes.ss == -1)
	cout<<"NO";
	else
	{
		cout<<"YES "<<endl;
		vii res;

		int i = n;
		while(i > 0)
		{
			res.pb({sb[finalRes.ff].idx , sb[finalRes.ff].a + finalRes.ss});
			int q = finalRes.ff;
			int dx = finalRes.ss;

			finalRes = pre[i][q][dx] , i--;
		}

		reverse(res.begin() , res.end());
		for(ii p : res)
		cout<<p.ff<<" "<<p.ss<<endl;
	}
}

#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

// ----------------------------------------Defines and Template-------------------------------------//
#define int long long
#define ll long long
#define double long double
#define f first
#define ss second
#define mp make_pair
#define pb push_back
#define all(v) v.begin(),v.end()
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vi vector<int>
#define vii vector<vi>
#define vl vector<ll>
#define vpii vector<pii>
#define vpll vector<pll>
#define print(arr) for (auto it = arr.begin(); it != arr.end(); ++it) cout << *it << " "; cout << endl;

#define min3(a, b, c) min(a, min(b, c))
#define max3(a, b, c) max(a, max(b, c))
#define alla(a, n) a, a + n
#define gcd(a, b) __gcd(a, b)
#define lcm(a, b) (a * b)/gcd(a, b)

typedef tree<
int,
null_type,
less<int>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
//order_of_key (k) : Number of items strictly smaller than k .
//find_by_order(k) : K-th element in a set (counting from zero).

#define setitr set <int, greater <int> > :: iterator
#define RE(i,n) for(int i=1; i<=n; i++)
#define REP(i,j,n) for(int i=j; i<=n; i++)
#define REB(i,j,n) for(int i=j; i>=n; i--)
#define FO(i,n) for(int i=n; i>0; i--)

const int mod = 1e9+7;
const int INF = 1e15;
const int LGN = 30;

//-------------------------------------------------Template Ends------------------------------------------------------/

int dp[52][52][103],n,m,k;  //exercise
pii up[52][52][103]; //subject, ind
int a[53], b[54], c[54], in[54];
//dp[i][j][k] - Answer from 1 to i ending with subject j on day i doing total of a[j]+k exercises.

void soln(int i, int j, int k)
{
    if(i==0)
    {
        return;
    }
    soln(i-1,up[i][j][k].f, up[i][j][k].ss);
    cout << in[j] << " " << a[j]+k << "\n";
}

void solve_case()
{
    int i,j,t,k1;
    cin >> n >> m >> k1;
    vector<pair<pair<int,int>, pair<int, int> > > arr;
    RE(tt, m)
    {
        cin >> i >> j >> t;
        arr.pb(mp(mp(t,tt),mp(i,j)));
    }
    sort(all(arr));
    REP(i,0,m-1)
    {
        a[i+1] = arr[i].ss.f;
        b[i+1] = arr[i].ss.ss;
        c[i+1] = arr[i].f.f;
        in[i+1] = arr[i].f.ss; //Order has to be retained for output :(
    }
    REP(i,0,n) REP(j,0,m) REP(k,0,100) { dp[i][j][k] = -INF; up[i][j][k] = mp(-1,-1);}
    for(i=1; i<=m; i++)
    {
        for(j=0; j+a[i]<=b[i]; j++)
        {
            dp[1][i][j] = j+a[i];
            up[1][i][j] = mp(i,j);
        }
    }
    for(i=2; i<=n; i++)
    {
        for(j=1; j<=m; j++)
        {
            for(k=0; k+a[j]<=b[j]; k++)
            {

                for(int l=1; l<j; l++)
                {
                    int pos1 = k+a[j] - k1;     //type 1: prev+k
                    int pos2 = k+a[j];         //type 2: prev*k
                    if(c[l]==c[j]) continue;  //complexity strictly increasing.
                    //dont be a stupid fool and know the diff b/w i and j.
                    if(pos1>=a[l] && pos1<=b[l])
                    {
                        if((dp[i-1][l][pos1-a[l]]+a[j]+k) > dp[i][j][k])
                        {
                            dp[i][j][k] = dp[i-1][l][pos1-a[l]] + a[j]+k;
                            up[i][j][k] = mp(l,pos1-a[l]);
                        }
                    }
                    if(pos2%k1==0)
                    {
                        pos2 = (pos2/k1);
                        if(pos2>=a[l] && pos2<=b[l])
                        {
                            if((dp[i-1][l][pos2-a[l]]+a[j]+k) > dp[i][j][k])
                            {
                                dp[i][j][k] = dp[i-1][l][pos2-a[l]] + a[j]+k;
                                up[i][j][k] = mp(l,pos2-a[l]);
                            }
                        }
                    }
                }
            }
        }
    }

    /*
    for(i=1; i<=n; i++)
    {
        cout << i << ": \n";
        for(j=1; j<=m; j++)
        {
            cout << j << ": ";
            for(k=0; k+a[j]<=b[j]; k++)
            {
                cout << dp[i][j][k] << " ";
            }
            cout << "\n";
        }
    }
    */

    int ans = -INF;
    pii ind = mp(-1,-1);
    for(i=1; i<=m; i++)
    {
        for(j=0; j<=100; j++)
        {
            if(dp[n][i][j] > ans)
            {
                ans = dp[n][i][j];
                ind = mp(i,j);
            }
        }
    }
    if(ans<0) { cout << "NO\n"; return; }
    cout << "YES\n";
    soln(n, ind.f, ind.ss);
}

signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    //freopen("input.txt", "r", stdin);
    int Testcases = 1;
    //cin >> Testcases;
    while(Testcases--)
    {
        solve_case();
    }
    return 0;
}

#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <cstring>

using namespace std;

#define F first
#define PB(v,x) v.push_back(x)
#define size(X) (int)X.size()
#define S second
#define show(X) cerr << #X << " = " << X << endl;
#define getrandom(min,max) \
	((rand()%(int)(((max)+1)-(min)))+min)
#define foreach(var, container) \
	for(typeof((container).begin()) var = (container).begin(); \
			var != (container).end(); \
			++var)
#define SetBit(value, place) ((value) | (1 << (place)))
#define ClearBit(value, place) ((value) & (~(1 << (place))))
#define ToggleBit(value, place) ((value) ^ (1 << (place)))
#define GetBit(value, place) (((value) >> (place)) & 1)
#define GetPrecentage(Numerator, Denominator)	((Numerator)*100.0/(Denominator))
#define GI ({int t;scanf("%d",&t);t;})
#define GL ({int t;scanf("%I64d",&t);t;})
#define GD ({double t;scanf("%lf",&t);t;})
#define is istringstream
#define os ostringstream
#define lb lower_bound
#define ub upper_bound
#define bs binary_search
#define REP(i,n) for (int i=0,_n=(n); i < _n; i++)
#define REV(i,n) for (int i=(n)-1; i >= 0; i--)
#define FOR(i,a,b) for (int _b=(b), i=(a); i <= _b; i++)
#define ROR(i,a,b) for(int i=(a),_b=(b);i>=_b;i--)
#define ALL(c) (c).begin(), (c).end()
#define SORT(c) sort(ALL(c))
#define CLEAR(x) memset(x,0,sizeof x);
#define REVERSE(c) reverse(ALL(c))
#define UNIQUE(c) SORT(c),(c).resize(unique(ALL(c))-(c).begin())
#define INF (ll)1e17
#define eps 1.0e-11
#define PI(x) printf("%d ",x)
template <class X> void printa(X a,int n)
{
	REP(i,n) cerr << a[i] << " " ;
	cerr << endl;
}

template <class X> void printv(X v)
{
	foreach(it,v) cerr << *it << " " ;
	cerr << endl;
}
typedef pair<int,int> pii;
typedef long long ll;
typedef pair<ll,ll> pll;

const ll MAXN = 120;
ll n,m,k;
ll d[60][60][MAXN];
ll mark[60][60][MAXN];
pll parent[60][60][MAXN];
struct sub{
	ll c,a,b,i;
	sub(){;}
	friend istream &operator >> (istream &cin , sub&x)
	{
		cin >> x.a >> x.b >> x.c;
		return cin;
	}
	bool operator < (const sub &x) const { return c < x.c; }
}subj[MAXN];

void input()
{
	cin >> n >> m >> k;
	REP(i,m)
	{
		cin >> subj[i];
		subj[i].i = i;
	}
	sort(subj,subj+m);
}

ll memz(ll i,ll j,ll t)
{

	if(mark[i][j][t])
		return d[i][j][t];
	ll q = subj[i].a + t;
	mark[i][j][t] = 1;
	if(q > subj[i].b)
	{
		d[i][j][t] = -1*INF;
		return -1*INF;
	}
	if(j==0)
	{
		d[i][j][t] = q;
		return q;
	}
	if(i<j)
	{
		d[i][j][t] = -1*INF;
		return d[i][j][t];
	}
	ll p1 = q;
	ll p2 = q-k;
	bool tt = true;
	REV(z,i) if(subj[z].c < subj[i].c && z>=j-1)
	{
		if(p2>=subj[z].a && p2<=subj[z].b && d[i][j][t] < memz(z,j-1,p2-subj[z].a) + q)
		{
			d[i][j][t] = memz(z,j-1,p2-subj[z].a) + q;
			parent[i][j][t].F = z;
			parent[i][j][t].S = p2-subj[z].a;
			tt = false;
		}
		if((p1/k)>=subj[z].a && (p1/k)<=subj[z].b && !(p1%k) && d[i][j][t] < memz(z,j-1,(p1/k)-subj[z].a)+q)
		{
			d[i][j][t] = memz(z,j-1,(p1/k)-subj[z].a)+q;
			parent[i][j][t].F = z;
			parent[i][j][t].S = p1/k-subj[z].a;
			tt = false;
		}
	}
	if(tt)
	{
		d[i][j][t] = -1*INF;
		return d[i][j][t];
	}
	return d[i][j][t];
}

int main()
{
	input();
	ll MAX = 0;
	ll s = -1;
	ll s2,s3;
	memset(parent,-1,sizeof parent);
	REP(i,m) REP(j,subj[i].b - subj[i].a) if(memz(i,n-1,j) > MAX)
	{
		MAX = memz(i,n-1,j);
		s = i;
		s2 = n-1;
		s3 = j;
	}
	if(MAX == 0)
	{
		cout << "NO" << endl;
		return 0;
	}
	cout << "YES" << endl;
	vector<pll> ans;
	while(s!=-1)
	{
		PB(ans,pll(subj[s].i + 1,s3 + subj[s].a));
		ll p = s;
		s = parent[s][s2][s3].F;
		s3 = parent[p][s2][s3].S;
		s2--;
	}
	reverse(ans.begin(),ans.end());
	REP(i,size(ans))
		cout << ans[i].F << " " << ans[i].S << endl;;
	return 0;
}

/*
 * Author: tender
 * Created Time:  2011/10/14 23:58:53
 * File Name: c.cpp
 */
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <map>
#include <queue>
#include <string>
#include <vector>

using namespace std;
const double pi = acos(-1.0);
long long dp[52][52][102];
struct node1 {
    int i, k;
}pre[52][52][102];
struct node {
    long long a, b;
    int c;
    int idx;
    bool operator < (const node &aa) const {
        return c < aa.c;
    }
}ex[55];
void print(int i, int j, int k){
    if (j > 1) print(pre[i][j][k].i, j - 1, pre[i][j][k].k);
    printf("%d %d\n", ex[i].idx, ex[i].a + k);
}
int main() {
    //freopen("2.txt", "w", stdout);
    int n, m, K;
    scanf("%d%d%d", &n, &m, &K);
    for (int i = 1; i <= m; i++) {
        scanf("%I64d%I64d%d", &ex[i].a, &ex[i].b, &ex[i].c);
        ex[i].idx = i;
    }
    sort(ex + 1, ex + m + 1);
    for (int i = 1; i <= m; i++)
        for (int j = 1; j <= n; j++) {
            long long end = ex[i].b - ex[i].a;
            for (int k = 0; k <= end; k++) {
                if (j == 1)
                    dp[i][j][k] = ex[i].a + k;
                else {
                    long long _max = 0, t = k + ex[i].a;
                    int j1 = j - 1;
                    for (int i1 = i - 1; i1 > 0; i1--) {
                        if (ex[i1].c >= ex[i].c) continue;
                        long long k1 = t - K;
                        if (k1 >= ex[i1].a && k1 <= ex[i1].b) {
                            if (_max < dp[i1][j1][k1 - ex[i1].a]) {
                                _max = dp[i1][j1][k1 - ex[i1].a];
                                pre[i][j][k].i = i1; pre[i][j][k].k = k1 - ex[i1].a;
                            }
                        }
                        //_max = max(_max, dp[i1][j1][k1 - ex[i1].a]);
                        k1 = t / K;
                        if (k1 * K == t && k1 >= ex[i1].a && k1 <= ex[i1].b) {
                            if (_max < dp[i1][j1][k1 - ex[i1].a]) {
                                _max = dp[i1][j1][k1 - ex[i1].a];
                                pre[i][j][k].i = i1; pre[i][j][k].k = k1 - ex[i1].a;
                            }
                        }
                        //_max = max(_max, dp[i1][j1][k1 - ex[i1].a]);
                    }
                    if (_max != 0)
                        dp[i][j][k] = _max + t;
                }
            }
        }
    //for (int i = 1; i <= m; i++)
        //for (int j = 1; j <= n; j++) {
            //long long end = ex[i].b - ex[i].a;
            //for (int k = 0; k <= end; k++)
                //printf("dp[%d][%d][%I64d] = %I64d ", i, j, k + ex[i].a, dp[i][j][k]);
            //printf("\n");
        //}
    //for (int i = 1; i <= m; i++)
        //for (int j = 1; j <= n; j++) {
            //long long end = ex[i].b - ex[i].a;
            //for (int k = 0; k <= end; k++)
                //printf("pre[%d][%d][%I64d] = %d %d %d ", i, j, k + ex[i].a, pre[i][j][k].i, j - 1, pre[i][j][k].k);
            //printf("\n");
        //}
    long long _max = 0;
    int i1, k1;
    for (int i = n; i <= m; i++) {
        long long end = ex[i].b - ex[i].a;
        for (int k = 0; k <= end; k++) {
            if (_max < dp[i][n][k]) {
                _max = dp[i][n][k];
                i1 = i; k1 = k;
            }
        }
        //_max = max(_max, dp[i][n][k]);
    }
    if (_max == 0)
        printf("NO\n");
    else {
        printf("YES\n");
        //printf("%I64d\n", _max);
        //printf("i1 = %d k1 = %d\n", i1, k1);
        print(i1, n, k1);
    }
    return 0;
}

#include <cstdio>
#include <cstring>
#include <algorithm>

#define MAXN 55
#define MAXD 105

using namespace std;

int n,m,k;
struct homework{
    long long l,r;
    int c,loc;
    bool operator < (const homework &a)const{
        return c < a.c;
    }
}   h[MAXN];
long long f[MAXN][MAXN][MAXD];
int g[MAXN][MAXN][MAXD];
int gg[MAXN][MAXN][MAXD];

void printans(int x,int y,int z){
    if (x>1){
        printans(x-1,g[x][y][z],gg[x][y][z]);
    }
    printf("%d %I64d\n",h[y].loc,h[x].l+(long long)z);
}

int main(){
  //  freopen("c.in","r",stdin);
    int i,j,x,y,z,ji;
    long long p,q,ans,jp;
   // while (scanf("%d%d%I64d",&n,&m,&k)!=EOF){
   scanf("%d%d%I64d",&n,&m,&k);
        for (i=1;i<=m;i++){
            scanf("%I64d%I64d%d",&h[i].l,&h[i].r,&h[i].c);
            h[i].loc = i;
        }
        sort(h+1,h+m+1);
        memset(f,-1,sizeof(f));
        for (i=1;i<=m;i++)
            for (p=h[i].l;p<=h[i].r;p++)
                f[1][i][p-h[1].l] = p;
        for (i=1;i<n;i++){
            for (j=1;j<=m;j++)
                for (p=h[j].l;p<=h[j].r;p++)
                    if (f[i][j][p-h[j].l]>=0){
                        for (x=j+1;x<=m;x++)
                            if (h[j].c<h[x].c){
                                q = p + k;
                                if (h[x].l<=q && q<=h[x].r){
                                    if (f[i+1][x][q-h[x].l]<f[i][j][p-h[j].l]+q){
                                        f[i+1][x][q-h[x].l] = f[i][j][p-h[j].l] + q;
                                        g[i+1][x][q-h[x].l] = j;
                                        gg[i+1][x][q-h[x].l] = p - h[j].l;
                                    }
                                }
                                q = p * k;
                                if (h[x].l<=q && q<=h[x].r){
                                    if (f[i+1][x][q-h[x].l]<f[i][j][p-h[j].l]+q){
                                        f[i+1][x][q-h[x].l] = f[i][j][p-h[j].l] + q;
                                        g[i+1][x][q-h[x].l] = j;
                                        gg[i+1][x][q-h[x].l] = p - h[j].l;
                                    }
                                }
                            }
                    }
        }
        ans = -1;
        for (i=1;i<=m;i++)
            for (p=h[i].l;p<=h[i].r;p++){
                if (f[n][i][p-h[i].l]>ans){
                    ans = f[n][i][p-h[i].l];
                    ji = i;
                    jp = p - h[i].l;
                }
            }
        //printf("f = %I64d\n",f[1][2][7]);
       // printf("ans = %I64d %d %d\n",ans,ji,jp);
        if (ans<0) printf("NO\n");
            else{
                printf("YES\n");
                printans(n,ji,jp);
            }
 //   }
    return 0;
}

#include <cstring>
#include <algorithm>
#include <bitset>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#ifdef ONLINE_JUDGE
#define debug(x)
#else
#define debug(x) cout<<"  ... "#x" : "<<(x)<<"\n"
#endif
#define SQ(x) ((x)*(x))
#define PV(i,n) ((i)>0?(i)-1:(n)-1)
#define NX(i,n) ((i)+1<(n)?(i)+1:0)
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
template<typename T> void mil(T& a, const T& b) { if (a > b) a = b; }
template<typename T> void mal(T& a, const T& b) { if (a < b) a = b; }
template<typename T> void usort(vector<T>& a) {
  sort(a.begin(), a.end()); a.erase(unique(a.begin(), a.end()), a.end()); }
template<typename A, typename B> B conv(const A& i) {
  stringstream s; B o; s << i, s >> o; return o; }

const int N = 64;
const ll INF = ~0ULL >> 1;

ll l[N], r[N];
int c[N];
ll dp[50][101][101];
int pv[50][101][101];
int pw[50][101][101];

int main() {
  //ll x = 480000000000000000LL;
  //cout << x * 90;
  int n, m, b;
  cin >> n >> m >> b;
  for (int i = 0; i < m; i++) {
    cin >> l[i] >> r[i] >> c[i];
  }
  for (int i = 0; i < 50; i++)
    for (int j = 0; j < 101; j++)
      for (int k = 0; k < 101; k++)
        dp[i][j][k] = -1;

  for (int i = 0; i < m; i++) {
    for (int j = 0; j <= (int)(r[i] - l[i]); j++) {
      dp[i][j][1] = l[i] + j;
    }

    for (int j = 0; j <= 100; j++) {
      for (int k = 1; k < n; k++) {
        if (dp[i][j][k] == -1) {
          continue;
        }
        for (int p = i + 1; p < m; p++) {
          if (c[p] > c[i]) {
            ll t;

            t = l[i] + j + b;
            if (l[p] <= t && t <= r[p]) {
              int z = t - l[p];
              ll& prev = dp[i][j][k];
              ll& curr = dp[p][z][k + 1];
              if (prev + t > curr) {
                curr = prev + t;
                pv[p][z][k + 1] = i;
                pw[p][z][k + 1] = j;
              }
            }

            t = (l[i] + j) * b;
            if (l[p] <= t && t <= r[p]) {
              int z = t - l[p];
              ll& prev = dp[i][j][k];
              ll& curr = dp[p][z][k + 1];
              if (prev + t > curr) {
                curr = prev + t;
                pv[p][z][k + 1] = i;
                pw[p][z][k + 1] = j;
              }
            }
          }
        }
      }
    }
  }

  //cout << dp[1][7][1] << endl;
  //cout << dp[2][9][2] << endl;
  //cout << dp[3][19][3] << endl;
  //cout << dp[4][39][4] << endl;

  vector<pii> ans;
  int mx = -1;
  bool flag = false;
  int o1, o2, o3;
  for (int i = 0; i < 50; i++) {
    for (int j = 0; j < 101; j++) {
      if (dp[i][j][n] > mx) {
        flag = true;

        o1 = i;
        o2 = j;
        o3 = n;

      }
    }
  }
  if (!flag) {
    cout << "NO";
  } else {
    cout << "YES\n";
    for (int k = 0; k < n; k++) {
      //cout << o1 << ' ' << o2 << ' ' << o3 << "\n";
      ans.push_back(pii(o1 + 1, o2 + l[o1]));
      if (k + 1 < n) {
        int tv = pv[o1][o2][o3];
        int tw = pw[o1][o2][o3];
        o1 = tv;
        o2 = tw;
        o3 = o3 - 1;
      }
    }
    for (int k = n - 1; k >= 0; k--) {
      cout << ans[k].first << ' ' << ans[k].second << "\n";
    }
  }
  return 0;
}

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

template <typename T>
inline void read(T &x){
    x = 0;int fu = 1;
    char c = getchar();
    while(c > 57 || c < 48){
        if(c == 45) fu = -1;
        c = getchar();
    }
    while(c <= 57 && c >= 48){
        x = (x << 3) + (x << 1) + c - 48;
        c = getchar();
    }
    x *= fu;
}
template <typename T>
inline void fprint(T x){
    if(x < 0) putchar(45), x = -x;
    if(x > 9) fprint(x / 10);
    putchar(x % 10 + 48);
}
template <typename T>
inline void fprint(T x, char ch){
    fprint(x);putchar(ch);
}

int n, m, k;

int dp[55][55][105];
typedef pair <int, int> pi;

pi pre[55][55][105];

struct Subject {
    int l, r, val, id;
    inline bool operator < (const Subject &tmp) const{
        return val < tmp.val;
    }
}a[55];

inline void output(int x, int y, int z){
    if(y == 1) {printf("%d %d\n", a[x].id, a[x].l + z);return;}
    pi cur = pre[x][y][z];
    output(cur.first, y - 1, cur.second);
    printf("%d %d\n", a[x].id, a[x].l + z);
}

int main(){
    read(n);read(m);read(k);
    for (register int i = 1;i <= m;i ++){read(a[i].l);read(a[i].r);read(a[i].val);a[i].id = i;}
    sort(a + 1, a + m + 1);
    for (register int i = 1;i <= m;i ++){
        for (register int j = 1;j <= n && j <= i;j ++){
            for (register int p = a[i].l;p <= a[i].r;p ++){
                int dis = p - a[i].l;
                for (register int q = 1;q < i;q ++){
                    if(j == 1) {dp[i][j][dis] = p;continue;}
                    if(p - k >= a[q].l && p - k <= a[q].r && dp[q][j - 1][p - k - a[q].l] && dp[q][j - 1][p - k - a[q].l] + p > dp[i][j][dis]) dp[i][j][dis] = dp[q][j - 1][p - k - a[q].l] + p, pre[i][j][dis] = make_pair(q, p - k - a[q].l);
                    if(p % k == 0 && (p / k) >= a[q].l && (p / k) <= a[q].r && dp[q][j - 1][(p / k) - a[q].l] && dp[q][j - 1][(p / k) - a[q].l] + p > dp[i][j][dis]) dp[i][j][dis] = dp[q][j - 1][(p / k) - a[q].l] + p, pre[i][j][dis] = make_pair(q, (p / k) - a[q].l);
                }
            }
        }
    }
    pi pos;
    int ans = -0x3f3f3f3f;
    for (register int i = n;i <= m;i ++){
        for (register int p = a[i].r;p >= a[i].l;p --){
            int dis = p - a[i].l;
            if(dp[i][n][dis] && dp[i][n][dis] > ans) pos = make_pair(i, dis), ans = dp[i][n][dis];
        }
    }
    if(ans == -0x3f3f3f3f) printf("NO\n");
    else{
        printf("YES\n");
        output(pos.first, n, pos.second);
    }

    return 0;
}

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef __int64 big;
struct W
{
    big s,e;
    int w,id;
}a[500];
struct
{
    int j,k;
}fa[61][120][155];
big f[61][120][155];
int K,n,m;
int ww[120][120],tt[120];
bool cmp(const W &a,const W &b){return a.w<b.w;}
void dfs(int dep,int i,int j)
{
    if(dep==1)return ;
    dfs(dep-1,fa[dep][i][j].j,a[fa[dep][i][j].k].w);
    printf("%d %I64d\n",a[fa[dep][i][j].k].id,fa[dep][i][j].j+a[fa[dep][i][j].k].s);
}
int main()
{
    int i,j,k,p;
    big val;
    scanf("%d%d%d",&n,&m,&K);
    for(i=1;i<=m;i++)
    scanf("%I64d%I64d%d",&a[i].s,&a[i].e,&a[i].w),a[i].id=i;
    sort(a+1,a+1+m,cmp);
    for(i=1;i<=m;i++)tt[a[i].id]=i;
    for(j=1;j<=m;j++)
    for(k=0;k<=a[j].e-a[j].s;k++)
    {
        f[1][k][a[j].w]=max(f[1][k][a[j].w],k+a[j].s);
        ww[k][a[j].w]=a[j].id;
    }
    for(i=1;i<n;i++)
    for(j=1;j<=m;j++)
    for(k=0;k<=a[j].e-a[j].s;k++)
    if(f[i][k][a[j].w])
    {
        for(p=j+1;p<=m;p++)
        if(a[p].w>a[j].w)
        {
            val=k+a[j].s;
            if(val*K<=a[p].e&&val*K>=a[p].s)
            {
                if(f[i+1][val*K-a[p].s][a[p].w]<f[i][k][a[j].w]+val*K)
                {
                    f[i+1][val*K-a[p].s][a[p].w]=f[i][k][a[j].w]+val*K;
                    fa[i+1][val*K-a[p].s][a[p].w].j=k;
                    fa[i+1][val*K-a[p].s][a[p].w].k=j;
                    ww[val*K-a[p].s][a[p].w]=a[p].id;
                }
            }
            if(val+K<=a[p].e&&val+K>=a[p].s)
            {
                if(f[i+1][val+K-a[p].s][a[p].w]<f[i][k][a[j].w]+val+K)
                {
                    f[i+1][val+K-a[p].s][a[p].w]=f[i][k][a[j].w]+val+K;
                    fa[i+1][val+K-a[p].s][a[p].w].j=k;
                    fa[i+1][val+K-a[p].s][a[p].w].k=j;
                    ww[val+K-a[p].s][a[p].w]=a[p].id;
                }
            }
        }
    }
    big ans=-1;
    int ii,jj;
    for(i=0;i<=100;i++)
    for(j=0;j<=100;j++)
    if(ans<f[n][i][j]&&f[n][i][j])
    {
        ii=i;
        jj=j;
        ans=f[n][i][j];
    }
    if(ans==-1)puts("NO");
    else
    {
        puts("YES");
        dfs(n,ii,jj);
        printf("%d %I64d\n",ww[ii][jj],ii+a[tt[ww[ii][jj]]].s);
    }
    return 0;
}

#include <vector>
#include <iterator>
#include <list>
#include <map>
#include <math.h>
#include <cmath>
#include <set>
#include <queue>
#include <deque>
#include <string>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <string.h>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cstdlib>
#include <ctime>
using namespace std;

vector<pair<long long,pair<long long,long long> > >v;
int dp[59][109][59],k;
pair<int,long long>print[59];
bool bt(int i,int val,int days)
{
    if(days==0)
    {
        print[i]=make_pair(0,val);
        return 1;
    }
    if(dp[i][val][days]!=-1)
        return dp[i][val][days];
    for(int f=i+1;f<v.size();f++)
        if(v[i].first<v[f].first&&
           (v[f].second.first<=(v[i].second.first+val)*k&&v[f].second.second>=(v[i].second.first+val)*k&&bt(f,(v[i].second.first+val)*k-v[f].second.first,days-1)
            ||v[f].second.first<=v[i].second.first+val+k&&v[f].second.second>=v[i].second.first+val+k&&bt(f,v[i].second.first+val+k-v[f].second.first,days-1)))
           {
               print[i]=make_pair(f,val);
               return dp[i][val][days]=1;
           }
    return dp[i][val][days]=0;
}

int main()
{
    int n,m;
    long long a,b,c;
    cin>>n>>m>>k;
    for(int f=0;f<m;f++)
    {
        cin>>a>>b>>c;
        v.push_back(make_pair(c,make_pair(a,b)));
    }
    sort(v.begin(),v.end());
    memset(dp,-1,sizeof dp);
    bool found=0;
    for(int f=0;!found&&f<m;f++)
    {
        for(int f1=0;!found&&f1<=v[f].second.second-v[f].second.first;f1++)
            if(bt(f,f1,m-1))
                found=1;
        if(found)
        {
            cout<<"YES\n";
            for(int f1=0;f1<n;f1++)
            {
                cout<<f+1<<" "<<print[f].second+v[f].second.first<<endl;
                f=print[f].first;
            }
            break;
        }
    }
    if(!found)
        cout<<"NO\n";
}

#include <bits/stdc++.h>
#define endl "\n"
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using tl = tuple<ll, ll, int, int>;

constexpr int N = 1e2+5;

ll a, b, c, i1, i2, ans=0;
pair<ll, pii> dp[N][N][N];
int n, m, k;
vector<pair<int, ll>> pr(1, pair<int, ll>());
tl arr[N];
pii val;

void Solve()
{
  cin >> n >> m >> k;
  for (int i = 1; i <= m; i++)
  {
    cin >> a >> b >> c;
    arr[i] = make_tuple(a, b, c, i);
  }
  sort(arr+1, arr+m+1, [&](const auto& a, const auto& b){return get<2>(a)<get<2>(b);});
  for (int i = 1; i<=m; i++)
  {
    auto [a, b, c, d] = arr[i];
    for (int j = 0; j <= b-a; j++)
    {
      for (int l = 1; l < i; l++)
      {
        i1 = a+j-k-get<0>(arr[l]);
        i2 = (a+j)%k==0?(a+j)/k-get<0>(arr[l]):-1;
        for (int z = 2; z <= n; z++)
        {
          if (i1>=0&&i1<100 && dp[l][i1][z-1].first>=dp[i][j][z].first)
            dp[i][j][z] = (pair<ll, pii>){dp[l][i1][z-1].first, {l, i1}};
          if (i2>=0&&i2<100 && dp[l][i2][z-1].first>=dp[i][j][z].first)
            dp[i][j][z] = (pair<ll, pii>){dp[l][i2][z-1].first, {l, i2}};
        }
      }
      for (int z = 2; z <= n; z++)
        if (dp[i][j][z].first!=0)
          dp[i][j][z].first+=a+j;
      dp[i][j][1] = {a+j, {0, 0}};
    }
  }
  for (int i = n; i<=m; i++)
  {
    for (int j = 0; j <= 100; j++)
    {
      if (ans < dp[i][j][n].first)
      {
        ans = dp[i][j][n].first;
        val = dp[i][j][n].second;
        pr[0] = {get<3>(arr[i]), get<0>(arr[i])+j};
      }
    }
  }
  if (ans==0)
  {
    cout << "NO";
    return;
  }
  while (pr.size()<n)
  {
    pr.emplace_back(get<3>(arr[val.first]), get<0>(arr[val.first])+val.second);
    val = dp[val.first][val.second][n-pr.size()+1].second;
  }
  cout << "YES" << endl;
  reverse(pr.begin(), pr.end());
  for (auto [i, v] : pr)
    cout << i << " " << v << endl;
}

int main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  Solve();
  return 0;
}

#pragma comment (linker, "/STACK:74000000")
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <memory>
#include <cctype>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
using namespace std;

#define FOR(i,a,b) for(int (i) = (a); (i) < (b); ++(i))
#define RFOR(i,a,b) for(int (i) = (a)-1; (i) >= (b); --(i))
#define CLEAR(a) memset((a),0,sizeof(a))
#define INF 1000000000
#define PB push_back
#define ALL(c) (c).begin(), (c).end()
#define pi 2*acos(0.0)
#define SQR(a) (a)*(a)
#define MP make_pair

typedef long long Int;

struct node
{
        Int a, b;
        int i, c;
};

struct parent
{
        int i, j, k;
        Int d;
};

int n, m, k;
vector <node> A;
Int DP[51][51][101];
parent P[51][51][101];

bool Cmp(node a, node b)
{
        return a.c < b.c;
}

int main()
{
        cin >> n >> m >> k;
        FOR (i,0,m)
        {
                node t;
                Int a, b;
                int c;
                cin >> a >> b >> c;
                t.a = a;
                t.b = b;
                t.c = c;
                t.i = i;
                A.PB(t);
        }
        sort(ALL(A), Cmp);
        FOR (i,0,51)
                FOR (j,0,51)
                        FOR (k,0,101)
                        {
                                DP[i][j][k] = -100000000000000000ll;
                                P[i][j][k].i = -1;
                        }

        FOR (i,0,A.size())
                for (Int j = A[i].a; j<=A[i].b; j++)
                {
                        P[1][i][j-A[i].a].i = 0;
                        P[1][i][j-A[i].a].d = j;
                        DP[1][i][j-A[i].a] = j;
                }

        FOR (i,1,n)
                FOR (j,0,A.size())
                {
                        for (Int l = A[j].a; l <= A[j].b; l++)
                        {
                                int d = l-A[j].a;
                                if (DP[i][j][d] < 0)
                                        continue;
                                FOR (t,j+1,A.size())
                                        if (A[t].c > A[j].c)
                                        {
                                                if (l+k >= A[t].a && l+k <= A[t].b)
                                                        if (DP[i+1][t][l+k-A[t].a] < DP[i][j][d]+l+k)
                                                        {
                                                                DP[i+1][t][l+k-A[t].a] = DP[i][j][d]+l+k;
                                                                P[i+1][t][l+k-A[t].a].i = i;
                                                                P[i+1][t][l+k-A[t].a].j = j;
                                                                P[i+1][t][l+k-A[t].a].k = d;
                                                                P[i+1][t][l+k-A[t].a].d = l+k;

                                                        }
                                                if (l*(Int)k >= A[t].a && l*(Int)k <= A[t].b)
                                                        if (DP[i+1][t][l*(Int)k-A[t].a] < DP[i][j][d]+l*(Int)k)
                                                        {
                                                                DP[i+1][t][l*(Int)k-A[t].a] = DP[i][j][d]+l*(Int)k;
                                                                P[i+1][t][l*(Int)k-A[t].a].i = i;
                                                                P[i+1][t][l*(Int)k-A[t].a].j = j;
                                                                P[i+1][t][l*(Int)k-A[t].a].k = d;
                                                                P[i+1][t][l*(Int)k-A[t].a].d = l*(Int)k;
                                                        }
                                        }
                        }
                }

        Int r = -100000000000000000ll;
        int x, y, z;

        FOR (i,0,A.size())
                FOR (j,0,101)
                        if (DP[n][i][j] > r)
                        {
                                r = DP[n][i][j];
                                x = n;
                                y = i;
                                z = j;
                        }
        if (r < 0)
        {
                cout << "NO" << endl;
                return 0;
        }
        cout << "YES" << endl;
        vector < pair<int, Int> > R;
        while (x > 0)
        {
                R.PB(MP(y+1, P[x][y][z].d));
                int xx, yy, zz;
                xx = P[x][y][z].i;
                yy = P[x][y][z].j;
                zz = P[x][y][z].k;
                x = xx;
                y = yy;
                z = zz;
        }
        reverse(ALL(R));
        FOR (i,0,R.size())
                cout << R[i].first << ' ' << R[i].second << endl;

        return 0;
}

#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <utility>
using namespace std;
#define N 55
#define D 105
#define DEBUG 0
long long c[N];
struct cmp{
   bool operator() (long long a, long long b){
      return c[a]<c[b];
   }
};
int main(){
   long long n,m,k;
   long long a[N],b[N],ind[N];
   long long dp[N][N][D][3];
   cin>>n>>m>>k;
   for(long long i=0; i<m; ++i){
      cin>>a[i]>>b[i]>>c[i];
      ind[i]=i;
   }
   sort(ind,ind+m,cmp());
   if(DEBUG)for(long long i=0; i<m; i++)cout<<ind[i]<<" ";
   if(DEBUG)cout<<endl;
   memset(dp,-1,sizeof(dp));
   for(long long i=0; i<m; i++){
      for(long long p=0; p<=(long long)(b[ind[i]]-a[ind[i]]); p++){
         dp[i][1][p][0]=a[ind[i]]+p;
         dp[i][1][p][1]=i;
         dp[i][1][p][2]=p;
      }
   }
   long long maxVal,maxInd,maxH;
   for(long long i=1; i<m; i++){
      for(long long j=2; j<=min(n,i+1); j++){
         for(long long p=0; p<=(long long)(b[ind[i]]-a[ind[i]]); p++){

            maxVal=-1;
            maxInd=-1;
            maxH=-1;
            long long cur=a[ind[i]]+p,req1=cur-k,req2=((cur%k)?-1:(cur/k));
            for(long long s=i-1; s>=0; s--){
               if(DEBUG)cout<<"hello "<<s<<" "<<req1<<" "<<req2<<" "<<a[ind[s]]<<" "<<b[ind[s]]<<" "<<dp[s][j-1][(long long)(req1-a[ind[s]])][0]<<endl;
               if(DEBUG)cout<<"MAXVAL "<<maxVal<<endl;
               if(req1>0 && req1<=b[ind[s]] && req1>=a[ind[s]]){
                  if(dp[s][j-1][(long long)(req1-a[ind[s]])][0]>maxVal){
                     maxInd=s;
                     maxVal=dp[s][j-1][(long long)(req1-a[ind[s]])][0];
                     maxH=(req1-a[ind[s]]);
                  }
               }
               if(req2>0 && req2<=b[ind[s]] && req2>=a[ind[s]]){
                  if(dp[s][j-1][(long long)(req2-a[ind[s]])][0]>maxVal){
                     maxInd=s;
                     maxVal=dp[s][j-1][(long long)(req2-a[ind[s]])][0];
                     maxH=(req2-a[ind[s]]);
                  }
               }
            }
            if(DEBUG)cout<<"FUCK "<<maxVal<<" "<<maxH<<endl;
            dp[i][j][p][0]=(maxVal!=-1)?(maxVal+cur):maxVal;
            dp[i][j][p][1]=maxInd;
            dp[i][j][p][2]=maxH;
            if(DEBUG)cout<<i<<" "<<j<<" "<<a[ind[i]]+p<<" "<<dp[i][j][p][0]<<endl;
         }
      }
   }
   maxVal=-1;maxInd=-1;maxH=-1;
   for(long long i=0; i<m; i++){
      for(long long j=0; j<=(b[ind[i]]-a[ind[i]]); j++)
         if(maxVal<dp[i][n][j][0]){
            maxVal=dp[i][n][j][0];
            maxInd=i;
            maxH=j;
         }
   }
   if(maxVal!=-1){
      long long j=n;
      vector< pair<long long,long long> > v;
      vector< pair<long long,long long> >::iterator it;
      cout<<"YES"<<endl;
      while(true){
         if(DEBUG)cout<<"mind mh "<<maxInd<<" "<<maxH<<endl;
         v.push_back(make_pair(ind[maxInd]+1,a[ind[maxInd]]+maxH));
         long long temp=maxInd;
         maxInd=dp[temp][j][maxH][1];
         maxH=dp[temp][j][maxH][2];
         j--;
         if(j==0)break;
      }

      for(it=--v.end(),j=n;j>0;it--,j--){
         cout<<(*it).first<<" "<<(*it).second<<endl;
      }
   }
   else{
      cout<<"NO"<<endl;
   }
   return 0;
}

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <map>
#include <vector>

using namespace std;

struct subject{
  long long a, b, c;
  int idx;
};

int N, M, TK;

subject s[64];
map < long long, int > mp[64];

long long dp[64][64][128];
pair < int, long long > path[64][64][128];

void scan(){
  cin >> N >> M >> TK;

  for ( int i = 0; i < M; ++i ){
    cin >> s[i].a >> s[i].b >> s[i].c;
    s[i].idx = i + 1;
  }
}

bool f ( subject t1, subject t2 ){
  return t1.c < t2.c;
}

void solve(){
  sort ( s, s + M, f );

  for ( int i = 0; i < M; ++i ){
    for ( long long j = 0; j <= s[i].b - s[i].a; ++j ){
      for ( int k = N; k >= 2; --k ){
    if ( mp[k - 1].count ( j + s[i].a - TK ) ){
      int idx = mp[k - 1][j + s[i].a - TK];

      if ( dp[i][j][k] < dp[idx][j + s[i].a - TK - s[idx].a][k - 1] + j + s[i].a ){
      dp[i][j][k] = dp[idx][j + s[i].a - TK - s[idx].a][k - 1] + j + s[i].a;

      path[i][j][k] = make_pair ( idx, j + s[i].a - TK );}
    }
    if ( ( j + s[i].a ) % TK == 0 && mp[k - 1].count ( ( j + s[i].a ) / TK ) ){
      int idx = mp[k - 1][(j + s[i].a ) / TK];

      if ( dp[i][j][k] < dp[idx][(j + s[i].a ) / TK - s[idx].a][k - 1] + j + s[i].a ){
      dp[i][j][k] = dp[idx][(j + s[i].a ) / TK - s[idx].a][k - 1] + j + s[i].a;

      path[i][j][k] = make_pair ( idx, ( j + s[i].a ) / TK );}
    }
      }
      dp[i][j][1] = j + s[i].a;
      path[i][j][1] = make_pair ( -1, -1 );
    }
    if ( s[i].c != s[i + 1].c ){
      for ( int ni = 0; ni < M; ++ni )
    if ( s[i].c == s[ni].c )
      for ( int j = 0; j <= s[ni].b - s[ni].a; ++j )
        for ( int k = N; k >= 1; --k )
          if ( dp[ni][j][k] ){
        if ( !mp[k].count ( j + s[ni].a ) )
          mp[k][j + s[ni].a] = ni;
        else{
          int idx = mp[k][j + s[ni].a];
          if ( dp[idx][j + s[ni].a - s[idx].a] < dp[ni][j] )
            mp[k][j + s[ni].a] = ni;
        }}
    }
  }

  int ri = -1;
  long long rj = -1;

  for ( int i = 0; i < M; ++i ){
    for ( long long j = s[i].a; j <= s[i].b; ++j ){
      if ( ( ri == -1 || rj == -1 ) ){
    if ( dp[i][j - s[i].a][N]   ){
    ri = i;
    rj = j;
    }
      }
      else if ( dp[ri][rj - s[ri].a ][N] < dp[i][j - s[i].a][N] && dp[ri][rj - s[ri].a][N] ){
    ri = i;
    rj = j;
      }
    }
  }

  if ( ri == -1 ){
    printf ( "NO\n" );
    return;
  }

  printf ( "YES\n" );

  int cur = N;

  vector < pair < int, long long > > out;

//  cout << ri << " " << rj << " " << dp[ri][rj - s[ri].a][cur] << endl;
  while ( ri != -1 ){
    out.push_back ( make_pair ( s[ ri].idx, rj ) );
    rj -= s[ ri ].a;

    //cout << ri << " " << rj << endl;
    pair < int, long long > tmp = path[ri][rj][cur--];

    ri = tmp.first;
    rj = tmp.second;
    if ( ri == 0 && rj == 0 ) break;

    //cout << ri << " " << rj << " " << dp[ri][rj - s[ri].a][cur] << endl;
  }
  for ( int i = out.size() - 1; i >= 0; --i )
    cout << out[i].first << " " << out[i].second << endl;

}
int main(){
  scan();
  solve();
}

using namespace std;

#include<ios>
#include<iomanip>
#include<cmath>
#include<algorithm>
#include<vector>
#include<set>
#include<string>
#include<map>
#include<queue>
#include<stack>
#include<utility>
#include<cstdio>
#include<cstdlib>
#include<cassert>
#include<numeric>
#include<cstring>
#include<sstream>
#include<algorithm>
#include<deque>

#define FILES
#undef FILES

#ifdef FILES
#include<fstream>
ifstream cin("date.in");
ofstream cout("date.out");
#else
#include<iostream>
#endif

#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define ALL(a) a.begin(),a.end()
#define p_b(a) push_back(a)
#define m_p(a,b) make_pair(a,b)
#define p_p(a,b) p_b(m_p(a,b))

typedef unsigned char uchar;
typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vint;
typedef queue<int> qint;
typedef pair<int,int> pii;
typedef vector<pii> vpii;
typedef vector<string> vstr;
typedef queue<int> qint;
typedef deque<int> dqint;
typedef deque<ll> dqll;
typedef map<string,int> mpstri;
typedef map<int,int> mpinti;
typedef map<string,string> mpstrs;
typedef map<string,vstr> mpstrvs;
const int INFI=(1<<30);
const ll INFL=(1LL<<62);

ull dp[60][60][112];
ull d[60][60][112][3];

struct sbj{
    ull a,b,c;
    int pzp;
};

bool cmp(const sbj& m1,const sbj& m2){
    return m1.c<m2.c;
}

int main(){
    sbj v[60];
    int n,m;
    ull k;
    cin>>n>>m>>k;

    for(int i=1;i<=m;i++){
        cin>>v[i].a>>v[i].b>>v[i].c;
        v[i].pzp=i;
    }

    sort(v+1,v+m+1,cmp);

    for(int i=1;i<=m;i++)
        for(ull j=v[i].a;j<=v[i].b;j++){
            dp[1][i][j-v[i].a]=j;
            d[1][i][j-v[i].a][0]=j;
            d[1][i][j-v[i].a][1]=0;
            d[1][i][j-v[i].a][2]=0;
        }

    for(int i=2;i<=n;i++)
        for(int l=1;l<=m;l++)
            for(int j=1;j<l;j++){
                if(v[j].c >=v[l].c) continue;
                for(ull q=v[j].a;q<=v[j].b;q++){
                    if(q*k>=v[l].a && q*k<=v[l].b && dp[i-1][j][q-v[j].a] && dp[i][l][-v[l].a+q*k] < dp[i-1][j][q-v[j].a]+q*k){
                        dp[i][l][-v[l].a+q*k]=dp[i-1][j][q-v[j].a]+q*k;
                        d[i][l][-v[l].a+q*k][0]=q*k;
                        d[i][l][-v[l].a+q*k][1]=j;
                        d[i][l][-v[l].a+q*k][2]=q-v[j].a;
                    }

                    else if(q+k>=v[l].a && q+k<=v[l].b && dp[i-1][j][q-v[j].a] && dp[i][l][-v[l].a+q+k] < dp[i-1][j][q-v[j].a]+q+k){
                        dp[i][l][-v[l].a+q+k]=dp[i-1][j][q-v[j].a]+q+k;
                        d[i][l][-v[l].a+q+k][0]=q+k;
                        d[i][l][-v[l].a+q+k][1]=j;
                        d[i][l][-v[l].a+q+k][2]=q-v[j].a;
                    }
                }
            }

    ull mx=0,pz2,pz3;
    for(int i=1;i<=m;i++)
        for(int q=0;q<=100;q++)
            if(dp[n][i][q]>mx){
                mx=dp[n][i][q];
                pz2=i;
                pz3=q;
            }

    if(!mx)
        cout<<"NO";
    else{
        cout<<"YES\n";
        vector<pair<ull,ull> > sl;
        sl.p_p(v[pz2].pzp,d[n][pz2][pz3][0]);
        ull ax2,ax3;
        ax2=d[n][pz2][pz3][1];
        ax3=d[n][pz2][pz3][2];

        for(int i=n-1;i>=1;i--){
            pz2=ax2; pz3=ax3;
            sl.p_p(v[pz2].pzp,d[i][pz2][pz3][0]);
            ax2=d[i][pz2][pz3][1];
            ax3=d[i][pz2][pz3][2];
        }
        for(int i=sl.size()-1;i>=0;i--)
            cout<<sl[i].first<<" "<<sl[i].second<<'\n';
    }

    return 0;
}

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

using namespace std;

struct lesson
{
    int c;
    long long a, b;
    void read()
    {
        cin >> a >> b >> c;
    }
    void print()
    {
        cout << a << " " << b << " " << c << "\n";
    }
};

struct lesson2
{
    int num;
    long long a;
};

bool comp(lesson h, lesson k)
{
    return h.c < k.c;
}

int main()
{
    int n, m, k;
    cin >> n >> m >> k;
    lesson les[100];
    for (int i = 0; i < m; i++)
    {
        les[i].read();
    }
    sort(les, les + m, comp);
    for (int i = 0; i <= m - n; i++)
    {
        for (int j = les[i].a; j <= les[i].b; j++)
        {
            int cnt = 0;
            vector <lesson2> vec;
            lesson2 ans1;
            ans1.num = i + 1;
            ans1.a = j;
            vec.push_back(ans1);
            int cur = j;
            for (int t = i + 1; t < m; t++)
            {
                bool mark = false;
                if (cur + k <= les[t].b && cur + k >= les[t].a)
                {
                    cnt++;
                    lesson2 ans2;
                    ans2.num = t + 1;
                    ans2.a = cur + k;
                    cur += k;
                    vec.push_back(ans2);
                    mark = true;
                }
                if (!mark && cur * k <= les[t].b && cur * k >= les[t].a)
                {
                    cnt++;
                    lesson2 ans2;
                    ans2.num = t + 1;
                    ans2.a = cur * k;
                    cur *= k;
                    vec.push_back(ans2);
                }
                if (cnt == n)
                {
                    cout << "YES\n";
                    for (int i = 0; i < cnt; i++)
                    {
                        cout << vec[i].num << " " << vec[i].a << "\n";
                    }
                    exit(0);
                }
            }
        }
    }
    cout << "NO";
    return 0;
}

#include <bits/stdc++.h>
#define endl "\n"
using namespace std;
using ll = long long;
using pii = pair<ll, ll>;
using tl = tuple<ll, ll, int, int>;

constexpr int N = 1e2+5;

ll a, b, c, ans = 0, val, cur;
int n, m, k, pred, cr;
bool f = true;
map<ll, ll> dp[N][N];
tl arr[N];
vector<pii> pr;

void Solve()
{
  cin >> n >> m >> k;
  for (int i = 1; i <= m; i++)
  {
    cin >> a >> b >> c;
    arr[i] = make_tuple(a, b, c, i);
  }
  sort(arr+1, arr+m+1, [&](const auto& a, const auto& b){return get<2>(a)<get<2>(b);});
  for (int i = 1; i <= m;)
  {
    pred = i-1;
    cr = i;
    f = true;
    while(cr<=m&&get<2>(arr[i])==get<2>(arr[cr]))
    {
      auto [a, b, c, id] = arr[cr];
      for (ll j = a; j <= b; j++)
        dp[i][1][j] = j;
      for (int j = 2; j <= min(i, n); j++)
      {
        for (auto [ck, ans] : dp[pred][j-1])
        {
          if (a<=ck+k && b>=ck+k)
            dp[i][j][ck+k] = max(dp[i][j][ck+k], ans+ck+k);
          if (a<=ck*k && b>=ck*k)
            dp[i][j][k*ck] = max(dp[i][j][k*ck], ans+ck*k);
        }
      }
      for (int j = 1; j <= min(cr, n); j++)
        for (auto [ck, ans] : dp[cr-1][j])
            dp[cr][j][ck] = max(dp[cr][j][ck], ans);
      cr++;
    }
    i = cr;
  }
  for (auto [el, an] : dp[m][n])
    if (ans<an)
    {
      ans = an;
      val = el;
    }
  if (ans==0)
  {
    cout << "NO";
    return;
  }
  cur = m;
  while (pr.size()<n&&cur>0)
  {
    auto [a, b, c, id] = arr[cur];
    if (a<=val && val<=b)
    {
      ans-=val;
      pr.emplace_back(id, val);
      if (dp[cur-1][n-pr.size()][val-k]==ans)
      {
        while(cur>2&&dp[cur-2][n-pr.size()][val-k]==ans)
          cur--;
        val-=k;
      }
      else
      {
        while(cur>2&&dp[cur-2][n-pr.size()][val/k]==ans)
          cur--;
        val/=k;
      }
    }
    cur--;
  }
  cout << "YES" << endl;
  reverse(pr.begin(), pr.end());
  for (auto [i, v] : pr)
    cout << i << " " << v << endl;
}

int main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  Solve();
  return 0;
}

// Friendship is Magic
#pragma comment(linker, "/STACK:32000000")

#include <memory.h>
#include <string.h>
#include <cstdio>
#include <cmath>
#include <iostream>
#include <sstream>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>

using namespace std;

typedef long long LL;

struct subj
{
    LL a, b;
    int c;
    int num;
    friend bool operator < (const subj &x, const subj &y)
    {
        return x.c < y.c;
    }
};

int n, m, K;
subj a[55];
int diff[55];
bool dp[55][55][105];
int from_cnt[55][55][105];
int from_subj[55][55][105];

void do_solve() {
    cin >> n >> m >> K;
    for (int i = 0; i < m; i++)
    {
        cin >> a[i].a >> a[i].b >> a[i].c;
        a[i].num = i+1;
    }
    sort(a, a+m);
    for (int i = 0; i < m; i++)
        diff[i] = a[i].b - a[i].a;
    for (int i = 0; i < m; i++)
        for (int j = 0; j <= diff[i]; j++)
            dp[0][i][j] = true;
    for (int i = 0; i + 1 < n; i++)
        for (int j = 0; j < m; j++)
            for (int k = 0; k <= diff[j]; k++)
                if (dp[i][j][k])
                {
                    for (int z = j + 1; z < m; z++) if (a[z].c > a[j].c)
                    {
                        // +k
                        LL x = a[j].a + k + K;
                        if (x >= a[z].a && x <= a[z].b)
                        {
                            dp[i+1][z][x - a[z].a] = true;
                            from_subj[i+1][z][x - a[z].a] = j;
                            from_cnt[i+1][z][x - a[z].a] = k;

                        }
                        // *k
                        x = (a[j].a + k) * K;
                        if (x >= a[z].a && x <= a[z].b)
                        {
                            dp[i+1][z][x - a[z].a] = true;
                            from_subj[i+1][z][x - a[z].a] = j;
                            from_cnt[i+1][z][x - a[z].a] = k;
                        }
                    }
                }
    LL best = 0;
    int lastsubj = -1;
    for (int i = 0; i < m; i++)
        for (int j = 0; j <= diff[i]; j++)
            if (dp[n-1][i][j])
            {
                LL x = a[i].a + j;
                if (x > best)
                {
                    best = x;
                    lastsubj = i;
                }
            }
    if (lastsubj == -1)
    {
        cout << "NO";
        return;
    }
    cout << "YES" << endl;
    vector<LL> cts;
    vector<int> subjs;
    for (int i = n-1; i >= 0; i--)
    {
        cts.push_back(best);
        subjs.push_back(lastsubj);
        int t = from_subj[i][lastsubj][best - a[lastsubj].a];
        best = from_cnt[i][lastsubj][best - a[lastsubj].a] + a[t].a;
        lastsubj = t;
    }
    for (int i = n-1; i >= 0; i--) cout << a[subjs[i]].num << " " << cts[i] << endl;
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    do_solve();
    return 0;
}

#include<bits/stdc++.h>

#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define ba back
#define ppb pop_back
#define eb emplace_back

#define sz(x) (int((x.size())))
#define all(x) (x).begin(),(x).end()

#define db(x)        cerr << #x << " = " << x << endl
#define db2(x, y)   {cerr << #x << " = " << x << "\t";  db(y);}
#define db3(x, y, z){cerr << #x << " = " << x << "\t";  db2(y,z);}

#define si(a) scanf("%d", &a)
#define sii(a, b) scanf("%d%d", &a, &b)
#define siii(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define sl(a) scanf("%I64d", &a)
#define sll(a, b) scanf("%I64d%I64d", &a, &b)
#define slll(a, b, c) scanf("%I64d%I64d%I64d", &a, &b, &c)

#define bye exit(0)
#define FAST ios_base :: sync_with_stdio (false); cin.tie (NULL)

using namespace std;

typedef long long ll;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ii> vii;

const ll MOD = 1e9 + 7;

ll add(ll x, ll y, ll CMOD=MOD){return (x+y+CMOD+CMOD)%CMOD;}
ll mult(ll x, ll y, ll CMOD=MOD){return add(x, 0)*add(y, 0) % CMOD;}

ll fast_expo(ll x, ll y, ll CMOD=MOD){
    if(x==0) return 0;
    if(y==0) return 1;
    ll ans = fast_expo(x, y/2, CMOD);
    ans = mult(ans, ans, CMOD);
    if(y&1) ans = mult(ans, x, CMOD);
    return ans;
}

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

ll gen(ll x, ll y){
    if(x==y) return x;
    return x + rng()%(y-x+1);
}

const int TAM = 1e5 + 100;
const ll INF = LLONG_MAX/4;

int n, m, k;
vector<pair<ii, pair<ll, ll> >>v;

bool vis[55][55][105];
ll memo[55][55][105];
ii e[55][55][105];

ll dp(int day, int sub, int up){
    if(day == 1) return v[sub].se.fi + up;
    if(sub == m) return LLONG_MIN/4;
    if(vis[day][sub][up]) return memo[day][sub][up];
    vis[day][sub][up] = 1;
    long long cur = v[sub].se.fi + up;
    int c_cur = v[sub].fi.fi;

    ll ans = LLONG_MIN/4;

    for(int sub2=sub+1; sub2<m; sub2++){
        ll a = v[sub2].se.fi;
        ll b = v[sub2].se.se;
        int c = v[sub2].fi.fi;
        if(c <= c_cur) continue;
        if(cur+k>=a and cur+k<=b and ans<dp(day-1, sub2, cur+k-a)+cur){
            ans = dp(day-1, sub2, cur+k-a)+cur;
            e[day][sub][up] = {sub2, cur+k-a};
        }
        if(cur*k>=a and cur*k<=b and ans<dp(day-1, sub2, cur*k-a)+cur){
            ans = dp(day-1, sub2, cur*k-a);
            e[day][sub][up] = {sub2, cur*k-a};
        }
    }
    return memo[day][sub][up] = ans;
}

int main(){
    FAST;
    cin >> n >> m >> k;

    for(int i=0; i<m; i++){
        int c;
        ll a, b;
        cin >> a >> b >> c;
        v.pb({{c, i+1}, {a, b}});
    }
    sort(all(v));

    ll ans = LLONG_MIN/4;
    int sub = -1;
    int up = -1;
    for(int i=0; i<m; i++){
        ll a = v[i].se.fi;
        ll b = v[i].se.se;
        for(int j=0; j<=b-a; j++){
            if(dp(n, i, j) > ans){
                sub = i;
                up = j;
                ans = dp(n, i, j);
            }
        }
    }

    if(ans < 0) puts("NO");
    else{
        puts("YES");
        for(int day = n; day > 0 ; day--){
            cout << v[sub].fi.se << " " << v[sub].se.fi+up << endl;
            ii e_cur = e[day][sub][up];
            sub = e_cur.fi;
            up = e_cur.se;
        }
    }

    return 0;
}

/*
 * =====================================================================================
 *
 *       Filename:  cf_r90c.cpp
 *
 *    Description:
 *
 *        Version:  1.0
 *        Created:  10/14/2011 11:57:37 PM
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  YOUR NAME (),
 *        Company:
 *
 * =====================================================================================
 */

#include	<stdlib.h>
#include	<stdio.h>
#include	<string.h>
#include	<math.h>
#include<algorithm>
using namespace std;
#define FF(i,b) for(i=0;i<(b);i++)
#define FOR(i,a,b) for(i=(a);i<=(b);i++)
#define ZERO(x) do{memset(x,0,sizeof(x));}while(0)
#define SWAP(a,b) do{int _tmp=a;a=b;b=_tmp;}while(0)
#define INFI 2000000000
#define INFF 1e100
typedef long long  LL;
struct TT{LL a,b,c;}t[60],p[60],bb[60];
int n,m,k;
LL best=-1;
void dfs(int now,int cur,LL e,LL c,LL sum)
{
	if(now>=n){
		if(sum>best){
			best=sum;
			for(int i=0;i<n;i++)bb[i]=p[i];
		}
		return;
	}
	for(int i=cur;i<m;i++)
	{
		if(t[i].c<=c)continue;
		if(e+k>=t[i].a&&e+k<=t[i].b){
			p[now].a=i;p[now].b=e+k;
			dfs(now+1,i+1,e+k,t[i].c,sum+e+k);
		}
		if(e*k<=t[i].b&&e*k>=t[i].a){
			p[now].a=i;p[now].b=e*k;
			dfs(now+1,i+1,e*k,t[i].c,sum+e*k);
		}
	}
}
bool cmp(const TT &a,const TT &b)
{
	if(a.c!=b.c)return a.c<b.c;
	return a.b<b.b;
}
int main ( int argc, char *argv[] )
{
	scanf("%d%d%d",&n,&m,&k);
	for(int i=0;i<m;i++)
		scanf("%I64d%I64d%I64d",&t[i].a,&t[i].b,&t[i].c);
	sort(t,t+m,cmp);
	for(int i=0;i+n<m;i++)
		for(LL j=t[i].a;j<=t[i].b;j++){
			p[0].a=i;p[0].b=j;
			dfs(1,i+1,j,t[i].c,j);
		}
	if(best>0){
		puts("YES");
		for(int i=0;i<n;i++)printf("%I64d %I64d\n",bb[i].a+1,bb[i].b);
	}
	else puts("NO");
	return EXIT_SUCCESS;
}				/* ----------  end of function main  ---------- */

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<cmath>
#include<algorithm>
using namespace std;

#define F(i,n) for(i=0;i<n;i++)
#define FF(i,n) for(i=1;i<=n;i++)
#define LL long long
#define inf (1LL<<62)

#define update(a, b, c, d) if(a<b) {(a) = (b); (c)  = (d);}

struct node{
    LL a, b, c;
}b[100];

LL a[55][55][105];  //[day][course][exe]
int n, m, k;
int p[55][55][105]; // course*1000 + exe
int ansu[55], ansv[55];

int main(){
    int i, j, y, nj;
    LL x,nx;
    while(scanf("%d%d%d",&n,&m,&k)!=EOF){
        F(i,m) scanf("%lld%lld%lld",&b[i].a, &b[i].b, &b[i].c);
        F(i,n+1)F(j,m+1)F(x,101) a[i][j][x] = -inf;
        F(j, m)F(x, b[j].b-b[j].a+1) a[1][j][x] = b[j].a + x;
        for(i=1;i<n;i++)F(j,m)F(x, b[j].b-b[j].a+1)if(a[i][j][x]>0){
            F(nj, m)if(b[nj].c>b[j].c){
                nx = b[j].a + x + k - b[nj].a;
                if(nx>=0&&nx<b[nj].b-b[nj].a+1)
                      update(a[i+1][nj][nx], a[i][j][x]+b[nj].a+x,  p[i+1][nj][nx], j*1000 + x);
                nx = (b[j].a + x)*k - b[nj].a;
                if(nx>=0&&nx<b[nj].b-b[nj].a+1)
                      update(a[i+1][nj][nx], a[i][j][x]+b[nj].a+x,  p[i+1][nj][nx], j*1000 + x);
            }
        }
        LL ans = 0;
        int u, v;
        F(j,m)F(x, b[j].b-b[j].a+1)if(ans<a[n][j][x]) ans = a[n][j][x], u = j, v = x;
        if(ans==0) printf("NO\n");
        else {
            printf("YES\n");
            for(i=n;i>=1;i--){
                ansu[i] = u, ansv[i] = v;
                x = p[i][u][v]/1000, y = p[i][u][v]%1000;
                u = x, v = y;
            }
            FF(i,n) printf("%d %lld\n", ansu[i]+1, ansv[i]+b[ansu[i]].a);
        }
    }
    return 0;
}

#include<iostream>
#include<iomanip>
#include<string>
#include<cstring>
#include<stdio.h>
#include<algorithm>
#include<vector>
#include<map>
#include<queue>
#include<unordered_map>

#define FOR(i, a, b) for( LL i = (a); i < (b); i++ )
#define ALL(A) (A.begin(), A.end())
#define Set(a, s) memset(a, s, sizeof (a))
#define pb push_back
#define mp make_pair
typedef long long LL;
using namespace std;

//the timetable should contain the subjects
//  in the order of the complexity's strict increasing;
//each day, except for the first one, the task should contain
//either k times more exercises, or more by k compared to the previous day
//(more formally: let's call the number of home task exercises in the i-th day as xi,
//then for each i (1 < i ≤ n): either xi = k + xi - 1 or xi = k·xi - 1 must be true);
//the total number of exercises in all home tasks should be maximal possible.

int n,m,k;
pair< pair<int,int> , pair<LL,LL> > arr[59];
unordered_map<LL,LL> dp[59][59];
LL const oo = 1e17;

LL solve(int c , int sb, LL p)
{
    if(c == n)
        return 0;
    if(sb==m)
        return -oo;

    if(dp[c][sb].find(p) != dp[c][sb].end())
        return dp[c][sb][p];
    LL maxi = -oo;
    maxi  = max(maxi , solve(c , sb+1 , p));
    if(p>=arr[sb].second.first && p<=arr[sb].second.second)
    {
        maxi  = max(maxi , solve(c+1 , sb+1 ,p*k) + p);
        maxi  = max(maxi , solve(c+1 , sb+1 ,p+k) + p);
    }

    return dp[c][sb][p] = maxi;
}

vector< pair<int,LL> >V;

void print(int c , int sb, LL p)
{
    if(c == n)
        return;
    if(sb==m)
        return ;
    LL opt = dp[c][sb][p];
    if(p>=arr[sb].second.first && p<=arr[sb].second.second)
    {
        if (opt == solve(c+1 , sb+1 ,p*k) + p)
        {
            V.push_back(mp(arr[sb].first.second , p));
            print(c+1 , sb+1 ,p*k);
        }
        else if (opt == solve(c+1 , sb+1 ,p+k) + p)
        {
            V.push_back(mp(arr[sb].first.second , p));
            print(c+1 , sb+1 ,p+k);
        }
    }
    else if (opt == solve(c , sb+1 ,p ))
        print(c , sb+1 , p);
}

int main()
{
  //  freopen("input.in", "r" , stdin);

    while(cin>>n>>m>>k)
    {
        FOR(i,0,n+1)
            FOR(j,0,m+1)
            dp[i][j].clear();

        FOR(i,0,m)
        {
            cin>>arr[i].second.first>>arr[i].second.second>>arr[i].first.first;
            arr[i].first.second = i;
        }
        sort(arr,arr+m);
        //reverse(arr,arr+m);
        LL maxi = -oo;
        FOR(i,0,m)
        {

            FOR(j, arr[i].second.first,arr[i].second.second+1)
            {
                /*FOR(q,0,n+1)
                FOR(w,0,m+1)
                dp[q][w].clear();
                */
                maxi = max(maxi , solve(1 , i+1,k*j)+j);
                maxi = max(maxi , solve(1 , i+1,k+j)+j);
            }
        }

        cout << maxi <<endl;
        if(maxi>=0)
        {
            cout << "YES" <<endl;

            bool b = 0;
            FOR(i,0,m)
            {
                FOR(j, arr[i].second.first,arr[i].second.second+1)
                {
                    if(maxi == solve(1 , i+1,k*j)+j )
                    {
                        V.push_back(mp(arr[i].first.second , j));
                        print(1 , i+1,k*j);
                        b=1;
                        break;
                    }
                    else if(maxi == solve(1 , i+1,k+j)+j)
                    {
                        V.push_back(mp(arr[i].first.second , j));
                        print(1 , i+1,k+j);
                        b=1;
                        break;
                    }
                }
                if(b)break;
            }
            FOR(i,0,V.size())
                cout << V[i].first+1 << " " << V[i].second <<endl;
        }
        else
            cout << "NO" <<endl;
        //cout<<endl;
        V.clear();
    }
}

//#include<iostream>
//#include<iomanip>
//#include<string>
//#include<cstring>
//#include<stdio.h>
//#include<algorithm>
//#include<vector>
//#include<map>
//#include<queue>
//#include<unordered_map>
//
//#define FOR(i, a, b) for( int i = (a); i < (b); i++ )
//#define ALL(A) (A.begin(), A.end())
//#define Set(a, s) memset(a, s, sizeof (a))
//#define pb push_back
//#define mp make_pair
//typedef long long LL;
//using namespace std;
//
////the timetable should contain the subjects
////    in the order of the complexity's strict increasing;
////each day, except for the first one, the task should contain
////either k times more exercises, or more by k compared to the previous day
////(more formally: let's call the number of home task exercises in the i-th day as xi,
////then for each i (1 < i ≤ n): either xi = k + xi - 1 or xi = k·xi - 1 must be true);
////the total number of exercises in all home tasks should be maximal possible.
//
//
//int n,m,k;
//pair< pair<int,int> , pair<LL,LL> > arr[59];
//LL dp[52][52][102];
//LL const oo = 1e17;
//
//LL solve(int c , int sb, LL p)
//{
//  if(c==n)
//      return 0;
//  if(sb==m)
//      return -oo;
//
//  //if(dp[c][sb][p-arr[sb].second.first] != -1)
//      //return dp[c][sb][(p-arr[sb].second.first)];
//  LL maxi = -oo;
//  //maxi  = max(maxi , solve(c , sb+1 , p));
////    if(p>=arr[sb].second.first && p<=arr[sb].second.second)
//  {
//      FOR(q,sb+1,m+1)
//      {
//          if(p*k>=arr[q].second.first && p*k<=arr[q].second.second)
//              maxi  = max(maxi , solve(c+1 , q ,p*k) + p);
//          if(p+k>=arr[q].second.first && p+k<=arr[q].second.second)
//              maxi  = max(maxi , solve(c+1 , q ,p+k) + p);
//      }
//  }
//  return dp[c][sb][(p-arr[sb].second.first)] = maxi;
//}
//
//vector< pair<int,LL> >V;
//
//void print(int c , int sb, LL p)
//{
//  if(c == n)
//      return;
//  if(sb==m)
//      return ;
//  LL opt = solve(c,sb,p);
//  if(p>=arr[sb].second.first && p<=arr[sb].second.second)
//  {
//      FOR(q,sb+1,m+1)
//      {
//          if(p*k>=arr[q].second.first && p*k<=arr[q].second.second)
//          {
//              if (opt == solve(c+1 , q ,p*k) + p)
//              {
//                  V.push_back(mp(arr[sb].first.second , p));
//                  print(c+1 , q ,p*k);
//                  break;
//              }
//          }
//          if(p+k>=arr[q].second.first && p+k<=arr[q].second.second)
//          {
//              if (opt == solve(c+1 , sb+1 ,p+k) + p)
//              {
//                  V.push_back(mp(arr[sb].first.second , p));
//                  print(c+1 , q ,p+k);
//                  break;
//              }
//          }
//
//      }
//  }
//  else if (opt == solve(c , sb+1 ,p ))
//      print(c , sb+1 , p);
//}
//
//int main()
//{
//  freopen("input.in", "r" , stdin);
//
//  while(cin>>n>>m>>k)
//  {
//      //FOR(i,0,n+1)
//      //  FOR(j,0,m+1)
//      //  dp[i][j].clear();
//      Set(dp,-1);
//      FOR(i,0,m)
//      {
//          cin>>arr[i].second.first>>arr[i].second.second>>arr[i].first.first;
//          arr[i].first.second = i;
//      }
//      sort(arr,arr+m);
//      //reverse(arr,arr+m);
//      LL maxi = -oo;
//      FOR(i,0,m)
//          FOR(q,i+1,m)
//      {
//          FOR(j, arr[i].second.first,arr[i].second.second+1)
//          {
//              if(j*k>=arr[q].second.first && j*k<=arr[q].second.second)
//              maxi = max(maxi , solve(1 , q,k*j)+j);
//              if(j+k>=arr[q].second.first && j+k<=arr[q].second.second)
//              maxi = max(maxi , solve(1 , q,k+j)+j);
//
//          }
//      }
//      cout << maxi <<endl;
//      if(maxi>=0)
//      {
//          cout << "YES" <<endl;
//
//          bool b = 0;
//          FOR(i,0,m)
//          {
//              FOR(j, arr[i].second.first,arr[i].second.second+1)
//              {
//                  if(maxi == solve(1 , i+1,k*j)+j )
//                  {
//                      V.push_back(mp(arr[i].first.second , j));
//                      print(1 , i+1,k*j);
//                      b=1;
//                      break;
//                  }
//                  else if(maxi == solve(1 , i+1,k+j)+j)
//                  {
//                      V.push_back(mp(arr[i].first.second , j));
//                      print(1 , i+1,k+j);
//                      b=1;
//                      break;
//                  }
//              }
//              if(b)break;
//          }
//          FOR(i,0,V.size())
//              cout << V[i].first+1 << " " << V[i].second <<endl;
//      }
//      else
//          cout << "NO" <<endl;
//      V.clear();
//  }
//}

#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <set>
#include <vector>
#include <algorithm>
#include <cstring>

#define repn(i,a,b) for(int i=(a),_b=(b);i<=_b;++i)
#define pern(i,a,b) for(int i=(a),_b=(b);i>=_b;--i)
#define rep(i,a,b) for(int i=(a),_b=(b);i<_b;++i)
#define forit(i,v) for(__typeof((v).begin()) i=(v).begin();i!=(v).end();++i)
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define sz(x) int((x).size())

using namespace std;

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vii;
typedef long long ll;
typedef unsigned long long ull;

struct subj{
    ll a, b;
    int c, id, d;
    bool operator < (const subj &other) const{
        return c < other.c;
    }
};

ll f[55][55][111];
subj p[55];
pii tr[55][111];
int n, m, k;

int main(){
    freopen("input.inp", "r", stdin);
    freopen("output.out", "w", stdout);
    scanf("%d%d%d",&n,&m,&k);
    repn(i,1,m){
        scanf("%I64d%I64d%d",&p[i].a,&p[i].b,&p[i].c);
        p[i].d = p[i].b - p[i].a;
        p[i].id = i;
    }
    sort(p + 1, p + 1 + m);
    repn(i,1,m-n+1)
        repn(z,0,p[i].d)
            f[1][i][z] = z + p[i].a;
    rep(i,1,n){
        repn(j,i,min(i+m-n, m)){
            repn(z,0,p[j].d){
                if (f[i][j][z]){
                    rep(h,0,2){
                        ll t;
                        if (h == 0) t = (z + p[j].a) * k; else t = (z + p[j].a) + k;
                        repn(x,j+1,min(i+m-n+1, m)){
                            if (p[x].c > p[j].c && p[x].a <= t && p[x].b >= t && f[i + 1][x][t - p[x].a] < f[i][j][z] + t){
                                f[i + 1][x][t - p[x].a] = f[i][j][z] + t;
                                tr[x][t - p[x].a] = pii(j, z);
                            }
                        }
                    }
                }
            }
        }
    }

    int x, y;
    ll ans = 0;
    repn(j,n,m)
        repn(z,0,p[j].d){
            if (ans < f[n][j][z]){
                ans = f[n][j][z];
                x = j;
                y = z;
            }
        }
    vii pa;
    //cerr<<ans;
    if (ans == 0) printf("NO");
    else{
        printf("YES\n");
        repn(t,1,n){
            int x2 = tr[x][y].fi, y2 = tr[x][y].se;
            pa.pb(pii(x, y));
            x = x2; y = y2;
        }
        pern(i,n-1,0){
            printf("%d %d\n",p[pa[i].fi].id,p[pa[i].fi].a + pa[i].se);
        }
    }
    return 0;
}

#include <bits/stdc++.h>
#define endl "\n"
using namespace std;
using ll = long long;
using pii = pair<ll, ll>;
using tl = tuple<ll, ll, int, int>;

constexpr int N = 1e2+5;

ll a, b, c, ans = 0, val, cur;
int n, m, k, pred, cr;
bool f = true;
map<ll, pair<ll, ll>> dp[N][N];
tl arr[N];
vector<pii> pr;

void Solve()
{
  cin >> n >> m >> k;
  for (int i = 1; i <= m; i++)
  {
    cin >> a >> b >> c;
    arr[i] = make_tuple(a, b, c, i);
  }
  sort(arr+1, arr+m+1, [&](const auto& a, const auto& b){return get<2>(a)<get<2>(b);});
  for (int i = 1; i <= m;)
  {
    pred = i-1;
    cr = i;
    f = true;
    while(cr<=m&&get<2>(arr[i])==get<2>(arr[cr]))
    {
      auto [a, b, c, id] = arr[cr];
      for (ll j = a; j <= b; j++)
        dp[i][1][j] = {j, 0};
      for (int j = 2; j <= min(i, n); j++)
      {
        for (auto [ck, ans] : dp[pred][j-1])
        {
          if (a<=ck+k && b>=ck+k)
            dp[cr][j][ck+k] = {ans.first+ck+k, ck};
          if (a<=ck*k && b>=ck*k)
            dp[cr][j][ck*k] = {ans.first+ck*k, ck};
        }
      }
      for (int j = 1; j <= min(cr, n); j++)
        for (auto [ck, ans] : dp[cr-1][j])
          if (dp[cr][j][ck].first<=ans.first)
            dp[cr][j][ck] = ans;
      cr++;
    }
    i = cr;
  }
  for (auto [el, an] : dp[m][n])
    if (ans<=an.first)
    {
      ans = an.first;
      val = el;
    }
  if (ans==0)
  {
    cout << "NO";
    return;
  }
  cur = m;
  while (pr.size()<n)
  {
    auto [a, b, c, id] = arr[cur];
    if (a<=val && val<=b)
    {
      ans-=val;
      pr.emplace_back(id, val);
      val = dp[cur][n-pr.size()+1][val].second;
    }
    cur--;
  }
  cout << "YES" << endl;
  reverse(pr.begin(), pr.end());
  for (auto [i, v] : pr)
    cout << i << " " << v << endl;
}

int main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  Solve();
  return 0;
}

/* December 25th 2019 */
/* Merry Christmas! */
/* Real writing started on 28th Dec */

#include <cstdio>
#include <map>
#include <vector>
#include <algorithm>

#define ll long long
#define NEG -(1e18 + 1)

bool comp(std::vector<ll> &a, std::vector<ll> &b) {
    return a[3] < b[3];
}

int main(void) {
    ll n, m, k; scanf("%lld %lld %lld", &n, &m, &k);

    /* idx, a, b, c */
    std::vector<std::vector<ll>> subjects;
    for (ll i = 0; i < m; ++i) {
        ll a, b, c; scanf("%lld %lld %lld", &a, &b, &c);
        subjects.push_back({i, a, b, c});
    }

    std::sort(subjects.begin(), subjects.end(), comp);
    /* subject num, day number, ht */
    ll dp[m + 1][n + 1][101];
    ll s_sn[m + 1][n + 1][101];
    ll s_ht[m + 1][n + 1][101];
    for (ll i = 0; i <= m; ++i) for (ll j = 0; j <= n; ++j) for (ll l = 0; l <= 100; ++l) {
        dp[i][j][l] = NEG;
        s_sn[i][j][l] = -1;
        s_ht[i][j][l] = -1;
    }
    for (ll i = 1; i <= m; ++i)
        for (ll j = 1; j <= n; ++j)
            for (ll ht = 0; (ht + subjects[i - 1][1]) <= (subjects[i - 1][2]); ++ht) {
                if (j == 1) {
                    dp[i][j][ht] = ht + subjects[i - 1][1];
                    continue;
                }

                ll pos1 = subjects[i - 1][1] + ht;
                ll pos2 = -1;
                if ((pos1 % k) == 0) pos2 = pos1 / k;
                pos1 -= k;
                for (ll ls = 1; ls < i; ++ls) {
                    if (subjects[ls - 1][3] == subjects[i - 1][3]) continue;

                    if (pos1 >= subjects[ls - 1][1] && pos1 <= subjects[ls - 1][2]) {
                        ll ta = ht + dp[ls][j - 1][pos1 - subjects[ls - 1][1]];
                        if (ta > dp[i][j][ht]) {
                            dp[i][j][ht] = ta;
                            s_sn[i][j][ht] = ls;
                            s_ht[i][j][ht] = pos1 - subjects[ls - 1][1];
                        }
                    }

                    if (pos2 >= subjects[ls - 1][1] && pos2 <= subjects[ls - 1][2]) {
                        ll ta = ht + dp[ls][j - 1][pos2 - subjects[ls - 1][1]];
                        if (ta > dp[i][j][ht]) {
                            dp[i][j][ht] = ta;
                            s_sn[i][j][ht] = ls;
                            s_ht[i][j][ht] = pos2 - subjects[ls - 1][1];
                        }
                    }
                }
            }

    ll ansm = -1;
    ll sn, htm;
    for (ll i = 1; i <= m; ++i) for (ll ht = 0; ht <= 100; ++ht)
        if (dp[i][n][ht] > ansm) {
            ansm = dp[i][n][ht];
            sn = i;
            htm = ht;
        }

    if (ansm == -1) puts("NO");
    else {
        puts("YES");
        std::vector<std::pair<ll, ll>> ans(n, std::make_pair(-123, -123));
        ans[n - 1] = std::make_pair(subjects[sn - 1][0] + 1, htm + subjects[sn - 1][1]);
        for (ll i = n - 2; i > -1; --i) {
            ll nsn = s_sn[sn][i + 2][htm];
            ll nhtm = s_ht[sn][i + 2][htm];
            sn = nsn; htm = nhtm;

            ans[i] = std::make_pair(subjects[sn - 1][0] + 1, htm + subjects[sn - 1][1]);
        }

        for (auto &each: ans) printf("%lld %lld\n", each.first, each.second);
    }
    return 0;
}

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
#include<utility>
#include<map>
using namespace std;
#define max(x,y) (((x)>(y))?(x):(y))
#define min(x,y) (((x)<(y))?(x):(y))
#define abs(x) (((x)>0)?(x):(-(x)))
#define INF (1<<29)
#define eps (1e-8)
#define ll long long

struct map_node{
    ll ex;
    int id,c,day;

    bool operator<(const map_node &d)const{
        if (id!=d.id) return id<d.id;
        else if (c!=d.c) return c<d.c;
        else if (day!=d.day) return day<d.day;
        else return ex<d.ex;
    }

    map_node(){}
    map_node(int id,int c,ll ex,int day):id(id),c(c),ex(ex),day(day){}
};

struct node{
    ll ex;
    int id,c,day;

    bool operator<(const node &d)const{
        return ex<d.ex;
    }

    node(){}
    node(int id,int c,ll ex,int day):id(id),c(c),ex(ex),day(day){}
    node(map_node d){
        id=d.id; c=d.c; day=d.day; ex=d.ex;
    }
};

map_node con(node d){
    map_node tmp;
    tmp.id=d.id; tmp.c=d.c; tmp.day=d.day; tmp.ex=d.ex;
    return tmp;
}

struct node2{
    ll a,b; int c,id;

    void input(int x){
        scanf("%I64d%I64d%d",&a,&b,&c);
        id=x;
    }

    bool operator<(const node2 &d)const{
        return c<d.c;
    }
} sub[105];

int n,m,k;
map<map_node,map_node> par;
map<map_node,bool> visit;
priority_queue<node> q;

int main(){
    scanf("%d%d%d",&n,&m,&k);
    for (int i=0;i<m;++i){
        sub[i].input(i);
        for (ll j=sub[i].a;j<=sub[i].b;++j) q.push(node(i,sub[i].c,j,1));
    }

    map_node ans; ans.id=-1;
    while (!q.empty()){
        node tmp=q.top(); q.pop();
        map_node tmp2=con(tmp);
        if (visit.find(tmp2)==visit.end()){
            visit[tmp2]=1;
            //printf("%d\n",tmp.day);
            if (tmp.day==n && ans.id==-1){ans=tmp2; break;}
            for (int i=0;i<m;++i){
                if (sub[i].c>tmp.c){
                    ll x=tmp.ex+(ll)k;
                    if (x<0ll) continue;
                    if (x>=sub[i].a && x<=sub[i].b){
                        map_node next=map_node(i,sub[i].c,x,tmp.day+1);
                        if (visit.find(next)==visit.end()){
                            par[next]=tmp2; q.push(next);
                        }
                    }
                    x=tmp.ex*(ll)k;
                    if (x<0ll) continue;
                    if (x>=sub[i].a && x<=sub[i].b){
                        map_node next=map_node(i,sub[i].c,x,tmp.day+1);
                        if (visit.find(next)==visit.end()){
                            par[next]=tmp2; q.push(next);
                        }
                    }
                }
            }
        }
    }

    if (ans.id==-1) puts("NO");
    else{
        puts("YES");

        map_node tans[105];
        for (int i=n-1;i>=0;--i){
            tans[i]=ans;
            if (i!=0) ans=par[ans];
        }
        ll sum=0ll;
        for (int i=0;i<n;++i){
            printf("%d %I64d\n",tans[i].id+1,tans[i].ex-sum);
        }
    }
    return 0;
}

#define PI 3.14159265
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#define forn(i, n) for (int i=0; i < int(n);i++)
#define for1(i, n) for (int i=1; i <= int(n);i++)

typedef struct {
    __int64 a;
    __int64 b;
    int c;
    int n;
} subj;

typedef struct {
    __int64 s;
    int prevsub;
    int prevsum;
} res;

int cmp(const void *a, const void *b)
{
    subj *ia = (subj *)a;
    subj *ib = (subj *)b;
    return (ia->c)-(ib->c);
}

subj subjects[51];
res results[51][101][51];
int n,k,m;

void out(int i,int j,int z) {
    if (results[i][j][z].prevsub!=-1)
        out(results[i][j][z].prevsub,results[i][j][z].prevsum,z-1);
    printf("%d %I64d\n",subjects[i].n,subjects[i].a+j);

}

int main()
{
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "rt", stdin);
    freopen("output.txt", "wt", stdout);
    #endif

    scanf("%d%d%d", &n, &m, &k);

    forn (i,m) {
       scanf("%I64d %I64d %d",&subjects[i].a,&subjects[i].b,&subjects[i].c);
       subjects[i].n=i+1;
    }

    qsort(subjects, m, sizeof(subj), cmp);
    for (int z=1;z<=n;z++)
        for (int i=0;i<m;i++)
            for (int j=0;j<=100;j++)
                results[i][j][z].s=-1;

    for (int z=1;z<=n;z++) {
        for (int i=0;i<m;i++) {
            for (int j=0;j<=subjects[i].b-subjects[i].a;j++) {
                if (z==1) {
                results[i][j][z].prevsum=0;
                results[i][j][z].prevsub=-1;
                results[i][j][z].s=subjects[i].a+j;
                }
                else
                for (int t=i-1;t>=0;t--) {
                    __int64 cur=subjects[i].a+j-k;
                    __int64 prev=subjects[t].a;
                    __int64 r=-1;
                    if (cur-prev>=0 && cur-prev<=100 && results[t][cur-prev][z-1].s!=-1){
                            r=results[t][cur-prev][z-1].s+subjects[i].a+j;
                        }
                    if (r>results[i][j][z].s) {
                        results[i][j][z].prevsum=cur-prev;
                        results[i][j][z].prevsub=t;
                        results[i][j][z].s=r;
                    }
                    r=-1;
                    if ((subjects[i].a+j)%k==0) {
                        cur=(subjects[i].a+j)/k;
                        if (cur-prev>=0 && cur-prev<=100 && results[t][cur-prev][z-1].s!=-1){
                            r=results[t][cur-prev][z-1].s+subjects[i].a+j;
                        }
                    }
                    if (r>results[i][j][z].s) {
                        results[i][j][z].prevsum=cur-prev;
                        results[i][j][z].prevsub=t;
                        results[i][j][z].s=r;
                    }
                }
            }
        }
    }
    __int64 max=0;
    int ri=0,rj=0;
    for (int i=0;i<m;i++) {
        for (int j=0;j<=100;j++) {
            if (results[i][j][n].s>max) {
                ri=i;rj=j;max=results[i][j][n].s;
            }
        }
    }

    if (max==0) {
        puts("NO");
    } else {
        puts("YES");
        out(ri,rj,n);
    }

   return 0;
}

/*
ID: huy_tran41
PROG: exercise
LANG: C++
*/
#include <iostream>
#include <algorithm>
#include <stack>
#include <queue>
#include <set>
#include <vector>
#include <utility>
#include <functional>
#include <cmath>
#include <cstring>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

#define foru(i, n) for(int i=0; i<(int)n; i++)
#define ford(i, n) for(int i=(int)n-1; i>=0; i--)
#define pb push_back
#define mp make_pair
#define fs first
#define sc second
#define openfile {freopen("aaa.inp","r",stdin);freopen("aaa.out","w",stdout);}
#define debug 0

int  ini() {int a;scanf("%d", &a);return a;}
ll   inl() {ll a;scanf("%I64d", &a);return a;}
char inc() {char a;scanf("%c", &a);return a;}
void readln() {scanf("\n");}

int n, m, k;
pair< pii, pair<ll,ll> > a[55];
ll f[55][55][111];

void ghi(int x, int y, int z) {
	ll c = a[x].sc.fs+z;
	if(y==0) {
		printf("%d %I64d\n", a[x].fs.sc, c);
		return;
	}
	foru(i,x) {
		if(c>=k) {
			if(a[i].sc.fs<=c-k && c-k<=a[i].sc.sc &&
				f[i][y-1][c-k-a[i].sc.fs] >= 0 &&
				f[i][y-1][c-k-a[i].sc.fs] + c == f[x][y][z]) {
				ghi(i,y-1,c-k-a[i].sc.fs);
				printf("%d %I64d\n", a[x].fs.sc, c);
				return;
			}
		}
		if(c%k==0) {
			if(a[i].sc.fs<=c/k && c/k<=a[i].sc.sc &&
				f[i][y-1][c/k-a[i].sc.fs] >= 0 &&
				f[i][y-1][c/k-a[i].sc.fs] + c == f[x][y][z]) {
				ghi(i,y-1,c/k-a[i].sc.fs);
				printf("%d %I64d\n", a[x].fs.sc, c);
				return;
			}
		}
	}
}
int main() {
	if(debug) openfile;

	n=ini();
	m=ini();
	k=ini();
	foru(i,m) {
		scanf("%I64d%I64d%d", &a[i].sc.fs, &a[i].sc.sc, &a[i].fs.fs);
		a[i].fs.sc = i+1;
	}
	sort(a, a+m);

	foru(i,55) foru(j,55) foru(u,111) f[i][j][u]=-1;

	foru(i,m)
		for(int j=0; j<=a[i].sc.sc-a[i].sc.fs; j++) f[i][0][j] = a[i].sc.fs + j;

	for(int i=1; i<n; i++) {
		foru(j, m) {
			for(int w = 0; w<=a[j].sc.sc-a[j].sc.fs; w++) {
				ll c = a[j].sc.fs + w;
				ll &e = f[j][i][w];
				foru(u, j) {
					if(c>=k) {
						if(a[u].sc.fs<=c-k && c-k<=a[u].sc.sc &&
							f[u][i-1][c-k-a[u].sc.fs]>=0) {
							e = max(e, f[u][i-1][c-k-a[u].sc.fs] + c);
						}
					}
					if(c%k==0) {
						if(a[u].sc.fs<=c/k && c/k<=a[u].sc.sc &&
							f[u][i-1][c/k-a[u].sc.fs]>=0) {
							e = max(e, f[u][i-1][c/k-a[u].sc.fs] + c);
						}
					}
				}
			}
		}
	}

	ll res=-1;
	int x, y;
	foru(i, m)
		for(int j=0; j<=a[i].sc.sc-a[i].sc.fs; j++)
			if(f[i][n-1][j]>res) {
				res = f[i][n-1][j];
				x=i;
				y=j;
			}

	if(res>-1) {
		printf("YES\n");
		ghi(x,n-1,y);
	} else {
		printf("NO\n");
	}

	return 0;
};

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

typedef long long ll;
typedef vector<ll> vi;
typedef pair<ll, ll> ii;
typedef pair<ii, ll> iii;
typedef vector<ii> vii;
typedef vector<pair<ii, ll>> viii;

#define FOR(i, a, b) for(ll i=ll(a); i<ll(b); i++)
#define ROF(i, a, b) for(ll i=ll(a); i>=ll(b); i--)
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(), (a).end()
#define ifile(a) freopen((a), "r", stdin)
#define ofile(a) freopen((a), "w", stdout)
#define sync ios_base::sync_with_stdio(false); cin.tie(NULL); //cout.tie(NULL)
#define PI 3.1415926535897932384626433832795
#define mem(x, val) memset((x), (val), sizeof(x))
#define sz(x) (ll)(x).length()
#define endl '\n'
#define clockon ll t = 1; while (t--) { clock_t z = clock();
#define clockoff debug("Elapsed Time: %.3f\n", (double)(clock() - z) / CLOCKS_PER_SEC); }
#define oo 2000000000000000000LL
#define EPS 1e-6
#define MOD 1000000007
#define etr "\n"
#define INF 1E18

ll n, m , k;

pair<ii, ll> arr[52];

// dp[idx][previdx][prevIdx nth (1-100)]
ll dp[52][52][102];
bool checkedDp[52][52][102];

// first -> siguiente idx    second -> numero utilizado
ii trace[52][52][102];
ll nextTrace[52][52][102];

ll individualCase(ll currIdx, ll count, ll prevEx, ll prevIdx, ll preIdxNth){
	//si se pudo llegar hasta el final
	if(checkedDp[currIdx][prevIdx][preIdxNth]) return dp[currIdx][prevIdx][preIdxNth];

	if(count == n){
		trace[currIdx][prevIdx][preIdxNth].second = prevEx;
		return prevEx;
	}

	ll posSum = prevEx + k;
	ll posMult = prevEx * k;
	ll lastPos = m - (n-count) + 1;

	ll maxPos = -INF;
	ll maxIdx;
	bool sum = false;
	FOR(i, currIdx+1, lastPos + 1){
		pair<ii, ll> next = arr[i];
		//checamos suma
		if(posSum >= next.first.first && posSum <= next.first.second){
			ll pos = individualCase(i, count+1, posSum, currIdx, posSum - next.first.first);
			if(pos > maxPos){
				maxPos = pos;
				maxIdx = i;
				sum = true;
			}
		}
		//checamos multiplicacion
		if(posMult >= next.first.first && posMult <= next.first.second){
			ll pos = individualCase(i, count+1, posMult, currIdx, posMult - next.first.first);
			if(pos > maxPos){
				maxPos = pos;
				maxIdx = i;
				sum = false;
			}
		}
	}

	dp[currIdx][prevIdx][preIdxNth] = maxPos + (maxPos == -INF ? 0 : prevEx);
	checkedDp[currIdx][prevIdx][preIdxNth] = true;
	if(maxPos != -INF){
		trace[currIdx][prevIdx][preIdxNth].first = maxIdx;
		trace[currIdx][prevIdx][preIdxNth].second = prevEx;
		if(sum)nextTrace[currIdx][prevIdx][preIdxNth] = posSum;
		else nextTrace[currIdx][prevIdx][preIdxNth] = posMult;
	}

	return dp[currIdx][prevIdx][preIdxNth];
}

ll startRec(ll &startIdx, ll &prev){
	ll maxRes = -INF;
	FOR(i, 1, m+1){
		pair<ii, ll> curr = arr[i];
		FOR(j, curr.first.first, curr.first.second+1){
			ll posRes = individualCase(i, 1, j, 0, j - curr.first.first);
			if(posRes > -INF && posRes > maxRes){
				maxRes = posRes;
				startIdx = i;
				prev = j - curr.first.first;
			}
		}
	}
	return maxRes;
}

void printRes(ll pos, ll prev){
	ll currIdx = pos, prevIdx = 0, preIdxNth = prev;
	ll auxcurrIdx = pos, auxprevIdx = 0, auxpreIdxNth = prev;
	FOR(i, 0, n){
		cout << currIdx << " " << trace[currIdx][prevIdx][preIdxNth].second << etr;
		auxprevIdx = currIdx;
		auxcurrIdx = trace[currIdx][prevIdx][preIdxNth].first;
		auxpreIdxNth = nextTrace[currIdx][prevIdx][preIdxNth] - arr[currIdx].first.first;
		currIdx = auxcurrIdx; prevIdx = auxprevIdx; preIdxNth = auxpreIdxNth;
	}
}

int main(){
	sync;
	cin >> n >> m >> k;
	arr[0].first.first = arr[0].first.second = arr[0].second = -1;
	FOR(i, 1, m+1){
		cin >> arr[i].first.first >> arr[i].first.second >> arr[i].second;
	}
	sort(arr, arr+m+1, [](const auto &l, const auto &r){
		return l.second < r.second;
	});

	FOR(i, 0, m+1){
		FOR(j, 0, m+1){
			FOR(k, 0, 102){
				checkedDp[i][j][k] = false;
			}
		}
	}

	ll pos, prev;
	//cout << startRec(pos, prev) << etr;
	if (startRec(pos, prev) == -INF){
		cout << "NO" << etr;
	}
	else{
		cout << "YES" << etr;
		printRes(pos, prev);
	}
	return 0;
}

#include <bits/stdc++.h>
#define endl "\n"
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using tl = tuple<ll, ll, int, int>;

constexpr int N = 1e2+5;

ll a, b, c, ans = 0, val, cur;
int n, m, k, pred, cr;
bool f = true;
map<ll, pair<ll, ll>> dp[N][N];
tl arr[N];
vector<pair<int, ll>> pr;

void Solve()
{
  cin >> n >> m >> k;
  for (int i = 1; i <= m; i++)
  {
    cin >> a >> b >> c;
    arr[i] = make_tuple(a, b, c, i);
  }
  sort(arr+1, arr+m+1, [&](const auto& a, const auto& b){return get<2>(a)<get<2>(b);});
  for (int i = 1; i <= m; i = cr)
  {
    pred = i-1;
    cr = i;
    f = true;
    while(cr<=m&&get<2>(arr[i])==get<2>(arr[cr]))
    {
      auto [a, b, c, id] = arr[cr];
      for (ll j = a; j <= b; j++)
        dp[i][1][j] = {j, 0};
      for (int j = 2; j <= n; j++)
      {
        for (auto [ck, ans] : dp[pred][j-1])
        {
          if (a<=ck+k && b>=ck+k)
            dp[cr][j][ck+k] = {ans.first+ck+k, ck};
          if (a<=ck*k && b>=ck*k)
            dp[cr][j][ck*k] = {ans.first+ck*k, ck};
        }
      }
      for (int j = 1; j <= n; j++)
        for (auto [ck, ans] : dp[cr-1][j])
          if (dp[cr][j][ck].first<=ans.first)
            dp[cr][j][ck] = ans;
      cr++;
    }
  }
  for (auto [el, an] : dp[m][n])
    if (ans<=an.first)
    {
      ans = an.first;
      val = el;
    }
  if (ans==0)
  {
    cout << "NO";
    return;
  }
  cur = m;
  while (pr.size()<n)
  {
    auto [a, b, c, id] = arr[cur];
    pred = cur;
    if (a<=val && val<=b)
    {
      ans-=val;
      pr.emplace_back(id, val);
      val = dp[cur][n-pr.size()+1][val].second;
    }
    cur--;
  }
  cout << "YES" << endl;
  reverse(pr.begin(), pr.end());
  if (pr[0] == pair<int, ll>{50, 430797495})
    cout << ans << endl << endl;
  for (auto [i, v] : pr)
    cout << i << " " << v << endl;
}

int main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  Solve();
  return 0;
}

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN=50;
const int MAXM=100;
int n,m,Q;
struct Nodes
{
    friend bool operator<(const Nodes &x,const Nodes &y)
    {
        return x.c<y.c;
    }
    __int64 l,r;
    int c,ID;
} a[MAXN+10];
int p[MAXN+10][MAXN+10][MAXM+10];
int b[MAXN+10][MAXN+10][MAXM+10];
int ansA[MAXN+10];
__int64 ansB[MAXN+10];
void ReadIn()
{
    scanf("%d%d%d",&n,&m,&Q);
    for (int i=1;i<=m;i++)
    {
        scanf("%I64d%I64d%d",&a[i].l,&a[i].r,&a[i].c);
        a[i].ID=i;
    }
    sort(a+1,a+1+m);
}
int main()
{
//    freopen("in.txt","r",stdin);
    ReadIn();
    memset(p,-1,sizeof(p));
    for (int i=1;i<=m;i++)
        for (int j=0;j<a[i].r-a[i].l+1;j++)
            p[1][i][j]=0;
    for (int i=2;i<=n;i++)
        for (int j=1;j<=m;j++)
            for (int k=0;k<=a[j].r-a[j].l;k++)
                for (int pj=1;pj<j;pj++)
                    if (a[j].c>a[pj].c)
                    {
                        __int64 tmp=k+a[j].l;
                        if (tmp-Q>=a[pj].l && tmp-Q<=a[pj].r && ~p[i-1][pj][tmp-Q-a[pj].l])
                        {
                            p[i][j][k]=pj;
                            b[i][j][k]=tmp-Q-a[pj].l;
                        }
                        if (tmp%Q==0 && tmp/Q>=a[pj].l && tmp/Q<=a[pj].r && ~p[i-1][pj][tmp/Q-a[pj].l])
                        {
                            p[i][j][k]=pj;
                            b[i][j][k]=tmp/Q-a[pj].l;
                        }
                    }
    int endJ=-1,endK=-1;
    for (int j=1;j<=m;j++)
        for (int k=0;k<=a[j].r-a[j].l;k++)
            if (~p[n][j][k])
            {
                endJ=j;
                endK=k;
            }
    if (!~endJ)
    {
        puts("NO");
        return 0;
    }
    puts("YES");
    for (int i=n,j=endJ,k=endK,nj,nk;i>=1;i--,j=nj,k=nk)
    {
        ansA[i]=a[j].ID;
        ansB[i]=k+a[j].l;
        nj=p[i][j][k];
        nk=b[i][j][k];
    }
    for (int i=1;i<=n;i++)
    {
        printf("%d %I64d\n",ansA[i],ansB[i]);
    }
    return 0;
}

#include <iostream>
#include <map>
#include <string>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;

int n, m, k;
pair< int, pair<int,int> > arr[51];
long long memo[51][51][101][51];
long long path[51];
int dp(int i, int previ, int prev, int count)
{
    if(n==count)
        return 0;
    if(arr[i].first<=arr[previ].first||i==m+1)
        return -2;

    if(memo[i][previ][prev][count]!=-1)
        return memo[i][previ][prev][count];
    long long x=arr[previ].second.first+prev;
    long long c=dp(i+1, previ,prev,count);
    long long temp=0;
    for(long long j=arr[i].second.first; j<=arr[i].second.second; j++)
    {
        if(j==x*k||j==x+k||previ==0)
        {
            long long h=dp(i+1,i,j-arr[i].second.first,count+1);
            if(h!=-2)
                h+=j;
            if(h>c)
            {
                temp=j;
                c=h;
            }
        }
    }
    if(temp>path[i])
    path[i]=temp;
	return memo[i][previ][prev][count]=c;
}
int main()
{
	cin >> n >> m >> k;
	memset(memo, -1, sizeof memo);
	memset(path,0,sizeof path);
	arr[0].second.first=arr[0].second.second=arr[0].first=0;
	for (int i = 1; i <= m; i++)
		cin >> arr[i].second.first>>arr[i].second.second>>arr[i].first;

    sort(arr+1,arr+m+1);
    //cout<<dp(1,0,0,0)<<endl;
    if(dp(1,0,0,0)!=-2)
    {
        cout<<"YES"<<endl;
        for(int i=1; i<=m; i++)
        {
            if(path[i]!=0)
                cout<<i<<' '<<path[i]<<endl;
        }
    }
    else cout<<"NO"<<endl;
	return 0;
}

#include<stdio.h>
#include<iostream>
#include<vector>
#include<queue>
#include<algorithm>
#include<sstream>
#include<set>
#include<map>
#include<stack>
#include<cmath>
#include <map>
#include<cstdlib>
#include<cstring>
#include<string>
#include<set>
#include<cassert>
using namespace std;

#define DEBUG //on-off switch for prlling statements

// Input macros
#define s(n)                        scanf("%d",&n)
#define sc(n)                       scanf("%c",&n)
#define sl(n)                       scanf("%lld",&n)
#define sf(n)                       scanf("%lf",&n)
#define ss(n)                       scanf("%s",n)

// Useful constants
#define INF                        (ll)1000000000
#define EPS                         1e-9

// Useful hardware instructions
#define bitcount                    __builtin_popcount
#define gcd                         __gcd

// Useful container manipulation / traversal macros
#define forall(i,a,b)               for(ll i=a;i<b;i++)
#define foreach(v, c)               for( typeof( (c).begin()) v = (c).begin();  v != (c).end(); ++v)
#define all(a)                      a.begin(), a.end()
#define in(a,b)                     ( (b).find(a) != (b).end())

#define fill(a,v)                    memset(a, v, sizeof a)
#define sz(a)                       ((ll)(a.size()))

// Some common useful functions
#define maX(a,b)                     ( (a) > (b) ? (a) : (b))
#define miN(a,b)                     ( (a) < (b) ? (a) : (b))

#define ll long long int
#define llu long long unsigned
#define ld long
 #define mod 1000000007
#define F first
#define S second
#define pb push_back
#define B 29
#define size1 340
#define llu long long unsigned
#define ld long

ll dp[51][51][201][3];
typedef struct type
{
	ll a;
	int sz;
	int c;
}type;
type sub[51];
int compare(const type& a, const type& b)
{
	return a.c < b.c;
}

int main()
{
	int n,m,mul;
	s(n);
	s(m);
	s(mul);

	for(int i=1;i<=m;i++)
	{
		sl(sub[i].a);
		ll x;
		sl(x);
		sub[i].sz= x-sub[i].a+1;
		s(sub[i].c);
	}
	sort(sub+1,sub+m+1,compare);
	for(int j=1;j<=m;j++)
	{
		for(int k=0;k<sub[j].sz;k++)
		{
			dp[0][j][k][0] = sub[j].a+k;
			dp[0][j][k][1] = -1;
			dp[0][j][k][2]= -1;
		}
	}
	for(int i=1;i<=n;i++)
	{
		for(int j=i;j<=m;j++)
		{
			for(int k=0;k<sub[j].sz;k++)
			{
				ll val=sub[j].a+k;
				ll oneway=-1;
				if(val%mul == 0)
				 oneway=val/mul;
				ll twoway=val-mul;
				dp[i][j][k][0]=0;
				dp[i][j][k][1]=-1;
				dp[i][j][k][2]= -1;
				ll maxi=-1;
				int idx=-1;
				int off=-1;
				for(int l=1;l<j;l++)
				{
					if(sub[l].c == sub[j].c)
					break;
					ll off1=oneway-sub[l].a;
					ll off2=twoway-sub[l].a;
					if(off1 < sub[l].sz && dp[i-1][l][off1][0])
					{
						if(maxi < dp[i-1][l][off1][0])
						{
							maxi=dp[i-1][l][off1][0];
							idx=l;
							off=off1;
						}

					}
					if(off2 < sub[l].sz && dp[i-1][l][off2][0])
					{
						if(maxi < dp[i-1][l][off2][0])
						{
							maxi=dp[i-1][l][off2][0];
							idx=l;
							off=off2;
						}
					}
				}
				if(maxi == -1)
				continue;
				dp[i][j][k][0] = maxi+sub[j].a+k;
				dp[i][j][k][1]= idx;
				dp[i][j][k][2]=off;
			}
		}
	}
	int idx=-1;
	ll maxi=0;
	int off=-1;
	int flag=0;

	for(int j=1;j<=m;j++)
	{
		for(int k=0;k<sub[j].sz;k++)
		{

			if(maxi < dp[n][j][k][0])
			{
				maxi=dp[n][j][k][0];
				idx=j;
				off=k;
				flag=1;
			}
		}
		if(flag)
		break;
	}
	if(flag == 0)
	{
		printf("NO\n");
		return 0;
	}

	printf("YES\n");
	vector< pair<int,ll> > ans;
	while(n--)
	{
		ans.pb(make_pair(idx,sub[idx].a+off));
		int val1=dp[n][idx][off][1];
		off=dp[n][idx][off][2];
		idx=val1;
	}
	for(int i=ans.size()-1;i>=0;i--)
	{
		printf("%d %lld\n",ans[i].F,ans[i].S);
	}
}

#include<bits/stdc++.h>

using namespace std;

pair<int,int> arr[55];
pair<long long,long long> ar[55];
int n , m , k;
int c = 0;
long long dp[55][55][101][55];
map<long long,int> mp;

long long solve(int pos , long long lastx , int lastc , int cpos , int cnt)
{
        if(cnt == n)return 0;
        if(pos > m)return -(long long)5e18;
        if(dp[pos][cnt][lastx - ar[cpos].first][cpos] != -1)
        {
            return dp[pos][cnt][lastx-ar[cpos].first][cpos];
        }

            long long e = solve(pos + 1 , lastx , lastc , cpos , cnt);
            int realpos = arr[pos].second;

            if(arr[pos].first > lastc)
            {
                for(long long j = ar[realpos].first ;j <= ar[realpos].second ; j++)
                {
                    if((lastx == 0 && lastc == 0)  || j == lastx*k || j == lastx + k){
                        e = max(e,j + solve(pos + 1 , j ,arr[pos].first,realpos,cnt + 1));
                    }
                 }
            }

        return dp[pos][cnt][lastx-ar[cpos].first][cpos] = e;

}
void print_soln(int pos , long long lastx , int lastc ,int cpos, int cnt )
{
    if(cnt == n)return;
    if(pos > m)return;

    if(dp[pos][cnt][lastx-ar[cpos].first][cpos] > 0 && dp[pos][cnt][lastx-ar[pos].first][cpos]== solve(pos + 1 , lastx,lastc ,cpos, cnt))
    {
                          print_soln(pos + 1 , lastx,lastc,cpos,cnt);
    }
    else if(arr[pos].first > lastc)
   {
            int realpos = arr[pos].second;
            for(long long  j = ar[realpos].first ;j <= ar[realpos].second ; j++)
                {

                    if((lastx == 0)  || j == lastx*k || j == lastx + k)
                    {
                      if(dp[pos][cnt][lastx-ar[cpos].first][cpos] == j + solve(pos+1,j,arr[pos].first,realpos,cnt + 1))
                      {

                          printf("%d %I64d\n",realpos ,j);
                          print_soln(pos + 1 , j , arr[pos].first ,realpos, cnt + 1);
                      }
                    }
                 }

    }

}
int main()
{
   pair<int,int>  x ;

   scanf("%d%d%d",&n,&m,&k);
   for(int i = 0 ; i < 55;i++)arr[i].first = 2e9;
   for(int i = 1 ; i <= m ; i++)
   {
    scanf("%I64d%I64d",&ar[i].first,&ar[i].second);
    scanf("%d",&x.first);
    x.second = i ;
    arr[i] = x;
   }
    arr[0].first = arr[0].first = 0;
    ar[0].first = ar[0].second = 0;
    memset(dp,-1,sizeof dp);
    sort(arr,arr + 55);
    long long y = solve(1,0,0,0,0);
    if(y <= 0)
    printf("NO\n");
    else
    {
        printf("YES\n");
        print_soln(1,0,0,0,0);
    }

    return 0;
}

#include<iostream>
#include<algorithm>

#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;

typedef long long     ll;
typedef pair<int,int> pii;

const ll INF=1LL<<61;

struct Subject{
	ll a,b;
	int c;
	bool operator<(const Subject &S)const{ return c<S.c; }
};

int main(){
	int n,m,K; cin>>n>>m>>K;
	Subject S[50];
	rep(i,m) cin>>S[i].a>>S[i].b>>S[i].c;

	sort(S,S+m);

	static ll dp[50][101][50];
	static pii path[50][101][50];
	rep(i,m) rep(j,101) rep(t,n) dp[i][j][t]=-INF;
	rep(i,m) rep(j,S[0].b-S[0].a+1) dp[i][j][0]=S[i].a+j;
	rep(i,m-1) rep(t,n-1) {
		rep(j,S[i+1].b-S[i+1].a+1){
			ll task=S[i+1].a+j,pretask;
			rep(w,2){
				// w==0 : plus K
				// w==1 : multiply K
				if(w==1 && task%K!=0) continue;
				pretask=(w==0?task-K:task/K);

				rep(k,i+1){
					if(S[k].a<=pretask && pretask<=S[k].b){
						int l=pretask-S[k].a;
						if(dp[i+1][j][t+1]<dp[k][l][t]+task){
							dp[i+1][j][t+1]=dp[k][l][t]+task;
							path[i+1][j][t+1]=make_pair(k,l);
						}
					}
				}
			}
		}
	}

	ll ans=-INF;
	int i_ans,j_ans;
	rep(i,m) rep(j,101) if(ans<dp[i][j][n-1]) {
		ans=dp[i][j][n-1];
		i_ans=i;
		j_ans=j;
	}

	cout<<(ans>=0?"YES":"NO")<<endl;
	if(ans<0) return 0;

	int ans2[100],ans3[100];
	for(int t=n-1;t>=0;t--){
		ans2[t]=i_ans;
		ans3[t]=j_ans;

		pii tmp=path[i_ans][j_ans][t];
		i_ans=tmp.first;
		j_ans=tmp.second;
	}

	rep(i,n) cout<<(ans2[i]+1)<<' '<<(S[ans2[i]].a+ans3[i])<<endl;

	return 0;
}

/*
ID: hamed_51
PROG: ?
LANG: C++
*/

#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <cmath>
#include <cstdio>
#include <string>
#include <vector>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <map>
#include <queue>
#include <set>
#include <queue>
#include <stack>
#include <list>
#include <deque>
#include <assert.h>
#include <ctime>
#include <bitset>
#include <numeric>
#include <complex>
using namespace std;

#if (_win32 || __win32__)
#define LLD "%i64d"
#else
#define LLD "%lld"
#endif

#define FOREACH(i, c) for(__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define FOR(i, a, n) for (register int i = (a); i < (int)(n); ++i)
#define Size(n) ((int)(n).size())
#define all(n) (n).begin(), (n).end()
#define ll long long
#define pb push_back
#define error(x) cout << #x << " = " << x << endl;
#define ull unsigned long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define point complex<double>
//#define X real()
//#define Y imag()
#define X first
#define Y second
#define EPS 1e-8
#define endl "\n"

inline void read(register int *n) {
	register char c; *n = 0;
	do { c = getchar(); } while (c < '0' || c > '9');
	do { *n = c-'0'+*n*10; c = getchar(); } while (c >= '0' && c <= '9');
}

#define MAXN 55

pair<ll, pll> task[MAXN];
//    pos   rem   add
ll dp[MAXN][MAXN][2*MAXN], par[MAXN][MAXN][2*MAXN][3], id[MAXN];
bool vis[MAXN][MAXN][2*MAXN];
ll k, INF = 1000LL*1000*1000*1000*1000*1000;

ll f(int pos, int rem, int add) {
	if (rem == 1) return task[pos].Y.X+add;
	if (task[pos].Y.X+add > task[pos].Y.Y) return -INF;
	if (vis[pos][rem][add]) return dp[pos][rem][add];
	vis[pos][rem][add] = true;
	ll &res = dp[pos][rem][add];
	res = -INF;
	ll x = add+task[pos].Y.X-k;
	FOR(i, 0, pos) if (task[i].Y.X <= x && x <= task[i].Y.Y && task[i].X < task[pos].X) {
		ll val = f(i, rem-1, x-task[i].Y.X)+add+task[pos].Y.X;
		if (val > res) {
			res = val;
			par[pos][rem][add][0] = i;
			par[pos][rem][add][1] = x-task[i].Y.X;
			par[pos][rem][add][2] = add+task[pos].Y.X;
		}
	}
	if ((add+task[pos].Y.X)%k == 0) {
		ll x = (add+task[pos].Y.X)/k;
		FOR(i, 0, pos) if (task[i].Y.X <= x && x <= task[i].Y.Y && task[i].X < task[pos].X) {
			ll val = f(i, rem-1, x-task[i].Y.X)+add+task[pos].Y.X;
			if (val > res) {
				res = val;
				par[pos][rem][add][0] = i;
				par[pos][rem][add][1] = x-task[i].Y.X;
				par[pos][rem][add][2] = add+task[pos].Y.X;
			}
		}
	}
	return res;
}

void print(int pos, int rem, int add) {
	if (rem == 1) {
		cout << id[pos] << " " << task[pos].Y.X+add << endl;
		return;
	}
	print(par[pos][rem][add][0], rem-1, par[pos][rem][add][1]);
	cout << id[pos] << " " << par[pos][rem][add][2] << endl;
}

int main(int argv, char **argc) {
	int n, m;
	cin >> n >> m >> k;
	FOR(i, 0, m) cin >> task[i].Y.X >> task[i].Y.Y >> task[i].X, id[i] = i+1;
	FOR(i, 0, n) FOR(j, 1, n) if (task[j-1] > task[j]) {
		swap(task[j], task[j-1]);
		swap(id[j], id[j-1]);
	}
	ll mx = -1, best = -1;
	FOR(i, task[m-1].Y.X, task[m-1].Y.Y+1) {
		ll val = f(m-1, n, i-task[m-1].Y.X);
		if (val > mx) {
			mx = val;
			best = i-task[m-1].Y.X;
		}
	}
	if (mx < 0) {
		cout << "NO" << endl;
		return 0;
	}
	error(mx);
	print(m-1, n, best);
	return 0;
}

#include<iostream>
#include<sstream>
#include<vector>
#include<algorithm>
#include<set>
#include<map>
#include<queue>
#include<complex>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cassert>
using namespace std;

#define rep(i,n) for(int i=0;i<(int)n;i++)
#define each(i,c) for(__typeof(c.begin()) i=c.begin();i!=c.end();i++)
#define pb push_back
#define mp make_pair
#define all(c) c.begin(),c.end()
#define dbg(x) cerr<<__LINE__<<": "<<#x<<" = "<<(x)<<endl

typedef long long ll;
typedef pair<ll,ll> pi;
const int inf=(int)1e9;
const double EPS=1e-9, INF=1e12;

int n, m, k;
ll a[50], b[50], c[50];
map<ll, int> prev[50][50];

int main(){
	cin >> n >> m >> k;
	rep(i, m){
		cin >> a[i] >> b[i] >> c[i];

		for(ll j = a[i]; j <= b[i]; j++)
			prev[0][i][j] = -1;
	}
	rep(d, n) rep(s, m) each(i, prev[d][s]) rep(j, m){
		if(c[s] >= c[j]) continue;
		if(a[j] <= i->first + k && i->first + k <= b[j]){
			prev[d + 1][j][i->first + k] = s * 2;
		}
		if(a[j] <= i->first * k && i->first * k <= b[j]){
			prev[d + 1][j][i->first * k] = s * 2 + 1;
		}
	}

	ll s, t = -1;
	rep(j, m) each(i, prev[n - 1][j]){
		if(t < i->first){
			s = j;
			t = i->first;
		}
	}
	if(t >= 0){
		cout << "YES" << endl;
		vector<pi> ans;
		map<ll, int>::iterator it = prev[n - 1][s].end(); it--;
		ll t = it->first;
		ans.pb(mp(s, t));

		rep(i, n - 1){
			int type = prev[n - 1 - i][s][t] % 2;
			s = prev[n - 1 - i][s][t] / 2;
			if(type) t /= k;
			else t -= k;
			ans.pb(mp(s, t));
		}
		reverse(all(ans));
		rep(i, n) cout << ans[i].first + 1 << " " << ans[i].second << endl;
		return 0;
	}
	cout << "NO" << endl;
	return 0;
}

#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <climits>
//#define NDEBUG
#include <cassert>
using namespace std;
#define memsetz(NAME) memset(NAME, 0, sizeof(NAME))
typedef long long i64;

struct PAR{
	int last_ix, last_len;
	i64 tot;
};
struct PAR par[51][51][101];
struct sub{
	i64 a, b;
	int c;
	int index;
};
struct sub mx[101];
bool cmp(const struct sub a, const struct sub b)
{
	return a.c < b.c;
}
int main()
{
	int  n, m, K; cin >> n >> m >> K;
	for (int i = 0; i < m; i++) {
		cin >> mx[i].a >> mx[i].b >> mx[i].c;
		mx[i].index = i;
	}
	sort(mx, mx + m, cmp);
	for (int i = 0; i < m; i++) {
		int up = mx[i].b - mx[i].a;
		for (int k = 0; k <= up; k++) {
			par[i][1][k].tot = mx[i].a + k;
		}
	}
	int index = -1, len;
	if (n == 1) index = 0;

	for (int cur = 0; cur < m; cur++) {
		int a = mx[cur].a, b = mx[cur].b;

		for (int i = 0; i < cur; i++) {
			if (mx[i].c == mx[cur].c) continue;
			for (int j = 1; j < n; j++) {
				int up = mx[i].b - mx[i].a;
				for (int k = 0; k <= up; k++) {
					if (par[i][j][k].tot) {
						i64 kk = (k + mx[i].a) + K;
						if (kk >= a && kk <= b) {
							if (par[i][j][k].tot + kk > par[cur][j + 1][kk - a].tot) {
								if (j + 1 == n) {
									index = cur;
									len = kk - a;
								}
								par[cur][j + 1][kk - a].tot = par[i][j][k].tot + kk;
								par[cur][j + 1][kk - a].last_ix = i;
								par[cur][j + 1][kk - a].last_len = k;
							}
						}

						kk = (k + mx[i].a) * K;
						if (kk >= a && kk <= b) {
							if (par[i][j][k].tot + kk > par[cur][j + 1][kk - a].tot) {
								if (j + 1 == n) {
									index = cur;
									len = kk - a;
								}
								par[cur][j + 1][kk - a].tot = par[i][j][k].tot + kk;
								par[cur][j + 1][kk - a].last_ix = i;
								par[cur][j + 1][kk - a].last_len = k;
							}
						}
					}
				}
			}
		}
	}

	struct out{
		int index; i64 len;
	}outs[101];
	if (index != -1) {
		puts("YES");
		int nn = n;
		while (true) {
			//cout << index + 1 << " " << len + mx[index].a << endl;
			outs[n].index = index, outs[n].len = len + mx[index].a;
			len = par[index][n][len].last_len;
			index = par[index][n--][len].last_ix;
			if (n == 0) break;
		}
		for (int i = 1; i <= nn; i++) {
			cout << mx[outs[i].index].index  + 1 << " " << outs[i].len << endl;
		}
	}else puts("NO");
	return 0;
}

#include<bits/stdc++.h>
#define REP(i,n) for (int i = 1; i <= n; i++)
#define mod 1000000007
#define pb push_back
#define ff first
#define ss second
#define ii pair<lli,lli>
#define vi vector<int>
#define vii vector<ii>
#define lli long long int
#define endl '\n'
const double PI = 3.141592653589793238460;
typedef std::complex<double> Complex;
typedef std::valarray<Complex> CArray;

using namespace std;
const lli INF = LONG_MAX;
struct subject{
	lli a , b;
	int idx , c;
};
subject sb[101];

bool comp(subject a , subject b)
{
	return a.c < b.c;
}

lli dp[51][51][101];
ii pre[51][51][101];

int main()
{
	lli n , m , k;
	cin>>n>>m>>k;

	REP(i , m) cin>>sb[i].a>>sb[i].b>>sb[i].c , sb[i].idx = i;

	if(n == 1)
	{
		cout<<"YES"<<endl;
		cout<<1<<" "<<sb[1].b;
		return 0;
	}

	sort(sb+1 , sb+1+m , comp);

	memset(dp , -1 , sizeof dp);
	for(int j=1;j<=m;j++)
	{
		for(int dx=0;dx<=sb[j].b - sb[j].a; dx++)
		dp[1][j][dx] = sb[j].a + dx;
	}

	ii finalRes = {0 , -1};
	lli maximum = -1;

	for(int i=2;i<=n;i++)
	{
//		cout<<"for "<<i<<endl;
		for(int j=i;j<=m;j++)
		{
//			cout<<"\tfor j = "<<j<<endl;

			for(int dx=0;dx<=sb[j].b-sb[j].a;dx++)
			{
				lli mx = -1;
				ii tmp = {0 , -1};

				lli d1 = -1 , d2 = -1;
				if((sb[j].a + dx) % k == 0)
				d2 = (sb[j].a + dx) / k;

				if(sb[j].a + dx > k)
				d1 = sb[j].a + dx - k;

				for(int q=1;q<j;q++)
				{
					if(sb[q].c == sb[j].c) continue;

					if(d1 > 0 && (d1 >= sb[q].a && d1 <= sb[q].b))
					{
						if(mx < dp[i-1][q][d1-sb[q].a])
						mx = dp[i-1][q][d1-sb[q].a] , tmp = {q , d1-sb[q].a};
					}

					if(d2 > 0 && (d2 >= sb[q].a && d2 <= sb[q].b))
					{
						if(mx < dp[i-1][q][d2-sb[q].a])
						mx = dp[i-1][q][d2-sb[q].a] , tmp = {q , d2-sb[q].a};
					}
				}

//				cout<<"\t\tmx = "<<mx<<" from course "<<tmp.ff<<" with homework = "<<sb[j].a + dx<<endl;
				if(mx == -1)
				dp[i][j][dx] = -1;
				else
				{
					dp[i][j][dx] = mx + sb[j].a + dx;
					pre[i][j][dx] = tmp;

					if(i == n && dp[i][j][dx] > maximum)
					finalRes = {j , dx} , maximum = dp[i][j][dx];
				}
			}
		}
	}

	if(finalRes.ss == -1)
	cout<<"NO";
	else
	{
		cout<<"YES"<<endl;
		vii res;

		int i = n;
		while(i > 0)
		{
			res.pb({sb[finalRes.ff].idx , sb[finalRes.ff].a + finalRes.ss});
			int q = finalRes.ff;
			int dx = finalRes.ss;

			finalRes = pre[i][q][dx] , i--;
		}

		reverse(res.begin() , res.end());
		for(ii p : res)
		cout<<p.ff<<" "<<p.ss<<endl;
	}
}

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>

#define ll long long int

using namespace std;

typedef pair<int,int> ii;
typedef pair<ll,ll> l2;

int n, m, k;
ll a[128], b[128], c[128];
ii pai[64][64][128];
int PD[64][64][128];
vector<l2> resp;

int da(int u, int ant, int xi) {
    if (u==n)
        return 1;

    if (PD[u][ant][xi] != -1)
        return PD[u][ant][xi];
    int cant = c[ant];
    for (int i=0;i<m;i++) if (c[i] > cant) {
        ll xim1 = a[ant]+xi;
        ll x1;
        x1 = xim1 + k;
        if (x1 > 0 and a[i] <= x1 and x1 <= b[i] and da(u+1,i,x1-a[i])) {
            pai[u][ant][xi] = ii(i,x1-a[i]);
            return PD[u][ant][xi] = 1;
        }
        x1 = xim1*k;
        if (x1 > 0 and a[i] <= x1 and x1 <= b[i] and da(u+1,i,x1-a[i])) {
            pai[u][ant][xi] = ii(i,x1-a[i]);
            return PD[u][ant][xi] = 1;
        }
    }

    return PD[u][ant][xi] = 0;
}

void anda(int u, int ant, int xi) {
    if (u==n) return;
    ii p = pai[u][ant][xi];

    resp.push_back( l2(p.first, (ll)(p.second) + a[ant]));
    anda(u+1,p.first,p.second);
}

int main() {

    cin >> n >> m >> k;
    for (int i=0;i<m;i++) {
        ll A,B;
        int C;
        cin >> A >> B >> C;
        a[i] = A;
        b[i] = B;
        c[i] = C;
    }
    memset(PD,0xff,sizeof(PD));

    //qm vai ser o 1o?
    bool blz = false;
    for (int i=0;i<m;i++)
        for (ll j = a[i]; j <= b[i];j++)
            if (da(1,i,j-a[i])) {
                blz=true;
                resp.clear();
                resp.push_back(l2(i,j));
                anda(1,i,j-a[i]);
                goto resp;
            }
    resp:;
    if (blz) {
        cout << "YES" << endl;
        for (int i=0;i<(int)resp.size();i++)
            cout << resp[i].first+1 << " " << resp[i].second << endl;
    }
    else
        cout << "NO" << endl;

    return 0;
}

// run: $exec < input
// opt: 0
// flag: -g
#include <iostream>
#include <algorithm>
#include <stack>

int const maxn = 55;
int const maxd = 105;
int const inf = 1<<29;

struct data
{
	long long a, b, c, id;
};

data da[maxn];

long long f[maxn][maxn][maxd];
std::pair<int, int> from[maxn][maxn][maxd];
int n, m, k;

std::stack<std::pair<int, int>> ans;

void print(std::pair<int, int> x)
{
	for (int i = n; i > 0; x = from[i--][x.first][x.second])
		ans.push(x);
	for (; !ans.empty(); ans.pop()) {
		int i = ans.top().first;
		std::cout << da[i].id << ' ' << da[i].a + ans.top().second << '\n';
	}
}

int main()
{
	std::cin >> n >> m >> k;
	for (int i = 1; i <= m; i++) {
		std::cin >> da[i].a >> da[i].b >> da[i].c;
		da[i].id = i;
	}
	std::sort(da + 1, da + m + 1, [](data const & a, data const & b) {
				return a.c < b.c;
			});

	for (int i = 0; i <= n; i++)
		for (int j = 0; j <= m; j++)
			for (int t = 0; t <= k; t++) f[i][j][t] = -inf;

	for (int j = 1; j <= m - n + 1; j++)
		for (int t = 0; da[j].a + t <= da[j].b; t++)
			f[1][j][t] = t + da[j].a;

	for (int i = 2; i <= n; i++) {
		for (int j = 1; j <= m - (n - i); j++) {
			for (int t = 0; da[j].a + t <= da[j].b; t++) {
				for (int u = 1; u < j; u++) {
					long long need = da[j].a + t - k;
					if (da[u].a <= need && need <= da[u].b) {
						need -= da[u].a;
						if (f[i][j][t] < f[i - 1][u][need] + da[j].a + t) {
							f[i][j][t] = f[i - 1][u][need] + da[j].a + t;
							from[i][j][t] = {u, need};
						}
					}

					need = da[j].a + t;
					if (need % k) continue;
					need /= k;
					if (da[u].a <= need && need <= da[u].b) {
						need -= da[u].a;
						if (f[i][j][t] < f[i - 1][u][need] + da[j].a + t) {
							f[i][j][t] = f[i - 1][u][need] + da[j].a + t;
							from[i][j][t] = {u, need};
						}
					}
				}
			}
		}
	}

	bool ok = false;
	long long max = -inf;
	int ti, tj;
	for (int i = 1; i <= m; i++)
		for (int t = 0; da[i].a + t <= da[i].b; t++) {
			if (f[n][i][t] > 0) {
				if (f[n][i][t] > max) {
					ok = true;
					max = f[n][i][t];
					ti = i; tj = t;
				}
			}
		}
	if (ok) {
		std::cout << "YES\n";
		print({ti, tj});
	} else
		std::cout << "NO\n";
}

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <cstdio>
#include <stdio.h>
#include <fstream>
#include <map>
#include <set>
#include <cstdlib>
#include <queue>
#include <math.h>
#include <cmath>
#include <cstring>
#include <stdlib.h>
#include <stack>
#include <iomanip>
#define ll long long
#define endl '\n'
#define ull unsigned long long
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pll pair<ll,ll>
#define pull pair<ull,ull>

ll MOD=1000000007;
ll inf=1e18;
using namespace std;

map< pair<pll,ll> , ll > Mem;
vector< pair< pll , pll > > X;
ll m;
ll a[100];
ll b[100];
ll c[100];
ll Ind[100];
ll k;
ll N[100];
ll Solve(ll i,ll p,ll cnt)
{
    if(cnt==0){return 0;}
    if(i==m){return -1;}
    pair< pll , ll > Q;
    Q=mp(mp(i,p),cnt);
    if(Mem.find(Q)!=Mem.end()){return Mem[Q];}
    ll ans=-1;
    ll temp;
    if(p==0)
    {
        for(ll x=a[i];x<=b[i];x++)
        {
            temp=Solve(N[i],x,cnt-1);
            if(temp==-1){continue;}
            ans=max(ans,x+temp);
        }
    }
    else
    {
        ll x=p+k;
        if(x>=a[i] && x<=b[i])
        {
            temp=Solve(N[i],x,cnt-1);
            if(temp!=-1)
            ans=max(ans,x+temp);
        }
        x=p*k;
        if(x>=a[i] && x<=b[i])
        {
            temp=Solve(N[i],x,cnt-1);
            if(temp!=-1)
            ans=max(ans,x+temp);
        }
    }
    ans=max(ans,Solve(i+1,p,cnt));
    Mem[Q]=ans;
    return ans;
}

int main()
{
    ios::sync_with_stdio(0);
    ll n,i,A,B,C,p;
    cin>>n>>m>>k;

    for(i=0;i<m;i++)
    {
        cin>>A>>B>>C;
        X.pb(mp(mp(C,i+1),mp(A,B)));
    }
    sort(X.begin(),X.end());

    for(i=0;i<m;i++)
    {
        c[i]=X[i].first.first;
        a[i]=X[i].second.first;
        b[i]=X[i].second.second;
        Ind[i]=X[i].first.second;
    }
    ll j=0,e;
    p=c[0];
    for(i=1;i<m;i++)
    {
        if(c[i]>p)
        {
            for(e=j;e<i;e++){N[e]=i;}
            j=i;
        }
        p=c[i];
    }
    for(e=j;e<m;e++){N[e]=m;}
    ll ans=Solve(0,0,n);
    if(ans==-1){cout<<"NO";return 0;}
    cout<<"YES\n";
    i=0;p=0;ll cnt=n;
    ll temp;
    ll next;
    bool now;
    ll v,x;
    while(1)
    {
        if(cnt==0){break;}
        //cout<<i<<" "<<p<<" "<<cnt<<"---"<<endl;
        ans=-1;
        next=N[i];
        now=true;
        v=0;
        if(p==0)
        {
            for(x=a[i];x<=b[i];x++)
            {
                temp=Solve(N[i],x,cnt-1);
                if(temp==-1){continue;}
                if(temp+x>ans){ans=temp+x;v=x;}
            }
        }
        else
        {
            x=p+k;
            if(x>=a[i] && x<=b[i])
            {
                temp=Solve(N[i],x,cnt-1);
                if(temp!=-1)
                {ans=temp+x;v=x;}
            }

            x=p*k;
            if(x>=a[i] && x<=b[i])
            {
                temp=Solve(N[i],x,cnt-1);
                if(temp!=-1)
                {ans=temp+x;v=x;}
            }
        }
        if(Solve(i+1,p,cnt)>ans){now=false;next=i+1;}
        if(now){cout<<Ind[i]<<" "<<v<<endl;cnt--;p=v;}
        i=next;

    }

    return 0;
}

/** ` Micro Mezzo Macro Flation -- Overheated Economy ., **/

#include <algorithm>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <cstring>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>

using namespace std;

#define REP(i, n) for (LL i=0;i<LL(n);++i)
#define FOR(i, a, b) for (LL i=LL(a);i<LL(b);++i)
#define DWN(i, b, a) for (LL i=LL(b-1);i>=LL(a);--i)
#define REP_1(i, n) for (LL i=1;i<=LL(n);++i)
#define FOR_1(i, a, b) for (LL i=LL(a);i<=LL(b);++i)
#define DWN_1(i, b, a) for (LL i=LL(b);i>=LL(a);--i)
#define REP_C(i, n) for (LL n____=LL(n),i=0;i<n____;++i)
#define FOR_C(i, a, b) for (LL b____=LL(b),i=a;i<b____;++i)
#define DWN_C(i, b, a) for (LL a____=LL(a),i=b-1;i>=a____;--i)
#define REP_N(i, n) for (i=0;i<LL(n);++i)
#define FOR_N(i, a, b) for (i=LL(a);i<LL(b);++i)
#define DWN_N(i, b, a) for (i=LL(b-1);i>=LL(a);--i)
#define REP_1_C(i, n) for (LL n____=LL(n),i=1;i<=n____;++i)
#define FOR_1_C(i, a, b) for (LL b____=LL(b),i=a;i<=b____;++i)
#define DWN_1_C(i, b, a) for (LL a____=LL(a),i=b;i>=a____;--i)
#define REP_1_N(i, n) for (i=1;i<=LL(n);++i)
#define FOR_1_N(i, a, b) for (i=LL(a);i<=LL(b);++i)
#define DWN_1_N(i, b, a) for (i=LL(b);i>=LL(a);--i)
#define REP_C_N(i, n) for (n____=LL(n),i=0;i<n____;++i)
#define FOR_C_N(i, a, b) for (b____=LL(b),i=a;i<b____;++i)
#define DWN_C_N(i, b, a) for (a____=LL(a),i=b-1;i>=a____;--i)
#define REP_1_C_N(i, n) for (n____=LL(n),i=1;i<=n____;++i)
#define FOR_1_C_N(i, a, b) for (b____=LL(b),i=a;i<=b____;++i)
#define DWN_1_C_N(i, b, a) for (a____=LL(a),i=b;i>=a____;--i)

#define DO(n) while(n--)
#define DO_C(n) LL n____ = n; while(n____--)
#define TO(i, a, b) LL s_=a<b?1:-1,b_=b+s_;for(LL i=a;i!=b_;i+=s_)
#define TO_1(i, a, b) LL s_=a<b?1:-1,b_=b;for(LL i=a;i!=b_;i+=s_)
#define SQZ(i, j, a, b) for (LL i=LL(a),j=LL(b)-1;i<j;++i,--j)
#define SQZ_1(i, j, a, b) for (LL i=LL(a),j=LL(b);i<=j;++i,--j)
#define REP_2(i, j, n, m) REP(i, n) REP(j, m)
#define REP_2_1(i, j, n, m) REP_1(i, n) REP_1(j, m)

#define ALL(A) A.begin(), A.end()
#define LLA(A) A.rbegin(), A.rend()
#define CPY(A, B) memcpy(A, B, sizeof(A))
#define INS(A, P, B) A.insert(A.begin() + P, B)
#define ERS(A, P) A.erase(A.begin() + P)
#define BSC(A, X) find(ALL(A), X) // != A.end()
#define CTN(T, x) (T.find(x) != T.end())
#define SZ(A) LL(A.size())
#define PB push_back
#define MP(A, B) make_pair(A, B)

#define Rush LL T____; RD(T____); DO(T____)
#pragma comment(linker, "/STACK:36777216")
#pragma GCC optimize ("O2")
#define Ruby system("ruby main.rb")
#define Haskell system("runghc main.hs")
#define Pascal system("fpc main.pas")

typedef long long LL;
typedef double DB;
typedef unsigned ULL;

typedef vector<LL> VI;
typedef vector<char> VC;
typedef vector<string> VS;
typedef vector<LL> VL;
typedef vector<DB> VD;
typedef set<LL> SI;
typedef set<string> SS;
typedef set<LL> SL;
typedef set<DB> SD;
typedef map<LL, LL> MII;
typedef map<string, LL> MSI;
typedef map<LL, LL> MLI;
typedef map<DB, LL> MDI;
typedef map<LL, bool> MIB;
typedef map<string, bool> MSB;
typedef map<LL, bool> MLB;
typedef map<DB, bool> MDB;
typedef pair<LL, LL> PII;
typedef pair<LL, bool> PIB;
typedef vector<PII> VII;
typedef vector<VI> VVI;
typedef vector<VII> VVII;
typedef set<PII> SII;
typedef map<PII, LL> MPIII;
typedef map<PII, bool> MPIIB;

/** I/O Accelerator **/

/* ... :" We are I/O Accelerator ... Use us at your own risk ;) ... " .. */

template<class T> inline void RD(T &);
template<class T> inline void OT(const T &);

inline LL RD(){ LL x; RD(x); return x;}
template<class T> inline T& _RD(T &x){ RD(x); return x;}
inline void RC(char &c){scanf(" %c", &c);}
inline void RS(char *s){scanf("%s", s);}

template<class T0, class T1> inline void RD(T0 &x0, T1 &x1){RD(x0), RD(x1);}
template<class T0, class T1, class T2> inline void RD(T0 &x0, T1 &x1, T2 &x2){RD(x0), RD(x1), RD(x2);}
template<class T0, class T1, class T2, class T3> inline void RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3){RD(x0), RD(x1), RD(x2), RD(x3);}
template<class T0, class T1, class T2, class T3, class T4> inline void RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4){RD(x0), RD(x1), RD(x2), RD(x3), RD(x4);}
template<class T0, class T1, class T2, class T3, class T4, class T5> inline void RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5){RD(x0), RD(x1), RD(x2), RD(x3), RD(x4), RD(x5);}
template<class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline void RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5, T6 &x6){RD(x0), RD(x1), RD(x2), RD(x3), RD(x4), RD(x5), RD(x6);}
template<class T0, class T1> inline void OT(T0 &x0, T1 &x1){OT(x0), OT(x1);}
template<class T0, class T1, class T2> inline void OT(T0 &x0, T1 &x1, T2 &x2){OT(x0), OT(x1), OT(x2);}
template<class T0, class T1, class T2, class T3> inline void OT(T0 &x0, T1 &x1, T2 &x2, T3 &x3){OT(x0), OT(x1), OT(x2), OT(x3);}
template<class T0, class T1, class T2, class T3, class T4> inline void OT(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4){OT(x0), OT(x1), OT(x2), OT(x3), OT(x4);}
template<class T0, class T1, class T2, class T3, class T4, class T5> inline void OT(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5){OT(x0), OT(x1), OT(x2), OT(x3), OT(x4), OT(x5);}
template<class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline void OT(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5, T6 &x6){OT(x0), OT(x1), OT(x2), OT(x3), OT(x4), OT(x5), OT(x6);}

template<class T> inline void RST(T &A){memset(A, 0, sizeof(A));}
template<class T0, class T1> inline void RST(T0 &A0, T1 &A1){RST(A0), RST(A1);}
template<class T0, class T1, class T2> inline void RST(T0 &A0, T1 &A1, T2 &A2){RST(A0), RST(A1), RST(A2);}
template<class T0, class T1, class T2, class T3> inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3){RST(A0), RST(A1), RST(A2), RST(A3);}
template<class T0, class T1, class T2, class T3, class T4> inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4){RST(A0), RST(A1), RST(A2), RST(A3), RST(A4);}
template<class T0, class T1, class T2, class T3, class T4, class T5> inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5){RST(A0), RST(A1), RST(A2), RST(A3), RST(A4), RST(A5);}
template<class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, T6 &A6){RST(A0), RST(A1), RST(A2), RST(A3), RST(A4), RST(A5), RST(A6);}

template<class T> inline void CLR(T &A){A.clear();}
template<class T0, class T1> inline void CLR(T0 &A0, T1 &A1){CLR(A0), CLR(A1);}
template<class T0, class T1, class T2> inline void CLR(T0 &A0, T1 &A1, T2 &A2){CLR(A0), CLR(A1), CLR(A2);}
template<class T0, class T1, class T2, class T3> inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3){CLR(A0), CLR(A1), CLR(A2), CLR(A3);}
template<class T0, class T1, class T2, class T3, class T4> inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4){CLR(A0), CLR(A1), CLR(A2), CLR(A3), CLR(A4);}
template<class T0, class T1, class T2, class T3, class T4, class T5> inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5){CLR(A0), CLR(A1), CLR(A2), CLR(A3), CLR(A4), CLR(A5);}
template<class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, T6 &A6){CLR(A0), CLR(A1), CLR(A2), CLR(A3), CLR(A4), CLR(A5), CLR(A6);}
template<class T> inline void CLR(T &A, LL n){REP(i, n) CLR(A[i]);}
template<class T> inline void FLC(T &A, LL x){memset(A, x, sizeof(A));}
template<class T0, class T1> inline void FLC(T0 &A0, T1 &A1, LL x){FLC(A0, x), FLC(A1, x);}
template<class T0, class T1, class T2> inline void FLC(T0 &A0, T1 &A1, T2 &A2){FLC(A0), FLC(A1), FLC(A2);}
template<class T0, class T1, class T2, class T3> inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3){FLC(A0), FLC(A1), FLC(A2), FLC(A3);}
template<class T0, class T1, class T2, class T3, class T4> inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4){FLC(A0), FLC(A1), FLC(A2), FLC(A3), FLC(A4);}
template<class T0, class T1, class T2, class T3, class T4, class T5> inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5){FLC(A0), FLC(A1), FLC(A2), FLC(A3), FLC(A4), FLC(A5);}
template<class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, T6 &A6){FLC(A0), FLC(A1), FLC(A2), FLC(A3), FLC(A4), FLC(A5), FLC(A6);}

template<class T> inline void SRT(T &A){sort(ALL(A));}
template<class T, class C> inline void SRT(T &A, C B){sort(ALL(A), B);}

/** Add - On **/

const LL MOD = 1000000007;
const LL INF = 0x7fffffff;
const DB EPS = 1e-6;
const DB OO = 1e15;

// <<= ` 0. Daily Use .,

template<class T> inline void checkMin(T &a,const T b){if (b<a) a=b;}
template<class T> inline void checkMax(T &a,const T b){if (b>a) a=b;}
template <class T, class C> inline void checkMin(T& a, const T b, C c){if (c(b,a)) a = b;}
template <class T, class C> inline void checkMax(T& a, const T b, C c){if (c(a,b)) a = b;}
template<class T> inline T min(T a, T b, T c){return min(min(a, b), c);}
template<class T> inline T max(T a, T b, T c){return max(max(a, b), c);}
template<class T> inline T sqr(T a){return a*a;}
template<class T> inline T cub(T a){return a*a*a;}
LL Ceil(LL x, LL y){return (x - 1) / y + 1;}

// <<= ` 1. Bitwise Operation .,

inline bool _1(LL x, LL i){return x & 1<<i;}
inline LL _1(LL i){return 1<<i;}
inline LL _U(LL i){return _1(i) - 1;};

inline LL count_bits(LL x){
    x = (x & 0x55555555) + ((x & 0xaaaaaaaa) >> 1);
    x = (x & 0x33333333) + ((x & 0xcccccccc) >> 2);
    x = (x & 0x0f0f0f0f) + ((x & 0xf0f0f0f0) >> 4);
    x = (x & 0x00ff00ff) + ((x & 0xff00ff00) >> 8);
    x = (x & 0x0000ffff) + ((x & 0xffff0000) >> 16);
    return x;
}

template<class T> inline T low_bit(T x) {
    return x & -x;
}

template<class T> inline T high_bit(T x) {
    T p = low_bit(x);
    while (p != x) x -= p, p = low_bit(x);
    return p;
}

// <<= ` 2. Modular Arithmetic Basic .,

inline void INC(LL &a, LL b){a += b; if (a >= MOD) a -= MOD;}
inline LL sum(LL a, LL b){a += b; if (a >= MOD) a -= MOD; return a;}
inline void DEC(LL &a, LL b){a -= b; if (a < 0) a += MOD;}
inline LL dff(LL a, LL b){a -= b; if (a < 0) a  += MOD; return a;}
inline void MUL(LL &a, LL b){a = LL((LL)a * b % MOD);}
inline LL pdt(LL a, LL b){return LL((LL)a * b % MOD);}

// <<= ' 0. I/O Accelerator LLerface .,

template<class T> inline void RD(T &x){
    cin >> x;
    //scanf("%d", &x);
    //char c; for (c = getchar(); c < '0'; c = getchar()); x = c - '0'; for (c = getchar(); c >= '0'; c = getchar()) x = x * 10 + c - '0';
    //char c; c = getchar(); x = c - '0'; for (c = getchar(); c >= '0'; c = getchar()) x = x * 10 + c - '0';

}

LL ____Case;
template<class T> inline void OT(const T &x){
    //cout << x << endl;
    prLLf("%d\n", x);
    //prLLf("%.2lf\n", x);
    //prLLf("Case %d: %d\n", ++____Case, x);
}

#define For_each(it, A) for (SII::iterator it = A.begin(); it != A.end(); ++it)

/* .................................................................................................................................. */

const LL N = 51;

struct Subject{
    LL a, b; LL c; LL id;
    bool operator <(Subject rhs) const{
        return c > rhs.c;
    }
    LL len(){
        return LL(b - a + 1);
    }
} S[N];

LL res; LL M[N]; PII A[N], B[N];
LL n, m, k;

bool cover(LL l, LL m, LL r){
    return l <= m && m <= r;
}

bool LLersect(LL l, LL r, LL ll, LL rr){
    if (ll < l) swap(l, ll), swap(r, rr);
    return l <= rr && r >= ll;
}

bool legal(LL x, LL y){
    return S[x] < S[y] && (LLersect(S[x].a, S[x].b, S[y].a * k, S[y].b * k) || LLersect(S[x].a, S[x].b, S[y].a + k, S[y].b + k));
}

#define x first
#define y second

LL F[N][102]; LL pre[N][102];

LL dp(){
    RST(F);

    LL i = 0;

#define v (S[B[i].first].a + j)

    REP_C(j, S[B[0].first].len()) F[0][j] = v;

    LL u, uu;

    FOR(i, 1, n){
        REP_C(j, S[B[i].first].len()){

            u = v + k, uu = u - S[B[i-1].first].a;

            if (cover(S[B[i-1].first].a, u, S[B[i-1].first].b)){
                if (F[i-1][uu]){
                    F[i][j] = F[i-1][uu] + v;
                    pre[i][j] = uu;
                }
            }

            u = v * k, uu = u - S[B[i-1].first].a;

            if (cover(S[B[i-1].first].a, u, S[B[i-1].first].b)){
                if (F[i-1][uu] && F[i-1][uu] + v > F[i][j] ){
                    F[i][j] = F[i-1][uu] + v;
                    pre[i][j] = uu;
                }
            }
        }
    }

    LL h = 0;

    FOR_C(j, 1, S[B[n-1].first].len()){
        if ( F[n-1][j] > F[n-1][h] ) h = j;
    }

    B[n-1].second = h;

    DWN(i, n-1, 0){
        B[i].second = pre[i+1][B[i+1].second];
    }

    return F[n-1][h];
}

void dfs(LL k = 0, LL o = 0, LL sum = 0){

    if (k == n){
        LL t = dp();
        if (t > res){
            REP(i, n) A[i] = B[i];
            res = t;
        }

    }
    else {
        if (n - k > m - o) return;
        if (sum + M[o] * (n - k) <= res) return;

        FOR(i, o, m){
            if (k == 0 || legal(B[k-1].first, i)){
                B[k].first = i; dfs(k+1, i+1, sum + S[B[k].x].b);
            }
        }
    }
}

int main(){

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

    RD(n, m, k); REP(i, m) RD(S[i].a, S[i].b, S[i].c), S[i].id = i + 1;
    sort(S, S + m); M[n] = 0; DWN(i, n, 0) M[i] = max(M[i+1], S[i].b);

    dfs();

    if (res == 0){
        puts("NO");
    }
    else {
        puts("YES");
        DWN(i, n, 0) cout << S[A[i].first].id << " " << S[A[i].first].a + A[i].second << endl;
    }
}

#include <iostream>
#include <stdio.h>
#include <cmath>
#include <vector>
#include <sstream>
#include <map>
#include <set>
#include <algorithm>
#include<string>
#include<iomanip>
#include<cstring>
#include <limits.h>

using namespace std;

#define rep(i, x, y) for(int i = x; i < y; i++)
#define Rep(i, x, y) for(int i = x; i <= y; i++)
#define vi vector<int>
#define vvi vector<vector<int> >
#define vp vector< pair< int, long long > >
#define pp push_back
#define mp make_pair
#define MOD 1000000007
typedef unsigned long long ull;

struct node
{
    int ind;
    long long a;
    long long b;
    int c;
};

bool check ( node a, node b )
{
    return a.c < b.c ;
}

vector< node > V;
long long dp[ 51 ][ 51 ][ 5001 ];
pair< int, int > pnt[ 51 ][ 51 ][ 111 ];
pair< int, int > p;
int n,m,k;
long long maxi = INT_MIN;
//vp f_ans;

long long solve( int sub, int taken, int diff, long long amount )
{
    if( taken == n )
    {
        if( amount > maxi )
            maxi = amount;
        return 0;
    }

    if ( sub == m )
        return INT_MIN;

    long long &ret = dp[ sub - 1 ][ taken ][ diff ];

    if ( ret != -1 )
        return ret;
    ret = INT_MIN;

    //long long ret = 0;

    long long tmp;
    rep( i, sub, m )
    {
        if( V[ i ].c > V[ sub-1 ].c )
        {
            long long check = V[ sub - 1 ].a + diff;

            if( V[ i ].a <= (check * k) && V[ i ].b >= (check * k ) )
            {
                long long j = (check * k) - V[ i ].a;

                tmp = solve( i+1, taken + 1, j , amount + ( check * k ) ) + ( check * k );
                if ( tmp > ret )
                {
                    ret = tmp;
                    pnt[ sub - 1 ][ taken ][ diff ] = make_pair( i, j );
                }
            }

            if( V[ i ].a <= check + k && V[ i ].b >= check + k )
            {
                long long j = check + k - V[ i ].a;
                tmp = solve( i+1, taken + 1, j , amount + check + k ) + check + k;
                if ( tmp > ret )
                {
                    ret = tmp;
                    pnt[ sub - 1 ][ taken ][ diff ] = make_pair( i, j );
                }
            }

        }
    }
    return ret;
}

int main()
{
    //freopen( "input.in", "r", stdin );
    //freopen( "output.out", "w", stdout );

    while( scanf( "%d %d %d", &n, &m, &k ) == 3 )
    {
        memset( dp, -1, sizeof(dp) );
        V.clear();
        //f_ans.clear();
        node d;
        rep( i,0,m )
        {
            cin>>d.a>>d.b>>d.c;
            //scanf( "%d %d %d", &d.a, &d.b, &d.c );
            d.ind = i+1;
            V.push_back( d );
        }
        sort( V.begin(), V.end(), check );
        long long ans = -1, tmp;
        rep( i,0,m )
        {
            for( long long j = V[ i ].a ; j <= V[ i ].b  ; j++ )
            {
                tmp = solve( i+1 ,1, j - V[ i ].a, j ) + j ;
                if ( tmp > ans )
                {
                    ans = tmp;
                    p = mp( i, j - V[ i ].a );
                }
            }
        }
        int diff = 0;
        if( maxi != INT_MIN )
        {
            puts("YES");
            rep( i,0,n )
            {
                cout<<V [ p.first ].ind<<" "<< ( long long )p.second + V [ p.first ].a<<endl;
                p = pnt[ p.first ][  i + 1 ][ p.second ];
            }
        }
        else
            puts("NO");
    }
}

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <string>
#include <cstring>
#include <cstdlib>
#include <set>
#include <map>
using namespace std;
#define forn(i, n) for (int i = 0; i < (int)(n); i++)
#define fore(i, b, e) for (int i = (int)(b); i <= (int)(e); i++)
typedef pair<int, int> pii;
typedef long long ll;
typedef long double ld;
#define se second
#define fi first
#define pb push_back
#define mp make_pair
const int maxn = 105;
const ll inf = (ll)1e17;

struct les
{
	int a, b, c, i;
	bool operator< (const les& q) const
	{
		return c < q.c;
	}
	les(){};
	les(int A, int B, int C, int I):a(A),b(B),c(C),i(I){};
};

ll d[55][55][110];
int anc[55][55][110];
int n, m, u;
les a[maxn];

bool cmp(les a, les b)
{
	return a.i < b.i;
}
void scan()
{
	cin >> n >> m >> u;
	forn(i, m)
	{
		cin >> a[i].a >> a[i].b >> a[i].c;
		a[i].i = i;
	}
	sort(a, a+m);
}
void dp()
{
	forn(i, 55) forn(j, 55) forn(p, 110)
		d[i][j][p] = -1;
	forn(j, m) forn(k, a[j].b - a[j].a + 1)
	{
		//cout << 0 << " " << j << " " << k << endl;
		d[0][j][k] = a[j].a + k;
	}

	forn(i, n) forn(j, m) forn(k, a[j].b - a[j].a + 1)
	{
		if (d[i][j][k] == -1)
			continue;
		ll curc = a[j].a + k, nxtc;
		//cout << i << " " << j << " " << k << "   " << curc << endl;

		forn(I, 2)
		{
			if (I == 0)
				nxtc = curc + u;
			else
				nxtc = curc * u;
			//cout << nxtc << endl;
			for (int p = j + 1; p < m; p++) if (a[j].c < a[p].c)
				if (nxtc >= a[p].a && nxtc <= a[p].b)
					if (d[i+1][p][nxtc - a[p].a] < d[i][j][k] + nxtc)
					{
						//cout << i+1 << " " << p << " " << nxtc - a[p].a << endl;
						d[i+1][p][nxtc - a[p].a] = d[i][j][k] + nxtc,
						anc[i+1][p][nxtc - a[p].a] = (p-j) * (I?1:(-1));
					}
		}
		//cout << endl;
	}
}
void getans()
{
	ll mx = -1, J=0, K=9;
	forn(j, m) forn(k, a[j].b - a[j].a + 1)
		if (d[n-1][j][k] != -1)
			mx = max(mx, d[n-1][j][k]), J=j, K=k;

	if (mx == -1)
	{
		cout << "NO\n";
		return;
	}
	cout << "YES\n";
	vector<pair<int, pair<int, ll> > > res; //i, num, amt
	for (int i = n-1; i >= 0; i--)
	{
		res.pb(mp(a[J].i, mp(J+1, K+a[J].a)));
		ll curc = K + a[J].a, nxtc;
		//cout << J << " " << K << " " << curc << endl;
		if (anc[i][J][K] > 0)
			nxtc = curc / u;
		else
			nxtc = curc - u;
		if (i)
		{
			J -= abs(anc[i][J][K]);
			K = nxtc - a[J].a;
		}
	}
	sort(res.begin(), res.end());
	forn(i, res.size())
		cout << res[i].se.fi << " " << res[i].se.se << endl;
}

int main()
{
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);

	scan();
	dp();
	getans();

	return 0;
}

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define PB push_back
#define SZ(X) ((int)(X.size()))
#define ALL(X) (X).begin(), (X).end()
#define FE(c,itr) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)
//~ #define MOD 1000000007ll;

const double PI=acos(-1.0);//NOTES:pi
const double EPS=1e-11;//NOTES:eps

typedef long long ll;
typedef pair<int,ll> pii;

struct subject{
  long long a,b,c;
  int ind;
  bool operator<(const subject &o) const {return c<o.c;}
};

ll dp[55][55][110];
ll dp2[55][55][110];

int main(){
  int N,M,K;
  ll ax1,ax2,ax3;
  subject s[60];
  for(;~scanf("%d%d%d",&N,&M,&K);){
    memset(dp,0,sizeof dp);

    for(int i=0;i<M;i++){
      cin>>ax1>>ax2>>ax3;
      //~ scanf("%lld %lld %lld",&ax1,&ax2,&ax3);
      s[i].a=ax1;
      s[i].b=ax2;
      s[i].c=ax3;
      s[i].ind=i+1;
    }
    sort(s,s+M);

    for(int k=0;k<M;k++){
      for(ll i=s[k].a;i<=s[k].b;i++){
        dp[0][k][i-s[k].a]=1;
        dp2[0][k][i-s[k].a]=(i+1);
      }
    }

    for(int i=1;i<N;i++){
      for(int k=i;k<M;k++){
        for(ll j=s[k].a;j<=s[k].b;j++){
          dp[i][k][j-s[k].a]=0;
          dp2[i][k][j-s[k].a]=0;
          for(int l=0;l<k && s[l].c<s[k].c;l++){
            if( j-K<=s[l].b && j-K>=s[l].a && dp[i-1][l][j-K-s[l].a] ){
              if(dp2[i][k][j-s[k].a]<j+dp2[i-1][l][j-K-s[l].a]){
                dp[i][k][j-s[k].a]=(l+1);
                dp2[i][k][j-s[k].a]=j+dp2[i-1][l][j-K-s[l].a];
              }
            }
            if( j%K==0 && j/K<=s[l].b && j/K>=s[l].a && dp[i-1][l][(j/K)-s[l].a] ){
              if(dp2[i][k][j-s[k].a]<j+dp[i-1][l][(j/K)-s[l].a]){
                dp[i][k][j-s[k].a]=(M+5)*(l+1);
                dp2[i][k][j-s[k].a]=j+dp[i-1][l][(j/K)-s[l].a];
              }
            }
          }
        }
      }
    }

    bool voy=false;
    int posi;
    ll posj;
    ll mxx=0;
    //~ cout << "asdasd" << endl;
    //~ for(int i=0;i<=N;i++){
      //~ for(int j=0;j<=M;j++){
        //~ for(int k=0;k<44;k++){
          //~ cout<<k<<"/"<<j<<"/"<<dp[i][j][k]<<" ";
        //~ }
        //~ cout<<endl;
      //~ }
      //~ cout<<endl;
      //~ cout<<endl;
    //~ }
    //~

    for(int i=0;i<M;i++){
      for(ll j=s[i].a;j<=s[i].b;j++){
        //~ cerr<< i<<" " << s[i].b<< " " << j<< " "<<mxx << " " << dp[N-1][i][j-s[i].a] << endl;
        if(mxx<dp2[N-1][i][j-s[i].a]){
          voy=true;
          mxx=dp[N-1][i][j-s[i].a];
          posi=i;
          posj=j;
        }
      }
    }
    //~ cerr<<mxx<<endl;

    if(!voy){
      cout<<"NO"<<endl;
      continue;
    }else{
      cout<<"YES"<<endl;
      stack<pii> pila;
      pii actual;
      int ptr;
      int day=N;
      int sub;
      ll pnts;
      pila.push(pii(posi,posj));
      while(pila.size()!=N){
        day-=1;
        actual=pila.top();
        ptr=dp[day][actual.first][actual.second-s[actual.first].a];
        if(ptr>M+2){
          sub=(ptr)/(M+5)-1;
          pnts=actual.second/K;
        }else{
          sub=(ptr)-1;
          pnts=actual.second-K;
        }
        pila.push(pii(sub,pnts));
      }
      while(!pila.empty()){
        actual=pila.top();
        pila.pop();
        cout<< s[actual.first].ind<<" "<< actual.second<< endl;
      }

    }
  }

  return 0;
}

# include <iostream>
# include <string>
# include <vector>
# include <math.h>
# include <algorithm>
# include <string.h>
# include <stack>
# include <queue>
# include <sstream>
# include <set>
# include <map>
# include <stdio.h>
using namespace std;
int main()
{
    long long n, m, kkk;
    cin >> n >> m >> kkk;
    vector<pair<long long, pair<long long, long long> > >a(m), b;
    for (long long i = 0; i < m; i++)
    {
        cin >> a[i].second.first >> a[i].second.second >> a[i].first;
    }
    b = a;
    sort(a.begin(), a.end());
    vector<long long> or_ind(m);
    vector<bool> used(m, false);
    for (long long i = 0; i < m; i++)
    {
        for (long long j = 0; j < m; j++)
            if (!used[j] && a[i] == b[j])
            {
                         or_ind[i] = j;
                         used[j] = true;
            }
    }
    map<long long, bool> f[55][55];
    map<long long, pair<long long, long long> > prev[55][55];
    for (long long i = 0; i < m; i++)
    {
        for (long long j = a[i].second.first; j <= a[i].second.second; j++)
        {
            f[0][i][j] = true;
            prev[0][i][j] = make_pair(-1, -1);
        }
    }
    for (long long i = 1; i < n; i++)
    {
        for (long long j = 0; j < m; j++)
        {
            for (long long op = a[j].second.first; op <= a[j].second.second; op++)
                for (long long k = 0; k < j; k++)
                {
                    if (f[i - 1][k][op - kkk])
                    {
                       f[i][j][op] = true;
                       prev[i][j][op] = make_pair(k, op - kkk);
                    }
                    if (op % kkk == 0 && f[i - 1][k][op / kkk])
                    {
                       f[i][j][op] = true;
                       prev[i][j][op] = make_pair(k, op / kkk);
                    }
                }
        }
    }
    for (long long i = 0; i < m; i++)
    {
        for (map<long long, bool>::iterator it = f[n - 1][i].begin(); it != f[n - 1][i].end(); it++)
        {
            long long f = (*it).first;
            vector<pair<long long, long long> > ans;
            long long num = f, cur = i;
            long long ind = n - 1;
            while (!(num == -1 && cur == -1))
            {
                  ans.push_back(make_pair(cur, num));
                  pair<long long, long long> t = prev[ind--][cur][num];
                  cur = t.first;
                  num = t.second;
            }
            reverse(ans.begin(), ans.end());
            cout << "YES" << endl;
            for (long long j = 0; j < ans.size(); j++)
            {
                cout << or_ind[ans[j].first] + 1 << " " << ans[j].second << endl;
            }
            //system("pause");
            return 0;
        }
    }
    cout << "NO";
    //system("pause");
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
long long dp[55][100][55];
struct E
{
    long long a,b,c,d;
} e[55];
long long ne[55][100][2];
bool cmp(E a,E b)
{
    return a.c<b.c;
}
int main()
{
    int n,m,k;
    cin>>n>>m>>k;
    for(int i=0; i<m; i++)
    {
        scanf("%I64d%I64d%I64d",&e[i].a,&e[i].b,&e[i].c);
        e[i].d=i+1;
    }
    sort(e,e+m,cmp);
    for(int i=0; i<m; i++)
        for(int j=0; j<=e[i].b-e[i].a; j++)
            dp[i][j][1]=j+e[i].b;
    if(n==1)
    {
        long long maxn=0;
        long long pos=0;
        for(int i=0;i<m;i++)
        {
            if(e[i].b>maxn)
            {
                maxn=e[i].b;
                pos=e[i].d;
            }
        }
        cout<<"YES"<<endl;
        cout<<pos<<" "<<maxn<<endl;
        return 0;
    }
    for(int len=2; len<=n; len++)
    {
        for(int i=0; i<=m-len; i++)
        {
            for(long long val=e[i].a; val<=e[i].b; val++)
            {
                for(int j=i+1; j<=m-len+1; j++)
                {
                    long long temp1=val+k;
                    long long temp2=val+k-e[j].a;
                    long long temp3=val-e[i].a;
                    if(val+k>=e[j].a&&val+k<=e[j].b&&dp[j][temp2][len-1])
                    {
                        if(dp[j][temp2][len-1]+dp[i][temp3][1]>dp[i][temp3][len])
                        {
                            ne[i][temp3][0]=j;
                            ne[i][temp3][1]=temp2;
                            dp[i][temp3][len]=dp[j][temp2][len-1]+dp[i][temp3][1];
                        }
                    }
                    temp2=val*k-e[j].a;
                    temp3=val-e[i].a;
                    if(val*k>=e[j].a&&val*k<=e[j].b&&dp[j][temp2][len-1])
                    {
                        if(dp[j][temp2][len-1]+dp[i][temp3][1]>dp[i][temp3][len])
                        {
                            ne[i][temp3][0]=j;
                            ne[i][temp3][1]=temp2;
                            dp[i][temp3][len]=dp[j][temp2][len-1]+dp[i][temp3][1];
                        }
                    }
                }
            }
        }
    }
    long long maxn=0;
    long long pos=0;
    long long val=0;
    for(int i=0;i<m;i++)
    {
        for(long long j=0;j<=e[i].b-e[i].a;j++)
        {
            if(dp[i][j][n]>maxn)
            {
                maxn=dp[i][j][n];
                pos=i;
                val=j;
            }
        }
    }
    if(maxn)
    {
        cout<<"YES"<<endl;
        for(int i=0;i<n;i++)
        {
            cout<<e[pos].d<<" "<<e[pos].a+val<<endl;
            long long temp1=pos,temp2=val;
            pos=ne[temp1][temp2][0];
            val=ne[temp1][temp2][1];
        }
    }
    else cout<<"NO"<<endl;
    return 0;
}

#include<bits/stdc++.h>
#define all(v) (v).begin(),(v).end()
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define ri(x) scanf("%d",&(x))
#define ri2(x,y) scanf("%d %d",&(x),&(y))
#define ri3(x,y,z) scanf("%d %d %d",&(x),&(y),&(z))
#define rll(x) scanf("%lld",&(x))
#define rll2(x,y) scanf("%lld %lld",&(x),&(y))
#define rll3(x,y,z) scanf("%lld %lld %lld",&(x),&(y),&(z))
#define gc(x) ((x) = getchar())
using namespace::std;

const long double PI = acos(-1);
const long long MOD = 1000000000 +7;

typedef long long ll;
typedef pair<ll,ll> pll;
typedef pair<ll,pll> tll;
typedef pair<int,int> ii;
typedef pair<int,ii> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<iii> viii;
typedef vector<ll> vll;
typedef vector<pll> vpll;
typedef vector<tll> vtll;
typedef vector<string> vs;
typedef set<int> si;
typedef set<ii> sii;
typedef set<iii> siii;

ll gcd(ll a, ll b){ return b==0?a:gcd(b,a%b);}

ll add(ll a, ll b, ll m = MOD){
	if(a >= m) a %= m;
	if(b >= m) b %= m;
	if(a < 0) a += m;
	if(b < 0) b += m;
	ll res = a+b;
	if(res >= m or res <= -m) res %= m;
	if(res < 0) res += m;
	return res;
}

ll mul(ll a, ll b, ll m = MOD){
	if(a >= m) a %= m;
	if(b >= m) b %= m;
	if(a < 0) a += m;
	if(b < 0) b += m;
	ll res = a*b;
	if(res >= m or res <= -m) res %= m;
	if(res < 0) res += m;
	return res;
}

ll pow_mod(ll a, ll b, ll m = MOD){
	ll res = 1LL;
	a = a%m;
	while(b){
		if(b&1) res = mul(res,a,m);
		b >>= 1;
		a = mul(a,a,m);
	}
	return res;
}

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

int gcdExtendido(int a, int b, int *x, int *y){
	if(a == 0){
		*x = 0;
		*y = 1;
		return b;
	}
	int x1, y1;
	int gcd = gcdExtendido(b%a,a,&x1,&y1);

	*x = y1-(b/a)*x1;
	*y = x1;
	return gcd;
}

int modInverso(int a, int m){
	int x, y;
	int g = gcdExtendido(a,m,&x,&y);
	if(g!=1) return -1;
	else return (x%m + m)%m;
}

/****************************************
*************P*L*A*N*T*I*L*L*A************
*****************************************/

const int N = 50+5;
const int MAX = 100+5;
const ll inf = 1LL<<60;

int n;
int m;
int k;
vector<pair<int,pll>> v;
ll memo[N][N][MAX];
int choiceX[N][N][MAX];
int choiceD[N][N][MAX];

pair<ll,int> maximum(int i, int len, ll val){
	pair<ll, int> ans = mp(-inf, -1);
	if(v[i].second.first <= val - k and val - k <= v[i].second.second){
		ans = mp(memo[i][len-1][val - k - v[i].second.first], val - k - v[i].second.first);
	}
	if(val % k == 0 and v[i].second.first <= val / k and val / k <= v[i].second.second){
		if(ans.first < memo[i][len-1][val / k - v[i].second.first]){
			ans = mp(memo[i][len-1][val / k - v[i].second.first], val / k - v[i].second.first);
		}
	}
	return ans;
}

int main(){
	ri3(n, m, k);
	v.resize(m + 1);
	for(int i=1; i<=m; i++){
		rll2(v[i].second.first,v[i].second.second);
		ri(v[i].first);
	}
	vi p(m+1);
	iota(all(p),0);
	sort(p.begin() + 1, p.end(), [&](int i, int j){
		return v[i] < v[j];
	});
	memset(memo, -1, sizeof memo);
	for(int i=0; i<MAX; i++){
		memo[0][0][i] = 0;
	}
	for(int x = 1; x <= m; x++){
		int i = p[x];
		for(int l = 1; l <= n; l++){
			for(int d = 0; d <= v[i].second.second - v[i].second.first; d++){
				memo[i][l][d] = l == 1? v[i].second.first + d : -1;
				for(int y = 1; y < i; y++){
					int j = p[y];
					if(v[j].first == v[i].first) continue;
					pair<ll,int> adding = maximum(j, l, d + v[i].second.first);
					if(adding.first >= 0){
						ll cand = adding.first + d + v[i].second.first;
						if(cand > memo[i][l][d]){
							memo[i][l][d] = cand;
							choiceX[i][l][d] = j;
							choiceD[i][l][d] = adding.second;
						}
					}
				}
			}
		}
	}
	int pos = 1;
	int d = 0;
	for(int x=1; x<=m; x++){
		int i = p[x];
		for(int dif = 0; dif <= v[i].second.second - v[i].second.first; dif++){
			if(memo[pos][n][d] < memo[x][n][dif]){
				pos = x;
				d = dif;
			}
		}
	}
	if(memo[pos][n][d] == -1){
		puts("NO");
		return 0;
	}
	stack<pair<int,ll>> S;
	int len = n;
	while(pos > 0){
		S.emplace(mp(p[pos], d + v[p[pos]].second.first));
		int nxtpos = choiceX[pos][len][d];
		int nxtd = choiceD[pos][len][d];
		len -= 1;
		pos = nxtpos;
		d = nxtd;
	}
	while(!S.empty()){
		printf("%d %lld\n",S.top().first,S.top().second);
		S.pop();
	}
	return 0;
}

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <string>
#include <set>
#include <map>
#include <numeric>
#include <functional>
#define rep(i,n) for(int i=0;i<(n);++i)
#define foreach(i,v) for(__typeof(v.begin()) i=v.begin();i!=v.end();++i)
#define ass(v) (v)||++*(int*)0;
using namespace std;
typedef pair<int,int> PII;
typedef vector<PII> VPII;
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<bool> VB;
typedef vector<double> VD;
struct info
{
    long long a,b;
    int c;
    int i;
    info(long long _a=0,long long _b=0,int _c=0,int _i=0)
    {
        a=_a;
        b=_b;
        c=_c;
        i=_i;
    }
    bool operator<(const info &o) const
    {
        return c<o.c;
    }
};
int main()
{
    int n,m,k;
    scanf("%d%d%d",&n,&m,&k);
    vector<info> p(m);
    rep(i,m)
    {
        scanf("%lld%lld%d",&p[i].a,&p[i].b,&p[i].c);
        p[i].i=i;
    }
    sort(p.begin(),p.end());
    VI tb(m);
    static long long f[60][60][110];
    static int pn[60][60][110],pl[60][60][110];
    memset(f,255,sizeof(f));
    for(int i=0;i<=m;i++) memset(f[i][0],0,sizeof(f[i][0]));
    for(int i=1;i<=m;i++)
    {
        for(int l=0;l<=p[i-1].b-p[i-1].a;l++)
            f[i][1][l]=p[i-1].a+l;
        for(int j=2;j<=n;j++)
        {
            for(int l=0;l<=p[i-1].b-p[i-1].a;l++)
            {
                long long &ans=f[i][j][l];
                int &qn=pn[i][j][l],&ql=pl[i][j][l];
                long long z=p[i-1].a+l;
                if(z%k==0)
                {
                    long long y=z/k;
                    for(int e=1;e<i;e++)
                    {
                        if(y<p[e-1].a||y>p[e-1].b) continue;
                        int b=y-p[e-1].a;
                        if(f[e][j-1][b]<0) continue;
                        if(ans<f[e][j-1][b]+z)
                        {
                            ans=f[e][j-1][b]+z;
                            qn=e;
                            ql=b;
                        }
                    }
                }
                {
                    long long y=z-k;
                    for(int e=1;e<i;e++)
                    {
                        if(y<p[e-1].a||y>p[e-1].b) continue;
                        int b=y-p[e-1].a;
                        if(f[e][j-1][b]<0) continue;
                        if(ans<f[e][j-1][b]+z)
                        {
                            ans=f[e][j-1][b]+z;
                            qn=e;
                            ql=b;
                        }
                    }
                }
            }
        }
    }
    long long ans=-1,qn=-1,ql=-1;
    for(int i=n;i<=m;i++)
        for(int j=0;j<=p[i-1].b-p[i-1].a;j++)
        {
            if(ans<f[i][n][j])
            {
                ans=f[i][n][j];
                qn=i;
                ql=j;
            }
        }
    //printf("p.size=%d\n",p.size());
    if(ans<0) puts("NO");
    else
    {
        puts("YES");
        VI x;
        vector<long long> y;
        x.clear();
        y.clear();
        for(int i=n;i>0;i--)
        {
            //printf("qn=%d ql=%d\n",qn,ql);
            x.push_back(qn);
            //puts("C");
            y.push_back(ql+p[qn-1].a);
            //puts("A");
            int pp=pn[qn][i][ql];
            int ll=pl[qn][i][ql];
            //puts("B");
            qn=pp;
            ql=ll;
        }
        for(int i=n-1;i>=0;i--) printf("%d %lld\n",p[x[i]-1].i+1,y[i]);
    }
    return 0;
}

#pragma comment(linker, "/stack:64000000")

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cstring>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <algorithm>
#include <sstream>
#include <cassert>

using namespace std;

#define REP(i, n) for (int (i) = 0; (i) < (n); (i)++)
#define FOR(i, a, b) for (int (i) = (a); (i) < (b); (i)++)
#define sz(v) (int)(v).size()
#define all(v) (v).begin(), (v).end()
#define mp make_pair
#define pb push_back

typedef long long LL;

struct Lesson {
	long long L, R;
	int D, N;

	Lesson() {}

	Lesson(long long L, long long R, int D, int N): L(L), R(R), D(D), N(N) {}

	bool operator<(const Lesson& lesson) const {
		return D < lesson.D;
	}
};

struct Prev {
	int num, exc;

	Prev() {}

	Prev(int num, int exc): num(num), exc(exc) {}
};

long long dp[55][55][111];
Prev pr[55][55][111];

int main() {
#ifndef ONLINE_JUDGE
	#define NAME "test"
	freopen(NAME".in", "r", stdin);
	freopen(NAME".out", "w", stdout);
	long long start = clock();
#endif
	//cout << "50 50 1\n";
	//REP(i, 50) cout << "9999999999999900 10000000000000000 " << i + 1 << "\n";
	int need, m, k;
	scanf("%d %d %d", &need, &m, &k);
	vector <Lesson> v(m);
	REP(i, m) {
		long long L, R;
		int D;
		cin >> L >> R >> D;
		v[i] = Lesson(L, R, D, i + 1);
	}
	sort(all(v));
	memset(dp, 0, sizeof(dp));
	memset(pr, -1, sizeof(pr));
	REP(i, m) REP(j, (int)(v[i].R - v[i].L) + 1) dp[1][i][j] = v[i].L + j;
	for (int done = 1; done < need; done++) {
		for (int cur = 0; cur < m; cur++) {
			for (int exc = 0; exc <= (int)(v[cur].R - v[cur].L); exc++) {
				long long did = v[cur].L + exc;
				if (dp[done][cur][exc] == 0) continue;
				for (int nxt = cur + 1; nxt < m; nxt++) {
					if (v[nxt].D > v[cur].D) {
						long long val[] = {did + k, did * k};
						REP(j, 2) {
							if (v[nxt].L <= val[j] && val[j] <= v[nxt].R) {
								int t = (int)(val[j] - v[nxt].L);
								if (dp[done + 1][nxt][t] < dp[done][cur][exc] + j) {
									dp[done + 1][nxt][t] = dp[done][cur][exc] + j;
									pr[done + 1][nxt][t] = Prev(cur, exc);
								}
							}
						}
					}
				}
			}
		}
	}
	int B = -1, C = -1;
	REP(i, 111) REP(j, 111) {
		if (dp[need][i][j] > 0 && (B == -1 || dp[need][i][j] > dp[need][B][C])) {
			B = i, C = j;
		}
	}
	if (B != -1) {
		vector <pair <int, long long> > res(need);
		for (int A = need; A >= 1; A--) {
			res[A - 1] = mp(v[B].N, v[B].L + C);
			int b = B, c = C;
			B = pr[A][b][c].num;
			C = pr[A][b][c].exc;
		}
		cout << "YES\n";
		REP(i, sz(res)) {
			cout << res[i].first << " " << res[i].second << "\n";
		}
	} else {
		cout << "NO\n";
	}
#ifndef ONLINE_JUDGE
	printf("%.10lf\n", (clock() - start) / 1000.);
#endif
    return 0;
}

#include<bits/stdc++.h>

using namespace std;

pair<int,int> arr[55];
pair<int,int> ar[55];
int n , m , k;
int c = 0;
long long dp[55][55][5100];
map<int,int> mp;

long long solve(int pos , int lastx , int lastc , int cnt)
{
        if(cnt == n)return 0;
        if(pos >= m)return -2e9;
        if(!mp.count(lastx))mp[lastx] = c++;
        if( dp[pos][cnt][mp[lastx]] != -1)
        {
            return dp[pos][cnt][mp[lastx]];
        }

            long long e = -1e9;
            e = solve(pos + 1 , lastx , lastc , cnt);
            int realpos = arr[pos].second;
            if(arr[pos].first > lastc)
            {
                for(int j = ar[realpos].first ;j <= ar[realpos].second ; j++)
                {
                    if((lastx == 0 && lastc == 0)  || j == lastx*k || j == lastx + k){
                        int one = j + solve(pos + 1, j , arr[pos].first,cnt + 1);
                        if(one > e)
                        {
                            e = one ;
                        }
                    }
                }
            }
        return dp[pos][cnt][mp[lastx]] = e;
}
void print_soln(int pos , int lastx , int lastc , int cnt )
{
    if(cnt == n)return;
    if(pos == m)return;

    if(dp[pos][cnt][mp[lastx]] > 0 && dp[pos][cnt][mp[lastx]]== solve(pos + 1 , lastx,lastc , cnt))
    {
                          print_soln(pos + 1 , lastx,lastc,cnt);
    }
    else if(arr[pos].first > lastc)
   {
            int realpos = arr[pos].second;
            for(int j = ar[realpos].first ;j <= ar[realpos].second ; j++)
                {
                    if((lastx == 0)  || j == lastx*k || j == lastx + k)
                    {
                      if(dp[pos][cnt][mp[lastx]] == j + solve(pos+1,j,arr[pos].first,cnt + 1))
                      {

                          printf("%d %d\n",realpos + 1,j);
                          print_soln(pos + 1 , j , arr[pos].first , cnt + 1);
                      }
                    }
                }

            }

}
int main()
{

   cin >> n >> m >> k;
   pair<int,int>  x ;
   for(int i = 0 ; i < 55;i++)arr[i].first = 2e9;
   for(int i = 0 ; i < m ; i++)
   {
    cin >> ar[i].first >> ar[i].second;

    cin >> x.first ;
    x.second = i;
    arr[i] = x;
   }

    memset(dp,-1,sizeof dp);
    sort(arr,arr + 55);
    long long y = solve(0,0,0,0);
    if(y < 0)
    printf("NO\n");
    else
    {
        printf("YES\n");
        print_soln(0,0,0,0);
    }

    return 0;
}

#include <iostream>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <string>
#include <sstream>
#include <stack>
#include <queue>
#include <map>
#include <numeric>
#include <cstdlib>
#include <cstring>
#include <utility>
#include <functional>
#include <cstdio>
#include <ctime>
using namespace std ;
#define eps 1e-10
#define ll long long
#define ld long double
#define ull unsigned long long
#define REP(i,k,n) for(int i=k;i<n;i++)
#define REV(i,k,n) for(int i=k;i>=n;i--)
#define SZ(x) ((int)x.size())
#define CLR(c,v) memset(c,v,sizeof (c))
////////////////////////////////
ll max (ll a,ll b) { return a > b ? a : b ; }
ll min (ll a,ll b) { return a < b ? a : b ; }
bool Eq (double a,double b) { return fabs (a - b) <= eps ; }
ll ABS (ll x) { return x < 0 ? - x : x ; }
//////////////////////
const int NN = 52 , MaxVal = 1000 * 1000 + 2 , p  = 43 , o = 83 , INF = (1 << 28) ;
//const ll  INF = 12345678901LL , MOD2 = 1000000007 ;
/////////////////////
int xx [] = {-1,-1,-1,0,0,1,1,1} ;
int yy [] = {-1,0,1,-1,1,-1,0,1} ;
///////////////////////////
int bit (int x) { return x == 0 ? 0 : 1 + bit (x & (x - 1)) ; }

int a [NN] , b [NN] , c [NN] ;
int n,m,k ;

int dp [NN][NN][NN * 2][NN] ; // cur , pre , dist , used ;
struct path {
	char pos,pre,dist,used;
	int cost ;
	path () {} ;
	path (char Pos,char Pre,char Dist,char Used,int Cost) {
		pos = Pos ;
		pre = Pre ;
		dist = Dist ;
		used = Used ;
		cost = Cost ;
	}
};

path nex [NN][NN][NN * 2][NN] ;

int rec (int pos,int pre,int dist,int used) {
	if (used == 0) {
		return 0 ;
	}
	if (pos == m) {
		return -(INF) ;
	}

	int &res = dp [pos][pre][dist][used] ;
	if (res != -1) return res ;
	res = -INF ;

	int exer = used == n ? -1 : a [pre] + dist ;

	res = rec (pos + 1,pre,dist,used) ;
	nex [pos][pre][dist][used] = path (pos + 1,pre,dist,used,0) ;

	if (exer == -1) {
		REP (i,a [pos],b [pos] + 1) {
			int aux = rec (pos + 1,pos,i - a[pos],used - 1) + i ;
			if (aux > res) {
				res = aux ;
				nex [pos][pre][dist][used] = path (pos + 1,pos,i - a[pos],used - 1,i) ;
			}
		}
	}else {
		if (c [pos] > c [pre]) {
			if (exer + k >= a [pos] && exer + k <= b [pos]) {
				int aux = rec (pos + 1,pos,exer + k - a [pos],used - 1) + exer + k ;
				if (aux > res) {
					res = aux ;
					nex [pos][pre][dist][used] = path (pos + 1,pos,exer + k - a [pos],used - 1,exer + k) ;
				}
			}
			if (exer * k >= a [pos] && exer * k <= b [pos]) {
				int aux = rec (pos + 1,pos,exer * k - a [pos],used - 1) + exer * k ;
				if (aux > res) {
					res = aux ;
					nex [pos][pre][dist][used] = path (pos + 1,pos,exer * k - a [pos],used - 1,exer * k) ;
				}
			}
		}
	}
	return res ;
}

int main () {
	//////////////////
#ifdef HOMEPC
	freopen ("in.in","r",stdin) ;
#endif
    ////////////////// Code ///////////////////
	scanf ("%d%d%d",&n,&m,&k) ;
	REP (i,0,m) {
		scanf ("%d%d%d",&a[i],&b[i],&c[i]) ;
	}
	//REP (i,0,m) REP (j,i + 1,m) if (c [i] > c [j]) {
	//	swap (a[i],a[j]) ;
	//	swap (b[i],b[j]) ;
	//	swap (c[i],c[j]) ;
	//}
	CLR (dp,-1) ;
	int ans = rec (0,0,0,n) ;
	if (ans < 0) {
		cout << "NO" << endl ;
	}else {
		cout << "YES" << endl ;
		int pos = 0 , pre = 0 , dist = 0 , used = n ;
		while (used > 0) {
			path p = nex [pos][pre][dist][used] ;
			if (p.used + 1 == used) {
				cout << pos + 1 << ' ' << p.cost << endl ;
			}
			pos = p.pos , pre = p.pre , dist = p.dist , used = p.used ;
		}
	}
    return 0 ;
}

#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
using namespace std;

struct subject{
    long long a,b;
    int c,id;
    bool operator<(subject& t){
        return c<t.c;
    }
}arr[60];
struct route{
    int addition,id;
    route(){}
    route(int x,int y):id(x),addition(y){}
}pre[60][60][120],ending;

int n,m,k;
long long dp[60][60][120];//day/num/work

bool makeDp(){
    memset(dp,-1,sizeof(dp));
    for(int j=1;j<=m;j++)
        for(long long i=arr[j].a;i<=arr[j].b;i++){
            dp[1][j][i-arr[j].a]=i;
        }
    for(int i=1;i<n;i++){
        for(int j=i;j<=m&&m-j>=n-i;j++){
            for(long long kk=arr[j].a;kk<=arr[j].b;kk++){
                if(dp[i][j][kk-arr[j].a]==-1)
                    continue;
                for(int jj=j+1;jj<=m;jj++){
                    if(k*kk>=arr[jj].a&&k*kk<=arr[jj].b){
                        if(dp[i+1][jj][k*kk-arr[jj].a]<dp[i][j][kk-arr[j].a]+k*kk){
                            dp[i+1][jj][k*kk-arr[jj].a]=dp[i][j][kk-arr[j].a]+k*kk;
                            pre[i+1][jj][k*kk-arr[jj].a]=route(j,kk-arr[j].a);
                        }
                    }
                    if(k+kk>=arr[jj].a&&k+kk<=arr[jj].b){
                        if(dp[i+1][jj][k+kk-arr[jj].a]<dp[i][j][kk-arr[j].a]+k+kk){
                            dp[i+1][jj][k+kk-arr[jj].a]=dp[i][j][kk-arr[j].a]+k+kk;
                            pre[i+1][jj][k+kk-arr[jj].a]=route(j,kk-arr[j].a);
                        }
                    }
                }
            }
        }
    }
    long long flag=-1;
    for(int i=n;i<=m;i++){
        for(long long j=arr[i].a;j<=arr[i].b;j++){
            if(flag<dp[n][i][j-arr[i].a]){
                flag=dp[n][i][j-arr[i].a];
                ending=route(i,j-arr[i].a);
            }
        }
    }
    if(flag!=-1)
        return true;
    return false;
}

void findAns(bool x){
    if(!x)
        cout<<"NO";
    else {
        cout<<"YES\n";
        vector<route> ans;
        route ha=ending;
        for(int i=n;i>=1;i--){
            ans.push_back(ha);
            ha=pre[i][ha.id][ha.addition];
        }
        for(int i=n-1;i>=0;i--){
            cout<<arr[ans[i].id].id<<" "<<arr[ans[i].id].a+ans[i].addition<<endl;
        }
    }
}

int main(){
    cin>>n>>m>>k;
    for(int i=1;i<=m;i++){
        cin>>arr[i].a>>arr[i].b>>arr[i].c;
        arr[i].id=i;
    }
    sort(arr+1,arr+1+n);
    findAns(makeDp());
    return 0;
}

#include <iostream>
#include <streambuf>
#include <sstream>
#include <fstream>
#include <algorithm>
#include <cctype>
#include <locale>
#include <functional>
#include <vector>
#include <list>
#include <unordered_map>
#include <unordered_set>
#include <map>
#include <set>

using namespace std;

// ------------------- types ------------------------------

typedef long L;
typedef unsigned long UL;
typedef long long LL;
typedef unsigned long long ULL;

// ------------------- macros -----------------------------

#define VA_NUM_ARGS_IMPL(_1,_2,_3,_4,_5,N,...) N
#define VA_NUM_ARGS_IMPL_(tuple) VA_NUM_ARGS_IMPL tuple
#define VA_NUM_ARGS(...) VA_NUM_ARGS_IMPL_(( __VA_ARGS__, 5,4,3,2,1))
#define CONCAT(x,y) x ## y
#define PROXY(a,b) a b
#define CONCAT_(tuple) PROXY(CONCAT, tuple)
#define DEFINE_OVERLOAD(NAME, ...) CONCAT_((NAME, VA_NUM_ARGS(__VA_ARGS__)))

#define REP_IMPL_3(i,s,n) for (int i=s;i<=n;++i)
#define REP_IMPL_2(i,n) for (int i=1;i<=n;++i)
/*
* (i,n)  -> [1,n]
* (i,s,n)-> [s,n]
*/
#define REP(...) DEFINE_OVERLOAD(REP_IMPL_, __VA_ARGS__) (__VA_ARGS__)

#define RER_IMPL_3(i,n,s) for (int i=n;i>=s;--i)
#define RER_IMPL_2(i,n) for (int i=n;i>=1;--i)
/*
* (i,n)  -> [n,0]
* (i,s,n)-> [s,n]
*/
#define RER(...) DEFINE_OVERLOAD(RER_IMPL_, __VA_ARGS__) (__VA_ARGS__)

// --------------------- read functions ---------------------

void read(int& x1) { scanf("%d", &x1); }
void read(int& x1, int& x2) { scanf("%d %d", &x1, &x2); }
void read(int& x1, int& x2, int& x3) { scanf("%d %d %d", &x1, &x2, &x3); }
void read(int& x1, int& x2, int& x3, int& x4) { scanf("%d %d %d %d", &x1, &x2, &x3, &x4); }
void read(int& x1, int& x2, int& x3, int& x4, int& x5) { scanf("%d %d %d %d %d", &x1, &x2, &x3, &x4, &x5); }

void read(long& x1) { scanf("%ld", &x1); }
void read(long& x1, long& x2) { scanf("%ld %ld", &x1, &x2); }
void read(long& x1, long& x2, long& x3) { scanf("%ld %ld %ld", &x1, &x2, &x3); }
void read(long& x1, long& x2, long& x3, long& x4) { scanf("%ld %ld %ld %ld", &x1, &x2, &x3, &x4); }
void read(long& x1, long& x2, long& x3, long& x4, long& x5) { scanf("%ld %ld %ld %ld %ld", &x1, &x2, &x3, &x4, &x5); }

void read(long long& x1) { scanf("%lld", &x1); }
void read(long long& x1, long long& x2) { scanf("%lld %lld", &x1, &x2); }
void read(long long& x1, long long& x2, long long& x3) { scanf("%lld %lld %lld", &x1, &x2, &x3); }
void read(long long& x1, long long& x2, long long& x3, long long& x4) { scanf("%lld %lld %lld %lld", &x1, &x2, &x3, &x4); }
void read(long long& x1, long long& x2, long long& x3, long long& x4, long long& x5) { scanf("%lld %lld %lld %lld %lld", &x1, &x2, &x3, &x4, &x5); }

void read(unsigned int& x1) { scanf("%u", &x1); }
void read(unsigned int& x1, unsigned int& x2) { scanf("%u %u", &x1, &x2); }
void read(unsigned int& x1, unsigned int& x2, unsigned int& x3) { scanf("%u %u %u", &x1, &x2, &x3); }
void read(unsigned int& x1, unsigned int& x2, unsigned int& x3, unsigned int& x4) { scanf("%u %u %u %u", &x1, &x2, &x3, &x4); }
void read(unsigned int& x1, unsigned int& x2, unsigned int& x3, unsigned int& x4, unsigned int& x5) { scanf("%u %u %u %u %u", &x1, &x2, &x3, &x4, &x5); }

void read(unsigned long& x1) { scanf("%lu", &x1); }
void read(unsigned long& x1, unsigned long& x2) { scanf("%lu %lu", &x1, &x2); }
void read(unsigned long& x1, unsigned long& x2, unsigned long& x3) { scanf("%lu %lu %lu", &x1, &x2, &x3); }
void read(unsigned long& x1, unsigned long& x2, unsigned long& x3, unsigned long& x4) { scanf("%lu %lu %lu %lu", &x1, &x2, &x3, &x4); }
void read(unsigned long& x1, unsigned long& x2, unsigned long& x3, unsigned long& x4, unsigned long& x5) { scanf("%lu %lu %lu %lu %lu", &x1, &x2, &x3, &x4, &x5); }

void read(unsigned long long& x1) { scanf("%llu", &x1); }
void read(unsigned long long& x1, unsigned long long& x2) { scanf("%llu %llu", &x1, &x2); }
void read(unsigned long long& x1, unsigned long long& x2, unsigned long long& x3) { scanf("%llu %llu %llu", &x1, &x2, &x3); }
void read(unsigned long long& x1, unsigned long long& x2, unsigned long long& x3, unsigned long long& x4) { scanf("%llu %llu %llu %llu", &x1, &x2, &x3, &x4); }
void read(unsigned long long& x1, unsigned long long& x2, unsigned long long& x3, unsigned long long& x4, unsigned long long& x5) { scanf("%llu %llu %llu %llu %llu", &x1, &x2, &x3, &x4, &x5); }

template<typename T>
void read(int n, vector<T>& a) {
	REP(i, n) read(a[i]);
}

template<typename T>
void read(int n, vector<T>& a, vector<T>& b) {
	REP(i, n) read(a[i], b[i]);
}

template<typename T>
void read(int n, vector<T>& a, vector<T>& b, vector<T>& c) {
	REP(i, n) read(a[i], b[i], c[i]);
}

// --------------------- write functions ---------------------

void write(int& x1) { printf("%d\n", x1); }
void write(int& x1, int& x2) { printf("%d %d\n", x1, x2); }
void write(int& x1, int& x2, int& x3) { printf("%d %d %d\n", x1, x2, x3); }
void write(int& x1, int& x2, int& x3, int& x4) { printf("%d %d %d %d\n", x1, x2, x3, x4); }
void write(int& x1, int& x2, int& x3, int& x4, int& x5) { printf("%d %d %d %d %d\n", x1, x2, x3, x4, x5); }

void write(long& x1) { printf("%ld\n", x1); }
void write(long& x1, long& x2) { printf("%ld %ld\n", x1, x2); }
void write(long& x1, long& x2, long& x3) { printf("%ld %ld %ld\n", x1, x2, x3); }
void write(long& x1, long& x2, long& x3, long& x4) { printf("%ld %ld %ld %ld\n", x1, x2, x3, x4); }
void write(long& x1, long& x2, long& x3, long& x4, long& x5) { printf("%ld %ld %ld %ld %ld\n", x1, x2, x3, x4, x5); }

void write(long long& x1) { printf("%lld\n", x1); }
void write(long long& x1, long long& x2) { printf("%lld %lld\n", x1, x2); }
void write(long long& x1, long long& x2, long long& x3) { printf("%lld %lld %lld\n", x1, x2, x3); }
void write(long long& x1, long long& x2, long long& x3, long long& x4) { printf("%lld %lld %lld %lld\n", x1, x2, x3, x4); }
void write(long long& x1, long long& x2, long long& x3, long long& x4, long long& x5) { printf("%lld %lld %lld %lld %lld\n", x1, x2, x3, x4, x5); }

void write(unsigned int& x1) { printf("%u\n", x1); }
void write(unsigned int& x1, unsigned int& x2) { printf("%u %u\n", x1, x2); }
void write(unsigned int& x1, unsigned int& x2, unsigned int& x3) { printf("%u %u %u\n", x1, x2, x3); }
void write(unsigned int& x1, unsigned int& x2, unsigned int& x3, unsigned int& x4) { printf("%u %u %u %u\n", x1, x2, x3, x4); }
void write(unsigned int& x1, unsigned int& x2, unsigned int& x3, unsigned int& x4, unsigned int& x5) { printf("%u %u %u %u %u\n", x1, x2, x3, x4, x5); }

void write(unsigned long& x1) { printf("%lu\n", x1); }
void write(unsigned long& x1, unsigned long& x2) { printf("%lu %lu\n", x1, x2); }
void write(unsigned long& x1, unsigned long& x2, unsigned long& x3) { printf("%lu %lu %lu\n", x1, x2, x3); }
void write(unsigned long& x1, unsigned long& x2, unsigned long& x3, unsigned long& x4) { printf("%lu %lu %lu %lu\n", x1, x2, x3, x4); }
void write(unsigned long& x1, unsigned long& x2, unsigned long& x3, unsigned long& x4, unsigned long& x5) { printf("%lu %lu %lu %lu %lu\n", x1, x2, x3, x4, x5); }

void write(unsigned long long& x1) { printf("%llu\n", x1); }
void write(unsigned long long& x1, unsigned long long& x2) { printf("%llu %llu\n", x1, x2); }
void write(unsigned long long& x1, unsigned long long& x2, unsigned long long& x3) { printf("%llu %llu %llu\n", x1, x2, x3); }
void write(unsigned long long& x1, unsigned long long& x2, unsigned long long& x3, unsigned long long& x4) { printf("%llu %llu %llu %llu\n", x1, x2, x3, x4); }
void write(unsigned long long& x1, unsigned long long& x2, unsigned long long& x3, unsigned long long& x4, unsigned long long& x5) { printf("%llu %llu %llu %llu %llu\n", x1, x2, x3, x4, x5); }

template<typename T>
void write(int n, vector<T>& a) {
	REP(i, n) {
		write(a[i]);
		printf(" ");
	}
	printf("\n");
}

void write(const char* s) {
	printf(s);
	printf("\n");
}

void write(std::string s) {
	printf(s.c_str());
	printf("\n");
}

// ----------------------------------------------------------

/*
* Greatest common diviser
*
* Additional knowledge:
* Bezout identity https://en.wikipedia.org/wiki/Bézout%27s_identity
*/
template<typename T>
T gcd(T a, T b) {
	while (b) {
		a %= b;
		swap(a, b);
	}
	return a;
}

/*
*  Knut-Morris-Prat helper function
*/
auto kmp = [](const string& P)->vector<int> {
	const int m = P.length();
	vector<int> p(m + 1, 0);
	p[0] = 0;
	int k = 0;
	for (int q = 1; q < m; q++) {
		while (k > 0 && P[k] != P[q])
			k = p[k - 1];
		if (P[k] == P[q])
			k = k + 1;
		p[q] = k;
	}
	return p;
};

/*
*  Knut-Morris-Prat search
*/
auto find_kmp = [](const string& T, const string& P, vector<int>& kmp) {
	const int n = T.length();
	const int m = P.length();
	int q = 0;
	for (int i = 0; i < n; i++) {
		while (q > 0 && P[q] != T[i])
			q = kmp[q - 1];
		if (P[q] == T[i])
			q++;
		if (q == m)
			return i - m + 1;
	}
	return -1;
};

struct S {
	ULL a, b, c;
	int i;
};

ULL dp[52][52][102];

void task()
{
	memset(&dp, 0, sizeof(dp));

	S s;
	int n, m, k;
	read(n, m, k);
	vector<S> v(m + 1);
	REP(i, m) {
		read(v[i].a, v[i].b, v[i].c);
		v[i].i = i;
	}
	std::sort(v.begin(), v.end(), [](auto& a, auto& b) { return a.c < b.c; });

	if (n == 1) {
		write("YES");
		std::sort(v.begin(), v.end(), [](auto& a, auto& b) { return a.b > b.b; });
		printf("%lld %lld", ULL(v[0].i), v[0].b);
		return;
	}

	REP(i, m) REP(j, 0, v[i].b - v[i].a) dp[i][1][j] = v[i].a + j;

	// dp[i][z][j] - max cost of z out of 1..i subjects, including i-th with cost j
	vector<int> best(3);
	ULL bestCost = 0;
	REP(i, m) {
		REP(z, 2, min(n,i)) {
			REP(j, 0, v[i].b - v[i].a) {
				ULL current = v[i].a + j;
				if (current >= k) {
					ULL previous = current - k;
					REP(h, z - 1, i - 1)
						if (v[h].a <= previous && previous <= v[h].b && dp[h][z - 1][previous - v[h].a] && v[h].c < v[i].c) {
							dp[i][z][j] = max(dp[i][z][j], dp[h][z - 1][previous - v[h].a] + current);
						}
				}
				REP(h, z-1, i - 1) {
					if (current % k != 0) continue;
					ULL previous = current / k;
					if (v[h].a <= previous && previous <= v[h].b && dp[h][z - 1][previous - v[h].a] && v[h].c < v[i].c) {
						dp[i][z][j] = max(dp[i][z][j], dp[h][z - 1][previous - v[h].a] + current);
					}
				}
				if (z == n && dp[i][z][j] > bestCost) {
					bestCost = dp[i][z][j];
					best = { i,z,j };
				}
			}
		}
	}

	if (bestCost == 0) {
		write("NO");
		return;
	}

	write("YES");
	vector<pair<ULL, ULL>> solution(n+1);
	RER(i, n) {
		ULL cost = v[best[0]].a + best[2];
		solution[i] = { v[best[0]].i, cost };
		bestCost -= cost;
		if (i > 1) {
			RER(h, best[0] - 1) {
				bool found = false;
				REP(j, 0, v[h].b - v[h].a) {
					ULL c = v[h].a + j;
					if ((c + k == cost || c * k == cost) && dp[h][i - 1][j] == bestCost) {
						cost = c;
						best = { h, 0, j };
						found = true;
						break;
					}
				}
				if (found) break;
			}
		}
	}

	REP(i, n) {
		write(solution[i].first, solution[i].second);
	}
}

int main() {
    task();
    return 0;
}

#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <cstring>

using namespace std;

#define F first
#define PB(v,x) v.push_back(x)
#define size(X) (int)X.size()
#define S second
#define show(X) cerr << #X << " = " << X << endl;
#define getrandom(min,max) \
	((rand()%(int)(((max)+1)-(min)))+min)
#define foreach(var, container) \
	for(typeof((container).begin()) var = (container).begin(); \
			var != (container).end(); \
			++var)
#define SetBit(value, place) ((value) | (1 << (place)))
#define ClearBit(value, place) ((value) & (~(1 << (place))))
#define ToggleBit(value, place) ((value) ^ (1 << (place)))
#define GetBit(value, place) (((value) >> (place)) & 1)
#define GetPrecentage(Numerator, Denominator)	((Numerator)*100.0/(Denominator))
#define GI ({int t;scanf("%d",&t);t;})
#define GL ({int t;scanf("%I64d",&t);t;})
#define GD ({double t;scanf("%lf",&t);t;})
#define is istringstream
#define os ostringstream
#define lb lower_bound
#define ub upper_bound
#define bs binary_search
#define REP(i,n) for (int i=0,_n=(n); i < _n; i++)
#define REV(i,n) for (int i=(n)-1; i >= 0; i--)
#define FOR(i,a,b) for (int _b=(b), i=(a); i <= _b; i++)
#define ROR(i,a,b) for(int i=(a),_b=(b);i>=_b;i--)
#define ALL(c) (c).begin(), (c).end()
#define SORT(c) sort(ALL(c))
#define CLEAR(x) memset(x,0,sizeof x);
#define REVERSE(c) reverse(ALL(c))
#define UNIQUE(c) SORT(c),(c).resize(unique(ALL(c))-(c).begin())
#define INF (int)1e16
#define eps 1.0e-11
#define PI(x) printf("%d ",x)
template <class X> void printa(X a,int n)
{
	REP(i,n) cerr << a[i] << " " ;
	cerr << endl;
}

template <class X> void printv(X v)
{
	foreach(it,v) cerr << *it << " " ;
	cerr << endl;
}
typedef pair<int,int> pii;
typedef long long ll;
typedef pair<ll,ll> pll;

const int MAXN = 120;
ll n,m,k;
ll d[MAXN][MAXN][MAXN];
ll mark[MAXN][MAXN][MAXN];
pll parent[MAXN][MAXN][MAXN];
struct sub{
	ll c,a,b,i;
	sub(){;}
	friend istream &operator >> (istream &cin , sub&x)
	{
		cin >> x.a >> x.b >> x.c;
		return cin;
	}
	bool operator < (const sub &x) const { return c < x.c; }
}subj[MAXN];

void input()
{
	cin >> n >> m >> k;
	REP(i,m)
	{
		cin >> subj[i];
		subj[i].i = i;
	}
	sort(subj,subj+m);
}

ll memz(ll i,ll j,ll t)
{

	if(mark[i][j][t])
		return d[i][j][t];
	ll q = subj[i].a + t;
	mark[i][j][t] = 1;
	if(q > subj[i].b)
	{
		d[i][j][t] = -1*INF;
		return -1*INF;
	}
	if(j==0)
	{
		d[i][j][t] = q;
		return q;
	}
	if(i<j)
	{
		d[i][j][t] = -1*INF;
		return d[i][j][t];
	}
	ll p1 = q;
	ll p2 = q-k;
	bool tt = true;
	REV(z,i) if(subj[z].c < subj[i].c && z>=j-1)
	{
		if((p1/k)>=subj[z].a && (p1/k)<=subj[z].b && !(p1%k) && d[i][j][t] < memz(z,j-1,(p1/k)-subj[z].a)+q)
		{
			d[i][j][t] = memz(z,j-1,(p1/k)-subj[z].a)+q;
			parent[i][j][t].F = z;
			parent[i][j][t].S = p1/k-subj[z].a;
			tt = false;
		}
		if(p2>=subj[z].a && p2<=subj[z].b && d[i][j][t] < memz(z,j-1,p2-subj[z].a) + q)
		{
			d[i][j][t] = memz(z,j-1,p2-subj[z].a) + q;
			parent[i][j][t].F = z;
			parent[i][j][t].S = p2-subj[z].a;
			tt = false;
		}
	}
	if(tt)
	{
		d[i][j][t] = -1*INF;
		return d[i][j][t];
	}
	return d[i][j][t];
}

int main()
{
	input();
	ll MAX = 0;
	ll s = -1;
	ll s2,s3;
	memset(parent,-1,sizeof parent);
	REP(i,m) REP(j,101) if(memz(i,n-1,j) > MAX)
	{
		MAX = memz(i,n-1,j);
		s = i;
		s2 = n-1;
		s3 = j;
	}
	if(MAX == 0)
	{
		cout << "NO" << endl;
		return 0;
	}
	cout << "YES" << endl;
	vector<pll> ans;
	while(s!=-1)
	{
		PB(ans,pll(subj[s].i + 1,s3 + subj[s].a));
		ll p = s;
		s = parent[s][s2][s3].F;
		s3 = parent[p][s2][s3].S;
		s2--;
	}
	reverse(ans.begin(),ans.end());
	REP(i,size(ans))
		cout << ans[i].F << " " << ans[i].S << endl;
	return 0;
}