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

using namespace std;

const int MOD = 998244353;

class Solution {
public:
    void execute(int test, istream &in) {
        int n;
        string s, t;
        in >> n >> s >> t;
        int i = 0, j = 0;
        int cnts = 0, cntt = 0;
        int answer = 0;
        while (i < n || j < n) {
            while (i < n && s[i] != '0')
                i++;
            while (j < n && t[j] != '0')
                j++;
            if (i < n && j < n) {
                if (i != j)
                    answer++;
            }
            if (i < n) {
                cnts++;
                i++;
            }
            if (j < n) {
                cntt++;
                j++;
            }
        }
        if (cnts != cntt)
            answer = -1;
        printf("%d\n", answer);
    }
};

int main(int argc, char* argv[])
{
#ifdef VLAD_LOCAL
    //FILE *f = fopen("in.txt", "r");
    ifstream f("in.txt");
    auto start = std::chrono::steady_clock::now();
#else
    //FILE *f = stdin;
    istream &f = cin;
#endif
    //std::ios_base::sync_with_stdio(false);
    int tests = 1;
    //f >> tests;
    for (int test = 0; test < tests; test++) {
        Solution sol;
        sol.execute(test, f);
    }
#ifdef VLAD_LOCAL
    auto end = std::chrono::steady_clock::now();
    double seconds = (double)std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() / 1000;
    printf("\n%.3lf seconds\n", seconds);
#endif
    return 0;
}

#include <bits/stdc++.h> 
using namespace std;
#define ll long long
#define ld long double
#define PI 3.141592653L
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
const ll INF = (ll)1e18;
const int N = 3e5 + 5;
const ll MOD = 1e9+7;

int parent[N];
const int dx[4] = {+1, 0, -1, 0};
const int dy[4] = {0, -1, 0, +1};
 
ll gcd(ll a, ll b) { 
    if (a == 0) 
        return b;  
    return gcd(b % a, a);  
}

ll extGcd(ll a, ll b, ll &x, ll &y) { // solves for x and y where ax+by=gcd(a,b)
    if(b == 0) {
        x = 1;
        y = 0;
        return a;
    }
    ll d = extGcd(b, a % b, y, x);
    y -= a / b * x;
    return d;
}

ll modInv(ll a, ll p) { // solve for x=a^-1 where ax + py = 1 % p
    if(gcd(a, p) != 1) return -1; // modInv only exists if a and p are coprime (gcd(a, p) = 1)
    ll x, y;
    extGcd(a, p, x, y);
    x = (x % p + p) % p;
    return x;
}
   
ll lcm(ll a, ll b) {  
    return (a * b) / gcd(a,b);  
} 
 
ll modpow(ll a, ll n, ll mod) {
    ll res = 1;
    while (n > 0) {
        if (n & 1) res = (res * a) % mod;
        a = (a * a) % mod;
        n >>= 1;
    }
    return res;
}
 
int find(int x) {
    return x == parent[x] ? x : parent[x] = find(parent[x]);
}

void Union(int x, int y) {
    int xPar = find(x), yPar = find(y);
    if(xPar != yPar) parent[xPar] = yPar;
}
 
ll nCr(ll n, ll r) {
    ll res = 1;
    for(ll i=1;i<=r;i++) {
        res = res * (n - r + i) / i;
    }
    return res;
}
 
int ask(int i, int j) {
    cout << "? " << i + 1 << ' ' << j + 1 << endl;
    int v;
    cin >> v;
    return v;
}

int msbPos(int n) {
    if(n == 0) return 0;
    int msbPos = 0;
    n /= 2;
    while(n != 0) {
        n /= 2;
        msbPos++;
    }
    return msbPos;
}

string toOct(int n) {
    string res;
    while(n > 0) {
        res = to_string(n % 8) + res;
        n /= 8;
    }
    return res;
}

void solve() {
    ll n, s, k;
    cin >> n >> s >> k;
    ll c = n - s;
    ll d = gcd(gcd(k, c), n);
    k /= d;
    c /= d;
    n /= d;
    if(modInv(k, n) == -1) {
        cout << -1 << endl;
    }
    else {
        cout << (modInv(k, n) * c) % n << endl; // solving x = (b * a^-1) % n. here kx = (n - s) % n -> x = (k^-1 * (c = n - s)) % n;
    }
} 

int main()
{
    IOS
    int t;
    cin >> t;
    while(t--) solve();
    //solve();
    return 0; 
}