Java 8 coding challenge : Jarvis and Seven Segments

All over the world, peoples are working on energy solution. It would be a tough time for our next generation to survive if we don’t think about solution. Tony stark is working on a new project and wants to display his project using “seven segment display – concept”. Tony Stark gave Jarvis a task to find a number from his Favorite list of number for which the energy consumption is lowest.

(Assuming that for a digit to represent Tony stark is using 7 bulbs and only those bulbs light up which are required to represent a number and rest other would be completely off.)

Help Jarvis and conserve energy.

Seven segment display – https://en.wikipedia.org/wiki/Seven-segment_display

enter image description here

Input:
First line will contain the number of test cases and for every test case first line will contain length of favorite list and the second line for a test case will contain n numbers

Output:
For every test case print the answer. If there exist more than 1 numbers for which same number of bulbs are required than output the number which occurs first in the Favorite list.

Constraints:
Test cases< 10
A[i] < 10^6
Size of list < 10^5

SAMPLE INPUT
1
5
1 2 3 4 5
SAMPLE OUTPUT
1
Explanation

Number 1 needs only two bulbs to represent.

Code:

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.HashMap;
 
 
 
 
public class Solution {
 
 
 public static void main(String args[]) throws IOException{
 InputReader in=new InputReader(System.in);
 PrintWriter out=new PrintWriter(System.out);
 int T=in.readInt();
 
 while(T-->0){
 long N=in.readLong();
 long min=222222222;
 long minnum=0;
 while(N-->0){
 long num=in.readCount();
 long count=in.getcount();
 if(count<min){
 min=count;
 minnum=num;
 }
 }
 out.println(minnum);
 out.flush();
 
 }
 
 out.close();
 
 }
}
 
class InputReader{
 InputStream in;
 int[] segment={6,2,5,5,4,5,6,3,7,6};
 long count=0;
 InputReader(InputStream in){
 this.in=in;
 }
 
 public long getcount(){
 return count;
 }
 private int read() throws IOException{
 return in.read();
 }
 
 
 
 public char readChar() throws IOException{
 int n=read();
 while(isWhiteSpace(n)){
 n=read();
 }
 return (char)n;
 }
 
 public int readInt() throws IOException{
 int number=0;
 int n=read();
 while(isWhiteSpace(n)){
 n=read();
 }
 while(!isWhiteSpace(n)){
 int integer=n-'0';
 number*=10;
 number+=integer;
 n=read();
 }
 return number;
 }
 
 public long readCount() throws IOException{
 long number=0;
 count=0;
 int n=read();
 while(isWhiteSpace(n)){
 n=read();
 }
 while(!isWhiteSpace(n)){
 int integer=n-'0';
 count+=segment[integer];
 number*=10;
 number+=integer;
 n=read();
 }
 return number;
 }
 
 
 public long readLong() throws IOException{
 long number=0;
 int n=read();
 while(isWhiteSpace(n)){
 n=read();
 }
 while(!isWhiteSpace(n)){
 int integer=n-'0';
 number*=10;
 number+=integer;
 n=read();
 }
 return number;
 }
 
 private boolean isWhiteSpace(int n){
 if(n=='\n'||n=='\r'||n=='\t'||n==' '||n==-1){
 return true;
 }else{
 return false;
 }
 
 }
 }

Java 8 Coding challenge: Raees’s Liquor Tank

enter image description here

Raees wants to build a new tank to hide his illegal liquor . He comes up with some designs . Now he asks you to find out maximum units of liquor he can fill in the tank.

You are given N elements , corresponding to heights of walls . See the figure for input 0 ,8 ,0 ,4 ,0 ,3 ,0 ,5 ,0 ,3 ,0 ,1 ,0.

Maximum amount of liquor that can be filled is 5 + 1 + 5 + 2 + 5 + 3 + 1 = 22.

Eg : For input 4 ,2 ,4 ,3 Output will be 2 .

INPUT

  • First line of input contains a single integer N.
  • Second line of input contains N non-negative integers ( A0A(n-1) ).

OUTPUT

  • Single integer , answer to the Raees’s question.

CONSTRAINTS

  • 1 <= N <= 1000
  • 1 <= Ai <= 1000
SAMPLE INPUT
13
0 8 0 4 0 3 0 5 0 3 0 1 0
SAMPLE OUTPUT
22

 Code:

import java.io.*;
import java.util.*;
import java.math.*;
import java.lang.*;
import static java.lang.Math.*;
 
public class Q1
{
 
 static class InputReader
 {
 private InputStream stream;
 private byte[] buf = new byte[1024];
 private int curChar;
 private int numChars;
 private SpaceCharFilter filter;
 
 public InputReader(InputStream stream)
 {
 this.stream = stream;
 }
 
 public int read()
 {
 if (numChars==-1) 
 throw new InputMismatchException();
 
 if (curChar >= numChars)
 {
 curChar = 0;
 try 
 {
 numChars = stream.read(buf);
 }
 catch (IOException e)
 {
 throw new InputMismatchException();
 }
 
 if(numChars <= 0) 
 return -1;
 }
 return buf[curChar++];
 }
 
 public String nextLine()
 {
 BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
 String str = "";
 try
 {
 str = br.readLine();
 }
 catch (IOException e)
 {
 e.printStackTrace();
 }
 return str;
 }
 public int nextInt()
 {
 int c = read();
 
 while(isSpaceChar(c)) 
 c = read();
 
 int sgn = 1;
 
 if (c == '-') 
 {
 sgn = -1;
 c = read();
 }
 
 int res = 0;
 do 
 {
 if(c<'0'||c>'9') 
 throw new InputMismatchException();
 res *= 10;
 res += c - '0';
 c = read();
 }
 while (!isSpaceChar(c)); 
 
 return res * sgn;
 }
 
 public long nextLong() 
 {
 int c = read();
 while (isSpaceChar(c))
 c = read();
 int sgn = 1;
 if (c == '-') 
 {
 sgn = -1;
 c = read();
 }
 long res = 0;
 
 do 
 {
 if (c < '0' || c > '9')
 throw new InputMismatchException();
 res *= 10;
 res += c - '0';
 c = read();
 }
 while (!isSpaceChar(c));
 return res * sgn;
 }
 
 public double nextDouble() 
 {
 int c = read();
 while (isSpaceChar(c))
 c = read();
 int sgn = 1;
 if (c == '-') 
 {
 sgn = -1;
 c = read();
 }
 double res = 0;
 while (!isSpaceChar(c) && c != '.') 
 {
 if (c == 'e' || c == 'E')
 return res * Math.pow(10, nextInt());
 if (c < '0' || c > '9')
 throw new InputMismatchException();
 res *= 10;
 res += c - '0';
 c = read();
 }
 if (c == '.') 
 {
 c = read();
 double m = 1;
 while (!isSpaceChar(c)) 
 {
 if (c == 'e' || c == 'E')
 return res * Math.pow(10, nextInt());
 if (c < '0' || c > '9')
 throw new InputMismatchException();
 m /= 10;
 res += (c - '0') * m;
 c = read();
 }
 }
 return res * sgn;
 }
 
 public String readString() 
 {
 int c = read();
 while (isSpaceChar(c))
 c = read();
 StringBuilder res = new StringBuilder();
 do 
 {
 res.appendCodePoint(c);
 c = read();
 } 
 while (!isSpaceChar(c));
 
 return res.toString();
 }
 
 public boolean isSpaceChar(int c) 
 {
 if (filter != null)
 return filter.isSpaceChar(c);
 return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
 }
 
 public String next() 
 {
 return readString();
 }
 
 public interface SpaceCharFilter 
 {
 public boolean isSpaceChar(int ch);
 }
 }
 
 public static int[] SOE()
 {
 int n=(int)(1e5); //till which digit
 
 int ip[]=new int[n];
 
 for(int i=2;i<(int)Math.sqrt(n);i++)
 {
 if(ip[i]==0)
 {
 for(int j=i*i;j<n;j=j+i)
 {
 ip[j]++;
 }
 }
 }
 
 ip[1]++; //counts 1 and 0 as primes
 ip[0]++;
 
 return ip;
 }
 
 private static long gcd(long a, long b)
 {
 while (b > 0)
 {
 long temp = b;
 b = a % b; // % is remainder
 a = temp;
 }
 return a;
}
 
 private static long gcd(long[] input)
 {
 long result = input[0];
 
 for(int i = 1; i < input.length; i++) 
 result = gcd(result, input[i]);
 
 return result;
 }
 
 private static long lcm(long a, long b)
 {
 return a * (b / gcd(a, b));
 }
 
 private static long lcm(long[] input)
 {
 long result = input[0];
 
 for(int i = 1; i < input.length; i++) 
 result = lcm(result, input[i]);
 
 return result;
 }
 
 public static void Array_2dsort(Integer[][] a)
 {
 
 Arrays.sort(a, new Comparator<Integer[]>() {
 public int compare(Integer[] int1, Integer[] int2) {
 Integer numOfKeys1 = int1[1]; //about which column u want to sort
 Integer numOfKeys2 = int2[1];
 return numOfKeys1.compareTo(numOfKeys2);
 }
 });
 
 }
 
 
 public static boolean Square(int x)
 {
 boolean ans=false;
 
 int a=(int)Math.sqrt(x);
 
 if(a*a==x)
 ans=true;
 
 return ans;
 }
 
 public static void main(String args[]) throws Exception
 {
 InputReader in=new InputReader(System.in); 
 PrintWriter w=new PrintWriter(System.out);
 
 int n=in.nextInt();
 
 int a[]=new int[n];
 
 for(int i=0;i<n;i++)
 a[i]=in.nextInt();
 
 
 int total=0;
 int l[]=new int[n];
 int r[]=new int[n];
 l[0]=a[0];
 r[n-1]=a[n-1];
 
 for(int i=1;i<n;i++)
 l[i]=(int)Math.max(l[i-1],a[i]);
 
 for(int i=n-2;i>=0;i--)
 r[i]=(int)Math.max(r[i+1],a[i]);
 
 for(int i=0;i<n;i++)
 total+=(int)Math.min(l[i],r[i])-a[i];
 
 w.println(total);
 
 w.close(); 
 }
}

Java 8 Coding challenge: Swapping Characters

Your are given a string of length N. You have to follow some rounds of swapping in the string until the below explained condition is reached.

In each round the, you have to swap the ith and (i+1)th character , then (i+2)th and (i+3)th character and continue till the end of string. In each round , a character from the left will be locked i.e not available for swapping after this round , thus after some rounds of swapping , all the characters will be locked at their final position to stop any more rounds of swapping .

See the sample explanation for more clarity.

Input

The first line of input contains a T, the number of test case. The first line of each Test case will contain an Integer N , denoting the length of the string. The second line of each Test case will contain a string of length N.

Output

For each and every test case, output the string after all positions are locked.

Constraints

  1. 0<T<100
  2. 0<N<10000
SAMPLE INPUT
2
6
abcdef
3
abc
SAMPLE OUTPUT
bdfeca
bca
Explanation

For the first test case , the given string is “abcdef” and all the characters are unlocked.

  1. After First round of swap , the string becomes “badcfe” . Now the leftmost unlocked character is locked i.e the string is now “b_adcfe”.
  2. After Second round of swap , the string becomes “b_dafce”. Now the leftmost unlocked character is locked i.e string is now “bd_afce”.
  3. After Third round of swap , the string becomes “bdfaec” . Now the leftmost unlocked character is locked i.e the string is now “bdf_aec”.
  4. After Fourth round of swap , the string becomes “bdfeac” . Now the leftmost unlocked character is locked i.e the string is now “bdfe_ac”..
  5. After Fifth round of swap , the string becomes “bdfeca” . Now the leftmost unlocked character is locked i.e the string is now “bdfec_a”.
  6. In the sixth round no more swap occur as there is only single unlocked character is left.

** (‘-‘ is the partition , where all the characters to its left are locked characters and all the characters to it right are unlocked characters).**

Therefore , we print the string “bdfeca”.

Code:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
class Main {
 
 public static void main(String args[] ) throws IOException {
 
 BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
 for(int k=Integer.parseInt(br.readLine());k>0;k--){
 br.readLine();
 String str=br.readLine();
 StringBuilder s=new StringBuilder(str);
 int start=0;
 int end=str.length()-1;
 for(int i=1;i<str.length();i+=2){
 s.setCharAt(start++, str.charAt(i));
 }
 for(int i=0;i<str.length();i+=2){
 s.setCharAt(end--, str.charAt(i));
 }
 System.out.println(s);
 }
 }
 public static void swap(StringBuilder s,int i,int j){
 char c=s.charAt(i);
 char d=s.charAt(j);
 s.setCharAt(i, d);
 s.setCharAt(j, c);
 }
 
}

Java 8 coding challenge: Addition ain’t simple

Jack is awesome. His friends call him little Einstein. To test him, his friends gave him a string. They told him to add the string with its reverse string and follow these rules:

  1. Every ith character of string will be added to every ith character of reverse string.
  2. Both string will contain only lower case alphabets(a-z).
  3. Eg:- a+a=b,a+c=d,z+a=a (Refer to sample test cases for more details)

Input:

First line contains a value N denoting number of test cases. Next N lines contains string str.

Output:

For every string str output the string that Jack’s friends wants from him.

Constraints

1 <= N <= 10

1 <= str <= 10^5

SAMPLE INPUT
4
hello
codeapocalypse
programming
world
SAMPLE OUTPUT
wqxqw
hhtdmqrrqmdthh
wfxtebetxfw
aajaa

Code:

import java.io.*;
import java.util.HashMap;
import java.util.InputMismatchException;
import java.util.Map;
 

public class AdditionAintSimple {
 static class Print {
 private final BufferedWriter bw;
 
 public Print() {
 this.bw = new BufferedWriter(new OutputStreamWriter(System.out));
 }
 
 public void print(Object object) throws IOException {
 bw.append("" + object);
 }
 
 public void println(Object object) throws IOException {
 print(object);
 bw.append("\n");
 }
 
 public void close() throws IOException {
 bw.close();
 }
 }
 
 static class Reader
 {
 final private int BUFFER_SIZE = 1 << 16;
 private DataInputStream din;
 private byte[] buffer;
 private int bufferPointer, bytesRead;
 
 public Reader()
 {
 din = new DataInputStream(System.in);
 buffer = new byte[BUFFER_SIZE];
 bufferPointer = bytesRead = 0;
 }
 
 public Reader(String file_name) throws IOException
 {
 din = new DataInputStream(new FileInputStream(file_name));
 buffer = new byte[BUFFER_SIZE];
 bufferPointer = bytesRead = 0;
 }
 
 public String readLine() throws IOException
 {
 byte[] buf = new byte[100000]; // line length
 int cnt = 0, c;
 while ((c = read()) != -1)
 {
 if (c == '\n')
 break;
 buf[cnt++] = (byte) c;
 }
 return new String(buf, 0, cnt);
 }
 
 public int nextInt() throws IOException
 {
 int ret = 0;
 byte c = read();
 while (c <= ' ')
 c = read();
 boolean neg = (c == '-');
 if (neg)
 c = read();
 do
 {
 ret = ret * 10 + c - '0';
 } while ((c = read()) >= '0' && c <= '9');
 
 if (neg)
 return -ret;
 return ret;
 }
 
 public long nextLong() throws IOException
 {
 long ret = 0;
 byte c = read();
 while (c <= ' ')
 c = read();
 boolean neg = (c == '-');
 if (neg)
 c = read();
 do {
 ret = ret * 10 + c - '0';
 }
 while ((c = read()) >= '0' && c <= '9');
 if (neg)
 return -ret;
 return ret;
 }
 
 public double nextDouble() throws IOException
 {
 double ret = 0, div = 1;
 byte c = read();
 while (c <= ' ')
 c = read();
 boolean neg = (c == '-');
 if (neg)
 c = read();
 
 do {
 ret = ret * 10 + c - '0';
 }
 while ((c = read()) >= '0' && c <= '9');
 
 if (c == '.')
 {
 while ((c = read()) >= '0' && c <= '9')
 {
 ret += (c - '0') / (div *= 10);
 }
 }
 
 if (neg)
 return -ret;
 return ret;
 }
 
 private void fillBuffer() throws IOException
 {
 bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
 if (bytesRead == -1)
 buffer[0] = -1;
 }
 
 private byte read() throws IOException
 {
 if (bufferPointer == bytesRead)
 fillBuffer();
 return buffer[bufferPointer++];
 }
 
 public void close() throws IOException
 {
 if (din == null)
 return;
 din.close();
 }
 }
 
 public static void main(String[] args) throws Exception{
 Reader rd = new Reader();
 Print pr = new Print();
 
 int t = rd.nextInt();
 String ip;
 
 
 int len;
 
 while(t-->0) {
 ip = rd.readLine();
 len = ip.length();
 char op[] = new char[len];
 
 for (int i = 0; i < len; i++) {
 op[i] = (char) ((( ip.charAt(i) + ip.charAt(len - i - 1) - 193) % 26)+97);
 }
 pr.println(String.valueOf(op));
 }
 pr.close();
 }
}

Java 8 Coding Challenge: COUNT NUMBERS

Your task is pretty simple , given a string S , find the total count of numbers present in the digit.

Input

The first line contains T , the number of test cases. The first line of each and every testc ase will contain a integer N , the length of the string . The second line of each and every test case will contain a string S of length N.

Output

For each and every testcase , output the total count of numbers present in the string.

Constraints

  1. 0<T<200
  2. 0<N<10000
SAMPLE INPUT
1
26
sadw96aeafae4awdw2wd100awd
SAMPLE OUTPUT
4
Explanation

For the first test case , the string given is “sadw96aeafae4awdw2wd100awd”. There are total of 4 numbers in the string – [96,4,2,100]. So , we output 4.

Code

import java.io.BufferedReader;
import java.io.InputStreamReader;
 
class TestClass {
 public static void main(String args[] ) throws Exception {
 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
 int T = Integer.parseInt(br.readLine());
 
 boolean bit=false;
 int count=0,i=0,j=0,N=0;
 char c='.';
 String ST = "";
 
 for (i=0; i<T; i++)
 {
 count=0;
 N = Integer.parseInt(br.readLine());
 ST = br.readLine();
 
 for(j=0; j<N; j++)
 {
 c = ST.charAt(j);
 if(c>47 && c<58)
 bit=true;
 else
 { if(bit)
 { bit=false;
 count++;
 }
 }
 }
 if(bit)
 { bit=false;
 count++;
 }
 System.out.println(count);
 }
 }
}

Java 8 coding challenge: Count Divisors

Problem:

You have been given 3 integers l, r and k. Find how many numbers between l and r (both inclusive) are divisible by k. You do not need to print these numbers, you just have to find their count.

Input Format
The first and only line of input contains 3 space separated integers l, r and k.

Output Format
Print the required answer on a single line.

Constraints
1lr10001≤l≤r≤1000
1k10001≤k≤1000

SAMPLE INPUT
1 10 1
SAMPLE OUTPUT
10

Code:

/* IMPORTANT: Multiple classes and nested static classes are supported */
 
/*
 * uncomment this if you want to read input.
//imports for BufferedReader
import java.io.BufferedReader;
import java.io.InputStreamReader;
 
//import for Scanner and other utility classes
import java.util.*;
*/
import java.util.Scanner;
 
class TestClass {
 public static void main(String args[] ) throws Exception {
 Scanner sc = new Scanner(System.in);
 
 int l = sc.nextInt();
 int r = sc.nextInt();
 int k = sc.nextInt();
 
 sc.close();
 int count = 0;
 if(l == r && l%k != 0) {
 System.out.println(0);
 System.exit(0);
 }
 
 while (l <= r) {
 if(l % k == 0) {
 break;
 }
 l++;
 }
 
 count = (r - l) / k + 1;
 System.out.println(count);
 }
}

Java 8 coding challenge: Find Product

Problem:

You have been given an array A of size N

consisting of positive integers. You need to find and print the product of all the number in this array Modulo  109+7.

Input Format:
The first line contains a single integer N

N denoting the size of the array. The next line contains N

N space separated integers denoting the elements of the array

Output Format:
Print a single integer denoting the product of all the elements of the array Modulo 109+7.

Constraints:
1≤N≤103

1≤A[i]≤103

 

SAMPLE INPUT
5
1 2 3 4 5
SAMPLE OUTPUT
120

Code:

import java.lang.Math;
import java.util.Scanner;
class TestClass
{
 public static void main(String args[] ) throws Exception 
 {
 Scanner sc=new Scanner(System.in);
 int []A=new int[1000];
 int a=sc.nextInt();
 int i;
 double answer=1.0;
 for(i=0;i<a;i++)
 {
 A[i]=sc.nextInt();
 }
 for(i=0;i<a;i++)
 {
 answer=(A[i]*answer)%((Math.pow(10,9))+7);
 
 }
 System.out.println((int)answer);
 }
}

Java 8 coding challenge: Palindromic String

Problem:

You have been given a String SS. You need to find and print whether this string is a palindrome or not. If yes, print “YES” (without quotes), else print “NO” (without quotes).

Input Format
The first and only line of input contains the String SS. The String shall consist of lowercase English alphabets only.

Output Format
Print the required answer on a single line.

Constraints 1|S|1001≤|S|≤100

Note
String SS consists of lowercase English Alphabets only.

SAMPLE INPUT
aba
SAMPLE OUTPUT
YES

Code:

/* IMPORTANT: Multiple classes and nested static classes are supported */
 
/*
 * uncomment this if you want to read input.
//imports for BufferedReader
import java.io.BufferedReader;
import java.io.InputStreamReader;
*/
//import for Scanner and other utility classes
import java.util.*;
 
 
class TestClass {
 public static void main(String args[] ) throws Exception {
 /*
 * Read input from stdin and provide input before running
 * Use either of these methods for input
 
 //BufferedReader
 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
 String line = br.readLine();
 int N = Integer.parseInt(line);
 */
 //Scanner
 Scanner s = new Scanner(System.in);
 String S = s.nextLine();
 String result="";
 
 for (int i = S.length()-1; i >=0; i--) {
 result=result+S.substring(i,i+1);
 }
 if(result.equals(S)){
 System.out.println("YES");
 }
 else{
 System.out.println("NO");
 }
 }
}

Java 8 coding challenge: Toggle String

Problem:

You have been given a String SS consisting of uppercase and lowercase English alphabets. You need to change the case of each alphabet in this String. That is, all the uppercase letters should be converted to lowercase and all the lowercase letters should be converted to uppercase. You need to then print the resultant String to output.

Input Format
The first and only line of input contains the String SS

Output Format
Print the resultant String on a single line.

Constraints
1|S|1001≤|S|≤100 where |S||S| denotes the length of string SS.

 

SAMPLE INPUT
abcdE
SAMPLE OUTPUT
ABCDe

 

Code:

import java.util.Scanner;
import java.lang.StringBuilder;
 
class TestClass {
 public static void main(String args[] ) throws Exception {
 Scanner sc= new Scanner(System.in);
 String s,s1,sx;
 char[] temp=new char[100];
 s=sc.nextLine();
 int x=s.length();
 for(int i=0;i<x;i++){
 char c = s.charAt(i);
 s1=Character.toString(c);
 if(Character.isLowerCase(c)){
 sx=s1.toUpperCase();
 temp[i]=sx.charAt(0);
 }
 else if(Character.isUpperCase(c)){
 
 sx=s1.toLowerCase();
 temp[i]=sx.charAt(0);
 }
 else{
 temp[i]=c;
 }
 }
 String s2= new String(temp);
 s2=s2.substring(0,x);
 System.out.println(s2);
 }
}

Java 8 coding challenge: Magical Word

Problem:

Dhananjay has recently learned about ASCII values.He is very fond of experimenting. With his knowledge of ASCII values and character he has developed a special word and named it Dhananjay’s Magical word.

A word which consist of alphabets whose ASCII values is a prime number is an Dhananjay’s Magical word. An alphabet is Dhananjay’s Magical alphabet if its ASCII value is prime.

Dhananjay’s nature is to boast about the things he know or have learnt about. So just to defame his friends he gives few string to his friends and ask them to convert it to Dhananjay’s Magical word. None of his friends would like to get insulted. Help them to convert the given strings to Dhananjay’s Magical Word.

Rules for converting:

1.Each character should be replaced by the nearest Dhananjay’s Magical alphabet.

2.If the character is equidistant with 2 Magical alphabets. The one with lower ASCII value will be considered as its replacement.

Input format:

First line of input contains an integer T number of test cases. Each test case contains an integer N (denoting the length of the string) and a string S.

Output Format:

For each test case, print Dhananjay’s Magical Word in a new line.

Constraints:

1 <= T <= 100

1 <= |S| <= 500

SAMPLE INPUT
1
6
AFREEN
SAMPLE OUTPUT
CGSCCO
Explanation

ASCII values of alphabets in AFREEN are 65, 70, 82, 69 ,69 and 78 respectively which are converted to CGSCCO with ASCII values 67, 71, 83, 67, 67, 79 respectively. All such ASCII values are prime numbers.

Code:

import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.Map;
 

public class MagicalWord {
 static class Print {
 private final BufferedWriter bw;
 
 public Print() {
 this.bw = new BufferedWriter(new OutputStreamWriter(System.out));
 }
 
 public void print(Object object) throws IOException {
 bw.append("" + object);
 }
 
 public void println(Object object) throws IOException {
 print(object);
 bw.append("\n");
 }
 
 public void close() throws IOException {
 bw.close();
 }
 }
 static class Reader {
 final private int BUFFER_SIZE = 1 << 16;
 private DataInputStream din;
 private byte[] buffer;
 private int bufferPointer, bytesRead;
 
 public Reader() {
 din = new DataInputStream(System.in);
 buffer = new byte[BUFFER_SIZE];
 bufferPointer = bytesRead = 0;
 }
 
 public String readLine() throws IOException {
 byte[] buf = new byte[100000]; // line length
 int cnt = 0, c;
 while ((c = read()) != -1) {
 if (c == '\n')
 break;
 buf[cnt++] = (byte) c;
 }
 return new String(buf, 0, cnt);
 }
 
 public int nextInt() throws IOException {
 int ret = 0;
 byte c = read();
 while (c <= ' ')
 c = read();
 boolean neg = (c == '-');
 if (neg)
 c = read();
 do {
 ret = ret * 10 + c - '0';
 } while ((c = read()) >= '0' && c <= '9');
 
 if (neg)
 return -ret;
 return ret;
 }
 
 private void fillBuffer() throws IOException {
 bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
 if (bytesRead == -1)
 buffer[0] = -1;
 }
 
 private byte read() throws IOException {
 if (bufferPointer == bytesRead)
 fillBuffer();
 return buffer[bufferPointer++];
 }
 
 public void close() throws IOException {
 if (din == null)
 return;
 din.close();
 }
 }
 
 public static void main(String[] args) throws Exception{
 Print pr = new Print();
 Reader rd = new Reader();
 
 
 Map<Character,Character> map = new HashMap<>();
 map.put('A','C');
 map.put('B','C');
 map.put('C','C');
 map.put('D','C');
 map.put('E','C');
 map.put('F','G');
 map.put('G','G');
 map.put('H','G');
 map.put('I','I');
 map.put('J','I');
 map.put('K','I');
 map.put('L','I');
 map.put('M','O');
 map.put('N','O');
 map.put('O','O');
 map.put('P','O');
 map.put('Q','O');
 map.put('R','S');
 map.put('S','S');
 map.put('T','S');
 map.put('U','S');
 map.put('V','S');
 map.put('W','Y');
 map.put('X','Y');
 map.put('Y','Y');
 map.put('Z','Y');
 map.put('a','a');
 map.put('b','a');
 map.put('c','a');
 map.put('d','e');
 map.put('e','e');
 map.put('f','e');
 map.put('g','g');
 map.put('h','g');
 map.put('i','g');
 map.put('j','k');
 map.put('k','k');
 map.put('l','k');
 map.put('m','m');
 map.put('n','m');
 map.put('o','m');
 map.put('p','q');
 map.put('q','q');
 map.put('r','q');
 map.put('s','q');
 map.put('t','q');
 map.put('u','q');
 map.put('v','q');
 map.put('w','q');
 map.put('x','q');
 map.put('y','q');
 map.put('z','q');
 
 map.put('\0','C');
 map.put('!','C');
 map.put('"','C');
 map.put('#','C');
 map.put('$','C');
 map.put('%','C');
 map.put('&','C');
 map.put('\'','C');
 map.put('(','C');
 map.put(')','C');
 map.put('*','C');
 map.put('+','C');
 map.put(',','C');
 map.put('-','C');
 map.put('.','C');
 map.put('/','C');
 map.put(':','C');
 map.put(';','C');
 map.put('<','C');
 map.put('=','C');
 map.put('>','C');
 map.put('?','C');
 map.put('@','C');
 map.put('[','Y');
 map.put('\\','Y');
 map.put(']','Y');
 map.put('^','a');
 map.put('_','a');
 map.put('`','a');
 map.put('{','q');
 map.put('|','q');
 map.put('}','q');
 map.put('~','q');
 
 map.put('0','C');
 map.put('1','C');
 map.put('2','C');
 map.put('3','C');
 map.put('4','C');
 map.put('5','C');
 map.put('6','C');
 map.put('7','C');
 map.put('8','C');
 map.put('9','C');
 
 int t = rd.nextInt();
 while(t-->0){
 int len = rd.nextInt();
 StringBuilder ip= new StringBuilder(rd.readLine());
 char op[] = new char[len];
 for(int i=0;i<len;i++){
 op[i] = map.get(ip.charAt(i));
 }
 pr.println(String.valueOf(op));
 }
 pr.close();
 }
 
}