import java.util.*;
import java.util.regex.*;
import java.lang.*;
import java.io.*;

class Ideone
{
	public static void main (String[] args) throws java.lang.Exception
	{
		String s = "This is a test\nAnd this is also a test\nAnd these are also tests\ntest\nЭто тест\nЭто также тест\nИ это также тесты";
		String rx = "(?sU)\\b(\\w+)\\b(?=.*\\b\\1\\b)";
		List<String> matches = new ArrayList<>();
		Matcher m = Pattern.compile(rx).matcher(s);
		while (m.find()) {
			matches.add(m.group());
		}
		System.out.println(s.replaceAll("(?U)\\b(?:" + String.join("|", matches) + ")\\b", "_$0"));
	}
}