1681 B Card Trick(无限循环数组)

B. Card Trick

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Monocarp has just learned a new card trick, and can't wait to present it to you. He shows you the entire deck of nn cards. You see that the values of cards from the topmost to the bottommost are integers a1,a2,…,ana1,a2,…,an, and all values are different.

Then he asks you to shuffle the deck mm times. With the jj-th shuffle, you should take bjbj topmost cards and move them under the remaining (n−bj)(n−bj) cards without changing the order.

And then, using some magic, Monocarp tells you the topmost card of the deck. However, you are not really buying that magic. You tell him that you know the topmost card yourself. Can you surprise Monocarp and tell him the topmost card before he shows it?

Input

The first line contains a single integer tt (1≤t≤1041≤t≤104) — the number of testcases.

The first line of each testcase contains a single integer nn (2≤n≤2⋅1052≤n≤2⋅105) — the number of cards in the deck.

The second line contains nn pairwise distinct integers a1,a2,…,ana1,a2,…,an (1≤ai≤n1≤ai≤n) — the values of the cards.

The third line contains a single integer mm (1≤m≤2⋅1051≤m≤2⋅105) — the number of shuffles.

The fourth line contains mm integers b1,b2,…,bmb1,b2,…,bm (1≤bj≤n−11≤bj≤n−1) — the amount of cards that are moved on the jj-th shuffle.

The sum of nn over all testcases doesn't exceed 2⋅1052⋅105. The sum of mm over all testcases doesn't exceed 2⋅1052⋅105.

Output

For each testcase, print a single integer — the value of the card on the top of the deck after the deck is shuffled mm times.

Example

input

Copy

3
2
1 2
3
1 1 1
4
3 1 4 2
2
3 1
5
2 1 5 4 3
5
3 2 1 2 1

output

Copy

2
3
3

Note

In the first testcase, each shuffle effectively swaps two cards. After three swaps, the deck will be [2,1][2,1].

In the second testcase, the second shuffle cancels what the first shuffle did. First, three topmost cards went underneath the last card, then that card went back below the remaining three cards. So the deck remained unchanged from the initial one — the topmost card has value 33.

无限循环数组,这个是一维的所以,把操作数对数组长度n取模就能随时提取操作后数组的第一个数

#include<iostream>
#include<algorithm>
using namespace std;
const int manx = 2e5 + 10;
int a[manx];
int b[manx];
int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		int n, m;
		cin >> n;
		for (int i = 0; i < n; i++)
		{
			cin >> a[i];
		}
		cin >> m;
		long long sum = 0;
		for (int i = 0; i < m; i++)
		{
			cin >> b[i];
			sum += b[i];
		}
		sum = sum % n;
		cout << a[sum] << endl;
	}
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值