// Arup Guha
// 7/13/2026
// Solution to 1994 UCF HSPT Problem: Dimwit Numbers

import java.util.*;

public class dimwit {

	public static void main(String[] args) {
	
		// Process cases.
		Scanner stdin = new Scanner(System.in);
		int nC = stdin.nextInt();
		
		for (int loop=0; loop<nC; loop++) {
		
			// Get input.
			char[] val = stdin.next().toCharArray();
			char[] oldbase = stdin.next().substring(2).toCharArray();
			char[] newbase = stdin.next().substring(2).toCharArray();
			
			// Max represent in new base.
			int myMax = 1;
			for (int i=0; i<newbase.length; i++)
				myMax *= (newbase[i]-'0'+1);
			myMax--;
			
			// Convert value to decimal.
			int inDec = toDecimal(val, oldbase);
			
			// Get this out of the way.
			if (inDec > myMax)
				System.out.println("OVERFLOW");
			
			// Go ahead convert and print.
			else {
			
				// Again easier to go from back to front.
				char[] res = new char[newbase.length];
				for (int i=newbase.length-1; i>=0; i--) {
					int baseD = newbase[i]-'0'+1;
					res[i] = (char)('0' + (inDec%baseD)) ;
					inDec /= baseD;
				}
				System.out.println(new String(res));
			}
		}
	}
	
	// Returns the base 10 value of val in base oldbase.
	public static int toDecimal(char[] val, char[] oldbase) {
		
		int mult = 1, res = 0;
		
		// Loop backwards...
		for (int i=oldbase.length-1; i>=0; i--) {
		
			// Add in contribution of this digit.
			res = res + mult*(val[i]-'0');
			
			// This is the next "digit" value.
			mult *= (oldbase[i]-'0'+1);
		}
		
		return res;
	}
}