View Code of Problem 2591

import java.util.*;
public class Main {
	public static void main(String[] args) {
		Scanner in =new Scanner(System.in);
		int t =in.nextInt();
		for(int i = 0;i<t;i++) {
			char[] a = in.next().toCharArray();
			char[] b = in.next().toCharArray();
			ArrayList<Character> al= new ArrayList<Character>(); 
			for(int j =0;j<a.length;j++) {
				for(int l =0;l<b.length;l++) {
					if(a[j]==b[l]) {
						al.add(b[l]);
						b[l]=' ';
						break;
					}
				}
			}
			Collections.sort(al);
			if(al.size()>0) {
				for(char c:al) {
					System.out.print(c);
				}
			}else {
				System.out.print(-1);
			}
			System.out.println();
		}
	}
}

Double click to view unformatted code.


Back to problem 2591