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.

{$A8,B-,C+,D+,E-,F-,G+,H+,I+,J-,K-,L+,M-,N+,O-,P+,Q-,R-,S-,T-,U-,V+,W-,X+,Y+,Z1}

{$APPTYPE CONSOLE}

uses
  SysUtils,
  Math;

const
   max_n = 50;
   max_m = 50;

var
  a, b : array[0..max_m + 1] of int64;
  c, num : array[0..max_m + 1] of integer;
  an : array[0..max_n + 1, 1..2] of int64;
  d : array[0..max_m + 1, 0..100, 0..max_n + 1] of int64;
  ei, es : array[0..max_m + 1, 0..100, 0..max_n + 1] of integer;
  i, j, t, s, n, m, k, ci, cs, ansi, anss : integer;
  x, ans : int64;

procedure swap(var a, b : integer);
var
  w : integer;
begin
  w := a;
  a := b;
  b := w;
end;

procedure swap64(var a, b : int64);
var
  w : int64;
begin
  w := a;
  a := b;
  b := w;
end;

procedure sort;
var
  i, j : integer;
begin
  for i := 1 to m - 1 do
    for j := 1 to m - i do
      if (c[j] > c[j + 1]) then begin
        swap(c[j], c[j + 1]);
        swap(num[j], num[j + 1]);
        swap64(a[j], a[j + 1]);
        swap64(b[j], b[j + 1]);
      end;
end;

begin
 // reset(input, 'input.txt');
 // rewrite(output, 'output.txt');

  read(n, m, k);
  fillchar(d, sizeof(d), 0);
  fillchar(ei, sizeof(ei), 0);
  fillchar(es, sizeof(es), 0);
  for i := 1 to m do begin
    read(a[i], b[i], c[i]);
    num[i] := i;
  end;
  sort;

  for i := 1 to m do
    for j := 0 to b[i] - a[i] do
      d[i, j, 1] := a[i] + j;

  for t := 2 to n do begin
    for i := 1 to m do
      for j := 1 to i - 1 do begin
        if c[i] <= c[j] then
          continue;
        for s := 0 to b[j] - a[j] do begin
          if d[j, s, t - 1] = 0 then
            continue;
          if (t = 2) and (i = 3) and (j = 2) then
            x := 0;
          x := (a[j] + s) * k;
          if (x >= a[i]) and (x <= b[i]) then begin
            if d[j, s, t - 1] + x > d[i, x - a[i], t] then begin
              d[i, x - a[i], t] := d[j, s, t - 1] + x;
              ei[i, x - a[i], t] := j;
              es[i, x - a[i], t] := s;
            end;
          end;
          x := (a[j] + s) + k;
          if (x >= a[i]) and (x <= b[i]) then begin
            if d[j, s, t - 1] + x > d[i, x - a[i], t] then begin
              d[i, x - a[i], t] := d[j, s, t - 1] + x;
              ei[i, x - a[i], t] := j;
              es[i, x - a[i], t] := s;
            end;
          end;
        end;
      end;
  end;

  ans := 0;
  for i := 1 to m do
    for s := 0 to b[i] - a[i] do
      if d[i, s, n] > ans then begin
        ans := d[i, s, n];
        ansi := i;
        anss := s;
      end;

  if ans = 0 then begin
    writeln('NO');
    halt(0);
  end;

  writeln('YES');
  for i := n downto 1 do begin
    an[i, 1] := num[ansi];
    an[i, 2] := a[ansi] + anss;
    ci := ei[ansi, anss, i];
    cs := es[ansi, anss, i];
    ansi := ci;
    anss := cs;
  end;

  for i := 1 to n do
    writeln(an[i, 1], ' ', an[i, 2]);

  //close(input);
  //close(output);
end.

{$APPTYPE CONSOLE}

var
  idx1,idx,idy,z,l,x,i,j,n,m:integer;
  ans,s1,s2,s,k:int64;
  an1,a,b:array[1..101] of int64;
  c1,nn1,nn,c:array[1..101] of integer;
  cur,an:array[1..51,1..51,0..101] of int64;
  prevl,prevs:array[1..51,1..51,0..101] of integer;

  Procedure swap(var a,b:int64);
  var
   x:int64;
  begin
   x:=a;
   a:=b;
   b:=x;
  end;

begin
 {reset(input,'Input.txt');
 rewrite(output,'Output.txt');}

 readln(n,m,k);

 For i:=1 to m do readln(a[i],b[i],c[i]);
 For i:=1 to m do nn[i]:=i;

  For i:=2 to m  do
    For j:=m downto i do If c[j-1]>c[j] then
    begin
     swap(a[j-1],a[j]);
     swap(b[j-1],b[j]);
     x:=c[j-1];
     c[j-1]:=c[j];
     c[j]:=x;
     x:=nn[j-1];
     nn[j-1]:=nn[j];
     nn[j]:=x;
    end;

 Fillchar(prevs,sizeof(prevs),0);
 Fillchar(prevl,sizeof(prevl),0);
 Fillchar(cur,sizeof(cur),0);

 For i:=1 to n do
  For j:=1 to m do
   For z:=0 to 101 do an[i,j,z]:=-100000000000000000;

 For i:=1 to m do
  For j:=0 to b[i]-a[i] do
  begin
   prevs[1,i,j]:=-1;
   prevl[1,i,j]:=-1;
   an[1,i,j]:=a[i]+j;
   cur[1,i,j]:=a[i]+j;
  end;

  //For i:=1 to m do writeln(a[i],' ',b[i],' ',c[i],' ',nn[i]);

 For i:=2 to n do
  For j:=1 to m do
   For z:=0 to b[j]-a[j] do
   begin
     s:=a[j]+z;
     If s mod k=0 then s1:=s div k else s1:=-1;
     s2:=s-k;
     For l:=1 to j-1 do
     begin
      If (c[j]>c[l]) and ((s1>=a[l]) and (s1<=b[l])) and (an[i-1,l,s1-a[l]]<>-100000000000000000)
      and (an[i,j,z]<an[i-1,l,s1-a[l]]+s) then
      begin
       an[i,j,z]:=an[i-1,l,s1-a[l]]+s;
       prevl[i,j,z]:=l;
       prevs[i,j,z]:=s1-a[l];
       cur[i,j,z]:=s;
      end;

      If (c[j]>c[l]) and ((s2>=a[l]) and (s2<=b[l])) and (an[i-1,l,s2-a[l]]<>-100000000000000000)
      and (an[i,j,z]<an[i-1,l,s2-a[l]]+s) then
      begin
       an[i,j,z]:=an[i-1,l,s2-a[l]]+s;
       prevl[i,j,z]:=l;
       prevs[i,j,z]:=s2-a[l];
       cur[i,j,z]:=s;
      end;
     end;
   end;

  ans:=-100000000000000000;
  idx:=0;
  idy:=0;

  For i:=1 to m do
   For j:=0 to b[i]-a[i] do
   If an[n,i,j]>ans then
   begin
    ans:=an[n,i,j];
    idx:=i;
    idy:=j;
   end;

  If ans=-100000000000000000 then writeln('NO') else
  begin
   writeln('YES');
   For i:=n downto 1 do
   begin
    nn1[i]:=nn[idx];
    c1[i]:=c[idx];
    an1[i]:=cur[i,idx,idy];
    idx1:=prevl[i,idx,idy];
    idy:=prevs[i,idx,idy];
    idx:=idx1;
   end;
   For i:=2 to n do
    For j:=n downto i do If  c1[j-1]>c1[j] then
    begin
     swap(an1[j-1],an1[j]);
     x:=nn1[j-1];
     nn1[j-1]:=nn1[j];
     nn1[j]:=x;
     x:=c1[j-1];
     c1[j-1]:=c1[j];
     c1[j]:=x;
    end;
    For i:=1 to n do writeln(nn1[i],' ',an1[i]);
  end;

end.

program task;
//{$APPTYPE DELPHI}
//{$APPTYPE CONSOLE}
{$R-,S-,Q-,I-,O+}

uses
  Math,
  SysUtils;

type kat=record
    a,b:int64;
    c,id:longint;
end;

var n,m,k,i,jj,l,i1,i2:longint;
    j,kkl,lk,rip:int64;
    kk:array[1..100]of kat;
    least:array[1..100]of longint;
    res:array[1..100,1..50,0..100]of int64;

function less(a,b:kat):boolean;
begin
    less:=(a.c<b.c)or((a.c=b.c)and(a.b<b.b));
end;

procedure bubble;
var i,j:longint;
    cc:kat;
begin
    for i:=2to m do
        for j:=m downto i do
            if less(kk[j],kk[j-1])then begin
                cc:=kk[j];
                kk[j]:=kk[j-1];
                kk[j-1]:=cc;
            end;
end;

procedure dls;
var i, j:longint;
begin
    for i:= 1 to m do begin
        j:=0;
        while (kk[j+1].c<kk[i].c) do inc(j);
        least[i]:=j;
    end;
end;

procedure init;
var i,j,k:longint;
begin
    for j:= 1 to 50 do
        for i:=1 to 100 do
            for k := 0 to 100 do
                res[i,j,k]:=0;
end;

procedure obr(x, y : longint;z:int64);
var j, kkl,lk : int64;
    ok:boolean;
begin
    ok:=false;
    if (x = 1) then
        writeln(kk[y].id,' ',z+kk[y].a)
    else begin
        j := z;
        kkl := kk[y].a+j;
        if kkl mod int64(k) = 0 then begin
            lk := kkl div int64(k);
            l:=1;
            while (not ok) and (l<=least[y]) do begin
                if (lk>=kk[l].a)and(lk<=kk[l].b)and(res[l,x-1,lk-kk[l].a]<>0)and
                (res[y,x,z]=res[l,x-1,lk-kk[l].a]+kkl)then begin
                    ok:=true;
                    obr(x-1,l,lk-kk[l].a);
                    writeln(kk[y].id,' ',z+kk[y].a);
                end;
                l:=l+1;
            end;
        end;
            lk:=kkl-int64(k);
            l:=1;
            while (not ok) and (l<=least[y]) do begin
                if (lk>=kk[l].a)and(lk<=kk[l].b)and(res[l,x-1,lk-kk[l].a]<>0)and
                (res[y,x,z]=res[l,x-1,lk-kk[l].a]+kkl)then begin
                    ok:=true;
                    obr(x-1,l,lk-kk[l].a);
                    writeln(kk[y].id,' ',z+kk[y].a);
                end;
                l:=l+1;
            end;
        end;
end;

begin
    //assign(input,'input.txt');
    //assign(output, 'output.txt');
    init;
    readln(n,m,k);
    for i := 1 to m do
        kk[i].id := i;
    for i:=1to m do
        readln(kk[i].a,kk[i].b,kk[i].c);
    bubble;
    for i:=1 to m do begin
        kkl:=kk[i].a;
        j:=0;
        //jj:=1;
        while kkl<=kk[i].b do begin
            res[i,1,j]:=kkl;
            //writeln('res :', jj, ':',i, ':', j, ' is ', res[i,jj,j]);
            j:=j+1;
            kkl:=kkl+1;
        end;
    end;
    dls;
    for jj:=2 to n do begin
        for i:=1to m do begin
            kkl:=kk[i].a;
            j:=0;
            while (kkl<=kk[i].b)do begin
                kkl:=j+kk[i].a;
                if kkl mod int64(k) = 0 then begin
                    lk:=kkl div int64(k);
                    for l:=1 to least[i] do
                    if (lk>=kk[l].a)and(lk<=kk[l].b)and(res[l,jj-1,lk-kk[l].a]<>0)then
                        res[i,jj,j]:=max(res[i,jj,j], res[l,jj-1,lk-kk[l].a]+kkl);
                end;
                lk:=kkl-int64(k);
                for l:=1 to least[i] do
                if (lk >= kk[l].a)and(lk<=kk[l].b)and(res[l,jj-1,lk-kk[l].a]<>0)then
                    res[i,jj,j]:=max(res[i,jj,j], res[l,jj-1,lk-kk[l].a]+kkl);
                kkl:=kkl+1;
                //writeln('res :', jj, ':',i, ':', j, ' is ', res[jj,i,j]);
                j:=j+1;
            end;
        end;
    end;
    rip:=0;
    i1:=0;
    i2:=0;
    for i:=1to m do begin
        kkl:=kk[i].a;
        j:=0;
        while(kkl<=kk[i].b)do begin
            rip:=max(rip,res[i,n,j]);
            if rip=res[i,n,j] then begin
                i1:=i;
                i2:=j;
            end;
            j:=j+1;
            kkl:=kkl+1;
        end
    end;
    if rip = 0 then writeln('NO') else begin
        writeln('YES');
        //writeln(rip,' ',i1, ' ',i2);
        obr(n, i1, i2);
    end;
    halt(0)
end.

{R+,S+,Q+,I+,O-}
{$R-,S-,Q-,I-,O+}
const inf = round(1e18);
var
  ki,kk,n,m,u,i,j,k,ni,z,q: longint;
  max,ft,tmp,x,y: int64;
  cnt,a,b: array [0..101] of int64;
  ans,c,d: array [0..101] of longint;
  ok: array [0..51,0..101,0..51,0..101] of boolean;
  f: array [0..51,0..51,0..101] of int64;
  fz,fq: array [0..51,0..51,0..101] of longint;
begin
//  assign(input,'in'); reset(input);
//  assign(output,'out'); rewrite(output);
  read(n,m,u);
  for i:=1 to m do read(a[i],b[i],c[i]);
  for i:=1 to m do d[i]:=i;
  for i:=1 to m-1 do
    for j:=i+1 to m do
      if c[i] > c[j] then
      begin
        tmp:=a[i]; a[i]:=a[j]; a[j]:=tmp;
        tmp:=b[i]; b[i]:=b[j]; b[j]:=tmp;
        tmp:=c[i]; c[i]:=c[j]; c[j]:=tmp;
        tmp:=d[i]; d[i]:=d[j]; d[j]:=tmp;
      end;
  for i:=1 to m do
    for j:=1 to n do
      for k:=0 to b[i]-a[i] do f[i,j,k]:=-inf;
  for i:=1 to m do
    for k:=0 to b[i]-a[i] do f[i,1,k]:=a[i]+k;
  fillchar(ok,sizeof(ok),False);
  for i:=1 to m do
    for k:=0 to b[i]-a[i] do
      for j:=i+1 to m do
        for q:=0 to b[j]-a[j] do
        begin
          x:=a[i]+k;
          y:=a[j]+q;
          if (y = x+u) or (y = x*u) then ok[j,q,i,k]:=True;
        end;
  for i:=2 to m do
    for j:=2 to n do
      if j > i then continue else
      for k:=0 to b[i]-a[i] do
        for z:=1 to i-1 do
          if c[i] > c[z] then
            for q:=0 to b[z]-a[z] do
              if ok[i,k,z,q] and (f[z,j-1,q] >= 0) then
              begin
                ft:=f[z,j-1,q]+a[i]+k;
                if ft > f[i,j,k] then
                begin
                  f[i,j,k]:=ft;
                  fz[i,j,k]:=z;
                  fq[i,j,k]:=q;
                end;
              end;
  max:=-1;
  ki:=0; kk:=0;
  for i:=1 to m do
    for k:=0 to b[i]-a[i] do
      if f[i,n,k] > max then
      begin
        max:=f[i,n,k];
        ki:=i;
        kk:=k;
      end;
  if max < 0 then writeln('NO') else
  begin
    writeln('YES');
    i:=ki; k:=kk;
    for j:=n downto 1 do
    begin
      ans[j]:=d[i];
      cnt[j]:=a[i]+k;
      if j = 1 then break;
      ni:=fz[i,j,k];
      k:=fq[i,j,k];
      i:=ni;
    end;
    for j:=1 to n do writeln(ans[j],' ',cnt[j]);
  end;
  close(input);
  close(output);
end.

program Project1;

{$APPTYPE CONSOLE}
{$O-} {$R+} {$Q+}

uses
  SysUtils,
  Math;

const
  CLen = 101;

var
  n, m, k: Int64;
  a, b, c: array [1..clen] of Int64;
  mx, d, p: array [1..clen, 1..clen, 0..clen] of Int64;

procedure write_res(x, y, z: Int64);
begin
  if d[x, y, z] = 1 then
    write_res(x-1, p[x, y, z], z + a[y] - k - a[p[x, y, z]])
  else if d[x, y, z] = 2 then
    write_res(x-1, p[x, y, z], ((z + a[y]) div k) - a[p[x, y, z]]);
  Writeln(y, ' ', a[y] + z);
end;

var
  i, j, x, y: Integer;
  val, mxx: Int64;

begin
  //Reset(input, 'input.txt'); rewrite(output, 'output.txt');
  read(n, m, k);
  for i := 1 to m do
    read(a[i], b[i], c[i]);
  for i := 1 to clen do
    for j := 1 to clen do
      for x := 0 to clen do
        begin
          d[i, j, x] := -1;
          p[i, j, x] := -1;
          mx[i, j, x] := -1;
        end;
  for i := 1 to m do
    for j := 0 to b[i] - a[i] do
      begin
        d[1, i, j] := 0;
        p[1, i, j] := 0;
        mx[1, i, j] := j + a[i];
      end;
  for i := 2 to n do
    for j := 1 to m do
      for x := 0 to clen do
        for y := 1 to m do
          if (d[i-1, j, x] <> -1) and (c[y] > c[j]) then
            begin
              val := x + a[j] + k - a[y];
              if (a[y] <= x + a[j] + k) and (x + a[j] + k <= b[y]) and (mx[i-1, j, x] + x + a[j] + k > mx[i, y, val]) then
                begin
                  d[i, y, val] := 1;
                  p[i, y, val] := j;
                  mx[i, y, val] := mx[i-1, j, x] + x + a[j] + k;
                end;
              val := (x + a[j]) * k - a[y];
              if (a[y] <= (x + a[j]) * k) and ((x + a[j]) * k <= b[y]) and (mx[i-1, j, x] + (x + a[j]) * k > mx[i, y, val]) then
                begin
                  d[i, y, val] := 2;
                  p[i, y, val] := j;
                  mx[i, y, val] := mx[i-1, j, x] + (x + a[j]) * k;
                end;
            end;
  mxx := -1;
  for j := 1 to m do
    for x := 0 to clen do
      mxx := max(mxx, mx[n, j, x]);
  for j := 1 to m do
    for x := 0 to clen do
      if (mxx <> -1) and (mx[n, j, x] = mxx) then
        begin
          Writeln('YES');
          write_res(n, j, x);
          halt;
        end;
  Writeln('NO');
end.

{$r+,i+,q+,s+,o-}
const maxn = 5100;
const maxm = 51;

var
  f:array[0..maxm, 0..maxm, 0..maxn] of int64;
  pi,pj,pk:array[0..maxm, 0..maxm, 0..maxn] of word;
  ax,ay,a,b,c,w:array[0..maxn] of int64;
  edn,beg:array[0..maxn] of longint;
  qk,nrj,nrk,ri,rj,rk,q,k,wn,n,m,i,j:longint;
  jj,max,me,kk:int64;

procedure qs(const l, r:longint);
var
  i,j:longint;
  x,y:int64;
begin
  i:=l; j:=r; x:=w[(l+r) shr 1];
  repeat
    while x>w[i] do inc(i);
    while x<w[j] do dec(j);
    if j<i then break;
    y:=w[i]; w[i]:=w[j]; w[j]:=y;
    inc(i); dec(j);
  until j<i;
  if l<j then qs(l, j);
  if i<r then qs(i, r);
end;

procedure upd(const ni, nj:longint; qk, nk, up:int64);
begin
  while w[qk] <> nk do ;
  if f[ni, nj, qk]<up then
  begin
    f[ni, nj, qk]:=up;
    pi[ni, nj, qk]:=i;
    pj[ni, nj, qk]:=j;
    pk[ni, nj, qk]:=k;
  end;
end;

begin
  wn:=0;
  read(n, m, kk);
  for i:=1 to m do read(a[i], b[i], c[i]);
  for i:=1 to m do
  begin
    beg[i]:=wn+1;
    jj:=a[i];
    while jj<=b[i] do
    begin
      inc(wn);
      w[wn]:=jj;
      inc(jj);
    end;
    edn[i]:=wn;
  end;
  for i:=1 to m do
  begin
    jj:=a[i];
    while jj<=b[i] do
    begin
      f[1, i, beg[i]+jj-a[i]]:=jj;
      inc(jj);
    end;
  end;
  for i:=1 to n-1 do for j:=1 to m do for k:=beg[j] to edn[j] do if f[i, j, k]>0 then
  begin
    me:=w[k];
    for q:=1 to m do if c[q]>c[j] then
    begin
      if (a[q]<=me+kk)and(me+kk<=b[q]) then upd(i+1, q, int64(beg[q])+me+kk-a[q], me+kk, f[i, j, k]+me+kk);
      if (a[q]<=me*kk)and(me*kk<=b[q]) then upd(i+1, q, int64(beg[q])+me*kk-a[q], me*kk, f[i, j, k]+me*kk);
    end;
  end;
  rk:=-1;
  max:=-1;
  for j:=1 to m do for k:=1 to wn do if (f[n, j, k]>0)and(f[n, j, k] > max) then
  begin
    max:=f[n, j, k];
    ri:=n;
    rj:=j;
    rk:=k;
  end;
  if rk = -1 then
  begin
    writeln('NO');
    close(output);
    halt;
  end;
  writeln('YES');
  for i:=n downto 1 do
  begin
    ax[i]:=rj;
    ay[i]:=w[rk];
    nrj:=pj[i, rj, rk];
    nrk:=pk[i, rj, rk];
    rj:=nrj; rk:=nrk;
  end;
  for i:=1 to n do writeln(ax[i], ' ', ay[i]);
  close(output);
end.

uses math;

const maxn=110;
  fi='';//'v.inp';
  fo='';//'v.out';
    no='NO';
    yes='YES';

var n,m,k,i1,j1,t1,dem,i,j,t,jj,tt1,tt2:longint;
    a,b,c:Array[0..maxn]of int64;
    ca1,ca2,res,cal:int64;
    re1,re2:Array[0..maxn]of longint;
    cl:Array[0..maxn]of longint;
    ii,jj1,tt:array[0..maxn div 2,0..maxn div 2,0..maxn]of longint;
    f:Array[0..maxn div 2,0..maxn div 2,0..maxn]of int64;
    f1,f2:text;

BEGIN
  assign(f1,fi);reset(f1);
  assign(f2,fo);rewrite(f2);
   readln(f1,n,m,k);
   for i:=1 to m do
     begin
       readln(f1,a[i],b[i],c[i]);
       cl[i]:=b[i]-a[i];
     end;

   res:=0;
   for i:=1 to m do
      for t:=0 to cl[i] do  begin
      f[1][i][t] := a[i]+t;
      if n=1 then
         begin
          if res<f[1][i][t] then
            begin
             res:=f[1][i][t];
             i1:=1;j1:=i;t1:=t;
            end;
         end;
      end;

  for i:=2 to n do
     for j:=1 to m do
       for jj:=1 to m do
         if c[j]>c[jj] then
           begin
             for t:=0 to cl[j] do
                begin
               cal:=a[j] + t;
                   ca1:=cal - k;
                   if (ca1>=a[jj]) and (ca1<=b[jj]) then begin
                   if f[i-1][jj][ca1-a[jj]] <>0 then
                      if f[i][j][t]<f[i-1][jj][ca1-a[jj]] + a[j] + t then
                          begin
                           f[i][j][t]:=f[i-1][jj][ca1-a[jj]] + a[j] + t;
                           ii[i][j][t]:=i-1;
                           jj1[i][j][t]:=jj;
                           tt[i][j][t]:=ca1-a[jj];
                          end;
                   end;
               if cal mod k=0 then begin ca2:=cal div k;
                 if (ca2>=a[jj]) and (ca2<=b[jj]) then
                 if f[i-1][jj][ca2-a[jj]] <>0 then
                       if f[i][j][t]<f[i-1][jj][ca2-a[jj]] + a[j] + t then
                          begin
                           f[i][j][t]:=f[i-1][jj][ca2-a[jj]] + a[j] + t;
                           ii[i][j][t]:=i-1;
                           jj1[i][j][t]:=jj;
                           tt[i][j][t]:=ca2-a[jj];
                          end;
                     end;

                if i=n then
                begin
                  if res<f[i][j][t] then
                    begin
                     res:=f[i][j][t];
                     i1:=i;j1:=j;t1:=t;
                    end;
                end;
                end;
           end;

        if res=0 then writeln(f2,'NO') else
         begin
          writeln(f2,yes);dem:=0;
          while i1<>1 do
            begin
             inc(dem);
             re1[dem]:=j1;
             re2[dem]:=t1;
             tt1:=jj1[i1][j1][t1];
             tt2:=tt[i1][j1][t1];
             j1:=tt1;t1:=tt2;
              dec(i1);
            end;
       inc(dem);
       re1[dem]:=j1;re2[dem]:=t1;
       for i:=dem downto 1 do
       writeln(f2,re1[i],' ',re2[i] + a[re1[i]]);
       end;
       close(f1);close(f2);
END.

Program C_REFORM;
Const MaxN=50;
      MaxV=100;
Type subject=Record a,b:qword; stt,c,v:Longint End;
Var l:array[1..MaxN,1..MaxN,0..MaxV] of qword;
    trace:array[1..MaxN,1..MaxN,0..MaxV] of Record a,b:Longint End;
    a:array[1..MaxN] of subject;
    n,m,k:Longint;
Procedure Input;
Var f:Text;
    i:Longint;
Begin
  Assign(f,''); Reset(f);
  Readln(f,n,m,k);
  For i:=1 to m do
    Begin
      Readln(f,a[i].a,a[i].b,a[i].c);
      a[i].v:=a[i].b-a[i].a;
      a[i].stt:=i;
    End;
  Close(f);
End;

Procedure Swap(var a,b:subject);
Var t:subject; Begin t:=a; a:=b; b:=t End;

Procedure QuickSort(l,r:Longint);
Var i,j,k:Longint;
Begin
  If l>=r then exit;
  i:=l; j:=r;
  k:=a[random(r-l+1)+l].c;
  Repeat
    While a[i].c<k do Inc(i);
    While a[j].c>k do Dec(j);
    If i<=j then
      Begin
        If i<j then Swap(a[i],a[j]);
        Inc(i); Dec(j);
      End;
  Until i>j;
  QuickSOrt(l,j); QUickSort(i,r)
End;

Procedure Optimize;
Var i,j,z,x,h:Longint;
    t:qword;
Begin
  For j:=1 to m-n+1 do
    For z:=0 to a[j].v do
      l[1,j,z]:=z+a[j].a;

  For i:=1 to n-1 do
    For j:=i to m-(n-i) do
      For z:=0 to a[j].v do
        If l[i,j,z]<>0 then
          For h:=1 to 2 do
            Begin
            If h=1 then t:=(z+a[j].a)*k else t:=(z+a[j].a)+k;
            For x:=j+1 to m-(n-i)+1 do
              If (a[x].c>a[j].c) and (a[x].a<=t) and (t<=a[x].b)
                and (l[i+1,x,t-a[x].a]<l[i,j,z]+t) then
                  Begin
                    l[i+1,x,t-a[x].a]:=l[i,j,z]+t;
                    With trace[i+1,x,t-a[x].a] do
                      Begin
                        a:=j; b:=z
                      End;
                  End;
            End;
End;

Procedure Output;
Var ds:array[1..MaxN] of Record stt:Longint; v:QWord End;
    i,j,z,x,y:Longint;
    max:qword;
Begin
  max:=0;
  For j:=n to m do
    For z:=0 to a[j].v do
      If max<l[n,j,z] then
        Begin
          max:=l[n,j,z];
          x:=j; y:=z;
        End;
  If max=0 then
    Begin
      Writeln('NO');
      Exit;
    End;
  i:=n;
  While i<>0 do
    Begin
      ds[i].stt:=a[x].stt; ds[i].v:=a[x].a+y;
      j:=trace[i,x,y].a; z:=trace[i,x,y].b;
      x:=j; y:=z;
      Dec(i);
    End;
  Writeln('YES');
  For i:=1 to n do Writeln(ds[i].stt,' ',ds[i].v);
End;

BEGIN
  Input;
  QuickSort(1,m);
  Optimize;
  Output;
END.

var njj,jj,n,m,k,i,j,l,ll:longint;
nlj,ans,lj,ra,pra,tt:int64;
a1,a2,a,b,c:array[0..100]of int64;
p1,p2,q:array[0..51,0..51,0..101]of int64;
 begin
  read(n,m,k);
  for i:=1 to m do
    read(a[i],b[i],c[i]);

  for i:=1 to n do
    for j:=1 to m do
      for ll:=0 to 101 do
        q[i,j,ll]:=-1000000000000000000;
  for j:=1 to m do
   begin
    tt:=b[j]-a[j];
    for l:=0 to tt do
      q[1,j,l]:=l+a[j];
   end;

  for i:=2 to n do
    begin
     for j:=1 to m do
       begin
        for l:=1 to m do
         if c[l]<c[j] then
           begin
            tt:=b[j]-a[j];

            for ll:=0 to tt do
              begin
               ra:=a[j]+ll;
               if ra>=k then
                 begin
                  pra:=ra-k;
                  if (a[l]<=pra)and(pra<=b[l]) then
                    if q[i-1,l,pra-a[l]]+ra>q[i,j,ll] then
                      begin
                       q[i,j,ll]:=q[i-1,l,pra-a[l]]+ra;
                       p1[i,j,ll]:=l;
                       p2[i,j,ll]:=pra-a[l];
                      end;
                 end;
               if ra mod k=0 then
                 begin
                  pra:=ra div k;
                  if (a[l]<=pra)and(pra<=b[l]) then
                    if q[i-1,l,pra-a[l]]+ra>q[i,j,ll] then
                      begin
                       q[i,j,ll]:=q[i-1,l,pra-a[l]]+ra;
                       p1[i,j,ll]:=l;
                       p2[i,j,ll]:=pra-a[l];
                      end;
                 end;
               if q[i,j,ll]<0 then q[i,j,ll]:=-1000000000000000000;
              end;
           end;
       end;
    end;
  ans:=-1000000000000000000;
  for j:=1 to m do
    begin
     tt:=b[j]-a[j];
     for ll:=0 to tt do
       if q[n,j,ll]>ans then
         begin
          ans:=q[n,j,ll];
          jj:=j;
          lj:=ll;
         end;
    end;

  if ans<0 then writeln('NO')
  else
   begin
    writeln('YES');
    for i:=n downto 1 do
      begin
       a1[i]:=jj;
       a2[i]:=lj+a[jj];
       if i=1 then break;
       njj:=p1[i,jj,lj];
       nlj:=p2[i,jj,lj];
       jj:=njj;
       lj:=nlj;
      end;
    for i:=1 to n do writeln(a1[i],' ',a2[i]);

   end;

 end.

program probcc;

var f:array[0..50,0..50,0..1000]of int64;
    pre,pp:array[0..50,0..50,0..1000]of longint;
    a,b,c,num,anss,anst:array[0..500]of int64;
    n,m,k,ml,mj,i,j,p,l:longint;maxx,min,t:int64;

procedure init;
begin
  //assign(input,'cc.in');reset(input);
  readln(n,m,k);min:=maxlongint;
  for i:=1 to m do begin
    readln(a[i],b[i],c[i]);
    if c[i]<min then min:=c[i];num[i]:=i;
  end;
  for i:=1 to m-1 do
    for j:=i+1 to m do
      if c[i]>c[j] then begin
        t:=c[i];c[i]:=c[j];c[j]:=t;
        t:=a[i];a[i]:=a[j];a[j]:=t;
        t:=b[i];b[i]:=b[j];b[j]:=t;
        t:=num[i];num[i]:=num[j];num[j]:=t;
      end;
end;

function max(a,b:int64):int64;
begin if a>b then exit(a) else exit(b);end;

procedure dp;
begin
  for i:=1 to n do
    for j:=1 to m do
      for l:=0 to b[j]-a[j] do begin
        f[i,j,l]:=0;
        for p:=1 to j-1 do if c[p]<c[j] then begin
          if i=1 then continue;
          if ((a[j]+l) mod k=0)and((a[j]+l)div k>=a[p])and((a[j]+l)div k<=b[p])
          then
            if f[i,j,l]<f[i-1,p,(a[j]+l) div k-a[p]] then begin
              f[i,j,l]:=f[i-1,p,(a[j]+l) div k-a[p]];
              pre[i,j,l]:=p;pp[i,j,l]:=0;
            end;
          if (a[j]+l-k>=a[p])and(a[j]+l-k<=b[p]) then
            if f[i,j,l]<f[i-1,p,(a[j]+l-k-a[p])] then begin
              f[i,j,l]:=f[i-1,p,(a[j]+l-k-a[p])];
              pre[i,j,l]:=p;pp[i,j,l]:=1;
            end;
        end;
        if (f[i,j,l]>0)or(i=1) then inc(f[i,j,l],a[j]+l) else f[i,j,l]:=-maxlongint;
      end;
end;

begin
  init;
  dp;
  maxx:=-maxlongint;
  for j:=1 to m do
    for l:=0 to b[j]-a[j] do maxx:=max(f[n,j,l],maxx);
  for j:=1 to m do for l:=0 to b[j]-a[j] do if maxx=f[n,j,l] then begin
    mj:=j;ml:=l;
  end;
  if maxx<=0 then writeln('NO') else begin
    writeln('YES');
    anss[n]:=mj;anst[n]:=a[mj]+ml;
    for i:=n-1 downto 1 do begin
      anss[i]:=pre[i+1,mj,ml];
      if pp[i+1,mj,ml]=1 then anst[i]:=a[mj]+ml-k else anst[i]:=(a[mj]+ml) div k;
      mj:=anss[i];ml:=anst[i]-a[mj];
    end;
    for i:=1 to n do writeln(num[anss[i]],' ',anst[i]);
  end;
end.

Program Education_Reform;
var i,j,z,x,n,m,nx,ny:longint;
    tmp,k:int64;
    s,t,a,id,ans,ans2:array[1..100]of int64;
    pre,pre2:array[1..50,0..50,0..100]of longint;
    f:array[1..50,0..50,0..100]of int64;
//===============================================================
procedure qsort(left,right:longint);
var l,r,mid,k:longint;
begin
  l:=left;r:=right;
  mid:=a[(l+r)shr 1];
  repeat
    while a[l]<mid do inc(l);
    while a[r]>mid do dec(r);
    if l<=r then
      begin
        tmp:=a[l];a[l]:=a[r];a[r]:=tmp;
        tmp:=s[l];s[l]:=s[r];s[r]:=tmp;
        tmp:=t[l];t[l]:=t[r];t[r]:=tmp;
        tmp:=id[l];id[l]:=id[r];id[r]:=tmp;
        inc(l);dec(r);
      end;
  until l>r;
  if left<r then qsort(left,r);
  if l<right then qsort(l,right);
end;
//===============================================================
procedure Print(x,y:longint);
begin
  for i:=n downto 1 do
    begin
      ans[i]:=id[x];
      ans2[i]:=s[x]+y;
      nx:=pre[i,x,y];
      ny:=pre2[i,x,y];
      x:=nx;y:=ny;
    end;
  for i:=1 to n do
    writeln(ans[i],' ',ans2[i]);
end;
//===============================================================
procedure DP;
begin
  for i:=1 to m do
    for j:=0 to t[i]-s[i] do
      if int64(s[i]+j)>f[1,i,j] then
        f[1,i,j]:=int64(s[i]+j);
  for i:=2 to n do
    for j:=i to m do
      for z:=1 to j-1 do
        for x:=0 to t[z]-s[z] do
          if (f[i-1,z,x]>0)and(a[j]>a[z]) then
            begin
              tmp:=s[z]+int64(x)+k;
              if (s[j]<=tmp)and(tmp<=t[j])
                and(f[i-1,z,x]+tmp>f[i,j,tmp-s[j]])then
                begin
                  f[i,j,tmp-s[j]]:=f[i-1,z,x]+tmp;
                  pre[i,j,tmp-s[j]]:=z;
                  pre2[i,j,tmp-s[j]]:=x;
                end;
              tmp:=int64(s[z]+x)*k;
              if (s[j]<=tmp)and(tmp<=t[j])
                and(f[i-1,z,x]+tmp>f[i,j,tmp-s[j]])then
                begin
                  f[i,j,tmp-s[j]]:=f[i-1,z,x]+tmp;
                  pre[i,j,tmp-s[j]]:=z;
                  pre2[i,j,tmp-s[j]]:=x;
                end;
            end;
  tmp:=0;
  for z:=n to m do
    for x:=0 to t[z]-s[z] do
      if f[n,z,x]>tmp then
        begin
          tmp:=f[n,z,x];
          nx:=z;ny:=x;
        end;

  if tmp>0 then
    begin
      writeln('YES');
      Print(nx,ny);
      halt;
    end;
  writeln('NO');
end;
//===============================================================
begin
  readln(n,m,k);
  for i:=1 to m do
    begin
      readln(s[i],t[i],a[i]);
      id[i]:=i;
    end;
  qsort(1,m);
  DP;
end.

var m,n,key,i,j,k,s,max1,max2:longint;
    a,b:array[0..1000] of int64;
    f:array[0..100,0..100,0..200] of int64;
    p,v:array[0..100,0..100,0..200] of longint;
    c,max:int64;
    q,w:array[0..1000] of longint;

procedure swapi(var a,b:int64);
  var r:int64;
  begin
    r:=a;
    a:=b;
    b:=r;
  end;

procedure swap(var a,b:longint);
  var r:longint;
  begin
    r:=a;
    a:=b;
    b:=r;
  end;

procedure search(i,j,k:longint);
  begin
    if p[i,j,k]<>-1 then search(p[i,j,k],j-1,v[i,j,k]);
    if p[i,j,k]=-1 then
      writeln(w[i],' ',f[i,j,k])
    else
      writeln(w[i],' ',f[i,j,k]-f[p[i,j,k],j-1,v[i,j,k]]);
  end;

begin
  readln(n,m,key);
  for i:=1 to m do
    begin
      readln(a[i],b[i],q[i]);
      w[i]:=i;
    end;
  for i:=1 to m-1 do
    for j:=i+1 to m do
      if q[i]>q[j] then
        begin
          swapi(a[i],a[j]);
          swapi(b[i],b[j]);
          swap(q[i],q[j]);
          swap(w[i],w[j]);
        end;
  fillchar(f,sizeof(f),0);
  fillchar(p,sizeof(p),0);
  fillchar(v,sizeof(v),0);
  for i:=0 to b[1]-a[1] do
    begin
      f[1,1,i]:=i+a[1];
      p[1,1,i]:=-1;
      v[1,1,i]:=-1;
    end;
  for i:=2 to m do
    begin
      for j:=0 to b[i]-a[i] do
        begin
          f[i,1,j]:=j+a[i];
          p[i,1,j]:=-1;
          v[i,1,j]:=-1;
        end;
      for j:=2 to n do
        for k:=1 to i-1 do
          if q[i]>q[k] then
            for s:=0 to b[k]-a[k] do
              begin
                if p[k,j-1,s]=0 then continue;
                c:=s+a[k]+key;
                if (c>=a[i]) and (c<=b[i]) then
                  if f[k,j-1,s]+c>f[i,j,c-a[i]] then
                    begin
                      f[i,j,c-a[i]]:=f[k,j-1,s]+c;
                      p[i,j,c-a[i]]:=k;
                      v[i,j,c-a[i]]:=s;
                    end;
                c:=(s+a[k])*key;
                if (c>=a[i]) and (c<=b[i]) then
                  if f[k,j-1,s]+c>f[i,j,c-a[i]] then
                    begin
                      f[i,j,c-a[i]]:=f[k,j-1,s]+c;
                      p[i,j,c-a[i]]:=k;
                      v[i,j,c-a[i]]:=s;
                    end;
              end;
    end;
  max1:=0;
  max2:=0;
  max:=0;
  for i:=1 to m do
    for j:=0 to b[i]-a[i] do
      if f[i,n,j]>max then
        begin
          max:=f[i,n,j];
          max1:=i;
          max2:=j;
        end;
  if max=0 then writeln('NO') else
    begin
      writeln('YES');
      search(max1,n,max2);
    end;
end.

{$r-,q-,s-,i-,o+}
type
  int64=qword;
const
  inf:int64=100000000000000000;
var
  f:array[0..55,0..55,0..555]of int64;
  py,px:array[0..55,0..55,0..555]of longint;
  pxx,pyy,xx,yy,q,tmp,kr,i,j,k,n,m,tk:longint;
  max,ti,tt:int64;
  r2,a,b:array[0..500500]of int64;
  r1,nom,c:array[0..500500]of longint;

begin
  read(n,m,tk);
  for i:=1 to m do read(a[i],b[i],c[i]);

  for i:=1 to m do nom[i]:=i;
  for i:=1 to m-1 do
    for j:=1 to m-1 do
      if (c[j]>c[j+1])then begin
        ti:=a[j];a[j]:=a[j+1];a[j+1]:=ti;
        ti:=b[j];b[j]:=b[j+1];b[j+1]:=ti;
        tmp:=c[j];c[j]:=c[j+1];c[j+1]:=tmp;
        tmp:=nom[j];nom[j]:=nom[j+1];nom[j+1]:=tmp;
      end;

  fillchar(f,sizeof(f),$0);
  for j:=1 to m do
    for k:=0 to longint(b[j]-a[j])do
      f[1][j][k]:=k+a[j];

  for i:=1 to n-1 do
    for j:=1 to m do
      for k:=0 to longint(b[j]-a[j])do
        if (f[i][j][k]<>0)then begin
          tt:=a[j]+k+tk;
          for q:=j+1 to m do
            if (c[q]>c[j])and(tt>=a[q])and(tt<=b[q])and(f[i][j][k]+tt>f[i+1][q][longint(tt-a[q])])
              then begin
                f[i+1][q][longint(tt-a[q])]:=f[i][j][k]+tt;
                px[i+1][q][longint(tt-a[q])]:=j;
                py[i+1][q][longint(tt-a[q])]:=k;
              end;
          tt:=a[j]+k;
          if (tt>inf/tk)then continue;
          tt:=tt*tk;
          for q:=j+1 to m do
            if (c[q]>c[j])and(tt>=a[q])and(tt<=b[q])and(f[i][j][k]+tt>f[i+1][q][longint(tt-a[q])])
              then begin
                f[i+1][q][longint(tt-a[q])]:=f[i][j][k]+tt;
                px[i+1][q][longint(tt-a[q])]:=j;
                py[i+1][q][longint(tt-a[q])]:=k;
              end;
        end;

  max:=0;
  xx:=0;
  yy:=0;
  for j:=1 to m do
    for k:=0 to longint(b[j]-a[j])do
      if (f[n][j][k]>max)then begin
        max:=f[n][j][k];
        xx:=j;yy:=k;
      end;
  if (max<1)then begin
    writeln('NO');
    exit;
  end;
  kr:=0;
  writeln('YES');
  for i:=n downto 1 do begin
    inc(kr);
    r1[kr]:=nom[xx];
    r2[kr]:=a[xx]+yy;
    pxx:=px[i][xx][yy];
    pyy:=py[i][xx][yy];
    xx:=pxx;
    yy:=pyy;
  end;
  for i:=kr downto 1 do writeln(r1[i],' ',r2[i]);
end.

program probcc;

var f:array[0..50,0..50,0..1000]of int64;
    pre,pp:array[0..50,0..50,0..1000]of longint;
    a,b,c,num,anss,anst:array[0..500]of int64;
    n,m,k,ml,mj,i,j,p,l:longint;maxx,t:int64;

procedure init;
begin
  //assign(input,'cc.in');reset(input);
  readln(n,m,k);
  for i:=1 to m do begin
    readln(a[i],b[i],c[i]);num[i]:=i;
  end;
  for i:=1 to m-1 do
    for j:=i+1 to m do
      if c[i]>c[j] then begin
        t:=c[i];c[i]:=c[j];c[j]:=t;
        t:=a[i];a[i]:=a[j];a[j]:=t;
        t:=b[i];b[i]:=b[j];b[j]:=t;
        t:=num[i];num[i]:=num[j];num[j]:=t;
      end;
end;

function max(a,b:int64):int64;
begin if a>b then exit(a) else exit(b);end;

procedure dp;
begin
  for i:=1 to n do
    for j:=1 to m do
      for l:=0 to b[j]-a[j] do begin
        f[i,j,l]:=0;
        for p:=1 to j-1 do if c[p]<c[j] then begin
          if i=1 then continue;
          if ((a[j]+l) mod k=0)and((a[j]+l)div k>=a[p])and((a[j]+l)div k<=b[p])
          then
            if f[i,j,l]<f[i-1,p,(a[j]+l) div k-a[p]] then begin
              f[i,j,l]:=f[i-1,p,(a[j]+l) div k-a[p]];
              pre[i,j,l]:=p;pp[i,j,l]:=0;
            end;
          if (a[j]+l-k>=a[p])and(a[j]+l-k<=b[p]) then
            if f[i,j,l]<f[i-1,p,(a[j]+l-k-a[p])] then begin
              f[i,j,l]:=f[i-1,p,(a[j]+l-k-a[p])];
              pre[i,j,l]:=p;pp[i,j,l]:=1;
            end;
        end;
        if (f[i,j,l]>0)or(i=1) then inc(f[i,j,l],a[j]+l) else f[i,j,l]:=-maxlongint;
      end;
end;

begin
  init;
  dp;
  maxx:=-maxlongint;
  for j:=1 to m do
    for l:=0 to b[j]-a[j] do maxx:=max(f[n,j,l],maxx);
  for j:=1 to m do for l:=0 to b[j]-a[j] do if maxx=f[n,j,l] then begin
    mj:=j;ml:=l;
  end;
  if maxx<=0 then writeln('NO') else begin
    writeln('YES');
    anss[n]:=mj;anst[n]:=a[mj]+ml;
    for i:=n-1 downto 1 do begin
      anss[i]:=pre[i+1,mj,ml];
      if pp[i+1,mj,ml]=1 then anst[i]:=a[mj]+ml-k else anst[i]:=(a[mj]+ml) div k;
      mj:=anss[i];ml:=anst[i]-a[mj];
    end;
    for i:=1 to n do writeln(num[anss[i]],' ',anst[i]);
  end;
end.

type tracer = record
i,j,k:longint;
pr:longint;
End;

type sub = record
a:int64;
b:int64;
vt:longint;
c:longint;
End;

var f1,f2:text;
A:array[1..50] of sub;
F:array[1..50,1..50,0..100] of int64;
TR:array[1..50,1..50,0..100] of tracer;
m,n,sk,i:longint;

Procedure Partition(L,H:longint);
var i,j:longint;
    Pivot:longint;
    tg:sub;
Begin
     if L>=H then Exit;
     pivot := A[Random(H-L+1)+L].c;
     i:=L; j:=H;
     Repeat
       while A[i].c<pivot do Inc(i);
       While A[j].c>Pivot do dec(j);
       if i<=j then
       Begin
         If i<j then
         Begin
           tg:=a[i];
           a[i]:=a[j];
           A[j]:=tg;
         End;
         inc(i);
         dec(j);
       End;
     Until i>j;
     Partition(L,j); Partition(i,H);
End;

Function MT(i,j,k,pr:longint):tracer;
Begin
        MT.i:=i;MT.j:=j;MT.k:=k;MT.pr:=pr;
End;

Procedure trace(i,j,k:longint);
begin
        if tr[i,j,k].i<>0 then
        trace(tr[i,j,k].i,tr[i,j,k].j,tr[i,j,k].k);
        writeln(f2,A[Tr[i,j,k].pr].vt,' ',A[j].a+k);
ENd;

Procedure Process;
var i,j,k,l:longint;
v:int64;
s:int64;
res:int64;
vti,vtj,vtk:longint;
Begin
        For i:=1 to m do
        for j:=0 to a[i].b-a[i].a do
        Begin
          F[1,i,j]:=j+a[i].a;
          TR[1,i,j].pr:=i;
        End;

        for i:=2 to n do
        begin
          for j:=1 to m do
          for k:=0 to a[j].b-a[j].a do
          begin
            for l:=1 to j-1 do
            Begin
              if A[l].c<A[j].c then
              begin
                s:=a[j].a+k;
                if (s mod sk=0) and (a[l].a<=s div sk) and (s div sk<=a[l].b) then
                Begin
                  v:=F[i-1,l,s div sk-a[l].a];
                  if (v<>0) and (F[i,j,k]<v+s) then
                  Begin
                    F[i,j,k]:=v+s;
                    TR[i,j,k]:=MT(i-1,l,s div sk-a[l].a,j);
                  End;
                End;
                if (a[l].a<=s - sk) and (s - sk<= a[l].b) then
                Begin
                  v:=F[i-1,l,s - sk  -a[l].a];
                  if (v<>0) and (F[i,j,k]<v+s) then
                  Begin
                    F[i,j,k]:=v+s;
                    TR[i,j,k]:=MT(i-1,l,s - sk-a[l].a,j);
                  End;
                End;
              end;
            end;
          End;
        ENd;

        res:=0;
        for i:=1 to m do
        for j:=0 to a[i].b-a[i].a do
        if res<F[n,i,j] then
        Begin
          res:=F[n,i,j];
          vti:=n;vtj:=i;vtk:=j;
        end;
        if res<>0 then
        Begin
          writeln(f2,'YES');
          trace(vti,vtj,vtk);
        End
        else
        writeln(f2,'NO');
End;

Begin
        assign(f1,'');reset(f1);
        assign(f2,'');rewrite(f2);
        readln(f1,n,m,sk);
        for i:=1 to m do begin readln(f1,A[i].a,a[i].b,a[i].c); a[i].vt:=i; End;
        Partition(1,m);
        Process;
        close(f1);
        close(f2);
End.

Const
        fi = '';
        fo = '';
        MaxN = 55;

Type    sub = record
        a,b,c,d : Int64;
end;
        Arr1 = Array[0..MaxN+1,0..MaxN+1,0..105] Of Int64;
        Arr2 = Array[0..MaxN+1] Of sub;
        Arr3 = Array[0..MaxN+1,0..MaxN+1,0..105] Of sub;

Var     f : Arr1;
        a, b : Arr2;
        t : Arr3;
        n, m, k, i, x, y, j, l, vti, vtj : Longint;
        z1, z2, kq : Int64;

Procedure sort(l,r : longint);
 Var    i, j, p : longint;
        tmp : sub;
 Begin
        if l >= r then exit;
        i := l; j := r; p := a[(i+j)div 2].c;
        repeat
                while a[i].c < p do i := i+1;
                while a[j].c > p do j := j-1;
                if i <= j then
                begin
                        tmp := a[i]; a[i] := a[j]; a[j] := tmp;
                        i := i+1; j := j-1;
                end;
        until i > j;
        sort(l,j); sort(i,r);
 End;

Function max(a,b : int64) : int64;
 Begin
        if a > b then exit(a); exit(b);
 End;

Begin
        assign(input,fi); reset(input);
        read(n,m,k);
        for i := 1 to m do
        begin
                read(a[i].a,a[i].b,a[i].c);
                a[i].d := i;
        end;
        close(input);
        ////////
        sort(1,m);
        for i := 1 to m do
        for x := 0 to a[i].b-a[i].a do
                f[1,i,x] := a[i].a+x;
        ////////
        for i := 2 to n do
        for j := 1 to m do
        begin
                for y := 0 to a[j].b-a[j].a do
                for x := 1 to j-1 do
                if a[x].c < a[j].c then
                begin
                        z1 := y+a[j].a - k;
                        if (y+a[j].a) mod k = 0 then
                                z2 := (y+a[j].a) div k
                        else    z2 := -1;
                        if (z1 >= a[x].a) and (z1 <= a[x].b)
                                and(f[i-1,x,z1-a[x].a]<>0) then
                                begin
                                        if f[i-1,x,z1-a[x].a]+y+a[j].a>f[i,j,y] then
                                        begin
                                                f[i,j,y] := f[i-1,x,z1-a[x].a]+y+a[j].a;
                                                t[i,j,y].a := x;
                                                t[i,j,y].b := z1-a[x].a;
                                        end;
                                end;
                        if (z2 >= a[x].a) and (z2 <= a[x].b)
                                and(f[i-1,x,z2-a[x].a]<>0) then
                                begin
                                        if f[i-1,x,z2-a[x].a]+y+a[j].a>f[i,j,y] then
                                        begin
                                                f[i,j,y] := f[i-1,x,z2-a[x].a]+y+a[j].a;
                                                t[i,j,y].a := x;
                                                t[i,j,y].b := z2-a[x].a;
                                        end;
                                end;
                end;
        end;
        kq := 0;
        for i := 1 to m do
        for j := 0 to a[i].b-a[i].a do
                if f[n,i,j] > kq then
                begin
                        kq := f[n,i,j];
                        vti := i;
                        vtj := j;
                end;
        if kq = 0 then
        begin
                writeln('NO'); halt;
        end;
        b[1].a := a[vti].d; b[1].b := vtj+a[vti].a;
        j := vtj;
        vtj := t[n,vti,vtj].b;
        vti := t[n,vti,j].a;
        l := 1;
        for i := n-1 downto 1 do
        begin
                inc(l);
                b[l].a := a[vti].d;
                b[l].b := vtj+a[vti].a;
                j := vtj;
                vtj := t[i,vti,vtj].b;
                vti := t[i,vti,j].a;
        end;
        writeln('YES');
        for i := l downto 1 do
                writeln(b[i].a,' ',b[i].b);
End.

const fi='';//119c.inp';
      fo='';//119c.out';

type rec=record
          day,subj,exer,posi:longint;
end;
     rec1=record
          a,b:int64;
          c,cs:longint;
end;

var f1:array[0..55,0..55,0..110] of int64;
    trace:array[0..55,0..55,0..110] of rec;
    n,m,k:longint;
    pt:array[1..55] of rec1;
    f:text;

procedure qs(dd,cc:longint);
var i,j:longint; kk,tg:rec1;
begin
     if dd>=cc then exit;
     i:=dd; j:=cc; kk:=pt[random(cc-dd+1)+dd];
     repeat
     begin
          while pt[i].c<kk.c do inc(i);
          while pt[j].c>kk.c do dec(j);
          if i<=j then
          begin
               tG:=pt[i]; pt[i]:=pt[j]; pt[j]:=tg;
               inc(i); dec(j);
          end;
     end
     until i>j;
     qs(dd,j); qs(i,cc);
end;

procedure nhap;
var i:longint;
begin
     assign(f,fi); reset(f);
     readln(f,n,m,k);
     for i:=1 to m do
          with pt[i] do
          begin
               readln(f,a,b,c);
               cs:=i;
          end;
     qs(1,m);
     close(f);
end;

procedure work;
var i,j,kk,l:longint;  none:rec;   sl,tg:int64;
begin
     none.day:=-1; none.subj:=-1; none.exer:=-1;  //none.posi:=-1;
     for i:=1 to m do
          for j:=0 to pt[i].b-pt[i].a do
          begin
               f1[1,i,j]:=pt[i].a+j;
               trace[1,i,j]:=none;
               trace[1,i,j].posi:=-1;//pt[i].cs;
          end;
     for i:=2 to n do
          for j:=1 to m do
               for kk:= 0 to pt[j].b-pt[j].a do
               begin
                    sl:=pt[j].a+kk;
                    if sl mod k =0 then
                    for l:=1 to j-1 do
                         if (pt[j].c>pt[l].c) and (pt[l].a<=sl div k) and (pt[l].b>=sl div k) then
                         begin
                              tg:=f1[i-1,l,sl div k-pt[l].a];//+sl;
                              if tg>0 then
                              if tg+sl>f1[i,j,kk] then
                              begin
                                   f1[i,j,kk]:=tg+sl;
                                   trace[i,j,kk].day:=i-1;
                                   trace[i,j,kk].subj:=l;
                                   trace[i,j,kk].exer:= sl div  k-pt[l].a;
                                   trace[i,j,kk].posi:=pt[l].cs;
                                 //  if trace[i,j,kk].posi=0 then
                                 //       writeln(f,i,' ',j,' ',kk);
                              end;
                         end;
                    if sl>=k then
                    for l:=1 to j-1 do
                         if (pt[j].c>pt[l].c) and (pt[l].a<=sl-k) and (pt[l].b>=sl-k) then
                         begin
                              tg:=f1[i-1,l,sl-k-pt[l].a];//+sl;
                              if tg>0 then
                              if tg+sl> f1[i,j,kk] then
                              begin
                                   f1[i,j,kk] := tg+sl;
                                   trace[i,j,kk].day:=i-1;
                                   trace[i,j,kk].subj:=l;
                                   trace[i,j,kk].exer:=sl-k- pt[l].a;
                                   trace[i,j,kk].posi:=pt[l].cs;
                                   //if trace[i,j,kk].posi =0 then
                                   //     writeln(f,i,' ',j,' ',kk);
                              end;
                         end;
               end;
end;

procedure truy_vet(x:rec);
begin

     if (trace[x.day,x.subj,x.exer].day<>-1)
         // and (trace[x.day,x.subj,x.exer].day<>0)
          then
          truy_vet(trace[x.day,x.subj,x.exer]);
     writeln(f,x.posi,' ',pt[x.subj].a+x.exer);
     //if x.posi=0 then writeln(f,x.day,' ',x.subj,' ',x.exer,' ',-1);
end;

procedure inkq;
var i,j:longint; res:int64; re:rec;
begin
     assign(f,fo); rewrite(f);
     res:=0;
     work;
     for i:=1 to m do
          for j:=0 to pt[i].b-pt[i].a do
               if f1[n,i,j]>res then
               begin
                    res:=f1[n,i,j];
                    re.day:=n; re.subj:=i; re.exer:=j;    re.posi:=pt[i].cs;
               end;
    // work;
     if res=0 then writeln(f,'NO')
     else
     begin
          //work;
          writeln(f,'YES');
          truy_vet(re);
     end;
     close(f);
end;

begin
     nhap;
    // work;
     inkq;
end.

{$MODE DELPHI}
{$R-,Q-,O+,S+}
uses
  Math;
var
  a, b, c, num : array [1..1000] of int64;
  p, d, q : array [1..50, 0..100, 0..50] of int64;
  max, k, numb, qq, m, j, n : int64;
  i, l,ol, ii, jj : longint;
  ok : array [1..50, 0..100, 0..50] of boolean;
  used : array [1..100] of boolean;
procedure swap(var a, b : int64);
var
  c : int64;
begin
  c := a;
  a := b;
   b:= c;
end;
procedure vos(j, i, k : int64);
begin
  if k < 0 then
    exit;
  vos(p[i, b[i] - j, k], q[i, b[i] - j, k], k - 1);
  writeln(num[i], ' ', j);
end;
begin
//  reset(input, 'input.txt');
//  rewrite(output, 'output.txt');
  readln(n, m, k);
  for i := 1 to m do
    begin
      read(a[i], b[i], c[i]);
      num[i] := i;
    end;
  if m = 1 then
    begin
      writeln('YES');
      writeln(1, ' ', b[1]);
      halt;
    end;
  for i := 1 to m do
    for l := i + 1 to m do
      if c[i] > c[l] then
        begin
          swap(c[i], c[l]);
          swap(a[i], a[l]);
          swap(b[i], b[l]);
          swap(num[i], num[l]);
        end;
  for i := 1 to m do
    begin
      j := a[i];
      while j <= b[i] do
        begin
          d[i, b[i] - j, 0] := j;
          ok[i, b[i] - j, 0] := true;
          inc(j);
        end;
    end;
  Dec(n);
  for i := 2 to m do
    begin
      ol := min(n , i  -1);
      for ii := 1 to ol do begin
      for jj := 1 to i - 1 do begin
      j := a[i];
      while j <= b[i] do
        begin
          if (j mod k = 0) and (a[jj] <= j div k) and (c[i] > c[jj]) and (b[jj] >= j div k) and ok[jj, b[jj] - (j div k), ii - 1] and (d[i, b[i] - j, ii] < d[jj, b[jj] - (j div k), ii - 1] + j) then
            begin
              d[i, b[i] - j, ii] := d[jj, b[jj] - (j div k), ii - 1] + j;
              q[i, b[i] - j, ii] := jj;
              p[i, b[i] - j, ii] := j div k;
              ok[i, b[i] - j, ii] := true;
            end;
          if (j - k >= a[jj]) and (j - k <= b[jj]) and (c[i] > c[jj]) and (d[i, b[i] - j, ii] < d[jj, b[jj] - (j - k), ii - 1] + j) and ok[jj, b[jj] - (j - k), ii - 1] then
            begin
              ok[i, b[i] - j, ii] := true;
              q[i, b[i] - j, ii] := jj;
              d[i, b[i] - j, ii] := d[jj, b[jj] - (j - k), ii - 1] + j;
              p[i, b[i] - j, ii] := j - k;
            end;
          inc(j);
        end;
      end;
//          d[i, b[i] - j, ii] := d[i, b[i] - j, ii] + j;
      end;
    end;
  max := 0;
  for i := 1 to m do
    begin
      j := a[i];
      while j <= b[i] do
        begin
          if (d[i, b[i] - j, n] > max) and ok[i, b[i] - j, n] then
            begin
              max := d[i, b[i] - j, n];
              numb := i;
              qq := j;
            end;
          inc(j);
        end;
    end;
  if max = 0 then
    writeln('NO')
  else
    begin
      writeln('YES');
      vos(qq, numb, n);
    end;
end.

const
{     fi='edu.inp';
     fo='edu.out';}
     fi='';
     fo='';
     maxn=111;

var
     f:text;
     a,b,c,ind:array[0..maxn]of int64;
     t1,t2,fx:array[0..maxn,0..maxn,0..maxn]of int64;
     k1,k2:array[0..maxn]of int64;
     n,m,k:longint;

function maxx(x,y:longint):longint;
begin
     maxx:=x;
     if y>x then maxx:=y;
end;

function minn(x,y:longint):longint;
begin
     minn:=x;
     if y<x then minn:=y;
end;

procedure swap(var x,y:int64);
var  z:int64;
begin
     z:=x;
     x:=y;
     y:=z;
end;

procedure sort;
var  i,j,t:integer;
begin
     for i:=1 to m-1 do
     begin
          t:=i;
          for j:=i+1 to m do
               if c[j]<c[t] then t:=j;
          swap(a[i],a[t]);
          swap(b[i],b[t]);
          swap(c[i],c[t]);
          swap(ind[i],ind[t]);
     end;
end;

procedure open;
var  i:longint;
begin
     assign(f,fi);reset(f);
     readln(f,n,m,k);
     for i:=1 to m do
     begin
          readln(f,a[i],b[i],c[i]);
          ind[i]:=i;
     end;
     close(f);
end;

procedure main;
var  kq,tmp,cv,sl,r1,r2:int64;
     i,j,t,i2:longint;
begin
     //fx[i,j,t] i cv dau trong j ngay va ngay j lam cv i
     fillchar(fx,sizeof(fx),0);
     fillchar(t1,sizeof(fx),0);
     fillchar(t2,sizeof(fx),0);
     for i:=1 to m do
          for t:=1 to b[i]-a[i]+1 do
               fx[i,1,t]:=t+a[i]-1;

     //
     for j:=2 to n do
     for i:=j to m do
          for t:=1 to b[i]-a[i]+1 do
          for i2:=j-1 to i-1 do
          if c[i]>c[i2] then
          begin
               tmp:=a[i]+t-1-k;
               if (a[i2]<=tmp) and (tmp<=b[i2]) then
                    if fx[i2,j-1,tmp-a[i2]+1]<>0 then
                    if fx[i,j,t]<fx[i2,j-1,tmp-a[i2]+1]+t+a[i]-1 then
                    begin
                         fx[i,j,t]:=fx[i2,j-1,tmp-a[i2]+1]+t+a[i]-1;
                         t1[i,j,t]:=i2;
                         t2[i,j,t]:=tmp-a[i2]+1;
                    end;

               if (t+a[i]-1) mod k=0 then
               begin
                    tmp:=(a[i]+t-1) div k;
                    if (a[i2]<=tmp) and (tmp<=b[i2]) then
                         if fx[i2,j-1,tmp-a[i2]+1]<>0 then
                         if fx[i,j,t]<fx[i2,j-1,tmp-a[i2]+1]+t+a[i]-1 then
                         begin
                              fx[i,j,t]:=fx[i2,j-1,tmp-a[i2]+1]+t+a[i]-1;
                              t1[i,j,t]:=i2;
                              t2[i,j,t]:=tmp-a[i2]+1;
                         end;
               end;
          end;

     kq:=0;
     for i:=n to m do
          for t:=1 to b[i]-a[i]+1 do
          begin
               if kq<fx[i,n,t] then
               begin
                    kq:=fx[i,n,t];
                    cv:=i;
                    sl:=t;
               end;
          end;

     assign(f,fo);rewrite(f);
     if kq=0 then writeln(f,'NO')
     else
     begin
          writeln(f,'YES');
          for j:=n downto 1 do
          begin
               r1:=cv;r2:=sl;
               k1[j]:=ind[cv];
               k2[j]:=sl+a[cv]-1;
               cv:=t1[r1,j,r2];
               sl:=t2[r1,j,r2];
          end;
          for j:=1 to n do writeln(f,k1[j],' ',k2[j]);
     end;
     close(f);
end;

begin
     open;
     sort;
     main;
end.

type pp=record
         x,y:int64;
        end;

var n,m,x,y,i,j:longint;
    max,k,g,k1,k2,t:int64;
    a,b,c,d:array[0..51] of int64;
    v:array[0..51,0..51,0..101] of int64;
    w:array[0..51,0..51,0..101] of pp;

procedure sort(l,r: longint);
      var
         i,j: longint;
         x,y: int64;
      begin
         i:=l;
         j:=r;
         x:=c[(l+r) div 2];
         repeat
           while c[i]<x do
            inc(i);
           while x<c[j] do
            dec(j);
           if not(i>j) then
             begin
                y:=a[i];
                a[i]:=a[j];
                a[j]:=y;
                y:=b[i];
                b[i]:=b[j];
                b[j]:=y;
                y:=c[i];
                c[i]:=c[j];
                c[j]:=y;
                y:=d[i];
                d[i]:=d[j];
                d[j]:=y;
                inc(i);
                j:=j-1;
             end;
         until i>j;
         if l<j then
           sort(l,j);
         if i<r then
           sort(i,r);
      end;

procedure tt(l,x,y:int64);
 begin
  if l=0 then exit;
  tt(l-1,w[l,x,y].x,w[l,x,y].y);
  writeln(d[x],' ',a[x]+y);
 end;

 begin

  read(n,m,k);
  for i:=1 to m do
   begin
    read(a[i],b[i],c[i]);
    d[i]:=i;
   end;
  sort(1,m);

  for i:=1 to m do
   for j:=0 to 100 do
    begin
     if a[i]+j>b[i] then break;
     v[1,i,j]:=a[i]+j;
    end;

  for i:=1 to n-1 do
   for x:=1 to m-1 do
    for y:=x+1 to m do
     for j:=0 to 100 do
      begin

       if v[i,x,j]=0 then continue;
       if c[y]<=c[x] then continue;

       t:=v[i,x,j];

       g:=a[x]+int64(j);

       k1:=g+k;
       k2:=g*k;

       if (a[y]<=k1) and
          (k1<=b[y]) then begin
                           if k1+t>v[i+1,y,k1-a[y]] then begin
                                                          v[i+1,y,k1-a[y]]:=k1+t;
                                                          w[i+1,y,k1-a[y]].x:=x;
                                                          w[i+1,y,k1-a[y]].y:=j;
                                                         end;
                          end;
       if (a[y]<=k2) and
          (k2<=b[y]) then begin
                           if k2+t>v[i+1,y,k2-a[y]] then begin
                                                          v[i+1,y,k2-a[y]]:=k2+t;
                                                          w[i+1,y,k2-a[y]].x:=x;
                                                          w[i+1,y,k2-a[y]].y:=j;
                                                         end;
                          end;
      end;

  max:=0;
  for i:=1 to m do
   for j:=0 to 100 do
    if v[n,i,j]>max then begin
                          max:=v[n,i,j];
                          x:=i;
                          y:=j;
                         end;

  if max=0 then begin
                 writeln('NO');
                 halt;
                end;

  writeln('YES');
  tt(n,x,y);
 end.

program Csgd;
const
  fi = '';
  fo = '';
  nmax = 50;
  dmax = 100;
type
  mon = record
    a, b: int64;
    c: longint;
    i : longint;
    end;
var
  a: array[1..nmax, 1..nmax, 0..dmax] of int64;
  t, p: array[1..nmax, 1..nmax, 0..dmax] of longint;
  b: array[1..nmax] of mon;
  n, m, k: longint;
  sum: int64;
  mi, ai: longint;
  f1, f2: text;

  procedure openf;
  begin
    assign(f1, fi); reset(f1);
    assign(f2, fo); rewrite(f2);
  end;

  procedure closef;
  begin
    close(f1); close(f2);
  end;

  procedure input;
  var
    i : longint;
  begin
    readln(f1, n, m, k);
    for i := 1 to m do
      begin
        readln(f1, b[i].a, b[i].b, b[i].c);
        b[i].i := i;
        b[i].b := b[i].b - b[i].a;
      end;
  end;

  procedure output;
  var
    i, tmp : longint;
  begin
    if sum = 0 then
      write(f2, 'NO')
    else
      begin
        writeln(f2, 'YES');
        for i := 1 to n do
          begin
            writeln(f2, b[mi].i, ' ', ai + b[mi].a);
            tmp := t[i, mi, ai];
            ai := p[i, mi, ai];
            mi := tmp;
          end;
      end;
  end;

  procedure sort(i, j: longint);
  var
    l, r: longint;
    x: longint;
    y: mon;
  begin
    l := i;
    r := j;
    x := b[random(r - l + 1) + l].c;
    repeat
      while b[l].c < x do inc(l);
      while b[r].c > x do dec(r);
      if l <= r then
        begin
          if l < r then
            begin
              y := b[l];
              b[l] := b[r];
              b[r] := y;
            end;
          inc(l);
          dec(r);
        end;
    until l > r;
    if l < j then sort(l, j);
    if i < r then sort(i, r);
  end;

  procedure process;
  var
    i, j, l, u, v: longint;
    ttt, tmp: int64;
  begin
    sort(1, m);
    for j := 1 to m do
      for l := 0 to b[j].b do
        a[n, j, l] := b[j].a + l;
    for i := n - 1 downto 1 do
      for j := m downto i do
        for l := 0 to b[j].b do
          begin
            tmp := 0;
            for u := m downto j + 1 do
              if b[j].c < b[u].c then
                begin
                  ttt := (b[j].a + l) + k - (b[u].a);
                  v := ttt;
                  if (v >= 0) and (v <= b[u].b) and (tmp < a[i + 1, u, v]) then
                    begin
                      tmp := a[i + 1, u, v];
                      t[i, j, l] := u;
                      p[i, j, l] := v;
                    end;
                  ttt := (b[j].a + l) * k - (b[u].a);
                  v := ttt;
                  if (v >= 0) and (v <= b[u].b) and (tmp < a[i + 1, u, v]) then
                    begin
                      tmp := a[i + 1, u, v];
                      t[i, j, l] := u;
                      p[i, j, l] := v;
                    end;
                end;
            if tmp > 0 then
              a[i, j, l] := tmp + b[j].a + l
            else
              a[i, j, l] := 0;
          end;
    sum := 0;
    for j := 1 to m do
      for l := 0 to b[j].b do
        if sum < a[1, j, l] then
          begin
            sum := a[1, j, l];
            mi := j;
            ai := l;
          end;
  end;

begin
  openf;
  input;
  process;
  output;
  closef;
end.

program reform;

uses
  Math;

const
  maxn = 51;
  maxk = 101;

type
  st = record
    a, b, c, id:  int64;
  end;
  anst = record
    num, sb:  int64;
  end;
  prevt = record
    last, temp: longint;
  end;

var
  subj: array [1..maxn] of st;
  ans:  array [1..maxn] of anst;
  dp: array [0..maxn, 0..maxk, 0..maxn] of int64;
  prev: array [0..maxn, 0..maxk, 0..maxn] of prevt;
  i, j, z, n, m, k, first, second, newfirst, newsecond, last, t: longint;
  temp: int64;

procedure swap(var a, b:  st);
var
  temp: st;
begin
  temp := a;
  a := b;
  b := temp;
end;

function less(a, b: st):  boolean;
begin
  if a.c < b.c then
    less := true
  else
    less := false;
end;

procedure qsort(low, high:  longint);
var
  i, j: longint;
  m:  st;
begin
  i := low; j := high;
  m := subj[i + random(j - i)];
  repeat
    while less(subj[i], m) do
      inc(i);
    while less(m, subj[j]) do
      dec(j);
    if (i <= j) then begin
      swap(subj[i], subj[j]);
      inc(i); dec(j);
    end;
  until (i > j);
  if (low < j) then qsort(low, j);
  if (i < high) then qsort(i, high);
end;

begin
  //reset(input, 'reform.in');
  //rewrite(output, 'reform.out');
  readln(n, m, k);
  for i := 1 to m do begin
    readln(subj[i].a, subj[i].b, subj[i].c);
    subj[i].id := i;
  end;
  qsort(1, m);
  first := 0; second := 0; dp[0, 0, n] := -1;
  for i := 1 to m do
    for j := 0 to (subj[i].b - subj[i].a) do begin
      dp[i, j, 1] := subj[i].a + j;
      for z := 2 to n do begin
        dp[i, j, z] := -1;
        for last := i - 1 downto 1 do
          if (subj[last].c < subj[i].c) then
            for t := 0 to 1 do begin
              temp := subj[i].a + j;
              if (t = 0) and (temp mod k = 0) then begin
                temp := (temp div k) - subj[last].a;
                if (temp >= 0) and (temp <= subj[last].b - subj[last].a) and (dp[last, temp, z - 1] <> -1) and (dp[last, temp, z - 1] + subj[i].a + j > dp[i, j, z]) then begin
                  dp[i, j, z] := dp[last, temp, z - 1] + subj[i].a + j;
                  prev[i, j, z].last := last;
                  prev[i, j, z].temp := temp;
                end;
              end
              else
                if (t = 1) and (temp >= k) then begin
                  temp := temp - k - subj[last].a;
                  if (temp >= 0) and (temp <= subj[last].b - subj[last].a) and (dp[last, temp, z - 1] <> -1) and (dp[last, temp, z - 1] + subj[i].a + j > dp[i, j, z]) then begin
                    dp[i, j, z] := dp[last, temp, z - 1] + subj[i].a + j;
                    prev[i, j, z].last := last;
                    prev[i, j, z].temp := temp;
                  end;
                end;
            end;
      end;
      if (dp[i, j, n] > dp[first, second, n]) then begin
        first := i;
        second := j;
      end;
    end;
    if (dp[first, second, n] = -1) then begin
      writeln('NO');
      halt(0);
    end;
    for i := n downto 1 do begin
      ans[i].num := subj[first].id;
      ans[i].sb := subj[first].a + second;
      newfirst := prev[first, second, i].last;
      newsecond := prev[first, second, i].temp;
      first := newfirst; second := newsecond;
    end;
    writeln('YES');
    for i := 1 to n do
      writeln(ans[i].num, ' ', ans[i].sb);
end.

{$APPTYPE CONSOLE}

var
  idx1,idx,idy,z,l,x,i,j,n,m:integer;
  ans,s1,s2,s,k:int64;
  an1,a,b:array[1..101] of int64;
  nn1,nn,c:array[1..101] of integer;
  an:array[1..51,1..51,0..101] of int64;
  cur,prevl,prevs:array[1..51,1..51,0..101] of integer;

  Procedure swap(var a,b:int64);
  var
   x:int64;
  begin
   x:=a;
   a:=b;
   b:=x;
  end;

  Procedure sort(l,r:integer);
  var
   i,j,x:integer;
  begin
    For i:=l+1 to r do
     For j:=r downto i do If a[j-1]>a[j] then
     begin
      swap(a[j-1],a[j]);
      swap(b[j-1],b[j]);
      x:=c[j-1];
      c[j-1]:=c[j];
      c[j]:=x;
      x:=nn[j-1];
      nn[j-1]:=nn[j];
      nn[j]:=x;
     end;
  end;

begin
 {reset(input,'Input.txt');
 rewrite(output,'Output.txt');}

 readln(n,m,k);

 For i:=1 to m do readln(a[i],b[i],c[i]);
 For i:=1 to m do nn[i]:=i;

  For i:=2 to m  do
    For j:=m downto i do If c[j-1]>c[j] then
    begin
     swap(a[j-1],a[j]);
     swap(b[j-1],b[j]);
     x:=c[j-1];
     c[j-1]:=c[j];
     c[j]:=x;
     x:=nn[j-1];
     nn[j-1]:=nn[j];
     nn[j]:=x;
    end;

 j:=1;
 c[m+1]:=-100;
 while j<=m do
 begin
  l:=j;
  while (c[j+1]=c[j]) do inc(j);
  Sort(l,j);
  inc(j);
 end;

 Fillchar(prevs,sizeof(prevs),0);
 Fillchar(prevl,sizeof(prevl),0);
 Fillchar(cur,sizeof(cur),0);

 For i:=1 to n do
  For j:=1 to m do
   For z:=0 to 101 do an[i,j,z]:=-100000000000000000;

 For i:=1 to m do
  For j:=0 to b[i]-a[i] do
  begin
   prevs[1,i,j]:=-1;
   prevl[1,i,j]:=-1;
   an[1,i,j]:=a[i]+j;
   cur[1,i,j]:=a[i]+j;
  end;

 For i:=2 to n do
  For j:=1 to m do
   For z:=0 to b[j]-a[j] do
   begin
     s:=a[j]+z;
     If s mod k=0 then s1:=s div k else s1:=-1;
     s2:=s-k;
     For l:=i to j-1 do
     begin
      If ((s1>=a[l]) and (s1<=b[l])) and (an[i-1,l,s1-a[l]]<>-100000000000000000)
      and (an[i,j,z]<an[i-1,l,s1-a[l]]+s) then
      begin
       an[i,j,z]:=an[i-1,l,s1-a[l]]+s;
       prevl[i,j,z]:=l;
       prevs[i,j,z]:=s1-a[l];
       cur[i,j,z]:=s;
      end;

      If ((s2>=a[l]) and (s2<=b[l])) and (an[i-1,l,s2-a[l]]<>-100000000000000000)
      and (an[i,j,z]<an[i-1,l,s2-a[l]]+s) then
      begin
       an[i,j,z]:=an[i-1,l,s2-a[l]]+s;
       prevl[i,j,z]:=l;
       prevs[i,j,z]:=s2-a[l];
       cur[i,j,z]:=s;
      end;
     end;
   end;

  ans:=-100000000000000000;
  idx:=0;
  idy:=0;

  For i:=1 to m do
   For j:=0 to b[i]-a[i] do
   If an[n,i,j]>ans then
   begin
    ans:=an[n,i,j];
    idx:=i;
    idy:=j;
   end;

  If ans=-100000000000000000 then writeln('NO') else
  begin
   writeln('YES');
   For i:=n downto 1 do
   begin
    nn1[i]:=nn[idx];
    an1[i]:=cur[i,idx,idy];
    idx1:=prevl[i,idx,idy];
    idy:=prevs[i,idx,idy];
    idx:=idx1;
   end;
   For i:=2 to n do
    For j:=n downto i do If  c[nn1[j-1]]>c[nn1[j]] then
    begin
     swap(an1[j-1],an1[j]);
     x:=nn1[j-1];
     nn1[j-1]:=nn1[j];
     nn1[j]:=x;
    end;
    For i:=1 to n do writeln(nn1[i],' ',an1[i]);
  end;

end.

{$APPTYPE CONSOLE}

var
  idx1,idx,idy,z,l,x,i,j,n,m:integer;
  ans,s1,s2,s,k:int64;
  an1,a,b:array[1..101] of int64;
  c1,nn1,nn,c:array[1..101] of integer;
  an:array[1..51,1..51,0..101] of int64;
  cur,prevl,prevs:array[1..51,1..51,0..101] of integer;

  Procedure swap(var a,b:int64);
  var
   x:int64;
  begin
   x:=a;
   a:=b;
   b:=x;
  end;

begin
 {reset(input,'Input.txt');
 rewrite(output,'Output.txt');}

 readln(n,m,k);

 For i:=1 to m do readln(a[i],b[i],c[i]);
 For i:=1 to m do nn[i]:=i;

  For i:=2 to m  do
    For j:=m downto i do If c[j-1]>c[j] then
    begin
     swap(a[j-1],a[j]);
     swap(b[j-1],b[j]);
     x:=c[j-1];
     c[j-1]:=c[j];
     c[j]:=x;
     x:=nn[j-1];
     nn[j-1]:=nn[j];
     nn[j]:=x;
    end;

 Fillchar(prevs,sizeof(prevs),0);
 Fillchar(prevl,sizeof(prevl),0);
 Fillchar(cur,sizeof(cur),0);

 For i:=1 to n do
  For j:=1 to m do
   For z:=0 to 101 do an[i,j,z]:=-100000000000000000;

 For i:=1 to m do
  For j:=0 to b[i]-a[i] do
  begin
   prevs[1,i,j]:=-1;
   prevl[1,i,j]:=-1;
   an[1,i,j]:=a[i]+j;
   cur[1,i,j]:=a[i]+j;
  end;

  //For i:=1 to m do writeln(a[i],' ',b[i],' ',c[i],' ',nn[i]);

 For i:=2 to n do
  For j:=i to m do
   For z:=0 to b[j]-a[j] do
   begin
     s:=a[j]+z;
     If s mod k=0 then s1:=s div k else s1:=-1;
     s2:=s-k;
     For l:=i-1 to j-1 do
     begin
      If (c[j]>c[l]) and ((s1>=a[l]) and (s1<=b[l])) and (an[i-1,l,s1-a[l]]<>-100000000000000000)
      and (an[i,j,z]<an[i-1,l,s1-a[l]]+s) then
      begin
       an[i,j,z]:=an[i-1,l,s1-a[l]]+s;
       prevl[i,j,z]:=l;
       prevs[i,j,z]:=s1-a[l];
       cur[i,j,z]:=s;
      end;

      If (c[j]>c[l]) and ((s2>=a[l]) and (s2<=b[l])) and (an[i-1,l,s2-a[l]]<>-100000000000000000)
      and (an[i,j,z]<an[i-1,l,s2-a[l]]+s) then
      begin
       an[i,j,z]:=an[i-1,l,s2-a[l]]+s;
       prevl[i,j,z]:=l;
       prevs[i,j,z]:=s2-a[l];
       cur[i,j,z]:=s;
      end;
     end;
   end;

  ans:=-100000000000000000;
  idx:=0;
  idy:=0;

  For i:=1 to m do
   For j:=0 to b[i]-a[i] do
   If an[n,i,j]>ans then
   begin
    ans:=an[n,i,j];
    idx:=i;
    idy:=j;
   end;

  If ans=-100000000000000000 then writeln('NO') else
  begin
   writeln('YES');
   For i:=n downto 1 do
   begin
    nn1[i]:=nn[idx];
    c1[i]:=c[idx];
    an1[i]:=cur[i,idx,idy];
    idx1:=prevl[i,idx,idy];
    idy:=prevs[i,idx,idy];
    idx:=idx1;
   end;
   For i:=2 to n do
    For j:=n downto i do If  c1[j-1]>c1[j] then
    begin
     swap(an1[j-1],an1[j]);
     x:=nn1[j-1];
     nn1[j-1]:=nn1[j];
     nn1[j]:=x;
     x:=c1[j-1];
     c1[j-1]:=c1[j];
     c1[j]:=x;
    end;
    For i:=1 to n do writeln(nn1[i],' ',an1[i]);
  end;

end.

{$APPTYPE CONSOLE}

var
  idx1,idx,idy,z,l,x,i,j,n,m:integer;
  ans,s1,s2,s,k:int64;
  an1,a,b:array[1..101] of int64;
  c1,nn1,nn,c:array[1..101] of integer;
  an:array[1..51,1..51,0..101] of int64;
  cur,prevl,prevs:array[1..51,1..51,0..101] of integer;

  Procedure swap(var a,b:int64);
  var
   x:int64;
  begin
   x:=a;
   a:=b;
   b:=x;
  end;

  Procedure sort(l,r:integer);
  var
   i,j,x:integer;
  begin
    For i:=l+1 to r do
     For j:=r downto i do If a[j-1]>a[j] then
     begin
      swap(a[j-1],a[j]);
      swap(b[j-1],b[j]);
      x:=c[j-1];
      c[j-1]:=c[j];
      c[j]:=x;
      x:=nn[j-1];
      nn[j-1]:=nn[j];
      nn[j]:=x;
     end;
  end;

begin
 {reset(input,'Input.txt');
 rewrite(output,'Output.txt'); }

 readln(n,m,k);

 For i:=1 to m do readln(a[i],b[i],c[i]);
 For i:=1 to m do nn[i]:=i;

  For i:=2 to m  do
    For j:=m downto i do If c[j-1]>c[j] then
    begin
     swap(a[j-1],a[j]);
     swap(b[j-1],b[j]);
     x:=c[j-1];
     c[j-1]:=c[j];
     c[j]:=x;
     x:=nn[j-1];
     nn[j-1]:=nn[j];
     nn[j]:=x;
    end;

 j:=1;
 c[m+1]:=-100;
 while j<=m do
 begin
  l:=j;
  while (c[j+1]=c[j]) do inc(j);
  Sort(l,j);
  inc(j);
 end;

 Fillchar(prevs,sizeof(prevs),0);
 Fillchar(prevl,sizeof(prevl),0);
 Fillchar(cur,sizeof(cur),0);

 For i:=1 to n do
  For j:=1 to m do
   For z:=0 to 101 do an[i,j,z]:=-100000000000000000;

 For i:=1 to m do
  For j:=0 to b[i]-a[i] do
  begin
   prevs[1,i,j]:=-1;
   prevl[1,i,j]:=-1;
   an[1,i,j]:=a[i]+j;
   cur[1,i,j]:=a[i]+j;
  end;

 For i:=2 to n do
  For j:=1 to m do
   For z:=0 to b[j]-a[j] do
   begin
     s:=a[j]+z;
     If s mod k=0 then s1:=s div k else s1:=-1;
     s2:=s-k;
     For l:=i to j-1 do
     begin
      If (c[j]>c[l]) and ((s1>=a[l]) and (s1<=b[l])) and (an[i-1,l,s1-a[l]]<>-100000000000000000)
      and (an[i,j,z]<an[i-1,l,s1-a[l]]+s) then
      begin
       an[i,j,z]:=an[i-1,l,s1-a[l]]+s;
       prevl[i,j,z]:=l;
       prevs[i,j,z]:=s1-a[l];
       cur[i,j,z]:=s;
      end;

      If (c[j]>c[l]) and ((s2>=a[l]) and (s2<=b[l])) and (an[i-1,l,s2-a[l]]<>-100000000000000000)
      and (an[i,j,z]<an[i-1,l,s2-a[l]]+s) then
      begin
       an[i,j,z]:=an[i-1,l,s2-a[l]]+s;
       prevl[i,j,z]:=l;
       prevs[i,j,z]:=s2-a[l];
       cur[i,j,z]:=s;
      end;
     end;
   end;

  ans:=-100000000000000000;
  idx:=0;
  idy:=0;

  For i:=1 to m do
   For j:=0 to b[i]-a[i] do
   If an[n,i,j]>ans then
   begin
    ans:=an[n,i,j];
    idx:=i;
    idy:=j;
   end;

  If ans=-100000000000000000 then writeln('NO') else
  begin
   writeln('YES');
   For i:=n downto 1 do
   begin
    nn1[i]:=nn[idx];
    c1[i]:=c[idx];
    an1[i]:=cur[i,idx,idy];
    idx1:=prevl[i,idx,idy];
    idy:=prevs[i,idx,idy];
    idx:=idx1;
   end;
   For i:=2 to n do
    For j:=n downto i do If  c1[j-1]>c1[j] then
    begin
     swap(an1[j-1],an1[j]);
     x:=nn1[j-1];
     nn1[j-1]:=nn1[j];
     nn1[j]:=x;
     x:=c1[j-1];
     c1[j-1]:=c1[j];
     c1[j]:=x;
    end;
    For i:=1 to n do writeln(nn1[i],' ',an1[i]);
  end;

end.

{$APPTYPE CONSOLE}

var
  idx1,idx,idy,z,l,x,i,j,n,m:integer;
  ans,s1,s2,s,k:int64;
  a,b:array[1..101] of int64;
  nn,c:array[1..101] of integer;
  an:array[1..51,1..51,0..101] of int64;
  cur,prevl,prevs:array[1..51,1..51,0..101] of integer;

  Procedure swap(var a,b:int64);
  var
   x:int64;
  begin
   x:=a;
   a:=b;
   b:=x;
  end;

  Procedure sort(l,r:integer);
  var
   i,j,x:integer;
  begin
    For i:=l+1 to r do
     For j:=r downto i do If a[j-1]>a[j] then
     begin
      swap(a[j-1],a[j]);
      swap(b[j-1],b[j]);
      x:=c[j-1];
      c[j-1]:=c[j];
      c[j]:=x;
      x:=nn[j-1];
      nn[j-1]:=nn[j];
      nn[j]:=x;
     end;
  end;

begin
 {reset(input,'Input.txt');
 rewrite(output,'Output.txt'); }

 readln(n,m,k);

 For i:=1 to m do readln(a[i],b[i],c[i]);
 For i:=1 to m do nn[i]:=i;

  For i:=2 to m  do
    For j:=m downto i do If c[j-1]>c[j] then
    begin
     swap(a[j-1],a[j]);
     swap(b[j-1],b[j]);
     x:=c[j-1];
     c[j-1]:=c[j];
     c[j]:=x;
     x:=nn[j-1];
     nn[j-1]:=nn[j];
     nn[j]:=x;
    end;

 j:=1;
 c[m+1]:=-100;
 while j<=m do
 begin
  l:=j;
  while (c[j+1]=c[j]) do inc(j);
  Sort(l,j);
  inc(j);
 end;

 Fillchar(prevs,sizeof(prevs),0);
 Fillchar(prevl,sizeof(prevl),0);
 Fillchar(cur,sizeof(cur),0);

 For i:=1 to n do
  For j:=1 to m do
   For z:=0 to 101 do an[i,j,z]:=-100000000000000000;

 For i:=1 to m do
  For j:=0 to b[i]-a[i] do
  begin
   prevs[1,i,j]:=-1;
   prevl[1,i,j]:=-1;
   an[1,i,j]:=a[i]+j;
   cur[1,i,j]:=a[i]+j;
  end;

 For i:=2 to n do
  For j:=1 to m do
   For z:=0 to b[j]-a[j] do
   begin
     s:=a[j]+z;
     If s mod k=0 then s1:=s div k else s1:=-1;
     s2:=s-k;
     For l:=i to j-1 do
     begin
      If ((s1>=a[l]) and (s1<=b[l])) and (an[i-1,l,s1-a[l]]<>-100000000000000000)
      and (an[i,j,z]<an[i-1,l,s1-a[l]]+s) then
      begin
       an[i,j,z]:=an[i-1,l,s1-a[l]]+s;
       prevl[i,j,z]:=l;
       prevs[i,j,z]:=s1-a[l];
       cur[i,j,z]:=s;
      end;

      If ((s2>=a[l]) and (s2<=b[l])) and (an[i-1,l,s2-a[l]]<>-100000000000000000)
      and (an[i,j,z]<an[i-1,l,s2-a[l]]+s) then
      begin
       an[i,j,z]:=an[i-1,l,s2-a[l]]+s;
       prevl[i,j,z]:=l;
       prevs[i,j,z]:=s2-a[l];
       cur[i,j,z]:=s;
      end;
     end;
   end;

  ans:=-100000000000000000;
  idx:=0;
  idy:=0;

  For i:=1 to m do
   For j:=0 to b[i]-a[i] do
   If an[n,i,j]>ans then
   begin
    ans:=an[n,i,j];
    idx:=i;
    idy:=j;
   end;

  If ans=-100000000000000000 then writeln('NO') else
  begin
   writeln('YES');
   For i:=n downto 1 do
   begin
    writeln(nn[idx],' ',cur[i,idx,idy]);
    idx1:=prevl[i,idx,idy];
    idy:=prevs[i,idx,idy];
    idx:=idx1;
   end;
  end;

end.

program task;
//{$APPTYPE DELPHI}
{$APPTYPE CONSOLE}
{$R-,S-,Q-,I-,O+}

uses
  Math,
  SysUtils;

type kat=record
    a,b:int64;
    c:longint;
end;

var n,m,k,i,jj,l,i1,i2:longint;
    j,kkl,lk,rip:int64;
    kk:array[1..100]of kat;
    least:array[1..100]of longint;
    res:array[1..100,1..50,0..100]of int64;

function less(a,b:kat):boolean;
begin
    less:=(a.c<b.c)or((a.c=b.c)and(a.b<b.b));
end;

procedure bubble;
var i,j:longint;
    cc:kat;
begin
    for i:=2to m do
        for j:=m downto i do
            if less(kk[j],kk[j-1])then begin
                cc:=kk[j];
                kk[j]:=kk[j-1];
                kk[j-1]:=cc;
            end;
end;

procedure dls;
var i, j:longint;
begin
    for i:= 1 to m do begin
        j:=0;
        while (kk[j+1].c<kk[i].c) do inc(j);
        least[i]:=j;
    end;
end;

procedure init;
var i,j,k:longint;
begin
    for j:= 1 to 50 do
        for i:=1 to 100 do
            for k := 0 to 100 do
                res[i,j,k]:=0;
end;

procedure obr(x, y : longint;z:int64);
var j, kkl,lk : int64;
    ok:boolean;
begin
    ok:=false;
    if (x = 1) then
        writeln(y,' ',z+kk[y].a)
    else begin
        j := z;
        kkl := kk[y].a+j;
        if kkl mod int64(k) = 0 then begin
            lk := kkl div int64(k);
            l:=1;
            while (not ok) and (l<=least[y]) do begin
                if (lk>=kk[l].a)and(lk<=kk[l].b)and(res[l,x-1,lk-kk[l].a]<>0)and
                (res[y,x,z]=res[l,x-1,lk-kk[l].a]+kkl)then begin
                    ok:=true;
                    obr(x-1,l,lk-kk[l].a);
                    writeln(y,' ',z+kk[y].a);
                end;
                l:=l+1;
            end;
        end;
            lk:=kkl-int64(k);
            l:=1;
            while (not ok) and (l<=least[y]) do begin
                if (lk>=kk[l].a)and(lk<=kk[l].b)and(res[l,x-1,lk-kk[l].a]<>0)and
                (res[y,x,z]=res[l,x-1,lk-kk[l].a]+kkl)then begin
                    ok:=true;
                    obr(x-1,l,lk-kk[l].a);
                    writeln(y,' ',z+kk[y].a);
                end;
                l:=l+1;
            end;
        end;
end;

begin
    assign(input,'input.txt');
    assign(output, 'output.txt');
    init;
    readln(n,m,k);
    for i:=1to m do
        readln(kk[i].a,kk[i].b,kk[i].c);
    bubble;
    for i:=1 to m do begin
        kkl:=kk[i].a;
        j:=0;
        //jj:=1;
        while kkl<=kk[i].b do begin
            res[i,1,j]:=kkl;
            //writeln('res :', jj, ':',i, ':', j, ' is ', res[i,jj,j]);
            j:=j+1;
            kkl:=kkl+1;
        end;
    end;
    dls;
    for jj:=2 to n do begin
        for i:=1to m do begin
            kkl:=kk[i].a;
            j:=0;
            while (kkl<=kk[i].b)do begin
                kkl:=j+kk[i].a;
                if kkl mod int64(k) = 0 then begin
                    lk:=kkl div int64(k);
                    for l:=1 to least[i] do
                    if (lk>=kk[l].a)and(lk<=kk[l].b)and(res[l,jj-1,lk-kk[l].a]<>0)then
                        res[i,jj,j]:=max(res[i,jj,j], res[l,jj-1,lk-kk[l].a]+kkl);
                end;
                lk:=kkl-int64(k);
                for l:=1 to least[i] do
                if (lk >= kk[l].a)and(lk<=kk[l].b)and(res[l,jj-1,lk-kk[l].a]<>0)then
                    res[i,jj,j]:=max(res[i,jj,j], res[l,jj-1,lk-kk[l].a]+kkl);
                kkl:=kkl+1;
                //writeln('res :', jj, ':',i, ':', j, ' is ', res[jj,i,j]);
                j:=j+1;
            end;
        end;
    end;
    rip:=0;
    i1:=0;
    i2:=0;
    for i:=1to m do begin
        kkl:=kk[i].a;
        j:=0;
        while(kkl<=kk[i].b)do begin
            rip:=max(rip,res[i,n,j]);
            if rip=res[i,n,j] then begin
                i1:=i;
                i2:=j;
            end;
            j:=j+1;
            kkl:=kkl+1;
        end
    end;
    if rip = 0 then writeln('NO') else begin
        writeln('YES');
        //writeln(rip,' ',i1, ' ',i2);
        obr(n, i1, i2);
    end;
    halt(0)
end.

program task;
//{$APPTYPE DELPHI}
{$APPTYPE CONSOLE}
{$R-,S-,Q-,I-,O+}

uses
  Math,
  SysUtils;

type kat=record
    a,b:int64;
    c:longint;
end;

var n,m,k,i,jj,l,i1,i2:longint;
    j,kkl,lk,rip:int64;
    kk:array[1..100]of kat;
    least:array[1..100]of longint;
    res:array[1..100,1..50,0..100]of int64;

function less(a,b:kat):boolean;
begin
    less:=(a.c<b.c)or((a.c=b.c)and(a.b<b.b));
end;

procedure bubble;
var i,j:longint;
    cc:kat;
begin
    for i:=2to m do
        for j:=m downto i do
            if less(kk[j],kk[j-1])then begin
                cc:=kk[j];
                kk[j]:=kk[j-1];
                kk[j-1]:=cc;
            end;
end;

procedure dls;
var i, j:longint;
begin
    for i:= 1 to m do begin
        j:=0;
        while (kk[j+1].c<kk[i].c) do inc(j);
        least[i]:=j;
    end;
end;

procedure init;
var i,j,k:longint;
begin
    for j:= 1 to 50 do
        for i:=1 to 100 do
            for k := 0 to 100 do
                res[i,j,k]:=0;
end;

procedure obr(x, y : longint;z:int64);
var j, kkl,lk : int64;
    ok:boolean;
begin
    ok:=false;
    if (x = 1) then
        writeln(y,' ',z+kk[y].a)
    else begin
        j := z;
        kkl := kk[y].a+j;
        if kkl mod int64(k) = 0 then begin
            lk := kkl div int64(k);
            l:=1;
            while (not ok) and (l<=least[y]) do begin
                if (lk>=kk[l].a)and(lk<=kk[l].b)and(res[l,x-1,lk-kk[l].a]<>0)and
                (res[y,x,z]=res[l,x-1,lk-kk[l].a]+kkl)then begin
                    ok:=true;
                    obr(x-1,l,lk-kk[l].a);
                    writeln(y,' ',z+kk[y].a);
                end;
                l:=l+1;
            end;
        end;
            lk:=kkl-int64(k);
            l:=1;
            while (not ok) and (l<=least[y]) do begin
                if (lk>=kk[l].a)and(lk<=kk[l].b)and(res[l,x-1,lk-kk[l].a]<>0)and
                (res[y,x,z]=res[l,x-1,lk-kk[l].a]+kkl)then begin
                    ok:=true;
                    obr(x-1,l,lk-kk[l].a);
                    writeln(y,' ',z+kk[y].a);
                end;
                l:=l+1;
            end;
        end;
end;

begin
    //assign(input,'input.txt');
    //assign(output, 'output.txt');
    init;
    readln(n,m,k);
    for i:=1to m do
        readln(kk[i].a,kk[i].b,kk[i].c);
    bubble;
    for i:=1 to m do begin
        kkl:=kk[i].a;
        j:=0;
        //jj:=1;
        while kkl<=kk[i].b do begin
            res[i,1,j]:=kkl;
            //writeln('res :', jj, ':',i, ':', j, ' is ', res[i,jj,j]);
            j:=j+1;
            kkl:=kkl+1;
        end;
    end;
    dls;
    for jj:=2 to n do begin
        for i:=1to m do begin
            kkl:=kk[i].a;
            j:=0;
            while (kkl<=kk[i].b)do begin
                kkl:=j+kk[i].a;
                if kkl mod int64(k) = 0 then begin
                    lk:=kkl div int64(k);
                    for l:=1 to least[i] do
                    if (lk>=kk[l].a)and(lk<=kk[l].b)and(res[l,jj-1,lk-kk[l].a]<>0)then
                        res[i,jj,j]:=max(res[i,jj,j], res[l,jj-1,lk-kk[l].a]+kkl);
                end;
                lk:=kkl-int64(k);
                for l:=1 to least[i] do
                if (lk >= kk[l].a)and(lk<=kk[l].b)and(res[l,jj-1,lk-kk[l].a]<>0)then
                    res[i,jj,j]:=max(res[i,jj,j], res[l,jj-1,lk-kk[l].a]+kkl);
                kkl:=kkl+1;
                //writeln('res :', jj, ':',i, ':', j, ' is ', res[jj,i,j]);
                j:=j+1;
            end;
        end;
    end;
    rip:=0;
    i1:=0;
    i2:=0;
    for i:=1to m do begin
        kkl:=kk[i].a;
        j:=0;
        while(kkl<=kk[i].b)do begin
            rip:=max(rip,res[i,n,j]);
            if rip=res[i,n,j] then begin
                i1:=i;
                i2:=j;
            end;
            j:=j+1;
            kkl:=kkl+1;
        end
    end;
    if rip = 0 then writeln('NO') else begin
        writeln('YES');
        //writeln(rip,' ',i1, ' ',i2);
        obr(n, i1, i2);
    end;
    halt(0)
end.

program Project1;

{$APPTYPE CONSOLE}
{$O+} {$R+}

uses
  SysUtils,
  Math;

const
  CLen = 101;

var
  n, m, k: Int64;
  a, b, c: array [1..clen] of Int64;
  mx, d, p: array [1..clen, 1..clen, 0..clen] of Int64;

procedure write_res(x, y, z: Int64);
begin
  if d[x, y, z] = 1 then
    write_res(x-1, p[x, y, z], z + a[y] - k - a[p[x, y, z]])
  else if d[x, y, z] = 2 then
    write_res(x-1, p[x, y, z], ((z + a[y]) div k) - a[p[x, y, z]]);
  Writeln(y, ' ', a[y] + z);
end;

var
  i, j, x, y: Integer;
  val, mxx: Int64;

begin
  //Reset(input, 'input.txt'); rewrite(output, 'output.txt');
  read(n, m, k);
  for i := 1 to m do
    read(a[i], b[i], c[i]);
  for i := 1 to clen do
    for j := 1 to clen do
      for x := 0 to clen do
        begin
          d[i, j, x] := -1;
          p[i, j, x] := -1;
          mx[i, j, x] := -1;
        end;
  for i := 1 to m do
    for j := 0 to b[i] - a[i] do
      begin
        d[1, i, j] := 0;
        p[1, i, j] := 0;
        mx[1, i, j] := j + a[i];
      end;
  for i := 2 to n do
    for j := 1 to m do
      for x := 0 to clen do
        for y := 1 to m do
          if (d[i-1, j, x] <> -1) and (c[y] > c[j]) then
            begin
              val := x + a[j] + k - a[y];
              if (a[y] <= x + a[j] + k) and (x + a[j] + k <= b[y]) and (mx[i, j, x] + x + a[j] + k > mx[i, y, val]) then
                begin
                  d[i, y, val] := 1;
                  p[i, y, val] := j;
                  mx[i, y, val] := mx[i, j, x] + x + a[j] + k;
                end;
              val := (x + a[j]) * k - a[y];
              if (a[y] <= (x + a[j]) * k) and ((x + a[j]) * k <= b[y]) and (mx[i, j, x] + (x + a[j]) * k > mx[i, y, val]) then
                begin
                  d[i, y, val] := 2;
                  p[i, y, val] := j;
                  mx[i, y, val] := mx[i, j, x] + (x + a[j]) * k;
                end;
            end;
  mxx := -1;
  for j := 1 to m do
    for x := 0 to clen do
      mxx := max(mxx, mx[n, j, x]);
  for j := 1 to m do
    for x := 0 to clen do
      if (mxx <> -1) and (mx[n, j, x] = mxx) then
        begin
          Writeln('YES');
          write_res(n, j, x);
          halt;
        end;
  Writeln('NO');
end.

program Project1;

{$APPTYPE CONSOLE}
{$O-} {$R+} {$Q+}

uses
  SysUtils,
  Math;

const
  CLen = 101;

var
  n, m, k: Int64;
  a, b, c: array [1..clen] of Int64;
  mx, d, p: array [1..clen, 1..clen, 0..clen] of Int64;

procedure write_res(x, y, z: Int64);
begin
  if d[x, y, z] = 1 then
    write_res(x-1, p[x, y, z], z + a[y] - k - a[p[x, y, z]])
  else if d[x, y, z] = 2 then
    write_res(x-1, p[x, y, z], ((z + a[y]) div k) - a[p[x, y, z]]);
  Writeln(y, ' ', a[y] + z);
end;

var
  i, j, x, y: Integer;
  val, mxx: Int64;

begin
  //Reset(input, 'input.txt'); rewrite(output, 'output.txt');
  read(n, m, k);
  for i := 1 to m do
    read(a[i], b[i], c[i]);
  for i := 1 to clen do
    for j := 1 to clen do
      for x := 0 to clen do
        begin
          d[i, j, x] := -1;
          p[i, j, x] := -1;
          mx[i, j, x] := -1;
        end;
  for i := 1 to m do
    for j := 0 to b[i] - a[i] do
      begin
        d[1, i, j] := 0;
        p[1, i, j] := 0;
        mx[1, i, j] := j + a[i];
      end;
  for i := 2 to n do
    for j := 1 to m do
      for x := 0 to clen do
        for y := 1 to m do
          if (d[i-1, j, x] <> -1) and (c[y] > c[j]) then
            begin
              val := x + a[j] + k - a[y];
              if (a[y] <= x + a[j] + k) and (x + a[j] + k <= b[y]) and (mx[i, j, x] + x + a[j] + k > mx[i, y, val]) then
                begin
                  d[i, y, val] := 1;
                  p[i, y, val] := j;
                  mx[i, y, val] := mx[i, j, x] + x + a[j] + k;
                end;
              val := (x + a[j]) * k - a[y];
              if (a[y] <= (x + a[j]) * k) and ((x + a[j]) * k <= b[y]) and (mx[i, j, x] + (x + a[j]) * k > mx[i, y, val]) then
                begin
                  d[i, y, val] := 2;
                  p[i, y, val] := j;
                  mx[i, y, val] := mx[i, j, x] + (x + a[j]) * k;
                end;
            end;
  mxx := -1;
  for j := 1 to m do
    for x := 0 to clen do
      mxx := max(mxx, mx[n, j, x]);
  for j := 1 to m do
    for x := 0 to clen do
      if (mxx <> -1) and (mx[n, j, x] = mxx) then
        begin
          Writeln('YES');
          write_res(n, j, x);
          halt;
        end;
  Writeln('NO');
end.

program Project1;

{$APPTYPE CONSOLE}
{$O+}

uses
  SysUtils;

const
  CLen = 100;

var
  n, m, k: Int64;
  a, b, c: array [1..clen] of Int64;
  d, p: array [1..clen, 1..clen, 1..clen] of Int64;

procedure write_res(x, y, z: Int64);
begin
  if d[x, y, z] = 1 then
    write_res(x-1, p[x, y, z], z + a[y] - k - a[p[x, y, z]])
  else if d[x, y, z] = 2 then
    write_res(x-1, p[x, y, z], ((z + a[y]) div k) - a[p[x, y, z]]);
  Writeln(y, ' ', a[y] + z);
end;

var
  i, j, x, y: Integer;
  val: Int64;

begin
  //Reset(input, 'input.txt'); rewrite(output, 'output.txt');
  Readln(n, m, k);
  for i := 1 to m do
    readln(a[i], b[i], c[i]);
  for i := 1 to clen do
    for j := 1 to clen do
      for x := 1 to clen do
        begin
          d[i, j, x] := -1;
          p[i, j, x] := -1;
        end;
  for i := 1 to m do
    for j := 0 to b[i] - a[i] do
      begin
        d[1, i, j] := 0;
        p[1, i, j] := 0;
      end;
  for i := 2 to n do
    for j := 1 to m do
      for x := 0 to clen do
        for y := 1 to m do
          if (d[i-1, j, x] <> -1) and (c[y] > c[j]) then
            begin
              if (a[y] <= x + a[j] + k) and (x + a[j] + k <= b[y]) then
                begin
                  val := x + a[j] + k - a[y];
                  d[i, y, val] := 1;
                  p[i, y, val] := j;
                end;
              if (a[y] <= (x + a[j]) * k) and ((x + a[j]) * k <= b[y]) then
                begin
                  val := (x + a[j]) * k - a[y];
                  d[i, y, val] := 2;
                  p[i, y, val] := j;
                end;
            end;
  for j := 1 to m do
    for x := 0 to clen do
      if d[n, j, x] <> -1 then
        begin
          Writeln('YES');
          write_res(n, j, x);
          halt;
        end;
  Writeln('NO');
end.

program Project1;

{$APPTYPE CONSOLE}
{$O+}

uses
  SysUtils, Math;

const
  CLen = 101;

var
  n, m, k: Int64;
  a, b, c: array [1..clen] of Int64;
  mx, d, p: array [1..clen, 1..clen, 1..clen] of Int64;

procedure write_res(x, y, z: Int64);
begin
  if d[x, y, z] = 1 then
    write_res(x-1, p[x, y, z], z + a[y] - k - a[p[x, y, z]])
  else if d[x, y, z] = 2 then
    write_res(x-1, p[x, y, z], ((z + a[y]) div k) - a[p[x, y, z]]);
  Writeln(y, ' ', a[y] + z);
end;

var
  i, j, x, y: Integer;
  val, mxx: Int64;

begin
  //Reset(input, 'input.txt'); rewrite(output, 'output.txt');
  read(n, m, k);
  for i := 1 to m do
    read(a[i], b[i], c[i]);
  for i := 1 to clen do
    for j := 1 to clen do
      for x := 1 to clen do
        begin
          d[i, j, x] := -1;
          p[i, j, x] := -1;
          mx[i, j, x] := -1;
        end;
  for i := 1 to m do
    for j := 0 to b[i] - a[i] do
      begin
        d[1, i, j] := 0;
        p[1, i, j] := 0;
        mx[1, i, j] := j + a[i];
      end;
  for i := 2 to n do
    for j := 1 to m do
      for x := 0 to clen do
        for y := 1 to m do
          if (d[i-1, j, x] <> -1) and (c[y] > c[j]) then
            begin
              val := x + a[j] + k - a[y];
              if (a[y] <= x + a[j] + k) and (x + a[j] + k <= b[y]) and (mx[i, j, x] + x + a[j] + k > mx[i, y, val]) then
                begin
                  d[i, y, val] := 1;
                  p[i, y, val] := j;
                  mx[i, y, val] := mx[i, j, x] + x + a[j] + k;
                end;
              val := (x + a[j]) * k - a[y];
              if (a[y] <= (x + a[j]) * k) and ((x + a[j]) * k <= b[y]) and (mx[i, j, x] + (x + a[j]) * k > mx[i, y, val]) then
                begin
                  d[i, y, val] := 2;
                  p[i, y, val] := j;
                  mx[i, y, val] := mx[i, j, x] + (x + a[j]) * k;
                end;
            end;
  mxx := -1;
  for j := 1 to m do
    for x := 0 to clen do
      mxx := max(mxx, mx[n, j, x]);
  for j := 1 to m do
    for x := 0 to clen do
      if (mxx <> -1) and (mx[n, j, x] = mxx) then
        begin
          Writeln('YES');
          write_res(n, j, x);
          halt;
        end;
  Writeln('NO');
end.

program Project1;

{$APPTYPE CONSOLE}
{$O-}

uses
  SysUtils, Math;

const
  CLen = 100;

var
  n, m, k: Int64;
  a, b, c: array [1..clen] of Int64;
  mx, d, p: array [1..clen, 1..clen, 1..clen] of Int64;

procedure write_res(x, y, z: Int64);
begin
  if d[x, y, z] = 1 then
    write_res(x-1, p[x, y, z], z + a[y] - k - a[p[x, y, z]])
  else if d[x, y, z] = 2 then
    write_res(x-1, p[x, y, z], ((z + a[y]) div k) - a[p[x, y, z]]);
  Writeln(y, ' ', a[y] + z);
end;

var
  i, j, x, y: Integer;
  val, mxx: Int64;

begin
  //Reset(input, 'input.txt'); rewrite(output, 'output.txt');
  Readln(n, m, k);
  for i := 1 to m do
    readln(a[i], b[i], c[i]);
  for i := 1 to clen do
    for j := 1 to clen do
      for x := 1 to clen do
        begin
          d[i, j, x] := -1;
          p[i, j, x] := -1;
          mx[i, j, x] := -1;
        end;
  for i := 1 to m do
    for j := 0 to b[i] - a[i] do
      begin
        d[1, i, j] := 0;
        p[1, i, j] := 0;
        mx[1, i, j] := j + a[i];
      end;
  for i := 2 to n do
    for j := 1 to m do
      for x := 0 to clen do
        for y := 1 to m do
          if (d[i-1, j, x] <> -1) and (c[y] > c[j]) then
            begin
              val := x + a[j] + k - a[y];
              if (a[y] <= x + a[j] + k) and (x + a[j] + k <= b[y]) and (mx[i, j, x] + x + a[j] + k > mx[i, y, val]) then
                begin
                  d[i, y, val] := 1;
                  p[i, y, val] := j;
                  mx[i, y, val] := mx[i, j, x] + x + a[j] + k;
                end;
              val := (x + a[j]) * k - a[y];
              if (a[y] <= (x + a[j]) * k) and ((x + a[j]) * k <= b[y]) and (mx[i, j, x] + (x + a[j]) * k > mx[i, y, val]) then
                begin
                  d[i, y, val] := 2;
                  p[i, y, val] := j;
                  mx[i, y, val] := mx[i, j, x] + (x + a[j]) * k;
                end;
            end;
  mxx := -1;
  for j := 1 to m do
    for x := 0 to clen do
      mxx := max(mxx, mx[n, j, x]);
  for j := 1 to m do
    for x := 0 to clen do
      if (mxx <> -1) and (mx[n, j, x] = mxx) then
        begin
          Writeln('YES');
          write_res(n, j, x);
          halt;
        end;
  Writeln('NO');
end.

const maxn = 5100;
const maxm = 51;

var
  f:array[0..maxm, 0..maxm, 0..maxn] of int64;
  pi,pj,pk:array[0..maxm, 0..maxm, 0..maxn] of word;
  ax,ay,a,b,c,w:array[0..maxn] of int64;
  edn,beg:array[0..maxn] of longint;
  qk,nrj,nrk,ri,rj,rk,q,k,wn,n,m,i,j:longint;
  max,me,kk:int64;

procedure qs(const l, r:longint);
var
  i,j:longint;
  x,y:int64;
begin
  i:=l; j:=r; x:=w[(l+r) shr 1];
  repeat
    while x>w[i] do inc(i);
    while x<w[j] do dec(j);
    if j<i then break;
    y:=w[i]; w[i]:=w[j]; w[j]:=y;
    inc(i); dec(j);
  until j<i;
  if l<j then qs(l, j);
  if i<r then qs(i, r);
end;

procedure upd(const ni, nj, qk:longint; nk, up:int64);
begin
  while w[qk] <> nk do ;
  if f[ni, nj, qk]<up then
  begin
    f[ni, nj, qk]:=up;
    pi[ni, nj, qk]:=i;
    pj[ni, nj, qk]:=j;
    pk[ni, nj, qk]:=k;
  end;
end;

begin
{  assign(input, 'in.txt');
  reset(input);
  assign(output, 'out.txt');
  rewrite(output);}
  read(n, m, kk);
  for i:=1 to m do read(a[i], b[i], c[i]);
  for i:=1 to m do
  begin
    beg[i]:=wn+1;
    j:=a[i];
    while j<=b[i] do
    begin
      inc(wn);
      w[wn]:=j;
      inc(j);
    end;
    edn[j]:=wn;
  end;
  for i:=1 to m do
  begin
    j:=a[i];
    while j<=b[i] do
    begin
      f[1, i, beg[i]+j-a[i]]:=j;
      inc(j);
    end;
  end;
  for i:=1 to n-1 do for j:=1 to m do for k:=beg[j] to edn[j] do if f[i, j, k]>0 then
  begin
    me:=w[k];
    for q:=1 to m do if c[q]>c[j] then
    begin
      if (a[q]<=me+kk)and(me+kk<=b[q]) then upd(i+1, q, beg[q]+me+kk-a[q], me+kk, f[i, j, k]+me+kk);
      if (a[q]<=me*kk)and(me*kk<=b[q]) then upd(i+1, q, beg[q]+me*kk-a[q], me*kk, f[i, j, k]+me*kk);
    end;
  end;
  rk:=-1;
  max:=-1;
  for j:=1 to m do for k:=1 to wn do if (f[i, j, k]>0)and(f[n, j, k] > max) then
  begin
    max:=f[n, j, k];
    ri:=n;
    rj:=j;
    rk:=k;
  end;
  if rk = -1 then
  begin
    writeln('NO');
    close(output);
    halt;
  end;
  writeln('YES');
  for i:=n downto 1 do
  begin
    ax[i]:=rj;
    ay[i]:=w[rk];
    nrj:=pj[i, rj, rk];
    nrk:=pk[i, rj, rk];
    rj:=nrj; rk:=nrk;
  end;
  for i:=1 to n do writeln(ax[i], ' ', ay[i]);
  close(output);
end.

const maxn = 5100;
const maxm = 51;

var
  f:array[0..maxm, 0..maxm, 0..maxn] of int64;
  pi,pj,pk:array[0..maxm, 0..maxm, 0..maxn] of word;
  ax,ay,a,b,c,w:array[0..maxn] of int64;
  beg:array[0..maxn] of longint;
  qk,nrj,nrk,ri,rj,rk,q,k,wn,n,m,i,j:longint;
  max,me,kk:int64;

procedure qs(const l, r:longint);
var
  i,j:longint;
  x,y:int64;
begin
  i:=l; j:=r; x:=w[(l+r) shr 1];
  repeat
    while x>w[i] do inc(i);
    while x<w[j] do dec(j);
    if j<i then break;
    y:=w[i]; w[i]:=w[j]; w[j]:=y;
    inc(i); dec(j);
  until j<i;
  if l<j then qs(l, j);
  if i<r then qs(i, r);
end;

function idx(const x:int64):longint;
var
  l,r,mid:longint;
begin
  l:=1; r:=wn;
  while l<r do
  begin
    mid:=(l+r) shr 1;
    if (w[mid]>=x) then r:=mid else l:=mid+1;
  end;
  idx:=l;
end;

procedure upd(const ni, nj, qk:longint; nk, up:int64);
begin
  if f[ni, nj, qk]<up then
  begin
    f[ni, nj, qk]:=up;
    pi[ni, nj, qk]:=i;
    pj[ni, nj, qk]:=j;
    pk[ni, nj, qk]:=k;
  end;
end;

begin
{  assign(input, 'in.txt');
  reset(input);
  assign(output, 'out.txt');
  rewrite(output);}
  read(n, m, kk);
  for i:=1 to m do read(a[i], b[i], c[i]);
  for i:=1 to m do
  begin
    beg[i]:=wn+1;
    j:=a[i];
    while j<=b[i] do
    begin
      inc(wn);
      w[wn]:=j;
      inc(j);
    end;
  end;
  for i:=1 to m do
  begin
    j:=a[i];
    while j<=b[i] do
    begin
      f[1, i, idx(j)]:=j;
      inc(j);
    end;
  end;
  for i:=1 to n-1 do for j:=1 to m do for k:=1 to wn do if f[i, j, k]>0 then
  begin
    me:=w[k];
    for q:=1 to m do if c[q]>c[j] then
    begin
      if (a[q]<=me+kk)and(me+kk<=b[q]) then upd(i+1, q, beg[q]+me+kk-a[q], me+kk, f[i, j, k]+me+kk);
      if (a[q]<=me*kk)and(me*kk<=b[q]) then upd(i+1, q, beg[q]+me*kk-a[q], me*kk, f[i, j, k]+me*kk);
    end;
  end;
  rk:=-1;
  max:=-1;
  for j:=1 to m do for k:=1 to wn do if (f[i, j, k]>0)and(f[n, j, k] > max) then
  begin
    max:=f[n, j, k];
    ri:=n;
    rj:=j;
    rk:=k;
  end;
  if rk = -1 then
  begin
    writeln('NO');
    close(output);
    halt;
  end;
  writeln('YES');
  for i:=n downto 1 do
  begin
    ax[i]:=rj;
    ay[i]:=w[rk];
    nrj:=pj[i, rj, rk];
    nrk:=pk[i, rj, rk];
    rj:=nrj; rk:=nrk;
  end;
  for i:=1 to n do writeln(ax[i], ' ', ay[i]);
  close(output);
end.

{$r+,i+,q+,s+,o-}
const maxn = 5100;
const maxm = 51;

var
  f:array[0..maxm, 0..maxm, 0..maxn] of int64;
  pi,pj,pk:array[0..maxm, 0..maxm, 0..maxn] of word;
  ax,ay,a,b,c,w:array[0..maxn] of int64;
  edn,beg:array[0..maxn] of longint;
  qk,nrj,nrk,ri,rj,rk,q,k,wn,n,m,i,j:longint;
  jj,max,me,kk:int64;

procedure qs(const l, r:longint);
var
  i,j:longint;
  x,y:int64;
begin
  i:=l; j:=r; x:=w[(l+r) shr 1];
  repeat
    while x>w[i] do inc(i);
    while x<w[j] do dec(j);
    if j<i then break;
    y:=w[i]; w[i]:=w[j]; w[j]:=y;
    inc(i); dec(j);
  until j<i;
  if l<j then qs(l, j);
  if i<r then qs(i, r);
end;

procedure upd(const ni, nj:longint; qk, nk, up:int64);
begin
  while w[qk] <> nk do ;
  if f[ni, nj, qk]<up then
  begin
    f[ni, nj, qk]:=up;
    pi[ni, nj, qk]:=i;
    pj[ni, nj, qk]:=j;
    pk[ni, nj, qk]:=k;
  end;
end;

begin
{  assign(input, 'in.txt');
  reset(input);
  assign(output, 'out.txt');
  rewrite(output);}
  wn:=0;
  read(n, m, kk);
  for i:=1 to m do read(a[i], b[i], c[i]);
  for i:=1 to m do
  begin
    beg[i]:=wn+1;
    jj:=a[i];
    while jj<=b[i] do
    begin
      inc(wn);
      w[wn]:=jj;
      inc(jj);
    end;
    edn[i]:=wn;
  end;
  for i:=1 to m do
  begin
    jj:=a[i];
    while jj<=b[i] do
    begin
      f[1, i, beg[i]+jj-a[i]]:=jj;
      inc(jj);
    end;
  end;
  for i:=1 to n-1 do for j:=1 to m do for k:=beg[j] to edn[j] do if f[i, j, k]>0 then
  begin
    me:=w[k];
    for q:=1 to m do if c[q]>c[j] then
    begin
      if (a[q]<=me+kk)and(me+kk<=b[q]) then upd(i+1, q, int64(beg[q])+me+kk-a[q], me+kk, f[i, j, k]+me+kk);
      if (a[q]<=me*kk)and(me*kk<=b[q]) then upd(i+1, q, int64(beg[q])+me*kk-a[q], me*kk, f[i, j, k]+me*kk);
    end;
  end;
  rk:=-1;
  max:=-1;
  for j:=1 to m do for k:=1 to wn do if (f[i, j, k]>0)and(f[n, j, k] > max) then
  begin
    max:=f[n, j, k];
    ri:=n;
    rj:=j;
    rk:=k;
  end;
  if rk = -1 then
  begin
    writeln('NO');
    close(output);
    halt;
  end;
  writeln('YES');
  for i:=n downto 1 do
  begin
    ax[i]:=rj;
    ay[i]:=w[rk];
    nrj:=pj[i, rj, rk];
    nrk:=pk[i, rj, rk];
    rj:=nrj; rk:=nrk;
  end;
  for i:=1 to n do writeln(ax[i], ' ', ay[i]);
  close(output);
end.

Program C_REFORM;
Const MaxN=50;
      MaxV=100;
Type subject=Record a,b:qword; stt,c,v:Longint End;
Var l:array[1..MaxN,1..MaxN,0..MaxV] of qword;
    trace:array[1..MaxN,1..MaxN,0..MaxV] of Record a,b:Longint End;
    a:array[1..MaxN] of subject;
    n,m,k:Longint;
Procedure Input;
Var f:Text;
    i:Longint;
Begin
  Assign(f,''); Reset(f);
  Readln(f,n,m,k);
  For i:=1 to m do
    Begin
      Readln(f,a[i].a,a[i].b,a[i].c);
      a[i].v:=a[i].b-a[i].a;
      a[i].stt:=i;
    End;
  Close(f);
End;

Procedure Swap(var a,b:subject);
Var t:subject; Begin t:=a; a:=b; b:=t End;

Procedure QuickSort(l,r:Longint);
Var i,j,k:Longint;
Begin
  If l>=r then exit;
  i:=l; j:=r;
  k:=a[random(r-l+1)+l].c;
  Repeat
    While a[i].c<k do Inc(i);
    While a[j].c>k do Dec(j);
    If i<=j then
      Begin
        If i<j then Swap(a[i],a[j]);
        Inc(i); Dec(j);
      End;
  Until i>j;
  QuickSOrt(l,j); QUickSort(i,r)
End;

Procedure Optimize;
Var i,j,z,x,h:Longint;
    t:qword;
Begin
  For j:=1 to m-n+1 do
    For z:=0 to a[1].v do
      l[1,j,z]:=z+a[1].a;

  For i:=1 to n-1 do
    For j:=i to m-(n-i) do
      For z:=0 to a[j].v do
        If l[i,j,z]<>0 then
          For h:=1 to 2 do
            Begin
            If h=1 then t:=(z+a[j].a)*k else t:=(z+a[j].a)+k;
            For x:=j+1 to m-(n-i)+1 do
              If (a[x].c>a[j].c) and (a[x].a<=t) and (t<=a[x].b)
                and (l[i+1,x,t-a[x].a]<l[i,j,z]+t) then
                  Begin
                    l[i+1,x,t-a[x].a]:=l[i,j,z]+t;
                    With trace[i+1,x,t-a[x].a] do
                      Begin
                        a:=j; b:=z
                      End;
                  End;
            End;
End;

Procedure Output;
Var ds:array[1..MaxN] of Record stt:Longint; v:QWord End;
    max,i,j,z,x,y:Longint;
Begin
  max:=0;
  For j:=n to m do
    For z:=0 to a[j].v do
      If max<l[n,j,z] then
        Begin
          max:=l[n,j,z];
          x:=j; y:=z;
        End;
  If max=0 then
    Begin
      Writeln('NO');
      Exit;
    End;
  i:=n;
  While i<>0 do
    Begin
      ds[i].stt:=a[x].stt; ds[i].v:=a[x].a+y;
      j:=trace[i,x,y].a; z:=trace[i,x,y].b;
      x:=j; y:=z;
      Dec(i);
    End;
  Writeln('YES');
  For i:=1 to n do Writeln(ds[i].stt,' ',ds[i].v);
End;

BEGIN
  Input;
  QuickSort(1,m);
  Optimize;
  Output;
END.

Program C_REFORM;
Const MaxN=50;
      MaxV=100;
Type subject=Record a,b:qword; stt,c,v:Longint End;
Var l:array[1..MaxN,1..MaxN,0..MaxV] of qword;
    trace:array[1..MaxN,1..MaxN,0..MaxV] of Record a,b:Longint End;
    a:array[1..MaxN] of subject;
    n,m,k:Longint;
Procedure Input;
Var f:Text;
    i:Longint;
Begin
  Assign(f,''); Reset(f);
  Readln(f,n,m,k);
  For i:=1 to m do
    Begin
      Readln(f,a[i].a,a[i].b,a[i].c);
      a[i].v:=a[i].b-a[i].a;
      a[i].stt:=i;
    End;
  Close(f);
End;

Procedure Swap(var a,b:subject);
Var t:subject; Begin t:=a; a:=b; b:=t End;

Procedure QuickSort(l,r:Longint);
Var i,j,k:Longint;
Begin
  If l>=r then exit;
  i:=l; j:=r;
  k:=a[random(r-l+1)+l].c;
  Repeat
    While a[i].c<k do Inc(i);
    While a[j].c>k do Dec(j);
    If i<=j then
      Begin
        If i<j then Swap(a[i],a[j]);
        Inc(i); Dec(j);
      End;
  Until i>j;
  QuickSOrt(l,j); QUickSort(i,r)
End;

Procedure Optimize;
Var i,j,z,x,h:Longint;
    t:qword;
Begin
  For j:=1 to m-n+1 do
    For z:=0 to a[1].v do
      l[1,j,z]:=z+a[1].a;

  For i:=1 to n-1 do
    For j:=i to m-(n-i) do
      For z:=0 to a[j].v do
        If l[i,j,z]<>0 then
          For h:=1 to 2 do
            Begin
            If h=1 then t:=(z+a[j].a)*k else t:=(z+a[j].a)+k;
            For x:=j+1 to m-(n-i)+1 do
              If (a[x].c>a[j].c) and (a[x].a<=t) and (t<=a[x].b)
                and (l[i+1,x,t-a[x].a]<l[i,j,z]+t) then
                  Begin
                    l[i+1,x,t-a[x].a]:=l[i,j,z]+t;
                    With trace[i+1,x,t-a[x].a] do
                      Begin
                        a:=j; b:=z
                      End;
                  End;
            End;
End;

Procedure Output;
Var ds:array[1..MaxN] of Record stt:Longint; v:QWord End;
    i,j,z,x,y:Longint;
    max:qword;
Begin
  max:=0;
  For j:=n to m do
    For z:=0 to a[j].v do
      If max<l[n,j,z] then
        Begin
          max:=l[n,j,z];
          x:=j; y:=z;
        End;
  If max=0 then
    Begin
      Writeln('NO');
      Exit;
    End;
  i:=n;
  While i<>0 do
    Begin
      ds[i].stt:=a[x].stt; ds[i].v:=a[x].a+y;
      j:=trace[i,x,y].a; z:=trace[i,x,y].b;
      x:=j; y:=z;
      Dec(i);
    End;
  Writeln('YES');
  For i:=1 to n do Writeln(ds[i].stt,' ',ds[i].v);
End;

BEGIN
  Input;
  QuickSort(1,m);
  Optimize;
  Output;
END.

Program Education_Reform;
var i,j,z,x,n,m,nx,ny:longint;
    tmp,k:int64;
    s,t,a,id,ans,ans2:array[1..100]of int64;
    pre,pre2:array[1..50,0..50,0..100]of longint;
    f:array[1..50,0..50,0..100]of boolean;
//===============================================================
procedure qsort(left,right:longint);
var l,r,mid,k:longint;
begin
  l:=left;r:=right;
  mid:=a[(l+r)shr 1];
  repeat
    while a[l]<mid do inc(l);
    while a[r]>mid do dec(r);
    if l<=r then
      begin
        tmp:=a[l];a[l]:=a[r];a[r]:=tmp;
        tmp:=s[l];s[l]:=s[r];s[r]:=tmp;
        tmp:=t[l];t[l]:=t[r];t[r]:=tmp;
        tmp:=id[l];id[l]:=id[r];id[r]:=tmp;
        inc(l);dec(r);
      end;
  until l>r;
  if left<r then qsort(left,r);
  if l<right then qsort(l,right);
end;
//===============================================================
procedure Print(x,y:longint);
begin
  for i:=n downto 1 do
    begin
      ans[i]:=id[x];
      ans2[i]:=s[x]+y;
      nx:=pre[i,x,y];
      ny:=pre2[i,x,y];
      x:=nx;y:=ny;
    end;
  for i:=1 to n do
    writeln(ans[i],' ',ans2[i]);
end;
//===============================================================
procedure DP;
begin
  for i:=1 to m do
    for j:=0 to t[i]-s[i] do
      f[1,i,j]:=true;
  for i:=2 to n do
    for j:=i to m do
      for z:=1 to j-1 do
        for x:=0 to t[z]-s[z] do
          if (f[i-1,z,x])and(a[j]>a[z]) then
            begin
              tmp:=s[z]+int64(x)+k;
              if (s[j]<=tmp)and(tmp<=t[j])then
                begin
                  f[i,j,tmp-s[j]]:=true;
                  pre[i,j,tmp-s[j]]:=z;
                  pre2[i,j,tmp-s[j]]:=x;
                end;
              tmp:=int64(s[z]+x)*k;
              if (s[j]<=tmp)and(tmp<=t[j])then
                begin
                  f[i,j,tmp-s[j]]:=true;
                  pre[i,j,tmp-s[j]]:=z;
                  pre2[i,j,tmp-s[j]]:=x;
                end;
            end;
  for z:=n to m do
    for x:=0 to t[z]-s[z] do
      if f[n,z,x] then
        begin
          writeln('YES');
          Print(z,x);
          halt;
        end;
  writeln('NO');
end;
//===============================================================
begin
  readln(n,m,k);
  for i:=1 to m do
    begin
      readln(s[i],t[i],a[i]);
      id[i]:=i;
    end;
  qsort(1,m);
  DP;
end.

Program Education_Reform;
var i,j,z,x,n,m,nx,ny:longint;
    tmp,k:int64;
    s,t,a,ans,ans2:array[1..100]of int64;
    pre,pre2:array[1..50,0..50,0..100]of longint;
    f:array[1..50,0..50,0..100]of boolean;
//===============================================================
procedure qsort(left,right:longint);
var l,r,mid,k:longint;
begin
  l:=left;r:=right;
  mid:=a[(l+r)shr 1];
  repeat
    while a[l]<mid do inc(l);
    while a[r]>mid do dec(r);
    if l<=r then
      begin
        tmp:=a[l];a[l]:=a[r];a[r]:=tmp;
        tmp:=s[l];s[l]:=s[r];s[r]:=tmp;
        tmp:=t[l];t[l]:=t[r];t[r]:=tmp;
        inc(l);dec(r);
      end;
  until l>r;
  if left<r then qsort(left,r);
  if l<right then qsort(l,right);
end;
//===============================================================
procedure Print(x,y:longint);
begin
  for i:=n downto 1 do
    begin
      ans[i]:=x;
      ans2[i]:=s[x]+y;
      nx:=pre[i,x,y];
      ny:=pre2[i,x,y];
      x:=nx;y:=ny;
    end;
  for i:=1 to n do
    writeln(ans[i],' ',ans2[i]);
end;
//===============================================================
procedure DP;
begin
  for i:=1 to m do
    for j:=0 to t[i]-s[i] do
      f[1,i,j]:=true;
  for i:=2 to n do
    for j:=i to m do
      for z:=1 to j-1 do
        for x:=0 to t[z]-s[z] do
          if (f[i-1,z,x])and(a[j]>a[z]) then
            begin
              tmp:=s[z]+int64(x)+k;
              if (s[j]<=tmp)and(tmp<=t[j])then
                begin
                  f[i,j,tmp-s[j]]:=true;
                  pre[i,j,tmp-s[j]]:=z;
                  pre2[i,j,tmp-s[j]]:=x;
                end;
              tmp:=int64(s[z]+x)*k;
              if (s[j]<=tmp)and(tmp<=t[j])then
                begin
                  f[i,j,tmp-s[j]]:=true;
                  pre[i,j,tmp-s[j]]:=z;
                  pre2[i,j,tmp-s[j]]:=x;
                end;
            end;
  for z:=n to m do
    for x:=0 to t[z]-s[z] do
      if f[n,z,x] then
        begin
          writeln('YES');
          Print(z,x);
          halt;
        end;
  writeln('NO');
end;
//===============================================================
begin
  readln(n,m,k);
  for i:=1 to m do readln(s[i],t[i],a[i]);
  qsort(1,m);
  DP;
end.

var m,n,key,i,j,k,s,max1,max2:longint;
    a,b:array[0..1000] of int64;
    f:array[0..100,0..100,0..200] of int64;
    p,v:array[0..100,0..100,0..200] of longint;
    c,max:int64;
    q,w:array[0..1000] of longint;

procedure swapi(var a,b:int64);
  var r:int64;
  begin
    r:=a;
    a:=b;
    b:=r;
  end;

procedure swap(var a,b:longint);
  var r:longint;
  begin
    r:=a;
    a:=b;
    b:=r;
  end;

procedure search(i,j,k:longint);
  begin
    if p[i,j,k]<>-1 then search(p[i,j,k],j-1,v[i,j,k]);
    if p[i,j,k]=-1 then
      writeln(w[i],' ',f[i,j,k])
    else
      writeln(w[i],' ',f[i,j,k]-f[p[i,j,k],j-1,v[i,j,k]]);
  end;

begin
  readln(n,m,key);
  for i:=1 to m do
    begin
      readln(a[i],b[i],q[i]);
      w[i]:=i;
    end;
  for i:=1 to m-1 do
    for j:=i+1 to m do
      if q[i]>q[j] then
        begin
          swapi(a[i],a[j]);
          swapi(b[i],b[j]);
          swap(q[i],q[j]);
          swap(w[i],w[j]);
        end;
  fillchar(f,sizeof(f),0);
  fillchar(p,sizeof(p),0);
  fillchar(v,sizeof(v),0);
  for i:=0 to b[1]-a[1] do
    begin
      f[1,1,i]:=i+a[1];
      p[1,1,i]:=-1;
      v[1,1,i]:=-1;
    end;
  for i:=2 to m do
    begin
      for j:=0 to b[i]-a[i] do
        begin
          f[i,1,j]:=j+a[i];
          p[i,1,j]:=-1;
          v[i,1,j]:=-1;
        end;
      for j:=2 to n do
        for k:=1 to i-1 do
          for s:=0 to b[k]-a[k] do
            begin
              if p[k,j-1,s]=0 then continue;
              c:=s+a[k]+key;
              if (c>=a[i]) and (c<=b[i]) then
                if f[k,j-1,s]+c>f[i,j,c-a[i]] then
                  begin
                    f[i,j,c-a[i]]:=f[k,j-1,s]+c;
                    p[i,j,c-a[i]]:=k;
                    v[i,j,c-a[i]]:=s;
                  end;
              c:=(s+a[k])*key;
              if (c>=a[i]) and (c<=b[i]) then
                if f[k,j-1,s]+c>f[i,j,c-a[i]] then
                  begin
                    f[i,j,c-a[i]]:=f[k,j-1,s]+c;
                    p[i,j,c-a[i]]:=k;
                    v[i,j,c-a[i]]:=s;
                  end;
            end;
    end;
  max1:=0;
  max2:=0;
  max:=0;
  for i:=1 to m do
    for j:=0 to b[i]-a[i] do
      if f[i,n,j]>max then
        begin
          max:=f[i,n,j];
          max1:=i;
          max2:=j;
        end;
  if max=0 then writeln('NO') else
    begin
      writeln('YES');
      search(max1,n,max2);
    end;
end.

{$r-,q-,s-,i-,o+}
const
  inf:int64=100000000000000000;
var
  f:array[0..55,0..55,0..555]of int64;
  py,px:array[0..55,0..55,0..555]of longint;
  pxx,pyy,xx,yy,q,tmp,kr,i,j,k,n,m,tk:longint;
  max,ti,tt:int64;
  r2,a,b:array[0..500500]of int64;
  r1,nom,c:array[0..500500]of longint;

begin
  read(n,m,tk);
  for i:=1 to m do read(a[i],b[i],c[i]);
  for i:=1 to m do nom[i]:=i;
  for i:=1 to m-1 do
    for j:=1 to m-1 do
      if (c[j]>c[j+1])then begin
        ti:=a[j];a[j]:=a[j+1];a[j+1]:=ti;
        ti:=b[j];b[j]:=b[j+1];b[j+1]:=ti;
        tmp:=c[j];c[j]:=c[j+1];c[j+1]:=tmp;
        tmp:=nom[j];nom[j]:=nom[j+1];nom[j+1]:=tmp;
      end;
  fillchar(f,sizeof(f),$0);
  for j:=1 to m do
    for k:=0 to longint(b[j]-a[j])do
      f[1][j][k]:=k+a[j];
  for i:=1 to n-1 do
    for j:=1 to m do
      for k:=0 to longint(b[j]-a[j])do
        if (f[i][j][k]<>0)then begin
          tt:=a[j]+k+tk;
          for q:=j+1 to m do
            if (tt>=a[q])and(tt<=b[q])and(f[i][j][k]+tt>f[i+1][q][longint(tt-a[q])])
              then begin
                f[i+1][q][longint(tt-a[q])]:=f[i][j][k]+tt;
                px[i+1][q][longint(tt-a[q])]:=j;
                py[i+1][q][longint(tt-a[q])]:=k;
              end;
          tt:=a[j]+k;
          if (tt>inf/tk)then continue;
          tt:=tt*tk;
          for q:=j+1 to m do
            if (tt>=a[q])and(tt<=b[q])and(f[i][j][k]+tt>f[i+1][q][longint(tt-a[q])])
              then begin
                f[i+1][q][longint(tt-a[q])]:=f[i][j][k]+tt;
                px[i+1][q][longint(tt-a[q])]:=j;
                py[i+1][q][longint(tt-a[q])]:=k;
              end;
        end;
  max:=-1;
  xx:=0;
  yy:=0;
  for j:=1 to m do
    for k:=0 to longint(b[j]-a[j])do
      if (f[n][j][k]>max)then begin
        max:=f[n][j][k];
        xx:=j;yy:=k;
      end;
  if (max<1)then begin
    writeln('NO');
    exit;
  end;
  writeln('YES');
  for i:=n downto 1 do begin
    inc(kr);
    r1[kr]:=nom[xx];
    r2[kr]:=a[xx]+yy;
    pxx:=px[i][xx][yy];
    pyy:=py[i][xx][yy];
    xx:=pxx;
    yy:=pyy;
  end;
  for i:=kr downto 1 do writeln(r1[i],' ',r2[i]);
end.

{$r-,q-,s-,i-,o+}
const
  inf:int64=100000000000000000;
var
  f:array[0..55,0..55,0..555]of int64;
  py,px:array[0..55,0..55,0..555]of longint;
  pxx,pyy,xx,yy,q,tmp,kr,i,j,k,n,m,tk:longint;
  max,ti,tt:int64;
  r2,a,b:array[0..500500]of int64;
  r1,nom,c:array[0..500500]of longint;

begin
  read(n,m,tk);
  for i:=1 to m do read(a[i],b[i],c[i]);
  for i:=1 to m do nom[i]:=i;
  for i:=1 to m-1 do
    for j:=1 to m-1 do
      if (c[j]>c[j+1])then begin
        ti:=a[j];a[j]:=a[j+1];a[j+1]:=ti;
        ti:=b[j];b[j]:=b[j+1];b[j+1]:=ti;
        tmp:=c[j];c[j]:=c[j+1];c[j+1]:=tmp;
        tmp:=nom[j];nom[j]:=nom[j+1];nom[j+1]:=tmp;
      end;
  fillchar(f,sizeof(f),$0);
  for j:=1 to m do
    for k:=0 to longint(b[j]-a[j]+1)do
      f[1][j][k]:=k+a[j];
  for i:=1 to n-1 do
    for j:=1 to m do
      for k:=0 to longint(b[j]-a[j]+1)do
        if (f[i][j][k]<>0)then begin
          tt:=a[j]+k;
          for q:=j+1 to m do
            if (tt+tk>=a[q])and(tt+tk<=b[q])and(f[i][j][k]+tt+tk>f[i+1][q][longint(tt+tk-a[q])])
              then begin
                f[i+1][q][longint(tt+tk-a[q])]:=f[i][j][k]+tt+tk;
                px[i+1][q][longint(tt+tk-a[q])]:=j;
                py[i+1][q][longint(tt+tk-a[q])]:=k;
              end;
          if (tt>inf/tk)then continue;
          for q:=j+1 to m do
            if (tt*tk>=a[q])and(tt*tk<=b[q])and(f[i][j][k]+tt*tk>f[i+1][q][longint(tt*tk-a[q])])
              then begin
                f[i+1][q][longint(tt*tk-a[q])]:=f[i][j][k]+tt*tk;
                px[i+1][q][longint(tt*tk-a[q])]:=j;
                py[i+1][q][longint(tt*tk-a[q])]:=k;
              end;
        end;
  max:=-1;
  xx:=0;
  yy:=0;
  for j:=1 to m do
    for k:=0 to longint(b[j]-a[j]+1)do
      if (f[n][j][k]>max)then begin
        max:=f[n][j][k];
        xx:=j;yy:=k;
      end;
  if (max<1)then begin
    writeln('NO');
    exit;
  end;
  writeln('YES');
  for i:=n downto 1 do begin
    inc(kr);
    r1[kr]:=nom[xx];
    r2[kr]:=a[xx]+yy;
    pxx:=px[i][xx][yy];
    pyy:=py[i][xx][yy];
    xx:=pxx;
    yy:=pyy;
  end;
  for i:=kr downto 1 do writeln(r1[i],' ',r2[i]);
end.

{$r-,q-,s-,i-,o+}
type
  int64=qword;
const
  inf:int64=100000000000000000;
var
  f:array[0..55,0..55,0..555]of int64;
  py,px:array[0..55,0..55,0..555]of longint;
  pxx,pyy,xx,yy,q,tmp,kr,i,j,k,n,m,tk:longint;
  max,ti,tt:int64;
  r2,a,b:array[0..500500]of int64;
  r1,nom,c:array[0..500500]of longint;

begin
  read(n,m,tk);
  for i:=1 to m do read(a[i],b[i],c[i]);

  for i:=1 to m do nom[i]:=i;
  for i:=1 to m-1 do
    for j:=1 to m-1 do
      if (c[j]>c[j+1])then begin
        ti:=a[j];a[j]:=a[j+1];a[j+1]:=ti;
        ti:=b[j];b[j]:=b[j+1];b[j+1]:=ti;
        tmp:=c[j];c[j]:=c[j+1];c[j+1]:=tmp;
        tmp:=nom[j];nom[j]:=nom[j+1];nom[j+1]:=tmp;
      end;

  fillchar(f,sizeof(f),$0);
  for j:=1 to m do
    for k:=0 to longint(b[j]-a[j])do
      f[1][j][k]:=k+a[j];

  for i:=1 to n-1 do
    for j:=1 to m do
      for k:=0 to longint(b[j]-a[j])do
        if (f[i][j][k]<>0)then begin
          tt:=a[j]+k+tk;
          for q:=j+1 to m do
            if (tt>=a[q])and(tt<=b[q])and(f[i][j][k]+tt>f[i+1][q][longint(tt-a[q])])
              then begin
                f[i+1][q][longint(tt-a[q])]:=f[i][j][k]+tt;
                px[i+1][q][longint(tt-a[q])]:=j;
                py[i+1][q][longint(tt-a[q])]:=k;
              end;
          tt:=a[j]+k;
          if (tt>inf/tk)then continue;
          tt:=tt*tk;
          for q:=j+1 to m do
            if (tt>=a[q])and(tt<=b[q])and(f[i][j][k]+tt>f[i+1][q][longint(tt-a[q])])
              then begin
                f[i+1][q][longint(tt-a[q])]:=f[i][j][k]+tt;
                px[i+1][q][longint(tt-a[q])]:=j;
                py[i+1][q][longint(tt-a[q])]:=k;
              end;
        end;

  max:=0;
  xx:=0;
  yy:=0;
  for j:=1 to m do
    for k:=0 to longint(b[j]-a[j])do
      if (f[n][j][k]>max)then begin
        max:=f[n][j][k];
        xx:=j;yy:=k;
      end;
  if (max<1)then begin
    writeln('NO');
    exit;
  end;
  kr:=0;
  writeln('YES');
  for i:=n downto 1 do begin
    inc(kr);
    r1[kr]:=nom[xx];
    r2[kr]:=a[xx]+yy;
    pxx:=px[i][xx][yy];
    pyy:=py[i][xx][yy];
    xx:=pxx;
    yy:=pyy;
  end;
  for i:=kr downto 1 do writeln(r1[i],' ',r2[i]);
end.

program task;
//{$APPTYPE DELPHI}
//{$APPTYPE CONSOLE}
{$R-,S-,Q-,I-,O+}

uses
  Math,
  SysUtils;

type kat=record
    a,b:int64;
    c:longint;
end;

var n,m,k,i,jj,l,i1,i2:longint;
    j,kkl,lk,rip:int64;
    kk:array[1..100]of kat;
    least:array[1..100]of longint;
    res:array[1..100,1..50,0..100]of int64;

function less(a,b:kat):boolean;
begin
    less:=(a.c<b.c)or((a.c=b.c)and(a.b<b.b));
end;

procedure bubble;
var i,j:longint;
    cc:kat;
begin
    for i:=2to m do
        for j:=m downto i do
            if less(kk[j],kk[j-1])then begin
                cc:=kk[j];
                kk[j]:=kk[j-1];
                kk[j-1]:=cc;
            end;
end;

procedure dls;
var i, j:longint;
begin
    for i:= 1 to m do begin
        j:=0;
        while (kk[j+1].c<kk[i].c) do inc(j);
        least[i]:=j;
    end;
end;

procedure init;
var i,j,k:longint;
begin
    for j:= 1 to 50 do
        for i:=1 to 100 do
            for k := 0 to 100 do
                res[i,j,k]:=0;
end;

procedure obr(x, y : longint;z:int64);
var j, kkl,lk : int64;
    ok:boolean;
begin
    ok:=false;
    if (x = 1) then
        writeln(y,' ',z+kk[y].a)
    else begin
        j := z;
        kkl := kk[y].a+j;
        if kkl mod int64(k) = 0 then begin
            lk := kkl div int64(k);
            l:=1;
            while (not ok) and (l<=least[y]) do begin
                if (lk>=kk[l].a)and(lk<=kk[l].b)and(res[l,x-1,lk-kk[l].a]<>0)and
                (res[y,x,z]=res[l,x-1,lk-kk[l].a]+kkl)then begin
                    ok:=true;
                    obr(x-1,l,lk-kk[l].a);
                    writeln(y,' ',z+kk[y].a);
                end;
                l:=l+1;
            end;
        end;
            lk:=kkl-int64(k);
            l:=1;
            while (not ok) and (l<=least[y]) do begin
                if (lk>=kk[l].a)and(lk<=kk[l].b)and(res[l,x-1,lk-kk[l].a]<>0)and
                (res[y,x,z]=res[l,x-1,lk-kk[l].a]+kkl)then begin
                    ok:=true;
                    obr(x-1,l,lk-kk[l].a);
                    writeln(y,' ',z+kk[y].a);
                end;
                l:=l+1;
            end;
        end;
end;

begin
    //assign(input,'input.txt');
    //assign(output, 'output.txt');
    init;
    readln(n,m,k);
    for i:=1to m do
        readln(kk[i].a,kk[i].b,kk[i].c);
    bubble;
    for i:=1 to m do begin
        kkl:=kk[i].a;
        j:=0;
        //jj:=1;
        while kkl<=kk[i].b do begin
            res[i,1,j]:=kkl;
            //writeln('res :', jj, ':',i, ':', j, ' is ', res[i,jj,j]);
            j:=j+1;
            kkl:=kkl+1;
        end;
    end;
    dls;
    for jj:=2 to n do begin
        for i:=1to m do begin
            kkl:=kk[i].a;
            j:=0;
            while (kkl<=kk[i].b)do begin
                kkl:=j+kk[i].a;
                if kkl mod int64(k) = 0 then begin
                    lk:=kkl div int64(k);
                    for l:=1 to least[i] do
                    if (lk>=kk[l].a)and(lk<=kk[l].b)and(res[l,jj-1,lk-kk[l].a]<>0)then
                        res[i,jj,j]:=max(res[i,jj,j], res[l,jj-1,lk-kk[l].a]+kkl);
                end;
                lk:=kkl-int64(k);
                for l:=1 to least[i] do
                if (lk >= kk[l].a)and(lk<=kk[l].b)and(res[l,jj-1,lk-kk[l].a]<>0)then
                    res[i,jj,j]:=max(res[i,jj,j], res[l,jj-1,lk-kk[l].a]+kkl);
                kkl:=kkl+1;
                //writeln('res :', jj, ':',i, ':', j, ' is ', res[jj,i,j]);
                j:=j+1;
            end;
        end;
    end;
    rip:=0;
    i1:=0;
    i2:=0;
    for i:=1to m do begin
        kkl:=kk[i].a;
        j:=0;
        while(kkl<=kk[i].b)do begin
            rip:=max(rip,res[i,n,j]);
            if rip=res[i,n,j] then begin
                i1:=i;
                i2:=j;
            end;
            j:=j+1;
            kkl:=kkl+1;
        end
    end;
    if rip = 0 then writeln('NO') else begin
        writeln('YES');
        //writeln(rip,' ',i1, ' ',i2);
        obr(n, i1, i2);
    end;
    halt(0)
end.

program task;
//{$APPTYPE DELPHI}
//{$APPTYPE CONSOLE}
{$R-,S-,Q-,I-,O-}

uses
  Math,
  SysUtils;

type kat=record
    a,b:int64;
    c,id:longint;
end;

var n,m,k,i,jj,l,i1,i2:longint;
    j,kkl,lk,rip:int64;
    kk:array[1..100]of kat;
    least:array[1..100]of longint;
    res:array[1..100,1..50,0..100]of int64;

function less(a,b:kat):boolean;
begin
    less:=(a.c<b.c)or((a.c=b.c)and(a.b<b.b));
end;

procedure bubble;
var i,j:longint;
    cc:kat;
begin
    for i:=2to m do
        for j:=m downto i do
            if less(kk[j],kk[j-1])then begin
                cc:=kk[j];
                kk[j]:=kk[j-1];
                kk[j-1]:=cc;
            end;
end;

procedure dls;
var i, j:longint;
begin
    for i:= 1 to m do begin
        j:=0;
        while (kk[j+1].c<kk[i].c) do inc(j);
        least[i]:=j;
    end;
end;

procedure init;
var i,j,k:longint;
begin
    for j:= 1 to 50 do
        for i:=1 to 100 do
            for k := 0 to 100 do
                res[i,j,k]:=0;
end;

procedure obr(x, y : longint;z:int64);
var j, kkl,lk : int64;
    ok:boolean;
begin
    ok:=false;
    if (x = 1) then
        writeln(kk[y].id,' ',z+kk[y].a)
    else begin
        j := z;
        kkl := kk[y].a+j;
        if kkl mod int64(k) = 0 then begin
            lk := kkl div int64(k);
            l:=1;
            while (not ok) and (l<=least[y]) do begin
                if (lk>=kk[l].a)and(lk<=kk[l].b)and(res[l,x-1,lk-kk[l].a]<>0)and
                (res[y,x,z]=res[l,x-1,lk-kk[l].a]+kkl)then begin
                    ok:=true;
                    obr(x-1,l,lk-kk[l].a);
                    writeln(kk[y].id,' ',z+kk[y].a);
                end;
                l:=l+1;
            end;
        end;
            lk:=kkl-int64(k);
            l:=1;
            while (not ok) and (l<=least[y]) do begin
                if (lk>=kk[l].a)and(lk<=kk[l].b)and(res[l,x-1,lk-kk[l].a]<>0)and
                (res[y,x,z]=res[l,x-1,lk-kk[l].a]+kkl)then begin
                    ok:=true;
                    obr(x-1,l,lk-kk[l].a);
                    writeln(kk[y].id,' ',z+kk[y].a);
                end;
                l:=l+1;
            end;
        end;
end;

begin
    assign(input,'input.txt');
    assign(output, 'output.txt');
    init;
    readln(n,m,k);
    for i := 1 to m do
        kk[i].id := i;
    for i:=1to m do
        readln(kk[i].a,kk[i].b,kk[i].c);
    bubble;
    for i:=1 to m do begin
        kkl:=kk[i].a;
        j:=0;
        //jj:=1;
        while kkl<=kk[i].b do begin
            res[i,1,j]:=kkl;
            //writeln('res :', jj, ':',i, ':', j, ' is ', res[i,jj,j]);
            j:=j+1;
            kkl:=kkl+1;
        end;
    end;
    dls;
    for jj:=2 to n do begin
        for i:=1to m do begin
            kkl:=kk[i].a;
            j:=0;
            while (kkl<=kk[i].b)do begin
                kkl:=j+kk[i].a;
                if kkl mod int64(k) = 0 then begin
                    lk:=kkl div int64(k);
                    for l:=1 to least[i] do
                    if (lk>=kk[l].a)and(lk<=kk[l].b)and(res[l,jj-1,lk-kk[l].a]<>0)then
                        res[i,jj,j]:=max(res[i,jj,j], res[l,jj-1,lk-kk[l].a]+kkl);
                end;
                lk:=kkl-int64(k);
                for l:=1 to least[i] do
                if (lk >= kk[l].a)and(lk<=kk[l].b)and(res[l,jj-1,lk-kk[l].a]<>0)then
                    res[i,jj,j]:=max(res[i,jj,j], res[l,jj-1,lk-kk[l].a]+kkl);
                kkl:=kkl+1;
                //writeln('res :', jj, ':',i, ':', j, ' is ', res[jj,i,j]);
                j:=j+1;
            end;
        end;
    end;
    rip:=0;
    i1:=0;
    i2:=0;
    for i:=1to m do begin
        kkl:=kk[i].a;
        j:=0;
        while(kkl<=kk[i].b)do begin
            rip:=max(rip,res[i,n,j]);
            if rip=res[i,n,j] then begin
                i1:=i;
                i2:=j;
            end;
            j:=j+1;
            kkl:=kkl+1;
        end
    end;
    if rip = 0 then writeln('NO') else begin
        writeln('YES');
        //writeln(rip,' ',i1, ' ',i2);
        obr(n, i1, i2);
    end;
    halt(0)
end.

const
  inf='reform.in';
  outf='reform.out';
var
  max:int64;
  i,j,l,n,m,k,p,mj,ml:longint;
  a,b,c,pos,ansj,ansl:array[0..100] of int64;
  f,pre,prej:array[0..50,0..50,0..100] of int64;
procedure swap(var x,y:int64);
var
  z:int64;
begin
  z:=x; x:=y; y:=z;
end;
procedure qsort(s,t:longint);
var
  i,j,now:longint;
begin
  i:=s; j:=t; now:=c[(i+j)>>1];
  repeat
    while c[i]<now do inc(i);
    while c[j]>now do dec(j);
    if i<=j then begin
      swap(a[i],a[j]);
      swap(b[i],b[j]);
      swap(c[i],c[j]);
      swap(pos[i],pos[j]);
      inc(i); dec(j);
    end;
  until i>j;
  if s<j then qsort(s,j);
  if i<t then qsort(i,t);
end;
begin
  readln(n,m,k);
  for i:=1 to m do begin
    pos[i]:=i;
    read(a[i],b[i],c[i]);
  end;
  qsort(1,m);
  for i:=1 to n do
    for j:=1 to m do
      for l:=0 to b[j]-a[j] do begin
        f[i,j,l]:=0;
        for p:=1 to j-1 do if c[p]<c[j] then begin
          if i=1 then continue;
          if ((a[j]+l) mod k=0) and ((a[j]+l) div k>=a[p]) and ((a[j]+l) div k<=b[p]) and (f[i-1,p,(a[j]+l) div k-a[p]]>f[i,j,l]) then begin
            f[i,j,l]:=f[i-1,p,(a[j]+l) div k-a[p]];
            prej[i,j,l]:=p;
            pre[i,j,l]:=0;
          end;
          if (a[j]+l-k>=a[p]) and (a[j]+l-k<=b[p]) and (f[i-1,p,a[j]+l-k-a[p]]>f[i,j,l]) then begin
            f[i,j,l]:=f[i-1,p,a[j]+l-k-a[p]];
            prej[i,j,l]:=p;
            pre[i,j,l]:=1;
          end;
        end;
        if (f[i,j,l]>0) or (i=1) then inc(f[i,j,l],a[j]+l) else f[i,j,l]:=-maxlongint;
      end;
  max:=-maxlongint;
  for j:=1 to m do
    for l:=0 to b[j]-a[j] do if f[n,j,l]>max then max:=f[n,j,l];
  for i:=1 to m do
    for l:=0 to b[j]-a[j] do if max=f[n,j,l] then begin
      mj:=j; ml:=l;
    end;
  if max<=0 then writeln('NO') else begin
    writeln('YES');
    ansj[n]:=mj; ansl[n]:=a[mj]+ml;
    for i:=n-1 downto 1 do begin
      ansj[i]:=prej[i+1,mj,ml];
      if pre[i+1,mj,ml]=1 then ansl[i]:=a[mj]+ml-k else ansl[i]:=(a[mj]+ml) div k;
      mj:=ansj[i];
      ml:=ansl[i]-a[mj];
    end;
    for i:=1 to n do writeln(pos[ansj[i]],' ',ansl[i]);
  end;
end.

program probcc;

var f:array[0..50,0..50,0..100]of int64;
    pre,pp:array[0..50,0..50,0..100]of longint;
    a,b,c,num,anss,anst:array[0..50]of int64;
    n,m,k,ml,mj,i,j,p,l:longint;maxx,min,t:int64;

procedure init;
begin
  //assign(input,'cc.in');reset(input);
  readln(n,m,k);min:=maxlongint;
  for i:=1 to m do begin
    readln(a[i],b[i],c[i]);
    if c[i]<min then min:=c[i];num[i]:=i;
  end;
  for i:=1 to m-1 do
    for j:=i+1 to m do
      if c[i]>c[j] then begin
        t:=c[i];c[i]:=c[j];c[j]:=t;
        t:=a[i];a[i]:=a[j];a[j]:=t;
        t:=b[i];b[i]:=b[j];b[j]:=t;
        t:=num[i];num[i]:=num[j];num[j]:=t;
      end;
end;

function max(a,b:longint):longint;
begin if a>b then exit(a) else exit(b);end;

procedure dp;
begin
  for i:=1 to n do
    for j:=1 to m do
      for l:=0 to b[j]-a[j] do begin
        f[i,j,l]:=0;
        for p:=1 to j-1 do if c[p]<c[j] then begin
          if i=1 then continue;
          if ((a[j]+l) mod k=0)and((a[j]+l)div k>=a[p])and((a[j]+l)div k<=b[p]) then
            if f[i,j,l]<f[i-1,p,(a[j]+l) div k-a[p]] then begin
              f[i,j,l]:=f[i-1,p,(a[j]+l) div k-a[p]];
              pre[i,j,l]:=p;pp[i,j,l]:=0;
            end;
          if (a[j]+l-k>=a[p])and(a[j]+l-k<=b[p]) then
            if f[i,j,l]<f[i-1,p,(a[j]+l-k-a[p])] then begin
              f[i,j,l]:=f[i-1,p,(a[j]+l-k-a[p])];
              pre[i,j,l]:=p;pp[i,j,l]:=1;
            end;
        end;
        if (f[i,j,l]>0)or(i=1) then inc(f[i,j,l],a[j]+l) else f[i,j,l]:=-maxlongint;
      end;
end;

begin
  init;
  dp;
  maxx:=-maxlongint;
  for j:=1 to m do for l:=1 to b[j]-a[j] do maxx:=max(f[n,j,l],maxx);
  for j:=1 to m do for l:=1 to b[j]-a[j] do if maxx=f[n,j,l] then begin
    mj:=j;ml:=l;
  end;
  if maxx<=0 then writeln('NO') else begin
    writeln('YES');
    anss[n]:=mj;anst[n]:=a[mj]+ml;
    for i:=n-1 downto 1 do begin
      anss[i]:=pre[i+1,mj,ml];
      if pp[i+1,mj,ml]=1 then anst[i]:=a[mj]+ml-k else anst[i]:=(a[mj]+ml) div k;
      mj:=anss[i];ml:=anst[i]-a[mj];
    end;
    for i:=1 to n do writeln(num[anss[i]],' ',anst[i]);
  end;
end.

program probcc;

var f:array[0..50,0..50,0..100]of int64;
    pre,pp:array[0..50,0..50,0..100]of longint;
    a,b,c,num,anss,anst:array[0..50]of int64;
    n,m,k,ml,mj,i,j,p,l:longint;maxx,min,t:int64;

procedure init;
begin
  //assign(input,'cc.in');reset(input);
  readln(n,m,k);min:=maxlongint;
  for i:=1 to m do begin
    readln(a[i],b[i],c[i]);
    if c[i]<min then min:=c[i];num[i]:=i;
  end;
  for i:=1 to m-1 do
    for j:=i+1 to m do
      if c[i]>c[j] then begin
        t:=c[i];c[i]:=c[j];c[j]:=t;
        t:=a[i];a[i]:=a[j];a[j]:=t;
        t:=b[i];b[i]:=b[j];b[j]:=t;
        t:=num[i];num[i]:=num[j];num[j]:=t;
      end;
end;

function max(a,b:longint):longint;
begin if a>b then exit(a) else exit(b);end;

procedure dp;
begin
  for i:=1 to n do
    for j:=1 to m do
      for l:=0 to b[j]-a[j] do begin
        f[i,j,l]:=0;
        for p:=1 to j-1 do if c[p]<c[j] then begin
          if i=1 then continue;
          if ((a[j]+l) mod k=0)and((a[j]+l)div k>=a[p])and((a[j]+l)div k<=b[p]) then
            if f[i,j,l]<f[i-1,p,(a[j]+l) div k-a[p]] then begin
              f[i,j,l]:=f[i-1,p,(a[j]+l) div k-a[p]];
              pre[i,j,l]:=p;pp[i,j,l]:=0;
            end;
          if (a[j]+l-k>=a[p])and(a[j]+l-k<=b[p]) then
            if f[i,j,l]<f[i-1,p,(a[j]+l-k-a[p])] then begin
              f[i,j,l]:=f[i-1,p,(a[j]+l-k-a[p])];
              pre[i,j,l]:=p;pp[i,j,l]:=1;
            end;
        end;
        if (f[i,j,l]>0)or(i=1) then inc(f[i,j,l],a[j]+l) else f[i,j,l]:=-maxlongint;
      end;
end;

begin
  init;
  dp;
  maxx:=-maxlongint;
  for j:=1 to m do for l:=1 to b[j]-a[j] do maxx:=max(f[n,j,l],maxx);
  for j:=1 to m do for l:=1 to b[j]-a[j] do if maxx=f[n,j,l] then begin
    mj:=j;ml:=l;
  end;
  if maxx=-maxlongint then writeln('NO') else begin
    writeln('YES');
    anss[n]:=mj;anst[n]:=a[mj]+ml;
    for i:=n-1 downto 1 do begin
      anss[i]:=pre[i+1,mj,ml];
      if pp[i+1,mj,ml]=1 then anst[i]:=a[mj]+ml-k else anst[i]:=(a[mj]+ml) div k;
      mj:=anss[i];ml:=anst[i]-a[mj];
    end;
    for i:=1 to n do writeln(num[anss[i]],' ',anst[i]);
  end;
end.

program probcc;

var f:array[0..50,0..50,0..100]of int64;
    pre,pp:array[0..50,0..50,0..100]of longint;
    a,b,c,num,anss,anst:array[0..50]of int64;
    n,m,k,ml,mj,i,j,p,l:longint;maxx,min,t:int64;

procedure init;
begin
  //assign(input,'cc.in');reset(input);
  readln(n,m,k);min:=maxlongint;
  for i:=1 to m do begin
    readln(a[i],b[i],c[i]);
    if c[i]<min then min:=c[i];num[i]:=i;
  end;
  for i:=1 to m-1 do
    for j:=i+1 to m do
      if c[i]>c[j] then begin
        t:=c[i];c[i]:=c[j];c[j]:=t;
        t:=a[i];a[i]:=a[j];a[j]:=t;
        t:=b[i];b[i]:=b[j];b[j]:=t;
        t:=num[i];num[i]:=num[j];num[j]:=t;
      end;
end;

function max(a,b:longint):longint;
begin if a>b then exit(a) else exit(b);end;

procedure dp;
begin
  for i:=1 to n do
    for j:=1 to m do
      for l:=0 to b[j]-a[j] do begin
        f[i,j,l]:=0;
        for p:=1 to j-1 do if c[p]<c[j] then begin
          if i=1 then continue;
          if ((a[j]+l) mod k=0)and((a[j]+l)div k>=a[p])and((a[j]+l)div k<=b[p]) then
            if f[i,j,l]<f[i-1,p,(a[j]+l) div k-a[p]] then begin
              f[i,j,l]:=f[i-1,p,(a[j]+l) div k-a[p]];
              pre[i,j,l]:=p;pp[i,j,l]:=0;
            end;
          if (a[j]+l-k>=a[p])and(a[j]+l-k<=b[p]) then
            if f[i,j,l]<f[i-1,p,(a[j]+l-k-a[p])] then begin
              f[i,j,l]:=f[i-1,p,(a[j]+l-k-a[p])];
              pre[i,j,l]:=p;pp[i,j,l]:=1;
            end;
        end;
        if (f[i,j,l]>0)or(i=1) then inc(f[i,j,l],a[j]+l) else f[i,j,l]:=-maxlongint;
      end;
end;

begin
  init;
  dp;
  maxx:=-maxlongint;
  for j:=1 to m do for l:=0 to b[j]-a[j] do maxx:=max(f[n,j,l],maxx);
  for j:=1 to m do for l:=0 to b[j]-a[j] do if maxx=f[n,j,l] then begin
    mj:=j;ml:=l;
  end;
  if maxx<=0 then writeln('NO') else begin
    writeln('YES');
    anss[n]:=mj;anst[n]:=a[mj]+ml;
    for i:=n-1 downto 1 do begin
      anss[i]:=pre[i+1,mj,ml];
      if pp[i+1,mj,ml]=1 then anst[i]:=a[mj]+ml-k else anst[i]:=(a[mj]+ml) div k;
      mj:=anss[i];ml:=anst[i]-a[mj];
    end;
    for i:=1 to n do writeln(num[anss[i]],' ',anst[i]);
  end;
end.

type tracer = record
i,j,k:longint;
pr:longint;
End;

type sub = record
a:int64;
b:int64;
vt:longint;
c:longint;
End;

var f1,f2:text;
A:array[1..50] of sub;
F:array[1..50,1..50,0..100] of int64;
TR:array[1..50,1..50,0..100] of tracer;
m,n,sk,i:longint;

Procedure Partition(L,H:longint);
var i,j:longint;
    Pivot:longint;
    tg:sub;
Begin
     if L>=H then Exit;
     pivot := A[Random(H-L+1)+L].c;
     i:=L; j:=H;
     Repeat
       while A[i].c<pivot do Inc(i);
       While A[j].c>Pivot do dec(j);
       if i<=j then
       Begin
         If i<j then
         Begin
           tg:=a[i];
           a[i]:=a[j];
           A[j]:=tg;
         End;
         inc(i);
         dec(j);
       End;
     Until i>j;
     Partition(L,j); Partition(i,H);
End;

Function MT(i,j,k,pr:longint):tracer;
Begin
        MT.i:=i;MT.j:=j;MT.k:=k;MT.pr:=pr;
End;

Procedure trace(i,j,k:longint);
begin
        if tr[i,j,k].i<>0 then
        trace(tr[i,j,k].i,tr[i,j,k].j,tr[i,j,k].k);
        writeln(f2,A[Tr[i,j,k].pr].vt,' ',A[j].a+k);
ENd;

Procedure Process;
var i,j,k,l:longint;
v:int64;
s:int64;
res:int64;
vti,vtj,vtk:longint;
Begin
        For i:=1 to m do
        for j:=0 to 100 do
        Begin
          F[1,i,j]:=j+a[i].a;
          TR[1,i,j].pr:=i;
        End;

        for i:=2 to n do
        begin
          for j:=1 to m do
          for k:=0 to a[j].b-a[j].a do
          begin
            for l:=1 to j-1 do
            Begin
              if A[l].c<A[j].c then
              begin
                s:=a[j].a+k;
                if (s mod sk=0) and (a[l].a<=s div sk) and (s div sk<=a[l].b) then
                Begin
                  v:=F[i-1,l,s div sk-a[l].a];
                  if (v<>0) and (F[i,j,k]<v+s) then
                  Begin
                    F[i,j,k]:=v+s;
                    TR[i,j,k]:=MT(i-1,l,s div sk-a[l].a,j);
                  End;
                End;
                if (a[l].a<=s - sk) and (s - sk<= a[l].b) then
                Begin
                  v:=F[i-1,l,s - sk  -a[l].a];
                  if (v<>0) and (F[i,j,k]<v+s) then
                  Begin
                    F[i,j,k]:=v+s;
                    TR[i,j,k]:=MT(i-1,l,s - sk-a[l].a,j);
                  End;
                End;
              end;
            end;
          End;
        ENd;

        res:=0;
        for i:=1 to m do
        for j:=0 to 100 do
        if res<F[n,i,j] then
        Begin
          res:=F[n,i,j];
          vti:=n;vtj:=i;vtk:=j;
        end;
        if res<>0 then
        Begin
          writeln(f2,'YES');
          trace(vti,vtj,vtk);
        End
        else
        writeln(f2,'NO');
End;

Begin
        assign(f1,'');reset(f1);
        assign(f2,'');rewrite(f2);
        readln(f1,n,m,sk);
        for i:=1 to m do begin readln(f1,A[i].a,a[i].b,a[i].c); a[i].vt:=i; End;
        Partition(1,m);
        Process;
        close(f1);
        close(f2);
End.

type tracer = record
i,j,k:longint;
pr:longint;
End;

type sub = record
a:int64;
b:int64;
vt:longint;
c:longint;
End;

var f1,f2:text;
A:array[1..50] of sub;
F:array[1..50,1..50,0..100] of int64;
TR:array[1..50,1..50,0..100] of tracer;
m,n,sk,i:longint;

Procedure Partition(L,H:longint);
var i,j:longint;
    Pivot:longint;
    tg:sub;
Begin
     if L>=H then Exit;
     pivot := A[Random(H-L+1)+L].c;
     i:=L; j:=H;
     Repeat
       while A[i].c<pivot do Inc(i);
       While A[j].c>Pivot do dec(j);
       if i<=j then
       Begin
         If i<j then
         Begin
           tg:=a[i];
           a[i]:=a[j];
           A[j]:=tg;
         End;
         inc(i);
         dec(j);
       End;
     Until i>j;
     Partition(L,j); Partition(i,H);
End;

Function MT(i,j,k,pr:longint):tracer;
Begin
        MT.i:=i;MT.j:=j;MT.k:=k;MT.pr:=pr;
End;

Procedure trace(i,j,k:longint);
begin
        if tr[i,j,k].i<>0 then
        trace(tr[i,j,k].i,tr[i,j,k].j,tr[i,j,k].k);
        writeln(f2,A[Tr[i,j,k].pr].vt,' ',A[j].a+k);
ENd;

Procedure Process;
var i,j,k,l:longint;
v:int64;
s:int64;
res:int64;
vti,vtj,vtk:longint;
Begin
        For i:=1 to m do
        for j:=0 to 100 do
        Begin
          F[1,i,j]:=j+a[i].a;
          TR[1,i,j].pr:=i;
        End;

        for i:=2 to n do
        begin
          for j:=1 to m do
          for k:=0 to a[j].b-a[j].a do
          begin
            if (i=4) and (j=5) and (k=39) then
            writeln('.');
            for l:=1 to j-1 do
            Begin
              if A[l].c<A[j].c then
              begin
                s:=a[j].a+k;
                if (s mod sk=0) and (a[l].a<=s div sk) and (s div sk<=a[l].b) then
                Begin
                  v:=F[i-1,l,s div sk-a[l].a];
                  if (v<>0) and (F[i,j,k]<v+s) then
                  Begin
                    F[i,j,k]:=v+s;
                    TR[i,j,k]:=MT(i-1,l,s div sk-a[l].a,j);
                  End;
                End;
                if (a[l].a<=s - sk) and (s - sk<= a[l].b) then
                Begin
                  v:=F[i-1,l,s - sk  -a[l].a];
                  if (v<>0) and (F[i,j,k]<v+s) then
                  Begin
                    F[i,j,k]:=v+s;
                    TR[i,j,k]:=MT(i-1,l,s - sk-a[l].a,j);
                  End;
                End;
              end;
            end;
          End;
        ENd;

        res:=0;
        for i:=1 to m do
        for j:=0 to 100 do
        if res<F[n,i,j] then
        Begin
          res:=F[n,i,j];
          vti:=n;vtj:=i;vtk:=j;
        end;
        if res<>0 then
        Begin
          writeln(f2,'YES');
          trace(vti,vtj,vtk);
        End
        else
        writeln(f2,'NO');
End;

Begin
        assign(f1,'');reset(f1);
        assign(f2,'');rewrite(f2);
        readln(f1,n,m,sk);
        for i:=1 to m do begin readln(f1,A[i].a,a[i].b,a[i].c); a[i].vt:=i; End;
        Partition(1,m);
        Process;
        close(f1);
        close(f2);
End.

const fi='';//119c.inp';
      fo='';//119c.out';

type rec=record
          day,subj,exer:longint;
end;
     rec1=record
          a,b:int64;
          c,cs:longint;
end;

var f1:array[0..55,0..55,0..110] of int64;
    trace:array[0..55,0..55,0..110] of rec;
    n,m,k:longint;
    pt:array[1..55] of rec1;
    f:text;

procedure qs(dd,cc:longint);
var i,j:longint; kk,tg:rec1;
begin
     if dd>=cc then exit;
     i:=dd; j:=cc; kk:=pt[random(cc-dd+1)+dd];
     repeat
     begin
          while pt[i].c<kk.c do inc(i);
          while pt[j].c>kk.c do dec(j);
          if i<=j then
          begin
               tG:=pt[i]; pt[i]:=pt[j]; pt[j]:=tg;
               inc(i); dec(j);
          end;
     end
     until i>j;
     qs(dd,j); qs(i,cc);
end;

procedure nhap;
var i:longint;
begin
     assign(f,fi); reset(f);
     readln(f,n,m,k);
     for i:=1 to m do
          with pt[i] do
          begin
               readln(f,a,b,c);
               cs:=i;
          end;
     qs(1,m);
     close(f);
end;

procedure work;
var i,j,kk,l:longint;  none:rec;   sl,tg:int64;
begin
     none.day:=-1; none.subj:=-1; none.exer:=-1;
     for i:=1 to m do
          for j:=0 to pt[i].b-pt[i].a do
          begin
               f1[1,pt[i].cs,j]:=pt[i].a+j;
               trace[1,pt[i].cs,j]:=none;
          end;
     for i:=2 to n do
          for j:=1 to m do
               for kk:= 0 to pt[j].b-pt[j].a do
               begin
                    sl:=pt[j].a+kk;
                    if sl mod k =0 then
                    for l:=1 to j-1 do
                         if (pt[l].a<=sl div k) and (pt[l].b>=sl div k) then
                         begin
                              tg:=f1[i-1,l,sl div k-pt[l].a];//+sl;
                              if tg>0 then
                              if tg+sl>f1[i,j,kk] then
                              begin
                                   f1[i,j,kk]:=tg+sl;
                                   trace[i,j,kk].day:=i-1;
                                   trace[i,j,kk].subj:=l;
                                   trace[i,j,kk].exer:= sl div  k-pt[l].a;
                              end;
                         end;
                    if sl>=k then
                    for l:=1 to j-1 do
                         if (pt[l].a<=sl-k) and (pt[l].b>=sl-k) then
                         begin
                              tg:=f1[i-1,l,sl-k-pt[l].a];//+sl;
                              if tg>0 then
                              if tg+sl> f1[i,j,kk] then
                              begin
                                   f1[i,j,kk] := tg+sl;
                                   trace[i,j,kk].day:=i-1;
                                   trace[i,j,kk].subj:=l;
                                   trace[i,j,kk].exer:=sl-k- pt[l].a;
                              end;
                         end;
               end;
end;

procedure truy_vet(x:rec);
begin
     if (trace[x.day,x.subj,x.exer].day<>-1) then
          truy_vet(trace[x.day,x.subj,x.exer]);
     writeln(f,pt[x.subj].cs,' ',pt[x.subj].a+x.exer);
end;

procedure inkq;
var i,j:longint; res:int64; re:rec;
begin
     assign(f,fo); rewrite(f);
     res:=0;
     for i:=1 to m do
          for j:=0 to pt[i].b-pt[i].a do
               if f1[n,i,j]>res then
               begin
                    res:=f1[n,i,j];
                    re.day:=n; re.subj:=i; re.exer:=j;
               end;
     if res=0 then writeln(f,'NO')
     else
     begin
          writeln(f,'YES');
          truy_vet(re);
     end;
     close(f);
end;

begin
     nhap;
     work;
     inkq;
end.

const fi='';//119c.inp';
      fo='';//119c.out';

type rec=record
          day,subj,exer:longint;
end;
     rec1=record
          a,b:int64;
          c,cs:longint;
end;

var f1:array[0..55,0..55,0..110] of int64;
    trace:array[0..55,0..55,0..110] of rec;
    n,m,k:longint;
    pt:array[1..55] of rec1;
    f:text;

procedure qs(dd,cc:longint);
var i,j:longint; kk,tg:rec1;
begin
     if dd>=cc then exit;
     i:=dd; j:=cc; kk:=pt[random(cc-dd+1)+dd];
     repeat
     begin
          while pt[i].c<kk.c do inc(i);
          while pt[j].c>kk.c do dec(j);
          if i<=j then
          begin
               tG:=pt[i]; pt[i]:=pt[j]; pt[j]:=tg;
               inc(i); dec(j);
          end;
     end
     until i>j;
     qs(dd,j); qs(i,cc);
end;

procedure nhap;
var i:longint;
begin
     assign(f,fi); reset(f);
     readln(f,n,m,k);
     for i:=1 to m do
          with pt[i] do
          begin
               readln(f,a,b,c);
               cs:=i;
          end;
     qs(1,m);
     close(f);
end;

procedure work;
var i,j,kk,l:longint;  none:rec;   sl,tg:int64;
begin
     none.day:=-1; none.subj:=-1; none.exer:=-1;
     for i:=1 to m do
          for j:=0 to pt[i].b-pt[i].a do
          begin
               f1[1,i,j]:=pt[i].a+j;
               trace[1,i,j]:=none;
          end;
     for i:=2 to n do
          for j:=1 to m do
               for kk:= 0 to pt[j].b-pt[j].a do
               begin
                    sl:=pt[j].a+kk;
                    if sl mod k =0 then
                    for l:=1 to j-1 do
                         if (pt[l].a<=sl div k) and (pt[l].b>=sl div k) then
                         begin
                              tg:=f1[i-1,l,sl div k-pt[l].a];//+sl;
                              if tg>0 then
                              if tg+sl>f1[i,j,kk] then
                              begin
                                   f1[i,j,kk]:=tg+sl;
                                   trace[i,j,kk].day:=i-1;
                                   trace[i,j,kk].subj:=l;
                                   trace[i,j,kk].exer:= sl div  k-pt[l].a;
                              end;
                         end;
                    if sl>=k then
                    for l:=1 to j-1 do
                         if (pt[l].a<=sl-k) and (pt[l].b>=sl-k) then
                         begin
                              tg:=f1[i-1,l,sl-k-pt[l].a];//+sl;
                              if tg>0 then
                              if tg+sl> f1[i,j,kk] then
                              begin
                                   f1[i,j,kk] := tg+sl;
                                   trace[i,j,kk].day:=i-1;
                                   trace[i,j,kk].subj:=l;
                                   trace[i,j,kk].exer:=sl-k- pt[l].a;
                              end;
                         end;
               end;
end;

procedure truy_vet(x:rec);
begin
     if (trace[x.day,x.subj,x.exer].day<>-1) then
          truy_vet(trace[x.day,x.subj,x.exer]);
     writeln(f,x.subj,' ',pt[x.subj].a+x.exer);
end;

procedure inkq;
var i,j:longint; res:int64; re:rec;
begin
     assign(f,fo); rewrite(f);
     res:=0;
     for i:=1 to m do
          for j:=0 to pt[i].b-pt[i].a do
               if f1[n,i,j]>res then
               begin
                    res:=f1[n,i,j];
                    re.day:=n; re.subj:=i; re.exer:=j;
               end;
     if res=0 then writeln(f,'NO')
     else
     begin
          writeln(f,'YES');
          truy_vet(re);
     end;
     close(f);
end;

begin
     nhap;
     work;
     inkq;
end.

const fi='';//119c.inp';
      fo='';//119c.out';

type rec=record
          day,subj,exer:longint;
end;
     rec1=record
          a,b:int64;
          c,cs:longint;
end;

var f1:array[0..55,0..55,0..110] of int64;
    trace:array[0..55,0..55,0..110] of rec;
    n,m,k:longint;
    pt:array[1..55] of rec1;
    f:text;

procedure qs(dd,cc:longint);
var i,j:longint; kk,tg:rec1;
begin
     if dd>=cc then exit;
     i:=dd; j:=cc; kk:=pt[random(cc-dd+1)+dd];
     repeat
     begin
          while pt[i].c<kk.c do inc(i);
          while pt[j].c>kk.c do dec(j);
          if i<=j then
          begin
               tG:=pt[i]; pt[i]:=pt[j]; pt[j]:=tg;
               inc(i); dec(j);
          end;
     end
     until i>j;
     qs(dd,j); qs(i,cc);
end;

procedure nhap;
var i:longint;
begin
     assign(f,fi); reset(f);
     readln(f,n,m,k);
     for i:=1 to m do
          with pt[i] do
          begin
               readln(f,a,b,c);
               cs:=i;
          end;
     qs(1,m);
     close(f);
end;

procedure work;
var i,j,kk,l:longint;  none:rec;   sl,tg:int64;
begin
     none.day:=-1; none.subj:=-1; none.exer:=-1;
     for i:=1 to m do
          for j:=0 to pt[i].b-pt[i].a do
          begin
               f1[1,pt[i].cs,j]:=pt[i].a+j;
               trace[1,pt[i].cs,j]:=none;
          end;
     for i:=2 to n do
          for j:=1 to m do
               for kk:= 0 to pt[j].b-pt[j].a do
               begin
                    sl:=pt[j].a+kk;
                    if sl mod k =0 then
                    for l:=1 to j-1 do
                         if (pt[j].c>pt[l].c) and (pt[l].a<=sl div k) and (pt[l].b>=sl div k) then
                         begin
                              tg:=f1[i-1,l,sl div k-pt[l].a];//+sl;
                              if tg>0 then
                              if tg+sl>f1[i,j,kk] then
                              begin
                                   f1[i,j,kk]:=tg+sl;
                                   trace[i,j,kk].day:=i-1;
                                   trace[i,j,kk].subj:=l;
                                   trace[i,j,kk].exer:= sl div  k-pt[l].a;
                              end;
                         end;
                    if sl>=k then
                    for l:=1 to j-1 do
                         if (pt[j].c>pt[l].c) and (pt[l].a<=sl-k) and (pt[l].b>=sl-k) then
                         begin
                              tg:=f1[i-1,l,sl-k-pt[l].a];//+sl;
                              if tg>0 then
                              if tg+sl> f1[i,j,kk] then
                              begin
                                   f1[i,j,kk] := tg+sl;
                                   trace[i,j,kk].day:=i-1;
                                   trace[i,j,kk].subj:=l;
                                   trace[i,j,kk].exer:=sl-k- pt[l].a;
                              end;
                         end;
               end;
end;

procedure truy_vet(x:rec);
begin
     if (trace[x.day,x.subj,x.exer].day<>-1) then
          truy_vet(trace[x.day,x.subj,x.exer]);
     writeln(f,pt[x.subj].cs,' ',pt[x.subj].a+x.exer);
end;

procedure inkq;
var i,j:longint; res:int64; re:rec;
begin
     assign(f,fo); rewrite(f);
     res:=0;
     for i:=1 to m do
          for j:=0 to pt[i].b-pt[i].a do
               if f1[n,i,j]>res then
               begin
                    res:=f1[n,i,j];
                    re.day:=n; re.subj:=i; re.exer:=j;
               end;
     if res=0 then writeln(f,'NO')
     else
     begin
          writeln(f,'YES');
          truy_vet(re);
     end;
     close(f);
end;

begin
     nhap;
     work;
     inkq;
end.

const fi='';//119c.inp';
      fo='';//119c.out';

type rec=record
          day,subj,exer:longint;
end;
     rec1=record
          a,b:int64;
          c,cs:longint;
end;

var f1:array[0..55,0..55,0..110] of int64;
    trace:array[0..55,0..55,0..110] of rec;
    n,m,k:longint;
    pt:array[1..55] of rec1;
    f:text;

procedure qs(dd,cc:longint);
var i,j:longint; kk,tg:rec1;
begin
     if dd>=cc then exit;
     i:=dd; j:=cc; kk:=pt[random(cc-dd+1)+dd];
     repeat
     begin
          while pt[i].c<kk.c do inc(i);
          while pt[j].c>kk.c do dec(j);
          if i<=j then
          begin
               tG:=pt[i]; pt[i]:=pt[j]; pt[j]:=tg;
               inc(i); dec(j);
          end;
     end
     until i>j;
     qs(dd,j); qs(i,cc);
end;

procedure nhap;
var i:longint;
begin
     assign(f,fi); reset(f);
     readln(f,n,m,k);
     for i:=1 to m do
          with pt[i] do
          begin
               readln(f,a,b,c);
               cs:=i;
          end;
     qs(1,m);
     close(f);
end;

procedure work;
var i,j,kk,l:longint;  none:rec;   sl,tg:int64;
begin
     none.day:=-1; none.subj:=-1; none.exer:=-1;
     for i:=1 to m do
          for j:=0 to pt[i].b-pt[i].a do
          begin
               f1[1,pt[i].cs,j]:=pt[i].a+j;
               trace[1,pt[i].cs,j]:=none;
          end;
     for i:=2 to n do
          for j:=1 to m do
               for kk:= 0 to pt[j].b-pt[j].a do
               begin
                    sl:=pt[j].a+kk;
                    if sl mod k =0 then
                    for l:=1 to j-1 do
                         if (pt[j].c>pt[j].c) and (pt[l].a<=sl div k) and (pt[l].b>=sl div k) then
                         begin
                              tg:=f1[i-1,l,sl div k-pt[l].a];//+sl;
                              if tg>0 then
                              if tg+sl>f1[i,j,kk] then
                              begin
                                   f1[i,j,kk]:=tg+sl;
                                   trace[i,j,kk].day:=i-1;
                                   trace[i,j,kk].subj:=l;
                                   trace[i,j,kk].exer:= sl div  k-pt[l].a;
                              end;
                         end;
                    if sl>=k then
                    for l:=1 to j-1 do
                         if (pt[j].c>pt[l].c) and (pt[l].a<=sl-k) and (pt[l].b>=sl-k) then
                         begin
                              tg:=f1[i-1,l,sl-k-pt[l].a];//+sl;
                              if tg>0 then
                              if tg+sl> f1[i,j,kk] then
                              begin
                                   f1[i,j,kk] := tg+sl;
                                   trace[i,j,kk].day:=i-1;
                                   trace[i,j,kk].subj:=l;
                                   trace[i,j,kk].exer:=sl-k- pt[l].a;
                              end;
                         end;
               end;
end;

procedure truy_vet(x:rec);
begin
     if (trace[x.day,x.subj,x.exer].day<>-1) then
          truy_vet(trace[x.day,x.subj,x.exer]);
     writeln(f,pt[x.subj].cs,' ',pt[x.subj].a+x.exer);
end;

procedure inkq;
var i,j:longint; res:int64; re:rec;
begin
     assign(f,fo); rewrite(f);
     res:=0;
     for i:=1 to m do
          for j:=0 to pt[i].b-pt[i].a do
               if f1[n,i,j]>res then
               begin
                    res:=f1[n,i,j];
                    re.day:=n; re.subj:=i; re.exer:=j;
               end;
     if res=0 then writeln(f,'NO')
     else
     begin
          writeln(f,'YES');
          truy_vet(re);
     end;
     close(f);
end;

begin
     nhap;
     work;
     inkq;
end.

const
{     fi='edu.inp';
     fo='edu.out';}
     fi='';
     fo='';
     maxn=111;

var
     f:text;
     a,b,c,ind:array[0..maxn]of int64;
     t1,t2,fx:array[0..maxn,0..maxn,0..maxn]of int64;
     k1,k2:array[0..maxn]of int64;
     n,m,k:longint;

function maxx(x,y:longint):longint;
begin
     maxx:=x;
     if y>x then maxx:=y;
end;

function minn(x,y:longint):longint;
begin
     minn:=x;
     if y<x then minn:=y;
end;

procedure swap(var x,y:int64);
var  z:int64;
begin
     z:=x;
     x:=y;
     y:=z;
end;

procedure sort;
var  i,j,t:integer;
begin
     for i:=1 to m-1 do
     begin
          t:=i;
          for j:=i+1 to m do
               if c[j]<c[t] then t:=j;
          swap(a[i],a[t]);
          swap(b[i],b[t]);
          swap(c[i],c[t]);
          swap(ind[i],ind[t]);
     end;
end;

procedure open;
var  i:longint;
begin
     assign(f,fi);reset(f);
     readln(f,n,m,k);
     for i:=1 to m do
     begin
          readln(f,a[i],b[i],c[i]);
          ind[i]:=i;
     end;
     close(f);
end;

procedure main;
var  kq,tmp,cv,sl,r1,r2:int64;
     i,j,t,i2:longint;
begin
     //fx[i,j,t] i cv dau trong j ngay va ngay j lam cv i
     fillchar(fx,sizeof(fx),0);
     fillchar(t1,sizeof(fx),0);
     fillchar(t2,sizeof(fx),0);
     for i:=1 to m do
          for t:=1 to b[i]-a[i]+1 do
               fx[i,1,t]:=t;

     //
     for j:=2 to n do
     for i:=j to m do
          for t:=1 to b[i]-a[i]+1 do
          for i2:=j-1 to i-1 do
          if c[i]>c[i2] then
          begin
               tmp:=a[i]+t-1-k;
               if (a[i2]<=tmp) and (tmp<=b[i2]) then
                    if fx[i2,j-1,tmp-a[i2]+1]<>0 then
                    if fx[i,j,t]<fx[i2,j-1,tmp-a[i2]+1]+t+a[i]-1 then
                    begin
                         fx[i,j,t]:=fx[i2,j-1,tmp-a[i2]+1]+t+a[i]-1;
                         t1[i,j,t]:=i2;
                         t2[i,j,t]:=tmp-a[i2]+1;
                    end;

               if (t+a[i]-1) mod k=0 then
               begin
                    tmp:=(a[i]+t-1) div k;
                    if (a[i2]<=tmp) and (tmp<=b[i2]) then
                         if fx[i2,j-1,tmp-a[i2]+1]<>0 then
                         if fx[i,j,t]<fx[i2,j-1,tmp-a[i2]+1]+t+a[i]-1 then
                         begin
                              fx[i,j,t]:=fx[i2,j-1,tmp-a[i2]+1]+t+a[i]-1;
                              t1[i,j,t]:=i2;
                              t2[i,j,t]:=tmp-a[i2]+1;
                         end;
               end;
          end;

     kq:=0;
     for i:=n to m do
          for t:=1 to b[i]-a[i]+1 do
          begin
               if kq<fx[i,n,t] then
               begin
                    kq:=fx[i,n,t];
                    cv:=i;
                    sl:=t;
               end;
          end;

     assign(f,fo);rewrite(f);
     if kq=0 then writeln(f,'NO')
     else
     begin
          writeln(f,'YES');
          for j:=n downto 1 do
          begin
               r1:=cv;r2:=sl;
               k1[j]:=ind[cv];
               k2[j]:=sl+a[cv]-1;
               cv:=t1[r1,j,r2];
               sl:=t2[r1,j,r2];
          end;
          for j:=1 to n do writeln(f,k1[j],' ',k2[j]);
     end;
     close(f);
end;

begin
     open;
     sort;
     main;
end.

const
{     fi='edu.inp';
     fo='edu.out';}
     fi='';
     fo='';
     maxn=111;

var
     f:text;
     a,b,c,ind:array[0..maxn]of longint;
     t1,t2,fx:array[0..maxn,0..maxn,0..maxn]of int64;
     k1,k2:array[0..maxn]of int64;
     n,m,k:longint;

function maxx(x,y:longint):longint;
begin
     maxx:=x;
     if y>x then maxx:=y;
end;

function minn(x,y:longint):longint;
begin
     minn:=x;
     if y<x then minn:=y;
end;

procedure swap(var x,y:longint);
var  z:longint;
begin
     z:=x;
     x:=y;
     y:=z;
end;

procedure sort;
var  i,j,t:integer;
begin
     for i:=1 to m-1 do
     begin
          t:=i;
          for j:=i+1 to m do
               if c[j]<c[t] then t:=j;
          swap(a[i],a[t]);
          swap(b[i],b[t]);
          swap(c[i],c[t]);
          swap(ind[i],ind[t]);
     end;
end;

procedure open;
var  i:longint;
begin
     assign(f,fi);reset(f);
     readln(f,n,m,k);
     for i:=1 to m do
     begin
          readln(f,a[i],b[i],c[i]);
          ind[i]:=i;
     end;
     close(f);
end;

procedure main;
var  kq,tmp,cv,sl,r1,r2:int64;
     i,j,t,i2:longint;
begin
     //fx[i,j,t] i cv dau trong j ngay va ngay j lam cv i
     fillchar(fx,sizeof(fx),0);
     fillchar(t1,sizeof(fx),0);
     fillchar(t2,sizeof(fx),0);
     for i:=1 to m do
          for t:=1 to b[i]-a[i]+1 do
               fx[i,1,t]:=t;

     //
     for j:=2 to n do
     for i:=j to m do
          for t:=1 to b[i]-a[i]+1 do
          for i2:=j-1 to i-1 do
          if c[i]>c[i2] then
          begin
               tmp:=a[i]+t-1-k;
               if (a[i2]<=tmp) and (tmp<=b[i2]) then
                    if fx[i,j,t]<fx[i2,j-1,tmp-a[i2]+1]+t+a[i]-1 then
                    begin
                         fx[i,j,t]:=fx[i2,j-1,tmp-a[i2]+1]+t+a[i]-1;
                         t1[i,j,t]:=i2;
                         t2[i,j,t]:=tmp-a[i2]+1;
                    end;

               if (t+a[i]-1) mod k=0 then
               begin
                    tmp:=(a[i]+t-1) div k;
                    if (a[i2]<=tmp) and (tmp<=b[i2]) then
                         if fx[i,j,t]<fx[i2,j-1,tmp-a[i2]+1]+t+a[i]-1 then
                         begin
                              fx[i,j,t]:=fx[i2,j-1,tmp-a[i2]+1]+t+a[i]-1;
                              t1[i,j,t]:=i2;
                              t2[i,j,t]:=tmp-a[i2]+1;
                         end;
               end;
          end;

     kq:=0;
     for i:=n to m do
          for t:=1 to b[i]-a[i]+1 do
          begin
               if kq<fx[i,n,t] then
               begin
                    kq:=fx[i,n,t];
                    cv:=i;
                    sl:=t;
               end;
          end;

     assign(f,fo);rewrite(f);
     if kq=0 then writeln(f,'NO')
     else
     begin
          writeln(f,'YES');
          for j:=n downto 1 do
          begin
               r1:=cv;r2:=sl;
               k1[j]:=ind[cv];
               k2[j]:=sl+a[cv]-1;
               cv:=t1[r1,j,r2];
               sl:=t2[r1,j,r2];
          end;
          for j:=1 to n do writeln(f,k1[j],' ',k2[j]);
     end;
     close(f);
end;

begin
     open;
     sort;
     main;
end.

const
{     fi='edu.inp';
     fo='edu.out';}
     fi='';
     fo='';
     maxn=111;

var
     f:text;
     a,b,c,ind:array[0..maxn]of int64;
     t1,t2,fx:array[0..maxn,0..maxn,0..maxn]of int64;
     k1,k2:array[0..maxn]of int64;
     n,m,k:longint;

function maxx(x,y:longint):longint;
begin
     maxx:=x;
     if y>x then maxx:=y;
end;

function minn(x,y:longint):longint;
begin
     minn:=x;
     if y<x then minn:=y;
end;

procedure swap(var x,y:int64);
var  z:int64;
begin
     z:=x;
     x:=y;
     y:=z;
end;

procedure sort;
var  i,j,t:integer;
begin
     for i:=1 to m-1 do
     begin
          t:=i;
          for j:=i+1 to m do
               if c[j]<c[t] then t:=j;
          swap(a[i],a[t]);
          swap(b[i],b[t]);
          swap(c[i],c[t]);
          swap(ind[i],ind[t]);
     end;
end;

procedure open;
var  i:longint;
begin
     assign(f,fi);reset(f);
     readln(f,n,m,k);
     for i:=1 to m do
     begin
          readln(f,a[i],b[i],c[i]);
          ind[i]:=i;
     end;
     close(f);
end;

procedure main;
var  kq,tmp,cv,sl,r1,r2:int64;
     i,j,t,i2:longint;
begin
     //fx[i,j,t] i cv dau trong j ngay va ngay j lam cv i
     fillchar(fx,sizeof(fx),0);
     fillchar(t1,sizeof(fx),0);
     fillchar(t2,sizeof(fx),0);
     for i:=1 to m do
          for t:=1 to b[i]-a[i]+1 do
               fx[i,1,t]:=t;

     //
     for j:=2 to n do
     for i:=j to m do
          for t:=1 to b[i]-a[i]+1 do
          for i2:=j-1 to i-1 do
          if c[i]>c[i2] then
          begin
               tmp:=a[i]+t-1-k;
               if (a[i2]<=tmp) and (tmp<=b[i2]) then
                    if fx[i,j,t]<fx[i2,j-1,tmp-a[i2]+1]+t+a[i]-1 then
                    begin
                         fx[i,j,t]:=fx[i2,j-1,tmp-a[i2]+1]+t+a[i]-1;
                         t1[i,j,t]:=i2;
                         t2[i,j,t]:=tmp-a[i2]+1;
                    end;

               if (t+a[i]-1) mod k=0 then
               begin
                    tmp:=(a[i]+t-1) div k;
                    if (a[i2]<=tmp) and (tmp<=b[i2]) then
                         if fx[i,j,t]<fx[i2,j-1,tmp-a[i2]+1]+t+a[i]-1 then
                         begin
                              fx[i,j,t]:=fx[i2,j-1,tmp-a[i2]+1]+t+a[i]-1;
                              t1[i,j,t]:=i2;
                              t2[i,j,t]:=tmp-a[i2]+1;
                         end;
               end;
          end;

     kq:=0;
     for i:=n to m do
          for t:=1 to b[i]-a[i]+1 do
          begin
               if kq<fx[i,n,t] then
               begin
                    kq:=fx[i,n,t];
                    cv:=i;
                    sl:=t;
               end;
          end;

     assign(f,fo);rewrite(f);
     if kq=0 then writeln(f,'NO')
     else
     begin
          writeln(f,'YES');
          for j:=n downto 1 do
          begin
               r1:=cv;r2:=sl;
               k1[j]:=ind[cv];
               k2[j]:=sl+a[cv]-1;
               cv:=t1[r1,j,r2];
               sl:=t2[r1,j,r2];
          end;
          for j:=1 to n do writeln(f,k1[j],' ',k2[j]);
     end;
     close(f);
end;

begin
     open;
     sort;
     main;
end.

type pp=record
         x,y:int64;
        end;

var n,m,x,y,i,j:longint;
    max,k,g,k1,k2,t:int64;
    a,b,c,d:array[0..51] of int64;
    v:array[0..51,0..51,0..101] of int64;
    w:array[0..51,0..51,0..101] of pp;

procedure sort(l,r: longint);
      var
         i,j,x,y: longint;
      begin
         i:=l;
         j:=r;
         x:=c[(l+r) div 2];
         repeat
           while c[i]<x do
            inc(i);
           while x<c[j] do
            dec(j);
           if not(i>j) then
             begin
                y:=a[i];
                a[i]:=a[j];
                a[j]:=y;
                y:=b[i];
                b[i]:=b[j];
                b[j]:=y;
                y:=c[i];
                c[i]:=c[j];
                c[j]:=y;
                y:=d[i];
                d[i]:=d[j];
                d[j]:=y;
                inc(i);
                j:=j-1;
             end;
         until i>j;
         if l<j then
           sort(l,j);
         if i<r then
           sort(i,r);
      end;

procedure tt(l,x,y:longint);
 begin
  if l=0 then exit;
  tt(l-1,w[l,x,y].x,w[l,x,y].y);
  writeln(d[x],' ',a[x]+y);
 end;

 begin

  read(n,m,k);
  for i:=1 to m do
   begin
    read(a[i],b[i],c[i]);
    d[i]:=i;
   end;
  sort(1,m);

  for i:=1 to m do
   for j:=0 to 100 do
    begin
     if a[i]+j>b[i] then break;
     v[1,i,j]:=a[i]+j;
    end;

  for i:=1 to n-1 do
   for x:=1 to m-1 do
    for y:=x+1 to m do
     for j:=0 to 100 do
      begin

       if v[i,x,j]=0 then continue;
       if c[y]<=c[x] then continue;

       t:=v[i,x,j];

       g:=a[x]+int64(j);

       k1:=g+k;
       k2:=g*k;

       if (a[y]<=k1) and
          (k1<=b[y]) then begin
                           if k1+t>v[i+1,y,k1-a[y]] then begin
                                                          v[i+1,y,k1-a[y]]:=k1+t;
                                                          w[i+1,y,k1-a[y]].x:=x;
                                                          w[i+1,y,k1-a[y]].y:=j;
                                                         end;
                          end;
       if (a[y]<=k2) and
          (k2<=b[y]) then begin
                           if k2+t>v[i+1,y,k2-a[y]] then begin
                                                          v[i+1,y,k2-a[y]]:=k2+t;
                                                          w[i+1,y,k2-a[y]].x:=x;
                                                          w[i+1,y,k2-a[y]].y:=j;
                                                         end;
                          end;
      end;

  max:=0;
  for i:=1 to m do
   for j:=0 to 100 do
    if v[n,i,j]>max then begin
                          max:=v[n,i,j];
                          x:=i;
                          y:=j;
                         end;

  if max=0 then begin
                 writeln('NO');
                 halt;
                end;

  writeln('YES');
  tt(n,x,y);
 end.

program Csgd;
const
  fi = '';
  fo = '';
  nmax = 50;
  dmax = 100;
type
  mon = record
    a, b: int64;
    c: longint;
    i : longint;
    end;
var
  a: array[1..nmax, 1..nmax, 0..dmax] of int64;
  t, p: array[1..nmax, 1..nmax, 0..dmax] of longint;
  b: array[1..nmax] of mon;
  n, m, k: longint;
  sum: int64;
  mi, ai: longint;
  f1, f2: text;

  procedure openf;
  begin
    assign(f1, fi); reset(f1);
    assign(f2, fo); rewrite(f2);
  end;

  procedure closef;
  begin
    close(f1); close(f2);
  end;

  procedure input;
  var
    i : longint;
  begin
    readln(f1, n, m, k);
    for i := 1 to m do
      begin
        readln(f1, b[i].a, b[i].b, b[i].c);
        b[i].i := i;
        b[i].b := b[i].b - b[i].a;
      end;
  end;

  procedure output;
  var
    i, tmp : longint;
  begin
    if sum = 0 then
      write(f2, 'NO')
    else
      begin
        writeln(f2, 'YES');
        for i := 1 to n do
          begin
            writeln(b[mi].i, ' ', ai + b[mi].a);
            tmp := t[i, mi, ai];
            ai := p[i, mi, ai];
            mi := tmp;
          end;
      end;
  end;

  procedure sort(i, j: longint);
  var
    l, r: longint;
    x: longint;
    y: mon;
  begin
    l := i;
    r := j;
    x := b[random(r - l + 1) + l].c;
    repeat
      while b[l].c < x do inc(l);
      while b[r].c > x do dec(r);
      if l <= r then
        begin
          if l < r then
            begin
              y := b[l];
              b[l] := b[r];
              b[r] := y;
            end;
          inc(l);
          dec(r);
        end;
    until l > r;
    if l < j then sort(l, j);
    if i < r then sort(i, r);
  end;

  procedure process;
  var
    i, j, l, u, v: longint;
    ttt, tmp: int64;
  begin
    sort(1, m);
    for j := 1 to m do
      for l := 0 to b[j].b do
        a[n, j, l] := b[j].a + l;
    for i := n - 1 downto 1 do
      for j := m downto i do
        for l := 0 to b[j].b do
          begin
            tmp := 0;
            for u := m downto j + 1 do
              if b[j].c < b[u].c then
                begin
                  ttt := (b[j].a + l) + k - (b[u].a);
                  v := ttt;
                  if (v >= 0) and (v <= b[u].b) and (tmp < a[i + 1, u, v]) then
                    begin
                      tmp := a[i + 1, u, v];
                      t[i, j, l] := u;
                      p[i, j, l] := v;
                    end;
                  ttt := (b[j].a + l) * k - (b[u].a);
                  v := ttt;
                  if (v >= 0) and (v <= b[u].b) and (tmp < a[i + 1, u, v]) then
                    begin
                      tmp := a[i + 1, u, v];
                      t[i, j, l] := u;
                      p[i, j, l] := v;
                    end;
                end;
            if tmp > 0 then
              a[i, j, l] := tmp + b[j].a + l
            else
              a[i, j, l] := 0;
          end;
    sum := 0;
    for j := 1 to m do
      for l := 0 to b[j].b do
        if sum < a[1, j, l] then
          begin
            sum := a[1, j, l];
            mi := j;
            ai := l;
          end;
  end;

begin
  openf;
  input;
  process;
  output;
  closef;
end.