#include<bits/stdc++.h>
using namespace std;
const long long mod=1e9+7;
long long fastpower(long long a,long long b){
long long ans=1;
while(b){
if(b&1) ans=ans*a%mod;
a=a*a%mod;
b/=2;
}
return ans;
}
long long f[1000010];
long long inv3;
void solve(){
string s,t;
int k;
cin>>s>>t>>k;
int n=max(t.size(),s.size());
vector<int> a,b,c;
int d[n+10];
for(int i=s.size()-1;i>=0;i--) a.push_back(s[i]-'0');
for(int i=t.size()-1;i>=0;i--) b.push_back(t[i]-'0');
for(int i=0;i<n;i++){
if(i>=a.size()) a.push_back(0);
if(i>=b.size()) b.push_back(0);
c.push_back(a[i]^b[i]);
}
int cnt=0,j=0;
for(int i=0;i<c.size();i+=2){
if(i+1==c.size()) c.push_back(0);
if(c[i]==1&&c[i+1]==0) d[j]=1;
if(c[i]==1&&c[i+1]==1) d[j]=2;
if(c[i]==0&&c[i+1]==1) d[j]=3;
if(c[i]==0&&c[i+1]==0) d[j]=4;
if(d[j]&&!cnt) cnt=i/2+1;
j++;
}
if(cnt==0){
if(k-1>=1000000) cout<<(fastpower(4,k)-4)*inv3%mod<<endl;
else cout<<(f[k-1]-1+mod)%mod<<endl;
return ;
}
if(k>cnt) {
cout<<-1<<endl;
return ;
}
long long ans=0;
ans=(fastpower(4,k)-4)*inv3%mod;
for(int i=0;i<d.size();i++){
ans=(ans+d[i]*f[i]%mod)%mod;
}
cout<<ans<<endl;
}
int main(){
inv3=fastpower(3,mod-2);
f[0]=1;
for(int i=1;i<=1000000;i++){
f[i]=(f[i-1]*4%mod+1)%mod;
}
int t;
cin>>t;
while(t--){
solve();
}
return 0;
}