You are given two integers $N$ and $M$. Find the number of sequences $A_1, A_2, \ldots, A_N$, where each element is an integer between $1$ and $M$ (inclusive) and no three consecutive elements are equal. Since this number could be very large, compute it modulo $10^9+7$.

-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first and only line of each test case contains two space-separated integers $N$ and $M$.

-----Output-----
For each test case, print a single line containing one integer ― the number of valid sequences modulo $10^9+7$.

-----Constraints-----
- $1 \le T \le 10^5$
- $1 \le N, M \le 10^{18}$

-----Subtasks-----
Subtask #1 (50 points):
- $T \le 20$
- $N \le 10^5$
Subtask #2 (50 points): original constraints

-----Example Input-----
2 
2 2
3 4

-----Example Output-----
4
60

```python

USER_FILENAME = "<user_code>"
import sys, json, builtins
import math, heapq, bisect, itertools, functools, collections, statistics, fractions, decimal, operator, array, random, re, string, os, pathlib
try:
    import resource as _res
except Exception:
    _res = None
from collections import Counter, defaultdict, deque, OrderedDict
from functools import lru_cache, reduce
from itertools import accumulate, permutations, combinations, product
from math import gcd, ceil, floor, sqrt
from decimal import Decimal
try:
    from typing import *
except Exception:
    pass
try:
    sys.setrecursionlimit(1_000_000)
except Exception:
    pass
try:
    import site as _site
    try:
        _sys_sites = _site.getsitepackages() if hasattr(_site, "getsitepackages") else []
        for _p in _sys_sites:
            if _p and _p not in sys.path:
                sys.path.append(_p)
    except Exception:
        pass
    try:
        _usp = _site.getusersitepackages()
        if isinstance(_usp, str):
            _usp = [_usp]
        for _p in _usp:
            if _p and _p not in sys.path:
                sys.path.append(_p)
    except Exception:
        pass
except Exception:
    pass

ns = {"__name__": "__main__"}
for _k, _v in list(globals().items()):
    if not _k.startswith("_"):
        ns[_k] = _v
_CODE = "import sys\ndef multiply(matrix1,matrix2,mod):\n ans=[[-1 for i in range(len(matrix2[0]))] for _ in range(len(matrix1))]\n #print(ans,'before')\n for i in range(len(matrix1)):\n  for j in range(len(matrix2[0])):\n   s=0\n   k=0\n   while k<len(matrix1[0]):\n    s+=(matrix1[i][k]*matrix2[k][j])%mod\n    k+=1\n   ans[i][j]=(s%mod)\n #print(ans,'ans')\n return ans\ndef power(a,b,mod):\n res=[[1,0],[0,1]]\n #print(res,'res')\n for i in range(len(a)):\n  for j in range(len(a[0])):\n   a[i][j]=a[i][j]%mod\n #print(a,'a')\n while b>0:\n  if (b&1):\n   res=multiply(res,a,mod)\n  b=b>>1\n  a=multiply(a,a,mod)\n return res\nt=int(sys.stdin.readline())\nmod=10**9+7\nfor _ in range(t):\n n,m=list(map(int,sys.stdin.readline().split()))\n base=[[m],[0]]\n a=[[m-1,m-1],[1,0]]\n #print(base,'base')\n #print(a,'a')\n ans=power(a,n-1,mod)\n #print(ans,'ans')\n ans=multiply(ans,base,mod)\n print((ans[0][0]+ans[1][0])%mod)\n \n   \n"
_co = compile(_CODE, USER_FILENAME, "exec")
exec(_co, ns, ns)

```

Measured memory_bytes: 13545