#include <bits/stdc++.h>
// #include <atcoder/all>
// using namespace atcoder;
#define rep(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define rep2(i, s, n) for (ll i = s; i < (ll)(n); i++)
#define repr(i, n) for (ll i = n; i >= 0; i--)
#define pb push_back
#define COUT(x) cout << (x) << "\n"
#define COUTF(x) cout << setprecision(15) << (x) << "\n"
#define ENDL cout << "\n"
#define DF(x) x.erase(x.begin())
#define ALL(x) x.begin(), x.end()
#define SORT(x) sort(ALL(x))
#define RSORT(x) sort(x.rbegin(), x.rend())
#define REVERSE(x) reverse(ALL(x))
#define MAX(x) *max_element(ALL(x))
#define MAXI(x) max_element(ALL(x)) - x.begin()
#define SUM(x) accumulate(ALL(x), 0ll)
#define COUNT(x, y) count(ALL(x), y);
#define ANS cout << ans << "\n"
#define YES cout << "YES\n";
#define NO cout << "NO\n";
#define Yes cout << "Yes\n";
#define No cout << "No\n";
#define init() \
  cin.tie(0);  \
  ios::sync_with_stdio(false)
#define debug(x) cerr << "[debug] " << #x << ": " << x << endl;
#define debugV(v)                       \
  cerr << "[debugV] " << #v << ":";     \
  rep(z, v.size()) cerr << " " << v[z]; \
  cerr << endl;
using namespace std;
using ll = long long;
using ld = long double;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using mll = map<ll, ll>;
using qll = queue<ll>;
using P = pair<ll, ll>;
using vp = vector<P>;
using vs = vector<string>;

template <typename T>
inline istream& operator>>(istream& i, vector<T>& v) {
  rep(j, v.size()) i >> v[j];
  return i;
}

template <typename T1, typename T2>
inline istream& operator>>(istream& i, pair<T1, T2>& v) {
  return i >> v.first >> v.second;
}

constexpr ll INF = 0x3f3f3f3f3f3f3f3f;
constexpr ld PI = 3.141592653589793238462643383279;
ll get_digit(ll x) {
  return to_string(x).size();
}

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

ll lcm(ll a, ll b) {
  return a / gcd(a, b) * b;
}

template <class T>
void chmax(T& a, const T& b) {
  if (a < b) a = b;
}

template <class T>
void chmin(T& a, const T& b) {
  if (b < a) a = b;
}

ll mod_pow(ll x, ll n, ll mod) {
  if (n == 0) return 1;
  ll res = mod_pow(x * x % mod, n / 2, mod);
  if (n & 1) res *= x;
  res %= mod;
  return res;
}

inline ll mod(ll a, ll m) {
  return (a % m + m) % m;
}

ll ext_gcd(ll a, ll b, ll& p, ll& q) {
  if (b == 0) {
    p = 1;
    q = 0;
    return a;
  }
  ll d = ext_gcd(b, a % b, q, p);
  q -= a / b * p;
  return d;
}

P crt(ll b1, ll m1, ll b2, ll m2) {
  ll p, q;
  ll d = ext_gcd(m1, m2, p, q);
  if ((b2 - b1) % d != 0) return {0, -1};
  ll m = lcm(m1, m2);
  ll tmp = (b2 - b1) / d * p % (m2 / d);
  ll r = mod(b1 + m1 * tmp, m);
  return make_pair(r, m);
}

signed main() {
  init();
  ll T;
  cin >> T;
  while (T--) {
    ll n, s, k;
    cin >> n >> s >> k;
    P p = crt(0, n, s % k, k);
    if (p.first == 0) p.first += p.second;
    ll ans = (p.first - s) / k;
    if (p.second == -1) ans = -1;
    ANS;
  }
  return 0;
}
#include<bits/stdc++.h>

using namespace std;
 
#define  GODSPEED       ios:: sync_with_stdio(0);cin.tie(0);cout.tie(0);cout<<fixed;cout<<setprecision(15);
#define  f              first               
#define  s              second
#define  newl           cout<<"\n";
#define  pb             push_back  
#define  mset(a,x)      memset(a,x,sizeof(a))
#define  debv(a)        for(auto it: a)cout<<it<<" ";newl;
#define  deb1(a)        cout<<a<<"\n";
#define  deb2(a,b)      cout<<a<<" "<<b<<"\n";
#define  deb3(a,b,c)    cout<<a<<" "<<b<<" "<<c<<"\n";
#define  deb4(a,b,c,d)  cout<<a<<" "<<b<<" "<<c<<" "<<d<<"\n";
#define  uniq(a)        a.resize(unique(a.begin(), a.end()) - a.begin());
#define  all(a)         a.begin(),a.end()           
 
typedef  long long             ll;
typedef  long double           ld;
typedef  pair<ll,ll>           pll;
typedef  vector<ll>            vll;
typedef  vector<pll>           vpll;

const ll   N     =  2e5+5;
const ll   mod   =  998244353;
const ll   INF   =  0x7f7f7f7f7f7f7f7f;
const int  INFi  =  0x7f7f7f7f;
const ll   LEVEL =  log2(N)+1;

ll n;
string s;

void solve(){
	cin >> n >> s;
	if(s == "1"){
		deb1((ll)2e10)
		return;
	}
	if(s == "0"){
		deb1((ll)1e10)
		return;
	}
	string t = "", init = "110";
	for(int i = 0; i < n; i++){
		t += init[i%3];
	}
	if(s == t){
		deb1((ll) (3e10 - n + 3) / 3)
		return;
	}
	t = "";
	for(int i = 0; i < n; i++){
		t += init[(i + 1)%3];
	}
	if(s == t){
		deb1((ll) (3e10 - n + 2) / 3)
		return;
	}
	t = "";
	for(int i = 0; i < n; i++){
		t += init[(i + 2)%3];
	}
	if(s == t){
		deb1((ll) (3e10 - n + 1) / 3)
		return;
	}
	deb1(0)
}

int main(){
	GODSPEED;
	int test = 1;
	//cin >> test;
	for(int i = 1; i <= test; i++){
		solve();
	}
	#ifndef ONLINE_JUDGE
	cout<<"\nTime Elapsed: " << 1.0*clock() / CLOCKS_PER_SEC << " sec\n";
	#endif
}