#line 1 "test/01_Math/01_NumberTheory/04.01_yukicoder-1287.test.cpp" #define PROBLEM "https://yukicoder.me/problems/no/1287" #line 1 "template/template.cpp" #include using namespace std; #line 4 "test/01_Math/01_NumberTheory/04.01_yukicoder-1287.test.cpp" #line 1 "01_Math/01_NumberTheory/04.01_ext-gcd.cpp" /** * @brief 拡張 Euclid 互助法 * @docs docs/ext-gcd.md */ long long ext_gcd(long long a, long long b, long long& x, long long& y) { if (b == 0) { x = 1; y = 0; return a; } auto q = a / b; auto g = ext_gcd(b, a - q * b , x, y); auto z = x - q * y; x = y; y = z; return g; } #line 1 "01_Math/02_Combinatorics/01.01_mod-pow.cpp" /** * @brief 累乗 * @docs docs/mod-pow.md */ long long mod_pow(long long a, long long n, long long m) { a %= m; if (a < 0) a += m; long long res = 1 % m; while (n) { if (n & 1) res = res * a % m; a = a * a % m; n >>= 1; } return res; } #line 7 "test/01_Math/01_NumberTheory/04.01_yukicoder-1287.test.cpp" int main() { const long long MOD = 1000000007; int T; cin >> T; for (int i = 0; i < T; ++i) { long long X, K, x, y; cin >> X >> K; ext_gcd(MOD - 1, K, x, y); y %= (MOD - 1); if (y < 0) y += MOD - 1; cout << mod_pow(X, y, MOD) << endl; } }