Problem:
Set Intersection

Find the intersection of two sets $A = \{a_0, a_1, ..., a_{n-1}\}$ and $B = \{b_0, b_1, ..., b_{m-1}\}$.

Input

The input is given in the following format.

$n$
$a_0 \; a_1 \; ... \; a_{n-1}$
$m$
$b_0 \; b_1 \; ... \; b_{m-1}$

Elements of $A$ and $B$ are given in ascending order respectively. There are no duplicate elements in each set.

Output

Print elements in the intersection in ascending order. Print an element in a line.

Constraints

$1 \leq n, m \leq 200,000$

$0 \leq a_0 < a_1 < ... < a_{n-1} \leq 10^9$

$0 \leq b_0 < b_1 < ... < b_{m-1} \leq 10^9$

Sample Input 1

4
1 2 5 8
5
2 3 5 9 11

Sample Output 1

2
5

Solution in JAVA (judge verdict: Accepted)

import java.util.ArrayList;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;

class Main {

	public static void main(String[] args) {
		Set<Long> set = new TreeSet<Long>();
		Set<Long> ans = new TreeSet<Long>();
		try (Scanner sc = new Scanner(System.in)){
			int times = sc.nextInt();
			for (int i=0; i<times; i++) {
				Long l = sc.nextLong();
				boolean check = set.add(l);
				if (!check) ans.add(l);  //重複した場合ansに追加
			}
			times = sc.nextInt();
			for (int i=0; i<times; i++) {
				Long l = sc.nextLong();
				boolean check = set.add(l);
				if (!check) ans.add(l);  //重複した場合ansに追加
			}
			ArrayList<Long>  a = new ArrayList<Long>(ans);
			for (int i=0; i<a.size(); i++) System.out.println(a.get(i));
		}
	}
}