#include<map> #include<set> #include<cmath> #include<queue> #include<stack> #include<vector> #include<cstdio> #include<iomanip> #include<cstdlib> #include<cstring> #include<iostream> #include<algorithm> #define pi acos(-1) #define ll long long #define mod 1000000007 #define ls l,m,rt<<1 #define rs m+1,r,rt<<1|1 #pragma comment(linker, "/STACK:1024000000,1024000000") using namespace std; const double g=10.0,eps=1e-9; const int N=10+5,maxn=1<<10+5,inf=0x3f3f3f3f; ll s; struct Node{ ll row,col; ll a[N][N]; }; Node mul(Node x,Node y) { Node ans; ans.row=x.row,ans.col=y.col; memset(ans.a,0,sizeof ans.a); for(ll i=0;i<x.row;i++) for(ll j=0;j<x.col;j++) for(ll k=0;k<y.col;k++) ans.a[i][k]=(ans.a[i][k]+x.a[i][j]*y.a[j][k]+s)%s; return ans; } Node quick_mul(Node x,ll n) { Node ans; ans.row=x.row,ans.col=x.col; memset(ans.a,0,sizeof ans.a); for(ll i=0;i<ans.col;i++)ans.a[i][i]=1; while(n){ if(n&1)ans=mul(ans,x); x=mul(x,x); n>>=1; } return ans; } int main() { ios::sync_with_stdio(false); cin.tie(0); // cout<<setiosflags(ios::fixed)<<setprecision(2); ll t,n,m,a,b; cin>>t; while(t--){ cin>>a>>b>>n>>m; s=1; for(int i=1;i<=m;i++)s*=10; if(n==0) { cout<<a%m<<endl; continue; } Node A; A.row=2,A.col=2; A.a[0][0]=0,A.a[0][1]=1; A.a[1][0]=1,A.a[1][1]=1; Node B; B.row=2,B.col=1; B.a[0][0]=a,B.a[1][0]=b; B=mul(quick_mul(A,n),B); cout<<B.a[0][0]%s<<endl; } return 0; }