#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<algorithm>
#include<vector>
#include<string.h>
#include<math.h>
#include<map>
#include<iomanip>
#include<queue>
#include<set>

using namespace std;
using ll = long long;
using ull = unsigned long long;

const long double inf = 1e9 + 7;

void solve(){
	int n , k;
	cin >> n >> k;
	vector<ll> a(n);
	for(int i = 0; i < n; i++){
		cin >> a[i];
	}
	sort(a.begin() , a.end());
	ll ans = a[n - 1];
	int id = n - 2;
	while(k--){
		ans += a[id];
		id--;
	}
	cout << ans << endl;
}

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	int t;
	cin >> t;
	for(int i = 0; i < t; i++){
		solve();
	}
	return 0;
}


#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<algorithm>
#include<vector>
#include<string.h>
#include<math.h>
#include<map>
#include<iomanip>
#include<queue>
#include<set>

using namespace std;
using ll = long long;
using ull = unsigned long long;

const long double inf = 1e9 + 7;

void solve(){
	int n , k;
	cin >> n >> k;
	vector<int> a(n);
	for(int i = 0; i < n; i++){
		cin >> a[i];
	}
	sort(a.rbegin() , a.rend());
	ll ans = a[0];
	int id = 1;
	while(k--){
		ans += (ll)a[id];
		id += 1;
	}
	cout << ans << endl;
}

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	int t;
	cin >> t;
	for(int i = 0; i < t; i++){
		solve();
	}
	return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <numeric>
#include <algorithm>
#include <set>

using namespace std;

#define endl '\n'

void fast_io()
{
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout.setf(ios::fixed);
  cout.precision(20);
}

int n, k;
multiset<int> my_set;

void putInSet(int a)
{
  if (my_set.size() > k)
  {
    auto min_it = begin(my_set);
    if (*min_it < a)
    {
      my_set.erase(min_it);
      my_set.insert(a);
    }
  }
  else
  {
    my_set.insert(a);
  }
}

void solvee()
{
  cin >> n >> k;
  my_set.clear();
  int a;
  for (int i = 0; i < n; ++i)
  {
    cin >> a;
    putInSet(a);
  }
  cout << accumulate(begin(my_set), end(my_set), static_cast<uint64_t>(0)) << endl;
}

void solve()
{
  int t;
  cin >> t;
  while (t--)
  {
    solvee();
  }
}

int main()
{
  fast_io();
  solve();

  return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <numeric>
#include <algorithm>
#include <set>

using namespace std;

#define endl '\n'

void fast_io()
{
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout.setf(ios::fixed);
  cout.precision(20);
}

int n, k;
multiset<int> my_set;

void putInSet(int a)
{
  if (my_set.size() > k)
  {
    auto min = begin(my_set);
    if (*min < a)
    {
      my_set.erase(min);
      my_set.insert(a);
    }
  }
  else
  {
    my_set.insert(a);
  }
}

void solvee()
{
  cin >> n >> k;
  my_set.clear();
  int a;
  for (int i = 0; i < n; ++i)
  {
    cin >> a;
    putInSet(a);
  }
  cout << accumulate(begin(my_set), end(my_set), 0) << endl;
}

void solve()
{
  int t;
  cin >> t;
  while (t--)
  {
    solvee();
  }
}

int main()
{
  fast_io();
  solve();

  return 0;
}


var tc = parseInt(readline());
for (; tc-- ;){
    var AR = readline().split(' ').map(x => parseInt(x));
    var n = AR[0], m = AR[1];

    var ar = readline().split(' ').map(x => parseInt(x));
    ar.sort((a, b) => b - a);
    var sum = 0;
    for (var i = 0; i <= m; i++) sum += ar[i];
    print(sum);
}


#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <stdio.h>
#include <vector>
#include <cstdlib>
#include <string>
#include <map>
#include <ctime>
#include <stack>
#include <set>
#include <memory.h>
#include <deque>
#include <queue>
#include <cctype>
#include <iomanip>
#include <bitset>
#include <sstream>
#include <unordered_map>
#include <unordered_set>
#include <functional>

# define pi acos(-1.)

using namespace std;
using ld = long double;
using ull = unsigned long long;
using ll = long long;

const double EPS = 1e-6;
const ll mod = 1000000000 + 7;
const ll N = 2100010;

int main()
{
	ios::sync_with_stdio(false);
	int t;
	cin >> t;
	while (t--)
	{
		int n;
		cin >> n;
		int k;
		cin >> k;
		vector<int> v(n);
		for (int i = 0; i < n; ++i) cin >> v[i];
		sort(v.begin(), v.end());
		++k;
		ll ans = 0;

		while (k--)
		{
			ans += v[k];
		}
		cout << ans << endl;
	}

}


#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <stdio.h>
#include <vector>
#include <cstdlib>
#include <string>
#include <map>
#include <ctime>
#include <stack>
#include <set>
#include <memory.h>
#include <deque>
#include <queue>
#include <cctype>
#include <iomanip>
#include <bitset>
#include <sstream>
#include <unordered_map>
#include <unordered_set>
#include <functional>

# define pi acos(-1.)

using namespace std;
using ld = long double;
using ull = unsigned long long;
using ll = long long;

const double EPS = 1e-6;
const ll mod = 1000000000 + 7;
const ll N = 2100010;

int main()
{
	ios::sync_with_stdio(false);
	int t;
	cin >> t;
	while (t--)
	{
		int n;
		cin >> n;
		int k;
		cin >> k;
		vector<int> v(n);
		for (int i = 0; i < n; ++i) cin >> v[i];
		sort(v.begin(), v.end());
		ll ans = v.back();
		int i = n - 2;
		while (k-- && i--)
		{
			ans += v[i];
		}
		cout << ans << endl;
	}

}


#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <stdio.h>
#include <vector>
#include <cstdlib>
#include <string>
#include <map>
#include <ctime>
#include <stack>
#include <set>
#include <memory.h>
#include <deque>
#include <queue>
#include <cctype>
#include <iomanip>
#include <bitset>
#include <sstream>
#include <unordered_map>
#include <unordered_set>
#include <functional>

# define pi acos(-1.)

using namespace std;
using ld = long double;
using ull = unsigned long long;
using ll = long long;

const double EPS = 1e-6;
const ll mod = 1000000000 + 7;
const ll N = 2100010;

int main()
{
	ios::sync_with_stdio(false);
	int t;
	cin >> t;
	while (t--)
	{
		int n;
		cin >> n;
		int k;
		cin >> k;
		vector<int> v(n);
		for (int i = 0; i < n; ++i) cin >> v[i];
		sort(v.begin(), v.end());
		++k;
		ll ans = 0LL;
		int i = n - 1;
		while (k-- && i)
		{
			ans += v[i--];
		}
		cout << ans << endl;
	}

}


#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <stdio.h>
#include <vector>
#include <cstdlib>
#include <string>
#include <map>
#include <ctime>
#include <stack>
#include <set>
#include <memory.h>
#include <deque>
#include <queue>
#include <cctype>
#include <iomanip>
#include <bitset>
#include <sstream>
#include <unordered_map>
#include <unordered_set>
#include <functional>

# define pi acos(-1.)

using namespace std;
using ld = long double;
using ull = unsigned long long;
using ll = long long;

const double EPS = 1e-6;
const ll mod = 1000000000 + 7;
const ll N = 2100010;

int main()
{
	ios::sync_with_stdio(false);
	int t;
	cin >> t;
	while (t--)
	{
		int n;
		cin >> n;
		int k;
		cin >> k;
		vector<int> v(n);
		for (int i = 0; i < n; ++i) cin >> v[i];
		sort(v.begin(), v.end());
		++k;
		ll ans = 0;
		int i = n - 1;
		while (k--, i--)
		{
			ans += v[i];
		}
		cout << ans << endl;
	}

}


#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <stdio.h>
#include <vector>
#include <cstdlib>
#include <string>
#include <map>
#include <ctime>
#include <stack>
#include <set>
#include <memory.h>
#include <deque>
#include <queue>
#include <cctype>
#include <iomanip>
#include <bitset>
#include <sstream>
#include <unordered_map>
#include <unordered_set>
#include <functional>

# define pi acos(-1.)

using namespace std;
using ld = long double;
using ull = unsigned long long;
using ll = long long;

const double EPS = 1e-6;
const ll mod = 1000000000 + 7;
const ll N = 2100010;

int main()
{
	ios::sync_with_stdio(false);
	int t;
	cin >> t;
	while (t--)
	{
		int n;
		cin >> n;
		int k;
		cin >> k;
		vector<int> v(n);
		for (int i = 0; i < n; ++i) cin >> v[i];
		sort(v.begin(), v.end());
		++k;
		ll ans = 0LL;
		int i = n - 1;
		while (k--)
		{
			ans += v[i--];
		}
		cout << ans << endl;
	}

}


#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <stdio.h>
#include <vector>
#include <cstdlib>
#include <string>
#include <map>
#include <ctime>
#include <stack>
#include <set>
#include <memory.h>
#include <deque>
#include <queue>
#include <cctype>
#include <iomanip>
#include <bitset>
#include <sstream>
#include <unordered_map>
#include <unordered_set>
#include <functional>

# define pi acos(-1.)

using namespace std;
using ld = long double;
using ull = unsigned long long;
using ll = long long;

const double EPS = 1e-6;
const ll mod = 1000000000 + 7;
const ll N = 2100010;

int main()
{
	ios::sync_with_stdio(false);
	int t;
	cin >> t;
	while (t--)
	{
		int n;
		cin >> n;
		int k;
		cin >> k;
		vector<int> v(n);
		for (int i = 0; i < n; ++i) cin >> v[i];
		sort(v.begin(), v.end());
		++k;
		ll ans = 0;
		int i = n - 1;
		while (k-- && i)
		{
			ans += v[i--];
		}
		cout << ans << endl;
	}

}


#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <stdio.h>
#include <vector>
#include <cstdlib>
#include <string>
#include <map>
#include <ctime>
#include <stack>
#include <set>
#include <memory.h>
#include <deque>
#include <queue>
#include <cctype>
#include <iomanip>
#include <bitset>
#include <sstream>
#include <unordered_map>
#include <unordered_set>
#include <functional>

# define pi acos(-1.)

using namespace std;
using ld = long double;
using ull = unsigned long long;
using ll = long long;

const double EPS = 1e-6;
const ll mod = 1000000000 + 7;
const ll N = 2100010;

int main()
{
	ios::sync_with_stdio(false);
	int t;
	cin >> t;
	while (t--)
	{
		int n;
		cin >> n;
		int k;
		cin >> k;
		vector<int> v(n);
		for (int i = 0; i < n; ++i) cin >> v[i];
		sort(v.begin(), v.end());
		ll ans = v.back();
		int i = n - 2;
		while (k--)
		{
			ans += v[i];
		}
		cout << ans << endl;
	}

}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    int t;
    cin>>t;
    while(t--){
      long int n,k;
      cin>>n>>k;
      long int a[n];
      for( long int i=0; i<n;i++){

        cin>>a[i];
        }
        sort(a, a+n);
        reverse(a, a+n);
        int b=a[0];
        for(long int i=1;i<=k;i++){
                b+=a[i];
                }
        cout<<b<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    int t;
    cin>>t;
    while(t--){
      long long n,k;
      cin>>n>>k;
      long long a[n];
      for( int i=0; i<n;i++){

        cin>>a[i];
        }
        sort(a, a+n);
        reverse(a, a+n);
        long long b=a[0];
        for(int i=1;i<=k;i++){
                b+=a[i];
                }
        cout<<b<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    int t;
    cin>>t;
    while(t--){
      int n,k;
      cin>>n>>k;
      int a[n];
      for(int i=0; i<n;i++){

        cin>>a[i];
        }
        sort(a, a+n);
        reverse(a, a+n);
        int b=a[0];
        for(int i=1;i<=k;i++){
                b+=a[i];

        }
        cout<<b;
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    int t;
    cin>>t;
    while(t--){
      int n,k;
      cin>>n>>k;
      int a[n];
      for(int i=0; i<n;i++){

        cin>>a[i];
        }
        sort(a, a+n);
        reverse(a, a+n);
        int b=a[0];
        for(int i=1;i<=k;i++){
                b+=a[i];
                }
        cout<<b<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    int t;
    cin>>t;
    while(t--){
      int n,k;
      cin>>n>>k;
      int a[n];
      for(int i=0; i<n;i++){

        cin>>a[i];
        }
        sort(a, a+n);
        reverse(a, a+n);
        int b=0;
        for(int i=0;i<k;i++){
                b+=a[i];

        }
        cout<<b;
    }
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

int main(){
	unsigned long n,k,t,temp,hasil[1000];
	cin>>t;

	for(int i=1;i<=t;i++){
		cin>>n>>k;
		long Ember[n];
		for(int j=1;j<=n;j++){
			cin>>Ember[j];
		}
		sort(Ember+1,Ember+n,greater<long long>());
		for(int j=1;j<=k;j++){
			Ember[1]=Ember[1]+Ember[1+j];
		}

		hasil[i]=Ember[1];
	}
	for(int i=1;i<=t;i++){
		cout<<hasil[i]<<endl;
	}

	return 0;
}


#include<bits/stdc++.h>

using namespace std;

int main(){
	unsigned long n,k,t,temp,hasil[1000];
	cin>>t;

	for(int i=1;i<=t;i++){
		cin>>n>>k;
		long Ember[n];
		for(int j=1;j<=n;j++){
			cin>>Ember[j];
		}
		sort(Ember+1,Ember+n,greater<long long>());
		for(int j=1;j<=k;j++){
			Ember[n]=Ember[n]+Ember[n-j];
		}

		hasil[i]=Ember[n];
	}
	for(int i=1;i<=t;i++){
		cout<<hasil[i]<<endl;
	}

	return 0;
}


#include<bits/stdc++.h>

using namespace std;

int main(){
	unsigned long n,k,t,temp,hasil[10000];
	cin>>t;

	for(int i=1;i<=t;i++){
		cin>>n>>k;
		long long Ember[n],j;
		for(j=1;j<=n;j++){
			cin>>Ember[j];
		}
		sort(Ember+1,Ember+n+1);
		for(j=1;j<=k;j++){
			Ember[n]=Ember[n]+Ember[n-j];
		}

		hasil[i]=Ember[n];
	}
	for(long i=1;i<=t;i++){
		cout<<hasil[i]<<endl;
	}

	return 0;
}


#include<iostream>

using namespace std;

int main(){
	unsigned long n,k,t,temp,hasil[1000];
	cin>>t;

	for(int i=1;i<=t;i++){
		cin>>n>>k;
		long Ember[i][n];
		for(int j=1;j<=n;j++){
			cin>>Ember[i][j];
		}
		for(int j=2;j<=n;j++){
			while(Ember[i][j]<Ember[i][j-1]);{
			 temp=Ember[i][j];
			 Ember[i][j]=Ember[i][j-1];
			 Ember[i][j-1]=temp;
			}
		}
		for(int j=1;j<=k;j++){
			Ember[i][n]=Ember[i][n]+Ember[i][n-j];
		}

		hasil[i]=Ember[i][n];
	}
	for(int i=1;i<=t;i++){
		cout<<hasil[i]<<endl;
	}

	return 0;
}


#include<bits/stdc++.h>

using namespace std;

int main(){
	unsigned long n,k,t,temp,hasil[1000];
	cin>>t;

	for(int i=1;i<=t;i++){
		cin>>n>>k;
		long Ember[n];
		for(int j=1;j<=n;j++){
			cin>>Ember[j];
		}
		sort(Ember,Ember+n,greater<long long>());
		for(int j=1;j<=k;j++){
			Ember[n]=Ember[n]+Ember[n-j];
		}

		hasil[i]=Ember[n];
	}
	for(int i=1;i<=t;i++){
		cout<<hasil[i]<<endl;
	}

	return 0;
}


#include<iostream>

using namespace std;

int main(){
	unsigned long n,k,t,temp,hasil[1000];
	cin>>t;

	for(int i=1;i<=t;i++){
		cin>>n>>k;
		long Ember[n];
		for(int j=1;j<=n;j++){
			cin>>Ember[j];
		}
		for(int j=1;j<=n;j++){
			while(Ember[j]<Ember[j-1]){
				temp=Ember[j];
				Ember[j]=Ember[j-1];
				Ember[j-1]=temp;
				j--;
			}
		}
		int kurang=1;
		while(k>0){
			Ember[n]=Ember[n]+Ember[n-kurang];
			Ember[n-kurang]=0;
			k--;
			kurang++;
		}
		hasil[i]=Ember[n];
		Ember[n]=0;
	}
	for(int i=1;i<=t;i++){
		cout<<hasil[i]<<endl;
	}

	return 0;
}


#include<iostream>

using namespace std;

int main(){
	unsigned long n,k,t,temp,hasil[1000];
	cin>>t;

	for(int i=1;i<=t;i++){
		cin>>n>>k;
		long Ember[i][n];
		for(int j=1;j<=n;j++){
			cin>>Ember[i][j];
		}
		for(int j=1;j<=n;j++){
			while(Ember[i][j]<Ember[i][j-1]){
				temp=Ember[i][j];
				Ember[i][j]=Ember[i][j-1];
				Ember[i][j-1]=temp;
				j--;
			}
		}
		int kurang=1;
		while(k>0){
			Ember[i][n]=Ember[i][n]+Ember[i][n-kurang];
			k--;
			kurang++;
		}
		hasil[i]=Ember[i][n];
	}
	for(int i=1;i<=t;i++){

		cout<<hasil[i]<<endl;
	}

	return 0;
}


#include<iostream>

using namespace std;

int main(){
	unsigned long n,k,t,temp,hasil[1000];
	cin>>t;

	for(int i=1;i<=t;i++){
		cin>>n>>k;
		long Ember[i][n];
		for(int j=1;j<=n;j++){
			cin>>Ember[i][j];
		}
		for(int j=2;j<=n;j++){
			while(Ember[i][j]<Ember[i][j-1]);{
			 temp=Ember[i][j];
			 Ember[i][j]=Ember[i][j-1];
			 Ember[i][j-1]=temp;
			}
		}
		int min=1;
		while(k>0){
			Ember[i][n]=Ember[i][n]+Ember[i][n-min];
			k--;
			min++;
		}
		hasil[i]=Ember[i][n];
	}
	for(int i=1;i<=t;i++){
		cout<<hasil[i]<<endl;
	}

	return 0;
}


#include<bits/stdc++.h>

using namespace std;

int main(){
	unsigned long long n,k,t,temp,hasil[10000];
	cin>>t;

	for(int i=1;i<=t;i++){
		cin>>n>>k;
		long long Ember[n],j;
		for(j=1;j<=n;j++){
			cin>>Ember[j];
		}
		sort(Ember+1,Ember+n+1);
		for(j=1;j<=k;j++){
			Ember[n]=Ember[n]+Ember[n-j];
		}

		hasil[i]=Ember[n];
	}
	for(long i=1;i<=t;i++){
		cout<<hasil[i]<<endl;
	}

	return 0;
}


#include<bits/stdc++.h>

using namespace std;

int main(){
	unsigned long n,k,t,temp,hasil[10000];
	cin>>t;

	for(int i=1;i<=t;i++){
		cin>>n>>k;
		long Ember[n];
		for(int j=1;j<=n;j++){
			cin>>Ember[j];
		}
		sort(Ember+1,Ember+n+1);
		for(int j=1;j<=k;j++){
			Ember[n]=Ember[n]+Ember[n-j];
		}

		hasil[i]=Ember[n];
	}
	for(int i=1;i<=t;i++){
		cout<<hasil[i]<<endl;
	}

	return 0;
}


#include<iostream>

using namespace std;

int main(){
	unsigned long n,k,t,temp,hasil[1000];
	cin>>t;

	for(int i=1;i<=t;i++){
		cin>>n>>k;
		long Ember[n];
		for(int j=1;j<=n;j++){
			cin>>Ember[j];
		}
		for(int j=1;j<=n;j++){
			while(Ember[j]<Ember[j-1]){
				temp=Ember[j];
				Ember[j]=Ember[j-1];
				Ember[j-1]=temp;
				j--;
			}
		}
		int kurang=1;
		while(k>0){
			Ember[n]=Ember[n]+Ember[n-kurang];
			Ember[n-kurang]=0;
			k--;
			kurang++;
		}
		hasil[i]=Ember[n];
		for(int j=1;j<=n;j++){
			Ember[j]=0;
		}
	}
	for(int i=1;i<=t;i++){
		cout<<hasil[i]<<endl;
	}

	return 0;
}


#include<bits/stdc++.h>

using namespace std;

int main(){
	unsigned long n,k,t,temp,hasil[1000];
	cin>>t;

	for(int i=1;i<=t;i++){
		cin>>n>>k;
		long long Ember[n];
		for(int j=1;j<=n;j++){
			cin>>Ember[j];
		}
		sort(Ember,Ember+n,greater<long long>());
		for(int j=1;j<=k;j++){
			Ember[n]=Ember[n]+Ember[n-j];
		}

		hasil[i]=Ember[n];
	}
	for(int i=1;i<=t;i++){
		cout<<hasil[i]<<endl;
	}

	return 0;
}


#include <iostream>
#include <iterator>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

#define PB push_back;

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int T,N,K;
    cin >> T;
    while(T--)
    {
        cin>>N>>K;
        K++;
        vector<int>a;
        a.reserve(N);
        copy_n(istream_iterator<int>(cin), N, back_inserter(a));
        sort(a.rbegin(),a.rend());
        long sum = 0;
        for(int i = 0; i < K; i++)
        {
            sum += a[i];
        }
        cout << sum<<"\n";
    }

    return 0;
}


#include <iostream>
#include <iterator>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

#define PB push_back;

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int T,N,K;
    cin >> T;
    while(T--)
    {
        cin>>N>>K;
        K++;
        vector<int>a;
        a.reserve(N);
        copy_n(istream_iterator<int>(cin), N, back_inserter(a));
        sort(a.rbegin(),a.rend());
        int sum = 0;
        for(int i = 0; i < K+1;i++)
        {
            sum += a[i];
            a[i] = 0;
        }
        cout << sum-a[N-1];
    }

    return 0;
}


#include <iostream>
#include <iterator>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

#define PB push_back;

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int T,N,K;
    cin >> T;
    while(T--)
    {
        cin>>N>>K;
        K++;
        vector<int>a;
        a.reserve(N);
        copy_n(istream_iterator<int>(cin), N, back_inserter(a));
        sort(a.rbegin(),a.rend());
        int sum = 0;
        for(int i = 0; i <= K; i++)
        {
            sum += a[i];
        }
        cout << sum;
    }

    return 0;
}


#include <iostream>
#include <iterator>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

#define PB push_back;

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int T,N,K;
    cin >> T;
    while(T--)
    {
        cin>>N>>K;
        K++;
        vector<int>a;
        a.reserve(N);
        copy_n(istream_iterator<int>(cin), N, back_inserter(a));
        sort(a.rbegin(),a.rend());
        long sum = 0;
        for(int i = 0; i <= K; i++)
        {
            sum += a[i];
        }
        cout << sum;
    }

    return 0;
}


#include <iostream>
#include <iterator>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

#define PB push_back;

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int T,N,K;
    cin >> T;
    while(T--)
    {
        cin>>N>>K;
        K++;
        vector<int>a;
        a.reserve(N);
        copy_n(istream_iterator<int>(cin), N, back_inserter(a));
        sort(a.rbegin(),a.rend());
        long sum = 0;
        for(int i = 0; i <= K; i++)
        {
            sum += a[i];
        }
        cout << sum<<"\n";
    }

    return 0;
}


#include <iostream>
#include <iterator>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

#define PB push_back;

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int T,N,K;
    cin >> T;
    while(T--)
    {
        cin>>N>>K;
        K++;
        vector<int>a;
        a.reserve(N);
        copy_n(istream_iterator<int>(cin), N, back_inserter(a));
        sort(a.rbegin(),a.rend());
        long long sum = 0;
        for(int i = 0; i < K; i++)
        {
            sum += a[i];
        }
        cout << sum <<"\n";
    }

    return 0;
}


#include <iostream>
#include <iterator>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

#define PB push_back;

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int T,N,K;
    cin >> T;
    while(T--)
    {
        cin>>N>>K;
        K++;
        vector<int>a;
        a.reserve(N);
        copy_n(istream_iterator<int>(cin), N, back_inserter(a));
        sort(a.rbegin(),a.rend());
        int sum = 0;
        for(int i = 0; i < K; i++)
        {
            sum += a[i];
        }
        cout << sum<<"\n";
    }

    return 0;
}


#include <iostream>
#include <iterator>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

#define PB push_back;

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int T,N,K;
    cin >> T;
    while(T--)
    {
        cin>>N>>K;
        K++;
        vector<int>a;
        a.reserve(N);
        copy_n(istream_iterator<int>(cin), N, back_inserter(a));
        sort(a.rbegin(),a.rend());
        int sum = 0;
        for(int i = 0; i < K; i++)
        {
            sum += a[i];
        }
        cout << sum;
    }

    return 0;
}


 /*

                            بِسْمِ ٱللَّٰهِ ٱلرَّحْمَٰنِ ٱلرَّحِيمِ
                          لآ اِلَهَ اِلّا اللّهُ مُحَمَّدٌ رَسُوُل اللّهِ
*/

/*
    Auther:   sammo_(None of them)
    from  :   https://n0oo0o0ob.sleek.page/

*/

#include <bits/stdc++.h>

using namespace std;
#define endl "\n"
#define YES cout << "YES" << endl
#define NO cout << "NO" << endl
#define yes cout << "yes" << endl
#define no cout << "no" << endl
#define Yes cout << "Yes" << endl
#define No cout << "No" << endl
#define scan scanf
#define print printf
#define pb push_back
#define test()     int t; cin >> t; while(t--)
#define point setprecision
#define all(x) begin(x), end(x)
#define rll(x) rbegin(x), rend(x)
#define noob n0oO0o0Ob
#define pf push_front
#define mx(v) *max_element(v.begin(),v.end())
#define mn(v) *min_element(v.begin(),v.end())
#define sum(v) accumulate(v.begin(),v.end(),0)
#define uni(v) v.erase(unique(v.begin(),v.end()),v.end());
#define gcd(a,b)        __gcd(a,b);
#define lcm(a,b)        (a/(gcd(a,b))*b);

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef double db;

const ld PI = acos((ld)-1);
const int MOD = 1000000007;
int z = 0;
int zz = 0;
int main()
{
    ios::sync_with_stdio(false);cin.tie(0);
    test(){
        int a, b;
        cin >> a >> b;
        int ara[a];
        for(int i = 0; i < a;i++){
            cin >> ara[i];
        }
        sort(ara, ara+a);
        int k = ara[a-1];
        for(int i = a-2; i > b; i--){
            //cout << ara[i] << ' ';
            k += ara[i];

        }
        cout << z+k << endl;
    }
    return 0;
}


 /*

                            بِسْمِ ٱللَّٰهِ ٱلرَّحْمَٰنِ ٱلرَّحِيمِ
                          لآ اِلَهَ اِلّا اللّهُ مُحَمَّدٌ رَسُوُل اللّهِ
*/

/*
    Auther:   sammo_(None of them)
    from  :   https://n0oo0o0ob.sleek.page/

*/

#include <bits/stdc++.h>

using namespace std;
#define endl "\n"
#define YES cout << "YES" << endl
#define NO cout << "NO" << endl
#define yes cout << "yes" << endl
#define no cout << "no" << endl
#define Yes cout << "Yes" << endl
#define No cout << "No" << endl
#define scan scanf
#define print printf
#define pb push_back
#define test()     int t; cin >> t; while(t--)
#define point setprecision
#define all(x) begin(x), end(x)
#define rll(x) rbegin(x), rend(x)
#define noob n0oO0o0Ob
#define pf push_front
#define mx(v) *max_element(v.begin(),v.end())
#define mn(v) *min_element(v.begin(),v.end())
#define sum(v) accumulate(v.begin(),v.end(),0)
#define uni(v) v.erase(unique(v.begin(),v.end()),v.end());
#define gcd(a,b)        __gcd(a,b);
#define lcm(a,b)        (a/(gcd(a,b))*b);

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef double db;

const ld PI = acos((ld)-1);
const int MOD = 1000000007;
int z = 0;
int zz = 0;
int main()
{
    ios::sync_with_stdio(false);cin.tie(0);
    test(){
        ll a, b;
        cin >> a >> b;
        ll ara[a];
        for(ll i = 0; i < a;i++){
            cin >> ara[i];
        }
        sort(ara, ara+a);
        ll k = ara[a-1];
        for(ll i = a-2; i > b; i--){
            //cout << ara[i] << ' ';
            k += ara[i];

        }
        cout << k << endl;
    }
    return 0;
}


 /*

                            بِسْمِ ٱللَّٰهِ ٱلرَّحْمَٰنِ ٱلرَّحِيمِ
                          لآ اِلَهَ اِلّا اللّهُ مُحَمَّدٌ رَسُوُل اللّهِ
*/

/*
    Auther:   sammo_(None of them)
    from  :   https://n0oo0o0ob.sleek.page/

*/

#include <bits/stdc++.h>

using namespace std;
#define endl "\n"
#define YES cout << "YES" << endl
#define NO cout << "NO" << endl
#define yes cout << "yes" << endl
#define no cout << "no" << endl
#define Yes cout << "Yes" << endl
#define No cout << "No" << endl
#define scan scanf
#define print printf
#define pb push_back
#define test()     int t; cin >> t; while(t--)
#define point setprecision
#define all(x) begin(x), end(x)
#define rll(x) rbegin(x), rend(x)
#define noob n0oO0o0Ob
#define pf push_front
#define mx(v) *max_element(v.begin(),v.end())
#define mn(v) *min_element(v.begin(),v.end())
#define sum(v) accumulate(v.begin(),v.end(),0)
#define uni(v) v.erase(unique(v.begin(),v.end()),v.end());
#define gcd(a,b)        __gcd(a,b);
#define lcm(a,b)        (a/(gcd(a,b))*b);

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef double db;

const ld PI = acos((ld)-1);
const int MOD = 1000000007;
int z = 0;
int zz = 0;
int main()
{
    ios::sync_with_stdio(false);cin.tie(0);
    test(){
        int a, b;
        cin >> a >> b;
        vector<int> v;
        for(int i = 0; i< a; i++){
            int x;
            cin>>x;
            v.pb(x);
        }
        int k = 0;
        sort(all(v));
        reverse(all(v));
        for(int i = 1; i<= b; i++){
            k+= v[i];
        }
        cout << v[0]+k << endl;
    }
    return 0;
}


 /*

                            بِسْمِ ٱللَّٰهِ ٱلرَّحْمَٰنِ ٱلرَّحِيمِ
                          لآ اِلَهَ اِلّا اللّهُ مُحَمَّدٌ رَسُوُل اللّهِ
*/

/*
    Auther:   sammo_(None of them)
    from  :   https://n0oo0o0ob.sleek.page/

*/

#include <bits/stdc++.h>

using namespace std;
#define endl "\n"
#define YES cout << "YES" << endl
#define NO cout << "NO" << endl
#define yes cout << "yes" << endl
#define no cout << "no" << endl
#define Yes cout << "Yes" << endl
#define No cout << "No" << endl
#define scan scanf
#define print printf
#define pb push_back
#define test()     int t; cin >> t; while(t--)
#define point setprecision
#define all(x) begin(x), end(x)
#define rll(x) rbegin(x), rend(x)
#define noob n0oO0o0Ob
#define pf push_front
#define mx(v) *max_element(v.begin(),v.end())
#define mn(v) *min_element(v.begin(),v.end())
#define sum(v) accumulate(v.begin(),v.end(),0)
#define uni(v) v.erase(unique(v.begin(),v.end()),v.end());
#define gcd(a,b)        __gcd(a,b);
#define lcm(a,b)        (a/(gcd(a,b))*b);

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef double db;

const ld PI = acos((ld)-1);
const int MOD = 1000000007;
int z = 0;
int zz = 0;
int main()
{
    ios::sync_with_stdio(false);cin.tie(0);
    test(){
        int a, b;
        cin >> a >> b;
        vector<int> v;
        for(int i = 0; i< a; i++){
            int x;
            cin>>x;
            v.pb(x);
        }

        ll k = 0;
        sort(all(v));
        reverse(all(v));
        for(int i = 1; i<= b; i++){
            k+= v[i];
        }
        cout << v[0]+k << endl;
    }
    return 0;
}


 /*

                            بِسْمِ ٱللَّٰهِ ٱلرَّحْمَٰنِ ٱلرَّحِيمِ
                          لآ اِلَهَ اِلّا اللّهُ مُحَمَّدٌ رَسُوُل اللّهِ
*/

/*
    Auther:   sammo_(None of them)
    from  :   https://n0oo0o0ob.sleek.page/

*/

#include <bits/stdc++.h>

using namespace std;
#define endl "\n"
#define YES cout << "YES" << endl
#define NO cout << "NO" << endl
#define yes cout << "yes" << endl
#define no cout << "no" << endl
#define Yes cout << "Yes" << endl
#define No cout << "No" << endl
#define scan scanf
#define print printf
#define pb push_back
#define test()     int t; cin >> t; while(t--)
#define point setprecision
#define all(x) begin(x), end(x)
#define rll(x) rbegin(x), rend(x)
#define noob n0oO0o0Ob
#define pf push_front
#define mx(v) *max_element(v.begin(),v.end())
#define mn(v) *min_element(v.begin(),v.end())
#define sum(v) accumulate(v.begin(),v.end(),0)
#define uni(v) v.erase(unique(v.begin(),v.end()),v.end());
#define gcd(a,b)        __gcd(a,b);
#define lcm(a,b)        (a/(gcd(a,b))*b);

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef double db;

const ld PI = acos((ld)-1);
const int MOD = 1000000007;
int z = 0;
int zz = 0;
int main()
{
    ios::sync_with_stdio(false);cin.tie(0);
    string x;
    cin >> x;
    int a = x.size();

    for(int i = 0; i< a-2;i++){
        if(x[i]=='W'&&x[i+1]=='U'&&x[i+2]=='B'){
            x.erase(i,i+0);
            x.erase(i,i+1);
            x[i] = '#';
    }
    }
    cout << x << endl;
    return 0;
}


 /*

                            بِسْمِ ٱللَّٰهِ ٱلرَّحْمَٰنِ ٱلرَّحِيمِ
                          لآ اِلَهَ اِلّا اللّهُ مُحَمَّدٌ رَسُوُل اللّهِ
*/

/*
    Auther:   sammo_(None of them)
    from  :   https://n0oo0o0ob.sleek.page/

*/

#include <bits/stdc++.h>

using namespace std;
#define endl "\n"
#define YES cout << "YES" << endl
#define NO cout << "NO" << endl
#define yes cout << "yes" << endl
#define no cout << "no" << endl
#define Yes cout << "Yes" << endl
#define No cout << "No" << endl
#define scan scanf
#define print printf
#define pb push_back
#define test()     int t; cin >> t; while(t--)
#define point setprecision
#define all(x) begin(x), end(x)
#define rll(x) rbegin(x), rend(x)
#define noob n0oO0o0Ob
#define pf push_front
#define mx(v) *max_element(v.begin(),v.end())
#define mn(v) *min_element(v.begin(),v.end())
#define sum(v) accumulate(v.begin(),v.end(),0)
#define uni(v) v.erase(unique(v.begin(),v.end()),v.end());
#define gcd(a,b)        __gcd(a,b);
#define lcm(a,b)        (a/(gcd(a,b))*b);

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef double db;

const ld PI = acos((ld)-1);
const int MOD = 1000000007;
int z = 0;
int zz = 0;
int main()
{
    ios::sync_with_stdio(false);cin.tie(0);
    test(){
        int a, b;
        cin >> a >> b;
        int ara[a];
        for(int i = 0; i < a;i++){
            cin >> ara[i];
        }
        sort(ara, ara+a);
        ull k = ara[a-1];
        for(int i = a-2; i > b; i--){
            //cout << ara[i] << ' ';
            k += ara[i];

        }
        cout << z+k << endl;
    }
    return 0;
}


using namespace std;
#include<bits/stdc++.h>
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int n,k,sum=0;
		cin>>n>>k;
		int a[n],z=n-1;
		for(int i=0;i<n;i++)
		cin>>a[i];
		sort(a,a+n);
		sum=a[z];
		if(n==1)
		cout<<a[0]<<"\n";
		else
		{
		while(k>=1)
		{   z--;
			sum+=a[z];

			k--;

		}
		cout<<sum<<"\n";
	}
	}
}


using namespace std;
#include<bits/stdc++.h>
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int n,k,sum=0;
		cin>>n>>k;
		int a[n],z=n-1;
		for(int i=0;i<n;i++)
		cin>>a[i];
		sort(a,a+n);
		sum=a[z];
		while(k>=1)
		{   --z;
			sum+=a[z];

			k--;

		}
		cout<<sum;
	}
}


using namespace std;
#include<bits/stdc++.h>
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int n,k,sum=0;
		cin>>n>>k;
		int a[n],z=n-1;
		for(int i=0;i<n;i++)
		cin>>a[i];
		sort(a,a+n);
		sum=a[z];
		while(k>=1)
		{   --z;
			sum+=a[z];

			k--;

		}
		cout<<sum<<"\n";
	}
}


using namespace std;
#include<bits/stdc++.h>
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int n,k,sum=0;
		cin>>n>>k;
		int a[n],z=n-1;
		for(int i=0;i<n;i++)
		cin>>a[i];
		sort(a,a+n);
		sum=a[z];
		if(n==1)
		cout<<[0];
		while(k>=1)
		{   z--;
			sum+=a[z];

			k--;

		}
		cout<<sum<<"\n";
	}
}


using namespace std;
#include<bits/stdc++.h>
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int n,k,sum=0;
		cin>>n>>k;
		int a[n],z=n-1;
		for(int i=0;i<n;i++)
		cin>>a[i];
		sort(a,a+n);
		sum=a[z];
		if(n==1)
		cout<<0<<"\n";
		else
		{
		while(k>=1)
		{   z--;
			sum+=a[z];

			k--;

		}
		cout<<sum<<"\n";
	}
	}
}


using namespace std;
#include<bits/stdc++.h>
int main()
{
	int t;
	cin>>t;
	while(t--)
	{   int n,k;
		long long sum;
		cin>>n>>k;
		long long a[n];
		int z=n-1;
		for(int i=0;i<n;i++)
		cin>>a[i];
		sort(a,a+n);
		sum=a[z];
		if(n==1&&k==1)
		cout<<0<<"\n";
		else
		{
		while(k>=1)
		{   z--;
			sum+=a[z];

			k--;

		}
		cout<<sum<<"\n";
	}
	}
}


using namespace std;
#include<bits/stdc++.h>
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int n,k,sum=0;
		cin>>n>>k;
		int a[n],z=n-1;
		for(int i=0;i<n;i++)
		cin>>a[i];
		sort(a,a+n);
		sum=a[z];
		if(n==1)
		cout<<a[0];
		while(k>=1)
		{   z--;
			sum+=a[z];

			k--;

		}
		cout<<sum<<"\n";
	}
}


using namespace std;
#include<bits/stdc++.h>
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int n,k,sum=0;
		cin>>n>>k;
		int a[n],z=n-1;
		for(int i=0;i<n;i++)
		cin>>a[i];
		sort(a,a+n);
		sum=a[z];
		if(n==1)
		cout<<a[0];
		else
		{
		while(k>=1)
		{   z--;
			sum+=a[z];

			k--;

		}
		cout<<sum<<"\n";
	}
	}
}


using namespace std;
#include<bits/stdc++.h>
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int n,k,sum=0;
		cin>>n>>k;
		int a[n],z=n-1;
		for(int i=0;i<n;i++)
		cin>>a[i];
		sort(a,a+n);
		sum=a[z];
		while(k>=1)
		{   z--;
			sum+=a[z];

			k--;

		}
		cout<<sum<<"\n";
	}
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
  int t;cin>>t;stop:
  while(t--)
  {
    int n,k;unsigned long long c=0,d=0;cin>>n>>k;int a[n];
    for(int i=0;i<n;i++)
    {
      cin>>a[i];c+=a[i];
    }sort(a,a+n);
    if(k>=n){cout<<c<<endl;goto stop;}
    for(int i=n-1;i>=n-k-1;i--)
    {
      d+=a[i];
    }cout<<d<<endl;

  }
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
  unsigned long long t;cin>>t;
  while(t--)
  {
    unsigned long long n,k,c=0,d=0;cin>>n>>k;unsigned long long a[n];
    for(unsigned long long i=0;i<n;i++)
    {
      cin>>a[i];c+=a[i];
    }sort(a,a+n);
    if(k>=n){cout<<c<<endl;break;}
    for(unsigned long long i=n-1;i>=n-k-1;i--)
    {
      d+=a[i];
    }cout<<d<<endl;

  }
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
  int t;cin>>t;stop:
  while(t--)
  {
    int n,k,d=0;unsigned long long c=0;cin>>n>>k;int a[n];
    for(int i=0;i<n;i++)
    {
      cin>>a[i];c+=a[i];
    }sort(a,a+n);
    if(k>=n){cout<<c<<endl;goto stop;}
    for(int i=n-1;i>=n-k-1;i--)
    {
      d+=a[i];
    }cout<<d<<endl;

  }
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
  unsigned long long t;cin>>t;stop:
  while(t--)
  {
    unsigned long long n,k,c=0,d=0;cin>>n>>k;unsigned long long a[n];
    for(unsigned long long i=0;i<n;i++)
    {
      cin>>a[i];c+=a[i];
    }sort(a,a+n);
    if(k>=n){cout<<c<<endl;goto stop;}
    for(unsigned long long i=n-1;i>=n-k-1;i--)
    {
      d+=a[i];
    }cout<<d<<endl;

  }
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
  int t;cin>>t;stop:
  while(t--)
  {
    int n,k,c=0,d=0;cin>>n>>k;int a[n];
    for(int i=0;i<n;i++)
    {
      cin>>a[i];c+=a[i];
    }sort(a,a+n);
    if(k>=n){cout<<c<<endl;goto stop;}
    for(int i=n-1;i>=n-k-1;i--)
    {
      d+=a[i];
    }cout<<d<<endl;

  }
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
  int t;cin>>t;stop:
  while(t--)
  {
    unsigned long long n,k,c=0,d=0;cin>>n>>k;int a[n];
    for(int i=0;i<n;i++)
    {
      cin>>a[i];c+=a[i];
    }sort(a,a+n);
    if(k>=n){cout<<c<<endl;goto stop;}
    for(int i=n-1;i>=n-k-1;i--)
    {
      d+=a[i];
    }cout<<d<<endl;

  }
}


#include<bits/stdc++.h>

using namespace std;

int main()
{
    long long  t;
    cin >> t;
    for(int i = 1; i <= t; ++i)
    {
        long long n, k, s = 0;
        cin >> n >> k;
        long long a[n + 1];
        for(int j = 1; j <= n; ++j)
            cin >> a[j];
        sort(a + 1, a + n + 1);
        for(int j = n; j >= k + 2; --j)
            s += a[j];
        cout << s << "\n";
    }
}


#include<bits/stdc++.h>

using namespace std;

int main()
{
    int t;
    cin >> t;
    for(int i = 1; i <= t; ++i)
    {
        int n, k, s = 0;
        cin >> n >> k;
        int a[n + 1];
        for(int i = 1; i <= n; ++i)
            cin >> a[i];
        sort(a + 1, a + n + 1);
        for(int i = 1; i <= k + 1; ++i)
            s += a[i];
        cout << s << "\n";
    }
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

int main()
{
    long long  t;
    cin >> t;
    for(int i = 1; i <= t; ++i)
    {
        long long n, k, s = 0;
        cin >> n >> k;
        long long a[n + 1];
        for(int j = 1; j <= n; ++j)
            cin >> a[j];
        sort(a + 1, a + n + 1);
        for(int j = n; j >= k + 1; --j)
            s += a[j];
        cout << s << "\n";
    }
}


#include<bits/stdc++.h>

using namespace std;

int main()
{
    long long  t;
    cin >> t;
    for(int j = 1; j <= t; ++j)
    {
        long long n, k, s = 0, kol = 0;
        cin >> n >> k;
        long long a[n + 1];
        for (int i = 1 ; i <= n; i++)
        cin >> a[i];
        sort(a + 1,a + n + 1);
        s=a[n];
        while(k){
        s += a[n - k];
        k--;
        }
        cout << s << "\n";
    }
}


#include<bits/stdc++.h>

using namespace std;

int main()
{
    long long  t;
    cin >> t;
    for(int i = 1; i <= t; ++i)
    {
        long long n, k, s = 0;
        cin >> n >> k;
        long long a[n + 1];
        for(int j = 1; j <= n; ++j)
            cin >> a[j];
        sort(a + 1, a + n + 1);
        for(int j = k + 1; j >= 1; --j)
            s += a[j];
        cout << s << "\n";
    }
}


#include<bits/stdc++.h>

using namespace std;

int main()
{
    long long  t;
    cin >> t;
    for(int i = 1; i <= t; ++i)
    {
        long long n, k, s = 0, kol = 0;
        cin >> n >> k;
        long long a[n + 1];
        for(int j = 1; j <= n; ++j)
            cin >> a[j];
        sort(a + 1, a + n + 1);
        for(int j = n; j >= n - k; --j)
        {
            s += a[i];
        }
        cout << s << "\n";
    }
}


#include<bits/stdc++.h>

using namespace std;

int main()
{
    long long  t;
    cin >> t;
    for(int i = 1; i <= t; ++i)
    {
        long long n, k, s = 0;
        cin >> n >> k;
        long long a[n + 1];
        for(int i = 1; i <= n; ++i)
            cin >> a[i];
        sort(a + 1, a + n + 1);
        for(int i = 1; i <= k + 1; ++i)
            s += a[i];
        cout << s << "\n";
    }
}


#include<bits/stdc++.h>

using namespace std;

int main()
{
    long long  t;
    cin >> t;
    for(int i = 1; i <= t; ++i)
    {
        long long n, k, s = 0;
        cin >> n >> k;
        long long a[n + 1];
        for(int i = 1; i <= n; ++i)
            cin >> a[i];
        sort(a + 1, a + n + 1);
        for(int i = k + 1; i >= 1; --i)
            s += a[i];
        cout << s << "\n";
    }
}


#include<bits/stdc++.h>

using namespace std;

int main()
{
    long long  t;
    cin >> t;
    for(int i = 1; i <= t; ++i)
    {
        long long n, k, s = 0, kol = 0;
        cin >> n >> k;
        long long a[n + 1];
        for(int j = 1; j <= n; ++j)
            cin >> a[j];
        sort(a + 1, a + n + 1);
        for(int j = n; j >= 1; --j)
        {
            ++kol;
            if(kol == k + 2)
                break;
            s += a[i];
        }
        cout << s << "\n";
    }
}


#include<bits/stdc++.h>

#define sz(x) (int)(x).size()
#define ins insert
#define pb push_back

typedef long long ll;
typedef long double ld;

using namespace std;

const int N = 1e6+1;

int main() {
    /*
    ios_base::sync_with_stdio(NULL);
    cin.tie(nullptr);
    cout.tie(nullptr);
    */
    long long t;
    cin >> t;
    while(t--) {
        long long n;
        cin >> n;
        long long k;
        cin >> k;
        long long a[n+1];
        for(int i = 1; i <= n; i++) {
            cin >> a[i];
        }
        long long sum = 0, ans =0;
        vector<long long>v;
        sort(a+1,a+n+1);
        for(int i = n-k; i <= n-1; i++) {
            a[n]+=a[i];
        }
        cout<<a[n]<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>

#define sz(x) (int)(x).size()
#define ins insert
#define pb push_back

typedef long long ll;
typedef long double ld;

using namespace std;

const int N = 1e6+1;

void solve() {
    long long n,k;
    cin >> n >> k;
    long long a[n+1];
    for(int i = 1; i <= n; i++) {
        cin >> a[i];
    }
    long long sum = 0;
    sort(a+1,a+n+1);
    for(int i = n-k; i <= n; i++) {
        sum+=a[i];
    }
    cout<<sum;
    cout<<endl;
}

int main() {
    /*
    ios_base::sync_with_stdio(NULL);
    cin.tie(nullptr);
    cout.tie(nullptr);-
    */
    long long t;
    cin >> t;
    while(t--) {
        solve();
    }
    return 0;
}


#include<bits/stdc++.h>

#define sz(x) (int)(x).size()
#define ins insert
#define pb push_back

typedef long long ll;
typedef long double ld;

using namespace std;

const int N = 1e6+1;

int main() {
    /*
    ios_base::sync_with_stdio(NULL);
    cin.tie(nullptr);
    cout.tie(nullptr);
    */
    long long t;
    cin >> t;
    while(t--) {
        long long n;
        cin >> n;
        long long k;
        cin >> k;
        long long a[n+1];
        for(int i = 1; i <= n; i++) {
            cin >> a[i];
        }
        long long sum = 0, ans =0;
        vector<long long>v;
        sort(a+1,a+n+1);
        for(int i = k+1; i < n; i++) {
            ans+=a[i];
        }
        cout<<ans<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>

#define sz(x) (int)(x).size()
#define ins insert
#define pb push_back

typedef long long ll;
typedef long double ld;

using namespace std;

const int N = 1e6+1;

int main() {
    /*
    ios_base::sync_with_stdio(NULL);
    cin.tie(nullptr);
    cout.tie(nullptr);
    */
    long long t;
    cin >> t;
    while(t--) {
        long long n;
        cin >> n;
        long long k;
        cin >> k;
        long long a[n+1];
        for(int i = 1; i <= n; i++) {
            cin >> a[i];
        }
        long long sum = 0, ans =0;
        vector<long long>v;
        sort(a+1,a+n+1);
        for(int i = n-k; i <= n; i++) {
            a[i]+=a[n];
        }
        cout<<a[n]<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>

#define sz(x) (int)(x).size()
#define ins insert
#define pb push_back

typedef long long ll;
typedef long double ld;

using namespace std;

const int N = 1e6+1;

int main() {
    /*
    ios_base::sync_with_stdio(NULL);
    cin.tie(nullptr);
    cout.tie(nullptr);
    */
    long long t;
    cin >> t;
    while(t--) {
        long long n;
        cin >> n;
        long long k;
        cin >> k;
        long long a[n+1];
        for(int i = 1; i <= n; i++) {
            cin >> a[i];
        }
        sort(a+1,a+n+1);
        cout<<a[n]+a[n-1]<<endl;

    }
    return 0;
}


#include<bits/stdc++.h>

#define sz(x) (int)(x).size()
#define ins insert
#define pb push_back

typedef long long ll;
typedef long double ld;

using namespace std;

const int N = 1e6+1;

void solve() {
    long long n,k;
    cin >> n >> k;
    long long a[n+1];
    for(int i = 1; i <= n; i++) {
        cin >> a[i];
    }
    long long sum = 0;
    sort(a+1,a+n+1);
    for(int i = n-k; i <= n; i++) {
        sum+=a[i];
    }
    cout<<sum<<endl;
}

int main() {
    /*
    ios_base::sync_with_stdio(NULL);
    cin.tie(nullptr);
    cout.tie(nullptr);-
    */
    long long t;
    cin >> t;
    while(t--) {
        solve();
    }
    return 0;
}


#include<bits/stdc++.h>
  struct cod{
    int x=0;
    int y=0;
  }codi;
  #define ll long long
  #define vi vector<long long int>
  #define pb push_back
  #define mp make_pair
  #define pi pair<int,int>
  #define rep(i,a,b) for(int i=a;i<b;i++)
  #define fio  ios::sync_with_stdio(0); cin.tie(0);
  #define mod 1000000007
  #define sqr(a) (a)*(a)
  #define re return
  #define min3(a,b,c) min((a),min((b),(c)))
  using namespace std;
void solving(){
    ll int n,k;
    cin>>n>>k;

    vector<ll int>arr(n);
    ll int i;
    for(i=0;i<n;i++)
    {
        cin>>arr[i];
    }

    sort(arr.begin(),arr.end());
    ll int cnt=0;
    for(i=0;i<n-1;i++)
    {
        if(arr[i]!=0)
        {
            arr[n-1]+=arr[i];
           // arr[i]=0;
            cnt++;
            if(cnt==k)
            {
                break;
            }
        }
    }

    cout<<arr[n-1];
    cout<<"\n";

}
int main(){
  fio;
  ll int t;
  cin>>t;
  while(t--)
  {
    solving();
  }
  re 0;
  }


#include<bits/stdc++.h>
  struct cod{
    int x=0;
    int y=0;
  }codi;
  #define ll long long
  #define vi vector<long long int>
  #define pb push_back
  #define mp make_pair
  #define pi pair<int,int>
  #define rep(i,a,b) for(int i=a;i<b;i++)
  #define fio  ios::sync_with_stdio(0); cin.tie(0);
  #define mod 1000000007
  #define sqr(a) (a)*(a)
  #define re return
  #define min3(a,b,c) min((a),min((b),(c)))
  using namespace std;
void solving(){
    ll int n,k;
    cin>>n>>k;

    vector<ll int>arr(n);
    ll int i;
    for(i=0;i<n;i++)
    {
        cin>>arr[i];
    }

    sort(arr.begin(),arr.end());
    ll int cnt=0,sum=0;
    for(i=0;i<n-1;i++)
    {
        if(arr[i]!=0)
        {
            sum += arr[i];
           // arr[i]=0;
            cnt++;
            if(cnt==k)
            {
                break;
            }
        }
    }

    cout<<sum+arr[n-1];
    cout<<"\n";

}
int main(){
  fio;
  ll int t;
  cin>>t;
  while(t--)
  {
    solving();
  }
  re 0;
  }


#include<bits/stdc++.h>
  struct cod{
    int x=0;
    int y=0;
  }codi;
  #define ll long long
  #define vi vector<long long int>
  #define pb push_back
  #define mp make_pair
  #define pi pair<int,int>
  #define rep(i,a,b) for(int i=a;i<b;i++)
  #define fio  ios::sync_with_stdio(0); cin.tie(0);
  #define mod 1000000007
  #define sqr(a) (a)*(a)
  #define re return
  #define min3(a,b,c) min((a),min((b),(c)))
  using namespace std;
void solving(){
    ll int n,k;
    cin>>n>>k;

    vector<int>arr(n);
    ll int i;
    for(i=0;i<n;i++)
    {
        cin>>arr[i];
    }

    sort(arr.begin(),arr.end());
    int cnt=0;
    for(i=0;i<n-1;i++)
    {
        if(arr[i]!=0)
        {
            arr[n-1]+=arr[i];
            arr[i]=0;
            cnt++;
            if(cnt>=k)
            {
                break;
            }
        }
    }

    if(k==0)
    {
        cout<<arr[n-1]-arr[0];
    }
    else
    {
        cout<<arr[n-1];
    }
    cout<<"\n";

}
int main(){
  fio;
  ll int t;
  cin>>t;
  while(t--)
  {
    solving();
  }
  re 0;
  }


#include<bits/stdc++.h>
  struct cod{
    int x=0;
    int y=0;
  }codi;
  #define ll long long
  #define vi vector<long long int>
  #define pb push_back
  #define mp make_pair
  #define pi pair<int,int>
  #define rep(i,a,b) for(int i=a;i<b;i++)
  #define fio  ios::sync_with_stdio(0); cin.tie(0);
  #define mod 1000000007
  #define sqr(a) (a)*(a)
  #define re return
  #define min3(a,b,c) min((a),min((b),(c)))
  using namespace std;
void solving(){
    ll int n,k;
    cin>>n>>k;

    vector<int>arr(n);
    ll int i;
    for(i=0;i<n;i++)
    {
        cin>>arr[i];
    }

    sort(arr.begin(),arr.end());
    int cnt=0;
    for(i=0;i<n-1;i++)
    {
        if(arr[i]!=0)
        {
            arr[i]=0;
            arr[n-1]+=arr[i];

            cnt++;
            if(cnt>=k)
            {
                break;
            }
        }
    }

    if(k==0)
    {
        cout<<arr[n-1]-arr[0];
    }
    else
    {
        cout<<arr[n-1];
    }
    cout<<"\n";

}
int main(){
  fio;
  ll int t;
  cin>>t;
  while(t--)
  {
    solving();
  }
  re 0;
  }


#include<bits/stdc++.h>
  struct cod{
    int x=0;
    int y=0;
  }codi;
  #define ll long long
  #define vi vector<long long int>
  #define pb push_back
  #define mp make_pair
  #define pi pair<int,int>
  #define rep(i,a,b) for(int i=a;i<b;i++)
  #define fio  ios::sync_with_stdio(0); cin.tie(0);
  #define mod 1000000007
  #define sqr(a) (a)*(a)
  #define re return
  #define min3(a,b,c) min((a),min((b),(c)))
  using namespace std;
void solving(){
    ll int n,k;
    cin>>n>>k;

    vector<int>arr(n);
    ll int i;
    for(i=0;i<n;i++)
    {
        cin>>arr[i];
    }

    sort(arr.begin(),arr.end());
    int cnt=0;
    for(i=0;i<n-1;i++)
    {
        if(arr[i]!=0)
        {
            arr[n-1]+=arr[i];
            arr[i]=0;
            cnt++;
            if(cnt>=k)
            {
                break;
            }
        }
    }

        cout<<arr[n-1];

    cout<<"\n";

}
int main(){
  fio;
  ll int t;
  cin>>t;
  while(t--)
  {
    solving();
  }
  re 0;
  }


#include<bits/stdc++.h>
  struct cod{
    int x=0;
    int y=0;
  }codi;
  #define ll long long
  #define vi vector<long long int>
  #define pb push_back
  #define mp make_pair
  #define pi pair<int,int>
  #define rep(i,a,b) for(int i=a;i<b;i++)
  #define fio  ios::sync_with_stdio(0); cin.tie(0);
  #define mod 1000000007
  #define sqr(a) (a)*(a)
  #define re return
  #define min3(a,b,c) min((a),min((b),(c)))
  using namespace std;
void solving(){
    ll int n,k;
    cin>>n>>k;

    vector<int>arr(n);
    ll int i;
    for(i=0;i<n;i++)
    {
        cin>>arr[i];
    }

    sort(arr.begin(),arr.end());
    int cnt=0;
    for(i=0;i<n-1;i++)
    {
        if(arr[i]!=0)
        {
            arr[n-1]+=arr[i];
           // arr[i]=0;
            cnt++;
            if(cnt==k)
            {
                break;
            }
        }
    }

    cout<<arr[n-1];
    cout<<"\n";

}
int main(){
  fio;
  ll int t;
  cin>>t;
  while(t--)
  {
    solving();
  }
  re 0;
  }


#include<bits/stdc++.h>
  struct cod{
    int x=0;
    int y=0;
  }codi;
  #define ll long long
  #define vi vector<long long int>
  #define pb push_back
  #define mp make_pair
  #define pi pair<int,int>
  #define rep(i,a,b) for(int i=a;i<b;i++)
  #define fio  ios::sync_with_stdio(0); cin.tie(0);
  #define mod 1000000007
  #define sqr(a) (a)*(a)
  #define re return
  #define min3(a,b,c) min((a),min((b),(c)))
  using namespace std;
void solving(){
    ll int n,k;
    cin>>n>>k;

    vector<int>arr(n);
    ll int i;
    for(i=0;i<n;i++)
    {
        cin>>arr[i];
    }

    sort(arr.begin(),arr.end());
    int cnt=0;
    for(i=0;i<n-1;i++)
    {
        if(arr[i]!=0)
        {
            arr[n-1]+=arr[i];
            arr[i]=0;
            cnt++;
            if(cnt>=k)
            {
                break;
            }
        }
    }

    cout<<arr[n-1]-arr[0];
    cout<<"\n";

}
int main(){
  fio;
  ll int t;
  cin>>t;
  while(t--)
  {
    solving();
  }
  re 0;
  }


#include<bits/stdc++.h>
  struct cod{
    int x=0;
    int y=0;
  }codi;
  #define ll long long
  #define vi vector<long long int>
  #define pb push_back
  #define mp make_pair
  #define pi pair<int,int>
  #define rep(i,a,b) for(int i=a;i<b;i++)
  #define fio  ios::sync_with_stdio(0); cin.tie(0);
  #define mod 1000000007
  #define sqr(a) (a)*(a)
  #define re return
  #define min3(a,b,c) min((a),min((b),(c)))
  using namespace std;
void solving(){
    ll int n,k;
    cin>>n>>k;

    vector<int> arr(n);
    ll int i;
    ll int sum=0;
    for(i=0;i<n;i++)
    {
        cin>>arr[i];
    }
    sort(arr.begin(),arr.end());
    reverse(arr.begin(),arr.end());
    for(i=0;i<=k;i++)
    {
        sum +=arr[i];
    }
    cout<<sum;
    cout<<"\n";

}
int main(){
  fio;
  ll int t;
  cin>>t;
  while(t--)
  {
    solving();
  }
  re 0;
  }


#include<bits/stdc++.h>
#define ll long long
using namespace std;
void merge_arrays(ll int arr[] , ll int low , ll int mid, ll int high)
{
    ll int a = mid - low + 1;
    ll int b = high - mid;
    ll int arr1[a],arr2[b];
    for(ll int i=0;i<a;i=i+1)
    {
        arr1[i] = arr[low + i];
    }
    for(ll int i=0;i<b;i=i+1)
    {
        arr2[i] = arr[mid + 1 + i];
    }
    ll int i = 0, j = 0, k = low;
    while(i < a && j < b)
    {
        if(arr1[i]<=arr2[j])
        {
            arr[k] = arr1[i];
            i++;
        }
        else
        {
            arr[k] = arr2[j];
            j++;
        }
        k++;
    }
    while(i<a)
    {
        arr[k] = arr1[i];
        i++;
        k++;
    }
    while(j<b)
    {
        arr[k] = arr2[j];
        j++;
        k++;
    }
}
void mergesort(ll int arr[] , ll int low , ll int high)
{
    if(low>=high)
    {
        return;
    }
    else
    {
        ll int mid = low + (high - low)/2;
        mergesort(arr,low,mid);
        mergesort(arr,mid+1,high);
        merge_arrays(arr,low,mid,high);
    }
}
int main()
{
    long int t;
    cin>>t;
    while(t--)
    {
        ll int n,k;
        cin>>n>>k;
        ll int arr[n];
        for(int i=0;i<n;i=i+1)
        {
            cin>>arr[i];
        }
        mergesort(arr,0,n-1);
        ll int sum = arr[n-1];
        for(int i = n-2;i>=0 && k > 0;i=i+1)
        {
            sum += arr[i];
            k -= 1;
        }
        cout<<sum<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

void swap(int* a, int* b)
{
    int t = *a;
    *a = *b;
    *b = t;
}

int partition (int arr[], int low, int high)
{
    int pivot = arr[high];
    int i = (low - 1);

    for (int j = low; j <= high- 1; j++)
    {
        if (arr[j] <= pivot)
        {
            i++;
            swap(&arr[i], &arr[j]);
        }
    }
    swap(&arr[i + 1], &arr[high]);
    return (i + 1);
}

void quickSort(int arr[], int low, int high)
{
    if (low < high)
    {
        int pi = partition(arr, low, high);
        quickSort(arr, low, pi - 1);
        quickSort(arr, pi + 1, high);
    }
}
int main()
{
    int a,b,c,minm;
    cin>>a;
    while(a--)
    {
        cin>>b>>c;
        int arr[b];
        for(int i=0;i<b;i=i+1)
        {
            cin>>arr[i];
        }
        quickSort(arr, 0, b-1);
        for(int i=b-2;i>b-2-c;i=i-1)
        {
            arr[b-1] += arr[i];
            arr[i] = 0;
            minm = arr[i];
        }
        for(int i=0;i<b;i=i+1)
        {
            //cout<<arr[i]<<" ";
        }
        cout<<arr[b-1] - minm<<endl;
    }
    return 0;
}


#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<deque>
#include<iterator>
#include<iostream>
#include<list>
#include<map>
#include<memory>
#include<queue>
#include<set>
#include<stack>
#include<string>
#include<vector>
#include<utility>
#include<bitset>
#define ll long long
const int Y=32000;
const int Day=86400;
const int Hour=3600;
using namespace std;

int Year[Y];
int m[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int summ[13];

bool ly(int k){
     k+=1970-1;
     if(k%100==0) return (k%400==0);
     return (k&3)==0;
}

void init(){
     int i;
     Year[0]=0;
     for(i=1;i<Y;i++)
        if(ly(i)) Year[i]=Year[i-1]+366;
        else Year[i]=Year[i-1]+365;
     summ[0]=0;
     for(i=1;i<=12;i++)
         summ[i]=summ[i-1]+m[i];
}

struct node{
    int hour,min,sec,month,date,day,year;
    int calc_day(){
        int d=Year[year-1];
        d+=summ[month-1];
        if(month>2 && ly(year)) d++;
        d+=date;
        return (d%7+2)%7+1;
    }
}cur,nxt;

ll getsec(node t){
           int d=Year[t.year-1];
           d+=summ[t.month-1];
           if(t.month>2 && ly(t.year)) d++;
           d+=t.date-1;
           int s=t.hour*60*60+t.min*60+t.sec;
           return (ll)s+1LL*d*Day;
}

void next_days(node &c,int d){
     c.date+=d;
     if(c.month==2 && ly(c.year))
        if(c.date<=29) return;
     if(c.date<=m[c.month]) return;
     c.date-=m[c.month];
     if(c.month==2 && ly(c.year)) c.date--;
     c.month++;
     if(c.month<=12) return;
     c.month=1;
     c.year++;
}

void first_day(node &c,int d){
    int curday=c.calc_day();//cout<<curday<<endl;
    while(curday!=d)
    {  next_days(c,1);
       curday=c.calc_day();
    }
}

void set0(node &ans,node st){
     if(st.hour==-1) ans.hour=0; else ans.hour=st.hour;
     if(st.min==-1) ans.min=0; else ans.min=st.min;
     if(st.sec==-1) ans.sec=0; else ans.sec=st.sec;
}

bool ok(node &a,node b){
     if(a.min>-1 && a.min<b.min) return false;
     if(a.min>-1)
     {  if(a.min>b.min)
        {  if(a.sec==-1) a.sec=0;return true;}
        else
        {  if(a.sec>-1 && a.sec<=b.sec) return false;
           if(a.sec>-1) return true;
           if(b.sec==59) return false;
           else {a.sec=b.sec+1;return true;}
        }
     }
     else
     {  if(a.sec>b.sec)
        { a.min=b.min;return true;}
        if(a.sec==b.sec)
        {  if(b.min==59) return false;
           a.min=b.min+1;
           return true;
        }
        if(a.sec>-1)
        {  if(b.min==59) return false;
           a.min=b.min+1;
           return true;
        }
        if(b.sec==59)
        {  if(b.min==59) return false;
           a.min=b.min+1;
           a.sec=0;
           return true;
        }
        a.sec=b.sec+1;
        a.min=b.min;
        return true;
     }
}

bool checktime(node &a,node b){
     if(b.hour==23 && b.min==59 && b.sec==59) return false;
     if(a.hour>-1 && a.hour<b.hour) return false;
     if(a.hour>b.hour)
     {  if(a.min==-1) a.min=0;
        if(a.sec==-1) a.sec=0;
        return true;
     }
     if(a.hour==b.hour) return ok(a,b);
     if(ok(a,b))
     {  a.hour=b.hour;
        return true;
     }
     if(b.hour==23) return false;
     a.hour=b.hour+1;
     if(a.min==-1) a.min=0;
     if(a.sec==-1) a.sec=0;
     return true;
}

node check_mon(const node &c,const node &st){
        node ans;
        ans.year=c.year;
        ans.month=c.month;
        if(st.day==-1)
        {  if(st.date==-1)
           { ans.date=c.date;
             ans.hour=st.hour;ans.min=st.min;ans.sec=st.sec;
             if(checktime(ans,c)) return ans;
             next_days(ans,1);
             if(ans.month==c.month)
             {  set0(ans,st);return ans;}
             ans.year=c.year+1;
             if(st.month!=-1) ans.month=st.month; else ans.month=1;
             ans.date=1;
             set0(ans,st);return ans;
           }
           else
           {  if(st.date>c.date)
              { ans.date=st.date;
                set0(ans,st);return ans;
              }
              else if(st.date==c.date)
              {  ans.date=c.date;
                 ans.hour=st.hour;ans.min=st.min;ans.sec=st.sec;
                 if(checktime(ans,c)) return ans;
              }
              ans.year=c.year+1;
              if(st.month!=-1) ans.month=c.month; else ans.month=1;
              ans.date=st.date;
              set0(ans,st);
              return ans;
           }
        }
        else
        {   ans.date=c.date;
            first_day(ans,st.day);
            if(ans.month==c.month)
            {  if(ans.date==c.date)
               {  ans.hour=st.hour;ans.min=st.min;ans.sec=st.sec;
                  if(checktime(ans,c)) return ans;
                  next_days(ans,1);
                  first_day(ans,st.day);
                  if(ans.month==st.month)
                  {  set0(ans,st);return ans;}
               }
               if(ans.month==c.month)
               {  set0(ans,st);return ans;}
            }
            ans.year++;
            if(st.month!=-1) ans.month=st.month; else ans.month=1;
            ans.date=1;
            first_day(ans,st.day);
            set0(ans,st);
            return ans;
        }
}

node calc(const node &st,const node &c){
     node ans;
     if(st.month>c.month)
     {  ans.year=c.year;
        ans.month=st.month;
        if(st.day==-1)
        {  if(st.date==-1) ans.date=1;
           else{
                if(st.month==2 && st.date==29 && !ly(c.year) )
                  { while(!ly(ans.year)) ans.year++;}
                ans.date=st.date;
           }
           set0(ans,st);
           return ans;
        }
        else
        {  ans.date=1;
           first_day(ans,st.day);
           set0(ans,st);
           return ans;
        }
     }
     else if(st.month<c.month && st.month>0)
     {  ans.year=c.year+1;
        ans.month=st.month;
        if(st.day==-1)
        {  if(st.date==-1) ans.date=1;
           else{ if(st.month==2 && st.date==29 && !ly(c.year))
                 {  while(!ly(ans.year)) ans.year++;}
                 ans.date=st.date;
           }
           set0(ans,st);
           return ans;
        }
        else
        {  ans.date=1;
           first_day(ans,st.day);
           set0(ans,st);
           return ans;
        }
     }
     else if(st.month==c.month)
     {  return check_mon(c,st);
     }
     else
     {  ans.month=c.month;
        ans=check_mon(c,st);
        if(ans.year==c.year) return ans;
        ans.month=c.month+1;
        ans.year=c.year;
        if(ans.month>12)
        { ans.month-=12;
          ans.year=c.year+1;
        }
        if(st.day==-1)
        {  if(st.date==-1)
           {  ans.date=1;
              set0(ans,st);
              return ans;
           }
           else
           {  while(true)
              {  if(ly(ans.year) && st.date>29) ans.month++;
                 else if(st.date>m[ans.month]) ans.month++;
                 else break;
                 if(ans.month>12) {ans.year++;ans.month=1;}
              }
              ans.date=st.date;
              set0(ans,st);
              return ans;
           }
        }
        else
        {   ans.date=1;
            first_day(ans,st.day);
            set0(ans,st);
            return ans;
        }
     }
}

int main(){
    node st;
    scanf("%d%d%d%d%d%d",&st.sec,&st.min,&st.hour,&st.day,&st.date,&st.month);
    int n,i;
    init();
    scanf("%d",&n);
    ll t;
    while(n--)
    {  scanf("%I64d",&t);
       int d=(int)(t/(ll)Day);
       int q=t-1LL*d*Day;
       ++d;
       int cury=upper_bound(Year,Year+Y,d)-Year;
       int curd,curmon;
       d-=Year[cury-1];
       if(d>0)
       {  for(i=1;i<=12;i++)
          {   if(i!=2)
              {  if(d<=m[i]) break;
                 d-=m[i];
              }
              else
              {   int cur;
                  if(ly(cury)) cur=29; else cur=28;
                  if(d<=cur) break;
                  d-=cur;
              }
          }
          curd=d;curmon=i;
       }
       else
       {  curd=31;curmon=12;cury--;}
       int curhr=q/Hour;
       q=q-curhr*Hour;
       int curmin=q/60;
       int cursec=q-60*curmin;
       cur.year=cury;cur.min=curmin;cur.sec=cursec;cur.hour=curhr;
       cur.month=curmon;cur.date=curd;cur.day=cur.calc_day();
       //cout<<cury+1970-1<<' '<<curmon<<' '<<curd<<' '<<cur.day<<' '<<curhr<<' '<<curmin<<cursec<<endl;
       ll res;
       if(st.date!=-1 && st.day!=-1)
       {  node tmp=st;
          tmp.date=-1;
          nxt=calc(tmp,cur);
          //cout<<nxt.year+1970-1<<' '<<nxt.month<<' '<<nxt.date<<' '<<nxt.day<<' '<<nxt.hour<<' '<<nxt.min<<' '<<nxt.sec<<endl;
          res=getsec(nxt);
          tmp=st;tmp.day=-1;
          nxt=calc(tmp,cur);
          //cout<<nxt.year+1970-1<<' '<<nxt.month<<' '<<nxt.date<<' '<<nxt.day<<' '<<nxt.hour<<nxt.min<<nxt.sec<<endl;
          res=min(res,getsec(nxt));
       }
       else
       {  nxt=calc(st,cur);
      // cout<<nxt.year+1970-1<<' '<<nxt.month<<' '<<nxt.date<<' '<<nxt.day<<' '<<nxt.hour<<nxt.min<<nxt.sec<<endl;

          res=getsec(nxt);
       }
       printf("%I64d\n",res);
    }
    //system("pause");
    return 0;
}


#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<deque>
#include<iterator>
#include<iostream>
#include<list>
#include<map>
#include<memory>
#include<queue>
#include<set>
#include<stack>
#include<string>
#include<vector>
#include<utility>
#include<bitset>
#define ll long long
const int Y=32000;
const int Day=86400;
const int Hour=3600;
using namespace std;

int Year[Y];
int m[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int summ[13];

bool ly(int k){
     k+=1970-1;
     if(k%100==0) return (k%400==0);
     return (k&3)==0;
}

void init(){
     int i;
     Year[0]=0;
     for(i=1;i<Y;i++)
        if(ly(i)) Year[i]=Year[i-1]+366;
        else Year[i]=Year[i-1]+365;
     summ[0]=0;
     for(i=1;i<=12;i++)
         summ[i]=summ[i-1]+m[i];
}

struct node{
    int hour,min,sec,month,date,day,year;
    int calc_day(){
        int d=Year[year-1];
        d+=summ[month-1];
        if(month>2 && ly(year)) d++;
        d+=date;
        return (d%7+2)%7+1;
    }
}cur,nxt;

ll getsec(node t){
           int d=Year[t.year-1];
           d+=summ[t.month-1];
           if(t.month>2 && ly(t.year)) d++;
           d+=t.date-1;
           int s=t.hour*60*60+t.min*60+t.sec;
           return (ll)s+1LL*d*Day;
}

void next_days(node &c,int d){
     c.date+=d;
     if(c.month==2 && ly(c.year))
        if(c.date<=29) return;
     if(c.date<=m[c.month]) return;
     c.date-=m[c.month];
     if(c.month==2 && ly(c.year)) c.date--;
     c.month++;
     if(c.month<=12) return;
     c.month=1;
     c.year++;
}

void first_day(node &c,int d){
    int curday=c.calc_day();//cout<<curday<<endl;
    while(curday!=d)
    {  next_days(c,1);
       curday=c.calc_day();
    }
}

void set0(node &ans,node st){
     if(st.hour==-1) ans.hour=0; else ans.hour=st.hour;
     if(st.min==-1) ans.min=0; else ans.min=st.min;
     if(st.sec==-1) ans.sec=0; else ans.sec=st.sec;
}

bool ok(node &a,node b){
     if(a.min>-1 && a.min<b.min) return false;
     if(a.min>-1)
     {  if(a.min>b.min)
        {  if(a.sec==-1) a.sec=0;return true;}
        else
        {  if(a.sec>-1 && a.sec<=b.sec) return false;
           if(a.sec>-1) return true;
           if(b.sec==59) return false;
           else {a.sec=b.sec+1;return true;}
        }
     }
     else
     {  if(a.sec>b.sec)
        { a.min=b.min;return true;}
        if(a.sec==b.sec)
        {  if(b.min==59) return false;
           a.min=b.min+1;
           return true;
        }
        if(a.sec>-1)
        {  if(b.min==59) return false;
           a.min=b.min+1;
           return true;
        }
        if(b.sec==59)
        {  if(b.min==59) return false;
           a.min=b.min+1;
           a.sec=0;
           return true;
        }
        a.sec=b.sec+1;
        a.min=b.min;
        return true;
     }
}

bool checktime(node &a,node b){
     if(b.hour==23 && b.min==59 && b.sec==59) return false;
     if(a.hour>-1 && a.hour<b.hour) return false;
     if(a.hour>b.hour)
     {  if(a.min==-1) a.min=0;
        if(a.sec==-1) a.sec=0;
        return true;
     }
     if(a.hour==b.hour) return ok(a,b);
     if(ok(a,b))
     {  a.hour=b.hour;
        return true;
     }
     if(b.hour==23) return false;
     a.hour=b.hour+1;
     if(a.min==-1) a.min=0;
     if(a.sec==-1) a.sec=0;
     return true;
}

node check_mon(const node &c,const node &st){
        node ans;
        ans.year=c.year;
        ans.month=c.month;
        if(st.day==-1)
        {  if(st.date==-1)
           { ans.date=c.date;
             ans.hour=st.hour;ans.min=st.min;ans.sec=st.sec;
             if(checktime(ans,c)) return ans;
             next_days(ans,1);
             if(ans.month==c.month)
             {  set0(ans,st);return ans;}
             ans.year=c.year+1;
             ans.month=c.month;
             ans.date=c.date;
             set0(ans,st);return ans;
           }
           else
           {  if(st.date>c.date)
              { ans.date=st.date;
                set0(ans,st);return ans;
              }
              else if(st.date==c.date)
              {  ans.date=c.date;
                 ans.hour=st.hour;ans.min=st.min;ans.sec=st.sec;
                 if(checktime(ans,c)) return ans;
              }
              ans.year=c.year+1;
              ans.month=c.month;
              ans.date=st.date;
              set0(ans,st);
              return ans;
           }
        }
        else
        {   ans.date=1;
            while(true)
            {  first_day(ans,st.day);
               if(ans.month!=st.month) break;
               if(ans.date>=c.date) break;
               next_days(ans,1);
            }
            if(ans.month==st.month)
            {  if(ans.date==c.date)
               {  ans.hour=st.hour;ans.min=st.min;ans.sec=st.sec;
                  if(checktime(ans,c)) return ans;
                  next_days(ans,1);
                  first_day(ans,st.day);
                  if(ans.month==st.month)
                  {  set0(ans,st);return ans;}
               }
               if(ans.month==st.month)
               {  set0(ans,st);return ans;}
            }
            ans.year++;
            ans.month=st.month;
            ans.date=1;
            first_day(ans,st.day);
            set0(ans,st);
            return ans;
        }
}

node calc(const node &st,const node &c){
     node ans;
     if(st.month>c.month)
     {  ans.year=c.year;
        ans.month=st.month;
        if(st.day==-1)
        {  if(st.date==-1) ans.date=1;
           else{
                if(st.month==2 && st.date==29 && !ly(c.year) )
                  { while(!ly(ans.year)) ans.year++;}
                ans.date=st.date;
           }
           set0(ans,st);
           return ans;
        }
        else
        {  ans.date=1;
           first_day(ans,st.day);
           set0(ans,st);
           return ans;
        }
     }
     else if(st.month<c.month && st.month>0)
     {  ans.year=c.year+1;
        ans.month=st.month;
        if(st.day==-1)
        {  if(st.date==-1) ans.date=1;
           else{ if(st.month==2 && st.date==29 && !ly(c.year))
                 {  while(!ly(ans.year)) ans.year++;}
                 ans.date=st.date;
           }
           set0(ans,st);
           return ans;
        }
        else
        {  ans.date=1;
           first_day(ans,st.day);
           set0(ans,st);
           return ans;
        }
     }
     else if(st.month==c.month)
     {  return check_mon(c,st);
     }
     else
     {  ans.month=c.month;
        ans=check_mon(c,st);
        if(ans.year==c.year) return ans;
        ans.month=c.month+1;
        if(ans.month>12)
        { ans.month-=12;
          ans.year=c.year+1;
        }
        if(st.day==-1)
        {  if(st.date==-1)
           {  ans.date=1;
              set0(ans,st);
              return ans;
           }
           else
           {  while(true)
              {  if(ly(ans.year) && st.date>29) ans.month++;
                 else if(st.date>m[ans.month]) ans.month++;
                 else break;
                 if(ans.month>12) {ans.year++;ans.month=1;}
              }
              ans.date=st.date;
              set0(ans,st);
              return ans;
           }
        }
        else
        {   ans.date=1;
            first_day(ans,st.day);
            set0(ans,st);
            return ans;
        }
     }
}

int main(){
    node st;
    scanf("%d%d%d%d%d%d",&st.sec,&st.min,&st.hour,&st.day,&st.date,&st.month);
    int n,i;
    init();
    scanf("%d",&n);
    ll t;
    while(n--)
    {  scanf("%I64d",&t);
       int d=(int)(t/(ll)Day);
       int q=t-1LL*d*Day;
       ++d;
       int cury=upper_bound(Year,Year+Y,d)-Year;
       int curd,curmon;
       d-=Year[cury-1];
       if(d>0)
       {  for(i=1;i<=12;i++)
          {   if(i!=2)
              {  if(d<=m[i]) break;
                 d-=m[i];
              }
              else
              {   int cur;
                  if(ly(cury)) cur=29; else cur=28;
                  if(d<=cur) break;
                  d-=cur;
              }
          }
          curd=d;curmon=i;
       }
       else
       {  curd=31;curmon=12;cury--;}
       int curhr=q/Hour;
       q=q-curhr*Hour;
       int curmin=q/60;
       int cursec=q-60*curmin;
       cur.year=cury;cur.min=curmin;cur.sec=cursec;cur.hour=curhr;
       cur.month=curmon;cur.date=curd;cur.day=cur.calc_day();
       //cout<<cury+1970-1<<' '<<curmon<<' '<<curd<<' '<<curhr<<curmin<<cursec<<endl;
       ll res;
       if(st.date!=-1 && st.day!=-1)
       {  node tmp=st;
          tmp.date=-1;
          nxt=calc(tmp,cur);
         // cout<<nxt.year+1970-1<<' '<<nxt.month<<' '<<nxt.date<<' '<<nxt.day<<' '<<nxt.hour<<' '<<nxt.min<<' '<<nxt.sec<<endl;
          res=getsec(nxt);
          tmp=st;tmp.day=-1;
          nxt=calc(tmp,cur);
         // cout<<nxt.year+1970-1<<' '<<nxt.month<<' '<<nxt.date<<' '<<nxt.day<<' '<<nxt.hour<<nxt.min<<nxt.sec<<endl;
          res=min(res,getsec(nxt));
       }
       else
       {  nxt=calc(st,cur);
          res=getsec(nxt);
       }
       printf("%I64d\n",res);
    }
    //system("pause");
    return 0;
}


#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<deque>
#include<iterator>
#include<iostream>
#include<list>
#include<map>
#include<memory>
#include<queue>
#include<set>
#include<stack>
#include<string>
#include<vector>
#include<utility>
#include<bitset>
#define ll long long
const int Y=32000;
const int Day=86400;
const int Hour=3600;
using namespace std;

int Year[Y];
int m[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int summ[13];

bool ly(int k){
     k+=1970-1;
     if(k%100==0) return (k%400==0);
     return (k&3)==0;
}

void init(){
     int i;
     Year[0]=0;
     for(i=1;i<Y;i++)
        if(ly(i)) Year[i]=Year[i-1]+366;
        else Year[i]=Year[i-1]+365;
     summ[0]=0;
     for(i=1;i<=12;i++)
         summ[i]=summ[i-1]+m[i];
}

struct node{
    int hour,min,sec,month,date,day,year;
    int calc_day(){
        int d=Year[year-1];
        d+=summ[month-1];
        if(month>2 && ly(year)) d++;
        d+=date;
        return (d%7+2)%7+1;
    }
}cur,nxt;

ll getsec(node t){
           int d=Year[t.year-1];
           d+=summ[t.month-1];
           if(t.month>2 && ly(t.year)) d++;
           d+=t.date-1;
           int s=t.hour*60*60+t.min*60+t.sec;
           return (ll)s+1LL*d*Day;
}

void next_days(node &c,int d){
     c.date+=d;
     if(c.month==2 && ly(c.year))
        if(c.date<=29) return;
     if(c.date<=m[c.month]) return;
     c.date-=m[c.month];
     if(c.month==2 && ly(c.year)) c.date--;
     c.month++;
     if(c.month<=12) return;
     c.month=1;
     c.year++;
}

void first_day(node &c,int d){
    int curday=c.calc_day();//cout<<curday<<endl;
    while(curday!=d)
    {  next_days(c,1);
       curday=c.calc_day();
    }
}

void set0(node &ans,node st){
     if(st.hour==-1) ans.hour=0; else ans.hour=st.hour;
     if(st.min==-1) ans.min=0; else ans.min=st.min;
     if(st.sec==-1) ans.sec=0; else ans.sec=st.sec;
}

bool ok(node &a,node b){
     if(a.min>-1 && a.min<b.min) return false;
     if(a.min>-1)
     {  if(a.min>b.min)
        {  if(a.sec==-1) a.sec=0;return true;}
        else
        {  if(a.sec>-1 && a.sec<=b.sec) return false;
           if(a.sec>-1) return true;
           if(b.sec==59) return false;
           else {a.sec=b.sec+1;return true;}
        }
     }
     else
     {  if(a.sec>b.sec)
        { a.min=b.min;return true;}
        if(a.sec==b.sec)
        {  if(b.min==59) return false;
           a.min=b.min+1;
           return true;
        }
        if(a.sec>-1)
        {  if(b.min==59) return false;
           a.min=b.min+1;
           return true;
        }
        if(b.sec==59)
        {  if(b.min==59) return false;
           a.min=b.min+1;
           a.sec=0;
           return true;
        }
        a.sec=b.sec+1;
        a.min=b.min;
        return true;
     }
}

bool checktime(node &a,node b){
     if(b.hour==23 && b.min==59 && b.sec==59) return false;
     if(a.hour>-1 && a.hour<b.hour) return false;
     if(a.hour>b.hour)
     {  if(a.min==-1) a.min=0;
        if(a.sec==-1) a.sec=0;
        return true;
     }
     if(a.hour==b.hour) return ok(a,b);
     if(ok(a,b))
     {  a.hour=b.hour;
        return true;
     }
     if(b.hour==23) return false;
     a.hour=b.hour+1;
     if(a.min==-1) a.min=0;
     if(a.sec==-1) a.sec=0;
     return true;
}

node check_mon(const node &c,const node &st){
        node ans;
        ans.year=c.year;
        ans.month=c.month;
        if(st.day==-1)
        {  if(st.date==-1)
           { ans.date=c.date;
             ans.hour=st.hour;ans.min=st.min;ans.sec=st.sec;
             if(checktime(ans,c)) return ans;
             next_days(ans,1);
             if(ans.month==c.month)
             {  set0(ans,st);return ans;}
             ans.year=c.year+1;
             ans.month=c.month;
             ans.date=c.date;
             set0(ans,st);return ans;
           }
           else
           {  if(st.date>c.date)
              { ans.date=st.date;
                set0(ans,st);return ans;
              }
              else if(st.date==c.date)
              {  ans.date=c.date;
                 ans.hour=st.hour;ans.min=st.min;ans.sec=st.sec;
                 if(checktime(ans,c)) return ans;
              }
              ans.year=c.year+1;
              ans.month=c.month;
              ans.date=st.date;
              set0(ans,st);
              return ans;
           }
        }
        else
        {   ans.date=1;
            while(true)
            {  first_day(ans,st.day);
               if(ans.month!=st.month) break;
               if(ans.date>=c.date) break;
               next_days(ans,1);
            }
            if(ans.month==st.month)
            {  if(ans.date==c.date)
               {  ans.hour=st.hour;ans.min=st.min;ans.sec=st.sec;
                  if(checktime(ans,c)) return ans;
                  next_days(ans,1);
                  first_day(ans,st.day);
                  if(ans.month==st.month)
                  {  set0(ans,st);return ans;}
               }
               if(ans.month==st.month)
               {  set0(ans,st);return ans;}
            }
            ans.year++;
            ans.month=st.month;
            ans.date=1;
            first_day(ans,st.day);
            set0(ans,st);
            return ans;
        }
}

node calc(const node &st,const node &c){
     node ans;
     if(st.month>c.month)
     {  ans.year=c.year;
        ans.month=st.month;
        if(st.day==-1)
        {  if(st.date==-1) ans.date=1;
           else{
                if(st.month==2 && st.date==29 && !ly(c.year) )
                  { while(!ly(ans.year)) ans.year++;}
                ans.date=st.date;
           }
           set0(ans,st);
           return ans;
        }
        else
        {  ans.date=1;
           first_day(ans,st.day);
           set0(ans,st);
           return ans;
        }
     }
     else if(st.month<c.month && st.month>0)
     {  ans.year=c.year+1;
        ans.month=st.month;
        if(st.day==-1)
        {  if(st.date==-1) ans.date=1;
           else{ if(st.month==2 && st.date==29 && !ly(c.year))
                 {  while(!ly(ans.year)) ans.year++;}
                 ans.date=st.date;
           }
           set0(ans,st);
           return ans;
        }
        else
        {  ans.date=1;
           first_day(ans,st.day);
           set0(ans,st);
           return ans;
        }
     }
     else if(st.month==c.month)
     {  return check_mon(c,st);
     }
     else
     {  ans.month=c.month;
        ans=check_mon(c,st);
        if(ans.year==c.year) return ans;
        ans.month=c.month+1;
        if(ans.month>12)
        { ans.month-=12;
          ans.year=c.year+1;
        }
        if(st.day==-1)
        {  if(st.date==-1)
           {  ans.date=1;
              set0(ans,st);
              return ans;
           }
           else
           {  while(true)
              {  if(ly(ans.year) && st.date>29) ans.month++;
                 else if(st.date>m[ans.month]) ans.month++;
                 else break;
                 if(ans.month>12) {ans.year++;ans.month=1;}
              }
              ans.date=st.date;
              set0(ans,st);
              return ans;
           }
        }
        else
        {   ans.date=1;
            first_day(ans,st.day);
            set0(ans,st);
            return ans;
        }
     }
}

int main(){
    node st;
    scanf("%d%d%d%d%d%d",&st.sec,&st.min,&st.hour,&st.day,&st.date,&st.month);
    int n,i;
    init();
    scanf("%d",&n);
    ll t;
    while(n--)
    {  scanf("%I64d",&t);
       int d=(int)(t/(ll)Day);
       int q=t-1LL*d*Day;
       ++d;
       int cury=upper_bound(Year,Year+Y,d)-Year;
       int curd,curmon;
       d-=Year[cury-1];
       if(d>0)
       {  for(i=1;i<=12;i++)
          {   if(i!=2)
              {  if(d<=m[i]) break;
                 d-=m[i];
              }
              else
              {   int cur;
                  if(ly(cury)) cur=29; else cur=28;
                  if(d<=cur) break;
                  d-=cur;
              }
          }
          curd=d;curmon=i;
       }
       else
       {  curd=31;curmon=12;cury--;}
       int curhr=q/Hour;
       q=q-curhr*Hour;
       int curmin=q/60;
       int cursec=q-60*curmin;
       cur.year=cury;cur.min=curmin;cur.sec=cursec;cur.hour=curhr;
       cur.month=curmon;cur.date=curd;cur.day=cur.calc_day();
       //cout<<cury+1970-1<<' '<<curmon<<' '<<curd<<' '<<cur.day<<' '<<curhr<<' '<<curmin<<cursec<<endl;
       ll res;
       if(st.date!=-1 && st.day!=-1)
       {  node tmp=st;
          tmp.date=-1;
          nxt=calc(tmp,cur);
         // cout<<nxt.year+1970-1<<' '<<nxt.month<<' '<<nxt.date<<' '<<nxt.day<<' '<<nxt.hour<<' '<<nxt.min<<' '<<nxt.sec<<endl;
          res=getsec(nxt);
          tmp=st;tmp.day=-1;
          nxt=calc(tmp,cur);
         // cout<<nxt.year+1970-1<<' '<<nxt.month<<' '<<nxt.date<<' '<<nxt.day<<' '<<nxt.hour<<nxt.min<<nxt.sec<<endl;
          res=min(res,getsec(nxt));
       }
       else
       {  nxt=calc(st,cur);
          res=getsec(nxt);
       }
       if(res==654057194400) cout<<cury+1970-1<<' '<<curmon<<' '<<curd<<' '<<cur.day<<' '<<curhr<<' '<<curmin<<cursec<<endl;
       printf("%I64d\n",res);
    }
    //system("pause");
    return 0;
}


#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<deque>
#include<iterator>
#include<iostream>
#include<list>
#include<map>
#include<memory>
#include<queue>
#include<set>
#include<stack>
#include<string>
#include<vector>
#include<utility>
#include<bitset>
#define ll long long
const int Y=32000;
const int Day=86400;
const int Hour=3600;
using namespace std;

int Year[Y];
int m[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int summ[13];

bool ly(int k){
     k+=1970-1;
     if(k%100==0) return (k%400==0);
     return (k&3)==0;
}

void init(){
     int i;
     Year[0]=0;
     for(i=1;i<Y;i++)
        if(ly(i)) Year[i]=Year[i-1]+366;
        else Year[i]=Year[i-1]+365;
     summ[0]=0;
     for(i=1;i<=12;i++)
         summ[i]=summ[i-1]+m[i];
}

struct node{
    int hour,min,sec,month,date,day,year;
    int calc_day(){
        int d=Year[year-1];
        d+=summ[month-1];
        if(month>2 && ly(year)) d++;
        d+=date;
        return (d%7+2)%7+1;
    }
}cur,nxt;

ll getsec(node t){
           int d=Year[t.year-1];
           d+=summ[t.month-1];
           if(t.month>2 && ly(t.year)) d++;
           d+=t.date-1;
           int s=t.hour*60*60+t.min*60+t.sec;
           return (ll)s+1LL*d*Day;
}

void next_days(node &c,int d){
     c.date+=d;
     if(c.month==2 && ly(c.year))
        if(c.date<=29) return;
     if(c.date<=m[c.month]) return;
     c.date-=m[c.month];
     if(c.month==2 && ly(c.year)) c.date--;
     c.month++;
     if(c.month<=12) return;
     c.month=1;
     c.year++;
}

void first_day(node &c,int d){
    int curday=c.calc_day();//cout<<curday<<endl;
    while(curday!=d)
    {  next_days(c,1);
       curday=c.calc_day();
    }
}

void set0(node &ans,node st){
     if(st.hour==-1) ans.hour=0; else ans.hour=st.hour;
     if(st.min==-1) ans.min=0; else ans.min=st.min;
     if(st.sec==-1) ans.sec=0; else ans.sec=st.sec;
}

bool ok(node &a,node b){
     if(a.min>-1 && a.min<b.min) return false;
     if(a.min>-1)
     {  if(a.min>b.min)
        {  if(a.sec==-1) a.sec=0;return true;}
        else
        {  if(a.sec>-1 && a.sec<=b.sec) return false;
           if(a.sec>-1) return true;
           if(b.sec==59) return false;
           else {a.sec=b.sec+1;return true;}
        }
     }
     else
     {  if(a.sec>b.sec)
        { a.min=b.min;return true;}
        if(a.sec==b.sec)
        {  if(b.min==59) return false;
           a.min=b.min+1;
           return true;
        }
        if(a.sec>-1)
        {  if(b.min==59) return false;
           a.min=b.min+1;
           return true;
        }
        if(b.sec==59)
        {  if(b.min==59) return false;
           a.min=b.min+1;
           a.sec=0;
           return true;
        }
        a.sec=b.sec+1;
        a.min=b.min;
        return true;
     }
}

bool checktime(node &a,node b){
     if(b.hour==23 && b.min==59 && b.sec==59) return false;
     if(a.hour>-1 && a.hour<b.hour) return false;
     if(a.hour>b.hour)
     {  if(a.min==-1) a.min=0;
        if(a.sec==-1) a.sec=0;
        return true;
     }
     if(a.hour==b.hour) return ok(a,b);
     if(ok(a,b))
     {  a.hour=b.hour;
        return true;
     }
     if(b.hour==23) return false;
     a.hour=b.hour+1;
     if(a.min==-1) a.min=0;
     if(a.sec==-1) a.sec=0;
     return true;
}

node check_mon(const node &c,const node &st){
        node ans;
        ans.year=c.year;
        ans.month=c.month;
        if(st.day==-1)
        {  if(st.date==-1)
           { ans.date=c.date;
             ans.hour=st.hour;ans.min=st.min;ans.sec=st.sec;
             if(checktime(ans,c)) return ans;
             next_days(ans,1);
             if(ans.month==c.month)
             {  set0(ans,st);return ans;}
             ans.year=c.year+1;
             ans.month=c.month;
             ans.date=1;
             set0(ans,st);return ans;
           }
           else
           {  if(st.date>c.date)
              { ans.date=st.date;
                set0(ans,st);return ans;
              }
              else if(st.date==c.date)
              {  ans.date=c.date;
                 ans.hour=st.hour;ans.min=st.min;ans.sec=st.sec;
                 if(checktime(ans,c)) return ans;
              }
              ans.year=c.year+1;
              ans.month=c.month;
              ans.date=st.date;
              set0(ans,st);
              return ans;
           }
        }
        else
        {   ans.date=1;
            while(true)
            {  first_day(ans,st.day);
               if(ans.month!=st.month) break;
               if(ans.date>=c.date) break;
               next_days(ans,1);
            }
            if(ans.month==st.month)
            {  if(ans.date==c.date)
               {  ans.hour=st.hour;ans.min=st.min;ans.sec=st.sec;
                  if(checktime(ans,c)) return ans;
                  next_days(ans,1);
                  first_day(ans,st.day);
                  if(ans.month==st.month)
                  {  set0(ans,st);return ans;}
               }
               if(ans.month==st.month)
               {  set0(ans,st);return ans;}
            }
            ans.year++;
            ans.month=st.month;
            ans.date=1;
            first_day(ans,st.day);
            set0(ans,st);
            return ans;
        }
}

node calc(const node &st,const node &c){
     node ans;
     if(st.month>c.month)
     {  ans.year=c.year;
        ans.month=st.month;
        if(st.day==-1)
        {  if(st.date==-1) ans.date=1;
           else{
                if(st.month==2 && st.date==29 && !ly(c.year) )
                  { while(!ly(ans.year)) ans.year++;}
                ans.date=st.date;
           }
           set0(ans,st);
           return ans;
        }
        else
        {  ans.date=1;
           first_day(ans,st.day);
           set0(ans,st);
           return ans;
        }
     }
     else if(st.month<c.month && st.month>0)
     {  ans.year=c.year+1;
        ans.month=st.month;
        if(st.day==-1)
        {  if(st.date==-1) ans.date=1;
           else{ if(st.month==2 && st.date==29 && !ly(c.year))
                 {  while(!ly(ans.year)) ans.year++;}
                 ans.date=st.date;
           }
           set0(ans,st);
           return ans;
        }
        else
        {  ans.date=1;
           first_day(ans,st.day);
           set0(ans,st);
           return ans;
        }
     }
     else if(st.month==c.month)
     {  return check_mon(c,st);
     }
     else
     {  ans.month=c.month;
        ans=check_mon(c,st);
        if(ans.year==c.year) return ans;
        ans.month=c.month+1;
        if(ans.month>12)
        { ans.month-=12;
          ans.year=c.year+1;
        }
        if(st.day==-1)
        {  if(st.date==-1)
           {  ans.date=1;
              set0(ans,st);
              return ans;
           }
           else
           {  while(true)
              {  if(ly(ans.year) && st.date>29) ans.month++;
                 else if(st.date>m[ans.month]) ans.month++;
                 else break;
                 if(ans.month>12) {ans.year++;ans.month=1;}
              }
              ans.date=st.date;
              set0(ans,st);
              return ans;
           }
        }
        else
        {   ans.date=1;
            first_day(ans,st.day);
            set0(ans,st);
            return ans;
        }
     }
}

int main(){
    node st;
    scanf("%d%d%d%d%d%d",&st.sec,&st.min,&st.hour,&st.day,&st.date,&st.month);
    int n,i;
    init();
    scanf("%d",&n);
    ll t;
    while(n--)
    {  scanf("%I64d",&t);
       int d=(int)(t/(ll)Day);
       int q=t-1LL*d*Day;
       ++d;
       int cury=upper_bound(Year,Year+Y,d)-Year;
       int curd,curmon;
       d-=Year[cury-1];
       if(d>0)
       {  for(i=1;i<=12;i++)
          {   if(i!=2)
              {  if(d<=m[i]) break;
                 d-=m[i];
              }
              else
              {   int cur;
                  if(ly(cury)) cur=29; else cur=28;
                  if(d<=cur) break;
                  d-=cur;
              }
          }
          curd=d;curmon=i;
       }
       else
       {  curd=31;curmon=12;cury--;}
       int curhr=q/Hour;
       q=q-curhr*Hour;
       int curmin=q/60;
       int cursec=q-60*curmin;
       cur.year=cury;cur.min=curmin;cur.sec=cursec;cur.hour=curhr;
       cur.month=curmon;cur.date=curd;cur.day=cur.calc_day();
       //cout<<cury+1970-1<<' '<<curmon<<' '<<curd<<' '<<cur.day<<' '<<curhr<<' '<<curmin<<cursec<<endl;
       ll res;
       if(st.date!=-1 && st.day!=-1)
       {  node tmp=st;
          tmp.date=-1;
          nxt=calc(tmp,cur);
         // cout<<nxt.year+1970-1<<' '<<nxt.month<<' '<<nxt.date<<' '<<nxt.day<<' '<<nxt.hour<<' '<<nxt.min<<' '<<nxt.sec<<endl;
          res=getsec(nxt);
          tmp=st;tmp.day=-1;
          nxt=calc(tmp,cur);
         // cout<<nxt.year+1970-1<<' '<<nxt.month<<' '<<nxt.date<<' '<<nxt.day<<' '<<nxt.hour<<nxt.min<<nxt.sec<<endl;
          res=min(res,getsec(nxt));
       }
       else
       {  nxt=calc(st,cur);
          res=getsec(nxt);
       }
       //if(res==654057194400) cout<<cury+1970-1<<curmon<<curd<<cur.day<<curhr<<curmin<<cursec<<endl;
       printf("%I64d\n",res);
    }
    //system("pause");
    return 0;
}