Problem:
Score : 100 points

Problem Statement

You are given two strings s and t consisting of lowercase English letters and an integer L.

We will consider generating a string of length L by concatenating one or more copies of s and t. Here, it is allowed to use the same string more than once.

For example, when s = at, t = code and L = 6, the strings atatat, atcode and codeat can be generated.

Among the strings that can be generated in this way, find the lexicographically smallest one. In the cases given as input, it is always possible to generate a string of length L.

Constraints

1 ≤ L ≤ 2 × 10^5

1 ≤ |s|, |t| ≤ L

s and t consist of lowercase English letters.

It is possible to generate a string of length L in the way described in Problem Statement.

Input

Input is given from Standard Input in the following format:

N
x_1 s_1
x_2 s_2
:
x_N s_N

Output

Print the lexicographically smallest string among the ones that can be generated in the way described in Problem Statement.

Sample Input 1

6
at
code

Sample Output 1

atatat

This input corresponds to the example shown in Problem Statement.

Sample Input 2

8
coding
festival

Sample Output 2

festival

It is possible that either s or t cannot be used at all in generating a string of length L.

Sample Input 3

8
same
same

Sample Output 3

samesame

It is also possible that s = t.

Sample Input 4

10
coin
age

Sample Output 4

ageagecoin

Solution in C (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(){
	int L, i, j, k, l, A, B, tmp;
	char c;
	scanf("%d", &L);
	char *S = (char *)malloc(sizeof(char) * (L + 1));
	char *T = (char *)malloc(sizeof(char) * (L + 1));
	char *strtmp;
	scanf("%s", S);
	scanf("%s", T);
	for(A = 0; S[A] != '\0'; A++){}
	for(B = 0; T[B] != '\0'; B++){}
	if(A > B){
		strtmp = S;
		S = T;
		T = strtmp;
		tmp = A;
		A = B;
		B = tmp;
	}
	c = T[B];
	T[B] = '\0';
	if(strcmp(S, T) == 0 && c < S[0]){
		T[B] = c;
		strtmp = S;
		S = T;
		T = strtmp;
		tmp = A;
		A = B;
		B = tmp;
	}
	else{
		T[B] = c;
		if(strcmp(S, T) > 0){
			strtmp = S;
			S = T;
			T = strtmp;
			tmp = A;
			A = B;
			B = tmp;
		}
	}
	for(l = 0; B * l <= L; l++){
		if((L - B * l) % A == 0){
			k = (L - B * l) / A;
			break;
		}
	}
	for(i = 0; i < k; i++){
		for(j = 0; j < A; j++){
			printf("%c", S[j]);
		}
	}
	for(i = 0; i < l; i++){
		for(j = 0; j < B; j++){
			printf("%c", T[j]);
		}
	}
	printf("\n");
	return 0;
}