// Arup Guha
// 10/17/2014
// Solution to 1994 UCF HS Contest Problem: Dimwit Numbers

import java.util.*;

public class dimwit {

	public static void main(String[] args) {

		Scanner stdin = new Scanner(System.in);
		int numCases = stdin.nextInt();

		// Go through each case.
		for (int i=0; i<numCases; i++) {

			// Read input.
			int orig = stdin.nextInt();
			char[] base1 = stdin.next().substring(2).toCharArray();
			char[] base2 = stdin.next().substring(2).toCharArray();

			// Convert to decimal.
			int decimal = getDecimal(orig, base1);

			// Convert to new base or detect overflow.
			if (decimal >= product(base2)) System.out.println("OVERFLOW");
			else                           System.out.println(getDW(decimal, base2));
		}
	}

	// Returns the decimal equivalent of n in base.
	public static int getDecimal(int n, char[] base) {

		// Peel off each digit from back.
		int value = 0, place = 1;
		for (int i=base.length-1; i>=0; i--) {
			value += (place*(n%10));
			n /= 10;
			place *= (base[i]-'0'+1);
		}
		return value;
	}

	// Returns product of digits.
	public static int product(char[] base) {
		int ans = 1;
		for (int i=0; i<base.length; i++)
			ans *= (base[i]-'0'+1);
		return ans;
	}

	// Returns value converted to base.
	public static String getDW(int value, char[] base) {

		// Also compute from back.
		String ans = "";
		for (int i=base.length-1; i>=0; i--) {
			int digit = value%(base[i]-'0'+1);
			ans = digit + ans;
			value /= (base[i]-'0'+1);
		}
		return ans;
	}
}