A. Entertainment in MAC

time limit per test

1 second

memory limit per test

256 megabytes

Congratulations, you have been accepted to the Master's Assistance Center! However, you were extremely bored in class and got tired of doing nothing, so you came up with a game for yourself.

You are given a string ss and an even integer nn. There are two types of operations that you can apply to it:

  1. Add the reversed string ss to the end of the string ss (for example, if s=s= cpm, then after applying the operation s=s= cpmmpc).
  2. Reverse the current string ss (for example, if s=s= cpm, then after applying the operation s=s= mpc).

It is required to determine the lexicographically smallest†† string that can be obtained after applying exactly nn operations. Note that you can apply operations of different types in any order, but you must apply exactly nn operations in total.

††A string aa is lexicographically smaller than a string bb if and only if one of the following holds:

  • aa is a prefix of bb, but a≠ba≠b;
  • in the first position where aa and bb differ, the string aa has a letter that appears earlier in the alphabet than the corresponding letter in bb.

Input

Each test consists of multiple test cases. The first line contains a single integer tt (1≤t≤5001≤t≤500) --- the number of test cases. The description of the test cases follows.

The first line of each test case contains a single even integer nn (2≤n≤1092≤n≤109) --- the number of operations applied to the string ss.

The second line of each test case contains a single string ss (1≤|s|≤1001≤|s|≤100), consisting of lowercase English letters, --- the string to which the operations are applied.

Output

For each test case, output a single line --- the lexicographically smallest string that can be obtained after applying exactly nn operations.

Example

Input

Copy

复制代码

5

4

cpm

2

grib

10

kupitimilablodarbuz

1000000000

capybara

6

abacaba

Output

Copy

复制代码
cpm
birggrib
kupitimilablodarbuz
arabypaccapybara
abacaba

Note

In the first test case, you can apply the operation of the second type (i.e., reverse the string ss) 44 times. Then the string ss will remain equal to cpm.

In the second test case, you can do the following:

  • Apply the operation of the second type, after which ss will become equal to birg.
  • Apply operation of the first type (i.e., add the reversed string ss to the end of the string ss), after which ss will become equal to birggrib.

解题说明:此题是一道字符串题,找规律能发现如果这个字符串翻转后更大,那么我们就可以进行n次翻转,这样最终的字符串就是原先的字符串,也就实现最小了。如果这个字符串s在反转后更小了,那么我们就必须执行奇数次翻转,才能保证这个s最终是较小的。但题目中说n是偶数,所以就至少一次拼接操作,拼接操作进行的越少越好, 因此翻转n − 1次,拼接一次。

cpp 复制代码
#include<iostream>
#include <cstring>
#include <algorithm>
#include<vector>
using namespace std;

void solve() 
{
	int n;
	cin >> n;
	string str;
	cin >> str;
	string rs = str;
	reverse(rs.begin(), rs.end());
	if (rs < str) 
	{
		cout << (rs + str) << '\n';
	}
	else
	{
		cout << str << '\n';
	}
}
int main() 
{
	int T = 1;
	cin >> T;
	while (T--)
	{
		solve();
	}
	return 0;
}
相关推荐
Ashore11_3 分钟前
蓝桥杯16届Java研究生组
java·算法·蓝桥杯
6Hzlia6 分钟前
【Hot 100 刷题计划】 LeetCode 76. 最小覆盖子串 | C++ 滑动窗口题解
c++·算法·leetcode
像素猎人10 分钟前
蓝桥杯OJ2049蓝桥勇士【动态规划】【dp[n]不是符合题意的答案,只是以an结尾的子问题的答案】
c++·算法·蓝桥杯·动态规划·区间dp
羊小猪~~10 分钟前
LLM--SFT简介
python·考研·算法·ai·大模型·llm·微调
广州灵眸科技有限公司22 分钟前
瑞芯微(EASY EAI)RV1126B 人脸98关键点算法识别
开发语言·科技·嵌入式硬件·物联网·算法·php
篮子里的玫瑰26 分钟前
FreeRTOS:信号量与互斥量在DMA串口发送中的实战剖析
stm32·单片机·嵌入式硬件·算法
hughnz27 分钟前
钻头技术持续突飞猛进:地热钻探领域的创新
人工智能·算法
xiaoye-duck33 分钟前
《算法题讲解指南:动态规划算法--子数组系列》--21.乘积最大子数组,22.乘积为正数的最长子数组
c++·算法·动态规划
MicroTech202534 分钟前
突破非幺正动力学瓶颈:MLGO微算法科技量子虚时演化赋能开放量子系统模拟
科技·算法·量子计算
计算机安禾36 分钟前
【数据结构与算法】第24篇:哈夫曼树与哈夫曼编码
c语言·开发语言·数据结构·c++·算法·visual studio