Missing Number

All submissions for this problem are available.
### Read problem statements in [Hindi](http://www.codechef.com/download/translated/OCT19/hindi/MSNG.pdf), [Bengali](http://www.codechef.com/download/translated/OCT19/bengali/MSNG.pdf), [Mandarin Chinese](http://www.codechef.com/download/translated/OCT19/mandarin/MSNG.pdf), [Russian](http://www.codechef.com/download/translated/OCT19/russian/MSNG.pdf), and [Vietnamese](http://www.codechef.com/download/translated/OCT19/vietnamese/MSNG.pdf) as well. Archaeologist John and his team have unearthed the remains of an ancient civilisation. The partially destroyed walls of its houses contain strange pairs of numbers. John assumes that the first number in each pair always corresponds to a base (itself written in base $10$) and the second number in that pair is written in this base ― for example, `3 2110` denotes $(2110)_3 = 2 \cdot 3^3 + 1 \cdot 3^2 + 1 \cdot 3 + 0 = 66$. Unfortunately, the first number (the base) is often lost or unreadable. John made a list of pairs of numbers found on the walls, where he represents lost bases by $1$. For example: ``` 1 10001 3 2110 1 18565 ``` John is wondering if all these pairs of numbers could represent a single common number ― that is, if it is possible to choose some number $X$ in decimal representation (in base $10$) and fill in all missing bases in such a way that the second numbers in all pairs written on the walls, when converted from their bases to base $10$, are equal to $X$. For each pair with a missing base $(1, Y)$, we may choose any integer base $B$ such that $2 \le B \le 36$ and $Y$ is a valid number in base $B$. Since John does not want to deal with huge numbers, he considers $X \gt 10^{12}$ to be invalid. Still, there are too many possibilities, so he needs your help. Your task is to find the smallest common number $X$ that satisfies all conditions, or determine that no such common number exists. ### Input  The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.  The first line of each test case contains a single integer $N$.  Each of the following $N$ lines contains an integer $B$ followed by a space and a string $Y$, describing a pair of numbers found on the walls; in the string $Y$, digits $10$ through $35$ (in base $10$) are represented by letters 'A' through 'Z'. ### Output For each test case, print a single line containing one integer ― the smallest possible common number $X$, or $1$ if there is no valid common number. ### Constraints  $1 \le T \le 200$  $1 \le N \le 100$  $2 \le B \le 36$ or $B = 1$  $Y$ contains only digits '0' through '9' and letters 'A' through 'Z'  for each pair $(B, Y)$ on the input, if $B \neq 1$, $Y$ is a valid number in base $B$  the length of string $Y$ is at most $40$ ### Subtasks **Subtask #1 (30 points):** there is at least one pair $(B, Y)$ such that $B \neq 1$ **Subtask #2 (70 points):** original constraints ### Example Input ``` 2 3 1 10000 8 20 1 16 3 1 10100 1 5A 1 1011010 ``` ### Example Output ``` 16 90 ``` ### Explanation **Example case 1:**  $(10000)$ in base $2$ is $16$ in base $10$  $(20)$ in base $8$ is $16$ in base $10$  $(16)$ in base $10$ is $16$ in base $10$ **Example case 2:**  $(10100)_3 = (90)_{10}$  $(5\mathrm{A})_{16} = (90)_{10}$  $(1011010)_2 = (90)_{10}$Author:  jarvisnaharoy 
Editorial  https://discuss.codechef.com/problems/MSNG 
Tags  conversion, jarvisnaharoy, oct19, r_64, simple 
Date Added:  16072019 
Time Limit:  1 sec 
Source Limit:  50000 Bytes 
Languages:  C, CPP14, JAVA, PYTH, PYTH 3.6, PYPY, CS2, PAS fpc, PAS gpc, RUBY, PHP, GO, NODEJS, HASK, rust, SCALA, swift, D, PERL, FORT, WSPC, ADA, CAML, ICK, BF, ASM, CLPS, PRLG, ICON, SCM qobi, PIKE, ST, NICE, LUA, BASH, NEM, LISP sbcl, LISP clisp, SCM guile, JS, ERL, TCL, SQL, kotlin, PERL6, TEXT, SCM chicken, PYP3, CLOJ, R, COB, FS 
Comments
 Please login at the top to post a comment.
SUCCESSFUL SUBMISSIONS
Fetching successful submissions
HELP
If you are still having problems, see a sample solution here. 