// Arup Guha
// Solution to 2011 NY Regional Problem D: Deconding EDSAC Data
// 10/22/2012

import java.util.*;
import java.math.*;

public class d {
	
	public static void main(String[] args) {
		
		Scanner stdin = new Scanner(System.in);
		
		// Go through cases.
		int numCases = stdin.nextInt();
		for (int loop=1; loop<=numCases; loop++) {
			
			int dummy = stdin.nextInt();
			String a = stdin.next();
			int b = stdin.nextInt();
			String c = stdin.next();
			
			// Starting value
			int value = convert(a.charAt(0));
			
			// Shift over b by 1 bit, value by 12 bits.
			value = (value << 12) + 2*b;
			
			// Last bit
			if (c.charAt(0) == 'D')
				value++;
				
			output(value,loop);
		}
	}
	
	public static void output(int value, int kase) {
		
		System.out.print(kase+" ");
		
		// Make negative, if necessary.
		if ((value & (1 << 16)) == (1 << 16)) 
			value -= (1 << 17);

		// Special Case, if I choose to use BigDecimal.
		boolean addDotZero = false;
		if (value == 0 || -value == (1 << 16))
			addDotZero = true;	
			
		BigInteger a = new BigInteger(""+value);
		int base = (1 << 16);
		BigInteger b = new BigInteger(""+base);
		
		// Create our fraction.
		BigDecimal num = new BigDecimal(a);
		BigDecimal den = new BigDecimal(b);
		BigDecimal ans = num.divide(den);
		
		// Only format issue.
		String s = ans.toString();
		if (addDotZero)
			s = s + ".0";
		
		// Print out the result.
		System.out.println(s);
	}
	
	// Converts a character to an integer from 0 to 31, using their
	// bizarre scheme.
	public static int convert(char a) {
		
		// Fixed array.
		String s = "PQWERTYUIOJ#SZK*?F@D!HNM&LXGABCV";
		
		// Look up.
		for (int i=0; i<s.length(); i++)
			if (a == s.charAt(i))
				return i;
				
		return 0;
	}
}