The Adventure in Panda Land Part I: Panda Number

No attempts yetTime limit1sMemory limit128 MB

Problem

In Panda Land the pandas have their own numeral system for writing numbers, and, surprisingly, it looks a lot like the Roman numerals of our own ancient history. Pandas cannot write (imagine a panda holding a pen!), so instead they cut and arrange bamboo stalks to spell out a number.

The rules for a Panda Number are:

  • The power-of-ten letters (I, X, C, M, W, Y, H, A) may be repeated at most three times. The other letters (V, L, N, E, F, K, T) may not be repeated.
  • When one or more letters follow a letter of greater value, their values are added, and the letters are written from the largest value to the smallest. For example, FXVIII = 50000 + 10 + 5 + 1 + 1 + 1 = 50018.
  • When a letter is placed before a letter of greater value, its value is subtracted, and at most one letter may be subtracted. For example:
    • XIV = 10 + (5 - 1) = 14
    • CIX = 100 + (10 - 1) = 109
    • MECIX = (5000 - 1000) + 100 + (10 - 1) = 4109
  • Only a power-of-ten letter may be subtracted (you may subtract I from V, or X from L, but not V from X, because V is not a power of ten).
  • You may not subtract a letter from one that is more than ten times greater (you may subtract I from X, but not I from L, so there is no such number as IL).

These rules guarantee that every decimal number has exactly one Panda Number representation.

Unlike Roman numerals, pandas recognize zero and negative numbers (proof that pandas are more advanced than the ancient Romans). A negative number is written by placing one extra bamboo as a minus sign in front of the letters. Zero is special and obeys none of the rules above; forming a zero requires five bamboos.

The number of bamboos needed to form a number is the sum of the bamboos required by every letter that appears in it. For example:

  • 4108 = 4000 + 100 + 8 = (5000 - 1000) + 100 + 5 + 1 + 1 + 1 = MECVIII (16 bamboos)
  • 4109 = 4000 + 100 + 9 = (5000 - 1000) + 100 + (10 - 1) = MECIX (14 bamboos)
  • -205 = -(200 + 5) = -(100 + 100 + 5) = -CCV (9 bamboos)

Given two numbers A and B, determine how many bamboos the pandas need in total to form every number from A to B, inclusive.

DecimalPanda NumberRequired Bamboo
1I1
5V2
10X2
50L2
100C3
500N3
1,000M4
5,000E4
10,000W4
50,000F3
100,000Y3
500,000K3
1,000,000H3
5,000,000T2
10,000,000A3

Input

The first line contains a single positive integer TT, the number of test cases. Each of the next TT lines contains two integers AA and BB (25,000,000AB25,000,000-25{,}000{,}000 \le A \le B \le 25{,}000{,}000).

Output

For each test case, print on its own line the total number of bamboos needed to form every number from AA to BB, inclusive.