LearnToCP
Sign in
Navigation
HomeRoadmapProblemsAbout Us
Theory
Contest Knowledge
Selecting an IDEInteractive TasksOutput-Only Tasks
Basics
Your First ProgramData types and IOC++ syntaxModuloFunctionsVectorsMatricesTime Complexity
Sorting
SortingCounting sortRadix Sort
Optimization Techniques
Two PointersSum of numbers 1 to nPrefix sumBinary SearchGreedyBinary Search FunctionsBinary Search by AnswerDivide and Conquer
Binary Numbers
Binary NumbersNumbers in codeBitwise OperationsBitmasks
Math
Binary ExponentiationPrime NumbersPrime FactorizationGCD and LCMSieve of EratosthenesModified Sieve
Data Structures
StringsStackQueueMapsSetsPriority QueueCustom Criteria for FunctionsSegment TreesFenwick TreesSparse TablesUnion FindSqrt Decomposition
Combinatorics
Addition PrincipleMultiplication PrincipleCombinatoric ObjectsInclusion Exclusion Principle
Geometry
Geometry BasicsVectorsCross and Dot ProductLinesPolygonsAnglesPoint in PolygonDistances and Intersection PointsConvex HullCircles
Recursion
PointersRecursionGenerating Combinatoric Objects
Dynamic Programming
About DPDP problemsTree DPBitmask DPDigit DP
Graph Theory
GraphsDFS and BFSShortest PathsTreesTopological SortingDijkstra's AlgorithmMinimum Spanning TreesShortest Path Algorithms
Advanced Graph Theory
BiconnectivityStrongly Connected ComponentsBipartite GraphGraph FlowAugmenting PathsFlow - Minimum Cut DualityHeavy-Light DecompositionCentroid Decomposition
Advanced Data Structures
2D and 3D Segment TreesLazy PropagationImplicit Segment TreesPersistent Segment TreesLowest Common AncestorTrieBalanced Binary Search TreesMo's Algorithm

Classy Numbers

MediumProblem #72
Time LimitMemoryInputOutput
2 s64 MBstdinstdout

Counting up to a bound is easier than counting inside a segment.

Call a positive integer classy if its decimal representation contains no more than 3 non-zero digits. So 4, 200000 and 10203 are classy, while 4231, 102306 and 7277420000 are not.

You are given a segment [l,r]. Count how many classy integers x satisfy l≤x≤r.

Input

First line of input will be a single integer t - the number of segments.
Each of the next t lines contains two integers l and r - the ends of one segment, both included.

Output

For every segment print a single line with the number of classy integers inside it.

Example

Input
4
1 1000
1024 1024
65536 65536
999999 1000001
Output
1000
1
0
2

Every number from 1 to 999 has at most three digits, so it cannot have more than three non-zero ones, and 1000 has a single non-zero digit - all 1000 are classy. 1024 has three non-zero digits and 65536 has five. In the last segment 999999 has six non-zero digits, while 1000000 and 1000001 have one and two.

Constraints

1≤t≤104
1≤l≤r≤1018


This problem is based on Classy Numbers, problem 1036C from Educational Codeforces Round 50, by Mike Mirzayanov and the Codeforces team. The statement here is our own.

Submit your solution

Sign in to submit your solution and track your progress.

Sign in to submit