Help me in solving PMD7 problem

My issue

Why power = power % 4
why it has picked 4 only

My code


/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
    public static void main (String[] args) throws java.lang.Exception
    {
        // your code goes here
        Scanner sc = new Scanner(System.in);
        int t=sc.nextInt();
        while(t-- >0)
        {
            long n=sc.nextLong();
            long m=sc.nextLong();
            long num=0;
            double power=m%4;
            if(power==0 && m!=0)
                power = 4;
            while(n!=0)
            {
                int digit = (int) Math.pow(n%10,power);
                digit = digit%10;
                num=num*10+digit;
                n=n/10;
            }
            if(num%7==0)
                System.out.println("YES");
            else
                System.out.println("NO");
            
        }
    }
}

Learning course: Prepare for your DSA interviews
Problem Link: Power M divisibility by 7 Practice Problem in - CodeChef

@rutikraj
here , plzz refer my c++ code for better understanding of the logic

#include <bits/stdc++.h>
using namespace std;

int main() {
	// your code goes here
	int t;
	cin>>t;
	while(t--)
	{
	    string x,s1;
	    cin>>x;
	    int m;
	    cin>>m;
	    for(int i=0;i<x.size();i++)
	    {
	        int val=x[i]-'0';
	        int tm=1;
	        for(int j=0;j<m;j++)
	        {
	            tm=(tm*val)%10;
	        }
	        s1+=(tm+'0');
	    }
	    reverse(s1.begin(),s1.end());
	    int an=stoi(s1);
	    if(an%7==0)
	    cout<<"YES";
	    else
	    cout<<"NO";
	    cout<<endl;
	}
	return 0;
}