#include<bits/stdc++.h>
using namespace std;
constexpr int N=1000005;
constexpr int MOD=1000000007;
int qpow(int a,int b)
{
int res=1;
while(b)
{
if(b&1) res=(long long)res*a%MOD;
a=(long long)a*a%MOD,b>>=1;
}
return res;
}
int getinv(int x)
{
return qpow(x,MOD-2);
}
string a,b;
int k;
void solve()
{
cin>>a>>b>>k;
reverse(a.begin(),a.end());
reverse(b.begin(),b.end());
string s=a;
while(s.size()<b.size()) s+='0';
for(int i=0;i<(int)s.size();i++)
if(i<(int)b.size()&&b[i]=='1') s[i]=s[i]=='0'?'1':'0';
while((int)s.size()>1&&s.back()=='0') s.pop_back();
if(s=="0")
{
int ans=(long long)(qpow(4,k)-4+MOD)*getinv(3)%MOD;
cout<<ans<<"\n";
return;
}
int cnt=0;
for(int i=0;i+1<(int)s.size();i+=2)
if(s[i]=='0'&&s[i+1]=='0') cnt++;
if(cnt<k-1)
{
cout<<-1<<"\n";
return;
}
if(s.size()%2==1) s+='0';
int pw=1;
int ans=0;
for(int i=0;i+1<(int)s.size();i+=2)
{
int c=0;
if(s[i]=='1'&&s[i+1]=='0') c=1;#include<bits/stdc++.h>
using namespace std;
constexpr int N=1000005;
constexpr int MOD=1000000007;
int qpow(int a,int b)
{
int res=1;
while(b)
{
if(b&1) res=(long long)res*a%MOD;
a=(long long)a*a%MOD,b>>=1;
}
return res;
}
int getinv(int x)
{
return qpow(x,MOD-2);
}
string a,b;
int k;
void solve()
{
cin>>a>>b>>k;
reverse(a.begin(),a.end());
reverse(b.begin(),b.end());
string s=a;
while(s.size()<b.size()) s+='0';
for(int i=0;i<(int)s.size();i++)
if(i<(int)b.size()&&b[i]=='1') s[i]=s[i]=='0'?'1':'0';
while((int)s.size()>1&&s.back()=='0') s.pop_back();
if(s=="0")
{
int ans=(long long)(qpow(4,k)-4+MOD)*getinv(3)%MOD;
cout<<ans<<"\n";
return;
}
int cnt=0;
for(int i=0;i+1<(int)s.size();i+=2)
if(s[i]=='0'&&s[i+1]=='0') cnt++;
if(cnt<k-1)
{
cout<<-1<<"\n";
return;
}
if(s.size()%2==1) s+='0';
int pw=1;
int ans=0;
for(int i=0;i+1<(int)s.size();i+=2)
{
int c=0;
if(s[i]=='1'&&s[i+1]=='0') c=1;
else if(s[i]=='1'&&s[i+1]=='1') c=2;
else if(s[i]=='0'&&s[i+1]=='1') c=3;
int val=(long long)(qpow(4,i/2+1)-1+MOD)*getinv(3)%MOD;
ans=(ans+(long long)c*val)%MOD;
}
ans=(ans+(long long)(qpow(4,k)-4+MOD)*getinv(3))%MOD;
cout<<ans<<"\n";
return;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr),cout.tie(nullptr);
int T;
cin>>T;
while(T--)
solve();
return 0;
}
else if(s[i]=='1'&&s[i+1]=='1') c=2;
else if(s[i]=='0'&&s[i+1]=='1') c=3;
int val=(long long)(qpow(4,i/2+1)-1+MOD)*getinv(3)%MOD;
ans=(ans+(long long)c*val)%MOD;
}
ans=(ans+(long long)(qpow(4,k)-4+MOD)*getinv(3))%MOD;
cout<<ans<<"\n";
return;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr),cout.tie(nullptr);
int T;
cin>>T;
while(T--)
solve();
return 0;
}