Katya recently started to invent programming tasks and prepare her own contests. What she does not like is boring and simple constraints. Katya is fed up with all those "N does not exceed a thousand" and "the sum of ai does not exceed a million" and she decided to come up with something a little more complicated.The last problem written by Katya deals with strings. The input is a string of small Latin letters. To make the statement longer and strike terror into the people who will solve the contest, Katya came up with the following set of k restrictions of the same type (characters in restrictions can be repeated and some restrictions may contradict each other):   The number of characters c1 in a string is not less than l1 and not more than r1.  ...  The number of characters ci in a string is not less than li and not more than ri.  ...  The number of characters ck in a string is not less than lk and not more than rk. However, having decided that it is too simple and obvious, Katya added the following condition: a string meets no less than L and not more than R constraints from the above given list.Katya does not like to compose difficult and mean tests, so she just took a big string s and wants to add to the tests all its substrings that meet the constraints. However, Katya got lost in her conditions and asked you to count the number of substrings of the string s that meet the conditions (each occurrence of the substring is counted separately).

The first line contains a non-empty string s, consisting of small Latin letters. The length of the string s does not exceed 105. The second line contains three space-separated integers k, L and R (0 ≤ L ≤ R ≤ k ≤ 500). Next k lines contain Katya's constrictions in the following form "ci li ri". All letters ci are small Latin letters, li and ri are integers (0 ≤ li ≤ ri ≤ |s|, where |s| is the length of string s). Letters ci are not necessarily different.

Print a single number — the number of substrings that meet the constrictions. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cout stream or the %I64d specificator.

NoteIn the first test we should count the number of strings that do not contain characters "e" and "o". All such strings are as follows (in the order of occurrence in the initial string from the left to the right): "c", "d"', "f", "r", "rc", "c", "s".In the second test we cannot achieve fulfilling exactly one of the two identical constrictions, so the answer is 0.

program cf138e;
var
  ct:array[0..100000]of longint;
  l,r,mk,lf,rf:array[0..1000]of longint;
  ss:array[0..100000]of char;
  s:array[0..100000,0..26]of longint;
  n,m,ll,rr,i,j,h,ctt:longint;
  ans:int64;
procedure init;
var i,j,h:longint;
begin
  n:=0;
  while not(eoln) do begin
    inc(n);
    read(ss[n]);
  end;      readln;

  for i:=0 to 26 do s[0,i]:=0;
  for i:=1 to n do begin
    for j:=0 to 26 do s[i,j]:=s[i-1,j];
    inc(s[i,ord(ss[i])-ord('a')]);
  end;

  readln(m,ll,rr);
  for i:=1 to m do begin
    read(ss[1]);
    mk[i]:=ord(ss[1])-ord('a');
    readln(l[i],r[i]);
  end;
end;

procedure main;
var
  i,j,h:longint;
  kind:boolean;
begin
  ans:=0;
  ctt:=0;
  for i:=1 to m do begin
    rf[i]:=0;
    lf[i]:=1;
  end;

 { for i:=1 to m do lf[i]:=1;
  fillchar(rf,sizeof(rf),0);     }
  for i:=1 to n do ct[i]:=0;
  for h:=1 to n do begin
    if ll=0 then
      inc(ctt,1);
    for i:=1 to m do begin
      while (rf[i]<h)and(s[h,mk[i]]-s[rf[i],mk[i]]>=l[i]) do begin
        if (ct[rf[i]+1]<=rr)and(ll<=ct[rf[i]+1]) then kind:=true
        else kind:=false;
        inc(ct[rf[i]+1]);
        if (not(kind))and(ct[rf[i]+1]<=rr)and(ct[rf[i]+1]>=ll) then
          inc(ctt);
        if (kind)and(not((ct[rf[i]+1]<=rr)and(ct[rf[i]+1]>=ll))) then
          dec(ctt);
        inc(rf[i]);
      end;

      while (lf[i]<=h)and(s[h,mk[i]]-s[lf[i]-1,mk[i]]>r[i]) do begin
        if (ct[lf[i]]<=rr)and(ll<=ct[lf[i]]) then kind:=true
        else kind:=false;
        dec(ct[lf[i]]);
        if (not(kind))and(ct[lf[i]]<=rr)and(ct[lf[i]]>=ll) then
          inc(ctt);
        if (kind)and(not((ct[lf[i]]<=rr)and(ct[lf[i]]>=ll))) then
          dec(ctt);
        inc(lf[i]);
      end;

    end;

    ans:=ans+ctt;
  end;
  writeln(ans);
end;

begin
  init;
  main;
end.

Program Helish_Constraints;
const maxn=100000;
var n,m,i,j,z,x,y,tot,lt,rt:longint;
    ans:int64;
    c:char;
    s:array[1..maxn]of char;
    a:array[1..maxn]of longint;
    ch:array[1..500]of char;
    u,v:array[1..500]of longint;
    p:array['a'..'z',0..maxn]of longint;
    r:array['a'..'z']of longint;
//==============================================================
procedure Init;
begin
  repeat
    inc(n);
    read(s[n]);
  until eoln;
  readln(m,x,y);
  for i:=1 to m do
    begin
      read(ch[i]);
      readln(u[i],v[i]);
    end;
end;
//==============================================================
procedure Modify(k,d:longint);
begin
  if d=1 then
    begin
      if a[k]=x-1 then inc(tot);
      if a[k]=y then dec(tot);
    end
  else
    begin
      if a[k]=x then dec(tot);
      if a[k]=y+1 then inc(tot);
    end;
  a[k]:=a[k]+d;
end;
//==============================================================
procedure Work;
begin
  c:=s[z];inc(r[c]);
  p[c,r[c]]:=z;
  if x=0 then inc(tot);
  for i:=1 to m do
    if ch[i]=c then
      begin
        if r[c]>v[i] then
          begin
            lt:=p[c,r[c]-v[i]-1]+1;
            rt:=p[c,r[c]-v[i]];
            for j:=lt to rt do Modify(j,-1);
          end;

        if (u[i]=0) then
          begin
            lt:=z;rt:=z;
          end
        else
          if r[c]>=u[i] then
            begin
              lt:=p[c,r[c]-u[i]]+1;
              rt:=p[c,r[c]-u[i]+1];
            end
        else rt:=lt-1;
        for j:=lt to rt do Modify(j,1);
      end
    else
      if u[i]=0 then Modify(z,1);
  ans:=ans+tot;
end;
//==============================================================
begin
  Init;
  for z:=1 to n do Work;
  writeln(ans);
end.

Program Helish_Constraints;
const maxn=100000;
var n,m,i,j,z,x,y,tot,lt,rt:longint;
    ans:int64;
    c:char;
    s:array[1..maxn]of char;
    a:array[1..maxn]of longint;
    ch:array[1..500]of char;
    u,v:array[1..500]of longint;
    p:array['a'..'z',0..maxn]of longint;
    r:array['a'..'z']of longint;
//==============================================================
procedure Init;
begin
  repeat
    inc(n);
    read(s[n]);
  until eoln;
  readln(m,x,y);
  for i:=1 to m do
    begin
      read(ch[i]);
      readln(u[i],v[i]);
    end;
end;
//==============================================================
procedure Modify(k,d:longint);
begin
  if d=1 then
    begin
      if a[k]=x-1 then inc(tot);
      if a[k]=y then dec(tot);
    end
  else
    begin
      if a[k]=x then dec(tot);
      if a[k]=y+1 then inc(tot);
    end;
  a[k]:=a[k]+d;
end;
//==============================================================
procedure Work;
begin
  c:=s[z];inc(r[c]);
  p[c,r[c]]:=z;
  if x=0 then inc(tot);
  for i:=1 to m do
    if ch[i]=c then
      begin
        if r[c]>v[i] then
          begin
            lt:=p[c,r[c]-v[i]-1]+1;
            rt:=p[c,r[c]-v[i]];
            for j:=lt to rt do Modify(j,-1);
          end;

        if (u[i]=0) then
          begin
            lt:=z;rt:=z;
          end
        else
          if r[c]>=u[i] then
            begin
              lt:=p[c,r[c]-u[i]]+1;
              rt:=p[c,r[c]-u[i]+1];
            end
        else rt:=lt-1;
        for j:=lt to rt do Modify(j,1);
      end
    else
      if u[i]=0 then Modify(z,1);
  ans:=ans+tot;
end;
//==============================================================
begin
//
  Init;
  for z:=1 to n do Work;
  writeln(ans);
end.

var
s:array[0..100010] of char;
sum:array[0..100010,'a'..'z'] of longint;
left,right,meet:array[0..100010] of longint;
letter:array[0..510] of char;
l,r:array[0..510] of longint;
uplim,downlim:longint;

function check(x:longint):boolean; begin exit((downlim<=x)and(x<=uplim)); end;

procedure lemon;
var n,i,j,count,Q,tmp:longint;
    ans:int64;
    ch:char;
begin n:=0;
	while not seekeoln(input) do
	begin read(ch);
		inc(n); s[n]:=ch;
	end;
	for ch:='a' to 'z' do sum[0,ch]:=0;
	for i:=1 to n do
	begin sum[i]:=sum[i-1];
		inc(sum[i,s[i]]);
	end;
	readln(Q,downlim,uplim);
	for i:=1 to Q do
	begin readln(letter[i],l[i],r[i]);
		if l[i]>r[i] then writeln('no');
	end;
	fillchar(meet,sizeof(meet),0);
	for i:=1 to Q do begin left[i]:=1;  right[i]:=0; end;
	ans:=0; count:=0;
	for i:=1 to n do
      begin for j:=1 to Q do
		begin //[left[j]~right[j],i] is available interval
			if sum[i,letter[j]]-sum[left[j]-1,letter[j]]<l[j] then continue;
			while (right[j]<i)and(sum[i,letter[j]]-sum[right[j],letter[j]]>=l[j]) do
			begin inc(right[j]);
				tmp:=count;
				if check(meet[right[j]]) then dec(count);
				inc(meet[right[j]]);
				if check(meet[right[j]]) then inc(count);
			end;
			while (left[j]<=i)and(sum[i,letter[j]]-sum[left[j]-1,letter[j]]>r[j]) do
			begin tmp:=count;
				if check(meet[left[j]]) then dec(count);
				dec(meet[left[j]]);
				if check(meet[left[j]]) then inc(count);
				inc(left[j]);
			end;
		end;
		//for j:=1 to Q do writeln(left[j],' ',right[j]);
		if downlim=0 then inc(count);
            inc(ans,count);
	end;
	writeln(ans);
end;

begin
{$ifndef ONLINE_JUDGE}
	assign(input,'138E.in');
	reset(input);
{$endif}
lemon;
end.

Program cf99e;
Var ss:ansistring;
    total:array['a'..'z'] of longint;
    place:array['a'..'z',0..100000] of longint;
    cc,s:array[1..100000] of char;
    cl,cr:array[1..100000] of longint;
    a,c:array[1..100000] of longint;
    ans:int64;
    d,i,j,k,left,right,n,m,cur,std:longint;
begin
  readln(ss);
  n:=length(ss);
  for i:=1 to n do
    begin
      s[i]:=ss[i];
      inc(total[s[i]]);
      c[i]:=total[s[i]];
      place[s[i],total[s[i]]]:=i;
    end;
  readln(m,left,right);
  for i:=1 to m do
    readln(cc[i],cl[i],cr[i]);
  cur:=0;
  for i:=1 to m do if cl[i]=0 then inc(std);
  for d:=1 to n do
    begin
      a[d]:=std;
      if (left<=a[d]) and (a[d]<=right) then inc(cur);
      for k:=1 to m do
        if cc[k]=s[d] then
          begin
            if c[d]-cr[k]-1>=0 then
              begin
                for i:=place[s[d],c[d]-cr[k]-1]+1 to place[s[d],c[d]-cr[k]] do
                  begin
                    dec(a[i]);
                    if a[i]=left-1 then cur:=cur-1;
                    if a[i]=right then cur:=cur+1;
                  end;
              end;
            if cl[k]<>0 then
            if c[d]-cl[k]>=0 then
              begin
                for i:=place[s[d],c[d]-cl[k]]+1 to place[s[d],c[d]-cl[k]+1] do
                  begin
                    inc(a[i]);
                    if a[i]=right+1 then cur:=cur-1;
                    if a[i]=left then cur:=cur+1;
                  end;
              end;
          end;
      ans:=ans+cur;
    end;
  writeln(ans);
end.

{$APPTYPE CONSOLE}
{$R-,Q-,O+,S-}
var
  n, m, i, j, q, x, y, lf, rg : longint;
  ans, cur : int64;
  c : char;
  s : string;
  op, opa : array [-1..1] of longint;
  a : array [1..1000010] of longint;
  ch : array [1..510] of char;
  u, v : array [1..510] of longint;
  p : array ['a'..'z', 0..100010] of longint;
  r : array ['a'..'z'] of longint;
procedure upd(k, d : longint);
begin
  if a[k] = x - 1 + op[d] then
    inc(cur, opa[d]);
  if a[k] = y + op[d] then
    dec(cur, opa[d]);
  inc(a[k], d);
end;
begin
 // reset(input, 'input.txt');
 // Rewrite(output, 'output.txt');
  Readln(s);
  ans := 0;
  lf := 0;
  op[1] := 0;
  op[-1] := 1;
  opa[1] := 1;
  opa[-1] := -1;
  n := Length(s);
  readln(m, x, y);
  for i := 1 to m do
    readln(ch[i], u[i], v[i]);
  for q := 1 to n do
    begin
      c := s[q];
      inc(r[c]);
      p[c, r[c]] := q;
      if x = 0 then
        inc(cur);
      for i := 1 to m do
        if ch[i] = c then
          begin
            if r[c] > v[i] then
              begin
                lf := p[c, r[c] - v[i] - 1] + 1;
                rg := p[c, r[c] - v[i]];
                for j := lf to rg do
                  upd(j, -1);
              end;
            rg := lf - 1;
            if u[i] = 0 then
              begin
                lf := q;
                rg := q;
              end
            else
              if r[c] >= u[i] then
                begin
                  lf := p[c, r[c] - u[i]] + 1;
                  rg := p[c, r[c] - u[i] + 1];
                end;
            for j := lf to rg do
              upd(j, 1);
          end
        else
          if u[i] = 0 then
            upd(q, 1);
      ans := ans + cur;
    end;
  writeln(ans);
end.

Program Helish_Constraints;
const maxn=100000;
      q=511;
var n,m,i,j,z,x,y,tot,lt,rt:longint;
    ans:int64;
    c:char;
    s:array[1..maxn]of char;
    a:array[1..maxn]of longint;
    ch:array[1..500]of char;
    u,v:array[1..500]of longint;
    p:array['a'..'z',0..q]of longint;
    r:array['a'..'z']of longint;
//==============================================================
procedure Init;
begin
  repeat
    inc(n);
    read(s[n]);
  until eoln;
  readln(m,x,y);
  for i:=1 to m do
    begin
      read(ch[i]);
      readln(u[i],v[i]);
    end;
end;
//==============================================================
procedure Work;
begin
  c:=s[z];
  r[c]:=(r[c]+1) and q;
  p[c,r[c]]:=z;
  if x=0 then inc(tot);
  for i:=1 to m do
    if ch[i]=c then
      begin
        lt:=p[c,(r[c]-v[i]-1)and q]+1;
        rt:=p[c,(r[c]-v[i])and q];
        for j:=lt to rt do
          begin
            if a[j]=x then dec(tot);
            if a[j]=y+1 then inc(tot);
            dec(a[j]);
          end;
        if (u[i]=0)and(v[i]>0) then
          begin
            lt:=z;rt:=z;
          end
        else
          begin
            lt:=p[c,(r[c]-u[i])and q]+1;
            rt:=p[c,(r[c]-u[i]+1)and q];
          end;
        for j:=lt to rt do
          begin
            if a[j]=x-1 then inc(tot);
            if a[j]=y then dec(tot);
            inc(a[j]);
          end;
      end
    else
      if u[i]=0 then
        begin
          if a[z]=x-1 then inc(tot);
          if a[z]=y then dec(tot);
          inc(a[z]);
        end;
  ans:=ans+tot;
end;
//==============================================================
begin
  Init;
  for z:=1 to n do Work;
  writeln(ans);
end.

Program Helish_Constraints;
const maxn=100000;
      q=511;
var n,m,i,j,z,x,y,tot,lt,rt:longint;
    ans:int64;
    c:char;
    s:array[1..maxn]of char;
    a:array[1..maxn]of longint;
    ch:array[1..500]of char;
    u,v:array[1..500]of longint;
    p:array['a'..'z',0..q]of longint;
    r:array['a'..'z']of longint;
//==============================================================
procedure Init;
begin
  repeat
    inc(n);
    read(s[n]);
  until eoln;
  readln(m,x,y);
  for i:=1 to m do
    begin
      read(ch[i]);
      readln(u[i],v[i]);
    end;
end;
//==============================================================
procedure Work;
begin
  c:=s[z];
  r[c]:=(r[c]+1) and q;
  p[c,r[c]]:=z;
  if x=0 then inc(tot);
  for i:=1 to m do
    if ch[i]=c then
      begin
        if u[i]=0 then
          begin
            lt:=p[c,(r[c]-v[i]-2)and q]+1;
            rt:=p[c,(r[c]-v[i]-1)and q];
          end
        else
          begin
            lt:=p[c,(r[c]-v[i]-1)and q]+1;
            rt:=p[c,(r[c]-v[i])and q];
          end;
        for j:=lt to rt do
          begin
            if a[j]=x then dec(tot);
            if a[j]=y+1 then inc(tot);
            dec(a[j]);
          end;

        if (u[i]=0)and(v[i]>0) then
          begin
            lt:=z;rt:=z;
          end
        else
          begin
            lt:=p[c,(r[c]-u[i])and q]+1;
            rt:=p[c,(r[c]-u[i]+1)and q];
          end;
        for j:=lt to rt do
          begin
            if a[j]=x-1 then inc(tot);
            if a[j]=y then dec(tot);
            inc(a[j]);
          end;
      end
    else
      if u[i]=0 then
        begin
          if a[z]=x-1 then inc(tot);
          if a[z]=y then dec(tot);
          inc(a[z]);
        end;
  ans:=ans+tot;
end;
//==============================================================
begin
  Init;
  for z:=1 to n do Work;
  writeln(ans);
end.

Program Helish_Constraints;
const maxn=100000;
      q=511;
var n,m,i,j,z,x,y,tot,lt,rt:longint;
    ans:int64;
    c:char;
    s:array[1..maxn]of char;
    a:array[1..maxn]of longint;
    ch:array[1..500]of char;
    u,v:array[1..500]of longint;
    p:array['a'..'z',0..q]of longint;
    r:array['a'..'z']of longint;
//==============================================================
procedure Init;
begin
  repeat
    inc(n);
    read(s[n]);
  until eoln;
  readln(m,x,y);
  for i:=1 to m do
    begin
      read(ch[i]);
      readln(u[i],v[i]);
    end;
end;
//==============================================================
procedure Work;
begin
  c:=s[z];
  r[c]:=(r[c]+1) and q;
  p[c,r[c]]:=z;
  if x=0 then inc(tot);
  for i:=1 to m do
    if ch[i]=c then
      begin
        if u[i]=0 then
          begin
            lt:=p[c,(r[c]-v[i]-2)and q]+1;
            if v[i]=0 then rt:=z
                      else rt:=p[c,(r[c]-v[i]-1)and q];
          end
        else
          begin
            lt:=p[c,(r[c]-v[i]-1)and q]+1;
            rt:=p[c,(r[c]-v[i])and q];
          end;

        for j:=lt to rt do
          begin
            if a[j]=x then dec(tot);
            if a[j]=y+1 then inc(tot);
            dec(a[j]);
          end;

        if (u[i]=0)and(v[i]>0) then
          begin
            lt:=z;rt:=z;
          end
        else
          begin
            lt:=p[c,(r[c]-u[i])and q]+1;
            rt:=p[c,(r[c]-u[i]+1)and q];
          end;
        for j:=lt to rt do
          begin
            if a[j]=x-1 then inc(tot);
            if a[j]=y then dec(tot);
            inc(a[j]);
          end;
      end
    else
      if u[i]=0 then
        begin
          if a[z]=x-1 then inc(tot);
          if a[z]=y then dec(tot);
          inc(a[z]);
        end;
  ans:=ans+tot;
end;
//==============================================================
begin
  Init;
  for z:=1 to n do Work;
  writeln(ans);
end.

Program Helish_Constraints;
const maxn=100000;
      q=511;
var n,m,i,j,z,x,y,tot,lt,rt:longint;
    ans:int64;
    c:char;
    s:array[1..maxn]of char;
    a:array[1..maxn]of longint;
    ch:array[1..500]of char;
    u,v:array[1..500]of longint;
    p:array['a'..'z',0..q]of longint;
    r:array['a'..'z']of longint;
//==============================================================
procedure Init;
begin
  repeat
    inc(n);
    read(s[n]);
  until eoln;
  readln(m,x,y);
  for i:=1 to m do
    begin
      read(ch[i]);
      readln(u[i],v[i]);
    end;
end;
//==============================================================
procedure Modify(k,d:longint);
begin
  if d=1 then
    begin
      if a[k]=x-1 then inc(tot);
      if a[k]=y then dec(tot);
    end
  else
    begin
      if a[k]=x then dec(tot);
      if a[k]=y+1 then inc(tot);
    end;
  a[k]:=a[k]+d;
end;
//==============================================================
procedure Work;
begin
  c:=s[z];
  r[c]:=(r[c]+1) and q;
  p[c,r[c]]:=z;
  if x=0 then inc(tot);
  for i:=1 to m do
    if ch[i]=c then
      begin
        if p[c,(r[c]-v[i])and q]>0 then
          begin
            if u[i]=0 then
              begin
                lt:=p[c,(r[c]-v[i]-2)and q]+1;
                if v[i]=0 then rt:=z
                          else rt:=p[c,(r[c]-v[i]-1)and q];
              end
            else
              begin
                lt:=p[c,(r[c]-v[i]-1)and q]+1;
                rt:=p[c,(r[c]-v[i])and q];
              end;
            for j:=lt to rt do Modify(j,-1);
          end;

        if (u[i]=0)and(v[i]>0) then
          begin
            lt:=z;rt:=z;
          end
        else
          begin
            lt:=p[c,(r[c]-u[i])and q]+1;
            rt:=p[c,(r[c]-u[i]+1)and q];
          end;
        for j:=lt to rt do Modify(j,1);
      end
    else
      if u[i]=0 then Modify(z,1);
  ans:=ans+tot;
end;
//==============================================================
begin
  Init;
  for z:=1 to n do Work;
  writeln(ans);
end.

Program Helish_Constraints;
const maxn=100000;
      q=511;
var n,m,i,j,z,x,y,tot,lt,rt:longint;
    ans:int64;
    c:char;
    s:array[1..maxn]of char;
    a:array[1..maxn]of longint;
    ch:array[1..500]of char;
    u,v:array[1..500]of longint;
    p:array['a'..'z',0..q]of longint;
    r:array['a'..'z']of longint;
//==============================================================
procedure Init;
begin
  repeat
    inc(n);
    read(s[n]);
  until eoln;
  readln(m,x,y);
  for i:=1 to m do
    begin
      read(ch[i]);
      readln(u[i],v[i]);
    end;
end;
//==============================================================
procedure Modify(k,d:longint);
begin
  if d=1 then
    begin
      if a[k]=x-1 then inc(tot);
      if a[k]=y then dec(tot);
    end
  else
    begin
      if a[k]=x then dec(tot);
      if a[k]=y+1 then inc(tot);
    end;
  a[k]:=a[k]+d;
end;
//==============================================================
procedure Work;
begin
  c:=s[z];
  r[c]:=(r[c]+1) and q;
  p[c,r[c]]:=z;
  if x=0 then inc(tot);
  for i:=1 to m do
    if ch[i]=c then
      begin
        if p[c,(r[c]-v[i])and q]>0 then
          begin
            lt:=p[c,(r[c]-v[i]-1)and q]+1;
            rt:=p[c,(r[c]-v[i])and q];
            for j:=lt to rt do Modify(j,-1);
          end;

        if (u[i]=0) then
          begin
            lt:=z;rt:=z;
          end
        else
          begin
            lt:=p[c,(r[c]-u[i])and q]+1;
            rt:=p[c,(r[c]-u[i]+1)and q];
          end;
        for j:=lt to rt do Modify(j,1);
      end
    else
      if u[i]=0 then Modify(z,1);
  ans:=ans+tot;
end;
//==============================================================
begin
  Init;
  for z:=1 to n do Work;
  writeln(ans);
end.

var
s:array[0..100010] of char;
sum:array[0..100010,'a'..'z'] of longint;
left,right,meet:array[0..100010] of longint;
letter:array[0..510] of char;
l,r:array[0..510] of longint;
uplim,downlim:longint;

function check(x:longint):boolean; begin exit((downlim<=x)and(x<=uplim)); end;

procedure lemon;
var n,i,j,count,Q,tmp:longint;
    ans:int64;
    ch:char;
begin n:=0;
	while not seekeoln(input) do
	begin read(ch);
		inc(n); s[n]:=ch;
	end;
	for ch:='a' to 'z' do sum[0,ch]:=0;
	for i:=1 to n do
	begin sum[i]:=sum[i-1];
		inc(sum[i,s[i]]);
	end;
	readln(Q,downlim,uplim);
	for i:=1 to Q do
	begin readln(letter[i],l[i],r[i]);
		if l[i]>r[i] then writeln('no');
	end;
	fillchar(meet,sizeof(meet),0);
	for i:=1 to n do begin left[i]:=1;  right[i]:=0; end;
	ans:=0; count:=0;
	for i:=1 to n do
      begin for j:=1 to Q do
		begin //[left[j]~right[j],i] is available interval
			if sum[i,letter[j]]-sum[left[j]-1,letter[j]]<l[j] then continue;
			while (right[j]<i)and(sum[i,letter[j]]-sum[right[j],letter[j]]>=l[j]) do
			begin inc(right[j]);
				tmp:=count;
				if check(meet[right[j]]) then dec(count);
				inc(meet[right[j]]);
				if check(meet[right[j]]) then inc(count);
				if (s[1]='l')and(s[2]='w')and(s[3]='x') then if tmp<>count then writeln('NOO');
			end;
			while (left[j]<=i)and(sum[i,letter[j]]-sum[left[j]-1,letter[j]]>r[j]) do
			begin tmp:=count;
				if check(meet[left[j]]) then dec(count);
				dec(meet[left[j]]);
				if check(meet[left[j]]) then inc(count);
				if (s[1]='l')and(s[2]='w')and(s[3]='x') then if tmp<>count then writeln('NOpO',' ',j,' ',letter[j],' ',l[j],' ',r[j],' ',left[j],' ',meet[left[j]]);
				inc(left[j]);
			end;
		end;
		//for j:=1 to Q do writeln(left[j],' ',right[j]);
		if downlim=0 then inc(count);
            if (s[1]='l')and(s[2]='w')and(s[3]='x') then if count<>i then writeln(i,' ',count);
		inc(ans,count);
	end;
	writeln(ans);
end;

begin
{$ifndef ONLINE_JUDGE}
	assign(input,'138E.in');
	reset(input);
{$endif}
lemon;
end.

var
s:array[0..100010] of char;
sum:array[0..100010,'a'..'z'] of longint;
left,right,meet:array[0..100010] of longint;
letter:array[0..510] of char;
l,r:array[0..510] of longint;
uplim,downlim:longint;

function check(x:longint):boolean; begin exit((downlim<=x)and(x<=uplim)); end;

procedure lemon;
var n,i,j,count,Q:longint;
    ans:int64;
    ch:char;
begin n:=0;
	while not seekeoln(input) do
	begin read(ch);
		inc(n); s[n]:=ch;
	end;
	for ch:='a' to 'z' do sum[0,ch]:=0;
	for i:=1 to n do
	begin sum[i]:=sum[i-1];
		inc(sum[i,s[i]]);
	end;
	readln(Q,downlim,uplim);
	if (s[1]='l')and(s[2]='w')and(s[3]='x') then writeln(Q,' ',downlim,' ',uplim);
	for i:=1 to Q do
	begin readln(letter[i],l[i],r[i]);
		if l[i]>r[i] then writeln('no');
	end;
	fillchar(meet,sizeof(meet),0);
	for i:=1 to n do begin left[i]:=1;  right[i]:=0; end;
	ans:=0; count:=0;
	for i:=1 to n do
      begin for j:=1 to Q do
		begin //[left[j]~right[j],i] is available interval
			if sum[i,letter[j]]-sum[left[j]-1,letter[j]]<l[j] then continue;
			while (right[j]<i)and(sum[i,letter[j]]-sum[right[j],letter[j]]>=l[j]) do
			begin inc(right[j]);
				if check(meet[right[j]]) then dec(count);
				inc(meet[right[j]]);
				if check(meet[right[j]]) then inc(count);
			end;
			while (left[j]<=i)and(sum[i,letter[j]]-sum[left[j]-1,letter[j]]>r[j]) do
			begin if check(meet[left[j]]) then dec(count);
				dec(meet[left[j]]);
				if check(meet[left[j]]) then inc(count);
				inc(left[j]);
			end;
		end;
		//for j:=1 to Q do writeln(left[j],' ',right[j]);
		if downlim=0 then inc(count);
            //writeln(count);
		inc(ans,count);
	end;
	writeln(ans);
end;

begin
{$ifndef ONLINE_JUDGE}
	assign(input,'138E.in');
	reset(input);
{$endif}
lemon;
end.

var
s:array[0..100010] of char;
sum:array[0..100010,'a'..'z'] of longint;
left,right,meet:array[0..100010] of longint;
letter:array[0..510] of char;
l,r:array[0..510] of longint;
uplim,downlim:longint;

function check(x:longint):boolean; begin exit((downlim<=x)and(x<=uplim)); end;

procedure lemon;
var n,i,j,count,Q:longint;
    ans:int64;
    ch:char;
begin n:=0;
	while not seekeoln(input) do
	begin read(ch);
		inc(n); s[n]:=ch;
	end;
	for ch:='a' to 'z' do sum[0,ch]:=0;
	for i:=1 to n do
	begin sum[i]:=sum[i-1];
		inc(sum[i,s[i]]);
	end;
	readln(Q,downlim,uplim);
	for i:=1 to Q do
	begin readln(letter[i],l[i],r[i]);
		if l[i]>r[i] then writeln('no');
	end;
	fillchar(meet,sizeof(meet),0);
	for i:=1 to n do begin left[i]:=1;  right[i]:=0; end;
	ans:=0; count:=0;
	for i:=1 to n do
      begin for j:=1 to Q do
		begin //[left[j]~right[j],i] is available interval
			if sum[i,letter[j]]-sum[left[j]-1,letter[j]]<l[j] then continue;
			while (right[j]<i)and(sum[i,letter[j]]-sum[right[j],letter[j]]>=l[j]) do
			begin inc(right[j]);
				if check(meet[right[j]]) then dec(count);
				inc(meet[right[j]]);
				if check(meet[right[j]]) then inc(count);
			end;
			while (left[j]<=right[j])and(sum[i,letter[j]]-sum[left[j]-1,letter[j]]>r[j]) do
			begin if check(meet[left[j]]) then dec(count);
				dec(meet[left[j]]);
				if check(meet[left[j]]) then inc(count);
				inc(left[j]);
			end;
		end;
		//for j:=1 to Q do writeln(left[j],' ',right[j]);
		if downlim=0 then inc(count);
            //writeln(count);
		inc(ans,count);
	end;
	writeln(ans);
end;

begin
{$ifndef ONLINE_JUDGE}
	assign(input,'138E.in');
	reset(input);
{$endif}
lemon;
end.

var
s:array[0..100010] of char;
sum:array[0..100010,'a'..'z'] of longint;
left,right,meet:array[0..100010] of longint;
letter:array[0..510] of char;
l,r:array[0..510] of longint;
uplim,downlim:longint;

function check(x:longint):boolean; begin exit((downlim<=x)and(x<=uplim)); end;

procedure lemon;
var n,i,j,count,Q:longint;
    ans:int64;
    ch:char;
begin n:=0;
	while not seekeoln(input) do
	begin read(ch);
		inc(n); s[n]:=ch;
	end;
	for ch:='a' to 'z' do sum[0,ch]:=0;
	for i:=1 to n do
	begin sum[i]:=sum[i-1];
		inc(sum[i,s[i]]);
	end;
	readln(Q,downlim,uplim);
	for i:=1 to Q do readln(letter[i],l[i],r[i]);
	fillchar(meet,sizeof(meet),0);
	for i:=1 to n do begin left[i]:=0;  right[i]:=0; end;
	ans:=0; count:=0;
	for i:=1 to n do
      begin for j:=1 to Q do
		begin //[left[j]~right[j],i] is available interval
			while (right[j]<i)and(sum[i,letter[j]]-sum[right[j],letter[j]]>=l[i]) do
			begin inc(right[j]);
				if check(meet[right[j]]) then dec(count);
				inc(meet[right[j]]);
				if check(meet[right[j]]) then inc(count);
			end;
			while (left[j]<=right[j])and(sum[i,letter[j]]-sum[left[j]-1,letter[j]]>r[i]) do
			begin if check(meet[left[j]]) then dec(count);
				dec(meet[left[j]]);
				if check(meet[left[j]]) then inc(count);
				inc(left[j]);
			end;
		end;
		if (downlim=0)and(meet[i]=0) then inc(count);
            writeln(count);
		inc(ans,count);
	end;
	writeln(ans);
end;

begin
{$ifndef ONLINE_JUDGE}
	assign(input,'138E.in');
	reset(input);
{$endif}
lemon;
end.

var
s:array[0..100010] of char;
sum:array[0..100010,'a'..'z'] of longint;
left,right,meet:array[0..100010] of longint;
letter:array[0..510] of char;
l,r:array[0..510] of longint;
uplim,downlim:longint;

function check(x:longint):boolean; begin exit((downlim<=x)and(x<=uplim)); end;

procedure lemon;
var n,i,j,count,Q:longint;
    ans:int64;
    ch:char;
begin n:=0;
	while not seekeoln(input) do
	begin read(ch);
		inc(n); s[n]:=ch;
	end;
	for ch:='a' to 'z' do sum[0,ch]:=0;
	for i:=1 to n do
	begin sum[i]:=sum[i-1];
		inc(sum[i,s[i]]);
	end;
	readln(Q,downlim,uplim);
	for i:=1 to Q do readln(letter[i],l[i],r[i]);
	fillchar(meet,sizeof(meet),0);
	for i:=1 to n do begin left[i]:=1;  right[i]:=0; end;
	ans:=0; count:=0;
	for i:=1 to n do
      begin for j:=1 to Q do
		begin //[left[j]~right[j],i] is available interval
			if sum[i,letter[j]]-sum[left[j]-1,letter[j]]<l[j] then continue;
			while (right[j]<i)and(sum[i,letter[j]]-sum[right[j],letter[j]]>=l[j]) do
			begin inc(right[j]);
				if check(meet[right[j]]) then dec(count);
				inc(meet[right[j]]);
				if check(meet[right[j]]) then inc(count);
			end;
			while (left[j]<=right[j])and(sum[i,letter[j]]-sum[left[j]-1,letter[j]]>r[j]) do
			begin if check(meet[left[j]]) then dec(count);
				dec(meet[left[j]]);
				if check(meet[left[j]]) then inc(count);
				inc(left[j]);
			end;
		end;
		//for j:=1 to Q do writeln(left[j],' ',right[j]);
		if downlim=0 then inc(count);
            //writeln(count);
		inc(ans,count);
	end;
	writeln(ans);
end;

begin
{$ifndef ONLINE_JUDGE}
	assign(input,'138E.in');
	reset(input);
{$endif}
lemon;
end.

var
s:array[0..100010] of char;
sum:array[0..100010,'a'..'z'] of longint;
left,right,meet:array[0..100010] of longint;
letter:array[0..510] of char;
l,r:array[0..510] of longint;
uplim,downlim:longint;

function check(x:longint):boolean; begin exit((downlim<=x)and(x<=uplim)); end;

procedure lemon;
var n,i,j,count,Q:longint;
    ans:int64;
    ch:char;
begin n:=0;
	while not seekeoln(input) do
	begin read(ch);
		inc(n); s[n]:=ch;
	end;
	for ch:='a' to 'z' do sum[0,ch]:=0;
	for i:=1 to n do
	begin sum[i]:=sum[i-1];
		inc(sum[i,s[i]]);
	end;
	readln(Q,downlim,uplim);
	for i:=1 to Q do
	begin readln(letter[i],l[i],r[i]);
		if l[i]>r[i] then writeln('no');
	end;
	fillchar(meet,sizeof(meet),0);
	for i:=1 to n do begin left[i]:=1;  right[i]:=0; end;
	ans:=0; count:=0;
	for i:=1 to n do
      begin for j:=1 to Q do
		begin //[left[j]~right[j],i] is available interval
			if sum[i,letter[j]]-sum[left[j]-1,letter[j]]<l[j] then continue;
			while (right[j]<i)and(sum[i,letter[j]]-sum[right[j],letter[j]]>=l[j]) do
			begin inc(right[j]);
				if check(meet[right[j]]) then dec(count);
				inc(meet[right[j]]);
				if check(meet[right[j]]) then inc(count);
			end;
			while (left[j]<=i)and(sum[i,letter[j]]-sum[left[j]-1,letter[j]]>r[j]) do
			begin if check(meet[left[j]]) then dec(count);
				dec(meet[left[j]]);
				if check(meet[left[j]]) then inc(count);
				inc(left[j]);
			end;
		end;
		//for j:=1 to Q do writeln(left[j],' ',right[j]);
		if downlim=0 then inc(count);
            if (s[1]='l')and(s[2]='w')and(s[3]='x') then if count<>i then writeln(i,' ',count);
		inc(ans,count);
	end;
	writeln(ans);
end;

begin
{$ifndef ONLINE_JUDGE}
	assign(input,'138E.in');
	reset(input);
{$endif}
lemon;
end.

var
s:array[0..100010] of char;
sum:array[0..100010,'a'..'z'] of longint;
left,right,meet:array[0..100010] of longint;
letter:array[0..510] of char;
l,r:array[0..510] of longint;
uplim,downlim:longint;

function check(x:longint):boolean; begin exit((downlim<=x)and(x<=uplim)); end;

procedure lemon;
var n,i,j,count,Q,tmp:longint;
    ans:int64;
    ch:char;
begin n:=0;
	while not seekeoln(input) do
	begin read(ch);
		inc(n); s[n]:=ch;
	end;
	for ch:='a' to 'z' do sum[0,ch]:=0;
	for i:=1 to n do
	begin sum[i]:=sum[i-1];
		inc(sum[i,s[i]]);
	end;
	readln(Q,downlim,uplim);
	for i:=1 to Q do
	begin readln(letter[i],l[i],r[i]);
		if l[i]>r[i] then writeln('no');
	end;
	fillchar(meet,sizeof(meet),0);
	for i:=1 to n do begin left[i]:=1;  right[i]:=0; end;
	ans:=0; count:=0;
	for i:=1 to n do
      begin for j:=1 to Q do
		begin //[left[j]~right[j],i] is available interval
			if sum[i,letter[j]]-sum[left[j]-1,letter[j]]<l[j] then continue;
			while (right[j]<i)and(sum[i,letter[j]]-sum[right[j],letter[j]]>=l[j]) do
			begin inc(right[j]);
				tmp:=count;
				if check(meet[right[j]]) then dec(count);
				inc(meet[right[j]]);
				if check(meet[right[j]]) then inc(count);
				if (s[1]='l')and(s[2]='w')and(s[3]='x') then if tmp<>count then writeln('NOO');
			end;
			while (left[j]<=i)and(sum[i,letter[j]]-sum[left[j]-1,letter[j]]>r[j]) do
			begin tmp:=count;
				if check(meet[left[j]]) then dec(count);
				dec(meet[left[j]]);
				if check(meet[left[j]]) then inc(count);
				if (s[1]='l')and(s[2]='w')and(s[3]='x') then if tmp<>count then writeln('NOpO');
				inc(left[j]);
			end;
		end;
		//for j:=1 to Q do writeln(left[j],' ',right[j]);
		if downlim=0 then inc(count);
            if (s[1]='l')and(s[2]='w')and(s[3]='x') then if count<>i then writeln(i,' ',count);
		inc(ans,count);
	end;
	writeln(ans);
end;

begin
{$ifndef ONLINE_JUDGE}
	assign(input,'138E.in');
	reset(input);
{$endif}
lemon;
end.

var
s:array[0..100010] of char;
sum:array[0..100010,'a'..'z'] of longint;
left,right,meet:array[0..100010] of longint;
letter:array[0..510] of char;
l,r:array[0..510] of longint;
uplim,downlim:longint;

function check(x:longint):boolean; begin exit((downlim<=x)and(x<=uplim)); end;

procedure lemon;
var n,i,j,count,Q:longint;
    ans:int64;
    ch:char;
begin n:=0;
	while not seekeoln(input) do
	begin read(ch);
		inc(n); s[n]:=ch;
	end;
	for ch:='a' to 'z' do sum[0,ch]:=0;
	for i:=1 to n do
	begin sum[i]:=sum[i-1];
		inc(sum[i,s[i]]);
	end;
	readln(Q,downlim,uplim);
	for i:=1 to Q do
	begin readln(letter[i],l[i],r[i]);
		if l[i]>r[i] then writeln('no');
	end;
	fillchar(meet,sizeof(meet),0);
	for i:=1 to n do begin left[i]:=1;  right[i]:=0; end;
	ans:=0; count:=0;
	for i:=1 to n do
      begin for j:=1 to Q do
		begin //[left[j]~right[j],i] is available interval
			if sum[i,letter[j]]-sum[left[j]-1,letter[j]]<l[j] then continue;
			while (right[j]<i)and(sum[i,letter[j]]-sum[right[j],letter[j]]>=l[j]) do
			begin inc(right[j]);
				if (s[1]='l')and(s[2]='w')and(s[3]='x') then if meet[right[j]]=500 then writeln('No');
				if check(meet[right[j]]) then dec(count);
				inc(meet[right[j]]);
				if check(meet[right[j]]) then inc(count);
			end;
			while (left[j]<=i)and(sum[i,letter[j]]-sum[left[j]-1,letter[j]]>r[j]) do
			begin if (s[1]='l')and(s[2]='w')and(s[3]='x') then if meet[right[j]]=0 then writeln('Nooo');
				if check(meet[left[j]]) then dec(count);
				dec(meet[left[j]]);
				if check(meet[left[j]]) then inc(count);
				inc(left[j]);
			end;
		end;
		//for j:=1 to Q do writeln(left[j],' ',right[j]);
		if downlim=0 then inc(count);
            if (s[1]='l')and(s[2]='w')and(s[3]='x') then if count<>i then writeln(i,' ',count);
		inc(ans,count);
	end;
	writeln(ans);
end;

begin
{$ifndef ONLINE_JUDGE}
	assign(input,'138E.in');
	reset(input);
{$endif}
lemon;
end.

var
s:array[0..100010] of char;
sum:array[0..100010,'a'..'z'] of longint;
left,right,meet:array[0..100010] of longint;
letter:array[0..510] of char;
l,r:array[0..510] of longint;
uplim,downlim:longint;

function check(x:longint):boolean; begin exit((downlim<=x)and(x<=uplim)); end;

procedure lemon;
var n,i,j,count,Q:longint;
    ans:int64;
    ch:char;
begin n:=0;
	while not seekeoln(input) do
	begin read(ch);
		inc(n); s[n]:=ch;
	end;
	for ch:='a' to 'z' do sum[0,ch]:=0;
	for i:=1 to n do
	begin sum[i]:=sum[i-1];
		inc(sum[i,s[i]]);
	end;
	readln(Q,downlim,uplim);
	for i:=1 to Q do
	begin readln(letter[i],l[i],r[i]);
		if l[i]>r[i] then writeln('no');
	end;
	fillchar(meet,sizeof(meet),0);
	for i:=1 to n do begin left[i]:=1;  right[i]:=0; end;
	ans:=0; count:=0;
	for i:=1 to n do
      begin for j:=1 to Q do
		begin //[left[j]~right[j],i] is available interval
			if sum[i,letter[j]]-sum[left[j]-1,letter[j]]<l[j] then continue;
			while (right[j]<i)and(sum[i,letter[j]]-sum[right[j],letter[j]]>=l[j]) do
			begin inc(right[j]);
				if check(meet[right[j]]) then dec(count);
				inc(meet[right[j]]);
				if check(meet[right[j]]) then inc(count);
			end;
			while (left[j]<=i)and(sum[i,letter[j]]-sum[left[j]-1,letter[j]]>r[j]) do
			begin if check(meet[left[j]]) then dec(count);
				dec(meet[left[j]]);
				if check(meet[left[j]]) then inc(count);
				inc(left[j]);
			end;
		end;
		//for j:=1 to Q do writeln(left[j],' ',right[j]);
		if downlim=0 then inc(count);
            //writeln(count);
		inc(ans,count);
	end;
	writeln(ans);
end;

begin
{$ifndef ONLINE_JUDGE}
	assign(input,'138E.in');
	reset(input);
{$endif}
lemon;
end.

var
s:array[0..100010] of char;
sum:array[0..100010,'a'..'z'] of longint;
left,right,meet:array[0..100010] of longint;
letter:array[0..510] of char;
l,r:array[0..510] of longint;
uplim,downlim:longint;

function check(x:longint):boolean; begin exit((downlim<=x)and(x<=uplim)); end;

procedure lemon;
var n,i,j,count,Q,tmp:longint;
    ans:int64;
    ch:char;
begin n:=0;
	while not seekeoln(input) do
	begin read(ch);
		inc(n); s[n]:=ch;
	end;
	for ch:='a' to 'z' do sum[0,ch]:=0;
	for i:=1 to n do
	begin sum[i]:=sum[i-1];
		inc(sum[i,s[i]]);
	end;
	readln(Q,downlim,uplim);
	for i:=1 to Q do
	begin readln(letter[i],l[i],r[i]);
		if l[i]>r[i] then writeln('no');
	end;
	fillchar(meet,sizeof(meet),0);
	for i:=1 to n do begin left[i]:=1;  right[i]:=0; end;
	ans:=0; count:=0;
	for i:=1 to n do
      begin for j:=1 to Q do
		begin //[left[j]~right[j],i] is available interval
			if sum[i,letter[j]]-sum[left[j]-1,letter[j]]<l[j] then continue;
			while (right[j]<i)and(sum[i,letter[j]]-sum[right[j],letter[j]]>=l[j]) do
			begin inc(right[j]);
				tmp:=count;
				if check(meet[right[j]]) then dec(count);
				inc(meet[right[j]]);
				if check(meet[right[j]]) then inc(count);
				if (s[1]='l')and(s[2]='w')and(s[3]='x') then if tmp<>count then writeln('NOO');
			end;
			while (left[j]<=i)and(sum[i,letter[j]]-sum[left[j]-1,letter[j]]>r[j]) do
			begin tmp:=count;
				if check(meet[left[j]]) then dec(count);
				dec(meet[left[j]]);
				if check(meet[left[j]]) then inc(count);
				if (s[1]='l')and(s[2]='w')and(s[3]='x') then if tmp<>count then writeln('NOpO',' ',meet[left[j]]);
				inc(left[j]);
			end;
		end;
		//for j:=1 to Q do writeln(left[j],' ',right[j]);
		if downlim=0 then inc(count);
            if (s[1]='l')and(s[2]='w')and(s[3]='x') then if count<>i then writeln(i,' ',count);
		inc(ans,count);
	end;
	writeln(ans);
end;

begin
{$ifndef ONLINE_JUDGE}
	assign(input,'138E.in');
	reset(input);
{$endif}
lemon;
end.

Program cf99e;
Var ss:ansistring;
    total:array['a'..'z'] of longint;
    place:array['a'..'z',0..100000] of longint;
    cc,s:array[1..100000] of char;
    cl,cr:array[1..100000] of longint;
    a,c:array[1..100000] of longint;
    ans:int64;
    d,i,j,k,left,right,n,m,cur,std:longint;
begin
  readln(ss);
  n:=length(ss);
  for i:=1 to n do
    begin
      s[i]:=ss[i];
      inc(total[s[i]]);
      c[i]:=total[s[i]];
      place[s[i],total[s[i]]]:=i;
    end;
  readln(m,left,right);
  for i:=1 to m do
    readln(cc[i],cl[i],cr[i]);
  cur:=0;
  for i:=1 to m do if cl[i]=0 then inc(std);
  for d:=1 to n do
    begin
      a[d]:=std;
      if (left<=a[d]) and (a[d]<=right) then inc(cur);
      for k:=1 to m do
        if cc[k]=s[d] then
          begin
            if c[d]-cr[k]-1>=0 then
              begin
                for i:=place[s[d],c[d]-cr[k]-1]+1 to place[s[d],c[d]-cr[k]] do
                  begin
                    dec(a[i]);
                    if a[i]=left-1 then cur:=cur-1;
                    if a[i]=right then cur:=cur+1;
                  end;
              end;
            if c[d]-cl[k]>=0 then
              begin
                for i:=place[s[d],c[d]-cl[k]]+1 to place[s[d],c[d]-cl[k]+1] do
                  begin
                    inc(a[i]);
                    if a[i]=right+1 then cur:=cur-1;
                    if a[i]=left then cur:=cur+1;
                  end;
              end;
          end;
      ans:=ans+cur;
    end;
  writeln(ans);
end.

Program cf99e;
Var s:ansistring;
    total:array['a'..'z'] of longint;
    place:array['a'..'z',0..100000] of longint;
    cc:array[1..100000] of char;
    cl,cr:array[1..100000] of longint;
    a,c:array[1..100000] of longint;
    ans:int64;
    d,i,j,k,left,right,n,m,cur:longint;
begin
  readln(s);
  n:=length(s);
  for i:=1 to n do
    begin
      inc(total[s[i]]);
      c[i]:=total[s[i]];
      place[s[i],total[s[i]]]:=i;
    end;
  readln(m,left,right);
  for i:=1 to m do
    readln(cc[i],cl[i],cr[i]);
  cur:=0;
  for d:=1 to n do
    begin
      if left=0 then inc(cur);
      for k:=1 to m do
        if cc[k]=s[d] then
          begin
            if c[d]-cr[k]-1>=0 then
              begin
                for i:=place[s[d],c[d]-cr[k]-1]+1 to place[s[d],c[d]-cr[k]] do
                  begin
                    dec(a[i]);
                    if a[i]=left-1 then cur:=cur-1;
                    if a[i]=right then cur:=cur+1;
                  end;
              end;
            if c[d]-cl[k]>=0 then
              begin
                for i:=place[s[d],c[d]-cl[k]]+1 to place[s[d],c[d]-cl[k]+1] do
                  begin
                    inc(a[i]);
                    if a[i]=right+1 then cur:=cur-1;
                    if a[i]=left then cur:=cur+1;
                  end;
              end;
          end;
      ans:=ans+cur;
    end;
  writeln(ans);
end.


It happened at the times of the Great Berland Empire. Once the Emperor dreamt that the Messenger from the gods ordered to build a temple whose base would be a convex polygon with n angles. Next morning the Emperor gave the command to build a temple whose base was a regular polygon with n angles. The temple was built but soon the Empire was shaken with disasters and crop failures. After an earthquake destroyed the temple, the Emperor understood that he somehow caused the wrath of gods to fall on his people. He ordered to bring the wise man. When the wise man appeared, the Emperor retold the dream to him and asked "Oh the wisest among the wisest, tell me how could I have infuriated the Gods?". "My Lord," the wise man answered. "As far as I can judge, the gods are angry because you were too haste to fulfill their order and didn't listen to the end of the message".Indeed, on the following night the Messenger appeared again. He reproached the Emperor for having chosen an imperfect shape for the temple. "But what shape can be more perfect than a regular polygon!?" cried the Emperor in his dream. To that the Messenger gave a complete and thorough reply.   All the vertices of the polygon should be positioned in the lattice points.  All the lengths of its sides should be different.  From the possible range of such polygons a polygon which maximum side is minimal possible must be chosen. You are an obedient architect who is going to make the temple's plan. Note that the polygon should be simple (having a border without self-intersections and overlapping) and convex, however, it is acceptable for three consecutive vertices to lie on the same line.

The first line contains the single number n (3 ≤ n ≤ 10000).

Print "YES" (without quotes) in the first line if it is possible to build a polygon possessing the needed qualities. In the next n lines print integer coordinates of the polygon vertices in the order in which they would be passed counter clockwise. The absolute value of the coordinates shouldn't exceed 109. No two vertices can coincide. It is permitted to print any of the possible solutions. Print "NO" if to build the polygon is impossible.

{$R+,S+,Q+,I+,O-}
{R-,S-,Q-,I-,O+}
var
  sum,j,tmp,n,a,b,m,i,sx,sy: longint;
  can: array [0..41111] of boolean;
  x,y,pred,last,xm,ym,ca,cb: array [0..41111] of longint;

procedure Sort(l,r:longint);
var
  i,j,pp,xx,yy,tmp: longint;
begin
  i:=l; j:=r;
  pp:=l+random(r-l+1);
  xx:=x[pp]; yy:=y[pp];
  repeat
    while x[i]*yy > xx*y[i] do inc(i);
    while x[j]*yy < xx*y[j] do dec(j);
    if i <= j then
    begin
      tmp:=x[i]; x[i]:=x[j]; x[j]:=tmp;
      tmp:=y[i]; y[i]:=y[j]; y[j]:=tmp;
      inc(i); dec(j);
    end;
  until i > j;
  if l < j then Sort(l,j);
  if i < r then Sort(i,r);
end;

begin
  randseed:=8753;
//  assign(input,'in'); reset(input);
//  assign(output,'out'); rewrite(output);
  read(n);
  if n = 3 then
  begin
    writeln('YES');
    writeln('0 0');
    writeln('1 0');
    writeln('0 2');
    halt;
  end;
  fillchar(can,sizeof(can),False);
  for a:=0 to 200 do
    for b:=0 to 200 do
      if a*a+b*b <= 40000 then
      begin
        can[a*a+b*b]:=True;
        ca[a*a+b*b]:=a;
        cb[a*a+b*b]:=b;
      end;
  m:=0;
  for i:=1 to 40000 do
    if can[i] then
    begin
      inc(m);
      xm[m]:=ca[i];
      ym[m]:=cb[i];
      if m = n+1 then break;
    end;
  sx:=0; sy:=0;
  for i:=1 to n do
  begin
    inc(sx,xm[i]);
    inc(sy,ym[i]);
  end;
  if (sx+sy) and 1 = 1 then
  begin
    inc(sx,xm[n+1]);
    inc(sy,ym[n+1]);
    for i:=1 to n do
      if ((sx-xm[i])+(sy-ym[i])) and 1 = 0 then
      begin
        dec(sx,xm[i]);
//        dec(sy,ym[i]);
        xm[i]:=xm[n+1];
        ym[i]:=ym[n+1];
        break;
      end;
  end;
  if sx and 1 = 1 then
    for i:=n downto 1 do
      if (xm[i]+ym[i]) and 1 = 1 then
      begin
        tmp:=xm[i];
        xm[i]:=ym[i];
        ym[i]:=tmp;
        break;
      end;
  fillchar(last,sizeof(last),0);
  for i:=1 to n do
  begin
    pred[i]:=last[xm[i]];
    last[xm[i]]:=i;
  end;
  sum:=0;
  for a:=200 downto 0 do
  begin
    j:=last[a];
    while j > 0 do
    begin
      if sum > 0 then xm[j]:=-xm[j];
      inc(sum,xm[j]);
      j:=pred[j];
    end;
  end;
  if sum <> 0 then while true do;
  fillchar(last,sizeof(last),0);
  for i:=1 to n do
  begin
    pred[i]:=last[ym[i]];
    last[ym[i]]:=i;
  end;
  sum:=0;
  for a:=200 downto 0 do
  begin
    j:=last[a];
    while j > 0 do
    begin
      if sum > 0 then ym[j]:=-ym[j];
      inc(sum,ym[j]);
      j:=pred[j];
    end;
  end;
  if sum <> 0 then while true do;
  writeln('YES');
  m:=0;
  for i:=1 to n do
    if (ym[i] > 0) or (ym[i] = 0) and (xm[i] > 0) then
    begin
      inc(m);
      x[m]:=xm[i];
      y[m]:=ym[i];
    end;
  if m > 0 then Sort(1,m);
  sx:=0; sy:=0;
  for i:=1 to m do
  begin
    writeln(sx,' ',sy);
    sx:=sx+x[i];
    sy:=sy+y[i];
  end;
  m:=0;
  for i:=1 to n do
    if not ((ym[i] > 0) or (ym[i] = 0) and (xm[i] > 0)) then
    begin
      inc(m);
      x[m]:=xm[i];
      y[m]:=-ym[i];
    end;
  if m > 0 then Sort(1,m);
  for i:=m downto 1 do
  begin
    writeln(sx,' ',sy);
    sx:=sx+x[i];
    sy:=sy-y[i];
  end;
  close(input);
  close(output);
end.

{$R+,S+,Q+,I+,O-}
{R-,S-,Q-,I-,O+}
var
  sum,j,tmp,n,a,b,m,i,sx,sy: longint;
  can: array [0..41111] of boolean;
  x,y,pred,last,xm,ym,ca,cb: array [0..41111] of longint;

procedure Sort(l,r:longint);
var
  i,j,pp,xx,yy,tmp: longint;
begin
  i:=l; j:=r;
  pp:=l+random(r-l+1);
  xx:=x[pp]; yy:=y[pp];
  repeat
    while x[i]*yy > xx*y[i] do inc(i);
    while x[j]*yy < xx*y[j] do dec(j);
    if i <= j then
    begin
      tmp:=x[i]; x[i]:=x[j]; x[j]:=tmp;
      tmp:=y[i]; y[i]:=y[j]; y[j]:=tmp;
      inc(i); dec(j);
    end;
  until i > j;
  if l < j then Sort(l,j);
  if i < r then Sort(i,r);
end;

begin
  randseed:=8753;
//  assign(input,'in'); reset(input);
//  assign(output,'out'); rewrite(output);
  read(n);
  fillchar(can,sizeof(can),False);
  for a:=0 to 200 do
    for b:=0 to 200 do
      if a*a+b*b <= 40000 then
      begin
        can[a*a+b*b]:=True;
        ca[a*a+b*b]:=a;
        cb[a*a+b*b]:=b;
      end;
  m:=0;
  for i:=1 to 40000 do
    if can[i] then
    begin
      inc(m);
      xm[m]:=ca[i];
      ym[m]:=cb[i];
      if m = n+1 then break;
    end;
  sx:=0; sy:=0;
  for i:=1 to n do
  begin
    inc(sx,xm[i]);
    inc(sy,ym[i]);
  end;
  if (sx+sy) and 1 = 1 then
  begin
    inc(sx,xm[n+1]);
    inc(sy,ym[n+1]);
    for i:=1 to n do
      if ((sx-xm[i])+(sy-ym[i])) and 1 = 0 then
      begin
        dec(sx,xm[i]);
//        dec(sy,ym[i]);
        xm[i]:=xm[n+1];
        ym[i]:=ym[n+1];
        break;
      end;
  end;
  if sx and 1 = 1 then
    for i:=1 to n do
      if (xm[i]+ym[i]) and 1 = 1 then
      begin
        tmp:=xm[i];
        xm[i]:=ym[i];
        ym[i]:=tmp;
        break;
      end;
  fillchar(last,sizeof(last),0);
  for i:=1 to n do
  begin
    pred[i]:=last[xm[i]];
    last[xm[i]]:=i;
  end;
  sum:=0;
  for a:=200 downto 0 do
  begin
    j:=last[a];
    while j > 0 do
    begin
      if sum > 0 then xm[j]:=-xm[j];
      inc(sum,xm[j]);
      j:=pred[j];
    end;
  end;
  fillchar(last,sizeof(last),0);
  for i:=1 to n do
  begin
    pred[i]:=last[ym[i]];
    last[ym[i]]:=i;
  end;
  sum:=0;
  for a:=200 downto 0 do
  begin
    j:=last[a];
    while j > 0 do
    begin
      if sum > 0 then ym[j]:=-ym[j];
      inc(sum,ym[j]);
      j:=pred[j];
    end;
  end;
  writeln('YES');
  m:=0;
  for i:=1 to n do
    if (ym[i] > 0) or (ym[i] = 0) and (xm[i] > 0) then
    begin
      inc(m);
      x[m]:=xm[i];
      y[m]:=ym[i];
    end;
  if m > 0 then Sort(1,m);
  sx:=0; sy:=0;
  for i:=1 to m do
  begin
    writeln(sx,' ',sy);
    sx:=sx+x[i];
    sy:=sy+y[i];
  end;
  m:=0;
  for i:=1 to n do
    if not ((ym[i] > 0) or (ym[i] = 0) and (xm[i] > 0)) then
    begin
      inc(m);
      x[m]:=xm[i];
      y[m]:=-ym[i];
    end;
  if m > 0 then Sort(1,m);
  for i:=m downto 1 do
  begin
    writeln(sx,' ',sy);
    sx:=sx+x[i];
    sy:=sy-y[i];
  end;
  close(input);
  close(output);
end.