#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#include <cmath>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <ctime>
#define pb push_back
#define ll long long
#define mp make_pair
#define f first
#define s second
#define pii pair < int, int >
#define ull unsigned long long
#define pll pair < ll, ll >
#define forit(it, s) for(__typeof(s.begin()) it = s.begin(); it != s.end(); it ++)
#define all(s) s.begin(), s.end()
#define sz(a) (int)a.size()

const int inf = (1ll << 30) - 1;
const int maxn = (int) 1e5 + 10;

using namespace std;
int n;
int a[111];
vector<int> p;

void go(int x){
	for(int i = 2; i * i <= x; i++){
		if(x % i == 0){
			p.pb(x);
			while(x % i == 0) x/=i;
		}
	}
	if(x > 1) p.pb(x);
}

map<int, int> dp;

int get(int mask){
	if(mask == 0) return 0;
	if(dp.count(mask)) return dp[mask];
	int cc = 0;
	while(mask >= (1<<cc)) cc++;

	int cnt = 0;
	set<int> S;
	for(int i = 0; i < 30; i++){
		int g = mask & ((1<<(i+1)) - 1);
		int d = mask >> (i + 1);
		if((d|g) != mask) S.insert(get(d|g));
	}
	while(S.count(cnt)) ++cnt;
	return dp[mask] = cnt;
}

int calc(int x){
	int mask = 0;
	for (int i = 0; i < n; ++i)
	{
		int cnt = 0;
		while(a[i] % x == 0){
			a[i] /= x;
			cnt++;
		}
		if(cnt){
			mask |= (1<<(cnt-1));
		}
	}
	return get(mask);
}

void solve(){
	cin >> n;
	for(int i = 0; i < n; i++)
		cin >> a[i];
	for(int i = 0; i < n; i++){
		go(a[i]);
	}
	for(int i = 0; i < 100; i++){
		//cout << i << " "<< get(i) << endl;
	}
	sort(all(p));
	int ans = 0;
	for(int i = 0; i < p.size(); i++){
		ans ^= calc(p[i]);
	}
	if(ans) printf("Mojtaba\n");
	else printf("Arpa\n");
}

int main () {
    //cout << "Hello world\n";
    int t=1;
    //scanf("%d", &t);
    for(int i=1; i <= t; i++){
      //printf("Case #%d\n", i);
      solve();
    }

    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#include <cmath>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <ctime>
#define pb push_back
#define ll long long
#define mp make_pair
#define f first
#define s second
#define pii pair < int, int >
#define ull unsigned long long
#define pll pair < ll, ll >
#define forit(it, s) for(__typeof(s.begin()) it = s.begin(); it != s.end(); it ++)
#define all(s) s.begin(), s.end()
#define sz(a) (int)a.size()

const int inf = (1ll << 30) - 1;
const int maxn = (int) 1e5 + 10;

using namespace std;
int n;
int a[111];
vector<int> p;

void go(int x){
	for(int i = 2; i * i <= x; i++){
		if(x % i == 0){
			p.pb(x);
			while(x % i == 0) x/=i;
		}
	}
	if(x > 1) p.pb(x);
}

map<int, int> dp;

int get(int mask){
	if(mask == 0) return 0;
	if(dp.count(mask)) return dp[mask];
	int cc = 0;
	while(mask >= (1<<cc)) cc++;
	set<int> S;
	for(int i = 0; i < 30; i++){
		int g = mask & ((1<<(i+1)) - 1);
		int d = mask >> (i + 1);
		if((d|g) != mask) S.insert(get(d|g));
	}
	int cnt = 0;
	while(S.count(cnt)) cnt++;
	return dp[mask] = cnt;
}

int calc(int x){
	int mask = 0;
	for (int i = 0; i < n; ++i)
	{
		int cnt = 0;
		while(a[i] % x == 0){
			a[i] /= x;
			cnt++;
		}
		if(cnt){
			mask |= (1<<(cnt-1));
		}
	}
	return get(mask);
}

void solve(){
	cin >> n;
	for(int i = 0; i < n; i++)
		cin >> a[i];
	for(int i = 0; i < n; i++){
		go(a[i]);
	}
	for(int i = 0; i < 100; i++){
		//cout << i << " "<< get(i) << endl;
	}
	sort(all(p));
	int ans = 0;
	for(int i = 0; i < p.size(); i++){
		ans ^= calc(p[i]);
	}
	if(!ans) printf("Mojtaba\n");
	else printf("Arpa\n");
}

int main () {
    //cout << "Hello world\n";
    int t=1;
    //scanf("%d", &t);
    for(int i=1; i <= t; i++){
      //printf("Case #%d\n", i);
      solve();
    }

    return 0;
}


#include <cstdio>
#include <map>
using namespace std;
int read()
{
    int x=0,flag=1;char c;
    while((c=getchar())<'0' || c>'9') if(c=='-') flag=-1;
    while(c>='0' && c<='9') x=(x<<3)+(x<<1)+(c^48),c=getchar();
    return x*flag;
}
map<int,int> st,sg;
void fuck(int x)
{
    for(int i=2;i*i<=x;i++)
    {
        int t=0;
        while(x%i==0)
        {
            t++;
            x/=i;
        }
        if(t) st[i]|=1<<(t-1);
    }
    if(x>1) st[x]|=1;
}
int get(int x)
{
    if(x==0) return 0;
    if(sg.count(x)) return sg[x];
    map<int,int> vis;
    int p=x,k=0;
    while(p) p>>=1,k++;
    for(int i=1;i<=k;i++)
        vis[get((x>>i)|(x&((1<<i-1)-1)))]=1;
    for(int i=0;;i++)
        if(!vis[i])
            return sg[x]=i;
}
int main()
{
    int n=read(),flag=0;
    for(int i=1;i<=n;i++)
        fuck(read());
    map<int,int> :: iterator it = st.begin();
    for(;it!=st.end();it++) flag^=get(it->second);
    if(flag) puts("Mojtaba");
    else puts("Arpa");
}


#include <cstdio>
#include <map>
using namespace std;
int read()
{
    int x=0,flag=1;char c;
    while((c=getchar())<'0' || c>'9') if(c=='-') flag=-1;
    while(c>='0' && c<='9') x=(x<<3)+(x<<1)+(c^48),c=getchar();
    return x*flag;
}
map<int,int> st,sg;
void fuck(int x)
{
    for(int i=2;i*i<=x;i++)
    {
        int t=0;
        while(x%i==0)
        {
            t++;
            x/=i;
        }
        if(t>1) st[i]|=1<<(t-1);
    }
    if(x>1) st[x]=1;
}
int get(int x)
{
    if(x==0) return 0;
    if(sg.count(x)) return sg[x];
    map<int,bool> vis;
    int p=x,k=0;
    while(p) p>>=1,k++;
    for(int i=1;i<=k;i++)
        vis[get((x>>i)|(x&((1<<i-1)-1)))]=1;
    for(int i=0;;i++)
        if(!vis[i])
            return sg[x]=i;
}
int main()
{
    int n=read(),flag=0;
    for(int i=1;i<=n;i++)
        fuck(read());
    map<int,int> :: iterator it = st.begin();
    for(;it!=st.end();it++) flag^=get(it->second);
    if(flag) puts("Mojtaba");
    else puts("Arpa");
}


#include <cstdio>
#include <map>
using namespace std;
int read()
{
    int x=0,flag=1;char c;
    while((c=getchar())<'0' || c>'9') if(c=='-') flag=-1;
    while(c>='0' && c<='9') x=(x<<3)+(x<<1)+(c^48),c=getchar();
    return x*flag;
}
map<int,int> st,sg;
void fuck(int x)
{
    for(int i=2;i*i<=x;i++)
    {
        int t=0;
        while(x%i==0)
        {
            t++;
            x/=i;
        }
        if(t) st[i]|=1<<(t-1);
    }
    if(x>1) st[x]=1;
}
int get(int x)
{
    if(x==0) return 0;
    if(sg.count(x)) return sg[x];
    map<int,bool> vis;
    int p=x,k=0;
    while(p) p>>=1,k++;
    for(int i=1;i<=k;i++)
        vis[get((x>>i)|(x&((1<<i-1)-1)))]=1;
    for(int i=0;;i++)
        if(!vis[i])
            return sg[x]=i;
}
int main()
{
    int n=read(),flag=0;
    for(int i=1;i<=n;i++)
        fuck(read());
    map<int,int> :: iterator it = st.begin();
    for(;it!=st.end();it++) flag^=get(it->second);
    if(flag) puts("Mojtaba");
    else puts("Arpa");
}


#include<bits/stdc++.h>
using namespace std;
int n;
map<int,int>id;
int pri[5050];
int tot;
int zt[5050];
void pia(int x)
{
	int c,p;
	for(int i=2;i*i<=x;++i)
		if(x%i==0)
		{
			c=0;
			while(x%i==0)c++,x/=i;
			p=id[i];
			if(p) zt[p]|=(1<<(c-1));
			else
			{
				id[i]=++tot;
				pri[tot]=i;
				zt[tot]|=(1<<(c-1));
			}
		}
	if(x==1)return;
	p=id[x];
	if(p)zt[p]|=1;
	else
	{
		id[x]=++tot;
		pri[tot]=x;
		zt[tot]|=1;
	}
}

map<int,int>sg;
map<int,bool>did;
int dfs(int cur)
{
	if(!cur)return 0;
	if(did[cur])return sg[cur];
	bool book[33];
	memset(book,0,sizeof(book));
	int nxt;
	for(int i=0;cur>>i;++i)
	{
		nxt=((cur>>(i+1))|(cur&((1<<i)-1)));
		book[dfs(nxt)]=1;
	}
	for(int i=0;;++i)
		if(!book[i])
		{
			sg[cur]=i;
			did[cur]=1;
			return i;
		}
}
int main()
{
	scanf("%d",&n);
	int xx;
	for(int i=1;i<=n;++i)
	{
		scanf("%d",&xx);
		pia(xx);
	}
	int ans=0;
	for(int i=1;i<=tot;++i)
		ans^=dfs(zt[i]);
	puts(ans?"Mojtaba":"Arpa");
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n;
map<int,int>id;
int pri[5050];
int tot;
int zt[5050];
void pia(int x)
{
	int c,p;
	for(int i=2;i*i<=x;++i)
		if(x%i==0)
		{
			c=0;
			while(x%i==0)c++,x/=i;
			p=id[i];
			if(p) zt[p]|=(1<<(c-1));
			else
			{
				id[i]=++tot;
				pri[tot]=i;
				zt[tot]|=(1<<(c-1));
			}
		}
	if(x==1)return;
	p=id[x];
	if(p)zt[p]|=1;
	else
	{
		id[x]=++tot;
		pri[tot]=x;
		zt[tot]|=1;
	}
}

map<int,int>sg;
int dfs(int cur)
{
	if(!cur)return 0;
	bool book[33];
	memset(book,0,sizeof(book));
	int nxt;
	for(int i=0;cur>>i;++i)
	{
		nxt=((cur>>(i+1))|(cur&((1<<i)-1)));
		book[dfs(nxt)]=1;
	}
	for(int i=0;;++i)
		if(!book[i])return i;
}
int main()
{
	scanf("%d",&n);
	int xx;
	for(int i=1;i<=n;++i)
	{
		scanf("%d",&xx);
		pia(xx);
	}
	int ans=0;
	for(int i=1;i<=tot;++i)
		ans^=dfs(zt[i]);
	puts(ans?"Mojtaba":"Arpa");
	return 0;
}


/*
    Zura Ja - nai
    KATSURA DA !!
*/

#include "bits/stdc++.h"

using namespace std;

#define F first
#define S second
#define mp(x, y) make_pair(x, y)
#define ios ios_base::sync_with_stdio(0);cin.tie(0);
#define pb push_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()

typedef long long ll;
typedef pair<int, int> ii;
typedef long double db;
typedef vector<int> vi;

const int N = 1e2 + 5;
const int mod = 1e9 + 7;
const db PI = acos(-1);

int a[N], n;

map<int, map<int, int> >ver;

void facto(int x){

    for(int i = 2; i * i <= x; i++)
        if(x%i == 0){
            int cnt = 0;
            while(x%i == 0)x/=i, cnt++;
            ver[i][cnt]++;
        }

    if(x > 1)
        ver[x][1]++;

}

void read(){

    scanf("%d", &n);

    for(int i = 0; i < n; scanf("%d", a + i++));

    for(int i = 0; i < n; i++)facto(a[i]);

}

string out[] = {"Arpa", "Mojtaba"};

map<int, int>dp;

int shift(int msk, int x){

    for(int i = x; i<= 30; i++){

        if(msk & (1 << i)){
            msk ^= (1 << i);
            msk |= (1 << (i - x));
        }

    }

    return msk;
}

int calc(int msk){

    if(dp.find(msk) != dp.end())
        return dp[msk];

    int &ret = dp[msk];

    ret = 0;

    int sv[33] = {};

    for(int i = 1; i <= 30; i++){

        if(msk >= (1 << (i))){
            sv[calc(shift(msk, i))]++;
        }

    }

    for(int i = 32; i>= 0; i--)
        if(!sv[i])
            ret = i;

    return ret;
}

string solve(){

    int ret = 0;

    for(auto v: ver){
        int msk  = 0;
        for(auto it : v.second)
            msk |= (1 << (it.first));

        ret ^= calc(msk);

    }

    return out[ret];

}

int main(){

    read();

    cout << solve() << '\n';

    return 0;
}


/*
    Zura Ja - nai
    KATSURA DA !!
*/

#include "bits/stdc++.h"

using namespace std;

#define F first
#define S second
#define mp(x, y) make_pair(x, y)
#define ios ios_base::sync_with_stdio(0);cin.tie(0);
#define pb push_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()

typedef long long ll;
typedef pair<int, int> ii;
typedef long double db;
typedef vector<int> vi;

const int N = 1e2 + 5;
const int mod = 1e9 + 7;
const db PI = acos(-1);

int a[N], n;

map<int, map<int, int> >ver;

void facto(int x){

    for(int i = 2; i * i <= x; i++)
        if(x%i == 0){
            int cnt = 0;
            while(x%i == 0)x/=i, cnt++;
            ver[i][cnt]++;
        }
    if(x > 1)
        ver[x][1]++;

}

void read(){

    scanf("%d", &n);

    for(int i = 0; i < n; scanf("%d", a + i++));

    for(int i = 0; i < n; i++)facto(a[i]);

}

string out[] = {"Arpa", "Mojtaba"};

map<int, int>dp;

int shift(int msk, int x){

    for(int i = x; i<= 30; i++){

        if(msk & (1 << i)){
            msk ^= (1 << i);
            msk |= (1 << (i - x));
        }

    }

    return msk;
}

int calc(int msk){

    if(dp.find(msk) != dp.end())
        return dp[msk];

    int &ret = dp[msk];

    ret = 0;

    for(int i = 1; i <= 30; i++){

        if(msk >= (1 << (i))){
            if(!calc(shift(msk, i))){
                ret = 1;
                break;
            }

        }

    }

    return ret;
}

string solve(){

    int ret = 0;

    for(auto v: ver){
        int msk  = 0;
        for(auto it : v.second)
            msk |= (1 << (it.first));

        ret ^= calc(msk);

    }

    return out[ret];

}

int main(){

    read();

    cout << solve() << '\n';

    return 0;
}


/*
    Zura Ja - nai
    KATSURA DA !!
*/

#include "bits/stdc++.h"

using namespace std;

#define F first
#define S second
#define mp(x, y) make_pair(x, y)
#define ios ios_base::sync_with_stdio(0);cin.tie(0);
#define pb push_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()

typedef long long ll;
typedef pair<int, int> ii;
typedef long double db;
typedef vector<int> vi;

const int N = 1e2 + 5;
const int mod = 1e9 + 7;
const db PI = acos(-1);

int a[N], n;

map<int, map<int, int> >ver;

void facto(int x){

    for(int i = 2; i * i <= x; i++)
        if(x%i == 0){
            int cnt = 0;
            while(x%i == 0)x/=i, cnt++;
            ver[i][cnt]++;
        }

    if(x > 1)
        ver[x][1]++;

}

void read(){

    scanf("%d", &n);

    for(int i = 0; i < n; scanf("%d", a + i++));

    for(int i = 0; i < n; i++)facto(a[i]);

}

string out[] = {"Arpa", "Mojtaba"};

map<int, int>dp;

int shift(int msk, int x){

    for(int i = x; i<= 30; i++){

        if(msk & (1 << i)){
            msk ^= (1 << i);
            msk |= (1 << (i - x));
        }

    }

    return msk;
}

int calc(int msk){

    if(dp.find(msk) != dp.end())
        return dp[msk];

    int &ret = dp[msk];

    ret = 0;

    int sv[33] = {};

    for(int i = 1; i <= 30; i++){

        if(msk >= (1 << (i))){
            sv[calc(shift(msk, i))]++;
        }

    }

    for(int i = 32; i>= 0; i--)
        if(!sv[i])
            ret = i;

    return ret;
}

string solve(){

    int ret = 0;

    for(auto v: ver){
        int msk  = 0;
        for(auto it : v.second)
            msk |= (1 << (it.first));

        ret ^= calc(msk);

    }

    ret = bool(ret);

    return out[ret];

}

int main(){

    read();

    cout << solve() << '\n';

    return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;

#define FOR(i, a, b) for (int i=a; i<(b); i++)
#define F0R(i, a) for (int i=0; i<(a); i++)
#define FORd(i,a,b) for (int i = (b)-1; i >= a; i--)
#define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--)

#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound

const int MOD = 1000000007;

map<int,vi> fac;
map<vi,int> num;
int n, ans = 0;

int get(vi x) {
    if (x.size() == 0) return num[x] = 0;
    if (num.count(x)) return num[x];
    int mx = x[x.size()-1];
    set<int> cur;
    FOR(i,1,mx+1) {
        vi x1;
        for (int j: x)
            if (mx <= j) {
                if (mx != j) x1.pb(j-mx);
            } else x1.pb(j);

        sort(x1.begin(),x1.end());
        cur.insert(get(x1));
    }
    for (int i = 0;;++i) if (cur.find(i) == cur.end()) return num[x] = i;
}

void process(int x) {
    for (int i = 2; i*i <= x; ++i) if (x % i == 0) {
        int f = 0;
        while (x % i == 0) x /= i, f++;
        fac[i].pb(f);
    }
    if (x != 1) fac[x].pb(1);
}

int main() {
	ios_base::sync_with_stdio(0);cin.tie(0);
	cin >> n;
	F0R(i,n) {
	    int a; cin >> a;
	    process(a);
	}
	for (auto a: fac) {
	    vi z = a.s; sort(z.begin(),z.end());
	    ans ^= get(z);
	}
	if (ans == 0) cout << "Arpa";
	else cout << "Mojitaba";
}

// read!
// ll vs. int!


#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;

#define FOR(i, a, b) for (int i=a; i<(b); i++)
#define F0R(i, a) for (int i=0; i<(a); i++)
#define FORd(i,a,b) for (int i = (b)-1; i >= a; i--)
#define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--)

#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound

const int MOD = 1000000007;

map<int,int> fac;
map<int,int> vals;
int n, ans = 0;

void process(int x) {
    for (int i = 2; i*i <= x; ++i) if (x % i == 0) {
        int f = 0;
        while (x % i == 0) x /= i, f++;
        fac[i] |= 1<<f;
    }
    if (x != 1) fac[x] |= (1<<1);
}

int get(int x) {
    if (x == 0) return 0;
    if (vals.count(x)) return vals[x];
    set<int> cur;
    FOR(i,1,30) if ((1<<i) <= x) {
        int x1 = (x%(1<<i))|(x>>i);
        if (x1&1) x1 ^= 1;
        cur.insert(get(x1));
    }
    for (int i = 0;;++i) if (cur.find(i) == cur.end()) return vals[x] = i;
}

int main() {
	ios_base::sync_with_stdio(0);cin.tie(0);
	cin >> n;
	F0R(i,n) {
	    int a; cin >> a;
	    process(a);
	}
	for (auto a: fac) ans ^= get(a.s);
	if (ans == 0) cout << "Arpa";
	else cout << "Mojtaba";
}

// read!
// ll vs. int!


#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;

#define FOR(i, a, b) for (int i=a; i<(b); i++)
#define F0R(i, a) for (int i=0; i<(a); i++)
#define FORd(i,a,b) for (int i = (b)-1; i >= a; i--)
#define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--)

#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound

const int MOD = 1000000007;

map<int,vi> fac;
map<vi,int> num;
int n, ans = 0;

int get(vi x) {
    if (x.size() == 0) return num[x] = 0;
    if (num.count(x)) return num[x];
    int mx = x[x.size()-1];
    set<int> cur;
    FOR(i,1,mx+1) {
        vi x1;
        for (int j: x)
            if (mx <= j) {
                if (mx != j) x1.pb(j-mx);
            } else x1.pb(j);

        sort(x1.begin(),x1.end());
        cur.insert(get(x1));
    }
    for (int i = 0;;++i) if (cur.find(i) == cur.end()) return num[x] = i;
}

void process(int x) {
    for (int i = 2; i*i <= x; ++i) if (x % i == 0) {
        int f = 0;
        while (x % i == 0) x /= i, f++;
        fac[i].pb(f);
    }
    if (x != 1) fac[x].pb(1);
}

int main() {
	ios_base::sync_with_stdio(0);cin.tie(0);
	cin >> n;
	F0R(i,n) {
	    int a; cin >> a;
	    process(a);
	}
	for (auto a: fac) {
	    vi z = a.s; sort(z.begin(),z.end());
	    ans ^= get(z);
	}
	if (ans == 0) cout << "Arpa";
	else cout << "Mojtaba";
}

// read!
// ll vs. int!


#include <iostream>
#include <algorithm>
#include <map>
#define MAXN 110
#define MAX 100000

using namespace std;

int n;
int a[MAXN];
int s[MAX];
vector<int> primes;
map<int,int> m;

int main() {
    for (int i = 2; i < MAX; i++) {
        if (s[i]) continue;
        primes.push_back(i);
        for (int j = i+i; j < MAX; j+=i) s[j] = 1;
    }

    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        for (int p : primes) {
            if (a[i] == 1) break;
            if (a[i] % p != 0) continue;
            int k = 0;
            while (a[i] % p) {
                a[i] /= p;
                k++;
            }
            m[p] = max(m[p], k);
        }
        if (a[i] > 1) {
            m[a[i]] = 1;
        }
    }

    int x = 0;
    for (auto it : m)
        x ^= it.second;

    if (x) cout << "Mojtaba\n";
    else cout << "Arpa\n";
}


#include <iostream>
#include <algorithm>
#include <map>
#define MAXN 110
#define MAX 100000

using namespace std;

int n;
int a[MAXN];
int s[MAX];
vector<int> primes;
map<int,int> m;

int main() {
    for (int i = 2; i < MAX; i++) {
        if (s[i]) continue;
        primes.push_back(i);
        for (int j = i+i; j < MAX; j++) s[j] = 1;
    }

    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        for (int p : primes) {
            if (a[i] == 1) break;
            if (a[i] % p != 0) continue;
            int k = 0;
            while (a[i] % p) {
                a[i] /= p;
                k++;
            }
            m[p] = max(m[p], k);
        }
        if (a[i] > 1) {
            m[a[i]] = 1;
        }
    }

    int x = 0;
    for (auto it : m)
        x ^= it.second;

    if (x) cout << "Mojitaba\n";
    else cout << "Arpa\n";
}


#include <iostream>
#include <algorithm>
#include <map>
#define MAXN 110
#define MAX 100000

using namespace std;

int n;
int a[MAXN];
int s[MAX];
vector<int> primes;
map<int,int> m;

int main() {
    for (int i = 2; i < MAX; i++) {
        if (s[i]) continue;
        primes.push_back(i);
        for (int j = i+i; j < MAX; j++) s[j] = 1;
    }

    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        for (int p : primes) {
            if (a[i] == 1) break;
            if (a[i] % p != 0) continue;
            int k = 0;
            while (a[i] % p) {
                a[i] /= p;
                k++;
            }
            m[p] = max(m[p], k);
        }
        if (a[i] > 1) {
            m[a[i]] = 1;
        }
    }

    int x = 0;
    for (auto it : m)
        x ^= it.second;

    if (x) cout << "Mojtaba\n";
    else cout << "Arpa\n";
}


#include <iostream>
#include <algorithm>
#include <map>
#define MAXN 110
#define MAX 100000

using namespace std;

int n;
int a[MAXN];
int s[MAX];
vector<int> primes;
map<int,int> m,g;

int grundy(int x) {
    if (x == 0 || x == 1) return 0;
    if (g.count(x)) return g[x];
    vector<int> cur;
    for (int i = 1; x >> i; i++) {
        int newx = (x & ((1<<i)-1)) | (x >> i);
        cur.push_back(grundy(newx));
    }
    sort(cur.begin(),cur.end());
    cur.erase(unique(cur.begin(),cur.end()),cur.end());
    int i;
    for (i = 0; i < cur.size(); i++){
        if (i != cur[i]) break;
    }
    return g[x] = i;
}

int main() {
    for (int i = 2; i < MAX; i++) {
        if (s[i]) continue;
        primes.push_back(i);
        for (int j = i+i; j < MAX; j+=i) s[j] = 1;
    }

    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        for (int p : primes) {
            if (a[i] == 1) break;
            if (a[i] % p != 0) continue;
            int k = 0;
            while (a[i] % p == 0) {
                a[i] /= p;
                k++;
            }
            m[p] |= (1 << k);
        }
        if (a[i] > 1) {
            m[a[i]] = 2;
        }
    }

    int x = 0;
    for (auto it : m)
        x ^= grundy(it.second);

    if (x) cout << "Mojtaba\n";
    else cout << "Arpa\n";
}


#include <iostream>
#include <algorithm>
#include <map>
#define MAXN 110
#define MAX 100000

using namespace std;

int n;
int a[MAXN];
int s[MAX];
vector<int> primes;
map<int,int> m,g;

int grundy(int x) {
    if (x == 0 || x == 1) return 0;
    if (g.count(x)) return g[x];
    vector<int> cur;
    for (int i = 1; x >> i; i++) {
        int newx = (x & ((1<<i)-1)) | (x >> i);
        cur.push_back(grundy(newx));
    }
    sort(cur.begin(),cur.end());
    cur.erase(unique(cur.begin(),cur.end()),cur.end());
    int i;
    for (i = 0; i < cur.size(); i++){
        if (i != cur[i]) break;
    }
    return g[x] = i;
}

int main() {
    for (int i = 2; i < MAX; i++) {
        if (s[i]) continue;
        primes.push_back(i);
        for (int j = i+i; j < MAX; j+=i) s[j] = 1;
    }

    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        for (int p : primes) {
            if (a[i] == 1) break;
            if (a[i] % p != 0) continue;
            int k = 0;
            while (a[i] % p == 0) {
                a[i] /= p;
                k++;
            }
            m[p] |= (1 << k);
        }
        if (a[i] > 1) {
            m[a[i]] = 1;
        }
    }

    int x = 0;
    for (auto it : m)
        x ^= grundy(it.second);

    if (x) cout << "Mojtaba\n";
    else cout << "Arpa\n";
}


#include <iostream>
#include <algorithm>
#include <map>
#define MAXN 110
#define MAX 100000

using namespace std;

int n;
int a[MAXN];
int s[MAX];
vector<int> primes;
map<int,int> m;

int main() {
    for (int i = 2; i < MAX; i++) {
        if (s[i]) continue;
        primes.push_back(i);
        cerr << i << endl;
        for (int j = i+i; j < MAX; j+=i) s[j] = 1;
    }

    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        for (int p : primes) {
            if (a[i] == 1) break;
            if (a[i] % p != 0) continue;
            int k = 0;
            while (a[i] % p == 0) {
                a[i] /= p;
                k++;
            }
            m[p] = max(m[p], k);
        }
        if (a[i] > 1) {
            m[a[i]] = 1;
        }
    }

    int x = 0;
    for (auto it : m)
        x ^= it.second;

    if (x) cout << "Mojtaba\n";
    else cout << "Arpa\n";
}


#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")
#include<cstdio>
#include<map>
using std::map;
const int N=30;
int n,ans;
map<int,int> M,memo;
int sg(int s)
{
	s&=~1;
	if(s==0)
		return 0;
	if(memo.find(s)!=memo.end())
		return memo[s];
	int now;
	bool book[N]={0};
	register int i;
	for(i=1;i<N;i++)
		if(s>>i)
			book[sg((s&((1<<i)-1))|(s>>i))]=1;
	for(now=0;book[now];now++);
	return memo[s]=now;
}
inline void add(int x)
{
	int cnt;
	register int i;
	for(i=2;i*i<=x;i++)
	{
		cnt=0;
		while(x%i==0)
			cnt++,x/=i;
		if(cnt)
			M[i]|=1<<cnt;
	}
	if(x>1)
		M[x]|=2;
	return;
}
signed main()
{
	int _;
	map<int,int>::iterator it;
	register int i;
	scanf("%d",&n);
	for(i=1;i<=n;i++)
	{
		scanf("%d",&_);
		add(_);
	}
	for(it=M.begin();it!=M.end();it++)
		ans^=sg(it->second);
	puts(ans?"Mojtaba":"Arpa");
	return 0;
}


#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")
#include<cstdio>
#include<map>
using std::map;
const int N=10;
int n,ans;
map<int,int> M,memo;
int sg(int s)
{
	s&=~1;
	if(s==0)
		return 0;
	if(memo.find(s)!=memo.end())
		return memo[s];
	int now;
	bool book[N]={0};
	register int i;
	for(i=1;i<N;i++)
		if(s>>i)
			book[sg((s&((1<<i)-1))|(s>>i))]=1;
	for(now=0;book[now];now++);
	return memo[s]=now;
}
inline void add(int x)
{
	int cnt;
	register int i;
	for(i=2;i*i<=x;i++)
	{
		cnt=0;
		while(x%i==0)
			cnt++,x/=i;
		if(cnt)
			M[i]|=1<<cnt;
	}
	if(x>1)
		M[x]|=2;
	return;
}
signed main()
{
	int _;
	map<int,int>::iterator it;
	register int i;
	scanf("%d",&n);
	for(i=1;i<=n;i++)
	{
		scanf("%d",&_);
		add(_);
	}
	for(it=M.begin();it!=M.end();it++)
		ans^=sg(it->second);
	puts(ans?"Mojtaba":"Arpa");
	return 0;
}


#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")
#include<cstdio>
#include<map>
using std::map;
const int N=35;
int n,ans;
map<int,int> M,memo;
int sg(int s)
{
	s&=~1;
	if(s==0)
		return 0;
	if(memo.find(s)!=memo.end())
		return memo[s];
	int now;
	bool book[N]={0};
	register int i;
	for(i=1;i<N;i++)
		if(s>>i)
			book[sg((s&((1<<i)-1))|(s>>i))]=1;
	for(now=0;book[now];now++);
	return memo[s]=now;
}
inline void add(int x)
{
	int cnt;
	register int i;
	for(i=2;i*i<=x;i++)
	{
		cnt=0;
		while(x%i==0)
			cnt++,x/=i;
		if(cnt)
			M[i]|=1<<cnt;
	}
	if(x>1)
		M[x]|=2;
	return;
}
signed main()
{
	int _;
	map<int,int>::iterator it;
	register int i;
	scanf("%d",&n);
	for(i=1;i<=n;i++)
	{
		scanf("%d",&_);
		add(_);
	}
	for(it=M.begin();it!=M.end();it++)
		ans^=sg(it->second);
	puts(ans?"Mojtaba":"Arpa");
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int > pii;
typedef pair<int,pii > piii;
typedef vector<int>     VI;

#define sc1(x) scanf("%d",&x);
#define sc2(x,y) scanf("%d%d",&x,&y);
#define sc3(x,y,z) scanf("%d%d%d",&x,&y,&z);
/*
#define sc1(x) scanf("%lld",&x);
#define sc2(x,y) scanf("%lld%lld",&x,&y);
#define sc3(x,y,z) scanf("%lld%lld%lld",&x,&y,&z);
*/
#define pb push_back
#define mp make_pair
#define ini(x,val) memset(x,val,sizeof(x));
#define fs first
#define sc second
#define MOD 1000000007
#define inf 1000000001
#define linf 99999999999999999ll	//long long inf
#define PI 3.1415926535897932384626
const double eps=0.000000000000001 ;

#define tr(container, it)  for(typeof(container.begin()) it = container.begin(); it != container.end(); it++)
#define PrintCont(cont) {cout<<("\n----------------\n");\
for(typeof(cont.begin()) it = cont.begin();it!=cont.end();++it) cout<<*it<<" ";cout<<("\n----------------\n");}
#define all(v) v.begin(),v.end()
string convertstring(ll n) { stringstream ss; ss << n ; return ss.str(); }

#define debug(x) cerr<<#x<<" :: "<<x<<"\n";
#define debug2(x,y) cerr<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\n";
#define debug3(x,y,z) cerr<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<"\n";
#define debug4(x,y,z,a) cerr<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<"\t"<<#a<<" :: "<<a<<"\n";
#define debugarr(a,st,en) {cerr<<"\n"<<#a<<" :: ";for(int i=st;i<=en;++i)cerr<<a[i]<<" ";cerr<<"\n";}

#define LIM 100005
VI pr;
map<int,int>mm;
unordered_map<int,int>dp;
int solve(int msk)
{
	if(dp.find(msk)!=dp.end())return dp[msk];
	set<int>st;
	for(int i=1;(1<<i)<=msk;++i)
	{
		st.insert(solve((msk>>i)|(msk&((1<<i)-1))));
	}
	int c = 0;
	while(st.find(c)!=st.end())c++;
	return dp[msk] = c;
}
int main()
{
	int n,i,j;
	sc1(n);
	for(i=1;i<=n;++i)
	{
		int x;
		sc1(x);
		for(j=2;j*j<=x;++j)
		{
			if(x%j==0)
			{
				int c = 0;
				while(x%j==0)
					c++,x/=j;
				pr.pb(j);
				mm[j]|=(1<<c);
			}
		}
		if(x > 1)
			pr.pb(x),mm[x]|=(1<<1);
	}
	int ans = 0;
	if(pr.size())
	{
		sort(all(pr));
		pr.erase(unique(all(pr)),pr.end());

		for(i=0;i<pr.size();++i)
		{
			dp.clear();
			debug2(pr[i],mm[pr[i]]);
			ans ^= solve(mm[pr[i]]);
		}
	}

	if(ans)
		printf("Mojtaba\n");
	else printf("Arpa\n");
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int > pii;
typedef pair<int,pii > piii;
typedef vector<int>     VI;

#define sc1(x) scanf("%d",&x);
#define sc2(x,y) scanf("%d%d",&x,&y);
#define sc3(x,y,z) scanf("%d%d%d",&x,&y,&z);
/*
#define sc1(x) scanf("%lld",&x);
#define sc2(x,y) scanf("%lld%lld",&x,&y);
#define sc3(x,y,z) scanf("%lld%lld%lld",&x,&y,&z);
*/
#define pb push_back
#define mp make_pair
#define ini(x,val) memset(x,val,sizeof(x));
#define fs first
#define sc second
#define MOD 1000000007
#define inf 1000000001
#define linf 99999999999999999ll	//long long inf
#define PI 3.1415926535897932384626
const double eps=0.000000000000001 ;

#define tr(container, it)  for(typeof(container.begin()) it = container.begin(); it != container.end(); it++)
#define PrintCont(cont) {cout<<("\n----------------\n");\
for(typeof(cont.begin()) it = cont.begin();it!=cont.end();++it) cout<<*it<<" ";cout<<("\n----------------\n");}
#define all(v) v.begin(),v.end()
string convertstring(ll n) { stringstream ss; ss << n ; return ss.str(); }

#define debug(x) cerr<<#x<<" :: "<<x<<"\n";
#define debug2(x,y) cerr<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\n";
#define debug3(x,y,z) cerr<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<"\n";
#define debug4(x,y,z,a) cerr<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<"\t"<<#a<<" :: "<<a<<"\n";
#define debugarr(a,st,en) {cerr<<"\n"<<#a<<" :: ";for(int i=st;i<=en;++i)cerr<<a[i]<<" ";cerr<<"\n";}

#define LIM 100005
VI pr;
map<int,int>mm;
unordered_map<int,int>dp;
int solve(int msk)
{
	if(dp.find(msk)!=dp.end())return dp[msk];
	set<int>st;
	for(int i=1;(1<<i)<=msk;++i)
	{
		st.insert(solve((msk>>i)|(msk&((1<<i)-1))));
	}
	int c = 0;
	while(st.find(c)!=st.end())c++;
	return dp[msk] = c;
}
int main()
{
	int n,i,j;
	sc1(n);
	for(i=1;i<=n;++i)
	{
		int x;
		sc1(x);
		for(j=2;j*j<=x;++j)
		{
			if(x%j==0)
			{
				int c = 0;
				while(x%j==0)
					c++,x/=j;
				pr.pb(j);
				mm[j]|=(1<<c);
			}
		}
		if(x > 1)
			pr.pb(x),mm[x]|=(1<<1);
	}
	int ans = 0;
	if(pr.size())
	{
		sort(all(pr));
		pr.erase(unique(all(pr)));

		for(i=0;i<pr.size();++i)
		{
			dp.clear();
			ans ^= solve(mm[pr[i]]);
		}
	}

	if(ans)
		printf("Mojtaba\n");
	else printf("Arpa\n");
	return 0;
}


#include <cstdlib>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <numeric>
#include <sstream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <stack>

using namespace std;

#define sz(X) ((int)(X).size())
#define pb push_back
#define mp make_pair
#define X first
#define Y second
#define all(X) (X).begin(),(X).end()
#define FOR(i, a, n) for(int i=(a), __ ## i=(n); i<__ ## i; i++)
#define REP(I,N) FOR(I,0,N)
#define PR(X) cout<<#X<<" = "<<(X)<<" "
#define PRL cout<<endl
#define PRV(X) {cout<<#X<<" = {";REP(__prv,sz(X)-1)cout<<(X)[__prv]<<",";if(sz(X))cout<<*(X).end();cout<<"}"<<endl;}

template<class T> ostream &operator<<(ostream &os, const vector<T> &vec)
{
	os << '{';
	REP(i, sz(vec))
	{
		os << vec[i];
		if (i + 1 != sz(vec))
			os << ',';
	}
	os << '}';
	return os;
}

template<class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &par)
{
	os << '(' << par.X << ',' << par.Y << ')';
	return os;
}

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<bool> VB;
typedef vector<unsigned long long> VULL;
typedef vector<vector<int>> VVI;
typedef queue<int> QI;

int gcd(int x, int y)
{
	return y ? gcd(y, x % y) : abs(x);
}

template<class T> T sqr(T x)
{
	return x * x;
}

const LL MOD = 1e9 + 7;

//base ^ p
LL bin_pow(LL base, LL p) {
	if (p == 1)
		return base;

	if (p % 2 == 0) {
		LL t = bin_pow(base, p / 2);
		return t * t % MOD;
	}
	else
		return bin_pow(base, p - 1) * base % MOD;
}

LL inverse_element(LL x) {
	return bin_pow(x, MOD - 2);
}

//(a / b) mod m
LL divide(LL a, LL b) {
	return a * inverse_element(b) % MOD;
}

//(a * b) mod m
LL mult(LL a, LL b) {
	return (a * b) % MOD;
}

LL a[111];
bool nonsimp[101010];

map<LL, bool> buf;
map<LL, bool> sol;

LL solve(LL num)
{
	if (num == 0)
		return 0;

	if (sol[num])
		return buf[num];

	int moves = 1;
	while ((num >> moves) > 0)
		moves++;

	vector<LL> vec;
	FOR(i, 1, moves + 1) {
		LL left = (num >> i);
		LL right = (num & ((1LL << (i - 1)) - 1));
		LL cur = (left | right);
		LL res = solve(cur);
		vec.pb(res);
	}

	sort(all(vec));
	LL res = 0;
	while (res < sz(vec) && vec[res] == res)
		res++;

	sol[num] = true;
	buf[num] = res;
	return res;
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);

	VI simp;
	FOR(i, 2, 101010) {
		if (nonsimp[i] == false) {
			simp.pb(i);
			for (LL j = (LL)i*i; j < 101010; j += i) {
				nonsimp[j] = true;
			}
		}
	}

	int n;
	cin >> n;

	REP(i, n) {
		cin >> a[i];
	}

	LL win = 0;

	REP(i, sz(simp)) {
		LL mask = 0;
		REP(j, n) {
			LL cnt = 0;
			while ((a[j] % simp[i]) == 0) {
				cnt++;
				a[j] /= simp[i];
			}

			if (cnt > 0)
				mask |= (1LL << (cnt - 1));
		}

		if (mask > 0) {
			win ^= solve(mask);
		}
	}

	REP(i, n) {
		if (a[i] > 1) {
			win ^= solve(1);
			FOR(j, i + 1, n) {
				if (a[j] == a[i])
					a[j] = 1;
			}
		}
	}

	if (win)
		cout << "Mojtaba";
	else
		cout << "Arpa";

	return 0;
}


#include <cstdlib>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <numeric>
#include <sstream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <stack>

using namespace std;

#define sz(X) ((int)(X).size())
#define pb push_back
#define mp make_pair
#define X first
#define Y second
#define all(X) (X).begin(),(X).end()
#define FOR(i, a, n) for(int i=(a), __ ## i=(n); i<__ ## i; i++)
#define REP(I,N) FOR(I,0,N)
#define PR(X) cout<<#X<<" = "<<(X)<<" "
#define PRL cout<<endl
#define PRV(X) {cout<<#X<<" = {";REP(__prv,sz(X)-1)cout<<(X)[__prv]<<",";if(sz(X))cout<<*(X).end();cout<<"}"<<endl;}

template<class T> ostream &operator<<(ostream &os, const vector<T> &vec)
{
	os << '{';
	REP(i, sz(vec))
	{
		os << vec[i];
		if (i + 1 != sz(vec))
			os << ',';
	}
	os << '}';
	return os;
}

template<class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &par)
{
	os << '(' << par.X << ',' << par.Y << ')';
	return os;
}

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<bool> VB;
typedef vector<unsigned long long> VULL;
typedef vector<vector<int>> VVI;
typedef queue<int> QI;

int gcd(int x, int y)
{
	return y ? gcd(y, x % y) : abs(x);
}

template<class T> T sqr(T x)
{
	return x * x;
}

const LL MOD = 1e9 + 7;

//base ^ p
LL bin_pow(LL base, LL p) {
	if (p == 1)
		return base;

	if (p % 2 == 0) {
		LL t = bin_pow(base, p / 2);
		return t * t % MOD;
	}
	else
		return bin_pow(base, p - 1) * base % MOD;
}

LL inverse_element(LL x) {
	return bin_pow(x, MOD - 2);
}

//(a / b) mod m
LL divide(LL a, LL b) {
	return a * inverse_element(b) % MOD;
}

//(a * b) mod m
LL mult(LL a, LL b) {
	return (a * b) % MOD;
}

LL a[111];
bool nonsimp[101010];

map<LL, bool> buf;
map<LL, bool> sol;

bool solve(LL num)
{
	if (num == 0)
		return false;

	if (sol[num])
		return buf[num];

	int moves = 1;
	while ((num >> moves) > 0)
		moves++;

	FOR(i, 1, moves + 1) {
		LL left = num >> i;
		LL right = num & (1LL << (i - 1) - 1);
		LL cur = left | right;
		if (!solve(cur)) {
			sol[num] = true;
			buf[num] = true;
			return true;
		}
	}

	sol[num] = true;
	buf[num] = false;
	return false;
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);

	VI simp;
	FOR(i, 2, 101010) {
		if (nonsimp[i] == false) {
			simp.pb(i);
			for (LL j = (LL)i*i; j < 101010; j += i) {
				nonsimp[j] = true;
			}
		}
	}

	int n;
	cin >> n;

	REP(i, n) {
		cin >> a[i];
	}

	bool win = false;

	REP(i, sz(simp)) {
		LL mask = 0;
		REP(j, n) {
			LL cnt = 0;
			while ((a[j] % simp[i]) == 0) {
				cnt++;
				a[j] /= simp[i];
			}

			if (cnt > 0)
				mask |= (1LL << (cnt - 1));
		}

		if (mask > 0) {
			if (solve(mask))
				win = !win;
		}
	}

	REP(i, n) {
		if (a[i] > 1) {
			win = !win;
			FOR(j, i + 1, n) {
				if (a[j] == a[i])
					a[j] = 1;
			}
		}
	}

	if (win)
		cout << "Mojtaba";
	else
		cout << "Arpa";

	return 0;
}


#include <cstdlib>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <numeric>
#include <sstream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <stack>

using namespace std;

#define sz(X) ((int)(X).size())
#define pb push_back
#define mp make_pair
#define X first
#define Y second
#define all(X) (X).begin(),(X).end()
#define FOR(i, a, n) for(int i=(a), __ ## i=(n); i<__ ## i; i++)
#define REP(I,N) FOR(I,0,N)
#define PR(X) cout<<#X<<" = "<<(X)<<" "
#define PRL cout<<endl
#define PRV(X) {cout<<#X<<" = {";REP(__prv,sz(X)-1)cout<<(X)[__prv]<<",";if(sz(X))cout<<*(X).end();cout<<"}"<<endl;}

template<class T> ostream &operator<<(ostream &os, const vector<T> &vec)
{
	os << '{';
	REP(i, sz(vec))
	{
		os << vec[i];
		if (i + 1 != sz(vec))
			os << ',';
	}
	os << '}';
	return os;
}

template<class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &par)
{
	os << '(' << par.X << ',' << par.Y << ')';
	return os;
}

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<bool> VB;
typedef vector<unsigned long long> VULL;
typedef vector<vector<int>> VVI;
typedef queue<int> QI;

int gcd(int x, int y)
{
	return y ? gcd(y, x % y) : abs(x);
}

template<class T> T sqr(T x)
{
	return x * x;
}

const LL MOD = 1e9 + 7;

//base ^ p
LL bin_pow(LL base, LL p) {
	if (p == 1)
		return base;

	if (p % 2 == 0) {
		LL t = bin_pow(base, p / 2);
		return t * t % MOD;
	}
	else
		return bin_pow(base, p - 1) * base % MOD;
}

LL inverse_element(LL x) {
	return bin_pow(x, MOD - 2);
}

//(a / b) mod m
LL divide(LL a, LL b) {
	return a * inverse_element(b) % MOD;
}

//(a * b) mod m
LL mult(LL a, LL b) {
	return (a * b) % MOD;
}

LL a[111];
bool nonsimp[101010];

map<LL, LL> buf;
map<LL, bool> sol;

LL solve(LL num)
{
	if (num == 0)
		return 0;

	if (sol[num])
		return buf[num];

	int moves = 1;
	while ((num >> moves) > 0)
		moves++;

	vector<LL> vec;
	FOR(i, 1, moves + 1) {
		LL left = (num >> i);
		LL right = (num & ((1LL << (i - 1)) - 1));
		LL cur = (left | right);
		LL res = solve(cur);
		vec.pb(res);
	}

	sort(all(vec));
	LL res = 0;
	while (res < sz(vec) && vec[res] == res)
		res++;

	sol[num] = true;
	buf[num] = res;
	return res;
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);

	VI simp;
	FOR(i, 2, 101010) {
		if (nonsimp[i] == false) {
			simp.pb(i);
			for (LL j = (LL)i*i; j < 101010; j += i) {
				nonsimp[j] = true;
			}
		}
	}

	int n;
	cin >> n;

	REP(i, n) {
		cin >> a[i];
	}

	LL win = 0;

	REP(i, sz(simp)) {
		LL mask = 0;
		REP(j, n) {
			LL cnt = 0;
			while ((a[j] % simp[i]) == 0) {
				cnt++;
				a[j] /= simp[i];
			}

			if (cnt > 0)
				mask |= (1LL << (cnt - 1));
		}

		if (mask > 0) {
			win ^= solve(mask);
		}
	}

	REP(i, n) {
		if (a[i] > 1) {
			win ^= solve(1);
			FOR(j, i + 1, n) {
				if (a[j] == a[i])
					a[j] = 1;
			}
		}
	}

	if (win)
		cout << "Mojtaba";
	else
		cout << "Arpa";

	return 0;
}


#include <cstdlib>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <numeric>
#include <sstream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <stack>

using namespace std;

#define sz(X) ((int)(X).size())
#define pb push_back
#define mp make_pair
#define X first
#define Y second
#define all(X) (X).begin(),(X).end()
#define FOR(i, a, n) for(int i=(a), __ ## i=(n); i<__ ## i; i++)
#define REP(I,N) FOR(I,0,N)
#define PR(X) cout<<#X<<" = "<<(X)<<" "
#define PRL cout<<endl
#define PRV(X) {cout<<#X<<" = {";REP(__prv,sz(X)-1)cout<<(X)[__prv]<<",";if(sz(X))cout<<*(X).end();cout<<"}"<<endl;}

template<class T> ostream &operator<<(ostream &os, const vector<T> &vec)
{
	os << '{';
	REP(i, sz(vec))
	{
		os << vec[i];
		if (i + 1 != sz(vec))
			os << ',';
	}
	os << '}';
	return os;
}

template<class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &par)
{
	os << '(' << par.X << ',' << par.Y << ')';
	return os;
}

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<bool> VB;
typedef vector<unsigned long long> VULL;
typedef vector<vector<int>> VVI;
typedef queue<int> QI;

int gcd(int x, int y)
{
	return y ? gcd(y, x % y) : abs(x);
}

template<class T> T sqr(T x)
{
	return x * x;
}

const LL MOD = 1e9 + 7;

//base ^ p
LL bin_pow(LL base, LL p) {
	if (p == 1)
		return base;

	if (p % 2 == 0) {
		LL t = bin_pow(base, p / 2);
		return t * t % MOD;
	}
	else
		return bin_pow(base, p - 1) * base % MOD;
}

LL inverse_element(LL x) {
	return bin_pow(x, MOD - 2);
}

//(a / b) mod m
LL divide(LL a, LL b) {
	return a * inverse_element(b) % MOD;
}

//(a * b) mod m
LL mult(LL a, LL b) {
	return (a * b) % MOD;
}

LL a[111];
bool nonsimp[101010];

map<LL, LL> buf;
map<LL, bool> sol;

LL solve(LL num)
{
	if (num == 0)
		return 0;

	if (sol[num])
		return buf[num];

	int moves = 1;
	while ((num >> moves) > 0)
		moves++;

	vector<LL> vec;
	FOR(i, 1, moves + 1) {
		LL left = (num >> i);
		LL right = (num & ((1LL << (i - 1)) - 1));
		LL cur = (left | right);
		LL res = solve(cur);
		vec.pb(res);
	}

	sort(all(vec));
	vec.erase(unique(vec.begin(), vec.end()), vec.end());
	LL res = 0;
	while (res < sz(vec) && vec[res] == res)
		res++;

	sol[num] = true;
	buf[num] = res;
	return res;
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);

	VI simp;
	FOR(i, 2, 101010) {
		if (nonsimp[i] == false) {
			simp.pb(i);
			for (LL j = (LL)i*i; j < 101010; j += i) {
				nonsimp[j] = true;
			}
		}
	}

	int n;
	cin >> n;

	REP(i, n) {
		cin >> a[i];
	}

	LL win = 0;

	REP(i, sz(simp)) {
		LL mask = 0;
		REP(j, n) {
			LL cnt = 0;
			while ((a[j] % simp[i]) == 0) {
				cnt++;
				a[j] /= simp[i];
			}

			if (cnt > 0)
				mask |= (1LL << (cnt - 1));
		}

		if (mask > 0) {
			win ^= solve(mask);
		}
	}

	REP(i, n) {
		if (a[i] > 1) {
			win ^= solve(1);
			FOR(j, i + 1, n) {
				if (a[j] == a[i])
					a[j] = 1;
			}
		}
	}

	if (win)
		cout << "Mojtaba";
	else
		cout << "Arpa";

	return 0;
}


#include <cstdlib>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <numeric>
#include <sstream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <stack>

using namespace std;

#define sz(X) ((int)(X).size())
#define pb push_back
#define mp make_pair
#define X first
#define Y second
#define all(X) (X).begin(),(X).end()
#define FOR(i, a, n) for(int i=(a), __ ## i=(n); i<__ ## i; i++)
#define REP(I,N) FOR(I,0,N)
#define PR(X) cout<<#X<<" = "<<(X)<<" "
#define PRL cout<<endl
#define PRV(X) {cout<<#X<<" = {";REP(__prv,sz(X)-1)cout<<(X)[__prv]<<",";if(sz(X))cout<<*(X).end();cout<<"}"<<endl;}

template<class T> ostream &operator<<(ostream &os, const vector<T> &vec)
{
	os << '{';
	REP(i, sz(vec))
	{
		os << vec[i];
		if (i + 1 != sz(vec))
			os << ',';
	}
	os << '}';
	return os;
}

template<class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &par)
{
	os << '(' << par.X << ',' << par.Y << ')';
	return os;
}

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<bool> VB;
typedef vector<unsigned long long> VULL;
typedef vector<vector<int>> VVI;
typedef queue<int> QI;

int gcd(int x, int y)
{
	return y ? gcd(y, x % y) : abs(x);
}

template<class T> T sqr(T x)
{
	return x * x;
}

const LL MOD = 1e9 + 7;

//base ^ p
LL bin_pow(LL base, LL p) {
	if (p == 1)
		return base;

	if (p % 2 == 0) {
		LL t = bin_pow(base, p / 2);
		return t * t % MOD;
	}
	else
		return bin_pow(base, p - 1) * base % MOD;
}

LL inverse_element(LL x) {
	return bin_pow(x, MOD - 2);
}

//(a / b) mod m
LL divide(LL a, LL b) {
	return a * inverse_element(b) % MOD;
}

//(a * b) mod m
LL mult(LL a, LL b) {
	return (a * b) % MOD;
}

LL a[111];
bool nonsimp[101010];

map<LL, bool> buf;
map<LL, bool> sol;

bool solve(LL num)
{
	if (num == 0)
		return false;

	if (sol[num])
		return buf[num];

	int moves = 1;
	while ((num >> moves) > 0)
		moves++;

	FOR(i, 1, moves + 1) {
		LL left = num >> i;
		LL right = num & (1LL << (i - 1) - 1);
		LL cur = left | right;
		if (!solve(cur)) {
			sol[num] = true;
			buf[num] = true;
			return true;
		}
	}

	sol[num] = true;
	buf[num] = false;
	return false;
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);

	VI simp;
	FOR(i, 2, 101010) {
		if (nonsimp[i] == false) {
			simp.pb(i);
			for (LL j = (LL)i*i; j < 101010; j += i) {
				nonsimp[j] = true;
			}
		}
	}

	int n;
	cin >> n;

	REP(i, n) {
		cin >> a[i];
	}

	bool win = false;

	REP(i, sz(simp)) {
		LL mask = 0;
		REP(j, n) {
			LL cnt = 0;
			while ((a[j] % simp[i]) == 0) {
				cnt++;
				a[j] /= simp[i];
			}

			if (cnt > 0)
				mask |= (1LL << (cnt - 1));
		}

		if (mask > 0) {
			if (solve(mask))
				win = !win;
		}
	}

	REP(i, n) {
		if (a[i] > 1)
			win = !win;
	}

	if (win)
		cout << "Mojtaba";
	else
		cout << "Arpa";

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define pfi(a) printf("%d",a);
#define pfl(a) printf("%lld",a);
#define pfin(a) printf("%d\n",a);
#define pfln(a) printf("%lld\n",a);
#define sfi(a) scanf("%d",&a);
#define sfl(a) scanf("%lld",&a);
#define pb push_back
#define f(i,a,b) for(int i=a;i<b;i++)
#define fi(i,a,b) for(int i=a-1;i>=b;i--)
#define ll long long
#define endl '\n'
const ll M=1e9+7;
const int MAXN=5e5+5;

vector<pair<int,ll>> v[MAXN];

pair<ll,ll> ans[MAXN];

int k;

void dfs(int node,int par){
	vector<pair<ll,ll> > util;
	ll sum=0;

	for(auto x:v[node]){
		if(x.first!=par){

			dfs(x.first,node);

			(sum+=ans[x.first].first);
			util.push_back({ans[x.first].second+x.second-ans[x.first].first,x.first});
		}
	}

	ll tot=sum;
	int cnt=0;

	sort(util.begin(),util.end(),greater<pair<ll,ll>>());

	for(auto y:util){
		cnt++;

		if(y.first>0){
			tot=tot+y.first;
		}
		if(cnt==k-1){

			ans[node].second=tot;
		}
		else if(cnt==k){
			ans[node].first=tot;
			break;
		}
	}

	if(cnt<k){
		ans[node].first=ans[node].second=tot;
	}
}

int main(){
 ios_base::sync_with_stdio(false);

 	int t; cin>>t;

 	while(t--){
 		int n;
 		cin>>n>>k;

 		f(i,1,n+1){
 			v[i].clear();
 			ans[i].first=0;
 			ans[i].second=0;
 		}

 		f(i,0,n-1){
 			int x,y,w; cin>>x>>y>>w;
 			v[x].pb({y,w}); v[y].pb({x,w});
 		}

 		dfs(1,1);

 		cout<<ans[1].first<<endl;
 	}

}


#include<bits/stdc++.h>
using namespace std;
#define pfi(a) printf("%d",a);
#define pfl(a) printf("%lld",a);
#define pfin(a) printf("%d\n",a);
#define pfln(a) printf("%lld\n",a);
#define sfi(a) scanf("%d",&a);
#define sfl(a) scanf("%lld",&a);
#define pb push_back
#define f(i,a,b) for(int i=a;i<b;i++)
#define fi(i,a,b) for(int i=a-1;i>=b;i--)
#define ll long long
#define endl '\n'
const ll M=1e9+7;
const int MAXN=5e5+5;

vector<pair<int,ll>> v[MAXN];

pair<ll,ll> ans[MAXN];

int k;

void dfs(int node,int par){
	vector<pair<ll,ll> > util;
	ll sum=0;

	for(auto x:v[node]){
		if(x.first!=par){

			dfs(x.first,node);

			(sum+=ans[x.first].first);
			util.push_back({ans[x.first].second+x.second-ans[x.first].first,x.first});
		}
	}

	ll tot=sum;
	int cnt=0;

	sort(util.begin(),util.end(),greater<pair<ll,ll>>());

	for(auto y:util){
		cnt++;

		if(y.first>0){
			tot=tot+y.first;
		}
		if(cnt==k-1){

			ans[node].second=tot;
		}
		else if(cnt==k){
			ans[node].first=tot;
			break;
		}
	}

	if(cnt<k){
		ans[node].first=ans[node].second=tot;
	}
}

int main(){
 ios_base::sync_with_stdio(false);

 	int t; cin>>t;

 	while(t--){
 		int n;
 		cin>>n>>k;

 		f(i,1,n+1)
 		v[i].clear();

 		f(i,0,n-1){
 			int x,y,w; cin>>x>>y>>w;
 			v[x].pb({y,w}); v[y].pb({x,w});
 		}

 		dfs(1,1);

 		cout<<ans[1].first<<endl;
 	}

}


#include<bits/stdc++.h>
using namespace std;
#define pfi(a) printf("%d",a);
#define pfl(a) printf("%lld",a);
#define pfin(a) printf("%d\n",a);
#define pfln(a) printf("%lld\n",a);
#define sfi(a) scanf("%d",&a);
#define sfl(a) scanf("%lld",&a);
#define pb push_back
#define f(i,a,b) for(int i=a;i<b;i++)
#define fi(i,a,b) for(int i=a-1;i>=b;i--)
#define ll long long
#define endl '\n'
const ll M=1e9+7;
const int MAXN=5e5+5;

vector<pair<int,ll>> v[MAXN];

pair<ll,ll> ans[MAXN];

int k;

void dfs(int node,int par){

	vector<ll> util;
	ll sum=0;

	for(auto x:v[node]){
		int y=x.first;
		if(y==par) continue;

			dfs(y,node);

			sum+=ans[y].first;

			util.push_back(ans[y].second+x.second-ans[y].first);
	}

	int cnt=0;

	ans[node].first=sum;
	ans[node].second=sum;

	sort(util.begin(),util.end(),greater<ll>());

	for(auto y:util){
		cnt++;

		if(y>0){

			if(cnt<=k-1){
				ans[node].second+=y;
				ans[node].first+=y;
			}
			else if(cnt<=k){
				ans[node].first+=y;
			}
		}
	}

}

int main(){
 ios_base::sync_with_stdio(false);

 	int t; cin>>t;

 	while(t--){
 		int n;
 		cin>>n>>k;

 		f(i,1,n+1){
 			v[i].clear();
 			ans[i].first=0;
 			ans[i].second=0;
 		}

 		f(i,0,n-1){
 			int x,y,w; cin>>x>>y>>w;
 			v[x].pb({y,w}); v[y].pb({x,w});
 		}

 		dfs(1,1);

 		cout<<ans[1].first<<endl;
 	}

}


#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define endl '\n'
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int mod=1e9+7;
//const int mod=998244353;
const double eps = 1e-10;
const double pi=acos(-1.0);
const int maxn=5e5+10;
const ll inf=0x3f3f3f3f;

int n,k;
vector<pii> g[maxn];
ll dp[maxn][2];
void dfs(int u,int fa){
    dp[u][0]=dp[u][1]=0;
    for(auto v:g[u]){
        if(v.fi==fa)continue;
        dfs(v.fi,u);
        dp[u][0]+=dp[v.fi][1];
        dp[u][1]+=dp[v.fi][1];
    }
    priority_queue<int> q;
    for(auto v:g[u]){
        if(v.fi==fa)continue;
        q.push(v.se+dp[v.fi][0]-dp[v.fi][1]);
    }
    for(int i=0;i<k&&q.size();i++){
        int x=q.top();q.pop();
        if(x<0)break;
        if(i<k-1)dp[u][0]+=x;
        dp[u][1]+=x;
    }
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int q;cin>>q;
    while(q--){

        cin>>n>>k;
        for(int i=1;i<=n;i++)g[i].clear();
        for(int i=1;i<n;i++){
            int u,v,w;
            cin>>u>>v>>w;
            g[u].pb(mp(v,w));
            g[v].pb(mp(u,w));
        }
        dfs(1,0);
        cout<<dp[1][1]<<endl;
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define endl '\n'
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int mod=1e9+7;
//const int mod=998244353;
const double eps = 1e-10;
const double pi=acos(-1.0);
const int maxn=5e5+10;
const ll inf=0x3f3f3f3f;

int n,k;
vector<pii> g[maxn];
int dp[maxn][2];
void dfs(int u,int fa){
    dp[u][0]=dp[u][1]=0;
    for(auto v:g[u]){
        if(v.fi==fa)continue;
        dfs(v.fi,u);
        dp[u][0]+=dp[v.fi][1];
        dp[u][1]+=dp[v.fi][1];
    }
    priority_queue<int> q;
    for(auto v:g[u]){
        if(v.fi==fa)continue;
        q.push(v.se+dp[v.fi][0]-dp[v.fi][1]);
    }
    for(int i=0;i<k&&q.size();i++){
        int x=q.top();q.pop();
        if(x<0)break;
        if(i<k-1)dp[u][0]+=x;
        dp[u][1]+=x;
    }
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int q;cin>>q;
    while(q--){

        cin>>n>>k;
        for(int i=1;i<=n;i++)g[i].clear();
        for(int i=1;i<n;i++){
            int u,v,w;
            cin>>u>>v>>w;
            g[u].pb(mp(v,w));
            g[v].pb(mp(u,w));
        }
        dfs(1,0);
        cout<<dp[1][1]<<endl;
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define int long long
vector<pair<int,int> > g[500005];
int dp1[500005];
int dp2[500005];
int d[500005];
int k;
void dfs(int node,int parent){
	for(auto it:g[node]){
		if(it.first!=parent){
			dfs(it.first,node);
		}
	}
	int ans1=0;
	if(d[node]<k or d[node]==1){
		for(auto it:g[node]){
			if(it.first!=parent){
				ans1=ans1+dp2[it.first]+it.second;
			}
		}
		dp1[node]=ans1;
		dp2[node]=ans1;
	}

	int ans2=0;
	if(d[node]>=k and d[node]!=1){
		vector<pair<int,int>> temp;
		for(auto it:g[node]){
			if(it.first!=parent){
				temp.push_back(make_pair(it.second,it.first));
			}
		}
		sort(temp.rbegin(),temp.rend());
		for(int i=0;i<k-1;i++){
			ans1=ans1+temp[i].first+dp2[temp[i].second];
			ans2=ans2+temp[i].first+dp2[temp[i].second];
		}
		for(int i=k-1;i<temp.size();i++){
			ans1=ans1+dp1[temp[i].second];
			ans2=ans2+dp1[temp[i].second];
		}
		ans1=ans1+temp[k-1].first;
		dp1[node]=ans1;
		dp2[node]=ans2;
	}
}
void solve(){
	int n;
	cin>>n>>k;
	for(int i=1;i<=n;i++){
		g[i].clear();
		dp1[i]=0;
		dp2[i]=0;
	}
	for(int i=0;i<n-1;i++){
		int u,v,w;
		cin>>u>>v>>w;
		g[u].push_back(make_pair(v,w));
		g[v].push_back(make_pair(u,w));
	}
	for(int i=1;i<=n;i++){
		d[i]=g[i].size();
	}
	dfs(1,-1);

	cout<<max(dp1[1],dp2[1])<<endl;

	return;
}
int32_t main(){

	int q;
	cin>>q;
	for(int i=0;i<q;i++){
		solve();
	}

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define int long long
vector<pair<int,int> > g[500005];
int dp1[500005];
int dp2[500005];
int d[500005];
int k;
void dfs(int node,int parent){
	for(auto it:g[node]){
		if(it.first!=parent){
			dfs(it.first,node);
		}
	}
	int ans1=0;
	if(d[node]<=k ){
		for(auto it:g[node]){
			if(it.first!=parent){
				ans1=ans1+max(dp2[it.first]+it.second,dp1[it.first]);
			}
		}
		dp1[node]=ans1;
		dp2[node]=ans1;
	}

	int ans2=0;
	if(d[node]>k){
		vector<pair<int,int>> temp;
		for(auto it:g[node]){
			if(it.first!=parent){
				temp.push_back(make_pair(it.second+dp2[it.first]-dp1[it.first],it.first));
			}
		}
		sort(temp.rbegin(),temp.rend());
		for(int i=0;i<k-1;i++){
			if(temp[i].first>0){
				ans1=ans1+temp[i].first+dp1[temp[i].second];
				ans2=ans2+temp[i].first+dp1[temp[i].second];
			}
			else{
				ans1+=dp1[temp[i].second];
				ans2+=dp1[temp[i].second];
			}
		}
		if(k-1<temp.size() and temp[k-1].first>0){
			ans1=ans1+temp[k-1].first+dp1[temp[k-1].second];
		}
		else{
			ans1=ans1+dp1[temp[k-1].second];
		}
		for(int i=k;i<temp.size();i++){
			ans1=ans1+dp1[temp[i].second];
		}
		for(int i=k-1;i<temp.size();i++){
			ans2=ans2+dp1[temp[i].second];
		}
		dp1[node]=ans1;
		dp2[node]=ans2;
	}
}
void solve(){
	int n;
	cin>>n>>k;
	for(int i=1;i<=n;i++){
		g[i].clear();
		dp1[i]=0;
		dp2[i]=0;
	}
	for(int i=0;i<n-1;i++){
		int u,v,w;
		cin>>u>>v>>w;
		g[u].push_back(make_pair(v,w));
		g[v].push_back(make_pair(u,w));
	}
	for(int i=1;i<=n;i++){
		d[i]=g[i].size();
	}
	dfs(1,-1);
	cout<<max(dp1[1],dp2[1])<<endl;

	return;
}
int32_t main(){
	IOS;
	int q;
	cin>>q;
	for(int i=0;i<q;i++){
		solve();
	}

	return 0;
}


#include <bits/stdc++.h>
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define rep(i, a, b) for (int i = (a); i < (b); ++i)
using namespace std;

#define int long long
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
ll mod = (1000000007LL);
inline ll Mod(ll a, ll b){return (a%b);}
inline ll poww(ll a, ll b){ll res = 1;while (b > 0){if(b & 1) res = (res * a)%mod;a = (a * a)%mod;b >>= 1;}return res;}
ll gcd (ll a, ll b) { while (b) { a %= b,swap(a, b);}return a;}
void read(vector<int> &w, int n){w.resize(n);for(int i = 0; i < n; i++) cin>>w[i];}
void print(vector<int> &w){for(int i =0; i < sz(w); i++){if(i == sz(w) - 1) cout<<w[i]<<"\n";else cout<<w[i]<<" ";}}
int prodmod(vector<int> w);
int summod(vector<int> w);
///CUIDADO COM MAXN
#define N 500050

int n, m, q, k, v[N], ans;
pii w[N];
string s;
vector<pii> grafo[N];
int dp[N][2];
void solve(int x, int p){
	int sum = 0;
	vector<int> caras;
	for(auto v: grafo[x]){
		if(v.f==p) continue;
		solve(v.f, x);
		sum += dp[v.f][0];
		caras.pb(max(dp[v.f][1] + v.s, dp[v.f][0]) - dp[v.f][0]);
	}
	int sum1=0,sum2=0;
	sort(caras.rbegin(), caras.rend());
	for(int i = 0; i < min(k, sz(caras)); i++){
		sum1 += caras[i];
		if(i<k-1) sum2 += caras[i];
	}
	dp[x][0] = sum+sum1;
	dp[x][1] = sum+sum2;
}
int32_t main(){
	ios::sync_with_stdio(false); cin.tie(0);
	cin>>q;
	while(q--){
		cin>>n>>k;
		for(int i = 1, a, b, c; i <n;i++){
			cin>>a>>b>>c;
			grafo[a].pb({b, c});
			grafo[b].pb({a, c});
		}
		solve(1, 1);
		cout<<max(dp[1][0], dp[1][1])<<"\n";
		for(int i=1;i<=n;i++){
			dp[i][0]=dp[i][1]=0;
			grafo[i].clear();
		}
	}
}
/*
    CUIDADO COM MAXN, MOD, OVERFLOW
    >Colocar (>DEFINE INT LONG LONG<) no inicio do template
    >mod = (1e9 + 7), por padrao (NAO ESQUECER DE ALTERAR)
    >NAO ESQUECER DEFINE INT LONG LONG
    > N = 1 ?
*/
int summod(vector<int> w){int curr=0;for(int i=0;i<sz(w); i++){curr = (curr+w[i])%mod;if(curr<0)curr+=mod;}return curr;}
int prodmod(vector<int> w){int curr = 1;
for(int i = 0; i < sz(w); i++){if(w[i] >= mod) w[i] %= mod;curr = (curr * w[i]) % mod;if(curr < 0) curr += mod;}return curr;}


#include <bits/stdc++.h>
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define rep(i, a, b) for (int i = (a); i < (b); ++i)
using namespace std;

#define int long long
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
ll mod = (1000000007LL);
inline ll Mod(ll a, ll b){return (a%b);}
inline ll poww(ll a, ll b){ll res = 1;while (b > 0){if(b & 1) res = (res * a)%mod;a = (a * a)%mod;b >>= 1;}return res;}
ll gcd (ll a, ll b) { while (b) { a %= b,swap(a, b);}return a;}
void read(vector<int> &w, int n){w.resize(n);for(int i = 0; i < n; i++) cin>>w[i];}
void print(vector<int> &w){for(int i =0; i < sz(w); i++){if(i == sz(w) - 1) cout<<w[i]<<"\n";else cout<<w[i]<<" ";}}
int prodmod(vector<int> w);
int summod(vector<int> w);
///CUIDADO COM MAXN
#define N 300050

int n, m, q, k, v[N], ans;
pii w[N];
string s;
vector<pii> grafo[N];
int dp[N][2];
void solve(int x, int p){
	int sum = 0;
	vector<int> caras;
	for(auto v: grafo[x]){
		if(v.f==p) continue;
		solve(v.f, x);
		sum += dp[v.f][0];
		caras.pb(max(dp[v.f][1] + v.s, dp[v.f][0]) - dp[v.f][0]);
	}
	int sum1=0,sum2=0;
	sort(caras.rbegin(), caras.rend());
	for(int i = 0; i < min(k, sz(caras)); i++){
		sum1 += caras[i];
		if(i<k-1) sum2 += caras[i];
	}
	dp[x][0] = sum+sum1;
	dp[x][1] = sum+sum2;
}
int32_t main(){
	ios::sync_with_stdio(false); cin.tie(0);
	cin>>q;
	while(q--){
		cin>>n>>k;
		for(int i = 1, a, b, c; i <n;i++){
			cin>>a>>b>>c;
			grafo[a].pb({b, c});
			grafo[b].pb({a, c});
		}
		solve(1, 1);
		cout<<max(dp[1][0], dp[1][1])<<"\n";
		for(int i=1;i<=n;i++){
			dp[i][0]=dp[i][1]=0;
			grafo[i].clear();
		}
	}
}
/*
    CUIDADO COM MAXN, MOD, OVERFLOW
    >Colocar (>DEFINE INT LONG LONG<) no inicio do template
    >mod = (1e9 + 7), por padrao (NAO ESQUECER DE ALTERAR)
    >NAO ESQUECER DEFINE INT LONG LONG
    > N = 1 ?
*/
int summod(vector<int> w){int curr=0;for(int i=0;i<sz(w); i++){curr = (curr+w[i])%mod;if(curr<0)curr+=mod;}return curr;}
int prodmod(vector<int> w){int curr = 1;
for(int i = 0; i < sz(w); i++){if(w[i] >= mod) w[i] %= mod;curr = (curr * w[i]) % mod;if(curr < 0) curr += mod;}return curr;}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define M 1000000007
#define fastio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define endl "\n"
#define all(x) (x).begin(), (x).end()
#define tr(x,it) for(auto it = x.begin();it!=x.end();++it)
#define fpresent(x,c) ((c).find(x) != (c).end())  //set,map
#define present(c,x) (find(all(c),x) != (c).end()) //vector
#define pb push_back
#define mp make_pair
#define setv(ar, val) memset(ar, val, sizeof(ar));
#define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
    cerr << *it << " = " << a << endl;
    err(++it, args...);
}
const int N = 500005;
vector<pair<ll, ll> > g[N];
vector<vector<ll> > dp(N, vector<ll> (2));

ll k;

void dfs(int x, int par){
    ll take_k = 0, take_k_1 = 0, i;

    vector<pair<ll, ll> > vp;
    for(auto it: g[x]){
        if(it.first == par)
            continue;
        dfs(it.first, x);
        vp.pb({it.second + dp[it.first][1] - dp[it.first][0], it.first});
    }
    sort(vp.rbegin(), vp.rend());

    for(i=0;i<k and i < vp.size();i++){
        take_k += vp[i].first + dp[vp[i].second][0];
        if(i<k-1)
            take_k_1 += vp[i].first + dp[vp[i].second][0];
        else
            take_k_1 += dp[vp[i].second][0];
    }
    for(;i<vp.size();i++){
        take_k += dp[vp[i].second][0];
        take_k_1 += dp[vp[i].second][0];
    }
    dp[x][0] = take_k;
    dp[x][1] = take_k_1;
}

int main()
{
    // freopen("input.in","r",stdin);
    // freopen("output.in","w",stdout);
    fastio;
    cin.tie(NULL);
    ll n,m,q,u,v,w,i,j,ans=0;
    cin>>q;

    while(q--){
        cin>>n>>k;
        for(i=0;i<n-1;i++){
            cin>>u>>v>>w;
            g[u].pb({v, w});
            g[v].pb({u, w});
        }

        dfs(1, -1);
        cout<<max(dp[1][0], dp[1][1])<<endl;

        for(i=1;i<=n;i++)
            g[i].clear(), dp[i][0] = 0, dp[i][1] = 0;
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define M 1000000007
#define fastio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define endl "\n"
#define all(x) (x).begin(), (x).end()
#define tr(x,it) for(auto it = x.begin();it!=x.end();++it)
#define fpresent(x,c) ((c).find(x) != (c).end())  //set,map
#define present(c,x) (find(all(c),x) != (c).end()) //vector
#define pb push_back
#define mp make_pair
#define setv(ar, val) memset(ar, val, sizeof(ar));
#define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
    cerr << *it << " = " << a << endl;
    err(++it, args...);
}
const int N = 500005;
vector<pair<ll, ll> > g[N];
vector<vector<ll> > dp(N, vector<ll> (2));

ll k;

void dfs(int x, int par){
    ll take_k = 0, take_k_1 = 0, i;

    vector<pair<ll, ll> > vp;
    for(auto it: g[x]){
        if(it.first == par)
            continue;
        dfs(it.first, x);
        vp.pb({it.second + dp[it.first][1], dp[it.first][0]});
    }
    sort(vp.rbegin(), vp.rend(), [](pair<ll, ll> &l, pair<ll, ll> &r){
        return l.first + r.second < l.second + r.first;
    });

    for(i=0;i<k and i < vp.size();i++){
        take_k += vp[i].first;
        if(i<k-1)
            take_k_1 += vp[i].first;
        else
            take_k_1 += vp[i].second;
    }
    for(;i<vp.size();i++){
        take_k += vp[i].second;
        take_k_1 += vp[i].second;
    }
    dp[x][0] = take_k;
    dp[x][1] = take_k_1;
}

int main()
{
    // freopen("input.in","r",stdin);
    // freopen("output.in","w",stdout);
    fastio;
    cin.tie(NULL);
    ll n,m,q,u,v,w,i,j,ans=0;
    cin>>q;

    while(q--){
        cin>>n>>k;
        for(i=0;i<n-1;i++){
            cin>>u>>v>>w;
            g[u].pb({v, w});
            g[v].pb({u, w});
        }

        dfs(1, -1);
        cout<<max(dp[1][0], dp[1][1])<<endl;

        for(i=1;i<=n;i++)
            g[i].clear(), dp[i][0] = 0, dp[i][1] = 0;
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define M 1000000007
#define fastio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define endl "\n"
#define all(x) (x).begin(), (x).end()
#define tr(x,it) for(auto it = x.begin();it!=x.end();++it)
#define fpresent(x,c) ((c).find(x) != (c).end())  //set,map
#define present(c,x) (find(all(c),x) != (c).end()) //vector
#define pb push_back
#define mp make_pair
#define setv(ar, val) memset(ar, val, sizeof(ar));
#define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
    cerr << *it << " = " << a << endl;
    err(++it, args...);
}
const int N = 500005;
vector<pair<ll, ll> > g[N];
vector<vector<ll> > dp(N, vector<ll> (2));

ll k;

void dfs(int x, int par){
    ll take_k = 0, take_k_1 = 0, i;

    vector<pair<ll, ll> > vp;
    for(auto it: g[x]){
        if(it.first == par)
            continue;
        dfs(it.first, x);
        vp.pb({it.second + dp[it.first][1], dp[it.first][0]});
    }
    sort(vp.rbegin(), vp.rend(), [](pair<ll, ll> &l, pair<ll, ll> &r){
        return l.first + r.second < l.second + r.first;
    });

    for(i=0;i<k and i < vp.size();i++){
        take_k += max(vp[i].first, vp[i].second);
        if(i<k-1)
            take_k_1 += max(vp[i].first, vp[i].second);
        else
            take_k_1 += vp[i].second;
    }
    for(;i<vp.size();i++){
        take_k += vp[i].second;
        take_k_1 += vp[i].second;
    }
    dp[x][0] = take_k;
    dp[x][1] = take_k_1;
}

int main()
{
    // freopen("input.in","r",stdin);
    // freopen("output.in","w",stdout);
    fastio;
    cin.tie(NULL);
    ll n,m,q,u,v,w,i,j,ans=0;
    cin>>q;

    while(q--){
        cin>>n>>k;
        for(i=0;i<n-1;i++){
            cin>>u>>v>>w;
            g[u].pb({v, w});
            g[v].pb({u, w});
        }

        dfs(1, -1);
        cout<<max(dp[1][0], dp[1][1])<<endl;

        for(i=1;i<=n;i++)
            g[i].clear(), dp[i][0] = 0, dp[i][1] = 0;
    }

    return 0;
}


#pragma GCC optimize ("O3")
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 5e5 + 5, M = 2 * N + 2;

int in[N];
int head[N], nxt[M], to[M], cst[M], ne, n, k;

void init(){
    ne = 0;
    memset(in, 0, n * sizeof in[0]);
    memset(head, -1, n * sizeof head[0]);
}

void addEdge(int f, int t, int c){
    to[ne] = t;
    cst[ne] = c;
    nxt[ne] = head[f];
    head[f] = ne++;
}

void addBiEdge(int a, int b, int c){
    addEdge(a, b, c);
    addEdge(b, a, c);
}

pair<ll, ll> dfs(int u, int par){
    if(in[u] == 1 && u)
        return make_pair(0LL, 0LL);

    ll total = 0;
    vector<ll> tmp;

    for(int e = head[u] ; ~e ; e = nxt[e]){
        int v = to[e];
        if(v == par)continue;

        auto p = dfs(v, u);

        total += p.first;
        tmp.emplace_back((p.second + 1LL * cst[e]) - p.first);
    }
    sort(tmp.begin(), tmp.end());

    int hna = min((int)tmp.size(), k - 1);

    while(hna--){
        if(tmp.back() <= 0)break;
        total += tmp.back();
        tmp.pop_back();
    }

    ll tani = total;
    if(tmp.size() && tmp.back() > 0)tani += tmp.back();

    return make_pair(tani, total);
}

int main(){
    int t;
    scanf("%d", &t);
    while(t--){
        scanf("%d%d", &n, &k);
        init();
        for(int i = 1 ; i < n ; ++i){
            int u, v, c;
            scanf("%d%d%d", &u, &v, &c);
            addBiEdge(--u, --v, c);
            ++in[u], ++in[v];
        }
        auto p = dfs(0, -1);
        printf("%lld\n", max(p.first, p.second));
    }
}


#pragma GCC optimize ("O3")
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 5e5 + 5, M = 2 * N + 2;

int in[N];
int head[N], nxt[M], to[M], cst[M], ne, n, k;

void init(){
    ne = 0;
    memset(in, 0, n * sizeof in[0]);
    memset(head, -1, n * sizeof head[0]);
}

void addEdge(int f, int t, int c){
    to[ne] = t;
    cst[ne] = c;
    nxt[ne] = head[f];
    head[f] = ne++;
}

void addBiEdge(int a, int b, int c){
    addEdge(a, b, c);
    addEdge(b, a, c);
}

pair<ll, ll> dfs(int u, int par){
    if(in[u] == 1)
        return make_pair(0LL, 0LL);

    ll total = 0;
    vector<ll> tmp;

    for(int e = head[u] ; ~e ; e = nxt[e]){
        int v = to[e];
        if(v == par)continue;

        auto p = dfs(v, u);

        total += p.first;
        tmp.emplace_back(abs((p.second + 1LL * cst[e]) - p.first));
    }
    sort(tmp.begin(), tmp.end());

    int hna = min((int)tmp.size(), k - 1);

    while(hna--){
        total += tmp.back();
        tmp.pop_back();
    }

    ll tani = total;
    if(tmp.size())tani += tmp.back();

    return make_pair(tani, total);
}

int main(){
    int t;
    scanf("%d", &t);
    while(t--){
        scanf("%d%d", &n, &k);
        init();
        for(int i = 1 ; i < n ; ++i){
            int u, v, c;
            scanf("%d%d%d", &u, &v, &c);
            addBiEdge(--u, --v, c);
            ++in[u], ++in[v];
        }
        auto p = dfs(0, -1);
        printf("%lld\n", max(p.first, p.second));
    }
}


#pragma GCC optimize ("O3")
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 5e5 + 5, M = 2 * N + 2;

int in[N];
int head[N], nxt[M], to[M], cst[M], ne, n, k;

void init(){
    ne = 0;
    memset(in, 0, n * sizeof in[0]);
    memset(head, -1, n * sizeof head[0]);
}

void addEdge(int f, int t, int c){
    to[ne] = t;
    cst[ne] = c;
    nxt[ne] = head[f];
    head[f] = ne++;
}

void addBiEdge(int a, int b, int c){
    addEdge(a, b, c);
    addEdge(b, a, c);
}

pair<ll, ll> dfs(int u, int par){
    if(in[u] == 1 && u)
        return make_pair(0LL, 0LL);

    ll total = 0;
    vector<ll> tmp;

    for(int e = head[u] ; ~e ; e = nxt[e]){
        int v = to[e];
        if(v == par)continue;

        auto p = dfs(v, u);

        total += p.first;
        tmp.emplace_back((p.second + 1LL * cst[e]) - p.first);
    }
    sort(tmp.begin(), tmp.end());

    int hna = min((int)tmp.size(), k - 1);

    while(hna--){
        total += tmp.back();
        tmp.pop_back();
    }

    ll tani = total;
    if(tmp.size())tani += tmp.back();

    return make_pair(tani, total);
}

int main(){
    int t;
    scanf("%d", &t);
    while(t--){
        scanf("%d%d", &n, &k);
        init();
        for(int i = 1 ; i < n ; ++i){
            int u, v, c;
            scanf("%d%d%d", &u, &v, &c);
            addBiEdge(--u, --v, c);
            ++in[u], ++in[v];
        }
        auto p = dfs(0, -1);
        printf("%lld\n", max(p.first, p.second));
    }
}


#pragma GCC optimize ("O3")
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 5e5 + 5, M = 2 * N + 2;

int in[N];
int head[N], nxt[M], to[M], cst[M], ne, n, k;

void init(){
    ne = 0;
    memset(in, 0, n * sizeof in[0]);
    memset(head, -1, n * sizeof head[0]);
}

void addEdge(int f, int t, int c){
    to[ne] = t;
    cst[ne] = c;
    nxt[ne] = head[f];
    head[f] = ne++;
}

void addBiEdge(int a, int b, int c){
    addEdge(a, b, c);
    addEdge(b, a, c);
}

pair<ll, ll> dfs(int u, int par){
    if(in[u] == 1 && u)
        return make_pair(0LL, 0LL);

    ll total = 0;
    vector<ll> tmp;

    for(int e = head[u] ; ~e ; e = nxt[e]){
        int v = to[e];
        if(v == par)continue;

        auto p = dfs(v, u);

        total += p.first;
        tmp.emplace_back(abs((p.second + 1LL * cst[e]) - p.first));
    }
    sort(tmp.begin(), tmp.end());

    int hna = min((int)tmp.size(), k - 1);

    while(hna--){
        total += tmp.back();
        tmp.pop_back();
    }

    ll tani = total;
    if(tmp.size())tani += tmp.back();

    return make_pair(tani, total);
}

int main(){
    int t;
    scanf("%d", &t);
    while(t--){
        scanf("%d%d", &n, &k);
        init();
        for(int i = 1 ; i < n ; ++i){
            int u, v, c;
            scanf("%d%d%d", &u, &v, &c);
            addBiEdge(--u, --v, c);
            ++in[u], ++in[v];
        }
        auto p = dfs(0, -1);
        printf("%lld\n", max(p.first, p.second));
    }
}


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

struct edge {
	int u, v, w;
};

int f (int root, int parent, vector < vector < ll > > & dp, vector < vector < pair < int, int > > > & g, int k) {
	ll curr = 0;
	vector < ll > adds;
	for (pair < int, int > p : g[root]) {
		int v = p.first, w = p.second;
		if (parent == v) continue;
		f (v, root, dp, g, k);
		curr += dp[v][0];
		adds.push_back (dp[v][1] + w - dp[v][0]);
	}
	sort (adds.begin(), adds.end(), greater < ll > ());
	for (int i = 0; i < min (k, (int)adds.size()); i++) {
		if (adds[i] > 0) curr += adds[i];
	}
	dp[root][0] = dp[root][1] = curr;
	if (k <= adds.size() && adds[k - 1] > 0) {
		dp[root][1] -= adds[k - 1];
	}
}

int main() {
	int q, n, k, u, v, w;
	cin >> q;
	while (q--) {
		cin >> n >> k;
		vector < vector < pair < int, int > > > g (n);
		for (int i = 1; i < n; i++) {
			cin >> u >> v >> w;
			u--, v--;
			g[u].push_back ({v, w});
			g[v].push_back ({u, w});
		}
		vector < vector < ll > > dp (n, vector < ll > (2));
		f (0, -1, dp, g, k);
		cout << dp[0][0] << endl;
	}
	return 0;
}


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

struct edge {
	int u, v, w;
};

int f (int root, int parent, vector < vector < ll > > & dp, vector < vector < pair < int, int > > > & g, int k) {
	ll curr = 0;
	vector < ll > adds;
	for (pair < int, int > p : g[root]) {
		int v = p.first, w = p.second;
		if (parent == v) continue;
		f (v, root, dp, g, k);
		curr += dp[v][0];
		adds.push_back (dp[v][1] + w - dp[v][0]);
	}
	sort (adds.begin(), adds.end(), [&] (const int & a, const int & b) {
		return a > b;
	});
	for (int i = 0; i < min (k, (int)adds.size()); i++) {
		if (adds[i] > 0) curr += adds[i];
	}
	dp[root][0] = dp[root][1] = curr;
	if (k <= adds.size() && adds[k - 1] > 0) {
		dp[root][1] -= adds[k - 1];
	}
}

int main() {
	int q, n, k, u, v, w;
	cin >> q;
	while (q--) {
		cin >> n >> k;
		vector < vector < pair < int, int > > > g (n);
		for (int i = 1; i < n; i++) {
			cin >> u >> v >> w;
			u--, v--;
			g[u].push_back ({v, w});
			g[v].push_back ({u, w});
		}
		vector < vector < ll > > dp (n, vector < ll > (2));
		f (0, -1, dp, g, k);
		cout << dp[0][0] << endl;
	}
	return 0;
}


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

const int N = 500005;

vector < pair < int, int > > g[N];
ll dp[N][2];
int k;

int f (int root, int parent) {
	ll curr = 0;
	vector < ll > adds;
	for (pair < int, int > p : g[root]) {
		int v = p.first, w = p.second;
		if (parent == v) continue;
		f (v, root);
		curr += dp[v][0];
		adds.push_back (dp[v][1] + w - dp[v][0]);
	}
	sort (adds.begin(), adds.end(), greater < ll > ());
	for (int i = 0; i < min (k, (int)adds.size()); i++) {
		if (adds[i] > 0) curr += adds[i];
	}
	dp[root][0] = dp[root][1] = curr;
	if (k <= adds.size() && adds[k - 1] > 0) {
		dp[root][1] -= adds[k - 1];
	}
}

int main() {
	int q, n, u, v, w;
	cin >> q;
	while (q--) {
		cin >> n >> k;
		for (int i = 0; i < n; i++) g[i].clear();
		for (int i = 1; i < n; i++) {
			cin >> u >> v >> w;
			u--, v--;
			g[u].push_back ({v, w});
			g[v].push_back ({u, w});
		}
		f (0, -1);
		cout << dp[0][0] << endl;
	}
	return 0;
}


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

const int N = 500005;

vector < pair < int, int > > g[N];
ll dp[N][2];
int k;

int f (int root, int parent) {
	ll curr = 0;
	vector < ll > adds;
	for (pair < int, int > p : g[root]) {
		int v = p.first, w = p.second;
		if (parent == v) continue;
		f (v, root);
		curr += dp[v][0];
		adds.push_back (dp[v][1] + w - dp[v][0]);
	}
	sort (adds.begin(), adds.end(), greater < ll > ());
	for (int i = 0; i < min (k, (int)adds.size()); i++) {
		if (adds[i] > 0) curr += adds[i];
	}
	dp[root][0] = dp[root][1] = curr;
	if (k <= adds.size() && adds[k - 1] > 0) {
		dp[root][1] -= adds[k - 1];
	}
}

int main() {
	ios::sync_with_stdio(false); cin.tie(0);
	int q, n, u, v, w;
	cin >> q;
	while (q--) {
		cin >> n >> k;
		for (int i = 0; i < n; i++) g[i].clear();
		for (int i = 1; i < n; i++) {
			cin >> u >> v >> w;
			u--, v--;
			g[u].push_back ({v, w});
			g[v].push_back ({u, w});
		}
		f (0, -1);
		cout << dp[0][0] << endl;
	}
	return 0;
}


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

//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;

#define ll long long int
#define ui unsigned int
#define ull unsigned long long
#define mem(a,b) memset((a),b,sizeof(a))
#define pii pair<ll,int>
#define fs first
#define sc second
#define PI acos(0.0)

//int dx[]= {0,0,1,-1};/*4 side move*/
//int dy[]= {-1,1,0,0};/*4 side move*/
//int dx[]= {1,1,0,-1,-1,-1,0,1};/*8 side move*/
//int dy[]= {0,1,1,1,0,-1,-1,-1};/*8 side move*/
//int dx[]={1,1,2,2,-1,-1,-2,-2};/*night move*/
//int dy[]={2,-2,1,-1,2,-2,1,-1};/*night move*/

//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
//ios_base::sync_with_stdio(0); cin.tie(0);

#define mod 1000000007
#define inf 2e9
#define N 500050

struct node
{
    int u,v;
    ll w;
};
bool operator<(node xx,node yy)
{
    return xx.w>yy.w;
}
node arr[N];
int rem[N];
ll ans;
vector<int>vc[N],wt[N];
ll dp[N][2];
int n,k;

void dfs(int u,int pr=-1)
{
    if(vc[u].size()==1 && u!=1)
    {
        dp[u][0]=dp[u][1]=0;
        return;
    }
    vector<node>temp;
    node xx;
    for(int i=0; i<vc[u].size(); i++)
    {
        int v=vc[u][i];
        ll w=wt[u][i];
        if(v==pr) continue;
        dfs(v,u);
        xx.w=w-(dp[v][0]-dp[v][1]);
        xx.u=v,xx.v=w;
        temp.push_back(xx);
    }
    sort(temp.begin(),temp.end());
    int rem=k;
    for(int i=0; i<temp.size(); i++)
    {
        ll nw=temp[i].w;
        int x=temp[i].u;
        int w=temp[i].v;
        if(nw>0)
        {
            if(rem)
            {
                dp[u][0]+=w+dp[x][1];
                if(rem>1) dp[u][1]+=w+dp[x][1];
                rem--;
            }
            else
            {
                dp[u][0]+=dp[x][0];
                dp[u][1]+=dp[x][0];
            }

        }
        else
        {
            dp[u][0]+=dp[x][0];
            dp[u][1]+=dp[x][0];
        }
    }

}

int main()
{
    int q;
    scanf("%d",&q);
    while(q--)
    {
        int u,v,w;
        scanf("%d %d",&n,&k);
        for(int i=0; i<=n; i++)
        {
            vc[i].clear(),wt[i].clear();
            dp[i][0]=dp[i][1]=0;
        }
        for(int i=1; i<n; i++)
        {
            scanf("%d %d %lld",&arr[i].u,&arr[i].v,&arr[i].w);
            vc[arr[i].u].push_back(arr[i].v);
            vc[arr[i].v].push_back(arr[i].u);
            wt[arr[i].u].push_back(arr[i].w);
            wt[arr[i].v].push_back(arr[i].w);
        }
        ans=0;
        dfs(1);
        printf("%lld\n",dp[1][0]);
    }

}


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

//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;

#define ll long long int
#define ui unsigned int
#define ull unsigned long long
#define mem(a,b) memset((a),b,sizeof(a))
#define pii pair<ll,int>
#define fs first
#define sc second
#define PI acos(0.0)

//int dx[]= {0,0,1,-1};/*4 side move*/
//int dy[]= {-1,1,0,0};/*4 side move*/
//int dx[]= {1,1,0,-1,-1,-1,0,1};/*8 side move*/
//int dy[]= {0,1,1,1,0,-1,-1,-1};/*8 side move*/
//int dx[]={1,1,2,2,-1,-1,-2,-2};/*night move*/
//int dy[]={2,-2,1,-1,2,-2,1,-1};/*night move*/

//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
//ios_base::sync_with_stdio(0); cin.tie(0);

#define mod 1000000007
#define inf 2e9
#define N 500050

struct node
{
    int u,v;
    ll w;
};
bool operator<(node xx,node yy)
{
    return xx.w>yy.w;
}
node arr[N];
int rem[N];
ll ans;
vector<int>vc[N],wt[N];
ll dp[N][2];
int n,k;

void dfs(int u,int pr=-1)
{
    if(vc[u].size()==1 && u!=1)
    {
        dp[u][0]=dp[u][1]=0;
        return;
    }
    dp[u][0]=dp[u][1]=0;
    vector<ll>temp;
    for(int i=0; i<vc[u].size(); i++)
    {
        int v=vc[u][i];
        ll w=wt[u][i];
        if(v==pr) continue;
        dfs(v,u);
        dp[u][0]+=dp[v][0],dp[u][1]+=dp[v][0];
        temp.push_back(w-(dp[v][0]-dp[v][1]));
    }
    sort(temp.rbegin(),temp.rend());
    int rem=k;
    for(int i=0; i<temp.size(); i++)
    {
        ll nw=temp[i];
        if(nw>0)
        {
            if(rem)
            {
                dp[u][0]+=nw;
                if(rem>1) dp[u][1]+=nw;
                rem--;
            }
        }
    }
}

int main()
{
    int q;
    scanf("%d",&q);
    while(q--)
    {
        int u,v,w;
        scanf("%d %d",&n,&k);
        for(int i=0; i<=n; i++)
        {
            vc[i].clear(),wt[i].clear();
            dp[i][0]=dp[i][1]=0;
        }
        for(int i=1; i<n; i++)
        {
            scanf("%d %d %lld",&arr[i].u,&arr[i].v,&arr[i].w);
            vc[arr[i].u].push_back(arr[i].v);
            vc[arr[i].v].push_back(arr[i].u);
            wt[arr[i].u].push_back(arr[i].w);
            wt[arr[i].v].push_back(arr[i].w);
        }
        ans=0;
        dfs(1);
        printf("%lld\n",dp[1][0]);
    }

}


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

//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;

#define ll long long int
#define ui unsigned int
#define ull unsigned long long
#define mem(a,b) memset((a),b,sizeof(a))
#define pii pair<int,int>
#define pll pair<ll,ll>
#define fs first
#define sc second
#define PI acos(0.0)

//int dx[]= {0,0,1,-1};/*4 side move*/
//int dy[]= {-1,1,0,0};/*4 side move*/
//int dx[]= {1,1,0,-1,-1,-1,0,1};/*8 side move*/
//int dy[]= {0,1,1,1,0,-1,-1,-1};/*8 side move*/
//int dx[]={1,1,2,2,-1,-1,-2,-2};/*night move*/
//int dy[]={2,-2,1,-1,2,-2,1,-1};/*night move*/

//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
//ios_base::sync_with_stdio(0); cin.tie(0);

#define mod 1000000007
#define inf 2e9
#define N 500050

struct node
{
    int u,v;
    int w;
};
bool operator<(node xx,node yy)
{
    return xx.w>yy.w;
}
node arr[N];
int rem[N];

int main()
{
    int q;
    scanf("%d",&q);
    while(q--)
    {
        int n,k,u,v,w;
        scanf("%d %d",&n,&k);
        for(int i=1; i<n; i++)
        {
            scanf("%d %d %d",&arr[i].u,&arr[i].v,&arr[i].w);
        }
        sort(arr+1,arr+n);
        for(int i=1; i<=n; i++) rem[i]=k;
        ll ans=0;
        for(int i=1; i<n; i++)
        {
            int u=arr[i].u,v=arr[i].v,w=arr[i].w;
            if(rem[u] && rem[v])
            {
                ans+=w;
                rem[u]--;
                rem[v]--;
            }
        }
        printf("%lld\n",ans);
    }

}


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

//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;

#define ll long long int
#define ui unsigned int
#define ull unsigned long long
#define mem(a,b) memset((a),b,sizeof(a))
#define pii pair<ll,int>
#define fs first
#define sc second
#define PI acos(0.0)

//int dx[]= {0,0,1,-1};/*4 side move*/
//int dy[]= {-1,1,0,0};/*4 side move*/
//int dx[]= {1,1,0,-1,-1,-1,0,1};/*8 side move*/
//int dy[]= {0,1,1,1,0,-1,-1,-1};/*8 side move*/
//int dx[]={1,1,2,2,-1,-1,-2,-2};/*night move*/
//int dy[]={2,-2,1,-1,2,-2,1,-1};/*night move*/

//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
//ios_base::sync_with_stdio(0); cin.tie(0);

#define mod 1000000007
#define inf 2e9
#define N 500050

struct node
{
    int u,v;
    ll w;
};
bool operator<(node xx,node yy)
{
    return xx.w>yy.w;
}
node arr[N];
int rem[N];
ll ans;
vector<int>vc[N],wt[N];
ll dp[N][2];
int n,k;

void dfs(int u,int pr=-1)
{
    if(vc[u].size()==1 && u!=1)
    {
        dp[u][0]=dp[u][1]=0;
        return;
    }
    vector<node>temp;
    node xx;
    for(int i=0; i<vc[u].size(); i++)
    {
        int v=vc[u][i];
        ll w=wt[u][i];
        if(v==pr) continue;
        dfs(v,u);
        xx.w=w-(dp[v][0]-dp[v][1]);
        xx.u=v,xx.v=w;
        temp.push_back(xx);
    }
    sort(temp.begin(),temp.end());
    ll mx1=0,mx2=0;
    int rem=k;
    for(int i=0; i<temp.size(); i++)
    {
        ll nw=temp[i].w;
        int x=temp[i].u;
        int w=temp[i].v;
        if(nw>0)
        {
            if(rem)
            {
                ans+=dp[x][1];
                mx1+=w;
                if(rem>1) mx2+=w;
                rem--;
            }
            else
            {
                ans+=dp[x][0];
            }
        }
        else
        {
            ans+=dp[x][0];
        }
    }
    dp[u][0]=mx1,dp[u][1]=mx2;
    if(u==1) ans+=dp[u][0];

}

int main()
{
    int q;
    scanf("%d",&q);
    while(q--)
    {
        int u,v,w;
        scanf("%d %d",&n,&k);
        for(int i=0; i<=n; i++)
        {
            vc[i].clear(),wt[i].clear();
            dp[i][0]=dp[i][1]=0;
        }
        for(int i=1; i<n; i++)
        {
            scanf("%d %d %lld",&arr[i].u,&arr[i].v,&arr[i].w);
            vc[arr[i].u].push_back(arr[i].v);
            vc[arr[i].v].push_back(arr[i].u);
            wt[arr[i].u].push_back(arr[i].w);
            wt[arr[i].v].push_back(arr[i].w);
        }
        ans=0;
        dfs(1);
        printf("%lld\n",ans);
    }

}


#include<bits/stdc++.h>
#define FiOS ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define all(x) x.begin(),x.end()
#define pb push_back
#define MP make_pair
#define rep(i,a,b) for(long long i=a;i<b;i++)
#define ren(i,a,b) for(int i=a;i>=b;i--)
#define ff first
#define ss second
#define pll pair<long long int,long long int>
#define pii pair<int,int>
#define vll vector<long long int>
using namespace std;

typedef unsigned long long int ull;
typedef long long int ll;
typedef long double ld;
int k ;
pll dfs( int i , int par , vector<pii> adj[] ){
    vector<pair<ll,pll> > vt ;
    for(auto it: adj[i]){
        if( it.ff == par ) continue ;
        pll x = dfs(it.ff,i,adj) ;
        vt.pb({ it.ss , x }) ;
    }
    sort( all(vt) , []( pair<ll,pll>& a , pair<ll,pll>& b ){
        return a.ff+b.ss.ss-b.ss.ff > b.ff+a.ss.ss-a.ss.ff ;
    }  ) ;
    int ct = k ;
    ll tot = 0 ; ll a = 0,b = 0 ;
    for( auto it: vt ){
        if( ct == 0 || it.ss.ss >= it.ff+it.ss.ff ) {  tot += it.ss.ss ; }
        else {
            if( ct == 1 ){  a = it.ss.ss ; b = it.ss.ff+it.ff ;      }
            else  tot += it.ss.ff+it.ff ;
            --ct ;
        }
    }
    return {tot+a,tot+b} ;
}

void solve(){
  int n ;  cin>>n>>k ;   vector<pii> adj[n+1] ;   --n ;
  while(n--){
      int a,b,c ; cin>>a>>b>>c ; adj[a].pb({b,c}) ; adj[b].pb({a,c}) ;
  }
  cout<< dfs(1,-1,adj).ss<<endl;
}

int main(){

    FiOS
    int t=1;
    cin>>t;
    while(t--){
        solve() ;
    }

}


#include<bits/stdc++.h>
#define FiOS ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define all(x) x.begin(),x.end()
#define pb push_back
#define MP make_pair
#define rep(i,a,b) for(long long i=a;i<b;i++)
#define ren(i,a,b) for(int i=a;i>=b;i--)
#define ff first
#define ss second
#define pll pair<long long int,long long int>
#define pii pair<int,int>
#define vll vector<long long int>
using namespace std;

typedef unsigned long long int ull;
typedef long long int ll;
typedef long double ld;
int k ;
pll dfs( int i , int par , vector<pii> adj[] ){
    vector<pair<ll,pll> > vt ;
    for(auto it: adj[i]){
        if( it.ff == par ) continue ;
        pll x = dfs(it.ff,i,adj) ;
        vt.pb({ it.ss , x }) ;
    }
    sort( all(vt) , []( pair<ll,pll>& a , pair<ll,pll>& b ){
        return a.ff+a.ss.ss-a.ss.ff > b.ff+b.ss.ss-b.ss.ff ;
    }  ) ;
    int ct = k ;
    ll tot = 0 ; ll a = 0,b = 0 ;
    for( auto it: vt ){
        if( ct == 0 || it.ss.ss >= it.ff+it.ss.ff ) {  tot += it.ss.ss ; }
        else {
            if( ct == 1 ){  a = it.ss.ss ; b = it.ss.ff+it.ff ;      }
            else  tot += it.ss.ff+it.ff ;
            --ct ;
        }
    }
    return {tot+a,tot+b} ;
}

void solve(){
  int n ;  cin>>n>>k ;   vector<pii> adj[n+1] ;   --n ;
  while(n--){
      int a,b,c ; cin>>a>>b>>c ; adj[a].pb({b,c}) ; adj[b].pb({a,c}) ;
  }
  cout<< dfs(1,-1,adj).ss<<endl;
}

int main(){

    FiOS
    int t=1;
    cin>>t;
    while(t--){
        solve() ;
    }

}


#include<bits/stdc++.h>
using namespace std;
#define endl '\n'

struct edge{
	int x, y, w;
	edge(int _x, int _y, int _w) : x(_x), y(_y), w(_w) {}
};

bool operator < (edge a, edge b){ return a.w < b.w; }

const int maxNodos = 501000;
vector<int> V(maxNodos+1);
priority_queue<edge> pq;

int main(){
	ios_base::sync_with_stdio(0);cin.tie(NULL);
	int t,n,k,x,y,w,i;
	long long ans;
	cin>>t;
	while(t--){
		cin>>n>>k;
		for(i=1; i<=n; i++){ V[i] = 0; }
		for(i=1; i<n; i++){
			cin>>x>>y>>w;
			pq.push(edge(x,y,w));
		}
		ans = 0;
		while(!pq.empty()){
			edge a = pq.top();
			pq.pop();
			if(V[a.x] < k && V[a.y] < k){
				ans += a.w;
				V[a.x]++;
				V[a.y]++;
			}
		}
		cout<<ans<<endl;
	}
}


#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
typedef pair<long long,long long> par;

const int maxNodos = 501000;
vector<par> Grafo[maxNodos + 1];
vector<int> S2(maxNodos + 1);

int sortDescendente(int a, int b){ return a > b; }

par solver(int x, int k){
	S2[x] = 1;
	if(Grafo[x].size() == 0)
	return par(0,0);

	int cont = 0;
	par ans = par(0,0);
	vector<par> V(Grafo[x].size());
	vector<long long> S(Grafo[x].size());
	for(int i=0; i<Grafo[x].size(); i++){
		if(S2[Grafo[x][i].first] == 0){
			V[cont] = solver(Grafo[x][i].first, k);
			ans.second += V[cont].first;
			S[cont] = Grafo[x][i].second - (V[cont].first - V[cont].second);
			cont++;
		}
	}
	sort(S.begin(), S.begin()+cont, sortDescendente);
	for(int i=0; i<(k-1) && i<cont; i++){
		if(S[i] <= 0)
		break;
		ans.second += S[i];
	}
	ans.first = ans.second;
	if(cont >= k && S[k-1] > 0)
	ans.first += S[k-1];

	return ans;
}

int main(){
	ios_base::sync_with_stdio(0);cin.tie(NULL);
	int t,n,k,x,y,w,i;
	long long ans;
	cin>>t;
	while(t--){
		cin>>n>>k;
		for(i=1; i<=n; i++){ S2[i] = 0; }
		for(i=1; i<n; i++){
			cin>>x>>y>>w;
			Grafo[x].push_back(par(y,w));
			Grafo[y].push_back(par(x,w));
		}
		par a = solver(1,k);
		ans = a.first;
		cout<<ans<<endl;
		for(i=1; i<=n; i++){ Grafo[i].clear(); }
	}
}


#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
typedef pair<long long,long long> par;

const int maxNodos = 501000;
vector<par> Grafo[maxNodos + 1];
vector<int> S2(maxNodos + 1);

int sortDescendente(int a, int b){ return a > b; }

par solver(int x, int k){
	S2[x] = 1;
	if(Grafo[x].size() == 0)
	return par(0,0);

	int cont = 0;
	par ans = par(0,0);
	vector<par> V(Grafo[x].size());
	vector<int> S(Grafo[x].size());
	for(int i=0; i<Grafo[x].size(); i++){
		if(S2[Grafo[x][i].first] == 0){
			V[cont] = solver(Grafo[x][i].first, k);
			ans.second += V[i].first;
			S[cont] = Grafo[x][i].second - (V[cont].first - V[cont].second);
			cont++;
		}
	}
	sort(S.begin(), S.begin()+cont, sortDescendente);
	for(int i=0; i<(k-1) && i<cont; i++){
		if(S[i] <= 0)
		break;
		ans.second += S[i];
	}
	ans.first = ans.second;
	if(cont >= k && S[k-1] > 0)
	ans.first += S[k-1];

	return ans;
}

int main(){
	ios_base::sync_with_stdio(0);cin.tie(NULL);
	int t,n,k,x,y,w,i;
	long long ans;
	cin>>t;
	while(t--){
		cin>>n>>k;
		for(i=1; i<=n; i++){ S2[i] = 0; }
		for(i=1; i<n; i++){
			cin>>x>>y>>w;
			Grafo[x].push_back(par(y,w));
			Grafo[y].push_back(par(x,w));
		}
		par a = solver(1,k);
		ans = a.first;
		cout<<ans<<endl;
		for(i=1; i<=n; i++){ Grafo[i].clear(); }
	}
}


#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#define mp				make_pair
#define pb				push_back
#define all(a)			(a).begin(), (a).end()
#define sz(a)			a.size()
#define srt(a)			sort(all(a))
#define mem(a, h)		memset(a, (h), sizeof(a))
#define f 				first
#define s 				second
#define fore(i, b, e)	for(int i = b; i < e; i++)
#define forg(i, b, e, m)	for(int i = b; i < e; i+=m)
//int in(){int r=0,c;for(c=getchar();c<=32;c=getchar());if(c=='-') return -in();for(;c>32;r=(r<<1)+(r<<3)+c-'0',c=getchar());return r;}

using namespace std;
//using namespace __gnu_pbds;

typedef long long 		ll;
typedef unsigned long long 		ull;
typedef pair<int, int>  ii;
typedef vector<int>     vi;
typedef vector<ii>      vii;
typedef vector<ll>      vll;
//typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
//find_by_order kth largest  order_of_key <
const int tam = 100010;
const int MOD = 1e9+7;
const int MOD1 = 988244535;
const double EPS = 1e-9;
const double PI = acos(-1);
int kp;
vector<int> g[500050];
vector<int> gp[500050];
ll dp[2][500050];
ll f(int node, int par, int us)
{
	if(dp[us][node] != -1) return dp[us][node];
	int k = kp - us;
	vector<ll> reses;
	ll a, b;
	ll res = 0;
	fore(i, 0, g[node].size())
	{
		if(g[node][i] != par)
		{
			a = f(g[node][i], node, 0);
			b = f(g[node][i], node, 1) + gp[node][i];
			res += a;
			reses.pb(b - a);
		}
	}
	sort(reses.rbegin(), reses.rend());
	int po = 0;
	while(po < reses.size() && reses[po] > 0 && po < k)
	{
		res += reses[po];
		po++;
	}
	return dp[us][node] = res;
}
int main()
{
	ios::sync_with_stdio(0); cin.tie(0);
	//freopen("qwe.txt", "r", stdin);
	//freopen("asd.txt", "w", stdout);
	ll res;
	int cas;
	int n;
	cin>>cas;
	int a, b, w;
	while(cas--)
	{
		cin>>n>>kp;
		//cout<<'#'<<kp<<'\n';
		fore(i, 0, n) {g[i].clear(), gp[i].clear();dp[0][i] = dp[1][i] = -1;}
		fore(i, 0, n-1)
		{
			cin>>a>>b>>w;
			a--; b--;
			g[a].pb(b);
			g[b].pb(a);
			gp[a].pb(w);
			gp[b].pb(w);
		}
		cout<<f(0, -1, 0)<<'\n';
	}
	return 0;
}

// read the question correctly (is y a vowel? what are the exact constraints?)
// look out for SPECIAL CASES (n=1?) and overflow (ll vs int?) ARRAY OUT OF BOUNDSS2


#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#define mp				make_pair
#define pb				push_back
#define all(a)			(a).begin(), (a).end()
#define sz(a)			a.size()
#define srt(a)			sort(all(a))
#define mem(a, h)		memset(a, (h), sizeof(a))
#define f 				first
#define s 				second
#define fore(i, b, e)	for(int i = b; i < e; i++)
#define forg(i, b, e, m)	for(int i = b; i < e; i+=m)
//int in(){int r=0,c;for(c=getchar();c<=32;c=getchar());if(c=='-') return -in();for(;c>32;r=(r<<1)+(r<<3)+c-'0',c=getchar());return r;}

using namespace std;
//using namespace __gnu_pbds;

typedef long long 		ll;
typedef unsigned long long 		ull;
typedef pair<int, int>  ii;
typedef vector<int>     vi;
typedef vector<ii>      vii;
typedef vector<ll>      vll;
//typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
//find_by_order kth largest  order_of_key <
const int tam = 100010;
const int MOD = 1e9+7;
const int MOD1 = 988244535;
const double EPS = 1e-9;
const double PI = acos(-1);
int ver[500050];
pair<int, pair<int,int>> ar[500050];
int main()
{
	ios::sync_with_stdio(0); cin.tie(0);
	//freopen("qwe.txt", "r", stdin);
	//freopen("asd.txt", "w", stdout);
	ll res;
	int cas;
	int n, k;
	cin>>cas;
	while(cas--)
	{
		cin>>n>>k;
		fore(i, 0, n-1) {cin>>ar[i].s.f>>ar[i].s.s>>ar[i].f; ar[i].s.f--; ar[i].s.s--;}
		sort(ar, ar + n-1);
		reverse(ar, ar + n-1);
		fore(i, 0, n) ver[i] = 0;
		res = 0;
		fore(i, 0, n-1)
		{
			if(ver[ar[i].s.f] < k && ver[ar[i].s.s] < k)
			{
				res += ar[i].f;
				ver[ar[i].s.f]++;
				ver[ar[i].s.s]++;
			}
		}
		cout<<res<<'\n';
	}
	return 0;
}

// read the question correctly (is y a vowel? what are the exact constraints?)
// look out for SPECIAL CASES (n=1?) and overflow (ll vs int?) ARRAY OUT OF BOUNDSS2