#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <functional> //for less
using namespace __gnu_pbds;
using namespace std;
#define int long long int
#define m_p make_pair
#define pb push_back
#define f first
#define s second
#define pii pair<int,int>
#define ios ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define double long double
const double pi=3.14159265358979;
const   int  mod=1e9+7;
int takemod(int a){return ((a%mod)+mod)%mod;}
typedef  tree < int ,  null_type ,  less<int> ,  rb_tree_tag ,  tree_order_statistics_node_update > ordered_set;
void ans(int p)
{
	if(p%2==0)cout<<"Ari"<<endl;
	else cout<<"Rich"<<endl;
}
signed main()
{
	ios
    int n,k;
    cin>>n>>k;
    string s;
    cin>>s;
    if(n==1)
    {
    	k--;
    	while(k--)s+=s[0];
    	cout<<s;
    	return 0;
    }
    int i=0;
    int j=n-1;
    int flag=0;
    int foo=0;
    string h1="",h2="",h4="",h5="";
    while(j>i)
    {
    	h1+=s[i];
    	h2+=s[j];
    	string h3=h2;
    	//cout<<h1<<" "<<h2<<endl;
    	reverse(h3.begin(),h3.end());
    	if(h3==h1)foo=1;
    	i++;
    	j--;

    }
    i++;
    j--;

    int  k1 = n/(h1.size());
       set<string> st;
   if(foo==1 and n%h1.size()==0)
    {
       int t=k1-2;
       while(t--)
       	{
       		st.insert(s.substr(i,h1.size()));
       		 i+=h1.size();
       	}

    }
    set<string> :: iterator itr;
    itr=st.begin();

    if((st.size()==1 and *itr==h1 and foo==1) or (foo==1 and k1==2 and n==2*h1.size()))flag=1;

    if(flag==1)
    {
    	k--;
    	while(k--)s+=h1;
    	cout<<s;
    	return 0;
    }
    if(foo==1)
    {
    	i=h1.size();
    	h1=s.substr(i,s.size());
    	k--;
    	while(k--)s+=h1;
    	cout<<s;
    	return 0;
    }
    i=0;
    j=s.size()-1;
    while(j>i and s[i]==s[j]){i++;j--;}
    string t=s.substr(i,s.size());

    k--;
    while(k--)s+=t;
    cout<<s;

}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <functional> //for less
using namespace __gnu_pbds;
using namespace std;
#define int long long int
#define m_p make_pair
#define pb push_back
#define f first
#define s second
#define pii pair<int,int>
#define ios ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define double long double
const double pi=3.14159265358979;
const   int  mod=1e9+7;
int takemod(int a){return ((a%mod)+mod)%mod;}
typedef  tree < int ,  null_type ,  less<int> ,  rb_tree_tag ,  tree_order_statistics_node_update > ordered_set;

signed main()
{
	ios
    int n,k;
    cin>>n>>k;
    string s;

    cin>>s;
    string cp=s;
    int i=0;
    int j=n-1;
    string h1="",h2="";
    int ind=0;
    int foo=0;
    while(j>i)
    {
    	h1+=s[i];
    	h2+=s[j];

    	string h3=h2;
    	reverse(h3.begin(),h3.end());
    	if(h3==h1)
    		{
    			ind=i;
    			foo=1;

    		}
    	i++;
    	j--;
    }

    if(foo==1)ind++;
    int ans=k;

    string h=s.substr(ind,s.size());
    while( ans--)s+=h;
    i=0;
    k--;
    int f=1;
    while(k--)
    {
     f=s.find(cp,f);
     f++;
    }
    f--;
    f+=cp.size()-1;
    i=0;
    while(i<=f)
    {
    	cout<<s[i];
    	i++;
    }

}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <functional> //for less
using namespace __gnu_pbds;
using namespace std;
#define int long long int
#define m_p make_pair
#define pb push_back
#define f first
#define s second
#define pii pair<int,int>
#define ios ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define double long double
const double pi=3.14159265358979;
const   int  mod=1e9+7;
int takemod(int a){return ((a%mod)+mod)%mod;}
typedef  tree < int ,  null_type ,  less<int> ,  rb_tree_tag ,  tree_order_statistics_node_update > ordered_set;
void ans(int p)
{
	if(p%2==0)cout<<"Ari"<<endl;
	else cout<<"Rich"<<endl;
}
signed main()
{
	ios
    int n,k;
    cin>>n>>k;
    string s;
    cin>>s;
    if(n==1)
    {
    	k--;
    	while(k--)s+=s[0];
    	cout<<s;
    	return 0;
    }
    int i=0;
    int j=n-1;
    int flag=0;
    int foo=0;
    string h1="",h2="",h4="",h5="";
    while(j>i)
    {
    	h1+=s[i];
    	h2+=s[j];
    	string h3=h2;
    	//cout<<h1<<" "<<h2<<endl;
    	reverse(h3.begin(),h3.end());
    	if(h3==h1)
    		{
    			foo=1;
    			break;

    		}
    	i++;
    	j--;

    }
    i++;
    j--;

    int  k1 = n/(h1.size());
       set<string> st;

    if(foo==1 and n%h1.size()==0)
    {
       int t=k1-2;
       while(t--)
       	{
       		st.insert(s.substr(i,h1.size()));
       		 i+=h1.size();
       	}

    }
    set<string> :: iterator itr;
    itr=st.begin();

    if(st.size()==1 and *itr==h1)flag=1;

    if(flag==1)
    {
    	k--;
    	while(k--)s+=h1;
    	cout<<s;
    	return 0;
    }

    i=0;
    j=s.size()-1;
    while(j>i and s[i]==s[j]){i++;j--;}
    string t=s.substr(i,s.size());

    k--;
    while(k--)s+=t;
    cout<<s;

}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <functional> //for less
using namespace __gnu_pbds;
using namespace std;
#define int long long int
#define m_p make_pair
#define pb push_back
#define f first
#define s second
#define pii pair<int,int>
#define ios ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define double long double
const double pi=3.14159265358979;
const   int  mod=1e9+7;
int takemod(int a){return ((a%mod)+mod)%mod;}
typedef  tree < int ,  null_type ,  less<int> ,  rb_tree_tag ,  tree_order_statistics_node_update > ordered_set;
void ans(int p)
{
	if(p%2==0)cout<<"Ari"<<endl;
	else cout<<"Rich"<<endl;
}
signed main()
{
	ios
    int n,k;
    cin>>n>>k;
    string s;
    cin>>s;
    if(n==1)
    {
    	k--;
    	while(k--)s+=s[0];
    	cout<<s;
    	return 0;
    }
    int i=0;
    int j=n-1;
    int flag=0;
    int foo=0;
    string h1="",h2="",h4="",h5="";
    while(j>i)
    {
    	h1+=s[i];
    	h2+=s[j];
    	string h3=h2;
    	//cout<<h1<<" "<<h2<<endl;
    	reverse(h3.begin(),h3.end());
    	if(h3==h1)
    		{
    			foo=1;
    			break;

    		}
    	i++;
    	j--;

    }
    i++;
    j--;

    int  k1 = n/(h1.size());
       set<string> st;

    if(foo==1 and n%h1.size()==0)
    {
       int t=k1-2;
       while(t--)
       	{
       		st.insert(s.substr(i,h1.size()));
       		 i+=h1.size();
       	}

    }
    set<string> :: iterator itr;
    itr=st.begin();

    if((st.size()==1 and *itr==h1 and foo==1) or (foo==1 and k1==2 and n==2*h1.size()))flag=1;

    if(flag==1)
    {
    	k--;
    	while(k--)s+=h1;
    	cout<<s;
    	return 0;
    }

    i=0;
    j=s.size()-1;
    while(j>i and s[i]==s[j]){i++;j--;}
    string t=s.substr(i,s.size());

    k--;
    while(k--)s+=t;
    cout<<s;

}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <functional> //for less
using namespace __gnu_pbds;
using namespace std;
#define int long long int
#define m_p make_pair
#define pb push_back
#define f first
#define s second
#define pii pair<int,int>
#define ios ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define double long double
const double pi=3.14159265358979;
const   int  mod=1e9+7;
int takemod(int a){return ((a%mod)+mod)%mod;}
typedef  tree < int ,  null_type ,  less<int> ,  rb_tree_tag ,  tree_order_statistics_node_update > ordered_set;
signed main()
{
	ios
    int n,k;
    cin>>n>>k;
    string s;
    cin>>s;
    int i=0;
    int j=s.size()-1;
    while(j>i and s[i]==s[j]){i++;j--;}
    string t=s.substr(i,s.size());

    k--;
    while(k--)s+=t;
    cout<<s;
}


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

int main () {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    ll t=1;
//    cin >> t;
    while (t--) {
        ll n,k;
        cin >> n>>k;
        string s;
        cin >> s;

        string pre="";
        ll len=0, start=0;
        for(ll i=1; i<n; ++i) {
            if(s[i]==s[start]) {
                start++;
            } else {
                start=0;
            }
        }
        if (start == 0) {
            start=s[0]==s[n-1];
        }
        pre=s.substr(start, n);
        for(ll i=0; i<k-1; ++i) {
            s += pre;
        }
        cout << s << "\n";
    }

    return 0;
}


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

int solve(string s)
{
    int n = s.length();

    int lps[n];
    lps[0] = 0;

    int len = 0;

    int i = 1;
    while (i < n)
    {
        if (s[i] == s[len])
        {
            len++;
            lps[i] = len;
            i++;
        }
        else
        {
            if (len != 0)
            {
                len = lps[len-1];
            }
            else
            {
                lps[i] = 0;
                i++;
            }
        }
    }

    int res = lps[n-1];
    return res;
}

int main () {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    ll t=1;
//    cin >> t;
    while (t--) {
        ll n,k;
        cin >> n>>k;
        string s;
        cin >> s;

        string pre=s.substr(solve(s), n);
        for(ll i=0; i<k-1; ++i) {
            s += pre;
        }
        cout << s << "\n";
    }

    return 0;
}


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

int solve(string s)
{
    int n = s.length();

    int lps[n];
    lps[0] = 0;

    int len = 0;

    int i = 1;
    while (i < n)
    {
        if (s[i] == s[len])
        {
            len++;
            lps[i] = len;
            i++;
        }
        else
        {
            if (len != 0)
            {
                len = lps[len-1];
            }
            else
            {
                lps[i] = 0;
                i++;
            }
        }
    }

    int res = lps[n-1];
    return res;
}

int main () {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    ll t=1;

    while (t--) {
        ll n,k;
        cin >> n>>k;
        string s;
        cin >> s;

        string pre=s.substr(solve(s), n);
        for(ll i=0; i<k-1; ++i) {
            s += pre;
        }
        cout << s << "\n";
    }

    return 0;
}


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

int main () {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    ll t=1;
//    cin >> t;
    while (t--) {
        ll n,k;
        cin >> n>>k;
        string s;
        cin >> s;

        string pre="";
        ll len=0, start=0;
        for(ll i=1; i<n; ++i) {
            if(s[i]==s[start]) {
                start++;
            } else {
                start=0;
                if (s[start]==s[i]) start=1;
            }
        }
        pre=s.substr(start, n);
        for(ll i=0; i<k-1; ++i) {
            s += pre;
        }
        cout << s << "\n";
    }

    return 0;
}


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

//abcdabca
//abcdbabcd
//babbabab
/*
0 0 0 0 1 2 3 0
*/
int solve (string s, int n) {
    vector<int>pre(n, 0);
    int len=0, i=1;

    while(i<n) {
        if(s[i]==s[len]) {
            s[i]=++len;
            ++i;
        } else {
            if (len==0) {
                ++i;
                continue ;
            }
            len=pre[len-1];
        }
    }
    return pre[n-1];
}

int main () {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    ll t=1;
//    cin >> t;
    while (t--) {
        ll n,k;
        cin >> n>>k;
        string s;
        cin >> s;

        string pre=s.substr(solve(s, n), n);
        for(ll i=0; i<k-1; ++i) {
            s += pre;
        }
        cout << s << "\n";
    }

    return 0;
}


#include <bits/stdc++.h>
#define all(nums) nums.begin(), nums.end()
#define allr(nums) nums.rbegin(), nums.rend()
using namespace std;
typedef long long int ll;
typedef vector<vector<int>>graph;
const ll imax = INT64_MAX;
const int imin = INT32_MIN;
const ll inf=1e18;
const ll MOD=1000000007;
const ll range=2*1e5+10;
template<class T>T cal(T val) {if (val<2)   return 0;return (val*1ll*(val-1))/2;}
template<class T>T mx(T begin, T end){return max_element(begin, end);}
template<class T>T mn(T begin, T end){return min_element(begin, end);}
template<class T>void print(vector<T>nums){for(T i:nums)cout<<i<<" ";cout<<"\n";}
template<class T>void read(vector<T>&nums){for(T& i:nums)cin>>i;}
template<class T>void alloc(vector<vector<T>*>nums, T n){for(auto v:nums)v->resize(n+1);}
template<class T>T mod(T val){val=(val%MOD+MOD)%MOD; return val;}

int pre(string s) {
    int r=0, n=s.size();
    vector<int>look(n);
    for (int i=1; i<n; ++i) {
        if (s[i] == s[r]) {
            look[i]=++r;
        } else {
            if (r == 0) {
                continue ;
            }
            r=look[r-1];
            i--;
        }
    }
    return look[n-1];
}
int main () {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    ll t=1;
    // cin>>t;
    while (t--) {
        int n, k;
        cin>>n>>k;
        string s;
        cin>>s;
        int len=pre(s);
         string p=s.substr(pre(s), n);
        for(ll i=0; i<k-1; ++i) {
            s += p;
        }
        cout << s << "\n";
    }

    return 0;
}


//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx,avx2,fma")
//#pragma GCC optimization ("unroll-loops")
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <bitset>
#include <stack>
#include <deque>
#include <iterator>
#include <tuple>
#include <iomanip>
#include <valarray>
#include <limits>
#include <sstream>
#include <cassert>
#include <functional>
#include <numeric>
#define in insert
#define pb push_back
#define mp make_pair
#define int long long
#define fi first
#define double long double
#define se second
#define endl '\n'
#define pii pair<int,int>
#define pdd pair<double,double>
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);
#define mod 1000000007
#define MAXN  301
#define ll unsigned long long
using namespace std;
/*using namespace __gnu_pbds;
 typedef tree<int, null_type, less<int>, rb_tree_tag,
 tree_order_statistics_node_update>
ordered_set;*/
int spf[MAXN];
int fac[MAXN];
void sieve()//sieve
{
    spf[1] = 1;
    for (int i=2; i<MAXN; i++)
        spf[i] = i;
    for (int i=4; i<MAXN; i+=2)
        spf[i] = 2;
    for (int i=3; i*i<MAXN; i++)
    {
        if (spf[i]==i)
        {
            for (int j=i*i; j<MAXN; j+=i)
                if (spf[j]==j)
                    spf[j]=i;
        }
    }
}//sieve ends
map<int,int> getfactor(int a)//factor prime
{
    map<int,int> m;
    while(a>1)
    {
        m[spf[a]]++;
        a/=spf[a];
    }
    return m;
}//prime factor ends
int power(int x,int y,int p)
{
    int res = 1;
    x = x%p;
    if (x == 0) return 0;
    while (y > 0)
    {
        if (y & 1)
            res = (res*x)% p;
        y = y>>1;
        x = (x*x)%p;
    }
    return res;
}
int gcd(int a, int b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}
int inverse(int a, int p)
{
    return power(a, p-2, p);
}
int ncr(int n, int r, int p)
{
    if(r>n)
        return 0;
    if (r==0)
        return 1;
    return (fac[n]*inverse(fac[r], p) % p *
            inverse(fac[n-r], p) % p) % p;
}
void solve()
{
    int n,k;
    cin>>n>>k;
    string s;
    int ans=0;
    for(int i=1;i<n;i++)
    {
        int l=i;
        int flag=0;
        for(int j=0,l<s.size();l++,j++)
        {
            if(s[l]!=s[j])
                flag++;
        }
        if(!flag)
            ans=l;
    }
    if(ans==0)
        ans=n-1;
    string t;
    for(int i=0;i<k;i++)
    {
        for(int j=0;j<ans;j++)
            t.pb(s[j]);
    }
    for(int i=ans;i<s.size();i++)
        t.pb(s[i]);
}
signed main()
{
    fast
    int test=1;
    //cin>>test;
    while(test--)
    {
        solve();
    }
}


//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx,avx2,fma")
//#pragma GCC optimization ("unroll-loops")
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <bitset>
#include <stack>
#include <deque>
#include <iterator>
#include <tuple>
#include <iomanip>
#include <valarray>
#include <limits>
#include <sstream>
#include <cassert>
#include <functional>
#include <numeric>
#define in insert
#define pb push_back
#define mp make_pair
#define int long long
#define fi first
#define double long double
#define se second
#define endl '\n'
#define pii pair<int,int>
#define pdd pair<double,double>
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);
#define mod 1000000007
#define MAXN  301
#define ll unsigned long long
using namespace std;
/*using namespace __gnu_pbds;
 typedef tree<int, null_type, less<int>, rb_tree_tag,
 tree_order_statistics_node_update>
ordered_set;*/
int spf[MAXN];
int fac[MAXN];
void sieve()//sieve
{
    spf[1] = 1;
    for (int i=2; i<MAXN; i++)
        spf[i] = i;
    for (int i=4; i<MAXN; i+=2)
        spf[i] = 2;
    for (int i=3; i*i<MAXN; i++)
    {
        if (spf[i]==i)
        {
            for (int j=i*i; j<MAXN; j+=i)
                if (spf[j]==j)
                    spf[j]=i;
        }
    }
}//sieve ends
map<int,int> getfactor(int a)//factor prime
{
    map<int,int> m;
    while(a>1)
    {
        m[spf[a]]++;
        a/=spf[a];
    }
    return m;
}//prime factor ends
int power(int x,int y,int p)
{
    int res = 1;
    x = x%p;
    if (x == 0) return 0;
    while (y > 0)
    {
        if (y & 1)
            res = (res*x)% p;
        y = y>>1;
        x = (x*x)%p;
    }
    return res;
}
int gcd(int a, int b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}
int inverse(int a, int p)
{
    return power(a, p-2, p);
}
int ncr(int n, int r, int p)
{
    if(r>n)
        return 0;
    if (r==0)
        return 1;
    return (fac[n]*inverse(fac[r], p) % p *
            inverse(fac[n-r], p) % p) % p;
}
void solve()
{
    int n,k;
    cin>>n>>k;
    string s;
    cin>>s;
    int ans=0;
    for(int i=1;i<n;i++)
    {
        int l=i;
        int flag=0;
        for(int j=0;l<n;l++,j++)
        {
            if(s[l]!=s[j])
                flag++;
        }
        if(!flag)
            ans=l;
    }
    if(ans==0)
        ans=n-1;
    string t;
    for(int i=0;i<k;i++)
    {
        for(int j=0;j<ans;j++)
            t.pb(s[j]);
    }
    for(int i=ans;i<s.size();i++)
        t.pb(s[i]);
    cout<<t<<endl;
}
signed main()
{
    fast
    int test=1;
    //cin>>test;
    while(test--)
    {
        solve();
    }
}


//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx,avx2,fma")
//#pragma GCC optimization ("unroll-loops")
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <bitset>
#include <stack>
#include <deque>
#include <iterator>
#include <tuple>
#include <iomanip>
#include <valarray>
#include <limits>
#include <sstream>
#include <cassert>
#include <functional>
#include <numeric>
#define in insert
#define pb push_back
#define mp make_pair
#define int long long
#define fi first
#define double long double
#define se second
#define endl '\n'
#define pii pair<int,int>
#define pdd pair<double,double>
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);
#define mod 1000000007
#define MAXN  301
#define ll unsigned long long
using namespace std;
/*using namespace __gnu_pbds;
 typedef tree<int, null_type, less<int>, rb_tree_tag,
 tree_order_statistics_node_update>
ordered_set;*/
int spf[MAXN];
int fac[MAXN];
void sieve()//sieve
{
    spf[1] = 1;
    for (int i=2; i<MAXN; i++)
        spf[i] = i;
    for (int i=4; i<MAXN; i+=2)
        spf[i] = 2;
    for (int i=3; i*i<MAXN; i++)
    {
        if (spf[i]==i)
        {
            for (int j=i*i; j<MAXN; j+=i)
                if (spf[j]==j)
                    spf[j]=i;
        }
    }
}//sieve ends
map<int,int> getfactor(int a)//factor prime
{
    map<int,int> m;
    while(a>1)
    {
        m[spf[a]]++;
        a/=spf[a];
    }
    return m;
}//prime factor ends
int power(int x,int y,int p)
{
    int res = 1;
    x = x%p;
    if (x == 0) return 0;
    while (y > 0)
    {
        if (y & 1)
            res = (res*x)% p;
        y = y>>1;
        x = (x*x)%p;
    }
    return res;
}
int gcd(int a, int b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}
int inverse(int a, int p)
{
    return power(a, p-2, p);
}
int ncr(int n, int r, int p)
{
    if(r>n)
        return 0;
    if (r==0)
        return 1;
    return (fac[n]*inverse(fac[r], p) % p *
            inverse(fac[n-r], p) % p) % p;
}
void solve()
{
    int n,k;
    cin>>n>>k;
    string s;
    cin>>s;
    int ans=0;
    for(int i=1;i<n;i++)
    {
        int l=i;
        int flag=0;
        for(int j=0;l<n;l++,j++)
        {
            if(s[l]!=s[j])
            {
                flag++;
                break;
            }
        }
        if(!flag)
        {
            ans=i;
            break;
        }
    }
    if(ans==0)
        ans=n;
    string t;
    cout<<ans<<endl;
    for(int i=0;i<k;i++)
    {
        for(int j=0;j<ans;j++)
            t.pb(s[j]);
    }
    for(int i=ans;i<s.size();i++)
        t.pb(s[i]);
    cout<<t<<endl;
}
signed main()
{
    fast
    int test=1;
    //cin>>test;
    while(test--)
    {
        solve();
    }
}


//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx,avx2,fma")
//#pragma GCC optimization ("unroll-loops")
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <bitset>
#include <stack>
#include <deque>
#include <iterator>
#include <tuple>
#include <iomanip>
#include <valarray>
#include <limits>
#include <sstream>
#include <cassert>
#include <functional>
#include <numeric>
#define in insert
#define pb push_back
#define mp make_pair
#define int long long
#define fi first
#define double long double
#define se second
#define endl '\n'
#define pii pair<int,int>
#define pdd pair<double,double>
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);
#define mod 1000000007
#define MAXN  301
#define ll unsigned long long
using namespace std;
/*using namespace __gnu_pbds;
 typedef tree<int, null_type, less<int>, rb_tree_tag,
 tree_order_statistics_node_update>
ordered_set;*/
int spf[MAXN];
int fac[MAXN];
void sieve()//sieve
{
    spf[1] = 1;
    for (int i=2; i<MAXN; i++)
        spf[i] = i;
    for (int i=4; i<MAXN; i+=2)
        spf[i] = 2;
    for (int i=3; i*i<MAXN; i++)
    {
        if (spf[i]==i)
        {
            for (int j=i*i; j<MAXN; j+=i)
                if (spf[j]==j)
                    spf[j]=i;
        }
    }
}//sieve ends
map<int,int> getfactor(int a)//factor prime
{
    map<int,int> m;
    while(a>1)
    {
        m[spf[a]]++;
        a/=spf[a];
    }
    return m;
}//prime factor ends
int power(int x,int y,int p)
{
    int res = 1;
    x = x%p;
    if (x == 0) return 0;
    while (y > 0)
    {
        if (y & 1)
            res = (res*x)% p;
        y = y>>1;
        x = (x*x)%p;
    }
    return res;
}
int gcd(int a, int b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}
int inverse(int a, int p)
{
    return power(a, p-2, p);
}
int ncr(int n, int r, int p)
{
    if(r>n)
        return 0;
    if (r==0)
        return 1;
    return (fac[n]*inverse(fac[r], p) % p *
            inverse(fac[n-r], p) % p) % p;
}
void solve()
{
    int n,k;
    cin>>n>>k;
    string s;
    cin>>s;
    int ans=0;
    for(int i=1;i<n;i++)
    {
        int l=i;
        int flag=0;
        for(int j=0;l<n;l++,j++)
        {
            if(s[l]!=s[j])
                flag++;
        }
        if(!flag)
        {
            ans=l;
            break;
        }
    }
    if(ans==0)
        ans=n-1;
    string t;
    cout<<ans<<endl;
    for(int i=0;i<k;i++)
    {
        for(int j=0;j<ans;j++)
            t.pb(s[j]);
    }
    for(int i=ans;i<s.size();i++)
        t.pb(s[i]);
    cout<<t<<endl;
}
signed main()
{
    fast
    int test=1;
    //cin>>test;
    while(test--)
    {
        solve();
    }
}


//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx,avx2,fma")
//#pragma GCC optimization ("unroll-loops")
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <bitset>
#include <stack>
#include <deque>
#include <iterator>
#include <tuple>
#include <iomanip>
#include <valarray>
#include <limits>
#include <sstream>
#include <cassert>
#include <functional>
#include <numeric>
#define in insert
#define pb push_back
#define mp make_pair
#define int long long
#define fi first
#define double long double
#define se second
#define endl '\n'
#define pii pair<int,int>
#define pdd pair<double,double>
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);
#define mod 1000000007
#define MAXN  301
#define ll unsigned long long
using namespace std;
/*using namespace __gnu_pbds;
 typedef tree<int, null_type, less<int>, rb_tree_tag,
 tree_order_statistics_node_update>
ordered_set;*/
int spf[MAXN];
int fac[MAXN];
void sieve()//sieve
{
    spf[1] = 1;
    for (int i=2; i<MAXN; i++)
        spf[i] = i;
    for (int i=4; i<MAXN; i+=2)
        spf[i] = 2;
    for (int i=3; i*i<MAXN; i++)
    {
        if (spf[i]==i)
        {
            for (int j=i*i; j<MAXN; j+=i)
                if (spf[j]==j)
                    spf[j]=i;
        }
    }
}//sieve ends
map<int,int> getfactor(int a)//factor prime
{
    map<int,int> m;
    while(a>1)
    {
        m[spf[a]]++;
        a/=spf[a];
    }
    return m;
}//prime factor ends
int power(int x,int y,int p)
{
    int res = 1;
    x = x%p;
    if (x == 0) return 0;
    while (y > 0)
    {
        if (y & 1)
            res = (res*x)% p;
        y = y>>1;
        x = (x*x)%p;
    }
    return res;
}
int gcd(int a, int b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}
int inverse(int a, int p)
{
    return power(a, p-2, p);
}
int ncr(int n, int r, int p)
{
    if(r>n)
        return 0;
    if (r==0)
        return 1;
    return (fac[n]*inverse(fac[r], p) % p *
            inverse(fac[n-r], p) % p) % p;
}
void solve()
{
    int n,k;
    cin>>n>>k;
    string s;
    int ans=0;
    for(int i=1;i<n;i++)
    {
        int l=i;
        int flag=0;
        for(int j=0;l<n;l++,j++)
        {
            if(s[l]!=s[j])
                flag++;
        }
        if(!flag)
            ans=l;
    }
    if(ans==0)
        ans=n-1;
    string t;
    for(int i=0;i<k;i++)
    {
        for(int j=0;j<ans;j++)
            t.pb(s[j]);
    }
    for(int i=ans;i<s.size();i++)
        t.pb(s[i]);
    cout<<t<<endl;
}
signed main()
{
    fast
    int test=1;
    //cin>>test;
    while(test--)
    {
        solve();
    }
}


#include<bits/stdc++.h>
using namespace std;
int main(){
    long long t,a,b,k,i,r=0,q=0,x=0;
    cin >> t;
    for(i=1;i<=t;i++){
        cin >> a>> b >>k;
        r=k%2;
        q=k/2;
        if(k==0){
          x=q*a+q*b;
          cout<<x;
        }
        else{
          x=q*a+q*b;
          x=x+a;
          cout<<x;
        }
        cout <<" ";
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
    long long t,a,b,k,i,r=0,q=0,x=0;
    cin >> t;
    for(i=1;i<=t;i++){
        cin >> a>> b >>k;
        r=k%2;
        q=k/2;
        if(k==0){
          x=q*a+q*b;
          cout<<x;
        }
        else{
          x=q*a+q*b;
          x=x+a;
          cout<<x;
        }
    }
    return0;
}


#include<bits/stdc++.h>
using namespace std;
long a[1005],b[1005],k[1005];

int main(){
    int runs;
    cin>>runs;
    for(int i = 0; i < runs; i++){
        cin>>a[i]>>b[i]>>k[i];
    }
    for(int i = 0; i < runs; i++){
        if(k[i]%2 == 0){
            if(a[i] == b[i]){
                cout<<"0"<<endl;
            }
            else{
                cout<<(a[i]-b[i])*k[i]/2<<endl;
            }
        }
        else{
            if(a[i] == b[i]){
                cout<<a[i]<<endl;
            }
            else{
                cout<<(a[i]-b[i])*(k[i]-1)/2+a[i]<<endl;
            }
        }
    }
}


#include<bits/stdc++.h>
using namespace std;
long a[1005],b[1005],k[1005];

int main(){
    int runs;
    cin>>runs;
    for(int i = 0; i < runs; i++){
        cin>>a[i]>>b[i]>>k[i];
    }
    for(int i = 0; i < runs; i++){
        if(k[i]%2 == 0){
            if(a[i] == b[i]){
                cout<<"0"<<endl;
            }
            else{
                cout<<(a[i]-b[i])*k[i]/2<<endl;
            }
        }
        else{
            if(a[i] == b[i]){
                cout<<a[i]<<endl;
            }
            else{
                cout<<a[i]*(k[i]+1)/2-b[i]*(k[i]-(k[i]+1)/2)<<endl;
            }
        }
    }
}


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

int main(){
    int runs;
    cin>>runs;
    while(runs--){
        long long a,b,k;
        cin >>a >>b >>k;
        long long ans = 0 ;
        if(k % 2 == 0){
            if(a == b) ans = 0 ;
            else ans = (a - b)*k/2 ;
        }
        else {
            if(a == b) ans = a ;
            else ans = (k / 2)*(a - b) + a;
        }
        cout<<ans<<endl;
    }
    return 0 ;
}


#include<bits/stdc++.h>
using namespace std;
const int manx = 1e5+5;

bool cmp(int a,int b){
    return a > b;
}

int sum[manx];
char a[manx];
int main(){
    int n,t,op;
    int ma = 0;
    int number = 0;
    int mann = 0;
    cin>>n;
    memset(sum, 0, sizeof(sum));
    for(int i = 0; i < n; i++){
        cin>>a[i];
    }
    int j = 0;
    for(int i = 0; i < n; i++){
        if(a[i]=='G')
            sum[j]++;
        if(a[i]=='S')
            j++;
    }
    for(int i = 0; i <= j; i++){
        if(sum[i] != 0){
            number++;
            t = i;
        }
        if(mann < sum[i]){
            mann = sum[i];
            op = i;
        }
    }
    if(number == 0)
        cout<<"0"<<endl;
    else if(number == 1)
        cout<<sum[t]<<endl;
    else if (number == 2){
        if(sum[t-1] != 0){
            cout<<sum[t-1]+sum[t]<<endl;
        }
        else{
            cout<<sum[op]+1<<endl;
        }
    }
    else {
        for(int i = 0; i <= j; i++){
            if(ma < sum[i]+sum[i+1]){
                ma = sum[i]+sum[i+1];
            }
        }
        cout<<ma+1<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std;
long a[1005],b[1005],k[1005];

int main()
{
    int runs;
    while(cin>>runs)
    {
        for(int i = 0; i < runs; i++)
        {
            cin>>a[i]>>b[i]>>k[i];
        }
        for(int i = 0; i < runs; i++)
        {
            if(k[i]%2 == 0)
            {
                if(a[i] == b[i])
                {
                    cout<<"0"<<endl;
                }
                else
                {
                    cout<<(a[i]-b[i])*k[i]/2<<endl;
                }
            }
            else
            {
                if(a[i] == b[i])
                {
                    cout<<a[i]<<endl;
                }
                else
                {
                    cout<<(a[i]-b[i])*(k[i]-1)/2+a[i]<<endl;
                }
            }
        }
    }
}


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

int main(){
    int runs;
    cin>>runs;
    while(runs--){
        long a,b,k;
        cin>>a>>b>>k;
        if(k%2 == 0){
            if(a == b){
                cout<<"0"<<endl;
            }
            else{
                cout<<(a-b)*k/2<<endl;
            }
        }
        else{
            if(a == b){
                cout<<a<<endl;
            }
            else{
                cout<<a*(k+1)/2-b*(k-(k+1)/2)<<endl;
            }
        }
    }
}


#include<bits/stdc++.h>
using namespace std;
int main(){
    long long t;cin>>t;
    for(long long z = 0;z < t;z++){
        long long a,b,k,ans = 0;
        cin>>a>>b>>k;
        for(long long i = 1;i <= k;i++){
            if(i % 2 != 0)
                ans += a;
            else
                ans -= b;
        }
        cout<<ans<<endl;

    }
}


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

int main(){
    long t;
    cin>>t;
    for(long i = 0;i < t;i++){
        long a,b,k;
        cin>>a>>b>>k;

        long pos = (a - b) * (k / 2) + a * (k % 2);
        cout<<pos<<endl;
    }

}


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

int main(){
    long long t;
    cin>>t;
    for(long long i = 0;i < t;i++){
        long long a,b,k;
        cin>>a>>b>>k;

        long long pos = (a - b) * (k / 2) + a * (k % 2);
        cout<<pos<<endl;
    }

}


#include<bits/stdc++.h>
using namespace std;
int main(){
    long t;cin>>t;
    for(long z = 0;z < t;z++){
        long a,b,k,ans = 0;
        cin>>a>>b>>k;
        for(long i = 1;i <= k;i++){
            if(i % 2 != 0)
                ans += a;
            else
                ans -= b;
        }
        cout<<ans<<endl;

    }
}


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

int main(){
    long t;
    cin>>t;
    for(long i = 0;i < t;i++){
        long a,b,k;
        cin>>a>>b>>k;

        long pos = 0;

        while(k > 0){
            if(k % 2 != 0){
                pos += a;
                k--;
            }
            else{
                pos -= b;
                k--;
            }
        }
        cout<<pos<<endl;
    }

}


#include <iostream>
#include <cmath>

using namespace std;

int main()
{
    int t;
	cin >> t;
	for (int i = 0; i < t; ++i) {
		int a, b, k;
		cin >> a >> b >> k;
		cout << (a - b) * 1ll * (k / 2) + a * (k & 1) << endl;
	}

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main() {
  int q;
  double a, b, k, s;
  cin >> q;
  while(q--) {
    s = 0;
    cin >> a >> b >> k;
    for(int i = 0; i < k; i++) {
      if(i % 2 == 0) {
        s += a;
      } else
        s -= b;
    }
    cout << fixed << s << endl;
  }
  return 0;
}


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

int main() {
  int q;
  long long a, b, k, s;
  cin >> q;
  while(q--) {
    cin >> a >> b >> k;
    if(k % 2 == 0) {
      cout << fixed << k / 2 * (a - b) << endl;
    } else {
      cout << fixed << (long(ceil((double)k / 2)) * a) - ((k - long(ceil((double)k / 2))) * b) << endl;
    }
  }
  return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main() {
  int q;
  long a, b, k, s;
  cin >> q;
  while(q--) {
    cin >> a >> b >> k;
    if(k % 2 == 0) {
      cout << fixed << long(k / 2 * (a - b)) << endl;
    } else {
      cout << fixed << long((ceil((double)k / 2) * a) - ((k - ceil((double)k / 2)) * b)) << endl;
    }
  }
  return 0;
}


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

int main() {
  int q;
  long long a, b, k, s;
  cin >> q;
  while(q--) {
    cin >> a >> b >> k;
    if(k % 2 == 0) {
      cout << fixed << k / 2 * (a - b) << endl;
    } else {
      cout << fixed << long((ceil((double)k / 2) * a) - ((k - ceil((double)k / 2)) * b)) << endl;
    }
  }
  return 0;
}


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

int main() {
  int q;
  long long a, b, k, s;
  cin >> q;
  while(q--) {
    cin >> a >> b >> k;
    if(k % 2 == 0) {
      cout << fixed << k / 2 * (a - b) << endl;
    } else {
      cout << fixed << long(ceil((double)k / 2)) * a) - ((k - long(ceil((double)k / 2))) * b << endl;
    }
  }
  return 0;
}


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

int main() {
  int q;
  long long a, b, k, s;
  cin >> q;
  while(q--) {
    cin >> a >> b >> k;
    if(k % 2 == 0) {
      cout << fixed << long(k / 2 * (a - b)) << endl;
    } else {
      cout << fixed << long((ceil((double)k / 2) * a) - ((k - ceil((double)k / 2)) * b)) << endl;
    }
  }
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define long long ll
int main(){
    int tt;cin>>tt;ll a,b,k,ans;
    while(tt--){
        cin>>a>>b>>k;ans=0;
        if(k%2==0){
            int right=k/2;
            int left=k%2;
            ans+=(a*right)-(b*left);
        }
        else{
            int right=k-(k%2);
            int left=k-2;
            ans+=(a*right)-(b*left);

        }
        cout<<ans<<endl;
    }

}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main(){
    int tt;cin>>tt;ll a,b,k,ans;
    while(tt--){
        cin>>a>>b>>k;ans=0;
        if(k%2==0){
           int right=k/2;
           int left=k-right;
            ans+=(a*right)-(b*left);
        }
        else{
           int right=(k/2+1);
           int left=k-right;
            ans+=(a*right)-(b*left);

        }
        cout<<ans<<endl;
    }

}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main(){
    int tt;cin>>tt;ll a,b,k,ans;
    while(tt--){
        cin>>a>>b>>k;ans=0;
        if(k%2==0){
            int right=k/2;
            int left=k-(k/2);
            ans+=(a*right)-(b*left);
        }
        else{
            int right=k-(k%2);
            int left=k-2;
            ans+=(a*right)-(b*left);

        }
        cout<<ans<<endl;
    }

}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main(){
    int tt;cin>>tt;ll a,b,k,ans;
    while(tt--){
        cin>>a>>b>>k;ans=0;
        if(k%2==0){
            int right=k/2;
            int left=k%2;
            ans+=(a*right)-(b*left);
        }
        else{
            int right=k-(k%2);
            int left=k-2;
            ans+=(a*right)-(b*left);

        }
        cout<<ans<<endl;
    }

}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main(){
    int tt;cin>>tt;ll a,b,k,ans;
    while(tt--){
        cin>>a>>b>>k;ans=0;
        if(k%2==0){
            int right=k/2;
            int left=k-(k%2);
            ans+=(a*right)-(b*left);
        }
        else{
            int right=k-(k%2);
            int left=k-2;
            ans+=(a*right)-(b*left);

        }
        cout<<ans<<endl;
    }

}


#include <iostream>

using namespace std;
void init(){
    cin.tie(0);
    cin.sync_with_stdio(0);
}

int main()
{
    init();
    long long  t,r,l,k;
    long long  ans=0;
    cin>>t;
    while(t)
    {
        cin>>r>>l>>k;
        if(k%2)
        {
          ans=(r-l)*(k/2);
          ans+=r;
        }
        else
        {
           ans=(r-1)*(k/2);
        }

           cout <<ans<< endl;
           ans=0;
           t--;
    }

    return 0;
}


#include <iostream>

using namespace std;
void init(){
    cin.tie(0);
    cin.sync_with_stdio(0);
}

int main()
{
    init();
    long long  t,r,l,k;
    double long  ans=0;
    cin>>t;
    while(t)
    {
        cin>>r>>l>>k;
        if(k%2)
        {
          ans=(r-l)*(k/2);
          ans+=r;
        }
        else
        {
           ans=(r-1)*(k/2);
        }

           cout <<ans<< endl;
           ans=0;
           t--;
    }

    return 0;
}


#include <iostream>

using namespace std;
void init(){
    cin.tie(0);
    cin.sync_with_stdio(0);
}

int main()
{
    init();
    long long  t,r,l,k,ans=0;
    cin>>t;
    while(t)
    {
        cin>>r>>l>>k;
        if(k%2)
        {
          ans=(r-l)*(k/2);
          ans+=r;
        }
        else
        {
           ans=(r-1)*(k/2);
        }

           cout <<ans<< endl;
           ans=0;
           t--;
    }

    return 0;
}


#include <iostream>

using namespace std;
void init(){
    cin.tie(0);
    cin.sync_with_stdio(0);
}

int main()
{
    init();
    long long  t,r,l,k;
    long long  ans=0;
    cin>>t;
    while(t)
    {
        cin>>r>>l>>k;
        if(k%2)
        {
          ans=r+((r-l)*(k/2));
        }
        else
        {
           ans=(r-1)*(k/2);
        }

           cout <<ans<< endl;
           ans=0;
           t--;
    }

    return 0;
}


#include <iostream>

using namespace std;
void init(){
    cin.tie(0);
    cin.sync_with_stdio(0);
}

int main()
{
    init();
    long   t,r,l,k;
    long long  ans=0;
    cin>>t;
    while(t)
    {
        cin>>r>>l>>k;
        if(k%2)
        {
          ans=(r-l)*(k/2);
          ans+=r;
        }
        else
        {
           ans=(r-1)*(k/2);
        }

           cout <<ans<< endl;
           ans=0;
           t--;
    }

    return 0;
}


#include <iostream>

using namespace std;
void init(){
    cin.tie(0);
    cin.sync_with_stdio(0);
}

int main()
{
    init();
    long long  t,r,l,k;
    long long  ans=0;
    cin>>t;
    while(t)
    {
        cin>>r>>l>>k;
        if(k%2)
        {
          ans=r+((r-l)*(k/2));
        }
        else
        {
           ans=(r-l)*(k/2);
        }

           cout <<ans<< endl;
           ans=0;
           t--;
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		long long int a,b,k;
		cin>>a>>b>>k;
		if(a==b && k%2==0)
		cout<<"0"<<endl;
		else if(a==b && k%2 != 0)
		cout<<"1"<<endl;
		else
		{
			if(k%2==0)
			{
			    a=a*k/2;
			    b=b*k/2;
			    cout<<a-b<<endl;
			}
			else
			{
			    a=a*k/2 + 1;
			    b=b*k/2;
			    cout<<a-b<<endl;
			}
		}
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		long int a,b,k;
		cin>>a>>b>>k;
		if(a==b && k%2==0)
		cout<<"0"<<endl;
		else if(a==b && k%2 != 0)
		cout<<"1"<<endl;
		else
		{
			long int d=0,count=0;
			if(k%2==0)
			{
			    a=a*k/2;
			    b=b*k/2;
			    cout<<a-b<<endl;
			}
			else
			{
			    a=a*k/2 + 1;
			    b=b*k/2;
			    cout<<a-b<<endl;
			}
		}
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		long int a,b,k;
		cin>>a>>b>>k;
		if(a==b && k%2==0)
		cout<<"0"<<endl;
		else if(a==b && k%2 != 0)
		cout<<"1"<<endl;
		else
		{
			long int d=0,count=0;
			if(k%2==0)
			{
			    a=a*k/2;
			    b=b*k/2;
			    cout<<a-b<<endl;
			}
			else
			{
			    a=a*(k/2 + 1);
			    b=b*(k/2);
			    cout<<a-b<<endl;
			}
		}
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		long long int a,b,k;
		cin>>a>>b>>k;
		if(a==b && k%2==0)
		cout<<"0"<<endl;
		else if(a==b && k%2 != 0)
		cout<<"1"<<endl;
		else
		{
			long long int d=0,count=0;
			while(count < k)
			{
				if(count%2==0)
				d+=a;
				else
				d-=b;

				count++;
			}
			cout<<d<<endl;
		}
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		long int a,b,k;
		cin>>a>>b>>k;
		if(a==b && k%2==0)
		cout<<"0"<<endl;
		else if(a==b && k%2 != 0)
		cout<<"1"<<endl;
		else
		{
			long int d=0,count=0;
			while(count < k)
			{
				if(count%2==0)
				d+=a;
				else
				d-=b;

				count++;
			}
			cout<<d<<endl;
		}
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
  int tc;
  cin>>tc;
    while(tc--)
    {
       long long int a,b,k;
        cin>>a>>b>>k;
          if(a==b)
          {
              if(k%2==0)
              cout<<"0"<<endl;
              else
              cout<<a<<endl;
          }
         else
         {
             if(k%2==0)
             {
                 a=a*k/2;
                 b=b*k/2;
                 cout<<a-b<<endl;
             }
             else
             {
                 a=a*(k/2 +1);
                 b=b*(k/2);
                 cout<<a-b<<endl;
             }
         }
    }
}


#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
void solve() {
    int t;
    cin >> t;
    while(t--) {
        ll a, b, k;
        cin >> a >> b >> k;
        if(k%2 == 0)
            cout << a*(k/2) - b*(k/2) << endl;
        else
            cout << a*(k/2 + 1) - b*(k/2) << endl;
    }
    return ;
}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    solve();
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    cin>>t;
    int a[t],b[t],k[t];
    long long int x[t]={0};
    for(int i=0;i<t;i++)
    {
        cin>>a[i]>>b[i]>>k[i];
        int temp=0;
        if(k[i]%2!=0)
        x[i]+=(k[i]/2+1)*a[i]-(k[i]/2)*b[i];
        else
        x[i]+=(k[i]/2)*a[i]-(k[i]/2)*b[i];
    }
    for(int i=0;i<t;i++)
    {
        cout<<x[i]<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    cin>>t;
    int a[t],b[t],k[t],x[t]={0};
    for(int i=0;i<t;i++)
    {
        cin>>a[i]>>b[i]>>k[i];
        int temp=0;
        if(k[i]%2!=0)
        x[i]+=(k[i]/2+1)*a[i]-(k[i]/2)*b[i];
        else
        x[i]+=(k[i]/2)*a[i]-(k[i]/2)*b[i];
    }
    for(int i=0;i<t;i++)
    {
        cout<<x[i]<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    cin>>t;
    long int a[t],b[t],k[t],x[t]={0};
    for(int i=0;i<t;i++)
    {
        cin>>a[i]>>b[i]>>k[i];
        int temp=0;
        if(k[i]%2!=0)
        x[i]+=(k[i]/2+1)*a[i]-(k[i]/2)*b[i];
        else
        x[i]+=(k[i]/2)*a[i]-(k[i]/2)*b[i];
    }
    for(int i=0;i<t;i++)
    {
        cout<<x[i]<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    cin>>t;
    unsigned long int a[t],b[t],k[t];
    long long int x[t]={0};
    for(int i=0;i<t;i++)
    {
        cin>>a[i]>>b[i]>>k[i];
        int temp=0;
        if(k[i]%2!=0)
        x[i]+=(k[i]/2+1)*a[i]-(k[i]/2)*b[i];
        else
        x[i]+=(k[i]/2)*a[i]-(k[i]/2)*b[i];
    }
    for(int i=0;i<t;i++)
    {
        cout<<x[i]<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    cin>>t;
    int a[t],b[t],k[t],x[t]={0};
    for(int i=0;i<t;i++)
    {
        cin>>a[i]>>b[i]>>k[i];
        int temp=0;
        while(temp<k)
        {
            if(temp%2==0)
            x[i]+=a[i];
            else
            x[i]-=b[i];
            temp++;
        }
    }
    for(int i=0;i<t;i++)
    {
        cout<<x[i]<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    cin>>t;
    int a[t],b[t],k[t],x[t]={0};
    for(int i=0;i<t;i++)
    {
        cin>>a[i]>>b[i]>>k[i];
        int temp=0;
        x[i]+=(k[i]/2+1)*a[i]-(k[i]/2)*b[i];
    }
    for(int i=0;i<t;i++)
    {
        cout<<x[i]<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    cin>>t;
    long int a[t],b[t],k[t];
    long long int x[t]={0};
    for(int i=0;i<t;i++)
    {
        cin>>a[i]>>b[i]>>k[i];
        int temp=0;
        if(k[i]%2!=0)
        x[i]+=(k[i]/2+1)*a[i]-(k[i]/2)*b[i];
        else
        x[i]+=(k[i]/2)*a[i]-(k[i]/2)*b[i];
    }
    for(int i=0;i<t;i++)
    {
        cout<<x[i]<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    cin>>t;
    int a[t],b[t],k[t],x[t]={0};
    for(int i=0;i<t;i++)
    {
        cin>>a[i]>>b[i]>>k[i];
        int temp=0;
        while(temp<k[i])
        {
            if(temp%2==0)
            x[i]+=a[i];
            else
            x[i]-=b[i];
            temp++;
        }
    }
    for(int i=0;i<t;i++)
    {
        cout<<x[i]<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    cin>>t;
    long long int a[t],b[t],k[t];
    long long int x[t]={0};
    for(int i=0;i<t;i++)
    {
        cin>>a[i]>>b[i]>>k[i];
        int temp=0;
        if(k[i]%2!=0)
        x[i]+=(k[i]/2+1)*a[i]-(k[i]/2)*b[i];
        else
        x[i]+=(k[i]/2)*a[i]-(k[i]/2)*b[i];
    }
    for(int i=0;i<t;i++)
    {
        cout<<x[i]<<endl;
    }
    return 0;
}


//by vishwajeet
#include<iostream>
#include<cmath>
#include<cstring>
using namespace std;
int main()
{

long long t;
cin>>t;
long long a[t][3],s[t];
for(long long i=0;i<t;i++)
    {
    	for(int j=0;j<3;j++)
    	{
    		cin>>a[i][j];
		}
		long long n1=a[i][2]/2;
		if(a[i][2]%2==0)
		    {

		    	s[i]=n1*a[i][0]-n1*a[i][1];
			}
		else
		    {
		        s[i]=(n1+1)*a[i][0]-n1*a[i][1];
			}
	}

for(long long i=0;i<t;i++)
    {
    	cout<<s[i]<<'\n';
	}

return 0;
}


//by vishwajeet
#include<iostream>
#include<cmath>
#include<cstring>
using namespace std;
int main()
{
long long t;
cin>>t;
long long a[t][3];
for(int i=0;i<t;i++)
    {
    	for(int j=0;j<3;j++)
    	{
    		cin>>a[i][j];
		}
	}
for(int i=0;i<t;i++)
    {
    	long long sum=0;
    	for(int j=0;j<a[i][2];j++)
    	    {
    	    	if(j%2==0)
    	    	    sum=sum+a[i][0];
    	    	else
    	    	    sum=sum-a[i][1];
			}
			cout<<sum<<'\n';
	}
return 0;


//by vishwajeet
#include<iostream>
#include<cmath>
#include<cstring>
using namespace std;
int main()
{
long long t;
cin>>t;
long long a[t][3];
for(int i=0;i<t;i++)
    {
    	for(int j=0;j<3;j++)
    	{
    		cin>>a[i][j];
		}
	}
for(int i=0;i<t;i++)
    {
    	int sum=0;
    	for(int j=0;j<a[i][2];j++)
    	    {
    	    	if(j%2==0)
    	    	    sum=sum+a[i][0];
    	    	else
    	    	    sum=sum-a[i][1];
			}
			cout<<sum<<'\n';
	}
return 0;
}


//by vishwajeet
#include<iostream>
#include<cmath>
#include<cstring>
using namespace std;
int main()
{
long long t;
cin>>t;
long long a[t][3];
for(int i=0;i<t;i++)
    {
    	for(int j=0;j<3;j++)
    	{
    		cin>>a[i][j];
		}
	}
for(int i=0;i<t;i++)
    {
    	long long sum=0;
    	for(int j=0;j<a[i][2];j++)
    	    {
    	    	if(j%2==0)
    	    	    sum=sum+a[i][0];
    	    	else
    	    	    sum=sum-a[i][1];
			}
			cout<<sum<<'\n';
	}
return 0;
}


//by vishwajeet
#include<iostream>
#include<cmath>
#include<cstring>
using namespace std;
int main()
{
int t;
cin>>t;
long long a[t][3];
for(int i=0;i<t;i++)
    {
    	for(int j=0;j<3;j++)
    	{
    		cin>>a[i][j];
		}
	}
for(int i=0;i<t;i++)
    {
    	long long sum=0;
    	for(long long j=0;j<a[i][2];j++)
    	    {
    	    	if(j%2==0)
    	    	    sum=sum+a[i][0];
    	    	else
    	    	    sum=sum-a[i][1];
			}
			cout<<sum<<'\n';
	}

return 0;
}


//by vishwajeet
#include<iostream>
#include<cmath>
#include<cstring>
using namespace std;
int main()
{
long long t;
cin>>t;
long long a[t][3];
for(long long i=0;i<t;i++)
    {
    	for(long long j=0;j<3;j++)
    	{
    		cin>>a[i][j];
		}
	}
for(long long i=0;i<t;i++)
    {
    	long long sum=0;
    	for(long long j=0;j<a[i][2];j++)
    	    {
    	    	if(j%2==0)
    	    	    sum=sum+a[i][0];
    	    	else
    	    	    sum=sum-a[i][1];
			}
			cout<<sum<<'\n';
	}

return 0;
}


#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<set>
using namespace std;
int main()
{
	int t;
	cin >> t;
	for (int i = 0; i < t; i++)
	{
		long long a, b, k;
		cin >> a >> b >> k;
		int half=0;
		int pos = 0;
		half = k / 2;
		if (k % 2 == 0)
		{

			pos += (long long) half * a;
			pos -= (long long) half * b;

		}
		else
		{
			pos += (long long) (half+ (long long) 1) * a;
			pos -= (long long) half * b;

		}
		//cout << (a - b) * 1ll * (k / 2) + a * (k & 1) << endl;

		cout << pos << endl;
	}

}


#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<set>
using namespace std;
int main()
{
	int t;
	cin >> t;
	for (int i = 0; i < t; i++)
	{
		int a, b, k;
		cin >> a >> b >> k;
		int half=0;
		unsigned long long pos = 0;
		half = k / 2;
		if (k % 2 == 0)
		{

			pos += half * a;
			pos -= half * b;

		}
		else
		{
			pos += (half+1) * a;
			pos -= half * b;

		}

		cout << pos << endl;
	}

}


#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<set>
using namespace std;
int main()
{
	int t;
	cin >> t;
	for (int i = 0; i < t; i++)
	{
		int a, b, k;
		cin >> a >> b >> k;
		int half=0;
		int pos = 0;
		half = k / 2;
		if (k % 2 == 0)
		{

			pos += half * a;
			pos -= half * b;

		}
		else
		{
			pos += (half+1) * a;
			pos -= half * b;

		}

		cout << pos << endl;
	}

}


#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<set>
using namespace std;
int main()
{
	int t;
	cin >> t;
	for (int i = 0; i < t; i++)
	{
		long long a, b, k;
		cin >> a >> b >> k;
		int half=0;
		int pos = 0;
		half = k / 2;
		if (k % 2 == 0)
		{

			pos += (long long) half * a;
			pos -= (long long) half * b;

		}
		else
		{
			pos += (long long) (half+1) * a;
			pos -= (long long) half * b;

		}
		//cout << (a - b) * 1ll * (k / 2) + a * (k & 1) << endl;

		cout << pos << endl;
	}

}


#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<set>
using namespace std;
int main()
{
	int t;
	cin >> t;
	for (int i = 0; i < t; i++)
	{
		int a, b, k;
		cin >> a >> b >> k;
		int half=0;
		unsigned pos = 0;
		half = k / 2;
		if (k % 2 == 0)
		{

			pos += half * a;
			pos -= half * b;

		}
		else
		{
			pos += (half+1) * a;
			pos -= half * b;

		}

		cout << pos << endl;
	}

}


#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<set>
using namespace std;
int main()
{
	int t;
	cin >> t;
	for (int i = 0; i < t; i++)
	{
		int a, b, k;
		cin >> a >> b >> k;
		int half=0;
		long long pos = 0;
		half = k / 2;
		if (k % 2 == 0)
		{

			pos += 1ll*half * a;
			pos -= 1ll*half * b;

		}
		else
		{
			pos += 1ll*(half+long long (1)) * a;
			pos -= 1ll*half * b;

		}
		//cout << (a - b) * 1ll * (k / 2) + a * (k & 1) << endl;

		cout << pos << endl;
	}

}


#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<set>
using namespace std;
int main()
{
	int t;
	cin >> t;
	for (int i = 0; i < t; i++)
	{
		int a, b, k;
		cin >> a >> b >> k;
		int half=0;
		long long pos = 0;
		half = k / 2;
		if (k % 2 == 0)
		{

			pos += 1ll*half * a;
			pos -= 1ll*half * b;

		}
		else
		{
			pos += 1ll*(half+1+1ll) * a;
			pos -= 1ll*half * b;

		}
		//cout << (a - b) * 1ll * (k / 2) + a * (k & 1) << endl;

		cout << pos << endl;
	}

}


#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<set>
using namespace std;
int main()
{
	int t;
	cin >> t;
	for (int i = 0; i < t; i++)
	{
		long long a, b, k;
		cin >> a >> b >> k;
		long long half=0;
		long long pos = 0;
		half = k / 2;
		if (k % 2 == 0)
		{

			pos += half * a;
			pos -= half * b;

		}
		else
		{
			pos += (half+1) * a;
			pos -= half * b;

		}
		//cout << (a - b) * 1ll * (k / 2) + a * (k & 1) << endl;

		cout << pos << endl;
	}

}


#include <bits/stdc++.h>

using namespace std;

#define speeds ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define watch(x) cout << (#x) << " is " << (x) << endl
#define pb push_back
#define mp make_pair
#define ll long long
#define  ull unsigned long long
#define pii pair <int, int>
#define sz(a) (int)a.size()
#define F first
#define S second
#define si set <int>
#define all(x) (x).begin(),(x).end()
ll gcd(ll a,ll b){if(b==0)return a;else return gcd(b,a%b);}
int lcm(int a,int b){return abs(a*b)/gcd(a,b);}

int main(){speeds;
    int t;
    cin >> t;
      ll a , b , sum = 0 , k;
    for(int i = 0 ; i < t; ++i){
        cin >> a >> b >> k;
    if( k % 2 == 0){
        cout << k / 2  * (a - b);
    }else{
        cout << ((k - 1) / 2 * (a - b)) - b ;
    }
        }

}


#include <bits/stdc++.h>

using namespace std;

#define speeds ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define watch(x) cout << (#x) << " is " << (x) << endl
#define pb push_back
#define mp make_pair
#define ll long long
#define  ull unsigned long long
#define pii pair <int, int>
#define sz(a) (int)a.size()
#define F first
#define S second
#define si set <int>
#define all(x) (x).begin(),(x).end()
ll gcd(ll a,ll b){if(b==0)return a;else return gcd(b,a%b);}
int lcm(int a,int b){return abs(a*b)/gcd(a,b);}

int main(){speeds;
    int t;
    cin >> t;
      ll a , b , sum = 0 , k;
    for(int i = 0 ; i < t; ++i){
        cin >> a >> b >> k;
        for(int j = 1 ; j <= k; ++j){
            if( j % 2 != 0 ){
                sum = sum + a;
            }else{
                sum = sum - b;
            }
        }
        cout << sum << endl;
        sum = 0;
    }
}


#include <bits/stdc++.h>

using namespace std;

#define speeds ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define watch(x) cout << (#x) << " is " << (x) << endl
#define pb push_back
#define mp make_pair
#define ll long long
#define  ull unsigned long long
#define pii pair <int, int>
#define sz(a) (int)a.size()
#define F first
#define S second
#define si set <int>
#define all(x) (x).begin(),(x).end()
ll gcd(ll a,ll b){if(b==0)return a;else return gcd(b,a%b);}
int lcm(int a,int b){return abs(a*b)/gcd(a,b);}

int main(){speeds;
    int t;
    cin >> t;
      ll a , b , sum = 0 , k;
    for(int i = 0 ; i < t; ++i){
        cin >> a >> b >> k;
    if( k % 2 == 0){
        cout << k  * (a - b);
    }else{
        cout << ((k - 1) * (a - b)) ;
    }
        }

}


#include <bits/stdc++.h>

using namespace std;

#define speeds ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define watch(x) cout << (#x) << " is " << (x) << endl
#define pb push_back
#define mp make_pair
#define ll long long
#define  ull unsigned long long
#define pii pair <int, int>
#define sz(a) (int)a.size()
#define F first
#define S second
#define si set <int>
#define all(x) (x).begin(),(x).end()
ll gcd(ll a,ll b){if(b==0)return a;else return gcd(b,a%b);}
int lcm(int a,int b){return abs(a*b)/gcd(a,b);}

int main(){speeds;
    int t;
    cin >> t;
    ll a , b , k;
    for(int i = 0 ; i < t; ++i){
    cin >> a >> b >> k;
    if( k % 2 == 0){
        cout << k / 2  * (a - b);
    }else{
        cout << ((k  / 2) * (a - b)) + a;
    }
    }

}


#include <bits/stdc++.h>

using namespace std;

#define speeds ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define watch(x) cout << (#x) << " is " << (x) << endl
#define pb push_back
#define mp make_pair
#define ll long long
#define  ull unsigned long long
#define pii pair <int, int>
#define sz(a) (int)a.size()
#define F first
#define S second
#define si set <int>
#define all(x) (x).begin(),(x).end()
ll gcd(ll a,ll b){if(b==0)return a;else return gcd(b,a%b);}
int lcm(int a,int b){return abs(a*b)/gcd(a,b);}

int main(){speeds;
    int t;
    cin >> t;
    ll a , b , k;
    for(int i = 0 ; i < t; ++i){
    cin >> a >> b >> k;
    if( k % 2 == 0){
        cout << k / 2  * (a - b);
    }else{
        cout << ((k - 1) / 2 * (a - b)) + a;
    }
    }

}


#include <bits/stdc++.h>

using namespace std;

#define speeds ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define watch(x) cout << (#x) << " is " << (x) << endl
#define pb push_back
#define mp make_pair
#define ll long long
#define  ull unsigned long long
#define pii pair <int, int>
#define sz(a) (int)a.size()
#define F first
#define S second
#define si set <int>
#define all(x) (x).begin(),(x).end()
ll gcd(ll a,ll b){if(b==0)return a;else return gcd(b,a%b);}
int lcm(int a,int b){return abs(a*b)/gcd(a,b);}

int main(){speeds;
    int t;
    cin >> t;
    ll a , b , k;
    for(int i = 0 ; i < t; ++i){
    cin >> a >> b >> k;
    if( k % 2 == 0){
        cout << k / 2  * (a - b) << endl;
    }else{
        cout << ((k  / 2) * (a - b)) + a << endl;
    }
    }

}


#include <iostream>
using namespace std;
int main(){
int n;
cin >> n;
for (int i=1; i<n+1;i++)
    {
        int a,b,k;
        cin >> a >> b >>k;
        cout << k*(a-b)/2 + (k%2)*a;

    }

}


#include <iostream>
using namespace std;
int main(){
int n;
cin >> n;
for (int i=1; i<n+1;i++)
    {
        int a,b,c;
        cin a >>b >>c;
        cout << k*(a-b)/2 + (k%2)*a;

    }

}


#include <iostream>
using namespace std;
int main(){
int n;
cin >> n;
for (int i=1; i<n+1;i++)
    {
        int a,b,k;
        cin a >>b >>k;
        cout << k*(a-b)/2 + (k%2)*a;

    }

}


#include <iostream>
using namespace std;
int main(){
int n;
cin >> n;
for (int i=1; i<n+1;i++)
    {
        int a,b,k;
        cin >> a >> b >>k;
        cout << k*(a-b)/2 + (k%2)*a<< '/n';

    }

}


#include <iostream>
using namespace std;
int main(){
long long n;
cin >> n;
for (long long i=1; i<n+1;i++)
    {
        long long a,b,k,f;
        cin >> a >> b >>k;
        f = k/2;
        cout << f*(a-b) + (k%2)*a<< endl ;

    }

}


#include <iostream>
using namespace std;
int main(){
int n;
cin >> n;
for (int i=1; i<n+1;i++)
    {
        int a,b,k;
        cin >> a >> b >>k;
        cout << (k/2)%(a-b) + (k%2)*a<< endl ;

    }

}


#include <iostream>
using namespace std;
int main(){
int n;
cin >> n;
for (int i=1; i<n+1;i++)
    {
        int a,b,k,f;
        cin >> a >> b >>k;
        f = k/2;
        cout << f*(a-b) + (k%2)*a<< endl ;

    }

}


#include <iostream>
using namespace std;
int main(){
int n;
cin >> n;
for (int i=1; i<n+1;i++)
    {
        int a,b,k;
        cin >> a >> b >>k;
        cout << k*(a-b)/2 + (k%2)*a<< endl ;

    }

}


#include <stdio.h>
#include <stdlib.h>
struct position{
		long long a,b,k,q=0;
	};
int cal(position *x)
{
	long long i;
	for(i=0;i<x->k;i++)
	{
		if(i%2==0)
		{
			x->q=x->q+x->a;
		}
		else
		{
			x->q=x->q-x->b;
		}
	}
}
int main()
{
	long long t,i;
	scanf("%lld",&t);
	position po[t];
	for(i=0;i<t;i++)
	{
		scanf("%lld %lld %lld",&po[i].a,&po[i].b,&po[i].k);
		cal(&po[i]);
	}
	for(i=0;i<t;i++)
	{
		printf("%lld\n",po[i].q);
	}
}


#include <stdio.h>
#include <stdlib.h>
struct position{
		long long a,b,k,q=0;
	};
int main()
{
	long long t,i,j;
	scanf("%lld",&t);
	position po[t];
	for(i=0;i<t;i++)
	{
		scanf("%lld %lld %lld",&po[i].a,&po[i].b,&po[i].k);
		if(po[i].a==po[i].b)
		{
			if(po[i].a==po[i].b)
			{
				po[i].q=po[i].a-(po[i].k+1)%2*po[i].b;
			}

			break;
		}
		for(j=0;j<po[i].k;j++)
	{
			po[i].q=po[i].q+(j+1)%2*po[i].a-j%2*po[i].b;
	}
	}
	for(i=0;i<t;i++)
	{
		printf("%lld\n",po[i].q);
	}
}


#include <stdio.h>
#include <stdlib.h>
struct position{
		long long a,b,k,q=0;
	};
int main()
{
	long long t,i,j;
	scanf("%lld",&t);
	position po[t];
	for(i=0;i<t;i++)
	{
		scanf("%lld %lld %lld",&po[i].a,&po[i].b,&po[i].k);
		if(po[i].a==po[i].b)
		{
				po[i].q=po[i].a-(po[i].k+1)%2*po[i].b;
		}
		else if(po[i].k%2==0)
		{
			po[i].q=po[i].k/2*(po[i].a-po[i].b);
		}
		else
		{
			po[i].q=po[i].k/2*(po[i].a-po[i].b)+po[i].a;
		}

	}
	for(i=0;i<t;i++)
	{
		printf("%lld\n",po[i].q);
	}
}


#include <stdio.h>
#include <stdlib.h>
struct position{
		long long a,b,k,q=0;
	};
int main()
{
	long long t,i,j;
	scanf("%lld",&t);
	position po[t];
	for(i=0;i<t;i++)
	{
		scanf("%lld %lld %lld",&po[i].a,&po[i].b,&po[i].k);
		if(po[i].a==po[i].b)
		{
			po[i].q=(po[i].k+1)%2*po[i].a-po[i].k%2*po[i].b;
			break;
		}
		for(j=0;j<po[i].k;j++)
	{
			po[i].q=po[i].q+(j+1)%2*po[i].a-j%2*po[i].b;
	}
	}
	for(i=0;i<t;i++)
	{
		printf("%lld\n",po[i].q);
	}
}


#include <stdio.h>
#include <stdlib.h>
struct position{
		long long a,b,k,q=0;
	};
int main()
{
	long long t,i,j;
	scanf("%lld",&t);
	position po[t];
	for(i=0;i<t;i++)
	{
		scanf("%lld %lld %lld",&po[i].a,&po[i].b,&po[i].k);
		if(po[i].a==po[i].b)
		{
				po[i].q=po[i].a-(po[i].k+1)%2*po[i].b;

			break;
		}
		else{
			for(j=0;j<po[i].k;j++)
	{
			po[i].q=po[i].q+(j+1)%2*po[i].a-j%2*po[i].b;
	}
		}

	}
	for(i=0;i<t;i++)
	{
		printf("%lld\n",po[i].q);
	}
}


#include <stdio.h>
#include <stdlib.h>
struct position{
		long long a,b,k,q=0;
	};
int main()
{
	long long t,i,j;
	scanf("%lld",&t);
	position po[t];
	for(i=0;i<t;i++)
	{
		scanf("%lld %lld %lld",&po[i].a,&po[i].b,&po[i].k);
		if(po[i].a==po[i].b)
		{
			po[i].q=(po[i].k%2+1)*po[i].a-po[i].k%2*po[i].b;
			break;
		}
		for(j=0;j<po[i].k;j++)
	{
			po[i].q=po[i].q+(j+1)%2*po[i].a-j%2*po[i].b;
	}
	}
	for(i=0;i<t;i++)
	{
		printf("%lld\n",po[i].q);
	}
}


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

const int inf=1e18;

int32_t main(){
    ios::sync_with_stdio(0),cin.tie(0);
    int n,m,k,tot=0;
    cin >> n >> m >> k;
    vector <pair <int,int> > v[4];
    for(int i=0;i<n;i++){
        int t,p,q;
        cin >> t >> p >> q;
        if(p and q)v[0].push_back({t,i+1});
        else if(p)v[1].push_back({t,i+1});
        else if(q)v[2].push_back({t,i+1});
        else v[3].push_back({t,i+1});
    }
    for(int i=0;i<4;i++)
        sort(v[i].begin(),v[i].end());
    int st=max(0ll,2*k-m),sz=min((int)v[1].size(),(int)v[2].size());
    st=max(st,k-sz);
    st=max(st,(int)v[0].size()+m-n);
    if((int)v[0].size()<st){
        cout << -1;
        return 0;
    }
    for(int i=0;i<st;i++){
        tot+=v[0][i].first;
    }
    int p[]={st,0,0,0},l=0;
    int a=st,b=st;
    while(b<k and l<sz){
        tot+=v[1][l].first+v[2][l].first;
        l++,a+=2,b++;
    }
    p[1]=p[2]=l;
    while(a<m){
        int mn=inf,id;
        for(int i=1;i<4;i++){
            if(p[i]==v[i].size())
                continue;
            if(v[i][p[i]].first<mn)mn=v[i][p[i]].first,id=i;
        }
        tot+=v[id][p[id]].first,p[id]++,a++;
    }
    int res=tot,bst[]={p[0],p[1],p[2],p[3]};
    if(a<m or b<k){
        cout << -1;
        return 0;
    }
    for(int i=st;i<min(m,(int)v[0].size());i++){
        tot+=v[0][i].first,p[0]++;
        l--;
        if(l<0){
            int mx=0,id;
            for(int i=1;i<4;i++){
                if(p[i]==0)
                    continue;
                if(v[i][p[i]-1].first>mx)mx=v[i][p[i]-1].first,id=i;
            }
            p[id]--;
            tot-=v[id][p[id]].first;
            continue;
        }
        p[1]--,p[2]--;
        tot-=v[1][p[1]].first;
        tot-=v[2][p[2]].first;
        int mn=inf,id;
        for(int i=1;i<4;i++){
            if(p[i]==v[i].size())
                continue;
            if(v[i][p[i]].first<mn)mn=v[i][p[i]].first,id=i;
        }
        tot+=v[id][p[id]].first,p[id]++;
        if(tot<res){
            res=tot;
            for(int j=0;j<4;j++)
                bst[j]=p[j];
        }
    }
    cout << res << '\n';
    for(int i=0;i<4;i++){
        for(int j=0;j<bst[i];j++)
            cout << v[i][j].second << ' ' ;
    }
}


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

const int inf=1e18;

int32_t main(){
    ios::sync_with_stdio(0),cin.tie(0);
    int n,m,k,tot=0;
    vector <int> ans;
    cin >> n >> m >> k;
    vector <pair <int,int> > w,x,y,z;
    for(int i=0;i<n;i++){
        int t,p,q;
        cin >> t >> p >> q;
        if(p and q)w.push_back({t,i+1});
        else if(p)x.push_back({t,i+1});
        else if(q)y.push_back({t,i+1});
        else z.push_back({t,i+1});
    }
    sort(w.begin(),w.end());
    sort(x.begin(),x.end());
    sort(y.begin(),y.end());
    int st=max(0ll,2*k-m),sz=min((int)x.size(),(int)y.size());
    st=max(st,k-sz);
    st=max(st,(int)w.size()+m-n);
    if((int)w.size()<st){
        cout << -1;
        return 0;
    }
    for(int i=0;i<st;i++){
        tot+=w[i].first;
        ans.push_back(w[i].second);
    }
    int pos=st,en=0,a=st,b=st,l=0;
    multiset <int> s;
    while(b<k and en<sz){
        tot+=x[en].first+y[en].first;
        en++,a+=2,b++;
    }
    for(int i=en;i<x.size();i++)
        z.push_back(x[i]);
    for(int i=en;i<y.size();i++)
        z.push_back(y[i]);
    sort(z.begin(),z.end());
    while(a<m and l<z.size()){
        tot+=z[l].first;
        s.insert(z[l].first);
        a++,l++;
    }
    int res=tot,tmp=en,pe=en;
    if(a<m or b<k){
        cout << -1;
        return 0;
    }
    for(int i=st;i<w.size();i++){
        en--;
        if(en<0)break;
        s.insert(x[en].first);
        s.insert(y[en].second);
        auto it=s.end();
        it--;
        tot-=*it;
        s.erase(it);
        it=s.end();
        it--;
        tot-=*it;
        s.erase(it);
        tot+=w[i].first;
        if(tot<res)res=tot,pos=i+1,pe=en;
    }
    en=pe;
    cout << res << '\n';
    for(int i=0;i<pos;i++)
        cout << w[i].second << ' ';
    for(int i=0;i<en;i++)
        cout << x[i].second << ' ' << y[i].second << ' ';
    for(int i=en;i<tmp;i++)
        z.push_back(x[i]),z.push_back(y[i]);
    sort(z.begin(),z.end());
    for(int i=0;i<m-pos-2*en;i++)
        cout << z[i].second << ' ';
}


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

const int inf=1e18;

int32_t main(){
    ios::sync_with_stdio(0),cin.tie(0);
    int n,m,k,tot=0;
    vector <int> ans;
    cin >> n >> m >> k;
    vector <pair <int,int> > w,x,y,z;
    for(int i=0;i<n;i++){
        int t,p,q;
        cin >> t >> p >> q;
        if(p and q)w.push_back({t,i+1});
        else if(p)x.push_back({t,i+1});
        else if(q)y.push_back({t,i+1});
        else z.push_back({t,i+1});
    }
    sort(w.begin(),w.end());
    sort(x.begin(),x.end());
    sort(y.begin(),y.end());
    if((int)w.size()<2*k-m){
        cout << -1;
        return 0;
    }
    for(int i=0;i<max(0ll,2*k-m);i++){
        tot+=w[i].first;
        ans.push_back(w[i].second);
    }
    int st=max(0ll,2*k-m),sz=min((int)x.size(),(int)y.size());
    int pos=st,en=0,a=st,b=st,l=0;
    multiset <int> s;
    while(b<k and en<sz){
        tot+=x[en].first+y[en].first;
        en++,a+=2,b++;
    }
    for(int i=en;i<x.size();i++)
        z.push_back(x[i]);
    for(int i=en;i<y.size();i++)
        z.push_back(y[i]);
    sort(z.begin(),z.end());
    while(a<m){
        tot+=z[l].first;
        s.insert(z[l].first);
        a++,l++;
    }
    int res=tot,tmp=en,pe=en;
    if(a<m or b<k){
        cout << -1;
        return 0;
    }
    for(int i=st;i<w.size();i++){
        tot+=w[i].first,en--;
        s.insert(x[en].first);
        s.insert(y[en].second);
        tot-=*s.rbegin();
        auto it=s.end();
        it--;
        s.erase(it);
        tot-=*s.rbegin();
        it=s.end();
        it--;
        s.erase(it);
        if(tot<res)res=tot,pos=i+1,pe=en;
    }
    en=pe;
    cout << res << '\n';
    for(int i=0;i<pos;i++)
        cout << w[i].second << ' ';
    for(int i=0;i<en;i++)
        cout << x[i].second << ' ' << y[i].second << ' ';
    for(int i=en;i<tmp;i++)
        z.push_back(x[i]),z.push_back(y[i]);
    sort(z.begin(),z.end());
    for(int i=0;i<m-pos-2*en;i++)
        cout << z[i].second << ' ';
}


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

const int inf=1e18;

int32_t main(){
    ios::sync_with_stdio(0),cin.tie(0);
    int n,m,k,tot=0;
    cin >> n >> m >> k;
    vector <pair <int,int> > v[4];
    for(int i=0;i<n;i++){
        int t,p,q;
        cin >> t >> p >> q;
        if(p and q)v[0].push_back({t,i+1});
        else if(p)v[1].push_back({t,i+1});
        else if(q)v[2].push_back({t,i+1});
        else v[3].push_back({t,i+1});
    }
    for(int i=0;i<4;i++)
        sort(v[i].begin(),v[i].end());
    int st=max(0ll,2*k-m),sz=min((int)v[1].size(),(int)v[2].size());
    st=max(st,k-sz);
    st=max(st,(int)v[0].size()+m-n);
    if((int)v[0].size()<st){
        cout << -1;
        return 0;
    }
    for(int i=0;i<st;i++){
        tot+=v[0][i].first;
    }
    int p[]={st,0,0,0},l=0;
    int a=st,b=st;
    while(b<k and l<sz){
        tot+=v[1][l].first+v[2][l].first;
        l++,a+=2,b++;
    }
    p[1]=p[2]=l;
    while(a<m){
        int mn=inf,id;
        for(int i=1;i<4;i++){
            if(p[i]==v[i].size())
                continue;
            if(v[i][p[i]].first<mn)mn=v[i][p[i]].first,id=i;
        }
        tot+=mn,p[id]++,a++;
    }
    int res=tot,bst[]={p[0],p[1],p[2],p[3]};
    if(a<m or b<k){
        cout << -1;
        return 0;
    }
    for(int i=st;i<min(m,(int)v[0].size());i++){
        tot+=v[0][i].first,p[0]++;
        l--;
        if(l<0){
            int mx=0,id;
            for(int i=1;i<4;i++){
                if(p[i]==0)
                    continue;
                if(v[i][p[i]-1].first>=mx)mx=v[i][p[i]-1].first,id=i;
            }
            p[id]--,tot-=mx;
            if(tot<res){
                res=tot;
                for(int j=0;j<4;j++)
                    bst[j]=p[j];
            }
            continue;
        }
        p[1]--,p[2]--;
        tot-=v[1][p[1]].first;
        tot-=v[2][p[2]].first;
        int mn=inf,id;
        for(int i=1;i<4;i++){
            if(p[i]==v[i].size())
                continue;
            if(v[i][p[i]].first<mn)mn=v[i][p[i]].first,id=i;
        }
        tot+=mn,p[id]++;
        if(tot<res){
            res=tot;
            for(int j=0;j<4;j++)
                bst[j]=p[j];
        }
    }
    cout << res << '\n';
    for(int i=0;i<4;i++){
        for(int j=0;j<bst[i];j++)
            cout << v[i][j].second << ' ' ;
    }
}


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

const int inf=1e18;

int32_t main(){
    ios::sync_with_stdio(0),cin.tie(0);
    int n,m,k,tot=0;
    cin >> n >> m >> k;
    vector <pair <int,int> > v[4];
    for(int i=0;i<n;i++){
        int t,p,q;
        cin >> t >> p >> q;
        if(p and q)v[0].push_back({t,i+1});
        else if(p)v[1].push_back({t,i+1});
        else if(q)v[2].push_back({t,i+1});
        else v[3].push_back({t,i+1});
    }
    for(int i=0;i<4;i++)
        sort(v[i].begin(),v[i].end());
    int st=max(0ll,2*k-m),sz=min((int)v[1].size(),(int)v[2].size());
    st=max(st,k-sz);
    st=max(st,(int)v[0].size()+m-n);
    if((int)v[0].size()<st){
        cout << -1;
        return 0;
    }
    for(int i=0;i<st;i++){
        tot+=v[0][i].first;
    }
    int p[]={st,0,0,0},l=0;
    int a=st,b=st;
    while(b<k and l<sz){
        tot+=v[1][l].first+v[2][l].first;
        l++,a+=2,b++;
    }
    p[1]=p[2]=l;
    while(a<m){
        int mn=inf,id;
        for(int i=1;i<4;i++){
            if(p[i]==v[i].size())
                continue;
            if(v[i][p[i]].first<mn)mn=v[i][p[i]].first,id=i;
        }
        tot+=mn,p[id]++,a++;
    }
    int res=tot,bst[]={p[0],p[1],p[2],p[3]};
    if(a<m or b<k){
        cout << -1;
        return 0;
    }
    assert(a==m and b==k);
    for(int i=st;i<min(m,(int)v[0].size());i++){
        tot+=v[0][i].first,p[0]++;
        l--;
        if(i>=k){
            int mx=0,id;
            for(int i=1;i<4;i++){
                if(p[i]==0)
                    continue;
                if(v[i][p[i]-1].first>=mx)mx=v[i][p[i]-1].first,id=i;
            }
            p[id]--,tot-=mx;
            if(tot<res){
                res=tot;
                for(int j=0;j<4;j++)
                    bst[j]=p[j];
            }
            continue;
        }
        p[1]--,p[2]--;
        tot-=v[1][p[1]].first;
        tot-=v[2][p[2]].first;
        int mn=inf,id;
        for(int i=1;i<4;i++){
            if(p[i]==v[i].size())
                continue;
            if(v[i][p[i]].first<mn)mn=v[i][p[i]].first,id=i;
        }
        tot+=mn,p[id]++;
        if(tot<res){
            res=tot;
            for(int j=0;j<4;j++)
                bst[j]=p[j];
        }
    }
    cout << res << '\n';
    for(int i=0;i<4;i++){
        for(int j=0;j<bst[i];j++)
            cout << v[i][j].second << ' ' ;
    }
}


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

const int inf=1e18;

int32_t main(){
    ios::sync_with_stdio(0),cin.tie(0);
    int n,m,k,tot=0;
    cin >> n >> m >> k;
    vector <pair <int,int> > v[4];
    for(int i=0;i<n;i++){
        int t,p,q;
        cin >> t >> p >> q;
        if(p and q)v[0].push_back({t,i+1});
        else if(p)v[1].push_back({t,i+1});
        else if(q)v[2].push_back({t,i+1});
        else v[3].push_back({t,i+1});
    }
    for(int i=0;i<4;i++)
        sort(v[i].begin(),v[i].end());
    int st=max(0ll,2*k-m),sz=min((int)v[1].size(),(int)v[2].size());
    st=max(st,k-sz);
    st=max(st,(int)v[0].size()+m-n);
    if((int)v[0].size()<st){
        cout << -1;
        return 0;
    }
    for(int i=0;i<st;i++){
        tot+=v[0][i].first;
    }
    int p[]={st,0,0,0},l=0;
    int a=st,b=st;
    while(b<k and l<sz){
        tot+=v[1][l].first+v[2][l].first;
        l++,a+=2,b++;
    }
    p[1]=p[2]=l;
    while(a<m){
        int mn=inf,id;
        for(int i=1;i<4;i++){
            if(p[i]==v[i].size())
                continue;
            if(v[i][p[i]].first<mn)mn=v[i][p[i]].first,id=i;
        }
        tot+=mn,p[id]++,a++;
    }
    int res=tot,bst[]={p[0],p[1],p[2],p[3]};
    if(a<m or b<k){
        cout << -1;
        return 0;
    }
    for(int i=st;i<min(m,(int)v[0].size());i++){
        tot+=v[0][i].first,p[0]++;
        l--;
        if(l<0){
            int mx=0,id;
            for(int i=1;i<4;i++){
                if(p[i]==0)
                    continue;
                if(v[i][p[i]-1].first>mx)mx=v[i][p[i]-1].first,id=i;
            }
            p[id]--,tot-=mx;
            if(tot<res){
                res=tot;
                for(int j=0;j<4;j++)
                    bst[j]=p[j];
            }
            continue;
        }
        p[1]--,p[2]--;
        tot-=v[1][p[1]].first;
        tot-=v[2][p[2]].first;
        int mn=inf,id;
        for(int i=1;i<4;i++){
            if(p[i]==v[i].size())
                continue;
            if(v[i][p[i]].first<mn)mn=v[i][p[i]].first,id=i;
        }
        tot+=mn,p[id]++;
        if(tot<res){
            res=tot;
            for(int j=0;j<4;j++)
                bst[j]=p[j];
        }
    }
    cout << res << '\n';
    for(int i=0;i<4;i++){
        for(int j=0;j<bst[i];j++)
            cout << v[i][j].second << ' ' ;
    }
}


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

const int inf=1e18;

int32_t main(){
    ios::sync_with_stdio(0),cin.tie(0);
    int n,m,k,tot=0;
    vector <int> ans;
    cin >> n >> m >> k;
    vector <pair <int,int> > w,x,y,z;
    for(int i=0;i<n;i++){
        int t,p,q;
        cin >> t >> p >> q;
        if(p and q)w.push_back({t,i+1});
        else if(p)x.push_back({t,i+1});
        else if(q)y.push_back({t,i+1});
        else z.push_back({t,i+1});
    }
    sort(w.begin(),w.end());
    sort(x.begin(),x.end());
    sort(y.begin(),y.end());
    int st=max(0ll,2*k-m),sz=min((int)x.size(),(int)y.size());
    st=max(st,k-sz);
    st=max(st,(int)w.size()+m-n);
    if((int)w.size()<st){
        cout << -1;
        return 0;
    }
    for(int i=0;i<st;i++){
        tot+=w[i].first;
        ans.push_back(w[i].second);
    }
    int pos=st,en=0,a=st,b=st,l=0;
    multiset <int> s;
    while(b<k and en<sz){
        tot+=x[en].first+y[en].first;
        en++,a+=2,b++;
    }
    for(int i=en;i<x.size();i++)
        z.push_back(x[i]);
    for(int i=en;i<y.size();i++)
        z.push_back(y[i]);
    sort(z.begin(),z.end());
    while(a<m and l<z.size()){
        tot+=z[l].first;
        s.insert(z[l].first);
        a++,l++;
    }
    int res=tot,tmp=en,pe=en;
    if(a<m or b<k){
        cout << -1;
        return 0;
    }
    for(int i=st;i<w.size();i++){
        tot+=w[i].first,en--;
        if(en<0)break;
        s.insert(x[en].first);
        s.insert(y[en].second);
        tot-=*s.rbegin();
        auto it=s.end();
        it--;
        s.erase(it);
        tot-=*s.rbegin();
        it=s.end();
        it--;
        s.erase(it);
        if(tot<res)res=tot,pos=i+1,pe=en;
    }
    en=pe;
    cout << res << '\n';
    for(int i=0;i<pos;i++)
        cout << w[i].second << ' ';
    for(int i=0;i<en;i++)
        cout << x[i].second << ' ' << y[i].second << ' ';
    for(int i=en;i<tmp;i++)
        z.push_back(x[i]),z.push_back(y[i]);
    sort(z.begin(),z.end());
    for(int i=0;i<m-pos-2*en;i++)
        cout << z[i].second << ' ';
}


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

const int inf=1e18;

int32_t main(){
    ios::sync_with_stdio(0),cin.tie(0);
    int n,m,k,tot=0;
    vector <int> ans;
    cin >> n >> m >> k;
    vector <pair <int,int> > w,x,y,z;
    for(int i=0;i<n;i++){
        int t,p,q;
        cin >> t >> p >> q;
        if(p and q)w.push_back({t,i+1});
        else if(p)x.push_back({t,i+1});
        else if(q)y.push_back({t,i+1});
        else z.push_back({t,i+1});
    }
    sort(w.begin(),w.end());
    sort(x.begin(),x.end());
    sort(y.begin(),y.end());
    int st=max(0ll,2*k-m),sz=min((int)x.size(),(int)y.size());
    st=max(st,k-sz);
    st=max(st,(int)w.size()+m-n);
    if((int)w.size()<st){
        cout << -1;
        return 0;
    }
    for(int i=0;i<st;i++){
        tot+=w[i].first;
        ans.push_back(w[i].second);
    }
    int pos=st,en=0,a=st,b=st,l=0;
    multiset <int> s;
    while(b<k and en<sz){
        tot+=x[en].first+y[en].first;
        en++,a+=2,b++;
    }
    for(int i=en;i<x.size();i++)
        z.push_back(x[i]);
    for(int i=en;i<y.size();i++)
        z.push_back(y[i]);
    sort(z.begin(),z.end());
    while(a<m and l<z.size()){
        tot+=z[l].first;
        s.insert(z[l].first);
        a++,l++;
    }
    int res=tot,tmp=en,pe=en;
    if(a<m or b<k){
        cout << -1;
        return 0;
    }
    for(int i=st;i<w.size();i++){
        en--;
        if(en<0)break;
        s.insert(x[en].first);
        s.insert(y[en].second);
        auto it=s.end();
        it--;
        tot-=*it;
        s.erase(it);
        it=s.end();
        it--;
        tot-=*it;
        s.erase(it);
        tot+=w[i].first;
        if(tot<0)break;
        if(tot<res)res=tot,pos=i+1,pe=en;
    }
    en=pe;
    cout << res << '\n';
    for(int i=0;i<pos;i++)
        cout << w[i].second << ' ';
    for(int i=0;i<en;i++)
        cout << x[i].second << ' ' << y[i].second << ' ';
    for(int i=en;i<tmp;i++)
        z.push_back(x[i]),z.push_back(y[i]);
    sort(z.begin(),z.end());
    for(int i=0;i<m-pos-2*en;i++)
        cout << z[i].second << ' ';
}


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

const int inf=1e18;

int32_t main(){
    ios::sync_with_stdio(0),cin.tie(0);
    int n,m,k,tot=0;
    cin >> n >> m >> k;
    vector <pair <int,int> > v[4];
    for(int i=0;i<n;i++){
        int t,p,q;
        cin >> t >> p >> q;
        if(p and q)v[0].push_back({t,i+1});
        else if(p)v[1].push_back({t,i+1});
        else if(q)v[2].push_back({t,i+1});
        else v[3].push_back({t,i+1});
    }
    for(int i=0;i<4;i++)
        sort(v[i].begin(),v[i].end());
    int st=max(0ll,2*k-m),sz=min((int)v[1].size(),(int)v[2].size());
    st=max(st,k-sz);
    st=max(st,(int)v[0].size()+m-n);
    if((int)v[0].size()<st){
        cout << -1;
        return 0;
    }
    for(int i=0;i<st;i++){
        tot+=v[0][i].first;
    }
    int p[]={st,0,0,0},l=0;
    int a=st,b=st;
    while(b<k and l<sz){
        tot+=v[1][l].first+v[2][l].first;
        l++,a+=2,b++;
    }
    p[1]=p[2]=l;
    while(a<m){
        int mn=inf,id;
        for(int i=1;i<4;i++){
            if(p[i]==(int)v[i].size())
                continue;
            if(v[i][p[i]].first<mn)mn=v[i][p[i]].first,id=i;
        }
        tot+=mn,p[id]++,a++;
        assert(mn<inf);
    }
    int res=tot,bst[]={p[0],p[1],p[2],p[3]};
    if(a<m or b<k){
        cout << -1;
        return 0;
    }
    for(int i=st;i<min(m,(int)v[0].size());i++){
        tot+=v[0][i].first,p[0]++;
        l--;
        if(l<0){
            int mx=0,id;
            for(int i=1;i<4;i++){
                if(p[i]==0)
                    continue;
                if(v[i][p[i]-1].first>=mx)mx=v[i][p[i]-1].first,id=i;
            }
            p[id]--,tot-=mx;
            if(tot<res){
                res=tot;
                for(int j=0;j<4;j++)
                    bst[j]=p[j];
            }
            continue;
        }
        p[1]--,p[2]--;
        tot-=v[1][p[1]].first;
        tot-=v[2][p[2]].first;
        int mn=inf,id;
        for(int i=1;i<4;i++){
            if(p[i]==(int)v[i].size())
                continue;
            if(v[i][p[i]].first<mn)mn=v[i][p[i]].first,id=i;
        }
        tot+=mn,p[id]++;
        assert(mn<inf);
        if(tot<res){
            res=tot;
            for(int j=0;j<4;j++)
                bst[j]=p[j];
        }
    }
    cout << res << '\n';
    for(int i=0;i<4;i++){
        for(int j=0;j<bst[i];j++)
            cout << v[i][j].second << ' ' ;
    }
}


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

const int inf=1e18;

int32_t main(){
    ios::sync_with_stdio(0),cin.tie(0);
    int n,m,k,tot=0;
    cin >> n >> m >> k;
    vector <pair <int,int> > w,x,y,z;
    for(int i=0;i<n;i++){
        int t,p,q;
        cin >> t >> p >> q;
        if(p and q)w.push_back({t,i+1});
        else if(p)x.push_back({t,i+1});
        else if(q)y.push_back({t,i+1});
        else z.push_back({t,i+1});
    }
    sort(w.begin(),w.end());
    sort(x.begin(),x.end());
    sort(y.begin(),y.end());
    int st=max(0ll,2*k-m),sz=min((int)x.size(),(int)y.size());
    st=max(st,k-sz);
    st=max(st,(int)w.size()+m-n);
    if((int)w.size()<st){
        cout << -1;
        return 0;
    }
    for(int i=0;i<st;i++){
        tot+=w[i].first;
    }
    int pos=st,en=0,a=st,b=st,l=0;
    multiset <int> s;
    while(b<k and en<sz){
        tot+=x[en].first+y[en].first;
        en++,a+=2,b++;
    }
    for(int i=en;i<(int)x.size();i++)
        z.push_back(x[i]);
    for(int i=en;i<(int)y.size();i++)
        z.push_back(y[i]);
    sort(z.begin(),z.end());
    while(a<m and l<(int)z.size()){
        tot+=z[l].first;
        s.insert(z[l].first);
        a++,l++;
    }
    int res=tot,tmp=en,pe=en;
    if(a<m or b<k){
        cout << -1;
        return 0;
    }
    for(int i=st;i<(int)w.size();i++){
        en--;
        tot+=w[i].first;
        if(en<0)break;
        s.insert(x[en].first);
        s.insert(y[en].second);
        auto it=s.end();
        it--;
        tot-=*it;
        if(tot<0){
            cout << tot << ' ' << *it << ' ' << s.size() << '\n';
        }
        s.erase(it);
        it=s.end();
        it--;
        tot-=*it;
        if(tot<0){
            cout << tot << ' ' << *it << ' ' << s.size() << '\n';
        }
        s.erase(it);
        if(tot<res)res=tot,pos=i+1,pe=en;
    }
    en=pe;
    cout << res << '\n';
    for(int i=0;i<pos;i++)
        cout << w[i].second << ' ';
    for(int i=0;i<en;i++)
        cout << x[i].second << ' ' << y[i].second << ' ';
    for(int i=en;i<tmp;i++)
        z.push_back(x[i]),z.push_back(y[i]);
    sort(z.begin(),z.end());
    for(int i=0;i<m-pos-2*en;i++)
        cout << z[i].second << ' ';
}


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

const int inf=1e18;

int32_t main(){
    ios::sync_with_stdio(0),cin.tie(0);
    int n,m,k,tot=0;
    vector <int> ans;
    cin >> n >> m >> k;
    vector <pair <int,int> > w,x,y,z;
    for(int i=0;i<n;i++){
        int t,p,q;
        cin >> t >> p >> q;
        if(p and q)w.push_back({t,i+1});
        else if(p)x.push_back({t,i+1});
        else if(q)y.push_back({t,i+1});
        else z.push_back({t,i+1});
    }
    sort(w.begin(),w.end());
    sort(x.begin(),x.end());
    sort(y.begin(),y.end());
    int st=max(0ll,2*k-m),sz=min((int)x.size(),(int)y.size());
    st=max(st,k-sz);
    st=max(st,(int)w.size()+m-n);
    if((int)w.size()<st){
        cout << -1;
        return 0;
    }
    for(int i=0;i<st;i++){
        tot+=w[i].first;
        ans.push_back(w[i].second);
    }
    int pos=st,en=0,a=st,b=st,l=0;
    multiset <int> s;
    while(b<k and en<sz){
        tot+=x[en].first+y[en].first;
        en++,a+=2,b++;
    }
    for(int i=en;i<x.size();i++)
        z.push_back(x[i]);
    for(int i=en;i<y.size();i++)
        z.push_back(y[i]);
    sort(z.begin(),z.end());
    while(a<m and l<z.size()){
        tot+=z[l].first;
        s.insert(z[l].first);
        a++,l++;
    }
    int res=tot,tmp=en,pe=en;
    if(a<m or b<k){
        cout << -1;
        return 0;
    }
    for(int i=st;i<w.size();i++){
        en--;
        if(en<0)break;
        s.insert(x[en].first);
        s.insert(y[en].first);
        auto it=s.end();
        it--;
        tot-=*it;
        s.erase(it);
        it=s.end();
        it--;
        tot-=*it;
        s.erase(it);
        tot+=w[i].first;
        if(tot<res)res=tot,pos=i+1,pe=en;
    }
    en=pe;
    cout << res << '\n';
    for(int i=0;i<pos;i++)
        cout << w[i].second << ' ';
    for(int i=0;i<en;i++)
        cout << x[i].second << ' ' << y[i].second << ' ';
    for(int i=en;i<tmp;i++)
        z.push_back(x[i]),z.push_back(y[i]);
    sort(z.begin(),z.end());
    for(int i=0;i<m-pos-2*en;i++)
        cout << z[i].second << ' ';
}


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

const int inf=1e18;

int32_t main(){
    ios::sync_with_stdio(0),cin.tie(0);
    int n,m,k,tot=0;
    cin >> n >> m >> k;
    vector <pair <int,int> > v[4];
    for(int i=0;i<n;i++){
        int t,p,q;
        cin >> t >> p >> q;
        if(p and q)v[0].push_back({t,i+1});
        else if(p)v[1].push_back({t,i+1});
        else if(q)v[2].push_back({t,i+1});
        else v[3].push_back({t,i+1});
    }
    for(int i=0;i<4;i++)
        sort(v[i].begin(),v[i].end());
    int st=max(0ll,2*k-m),sz=min((int)v[1].size(),(int)v[2].size());
    st=max(st,k-sz);
    st=max(st,(int)v[0].size()+m-n);
    if((int)v[0].size()<st){
        cout << -1;
        return 0;
    }
    for(int i=0;i<st;i++){
        tot+=v[0][i].first;
    }
    int p[]={st,0,0,0},l=0;
    int a=st,b=st;
    while(b<k and l<sz){
        tot+=v[1][l].first+v[2][l].first;
        l++,a+=2,b++;
    }
    p[1]=p[2]=l;
    while(a<m){
        int mn=inf,id;
        for(int i=1;i<4;i++){
            if(p[i]==(int)v[i].size())
                continue;
            if(v[i][p[i]].first<mn)mn=v[i][p[i]].first,id=i;
        }
        tot+=mn,p[id]++,a++;
    }
    int res=tot,bst[]={p[0],p[1],p[2],p[3]};
    if(a<m or b<k){
        cout << -1;
        return 0;
    }
    for(int i=st;i<min(m,(int)v[0].size());i++){
        tot+=v[0][i].first,p[0]++;
        l--;
        if(l<0){
            int mx=0,id;
            for(int i=1;i<4;i++){
                if(p[i]==0)
                    continue;
                if(v[i][p[i]-1].first>=mx)mx=v[i][p[i]-1].first,id=i;
            }
            p[id]--,tot-=mx;
            if(tot<res){
                res=tot;
                for(int j=0;j<4;j++)
                    bst[j]=p[j];
            }
            continue;
        }
        p[1]--,p[2]--;
        tot-=v[1][p[1]].first;
        tot-=v[2][p[2]].first;
        int mn=inf,id;
        for(int i=1;i<4;i++){
            if(p[i]==(int)v[i].size())
                continue;
            if(v[i][p[i]].first<mn)mn=v[i][p[i]].first,id=i;
        }
        tot+=mn,p[id]++;
        if(tot<res){
            res=tot;
            for(int j=0;j<4;j++)
                bst[j]=p[j];
        }
    }
    cout << res << '\n';
    for(int i=0;i<4;i++){
        for(int j=0;j<bst[i];j++)
            cout << v[i][j].second << ' ' ;
    }
}


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

const int inf=1e18;

int32_t main(){
    ios::sync_with_stdio(0),cin.tie(0);
    int n,m,k,tot=0;
    cin >> n >> m >> k;
    vector <pair <int,int> > w,x,y,z;
    for(int i=0;i<n;i++){
        int t,p,q;
        cin >> t >> p >> q;
        if(p and q)w.push_back({t,i+1});
        else if(p)x.push_back({t,i+1});
        else if(q)y.push_back({t,i+1});
        else z.push_back({t,i+1});
    }
    sort(w.begin(),w.end());
    sort(x.begin(),x.end());
    sort(y.begin(),y.end());
    int st=max(0ll,2*k-m),sz=min((int)x.size(),(int)y.size());
    st=max(st,k-sz);
    st=max(st,(int)w.size()+m-n);
    if((int)w.size()<st){
        cout << -1;
        return 0;
    }
    for(int i=0;i<st;i++){
        tot+=w[i].first;
    }
    int pos=st,en=0,a=st,b=st,l=0;
    multiset <int> s;
    while(b<k and en<sz){
        tot+=x[en].first+y[en].first;
        en++,a+=2,b++;
    }
    for(int i=en;i<(int)x.size();i++)
        z.push_back(x[i]);
    for(int i=en;i<(int)y.size();i++)
        z.push_back(y[i]);
    sort(z.begin(),z.end());
    while(a<m and l<(int)z.size()){
        tot+=z[l].first;
        s.insert(z[l].first);
        a++,l++;
    }
    int res=tot,tmp=en,pe=en;
    if(a<m or b<k){
        cout << -1;
        return 0;
    }
    for(int i=st;i<(int)w.size();i++){
        en--;
        if(en<0)break;
        s.insert(x[en].first);
        s.insert(y[en].second);
        auto it=s.end();
        it--;
        tot-=*it;
        s.erase(it);
        it=s.end();
        it--;
        tot-=*it;
        s.erase(it);
        tot+=w[i].first;
        if(tot<res)res=tot,pos=i+1,pe=en;
    }
    en=pe;
    cout << res << '\n';
    for(int i=0;i<pos;i++)
        cout << w[i].second << ' ';
    for(int i=0;i<en;i++)
        cout << x[i].second << ' ' << y[i].second << ' ';
    for(int i=en;i<tmp;i++)
        z.push_back(x[i]),z.push_back(y[i]);
    sort(z.begin(),z.end());
    for(int i=0;i<m-pos-2*en;i++)
        cout << z[i].second << ' ';
}


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

const int inf=1e18;

int32_t main(){
    ios::sync_with_stdio(0),cin.tie(0);
    int n,m,k,tot=0;
    cin >> n >> m >> k;
    vector <pair <int,int> > w,x,y,z;
    for(int i=0;i<n;i++){
        int t,p,q;
        cin >> t >> p >> q;
        if(p and q)w.push_back({t,i+1});
        else if(p)x.push_back({t,i+1});
        else if(q)y.push_back({t,i+1});
        else z.push_back({t,i+1});
    }
    sort(w.begin(),w.end());
    sort(x.begin(),x.end());
    sort(y.begin(),y.end());
    int st=max(0ll,2*k-m),sz=min((int)x.size(),(int)y.size());
    st=max(st,k-sz);
    st=max(st,(int)w.size()+m-n);
    if((int)w.size()<st){
        cout << -1;
        return 0;
    }
    for(int i=0;i<st;i++){
        tot+=w[i].first;
    }
    int pos=st,en=0,a=st,b=st,l=0;
    multiset <int> s;
    while(b<k and en<sz){
        tot+=x[en].first+y[en].first;
        en++,a+=2,b++;
    }
    for(int i=en;i<(int)x.size();i++)
        z.push_back(x[i]);
    for(int i=en;i<(int)y.size();i++)
        z.push_back(y[i]);
    sort(z.begin(),z.end());
    while(a<m and l<(int)z.size()){
        tot+=z[l].first;
        s.insert(z[l].first);
        a++,l++;
    }
    int res=tot,tmp=en,pe=en;
    if(a<m or b<k){
        cout << -1;
        return 0;
    }
    for(int i=st;i<(int)w.size();i++){
        en--;
        if(en<0)break;
        s.insert(x[en].first);
        s.insert(y[en].second);
        auto it=s.end();
        it--;
        tot-=*it;
        s.erase(it);
        it=s.end();
        it--;
        tot-=*it;
        s.erase(it);
        tot+=w[i].first;
        if(tot<0){
            cout << i << ' ' << (int)w.size() << ' ' << (int)s.size() << w[i].first << '\n';
            break;
        }
        if(tot<res)res=tot,pos=i+1,pe=en;
    }
    en=pe;
    cout << res << '\n';
    for(int i=0;i<pos;i++)
        cout << w[i].second << ' ';
    for(int i=0;i<en;i++)
        cout << x[i].second << ' ' << y[i].second << ' ';
    for(int i=en;i<tmp;i++)
        z.push_back(x[i]),z.push_back(y[i]);
    sort(z.begin(),z.end());
    for(int i=0;i<m-pos-2*en;i++)
        cout << z[i].second << ' ';
}


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

const int N=1009;
int a[N],n;

int32_t main(){
    ios::sync_with_stdio(0),cin.tie(0);
    cin >> n;
    for(int i=0;i<n;i++)
        cin >> a[i];
    vector <pair <int,int> > ans;
    for(int i=0;i<n;i++){
        for(int j=i+1;j<n;j++){
            if(a[i]>a[j])ans.push_back({i,j});
        }
    }
    auto fn=[&](auto x,auto y){
        if(a[x.first]==a[y.first]){
            if(x.first==y.first)
                return x.second>y.second;
            return x.first<y.first;
        }
        return a[x.first]<a[y.first];
    };
    sort(ans.begin(),ans.end(),fn);
    cout << ans.size() << '\n';
    for(auto [x,y]:ans)
        cout << x+1 << ' ' << y+1 << '\n';
}


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

const int inf=1e18;

int32_t main(){
    ios::sync_with_stdio(0),cin.tie(0);
    int n,m,k,tot=0;
    vector <int> ans;
    cin >> n >> m >> k;
    vector <pair <int,int> > w,x,y,z;
    for(int i=0;i<n;i++){
        int t,p,q;
        cin >> t >> p >> q;
        if(p and q)w.push_back({t,i+1});
        else if(p)x.push_back({t,i+1});
        else if(q)y.push_back({t,i+1});
        else z.push_back({t,i+1});
    }
    sort(w.begin(),w.end());
    sort(x.begin(),x.end());
    sort(y.begin(),y.end());
    int st=max(0ll,2*k-m),sz=min((int)x.size(),(int)y.size());
    st=max(st,k-sz);
    if((int)w.size()<st){
        cout << -1;
        return 0;
    }
    for(int i=0;i<st;i++){
        tot+=w[i].first;
        ans.push_back(w[i].second);
    }
    int pos=st,en=0,a=st,b=st,l=0;
    multiset <int> s;
    while(b<k and en<sz){
        tot+=x[en].first+y[en].first;
        en++,a+=2,b++;
    }
    for(int i=en;i<x.size();i++)
        z.push_back(x[i]);
    for(int i=en;i<y.size();i++)
        z.push_back(y[i]);
    sort(z.begin(),z.end());
    assert(a+(int)z.size()==n);
    while(a<m and l<z.size()){
        tot+=z[l].first;
        s.insert(z[l].first);
        a++,l++;
    }
    int res=tot,tmp=en,pe=en;
    if(a<m or b<k){
        cout << -1;
        return 0;
    }
    for(int i=st;i<w.size();i++){
        tot+=w[i].first,en--;
        s.insert(x[en].first);
        s.insert(y[en].second);
        tot-=*s.rbegin();
        auto it=s.end();
        it--;
        s.erase(it);
        tot-=*s.rbegin();
        it=s.end();
        it--;
        s.erase(it);
        if(tot<res)res=tot,pos=i+1,pe=en;
    }
    en=pe;
    cout << res << '\n';
    for(int i=0;i<pos;i++)
        cout << w[i].second << ' ';
    for(int i=0;i<en;i++)
        cout << x[i].second << ' ' << y[i].second << ' ';
    for(int i=en;i<tmp;i++)
        z.push_back(x[i]),z.push_back(y[i]);
    sort(z.begin(),z.end());
    for(int i=0;i<m-pos-2*en;i++)
        cout << z[i].second << ' ';
}