Alex Magic Product
Tag(s):

## Easy, Modular arithmetic, Simple-math

Problem
Editorial
Analytics

Alex, a sophomore got inspired to do mathematics after watching a movie "The Man who knew Infinity". One day, he was studying mathematics in the library and in one of the books he discovered a way to perform multiplication of numbers in a new fashion by using digits of a number. He found that method interesting and decided to formulate an algorithm for the same method. Alex finds it difficult to implement and he asks for your help.

Given a number N. Let the first digit of the number be X. P1 is the number formed using first X digits of the given number in the sequence. Let the digit after Xth digit is Y. P2 is another number formed using Y digits starting from (X + 1)th position in sequence and so on. If the number of digits to be used to form a number are more than the remaining digits, then simply form the number using digits till the last digit. Product of (P1 * P2 * P3 ... * Pi) is known as magic product. Help Alex to find the magic product.

Input Format:
The first line of the input contains an integer T denoting the number of test cases. Each test case contains a number N followed by values of A and B respectively.

Output Format:
For each test case, output the magic product modulo 10A + B.

Constraints:

• T ∈ [1, 105]
• N ∈ [1, 10100]
• A ∈ [1, 9]
• B ∈ [0, 100]

NOTE: Any digit in the given number N will not be equal to 0.

SAMPLE INPUT
3
46542368
1 5
35935
2 9
54321
1 3
SAMPLE OUTPUT
1
30
7
Explanation

Case 1: 54321 = (54321) % (101+3) = 7
Case 2: 35935 = ( (359) * (35) ) % (102+9) = 30
Case 3: 46542368 = ( (4654) * (23) * (68) ) % (101+5) = 1

Time Limit: 0.5 sec(s) for each input file.
Memory Limit: 256 MB
Source Limit: 1024 KB
Marking Scheme: Marks are awarded when all the testcases pass.
Allowed Languages: C, C++, C++14, Clojure, C#, D, Erlang, F#, Go, Groovy, Haskell, Java, Java 8, JavaScript(Rhino), JavaScript(Node.js), Julia, Kotlin, Lisp, Lisp (SBCL), Lua, Objective-C, OCaml, Octave, Pascal, Perl, PHP, Python, Python 3, R(RScript), Racket, Ruby, Rust, Scala, Swift, Visual Basic

## CODE EDITOR

Initializing Code Editor...

## This Problem was Asked in

Challenge Name

Code Emblaze

OTHER PROBLEMS OF THIS CHALLENGE
• Math > Number Theory
• Basic Programming > Implementation
• Data Structures > Arrays
• Math > Number Theory