#include<iostream>
#include<iomanip>
#include<vector>
#include<algorithm>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<queue>
#include<cstring>
#include<tuple>
#define rep(i,n) for(int i = 0;i < (n);i++)
using namespace std;
using ll = long long;
const int mod = 1e9 + 7;
// {g,x y} : ax * by = g
tuple<ll,ll,ll> extgcd(ll a,ll b){
    if(b == 0) return {a,1,0};
    ll g,x,y;
    tie(g,x,y) = extgcd(b, a % b);
    return {g,y,x - a / b * y};
}
void solve(){
    int n,s,k;
    cin >> n >> s >> k;
    ll g,x,y;
    tie(g,x,y) = extgcd(k,n);
    if(s % g != 0){
        cout << -1 <<endl;
        return;
    }
    n /= g;
    s /= g;
    k /= g;
    ll ans = ((x* - s) % n + n) % n;
    cout << ans << endl;
}
int main(){
    int t;
    cin >> t;
    rep(i,t){
        solve();
    }
    return 0;
    
}
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;

int dp[101][505001];

int main(){
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	int n,k,mod; cin >> n >> k >> mod;
	dp[0][0]=1;
	int sum=0;
	for(int i=1;i<=n;i++){
		sum+=i;
		vector<int> ps(i);
		for(int j=0;j<=sum*k;j++){
			int x=j%i;
			(ps[x]+=dp[i-1][j])%=mod;
			if(j-i*(k+1)>=0) (ps[x]-=dp[i-1][j-i*(k+1)])%=mod;
			dp[i][j]=ps[x];
			if(dp[i][j]<0)dp[i][j]+=mod;
		}
	}
	sum=0;
	for(int i=1;i<=n;i++){
		sum+=i;
		ll res=0;
		for(int j=0;j<=sum*k;j++){
			(res+=(ll)dp[i-1][j]*(ll)dp[n-i][j]%mod)%=mod;
		}
		res=res*(k+1)-1;
		res%=mod;
		if(res<0)res+=mod;
		cout << res << "\n";
	}
}