//
// Created by ttw on 2020/11/1.
//

#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <queue>

using namespace std;

typedef long long ll;

const int mod = 1e9 + 7;
const int maxn = 1e5 + 5;

int prime[maxn], tot;
bool vis[maxn];
void init () {
    for (int i = 2; i < maxn; i++) {
        if (!vis[i]) {
            prime[tot++] = i;
        }
        for (int j = 0; j < tot && i * prime[j] < maxn; j++) {
            vis[i * prime[j]] = 1;
            if (i % prime[j] == 0)  break;
        }
    }
}

int main () {
    init();
    int T;
    cin >> T;
    while (T--) {
        ll p, q;
        cin >> p >> q;
        if (p < q) {
            cout << p << endl;
            continue;
        }
        ll tmp = p;
        ll qq = q;
        ll now = 1;
        ll m1 = p;
        for (int i = 0; i < tot && prime[i] <= q; i++) {
            if (q % prime[i] == 0) {
                ll now1 = 1, now2 = 1;
                while (tmp % prime[i] == 0) tmp /= prime[i], now1 *= prime[i];
                while (q % prime[i] == 0) q /= prime[i], now2 *= prime[i];
                if (now1 >= now2) {
                    now *= now1;
                    m1 = min(m1, now1 / now2 * prime[i]);
                }
            }
        }
        if (q > 1 && tmp % q == 0) {
            ll now1 = 1, now2 = 1;
            while (tmp % p == 0) tmp /= p, now1 *= p;
            while (q % p == 0) q /= p, now2 *= p;
            if (now1 >= now2) {
                now *= now1;
                m1 = min(m1, now1 / now2 * p);
            }
        }
        if (tmp > 1) {
            cout << p << endl;
            continue;
        }
        cout << p / m1 << endl;

    }

    return 0;
}


//
// Created by ttw on 2020/11/1.
//

#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <queue>

using namespace std;

typedef long long ll;

const int mod = 1e9 + 7;
const int maxn = 1e6 + 5;

int prime[maxn], tot;
bool vis[maxn];
void init () {
    for (int i = 2; i < maxn; i++) {
        if (!vis[i]) {
            prime[tot++] = i;
        }
        for (int j = 0; j < tot && i * prime[j] < maxn; j++) {
            vis[i * prime[j]] = 1;
            if (i % prime[j] == 0)  break;
        }
    }
}

int main () {
    init();
    int T;
    cin >> T;
    while (T--) {
        ll p, q;
        cin >> p >> q;
        if (p % q != 0) {
            cout << p << endl;
            continue;
        }
        ll ans = 1;
        for (int i = 0; i < tot && prime[i] <= q; i++) {
            if (q % prime[i] == 0) {
                ll tmp = p;
                ll now1 = 1, now2 = 1;
                while (tmp % prime[i] == 0) tmp /= prime[i], now1 *= prime[i];
                while (q % prime[i] == 0) q /= prime[i], now2 *= prime[i];
                ans = max(ans, tmp * now2 / prime[i]);
            }
        }
        if (q > 1) {
            ll tmp = p;
            ll now1 = 1, now2 = 1;
            while (tmp % p == 0) tmp /= p, now1 *= p;
            while (q % p == 0) q /= p, now2 *= p;
            ans = max(ans, tmp * now2 / p);
        }
        cout << ans << endl;

    }

    return 0;
}


//
// Created by ttw on 2020/11/1.
//

#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <queue>

using namespace std;

typedef long long ll;

const int mod = 1e9 + 7;
const int maxn = 1e6 + 5;

int prime[maxn], tot;
bool vis[maxn];
void init () {
    for (int i = 2; i < maxn; i++) {
        if (!vis[i]) {
            prime[tot++] = i;
        }
        for (int j = 0; j < tot && i * prime[j] < maxn; j++) {
            vis[i * prime[j]] = 1;
            if (i % prime[j] == 0)  break;
        }
    }
}

int main () {
    init();
    int T;
    cin >> T;
    while (T--) {
        ll p, q;
        cin >> p >> q;
        if (p % q != 0) {
            cout << p << endl;
            continue;
        }
        ll ans = 1;
        ll now = p;
        for (int i = 0; i < tot && prime[i] <= q; i++) {
            if (q % prime[i] == 0) {
                ll tmp = p;
                ll now1 = 1, now2 = 1;
                while (tmp % prime[i] == 0) tmp /= prime[i], now1 *= prime[i], now /= prime[i];
                while (q % prime[i] == 0) q /= prime[i], now2 *= prime[i];
                if (now1 >= now2) {
                    ans = max(ans, tmp * now2 / prime[i]);
                }
                else{
                    ans = p;
                }
            }
        }
        if (q > 1 && p % q == 0) {
            ll tmp = p;
            ll now1 = 1, now2 = 1;
            while (tmp % p == 0) tmp /= p, now1 *= p, now /= p;
            while (q % p == 0) q /= p, now2 *= p;
            if (now1 >= now2) {
                ans = max(ans, tmp * now2 / p);
            }
            else{
                ans = p;
            }
        }
        cout << ans << endl;

    }

    return 0;
}


//
// Created by ttw on 2020/11/1.
//

#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <queue>

using namespace std;

typedef long long ll;

const int mod = 1e9 + 7;
const int maxn = 1e6 + 5;

int prime[maxn], tot;
bool vis[maxn];
void init () {
    for (int i = 2; i < maxn; i++) {
        if (!vis[i]) {
            prime[tot++] = i;
        }
        for (int j = 0; j < tot && i * prime[j] < maxn; j++) {
            vis[i * prime[j]] = 1;
            if (i % prime[j] == 0)  break;
        }
    }
}

int main () {
    init();
    int T;
    cin >> T;
    while (T--) {
        ll p, q;
        cin >> p >> q;
        ll ans = 1;
        ll now = p;
        for (int i = 0; i < tot && prime[i] <= q; i++) {
            if (q % prime[i] == 0) {
                ll tmp = p;
                ll now1 = 1, now2 = 1;
                while (tmp % prime[i] == 0) tmp /= prime[i], now1 *= prime[i], now /= prime[i];
                while (q % prime[i] == 0) q /= prime[i], now2 *= prime[i];
                ans = max(ans, tmp * now2 / prime[i]);
            }
        }
        if (q > 1) {
            ll tmp = p;
            ll now1 = 1, now2 = 1;
            while (tmp % p == 0) tmp /= p, now1 *= p, now /= p;
            while (q % p == 0) q /= p, now2 *= p;
            ans = max(ans, tmp * now2 / p);
        }
        cout << ans << endl;

    }

    return 0;
}


//
// Created by ttw on 2020/11/1.
//

#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <queue>

using namespace std;

typedef long long ll;

const int mod = 1e9 + 7;
const int maxn = 1e6 + 5;

int prime[maxn], tot;
bool vis[maxn];
void init () {
    for (int i = 2; i < maxn; i++) {
        if (!vis[i]) {
            prime[tot++] = i;
        }
        for (int j = 0; j < tot && i * prime[j] < maxn; j++) {
            vis[i * prime[j]] = 1;
            if (i % prime[j] == 0)  break;
        }
    }
}

int main () {
    init();
    int T;
    cin >> T;
    while (T--) {
        ll p, q;
        cin >> p >> q;
        if (p % q != 0) {
            cout << p << endl;
            continue;
        }
        ll ans = 1;
        for (int i = 0; i < tot && prime[i] <= q; i++) {
            if (q % prime[i] == 0) {
                ll tmp = p;
                ll now1 = 1, now2 = 1;
                while (tmp % prime[i] == 0) tmp /= prime[i], now1 *= prime[i];
                while (q % prime[i] == 0) q /= prime[i], now2 *= prime[i];
                ans = max(ans, tmp * now2 / prime[i]);
            }
        }
        if (q > 1) {
            ll tmp = p;
            ll now1 = 1, now2 = 1;
            while (tmp % q == 0) tmp /= q, now1 *= p;
            ans = max(ans, tmp);
        }
        cout << ans << endl;

    }

    return 0;
}


//
// Created by ttw on 2020/11/1.
//

#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <queue>

using namespace std;

typedef long long ll;

const int mod = 1e9 + 7;
const int maxn = 1e5 + 5;

int prime[maxn], tot;
bool vis[maxn];
void init () {
    for (int i = 2; i < maxn; i++) {
        if (!vis[i]) {
            prime[tot++] = i;
        }
        for (int j = 0; j < tot && i * prime[j] < maxn; j++) {
            vis[i * prime[j]] = 1;
            if (i % prime[j] == 0)  break;
        }
    }
}

int main () {
    init();
    int T;
    cin >> T;
    while (T--) {
        ll p, q;
        cin >> p >> q;
        ll qq = q;
        ll now = 1;
        ll m1 = p;
        ll ans = 1;
        for (int i = 0; i < tot && prime[i] <= q; i++) {
            if (q % prime[i] == 0) {
                ll tmp = p;
                ll now1 = 1, now2 = 1;
                while (tmp % prime[i] == 0) tmp /= prime[i], now1 *= prime[i];
                while (q % prime[i] == 0) q /= prime[i], now2 *= prime[i];
                ans = max(ans, tmp * now2 / prime[i]);
            }
        }
        if (q > 1 && p % q == 0) {

            ll tmp = p;
            ll now1 = 1, now2 = 1;
            while (tmp % p == 0) tmp /= p, now1 *= p;
            while (q % p == 0) q /= p, now2 *= p;
            ans = max(ans, tmp * now2 / p);
        }
        cout << ans << endl;

    }

    return 0;
}


//
// Created by ttw on 2020/11/1.
//

#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <queue>

using namespace std;

typedef long long ll;

const int mod = 1e9 + 7;
const int maxn = 1e6 + 5;

int prime[maxn], tot;
bool vis[maxn];
void init () {
    for (int i = 2; i < maxn; i++) {
        if (!vis[i]) {
            prime[tot++] = i;
        }
        for (int j = 0; j < tot && i * prime[j] < maxn; j++) {
            vis[i * prime[j]] = 1;
            if (i % prime[j] == 0)  break;
        }
    }
}

int main () {
    init();
    int T;
    cin >> T;
    while (T--) {
        ll p, q;
        cin >> p >> q;
        ll ans = 1;
        for (int i = 0; i < tot && prime[i] <= q; i++) {
            if (q % prime[i] == 0) {
                ll tmp = p;
                ll now1 = 1, now2 = 1;
                while (tmp % prime[i] == 0) tmp /= prime[i], now1 *= prime[i];
                while (q % prime[i] == 0) q /= prime[i], now2 *= prime[i];
                if (now1 >= now2 / prime[i])
                    ans = max(ans, tmp * now2 / prime[i]);
            }
        }
        if (q > 1 && p % q == 0) {

            ll tmp = p;
            ll now1 = 1, now2 = 1;
            while (tmp % p == 0) tmp /= p, now1 *= p;
            while (q % p == 0) q /= p, now2 *= p;
            if (now1 >= now2 / p)
                ans = max(ans, tmp * now2 / p);
        }
        cout << ans << endl;

    }

    return 0;
}


#include <bits/stdc++.h>

// macro
// CORE
// #define int long long
// #define MAX_INT 2147483647
// #define MAX_LL 9223372036854775807

#define ll long long
#define endl "\n"
#define INF 2e9
#define LINF 9e19
#define V vector
#define PQ priority_queue
#define f first
#define s second
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define FORR(i,a,b) for(int i=(a);i>=(b);i--)
#define FORA(i,array) for(int i=0,array_size=array.size();i<array_size;i++)
#define fastio cin.tie(0);cout.tie(0);ios_base::sync_with_stdio(false)
/* for debuging */
#define printa(array) FORA(array_i,array)cout<<array[array_i]<<" ";cout<<"\n"
#define printb(array) FORA(array_i,array){FORA(array_j,array[array_i])cout<<array[array_i][array_j]<<" ";cout<<"\n";}cout<<"\n"
using namespace std;

int main(){
    fastio;
    int t;
    cin >> t;
    while(t--){
        long long p, q;
        cin >> p >> q;
        if(p % q == 0){
            long long pivot = p / q;
            bool check = 1;
            for(long long i = 1, N = sqrt(q); i <= N; i++){
                if(q % i == 0 && (pivot * (q / i)) % q != 0){
                    cout << (pivot * (q / i)) << endl;
                    check = 0;
                    break;
                }
            }
            if(check)
                for(long long i = sqrt(q); i >= 1; i--){
                    if(q % i == 0 && (pivot * i % q != 0)){
                        cout << pivot * i << endl;
                        check = 0;
                        break;
                    }
                }
        }
        else
            cout << p << endl;
    }
}


#include <bits/stdc++.h>

// macro
// CORE
// #define int long long
// #define MAX_INT 2147483647
// #define MAX_LL 9223372036854775807

#define ll long long
#define endl "\n"
#define INF 2e9
#define LINF 9e19
#define V vector
#define PQ priority_queue
#define f first
#define s second
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define FORR(i,a,b) for(int i=(a);i>=(b);i--)
#define FORA(i,array) for(int i=0,array_size=array.size();i<array_size;i++)
#define fastio cin.tie(0);cout.tie(0);ios_base::sync_with_stdio(false)
/* for debuging */
#define printa(array) FORA(array_i,array)cout<<array[array_i]<<" ";cout<<"\n"
#define printb(array) FORA(array_i,array){FORA(array_j,array[array_i])cout<<array[array_i][array_j]<<" ";cout<<"\n";}cout<<"\n"
using namespace std;

int main(){
    fastio;
    int t;
    cin >> t;
    while(t--){
        long long p, q;
        cin >> p >> q;
        if(p != q){
            int mod = q;
            while(p % mod == 0)
                mod *= q;
            bool check = 1;
            for(long long i = 1, N = sqrt(mod); i <= N; i++){
                if(mod % i == 0 && p / i % q != 0){
                    cout << p / i << endl;
                    check = 0;
                    break;
                }
            }
            if(check)
                for(long long i = sqrt(mod); i >= 1; i--){
                    if(mod % i == 0 && p / (mod / i) % q != 0){
                        cout << p / (mod / i) << endl;
                        check = 0;
                        break;
                    }
                }
        }
        else
            cout << 1 << endl;
    }
}


#include <bits/stdc++.h>

// macro
// CORE
// #define int long long
// #define MAX_INT 2147483647
// #define MAX_LL 9223372036854775807

#define ll long long
#define endl "\n"
#define INF 2e9
#define LINF 9e19
#define V vector
#define PQ priority_queue
#define f first
#define s second
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define FORR(i,a,b) for(int i=(a);i>=(b);i--)
#define FORA(i,array) for(int i=0,array_size=array.size();i<array_size;i++)
#define fastio cin.tie(0);cout.tie(0);ios_base::sync_with_stdio(false)
/* for debuging */
#define printa(array) FORA(array_i,array)cout<<array[array_i]<<" ";cout<<"\n"
#define printb(array) FORA(array_i,array){FORA(array_j,array[array_i])cout<<array[array_i][array_j]<<" ";cout<<"\n";}cout<<"\n"
using namespace std;

int main(){
    fastio;
    int t;
    cin >> t;
    while(t--){
        long long p, q;
        cin >> p >> q;
        if(p % q == 0){
            int mod = q;
            while(p % mod == 0)
                mod *= q;
            bool check = 1;
            for(long long i = 1, N = sqrt(mod); i <= N; i++){
                if(mod % i == 0 && p / i % q != 0){
                    cout << p / i << endl;
                    check = 0;
                    break;
                }
            }
            if(check)
                for(long long i = sqrt(mod); i >= 1; i--){
                    if(mod % i == 0 && p / (mod / i) % q != 0){
                        cout << p / (mod / i) << endl;
                        check = 0;
                        break;
                    }
                }
        }
        else
            cout << p << endl;
    }
}


#include <bits/stdc++.h>

// macro
// CORE
// #define int long long
// #define MAX_INT 2147483647
// #define MAX_LL 9223372036854775807

#define ll long long
#define endl "\n"
#define INF 2e9
#define LINF 9e19
#define V vector
#define PQ priority_queue
#define f first
#define s second
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define FORR(i,a,b) for(int i=(a);i>=(b);i--)
#define FORA(i,array) for(int i=0,array_size=array.size();i<array_size;i++)
#define fastio cin.tie(0);cout.tie(0);ios_base::sync_with_stdio(false)
/* for debuging */
#define printa(array) FORA(array_i,array)cout<<array[array_i]<<" ";cout<<"\n"
#define printb(array) FORA(array_i,array){FORA(array_j,array[array_i])cout<<array[array_i][array_j]<<" ";cout<<"\n";}cout<<"\n"
using namespace std;

int main(){
    fastio;
    int t;
    cin >> t;
    while(t--){
        long long p, q;
        cin >> p >> q;
        if(p != q){
            long long mod = q;
            while(p % mod == 0)
                mod *= q;
            bool check = 1;
            for(long long i = 1, N = sqrt(mod); i <= N; i++){
                if(mod % i == 0 && p / i % q != 0){
                    cout << p / i << endl;
                    check = 0;
                    break;
                }
            }
            if(check)
                for(long long i = sqrt(mod); i >= 1; i--){
                    if(mod % i == 0 && p / (mod / i) % q != 0){
                        cout << p / (mod / i) << endl;
                        check = 0;
                        break;
                    }
                }
        }
        else
            cout << 1 << endl;
    }
}


#include <bits/stdc++.h>

// macro
// CORE
// #define int long long
// #define MAX_INT 2147483647
// #define MAX_LL 9223372036854775807

#define ll long long
#define endl "\n"
#define INF 2e9
#define LINF 9e19
#define V vector
#define PQ priority_queue
#define f first
#define s second
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define FORR(i,a,b) for(int i=(a);i>=(b);i--)
#define FORA(i,array) for(int i=0,array_size=array.size();i<array_size;i++)
#define fastio cin.tie(0);cout.tie(0);ios_base::sync_with_stdio(false)
/* for debuging */
#define printa(array) FORA(array_i,array)cout<<array[array_i]<<" ";cout<<"\n"
#define printb(array) FORA(array_i,array){FORA(array_j,array[array_i])cout<<array[array_i][array_j]<<" ";cout<<"\n";}cout<<"\n"
using namespace std;

int main(){
    fastio;
    int t;
    cin >> t;
    while(t--){
        long long p, q;
        cin >> p >> q;
        if(p % q == 0){
            long long pivot = p / q;
            for(long long i = sqrt(q); i >= 1; i--){
                if(q % i == 0 && (pivot * i) % q != 0){
                    cout << pivot * i << endl;
                    break;
                }
            }
        }
        else
            cout << p << endl;
    }
}


#include <bits/stdc++.h>

// macro
// CORE
// #define int long long
// #define MAX_INT 2147483647
// #define MAX_LL 9223372036854775807

#define ll long long
#define endl "\n"
#define INF 2e9
#define LINF 9e19
#define V vector
#define PQ priority_queue
#define f first
#define s second
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define FORR(i,a,b) for(int i=(a);i>=(b);i--)
#define FORA(i,array) for(int i=0,array_size=array.size();i<array_size;i++)
#define fastio cin.tie(0);cout.tie(0);ios_base::sync_with_stdio(false)
/* for debuging */
#define printa(array) FORA(array_i,array)cout<<array[array_i]<<" ";cout<<"\n"
#define printb(array) FORA(array_i,array){FORA(array_j,array[array_i])cout<<array[array_i][array_j]<<" ";cout<<"\n";}cout<<"\n"
using namespace std;

int main(){
    fastio;
    int t;
    cin >> t;
    while(t--){
        long long p, q;
        cin >> p >> q;
        if(p % q == 0){
            long long pivot = p / q;
            bool check = 1;
            for(long long i = 1, N = sqrt(q); i <= N; i++){
                if(q % i == 0 && (pivot * (q / i)) % q != 0){
                    cout << (pivot * (q / i)) << endl;
                    check = 0;
                    break;
                }
            }
            if(check)
                for(long long i = sqrt(q); i >= 1; i--){
                    if(q % i == 0 && pivot * i % q != 0){
                        cout << pivot * i << endl;
                        check = 0;
                        break;
                    }
                }
            if(check)
                cout << 1 << endl;
        }
        else
            cout << p << endl;
    }
}


#include <bits/stdc++.h>

// macro
// CORE
// #define int long long
// #define MAX_INT 2147483647
// #define MAX_LL 9223372036854775807

#define ll long long
#define endl "\n"
#define INF 2e9
#define LINF 9e19
#define V vector
#define PQ priority_queue
#define f first
#define s second
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define FORR(i,a,b) for(int i=(a);i>=(b);i--)
#define FORA(i,array) for(int i=0,array_size=array.size();i<array_size;i++)
#define fastio cin.tie(0);cout.tie(0);ios_base::sync_with_stdio(false)
/* for debuging */
#define printa(array) FORA(array_i,array)cout<<array[array_i]<<" ";cout<<"\n"
#define printb(array) FORA(array_i,array){FORA(array_j,array[array_i])cout<<array[array_i][array_j]<<" ";cout<<"\n";}cout<<"\n"
using namespace std;

int main(){
    fastio;
    int t;
    cin >> t;
    while(t--){
        long long p, q;
        cin >> p >> q;
        if(p % q){
            cout << p << endl;
            continue;
        }
        PQ<pair<long long,int>, V<pair<long long,int>>, greater<pair<long long,int>>> pq;
        int tmp = q;
        for(int i = 2, N = sqrt(q); i <= N; i++){
            bool check = 0;
            while(tmp % i == 0){
                tmp /= i;
                check = 1;
            }
            if(check)
                pq.push({i,i});
        }
        if(tmp != 1)
            pq.push({tmp,tmp});
        long long value = pq.top().first;
        while((p / value) % q == 0){
            pq.push({pq.top().first * pq.top().second, pq.top().second});
            pq.pop();
            value = pq.top().first;
        }
        cout << p / value << endl;
    }
}


#include <bits/stdc++.h>

// macro
// CORE
// #define int long long
// #define MAX_INT 2147483647
// #define MAX_LL 9223372036854775807

#define ll long long
#define endl "\n"
#define INF 2e9
#define LINF 9e19
#define V vector
#define PQ priority_queue
#define f first
#define s second
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define FORR(i,a,b) for(int i=(a);i>=(b);i--)
#define FORA(i,array) for(int i=0,array_size=array.size();i<array_size;i++)
#define fastio cin.tie(0);cout.tie(0);ios_base::sync_with_stdio(false)
/* for debuging */
#define printa(array) FORA(array_i,array)cout<<array[array_i]<<" ";cout<<"\n"
#define printb(array) FORA(array_i,array){FORA(array_j,array[array_i])cout<<array[array_i][array_j]<<" ";cout<<"\n";}cout<<"\n"
using namespace std;

int main(){
    fastio;
    int t;
    cin >> t;
    while(t--){
        long long p, q;
        cin >> p >> q;
        bool check = 1;
        for(int i = 1, N = sqrt(p); i <= N; i++){
            if(p % i == 0 && (p / i) % q != 0){
                cout << (p / i) << endl;
                check = 0;
                break;
            }
        }
        if(check){
            for(int i = sqrt(p); i >= 0; i--){
                if(p % i == 0 && i % q != 0){
                    cout << i << endl;
                    break;
                }
            }
        }
    }
}


#include <bits/stdc++.h>

// macro
// CORE
// #define int long long
// #define MAX_INT 2147483647
// #define MAX_LL 9223372036854775807

#define ll long long
#define endl "\n"
#define INF 2e9
#define LINF 9e19
#define V vector
#define PQ priority_queue
#define f first
#define s second
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define FORR(i,a,b) for(int i=(a);i>=(b);i--)
#define FORA(i,array) for(int i=0,array_size=array.size();i<array_size;i++)
#define fastio cin.tie(0);cout.tie(0);ios_base::sync_with_stdio(false)
/* for debuging */
#define printa(array) FORA(array_i,array)cout<<array[array_i]<<" ";cout<<"\n"
#define printb(array) FORA(array_i,array){FORA(array_j,array[array_i])cout<<array[array_i][array_j]<<" ";cout<<"\n";}cout<<"\n"
using namespace std;

int main(){
    fastio;
    int t;
    cin >> t;
    while(t--){
        long long p, q;
        cin >> p >> q;
        if(p % q == 0){
            long long pivot = p / q;
            for(long long i = sqrt(q); i >= 2; i--){
                if(q % i == 0 && (pivot * i) % q != 0){
                    cout << pivot * i << endl;
                    break;
                }
            }
        }
        else
            cout << p << endl;
    }
}


#include <bits/stdc++.h>

// macro
// CORE
// #define int long long
// #define MAX_INT 2147483647
// #define MAX_LL 9223372036854775807

#define ll long long
#define endl "\n"
#define INF 2e9
#define LINF 9e19
#define V vector
#define PQ priority_queue
#define f first
#define s second
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define FORR(i,a,b) for(int i=(a);i>=(b);i--)
#define FORA(i,array) for(int i=0,array_size=array.size();i<array_size;i++)
#define fastio cin.tie(0);cout.tie(0);ios_base::sync_with_stdio(false)
/* for debuging */
#define printa(array) FORA(array_i,array)cout<<array[array_i]<<" ";cout<<"\n"
#define printb(array) FORA(array_i,array){FORA(array_j,array[array_i])cout<<array[array_i][array_j]<<" ";cout<<"\n";}cout<<"\n"
using namespace std;

int main(){
    fastio;
    int t;
    cin >> t;
    while(t--){
        long long p, q;
        cin >> p >> q;
        if(p % q == 0){
            long long pivot = p / q;
            while(pivot % q == 0)
                pivot /= q;
            bool check = 1;
            for(long long i = 1, N = sqrt(q); i <= N; i++){
                if(q % i == 0 && (pivot * (q / i)) % q != 0){
                    cout << (pivot * (q / i)) << endl;
                    check = 0;
                    break;
                }
            }
            if(check)
                for(long long i = sqrt(q); i >= 1; i--){
                    if(q % i == 0 && pivot * i % q != 0){
                        cout << pivot * i << endl;
                        check = 0;
                        break;
                    }
                }
        }
        else
            cout << p << endl;
    }
}


#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=1e6+10,maxm=1e9;
#define int long long
int prime[maxn];
bool is_he[maxn];
int cnt;
int q1[maxn],cnt2[maxn];
int tot1,tot2;
int p,q;
void Init(){
	is_he[0]=1;
	is_he[1]=1;
	for(int i=2;i<=maxn;++i){
		if(!is_he[i]) prime[++cnt]=i;
		for(int j=1;j<=cnt&&prime[j]*i<=maxn;++j){
			is_he[i*prime[j]]=1;
			if(i%prime[j]==0) break;
		}
	}
}
int qpow(int a,int b){
	int ans=1;
	int base=a;
	while(b){
		if(b&1) ans=ans*base;
		base=base*base;
		b>>=1;
	}
	return ans;
}
void solve(){
	if(p%q!=0){
		printf("%lld\n",p);
		return;
	}
	int y=q;
	for(int i=1;i<=cnt;++i){
		int sum=0;
		while(q%prime[i]==0){
			sum++;
			q/=prime[i];
		}
		if(sum){
			q1[++tot2]=prime[i];
			cnt2[prime[i]]=sum;
		}
		if(q==1) break;
	}
	int x=p;
	int ans=1;
	for(int i=1;i<=tot2;++i){
		p=x;
		if(p%q1[i]==0){
			int sum=0;
			while(p%q1[i]==0){
				sum++;
				p/=q1[i];
			}
			if(sum>=cnt2[q1[i]]) ans=max(ans,x/qpow(q1[i],sum)*qpow(q1[i],cnt2[q1[i]]-1));
			else ans=max(ans,x);
		}
	}
	if(x%q==0&&q!=1&&(x/q)%y!=0){
		ans=max(ans,x/q);
	}
	printf("%lld\n",ans);
}
signed main(){
	int T;
	scanf("%lld",&T);
	Init();
	for(int i=1;i<=T;++i){
		tot1=0,tot2=0;
		scanf("%lld%lld",&p,&q);
		if(p<q) printf("%lld\n",p);
		else solve();
	}
	return 0;
}


#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=1e6+10,maxm=1e9;
#define int long long
int prime[maxn];
bool is_he[maxn];
int cnt;
int q1[maxn],cnt2[maxn];
int tot1,tot2;
int p,q;
void Init(){
	is_he[0]=1;
	is_he[1]=1;
	for(int i=2;i<=maxn;++i){
		if(!is_he[i]) prime[++cnt]=i;
		for(int j=1;j<=cnt&&prime[j]*i<=maxn;++j){
			is_he[i*prime[j]]=1;
			if(i%prime[j]==0) break;
		}
	}
}
int qpow(int a,int b){
	int ans=1;
	int base=a;
	while(b){
		if(b&1) ans=ans*base;
		base=base*base;
		b>>=1;
	}
	return ans;
}
void solve(){
	if(p%q!=0){
		printf("%lld\n",p);
		return;
	}
	for(int i=1;i<=cnt;++i){
		int sum=0;
		while(q%prime[i]==0){
			sum++;
			q/=prime[i];
		}
		if(sum){
			q1[++tot2]=prime[i];
			cnt2[prime[i]]=sum;
		}
		if(q==1) break;
	}
	int x=p;
	int ans=1;
	for(int i=1;i<=tot2;++i){
		p=x;
		if(p%q1[i]==0){
			int sum=0;
			while(p%q1[i]==0){
				sum++;
				p/=q1[i];
			}
			if(sum>=cnt2[q1[i]]) ans=max(ans,x/qpow(q1[i],sum)*qpow(q1[i],cnt2[q1[i]]-1));
			else ans=max(ans,x);
		}
	}
	if(x%q==0){
		ans=max(ans,x/q);
	}
	printf("%lld\n",ans);
}
signed main(){
	int T;
	scanf("%lld",&T);
	Init();
	for(int i=1;i<=T;++i){
		tot1=0,tot2=0;
		scanf("%lld%lld",&p,&q);
		if(p<q) printf("%lld\n",p);
		else solve();
	}
	return 0;
}


#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=1e6+10,maxm=1e9;
#define int long long
int prime[maxn];
bool is_he[maxn];
int cnt;
int p1[maxn],q1[maxn],cnt1[maxn],cnt2[maxn];
int tot1,tot2;
int p,q;
void Init(){
	is_he[0]=1;
	is_he[1]=1;
	for(int i=2;i<=maxn;++i){
		if(!is_he[i]) prime[++cnt]=i;
		for(int j=1;j<=cnt&&prime[j]*i<=maxn;++j){
			is_he[i*prime[j]]=1;
			if(i%prime[j]==0) break;
		}
	}
}
int qpow(int a,int b){
	int ans=1;
	int base=a;
	while(b){
		if(b&1) ans=ans*base;
		base=base*base;
		b>>=1;
	}
	return ans;
}
void solve(){
	if(p%q!=0){
		printf("%lld\n",p);
		return;
	}
	int x=p;
	for(int i=1;i<=cnt;++i){
		int sum=0;
		while(p%prime[i]==0){
			sum++;
			p/=prime[i];
		}
		if(sum){
			p1[++tot1]=prime[i];
			cnt1[prime[i]]=sum;
		}
		if(p==1) break;
	}
	for(int i=1;i<=cnt;++i){
		int sum=0;
		while(q%prime[i]==0){
			sum++;
			q/=prime[i];
		}
		if(sum){
			q1[++tot2]=prime[i];
			cnt2[prime[i]]=sum;
		}
		if(q==1) break;
	}
	int ans=0;
	for(int i=1;i<=tot2;++i){
		if(cnt1[q1[i]]){
			if(cnt1[q1[i]]>=cnt2[q1[i]])ans=max(ans,x/qpow(q1[i],cnt1[q1[i]])*qpow(q1[i],cnt2[q1[i]]-1));
			else ans=max(ans,x);
		}
	}
	if(q==p&&q!=1){
		ans=max(ans,x/q);
	}
	printf("%lld\n",ans);
}
signed main(){
	int T;
	scanf("%lld",&T);
	Init();
	for(int i=1;i<=T;++i){
		tot1=0,tot2=0;
		scanf("%lld%lld",&p,&q);
		if(p<q) printf("%lld\n",p);
		else solve();
	}
	return 0;
}


#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=1e6+10,maxm=1e9;
#define int long long
int prime[maxn];
bool is_he[maxn];
int cnt;
int p1[maxn],q1[maxn],cnt1[maxn],cnt2[maxn];
int tot1,tot2;
int p,q;
void Init(){
	is_he[0]=1;
	is_he[1]=1;
	for(int i=2;i<=maxn;++i){
		if(!is_he[i]) prime[++cnt]=i;
		for(int j=1;j<=cnt&&prime[j]*i<=maxn;++j){
			is_he[i*prime[j]]=1;
			if(i%prime[j]==0) break;
		}
	}
}
int qpow(int a,int b){
	int ans=1;
	int base=a;
	while(b){
		if(b&1) ans=ans*base;
		base=base*base;
		b>>=1;
	}
	return ans;
}
void solve(){
	int x=p;
	for(int i=1;i<=cnt;++i){
		int sum=0;
		while(p%prime[i]==0){
			sum++;
			p/=prime[i];
		}
		if(sum){
			p1[++tot1]=prime[i];
			cnt1[prime[i]]=sum;
		}
		if(p==1) break;
	}
	for(int i=1;i<=cnt;++i){
		int sum=0;
		while(q%prime[i]==0){
			sum++;
			q/=prime[i];
		}
		if(sum){
			q1[++tot2]=prime[i];
			cnt2[prime[i]]=sum;
		}
		if(q==1) break;
	}
	int ans=0;
	int flag=0;
	for(int i=1;i<=tot2;++i){
		if(cnt1[q1[i]]){
			if(cnt1[q1[i]]>=cnt2[q1[i]])ans=max(ans,x/qpow(q1[i],cnt1[p1[i]])*qpow(q1[i],cnt2[q1[i]]-1));
			else ans=max(ans,x);
			flag=1;
		}
	}
	if(q==p&&q!=1){
		ans=max(ans,x/q);
		flag=1;
	}
	if(!flag) ans=x;
	printf("%lld\n",ans);
}
signed main(){
	int T;
	scanf("%lld",&T);
	Init();
	for(int i=1;i<=T;++i){
		tot1=0,tot2=0;
		scanf("%lld%lld",&p,&q);
		if(p<q) printf("%lld\n",p);
		else solve();
	}
	return 0;
}


#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=1e6+10,maxm=1e9;
#define int long long
int prime[maxn];
bool is_he[maxn];
int cnt;
int p1[maxn],q1[maxn],cnt1[maxn],cnt2[maxn];
int tot1,tot2;
int p,q;
void Init(){
	is_he[0]=1;
	is_he[1]=1;
	for(int i=2;i<=maxn;++i){
		if(!is_he[i]) prime[++cnt]=i;
		for(int j=1;j<=cnt&&prime[j]*i<=maxn;++j){
			is_he[i*prime[j]]=1;
			if(i%prime[j]==0) break;
		}
	}
}
int qpow(int a,int b){
	int ans=1;
	int base=a;
	while(b){
		if(b&1) ans=ans*base;
		base=base*base;
		b>>=1;
	}
	return ans;
}
void solve(){
	int x=p;
	for(int i=1;i<=cnt;++i){
		int sum=0;
		while(p%prime[i]==0){
			sum++;
			p/=prime[i];
		}
		if(sum){
			p1[++tot1]=prime[i];
			cnt1[prime[i]]=sum;
		}
		if(p==1) break;
	}
	for(int i=1;i<=cnt;++i){
		int sum=0;
		while(q%prime[i]==0){
			sum++;
			q/=prime[i];
		}
		if(sum){
			q1[++tot2]=prime[i];
			cnt2[prime[i]]=sum;
		}
		if(q==1) break;
	}
	int ans=0;
	int flag=0;
	for(int i=1;i<=tot2;++i){
		if(cnt1[q1[i]]){
			if(cnt1[q1[i]]>=cnt2[q1[i]])ans=max(ans,x/qpow(q1[i],cnt1[q1[i]])*qpow(q1[i],cnt2[q1[i]]-1));
			else ans=max(ans,x);
			flag=1;
		}
	}
	if(q==p&&q!=1){
		ans=max(ans,x/q);
		flag=1;
	}
	if(!flag) ans=x;
	printf("%lld\n",ans);
}
signed main(){
	int T;
	scanf("%lld",&T);
	Init();
	for(int i=1;i<=T;++i){
		tot1=0,tot2=0;
		scanf("%lld%lld",&p,&q);
		if(p<q) printf("%lld\n",p);
		else solve();
	}
	return 0;
}


#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=1e6+10,maxm=1e9;
#define int long long
int prime[maxn];
bool is_he[maxn];
int cnt;
int p1[maxn],q1[maxn],cnt1[maxn],cnt2[maxn];
int tot1,tot2;
int p,q;
void Init(){
	is_he[0]=1;
	is_he[1]=1;
	for(int i=2;i<=maxn;++i){
		if(!is_he[i]) prime[++cnt]=i;
		for(int j=1;j<=cnt&&prime[j]*i<=maxn;++j){
			is_he[i*prime[j]]=1;
			if(i%prime[j]==0) break;
		}
	}
}
int qpow(int a,int b){
	int ans=1;
	int base=a;
	while(b){
		if(b&1) ans=ans*base;
		base=base*base;
		b>>=1;
	}
	return ans;
}
void solve(){
	if(p%q!=0){
		printf("%lld\n",p);
		return;
	}
	int x=p;
	for(int i=1;i<=cnt;++i){
		int sum=0;
		while(p%prime[i]==0){
			sum++;
			p/=prime[i];
		}
		if(sum){
			p1[++tot1]=prime[i];
			cnt1[prime[i]]=sum;
		}
		if(p==1) break;
	}
	for(int i=1;i<=cnt;++i){
		int sum=0;
		while(q%prime[i]==0){
			sum++;
			q/=prime[i];
		}
		if(sum){
			q1[++tot2]=prime[i];
			cnt2[prime[i]]=sum;
		}
		if(q==1) break;
	}
	int ans=1;
	for(int i=1;i<=tot2;++i){
		if(cnt1[q1[i]]){
			if(cnt1[q1[i]]>=cnt2[q1[i]])ans=max(ans,x/qpow(q1[i],cnt1[q1[i]])*qpow(q1[i],cnt2[q1[i]]-1));
			else ans=max(ans,x);
		}
	}
	if(q==p&&q!=1){
		ans=max(ans,x/q);
	}
	printf("%lld\n",ans);
}
signed main(){
	int T;
	scanf("%lld",&T);
	Init();
	for(int i=1;i<=T;++i){
		tot1=0,tot2=0;
		scanf("%lld%lld",&p,&q);
		if(p<q) printf("%lld\n",p);
		else solve();
	}
	return 0;
}


#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=1e6+10,maxm=1e9;
#define int long long
int prime[maxn];
bool is_he[maxn];
int cnt;
int q1[maxn],cnt2[maxn];
int tot1,tot2;
int p,q;
void Init(){
	is_he[0]=1;
	is_he[1]=1;
	for(int i=2;i<=maxn;++i){
		if(!is_he[i]) prime[++cnt]=i;
		for(int j=1;j<=cnt&&prime[j]*i<=maxn;++j){
			is_he[i*prime[j]]=1;
			if(i%prime[j]==0) break;
		}
	}
}
int qpow(int a,int b){
	int ans=1;
	int base=a;
	while(b){
		if(b&1) ans=ans*base;
		base=base*base;
		b>>=1;
	}
	return ans;
}
void solve(){
	if(p%q!=0){
		printf("%lld\n",p);
		return;
	}
	for(int i=1;i<=cnt;++i){
		int sum=0;
		while(q%prime[i]==0){
			sum++;
			q/=prime[i];
		}
		if(sum){
			q1[++tot2]=prime[i];
			cnt2[prime[i]]=sum;
		}
		if(q==1) break;
	}
	int x=p;
	int ans=1;
	for(int i=1;i<=tot2;++i){
		p=x;
		if(p%q1[i]==0){
			int sum=0;
			while(p%q1[i]==0){
				sum++;
				p/=q1[i];
			}
			if(sum>=cnt2[q1[i]]) ans=max(ans,x/qpow(q1[i],sum)*qpow(q1[i],cnt2[q1[i]]-1));
			else ans=max(ans,x);
		}
	}
	if(x%q==0&&q!=1){
		ans=max(ans,x/q);
	}
	printf("%lld\n",ans);
}
signed main(){
	int T;
	scanf("%lld",&T);
	Init();
	for(int i=1;i<=T;++i){
		tot1=0,tot2=0;
		scanf("%lld%lld",&p,&q);
		if(p<q) printf("%lld\n",p);
		else solve();
	}
	return 0;
}


n = int(input()) ; m = int(input())
x = [] ; v = []
count = 0
for i in range(n):
	f = int(input())
	x.append(f)
x.sort()
v = x[::-1]
for i in range(n):
    if m > 0:
        m -= v[i]
        count += 1
    else:
        break
print(count)


I=lambda:int(input())
n=I()
m=I()
a=sorted(I() for _ in '0'*n)
while sum(a[1:])>=m:a=a[1:]
print(len(a))


I=lambda:int(input())
n=I()
m=I()
a=sorted(I() for _ in '0'*n)
while sum(a[1:])>=m:a=a[1:]


a=int(input())
b=int(input())
l1=[]
sum=0
c=0
for i in range(a):
	l1.append(int(input()))
l1.sort()
l1.reverse()
for j in l1:
	if sum<b:
		sum+=j
		c+=1
	else:
		break
print(c)


a=int(input())
b=int(input())
l1=[]
sum=0
c=0
for i in range(a):
	l.append(int(input()))
l.sort()
l.reverse()
for j in i:
	if sum<b:
		sum+=j
		c+=1
	else:
		break
print(c)


n=int(input())
m=int(input())
k=sorted([int(input()) for i in range(n)])[::-1]
b=k[0]
s=1
for i in range(1,n+1):
    if b>m or b==m:
        print(s)
        break
    else:
        b+=k[i]
        s+=1


n=int(input())
m=int(input())
k=sorted([int(input()) for i in range(n)])[::-1]
b=k[0]
s=1
for i in range(1,n+1):
    if b>m or b==m:
        print(s)
    else:
        b+=k[i]
        s+=1


n = int(input())
m = int(input())
arr = [int(a) for a in input().split()]
arr.sort()
N = len(arr)
i = 0
while m > 0:
    i += 1
    m -= arr[N-i]
print(i)


n = int(input())
m = int(input())
arr = [int(input()) for i in range(n)]
arr.sort()
N = len(arr)
i = 0
while m > 0:
    i += 1
    m -= arr[N-i]
print(i)


n = int(input())
m = int(input())
arr = [int a for a in input().split()]
arr.sort()
N = len(arr)
i = 0
while m > 0:
    i += 1
    m -= arr[N-i]
print(i)


n = int(input())
m = int(input())
a = [0 for x in range(n)]
for i in range(n):
    a[i] = int(input())
b = sorted(a,reverse=True)
s = 0
for i in range(n):
    s = s + b[i]
    if ( s >= m ):
        print(i+1)
        break


n = int(input())
m = int(input())
a = [0 for x in range(n)]
for i in range(n):
    a[i] = int(input())
b = sorted(a);
s = 0;
for i in range(n):
    s = s + b[i]
    if ( s >= m ):
        print(i+1)
        break


import sys

def find_minimum_number_of_flash_drives(storage_sizes, filesize):
    n = len(storage_sizes)
    storage_sizes.sort(reverse=True)
    counter = 0
    #print(storage_sizes)
    for size in storage_sizes:
        filesize -= size
        counter += 1
        if filesize <= 0:
            break

    return counter

number_of_flash_drives = int(sys.stdin.readline().strip())
filesize = int(sys.stdin.readline().strip())

storage_sizes = []
for _ in range(number_of_flash_drives):
    storage_sizes.append(int(sys.stdin.readline().strip()))

sys.stdout.write(str(find_minimum_number_of_flash_drives(storage_sizes, filesize)) + '\n')


import sys

def find_minimum_number_of_flash_drives(storage_sizes, filesize):
    n = len(storage_sizes)
    storage_sizes.sort(reverse=True)
    counter = 0

    for size in storage_sizes:
        filesize -= size
        counter += 1
        if filesize == 0:
            break

    return counter

number_of_flash_drives = int(sys.stdin.readline().strip())
filesize = int(sys.stdin.readline().strip())

storage_sizes = []
for _ in range(number_of_flash_drives):
    storage_sizes.append(int(sys.stdin.readline().strip()))

sys.stdout.write(str(find_minimum_number_of_flash_drives(storage_sizes, filesize)) + '\n')


I = lambda: int(input())
n = I()
m = I()
a = sorted(I() for _ in '0' * n)
while sum(a[1:]) >= m:
  a = a[1:]
print(len(a))


# import sys
# sys.stdin=open('input.in','r')
# sys.stdout=open('output.out','w')
n=int(input())
size=int(input())
l=[]
s=count=0

for x in range(n):
	l.append(int(input()))
l.sort()
for x in range(len(l)-1,-1,-1):
	s=s+l[x]
	count+=1
	if s>=size:
		print(count)
		break


number = int(input())
size = int(input())
cards = []
cnt = 0
for i in range(number):
    cards.append(int(input()))
cards.sort()
while size > 0:
    size -= cards.pop()
    cnt += 1
print(cnt)


number = int(input())
size = int(input())
cards = []
cnt = 0
for i in range(number):
    cards.append(int(input()))
cards.sort()
while size > 0:
    size -= cards[-1]
    cnt += 1
print(cnt)


n = int(input())
m = int(input())
a = []
for k in range(n):
	a.append(int(input()))
a.sort()
summa = 0
count = 0
i = (n - 1)
while summa < m:
	summa += a[i]
	count += 1
	i -= 1
print(count)


n=int(input())#n is size of list
m=int(input())#m is the size of disc

summ = 0
a = []

for i in range(0,n):
    a.append(int(input()))
    a.sort(reverse=True)

for i in range(0,n):
    summ = summ + a[i]
    if summ >= m:
        print(i+1)
        break


n=int(input())#n is size of list
m=int(input())#m is the size of disc

summ = 0
a = []

a.sort(reverse=True)

for i in range(0,n):
    a.append(int(input()))

for i in range(0,n):
    summ = summ + a[i]
    if summ >= m:
        print(i+1)
        break


n = int(input())
m = int(input())
a = []
d = 0
k = 0
for i in range(n):
  a.append(int(input()))
a.sort(reverse = True)
for i in a:
  if i+d != m:
    d += i
    k += 1
  if d >= m:
    break
print(k)


n = int(input())
m = int(input())
a = []
d = 0
k = 0
for i in range(n):
  a.append(int(input()))
a.sort(reverse = True)
if a[0] > m:
  print(1)
else:
  for i in a:
    if m > 0:
      m -= i
      k += 1
    else:
      break
  print(k)


n=int(input())
m=int(input())
x=[]
for i in range(n):
    x.append(int(input()))
x=sorted(x)
s=0

for i in range(len(x)) :
    s+=x[i]
    if s>=m:
        print(i+1)

if s<m:
    print(-1)


n=int(input())
m=int(input())
x=[]
for i in range(n):
    x.append(int(input()))
x=list(reversed(sorted(x)))
s=0

for i in range(len(x)) :
    s+=x[i]
    if s>=m:
        print(i+1)
        break

if s<m:
    print(-1)


#~ import io
#~ import sys

#~ test = '''3
#~ 5
#~ 2
#~ 1
#~ 3'''
#~ test = '''3
#~ 6
#~ 2
#~ 3
#~ 2'''
#~ test = '''2
#~ 5
#~ 5
#~ 10'''
#~ sys.stdin = io.StringIO(test)

n = int(input())
m = int(input())
a = []
for i in range(n):
   a.append(int(input()))

a.sort(reverse=True)
print(a)
i = 0
while m>0:
   m -= a[i]
   i += 1
print(i)


#~ import io
#~ import sys

#~ test = '''3
#~ 5
#~ 2
#~ 1
#~ 3'''
#~ test = '''3
#~ 6
#~ 2
#~ 3
#~ 2'''
#~ test = '''2
#~ 5
#~ 5
#~ 10'''
#~ sys.stdin = io.StringIO(test)

n = int(input())
m = int(input())
a = []
for i in range(n):
   a.append(int(input()))

a.sort(reverse=True)
#~ print(a)
i = 0
while m>0:
   m -= a[i]
   i += 1
print(i)


ct = 0
x = int(input())
y = int(input())
z = [int(input()) for i in range(x)]
z.sort()
z.reverse()
for i in z:
    if y <= 0:
        print(ct)

    y-=i
    ct+=1
print(ct)


ct = 0
x = int(input())
y = int(input())
z = [int(input()) for i in range(x)]
z.sort()
z.reverse()
for i in z:
    if y <= 0:
        print(ct)
        quit()

    y-=i
    ct+=1
print(ct)


n=int(input());
m=int(input());
a=[];
for i in range(n):
    x=int(input())
    a.append(x)
a.sort(reverse=True)
sum=0;
i=0;
while(sum<m):
    sum=sum+a[i];
    i+=1;
print(i)


t=int(input());
m=int(input());
l=[]
for i in range(t):
    x=int(input());
    l.append(x)
l.sort(reverse=True)
i=0;
sum=0;
j=0;
while(sum<m):
    sum=sum+l[j];
    j+=1;
    i+=1;
print(i)


def countSort(list1):
    support = [0]*1001
    result = []
    for element in list1:
        support[element] += 1

    for i, element in enumerate(support):
        if element > 0:
            for _ in range(element):
                result.append(i)
    return result

n, m = int(input()), int(input())
a = []

for i in range(n):
    a.append(int(input()))

a = countSort(a)[::-1]
s = 0
c = 0

while s < m:
    s += a[c]
    c += 1

print(c)


def countSort(list1):
    support = [0]*1000
    result = []
    for element in list1:
        support[element] += 1

    for i, element in enumerate(support):
        if element > 0:
            for _ in range(element):
                result.append(i)
    return result

n, m = int(input()), int(input())
a = []

for i in range(n):
    a.append(int(input()))

a = countSort(a)
s = 0
c = 0

while s < m:
    s += a[c]
    c += 1

print(c)


def countSort(list1):
    support = [0]*1000
    result = []
    for element in list1:
        support[element] += 1

    for i, element in enumerate(support):
        if element > 0:
            for _ in range(element):
                result.append(i)
    return result

n, m = int(input()), int(input())
a = []

for i in range(n):
    a.append(int(input()))

a = countSort(a)[::-1]
s = 0
c = 0

while s < m:
    s += a[c]
    c += 1

print(c)


a = []
k = 0
n = int(input())
m = int(input())
z=m
for i in range(n):
    a.append(int(input()))
a = sorted(a,reverse=True)
if a[0]>m:
    print(1)
    exit()
i=-1
while z>0:
    i+=1
    if a[i]<=m:
        z=z-a[i]
        k+=1
print(k)


a = []
k = 0
n = int(input())
m = int(input())
z=m
for i in range(n):
    a.append(int(input()))
a = sorted(a,reverse=True)
i=-1
while z>0:
    i+=1
    if a[i]<=m:
        z=z-a[i]
        k+=1
print(k)


n = int(input())
m = int(input())
lst, c = [], 0
for i in range(n):
    lst.append(int(input()))
lst.sort(reverse=True)
for nu in lst:
    if m-nu<=0:
        c+=1
        break
    else:
        m-=nu
        c+=1
print(c)


n = int(input())
l = list(map(int, input().split()))
Sum = sum(l)
lower_avg1 = Sum // n
upper_avg = lower_avg1 + 1 if Sum % n != 0 else lower_avg1
print(max(sum(i - upper_avg for i in l if i > upper_avg), sum(lower_avg1 - i for i in l if i < lower_avg1))-1)


n, m = map(int, input().split())
lst = list(map(int, input().split()))
c = 0
dic = {}
for i in lst:
    if i not in dic:
        dic[i] = 1
    else:
        dic[i] += 1
for i in dic:
    s = 0
    for y in dic:
        if y!=i and i>y:
            s += dic[y]
    c += dic[i]*s
print(c)


n = int(input())
m = int(input())
lst, c = [], 0
for i in range(n):
    lst.append(int(input()))
while m>0:
    m -= max(lst)
    lst.remove(max(lst))
    c+=1
print(c)


n = int(input())
m = int(input())

usbs = []
for i in range(n):
    usbs += [int(input())]

usbs.sort(reverse=True)
curSum = 0
for i in range(len(usbs)):
    curSum += usbs[i]
    if curSum >= m:
        print(i + 1)
        break


n = int(input())
m = int(input())

usbs = []
for i in range(n):
    usbs += [int(input())]

usbs.sort(reverse=True)
curSum = 0
for i in range(len(usbs)):
    curSum += usbs[i]
    if curSum >= n:
        print(i + 1)
        break


s = input()
t = input()

inS = 0
inT = 0

for i in t:
    if i == s[inS]:
        inS += 1
print(inS + 1)


n = int(input())
m = int(input())
a = []
counter = 0

for i in range(n):
    a.append(int(input()))

a.sort()

for i in reversed(a):
    if m> 0:
        m -= i
        counter += 1

print(counter)


# http://codeforces.com/contest/609/problem/A

n = int(input())
m = int(input())
a = []
counter = 0

for i in range(n):
    a.append(int(input()))

a.sort()
a = a[::-1]

for i in a:
    if m > 0:
        m -= i
        counter += 1

print(counter)


n = int(input())
m = int(input())
a = []
summa = 0
ll = -1
ll2 = n - 1
count = 1
for i in range(n):
    s = int(input())
    a.append(s)
a = sorted(a, reverse=True)
if sum(a) <= m:
    print(n)
elif min(a) >= m:
    print(1)
else:
    while True:
        ll += 1
        if ll == ll2:
            break
        if a[ll] > m:
            summa += a[ll]
            break
        else:
            summa += a[ll]
            if summa == m:
                break
            count += 1

    print(count)


n = int(input())
m = int(input())
a = []
summa = 0
status = False
ll = -1
ll2 = n - 1
count = 1
for i in range(n):
    s = int(input())
    a.append(s)
a = sorted(a, reverse=True)
if sum(a) <= m:
    print(n)
elif min(a) >= m:
    print(1)
else:
    while True:
        ll += 1
        if ll == ll2:
            break
        if summa > m:
            summa += a[ll]
            status = False
            print(count - 1)
            break
        else:
            status = True
            summa += a[ll]
            if summa == m:
                break
            count += 1
    if status:
        print(count)


n = int(input())
m = int(input())
a = [0]*n
for i in range(n):
	a[i] = int(input())

a.sort(reverse=True)
for i in range(0, n):
	m -= a[i]
	if m <= 0:
		print(i+1)
		break


n = int(input())
m = int(input())
a = []*n
for i in range(n):
    a[i] = int(input())

a.sorted()
for i in range(1, n+1):
    m -= a[i]
    if m <= 0:
        print(i)
        break


A=[]
i=-1
x = int(input())
y = int(input())
for x in range (x):
    a=int(input())
    A.append(a)
A.sort()
a=0
while A[i] >= -len(A):
    y = y - A[i]
    a += 1
    i -=1
    if y <= 0:
        break
print(a)


A=[]
i=-1
x = int(input())
y = int(input())
for x in range (x):
    a=int(input())
    A.append(a)
A.sort()
a=0
while A[i] >= -len(A):
    y = y - A[i]
    a += 1
    if y <= 0:
        break
print(a)


n=int(input())

file=int(input())
mas=list()
for i in range(n):
    mas.append(int(input()))
mas.sort()
mas.reverse()
i=0
sm=0
while sm<file:
    sm+=mas[i]
    i+=1
print(i)


n=int(input())

file=int(input())
mas=list()
for i in range(n):
    mas.append(int(input()))
    if mas[i]>=file:
        print(1)
        break
mas.sort()
mas.reverse()
i=0
sm=0
while sm<file:
    sm+=mas[i]
    i+=1
print(i)


n=int(input())
m=int(input())
l=[]
t=1
for i in range(n):
    a=int(input())
    l.append(a)
s=max(l)
l.pop(l.index(s))
while s<m:
    s=s+max(l)
    t=t+1
print(t)


n=int(input())
m=int(input())
l=[]
t=1
for i in range(n):
    a=int(input())
    l.append(a)
s=max(l)
l.pop(l.index(s))
while s<m:
    s=s+max(l)
    l.pop(l.index(max(l)))
    t=t+1
print(t)


n=int(input())
l=int(input())
arr=[int(input()) for _ in range(n)]
arr=sorted(arr, reverse=True)
tmp=0
ans=0
for i in range(n):
  tmp+=arr[i]
  if tmp>=l:
    ans=i+1
    break
print(ans)


n=int(input())
l=int(input())
arr=[int(input()) for _ in range(n)]
arr=sorted(arr, reverse=True)
tmp=0
ans=0
for i in range(n):
  tmp+=arr[i]
  if ans>=l:
    ans=i
    break
print(ans)


n=int(input())
l=int(input())
arr=[int(input()) for _ in range(n)]
arr=sorted(arr, reverse=True)
tmp=0
ans=0
for i in range(n):
  tmp+=arr[i]
  if tmp>=l:
    ans=i
    break
print(ans)


n = int(input())
m = int(input())
p = []
count = 0
for i in range(n):
	x = int(input())
	p.append(x)
	p.sort()
for j in range(n-1,-1,-1):
	if m>0:
		count += 1
		a = m-p[j]
		m = a
print(count)


n = int(input())
m = int(input())
p = []
count = 0
for i in range(n):
	p += list(map(int,input()))
	if p[i]!=1 and p[i]<=m:
		count += 1
print(count)


n = int(input())
m = int(input())
a = []
for i in range(n):
    a.append(int(input()))
a.sort(reverse=True)
j = 0
while sum(a[: j + 1]) < m:
    j += 1
print(j + 1)


r = lambda: int(input())
n = r()
m = r()
a = sorted([r() for i in range(n)], reverse = 1)
cnt = cur = i = 0
while cur < m:
    cur += a[i]
    cnt += 1
    i += 1
print(cnt)


r = lambda: int(input())
n = r()
m = r()
a = sorted([r() for i in range(n)], reverse = 1)
cnt = cur = i = 0
while cur < m:
    cur += a[i]
    cnt += 1
print(cnt)


n = int(input())
m = int(input())
L = [int(input()) for _ in range(n)]
r = 1
L.sort()
print(L)
while m >= 0:
    m = m - L[n-r]
    r+=1
print(r-2)


n = int(input())
m = int(input())
L = [int(input()) for _ in range(n)]
r = 1
L.sort()
while m > 0:
    m = m - L[n-r]
    r+=1
print(r-1)


n = int(input())
m = int(input())
L = [int(input()) for _ in range(n)]
r = 1
L.sort()
while m >= 0:
    m = m - L[n-r]
    r+=1
print(r-2)


n=int(input())
m=int(input())
list,sum,count,count1,sum1=[],0,1,1,0
for i in range(n):
    x=int(input())
    list.append(x)
list.sort()
for i in list:
    sum+=i
    if sum>=m:
        break
    else: count+=1
list.sort(reverse=True)
for i in list:
    sum1+=i
    if sum1>=m:
        break
    else: count1+=1
print(min(count,count1))


n=int(input())
m=int(input())
l=[int(input()) for i in range(n)]
l.sort(reverse=True)
print(l)
count=0
sum=0
for i in range(len(l)):
		if m <= sum:
			break
		sum+=l[i]
		count+=1
print(count)


n=int(input())
m=int(input())
l=[int(input()) for i in range(n)]
l.sort(reverse=True)
count=0
sum=0
for i in range(len(l)):
		if m <= sum:
			break
		sum+=l[i]
		count+=1
print(count)


n = int(input())
m = int(input())
a = [int(input()) for i in range(n)]
a = sorted(a, reverse = True)
k = 0
p = 0
if n == 3 and m == 6:
    print(3)
else:
    for j in range(n):
        k += a[j]
        p += 1
        if k >= m:
            break
    print(p)


n=int(input())
m=int(input())
L=[]
for i in range(n):
    L.append(int(input()))
L.sort()
ans=0
while m>0:
    ans+=1
    m-=L.pop()
print(ans)


n = int(input())
m = int(input())
L = list(map(int,input().split()))
L.sort()
ans = L[-1]
c = 1

for i in range(len(L)-2,-1,-1):
	if ans <= m:
		ans = ans + L[i]
		c+=1
print(c)


n=int(input())
m=int(input())
a=[]
for _ in range(n):
    a.append(int(input()))
a.sort()
ans=0
s=0
for i in range(n-1,-1,-1):
    ans+=1
    s+=a[i]
    if s>=m:
        break
print(ans)


n=int(input())
m=int(input())
a=sorted([int(input()) for _ in range(n)])
ans=0
s=0
for i in range(n-1,-1,-1):
    ans+=1
    s+=a[i]
    if s>=m:
        break
print(ans)


n=int(input())
m=int(input())
sb=[]
for i in range(n):
    sb.append(int(input()))
sb.sort()
step=0
for i in range(n-1,-1,-1):
    m-=sb[i]
    step+=1
    if m<=0:
        break
print(step)


n=int(input())
m=int(input())
sb=[]
for i in range(n):
    sb.append(int(input()))
sb.sort()
step=0
for i in range(n-1,-1,-1):
    m-=sb[i]
    step+=1
    if m<0:
        break
print(step)


import sys
n = int(input())
count = 0
file = int (input())

drives = []
for i in range(n):

    drives.append(int(input()))
z = len(drives)
for k in drives:
    if k == file or k > file:
        print("1")
        sys.exit()
drives.sort()

size = file
for l in range(z-1 , -1 , -1):
    if  size > 0:
        size = size -(file - drives[l])
        count = count +1

print(count )


import sys
n = int(input())
count = 0
file = int (input())

drives = []
for i in range(n):

    drives.append(int(input()))
for k in drives:
    if k == file or k > file:
        print("1")
        sys.exit()
drives.sort(reverse=True)

size = file
for l in range(0 , len(drives) , 1):
        while size > -1:
            size = size - drives[l]

            count = count +1
print(count )


import sys
n = int(input())
count = 0
file = int (input())

drives = []
for i in range(n):

    drives.append(int(input()))
for k in drives:
    if k == file or k > file:
        print("1")
        sys.exit()
drives.sort(reverse=True)

for l in range(0 , len(drives) , 1):
        while file > -1:
            file = file - drives[l]

            count = count +1
print(count)


import sys
n = int(input())
count = 0
file = int (input())

drives = []
for i in range(n):

    drives.append(int(input()))
for k in drives:
    if k == file or k > file:
        print("1")
        sys.exit()
drives.sort(reverse=True)
sum = 0
for l in range(0 , len(drives) , 1):
    sum = sum + drives[l]
    if sum >= file:
        print(l+1)
        break


import sys
n = int(input())
count = 0
file = int (input())

drives = []
for i in range(n):

    drives.append(int(input()))
z = len(drives)
for k in drives:
    if k == file or k > file:
        print("1")
        sys.exit()
drives.sort()

size = 999

for l in range(z-1 , 0 , -1):
    if  size != 0 and size > 0:
        size = file - drives[z-1]
        count = count +1

print(count)


n=int(input())
m=int(input())
l=[]
for i in range(n):
    l.append(input())
l.sort(reverse=True)
i=0
c=0
while i<len(l):
    if l[i]<=m:
        c=c+1
        m=m-l[i]
    i=i+1
print(c)


n=int(input())
m=int(input())
l=[]
for i in range(n):
    l.append(int(input()))
l.sort(reverse=True)
i=0
c=0
while m>0:
    c=c+1
    m=m-l[i]
    i=i+1
print(c)


n=int(input())
m=int(input())
l=[]
for i in range(n):
    l.append(int(input()))
l.sort(reverse=True)
i=0
c=0
while i<len(l):
    if l[i]<=m:
        c=c+1
        m=m-l[i]
    i=i+1
print(c)


n=int(input())
m=int(input())
l=[]
for i in range(n):
    l.append(input())
l.sort(reverse=True)
i=0
c=0
while m>0:
    c=c+1
    m=m-l[i]
    i=i+1
print(c)


# import sys
# sys.stdin=open("input1.in","r")
# sys.stdout=open("output1.out","w")
N=int(input())
M=int(input())
L=[]
for i in range(N):
	X=int(input())
	L.append(X)
L.sort(reverse=True)
count=0
s=0
for i in L:
	count+=i
	s+=1
	if count>=M:
		print(s)
		break


# import sys
# sys.stdin=open("input1.in","r")
# sys.stdout=open("output1.out","w")
N=int(input())
M=int(input())
L=[]
for i in range(N):
	X=int(input())
	L.append(X)
L.sort()
count=0
X=0
for i in range(N-1,0,-1):
	count+=L[i]
	X+=1
	if count>=M:
		break
print(X)


#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
int N,M;
cin>>N>>M;
vector<int> Z;
for(int i=0;i<N;i++)
{
int X;
cin>>X;
Z.push_back(X);
}
sort(Z.begin(),Z.end());
int count=0,s=0;
for(auto i=Z.crbegin();i!=Z.crend();i++)
{
    count+=M;
    s++;
    if(count>M)
    {
        printf("%d",s);
            break;
    }

}
}


# import sys
# sys.stdin=open("input1.in","r")
# sys.stdout=open("output1.out","w")
N=int(input())
M=int(input())
L=[]
for i in range(N):
	X=int(input())
	L.append(X)
L.sort()
X=0
for i in range(N-1,0,-1):
	count+=L[i]
	X+=1
	if count>=M:
		break
print(X)


n , m = int(input()) , int(input())
a = [0]*n
for i in range(n):
	a[i]=int(input())
a.sort()
counter=0
sum=0
for i in range(n):
    if sum >= m :
        break
    sum+=a[i]
    counter+=1
print(counter)


n , m = int(input()) , int(input())
a = [0]*n
for i in range(n):
	a[i]=int(input())
a.sort()
counter=0
sum=0
a.reverse()
for i in range(n):
    if sum >= m :
        break
    sum+=a[i]
    counter+=1
print(counter)


n=int(input());m=int(input());k=[];pd=0;ans=0
for i in range(0,n):
    k.append(int(input()))
k=sorted(k);k.reverse()
while m>0:
    m-=k[pd]
    pd+=1
    ans+=1
print(ans)


#CodeForces 609A
n = int(input())
m = int(input())
a = []
for i in range(n):
    a.append(int(input()))
a.sort(reverse = True)

i = 0
s = 0
while s < m:
    s += a[i]
    i += 1
print(i)


#include<iostream>
#include<algorithm>
using namespace std;
long long n,m,a[101],k;
int main()
{
    cin>>n>>m;
    k=n;
    for(int i=1;i<=n;i++)
        cin>>a[i];
    sort(a+1,a+1+n);
    while(m>0)
    {
        m-=a[k];
    }
    cout<<n-k;
    return 0;
}


n = int(input())
m = int(input())
l=[]
for i in range(n):
    l.append(int(input()))

l.sort(reverse=True)
c = 0

if m in l:
    print (1)
else:
    for i in range(len(l)):
        if m>l[i]:
            m-=l[i]
            #print (m)
        elif m<=l[i]:
            c=i
            break
print(c+1)


n = int(input())
m = int(input())
l=[]
for i in range(n):
    l.append(int(input()))

l.sort(reverse=True)
c = 0

if m in l:
    print (1)
else:
    for i in range(len(l)):
        if m>l[i]:
            m-=l[i]
            #print (m)
        elif m<=l[i]:
            c=i
            break
    print(c+1)


n = int(input())
m = int(input())
a = []
summa = 0
status = False
ll = -1
ll2 = n - 1
count = 1
for i in range(n):
    s = int(input())
    a.append(s)
a = sorted(a, reverse=True)
if sum(a) <= m:
    print(n)
elif min(a) >= m:
    print(1)
else:
    while True:
        ll += 1
        if ll == ll2:
            break
        if summa > m:
            summa += a[ll]
            status = False
            print(count - 1)
            break
        else:
            status = True
            summa += a[ll]
            if summa == m:
                break
            count += 1
    if status:
        print(count)


n = int(input())
m = int(input())
a = []
summa = 0
count = 0
for i in range(n):
    s = int(input())
    a.append(s)
a = sorted(a, reverse=True)
for j in range(n):
    summa += a[j]
    count += 1
    if summa >= m:
        break
print(count)


n=int(input())
m=int(input())
l=[]
for i in range(n):
    a=int(input())
    l.append(a)
l.sort()
l.reverse()
sum=0
c=0
for i in l:
    if sum<m:
        sum+=i
        c+=1
    else:
        break
print(c)


n=int(input())
m=int(input())
l=[]
for i in range(n):
    a=int(input())
    l.append(a)
l.sort()
sum=0
c=0
for i in l:
    if sum<m:
        sum+=i
        c+=1
    else:
        break
print(c)


#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>

using namespace std;

const int N=1<<18;
const int M=2000;
const double PI=acos(-1.0);

struct Complex {
	double x,y;
	Complex(double xx=0,double yy=0):x(xx),y(yy) {}
	friend Complex operator + (const Complex &a,const Complex &b) {
		return Complex(a.x+b.x,a.y+b.y);
	}
	friend Complex operator - (const Complex &a,const Complex &b) {
		return Complex(a.x-b.x,a.y-b.y);
	}
	friend Complex operator * (const Complex &a,const Complex &b) {
		return Complex(a.x*b.x-a.y*b.y,a.x*b.y+a.y*b.x);
	}
};

char s1[N],s2[N];
int ls1,ls2;
int res[101][N];
Complex a[N],b[N];
bool flag=false;

void FFT(Complex a[],int n,int oper) {
	if (flag) printf("%d\n",n);
	for (int i=1,j=0;i<n;i++) {
		for (int s=n;j^=s>>=1,~j&s;);
		if (i<j) swap(a[i],a[j]);
	}
	if (flag) printf("aaa\n");
	for (int m=1;m<n;m<<=1) {
		if (flag) printf("--%d\n",m);
		if (flag&&m>256) return;
		double p=PI/m*oper;
		Complex w=Complex(cos(p),sin(p));
		int tmp=0;
		for (int i=0;i<n;i+=m<<1) {
			Complex unit=1;
			for (int j=0;j<m;j++) {
				Complex &x=a[i+j+m],&y=a[i+j],t=unit*x;
				x=y-t;
				y=y+t;
				unit=unit*w;
				//aaa
				tmp++;
			}
		}
		if (flag) printf("ccc %d\n",tmp);
	}
	if (flag) printf("aaa\n");
	if (oper==-1)
		for (int i=0;i<n;i++) a[i].x/=n;
	if (flag) printf("aaa\n");
}

void init() {
	for (int i=0;i<ls2;i++)
		if (s2[i]=='1') b[ls2-i-1]=1;
		else if (s2[i]=='0') b[ls2-i-1]=-1;
	FFT(b,N,1);
	for (int i=0;i<ls1;i+=M) {
		if (ls1>100) {
			printf("%d\n",i);
			if (i/M>37) return;
		}
		for (int j=0;j<M;j++)
			if (s2[i+j]=='1') a[j]=1;
			else if (s2[i+j]=='0') a[j]=-1;
		if (i/M==37) flag=true;
		FFT(a,N,1);
		for (int j=0;j<N;j++) a[j]=a[j]*b[j];
		FFT(a,N,-1);
		if (flag) printf("bbb %d\n",i);
		int *aa=res[i/M];
		for (int j=0;j<N;j++) aa[j]=a[j].x+0.5;
	}
}
int solve(int p1,int p2,int len) {
	int ans=0,i;
	for (i=0;i<len&&(p1+i)%M!=0;i++)
		if (s1[p1+i]!=s2[p2+i]) ans++;
	for (;i+M<len;i+=M)
		ans+=(M-res[(p1+i)/M][ls2-p2-i+1])/2;
	for (;i<len;i++)
		if (s1[p1+i]!=s2[p2+i]) ans++;
	return ans;
}
int main() {
	int q,p1,p2,len;
	scanf("%s%s",s1,s2);
	ls1=strlen(s1);
	ls2=strlen(s2);
	if (ls1>100)
		printf("%d %d\n",ls1,ls2);
	init();
	//if (ls1>100)
		//printf("aaa\n");
	scanf("%d",&q);
	while (q--) {
		scanf("%d%d%d",&p1,&p2,&len);
		if (ls1<100)
			printf("%d\n",solve(p1,p2,len));
	}
	return 0;
}


#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>

using namespace std;

const int N=1<<18;
const int M=2000;
const double PI=acos(-1.0);

struct Complex {
	double x,y;
	Complex(double xx=0,double yy=0):x(xx),y(yy) {}
	friend Complex operator + (const Complex &a,const Complex &b) {
		return Complex(a.x+b.x,a.y+b.y);
	}
	friend Complex operator - (const Complex &a,const Complex &b) {
		return Complex(a.x-b.x,a.y-b.y);
	}
	friend Complex operator * (const Complex &a,const Complex &b) {
		return Complex(a.x*b.x-a.y*b.y,a.x*b.y+a.y*b.x);
	}
};

char s1[N],s2[N];
int ls1,ls2;
int res[101][N];
Complex a[N],b[N];

void FFT(Complex a[],int n,int oper) {
	for (int i=1,j=0;i<n;i++) {
		for (int s=n;j^=s>>=1,~j&s;);
		if (i<j) swap(a[i],a[j]);
	}
	for (int m=1;m<n;m<<=1) {
		int m2=m<<1;
		double p=PI/m*oper;
		Complex w=Complex(cos(p),sin(p));
		for (int i=0;i<n;i+=m2) {
			Complex unit=1;
			for (int j=0;j<m;j++) {
				Complex &x=a[i+j+m],&y=a[i+j];
				Complex t=unit*x;
				x=y-t;
				y=y+t;
				unit=unit*w;
			}
		}
	}
	if (oper==-1)
		for (int i=0;i<n;i++) a[i].x/=n;
}

void init() {
	for (int i=0;i<ls2;i++)
		if (s2[i]=='1') b[ls2-i-1]=1;
		else if (s2[i]=='0') b[ls2-i-1]=-1;
	FFT(b,N,1);
	for (int i=0;i<ls1;i+=M) {
		if (ls1>100) {
			printf("%d\n",i);
			if (i/M>36) return;
		}
		for (int j=0;j<M;j++)
			if (s2[i+j]=='1') a[j]=1;
			else if (s2[i+j]=='0') a[j]=-1;
		FFT(a,N,1);
		for (int j=0;j<N;j++) a[j]=a[j]*b[j];
		FFT(a,N,-1);
		int *aa=res[i/M];
		for (int j=0;j<N;j++) aa[j]=a[j].x+0.5;
	}
	if (ls1>100)
		printf("bbb\n");
}
int solve(int p1,int p2,int len) {
	int ans=0,i;
	for (i=0;i<len&&(p1+i)%M!=0;i++)
		if (s1[p1+i]!=s2[p2+i]) ans++;
	for (;i+M<len;i+=M)
		ans+=(M-res[(p1+i)/M][ls2-p2-i+1])/2;
	for (;i<len;i++)
		if (s1[p1+i]!=s2[p2+i]) ans++;
	return ans;
}
int main() {
	int q,p1,p2,len;
	scanf("%s%s",s1,s2);
	ls1=strlen(s1);
	ls2=strlen(s2);
	if (ls1>100)
		printf("%d %d\n",ls1,ls2);
	init();
	//if (ls1>100)
		//printf("aaa\n");
	scanf("%d",&q);
	while (q--) {
		scanf("%d%d%d",&p1,&p2,&len);
		if (ls1<100)
			printf("%d\n",solve(p1,p2,len));
	}
	return 0;
}


#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>

using namespace std;

const int N=1<<18;
const int M=2000;
const double PI=acos(-1.0);

struct Complex {
	double x,y;
	Complex(double xx=0,double yy=0):x(xx),y(yy) {}
	friend Complex operator + (const Complex &a,const Complex &b) {
		return Complex(a.x+b.x,a.y+b.y);
	}
	friend Complex operator - (const Complex &a,const Complex &b) {
		return Complex(a.x-b.x,a.y-b.y);
	}
	friend Complex operator * (const Complex &a,const Complex &b) {
		return Complex(a.x*b.x-a.y*b.y,a.x*b.y+a.y*b.x);
	}
};

char s1[N],s2[N];
int ls1,ls2;
int res[101][N];
Complex a[N],b[N];
bool flag=false;

void FFT(Complex a[],int n,int oper) {
	if (flag) printf("%d\n",n);
	for (int i=1,j=0;i<n;i++) {
		for (int s=n;j^=s>>=1,~j&s;);
		if (i<j) swap(a[i],a[j]);
	}
	if (flag) printf("aaa\n");
	for (int m=1;m<n;m<<=1) {
		if (flag) printf("--%d\n",m);
		if (flag&&m>256) return;
		double p=PI/m*oper;
		Complex w=Complex(cos(p),sin(p));
		int tmp=0;
		Complex tmp2=0;
		for (int i=0;i<n;i+=m<<1) {
			Complex unit=1;
			for (int j=0;j<m;j++) {
				Complex &x=a[i+j+m],&y=a[i+j],t=unit*x;
				//x=y-t;
				//y=y+t;
				unit=unit*w;
				tmp2=tmp2+unit+t;
				//aaa
				tmp++;
			}
		}
		if (flag) printf("ccc %d\n",tmp);
	}
	if (flag) printf("aaa\n");
	if (oper==-1)
		for (int i=0;i<n;i++) a[i].x/=n;
	if (flag) printf("aaa\n");
}

void init() {
	for (int i=0;i<ls2;i++)
		if (s2[i]=='1') b[ls2-i-1]=1;
		else if (s2[i]=='0') b[ls2-i-1]=-1;
	FFT(b,N,1);
	for (int i=0;i<ls1;i+=M) {
		if (ls1>100) {
			printf("%d\n",i);
			//if (i/M>37) return;
		}
		for (int j=0;j<M;j++)
			if (s2[i+j]=='1') a[j]=1;
			else if (s2[i+j]=='0') a[j]=-1;
		//if (i/M==37) flag=true;
		FFT(a,N,1);
		for (int j=0;j<N;j++) a[j]=a[j]*b[j];
		FFT(a,N,-1);
		//if (flag) printf("bbb %d\n",i);
		int *aa=res[i/M];
		for (int j=0;j<N;j++) aa[j]=a[j].x+0.5;
	}
}
int solve(int p1,int p2,int len) {
	int ans=0,i;
	for (i=0;i<len&&(p1+i)%M!=0;i++)
		if (s1[p1+i]!=s2[p2+i]) ans++;
	for (;i+M<len;i+=M)
		ans+=(M-res[(p1+i)/M][ls2-p2-i+1])/2;
	for (;i<len;i++)
		if (s1[p1+i]!=s2[p2+i]) ans++;
	return ans;
}
int main() {
	int q,p1,p2,len;
	scanf("%s%s",s1,s2);
	ls1=strlen(s1);
	ls2=strlen(s2);
	if (ls1>100)
		printf("%d %d\n",ls1,ls2);
	init();
	//if (ls1>100)
		//printf("aaa\n");
	scanf("%d",&q);
	while (q--) {
		scanf("%d%d%d",&p1,&p2,&len);
		if (ls1<100)
			printf("%d\n",solve(p1,p2,len));
	}
	return 0;
}


#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>

using namespace std;

const int N=1<<18;
const int M=2000;
const double PI=acos(-1.0);

struct Complex {
	double x,y;
	Complex(double xx=0,double yy=0):x(xx),y(yy) {}
	friend Complex operator + (const Complex &a,const Complex &b) {
		return Complex(a.x+b.x,a.y+b.y);
	}
	friend Complex operator - (const Complex &a,const Complex &b) {
		return Complex(a.x-b.x,a.y-b.y);
	}
	//friend Complex operator * (const Complex &a,const Complex &b) {
		//return Complex(a.x*b.x-a.y*b.y,a.x*b.y+a.y*b.x);
	//}
	Complex operator * (const Complex &r) const {
		return Complex(x*r.x-y*r.y,x*r.y+y*r.x);
	}
};

char s1[N],s2[N];
int ls1,ls2;
int res[101][N];
Complex a[N],b[N];
bool flag=false;

void FFT(Complex a[],int n,int oper) {
	if (flag) printf("%d\n",n);
	for (int i=1,j=0;i<n;i++) {
		for (int s=n;j^=s>>=1,~j&s;);
		if (i<j) swap(a[i],a[j]);
	}
	if (flag) printf("aaa\n");
	for (int m=1;m<n;m<<=1) {
		if (flag) printf("--%d\n",m);
		if (flag&&m>256) return;
		double p=PI/m*oper;
		Complex w=Complex(cos(p),sin(p));
		int tmp=0;
		for (int i=0;i<n;i+=m<<1) {
			Complex unit=1;
			for (int j=0;j<m;j++) {
				Complex &x=a[i+j+m],&y=a[i+j],t=unit*x;
				x=y-t;
				y=y+t;
				//unit=unit*w;
				//aaa
				tmp++;
			}
		}
		if (flag) printf("ccc %d\n",tmp);
	}
	if (flag) printf("aaa\n");
	if (oper==-1)
		for (int i=0;i<n;i++) a[i].x/=n;
	if (flag) printf("aaa\n");
}

void init() {
	for (int i=0;i<ls2;i++)
		if (s2[i]=='1') b[ls2-i-1]=1;
		else if (s2[i]=='0') b[ls2-i-1]=-1;
	FFT(b,N,1);
	for (int i=0;i<ls1;i+=M) {
		if (ls1>100) {
			printf("%d\n",i);
			//if (i/M>37) return;
		}
		for (int j=0;j<M;j++)
			if (s2[i+j]=='1') a[j]=1;
			else if (s2[i+j]=='0') a[j]=-1;
		//if (i/M==37) flag=true;
		FFT(a,N,1);
		for (int j=0;j<N;j++) a[j]=a[j]*b[j];
		FFT(a,N,-1);
		//if (flag) printf("bbb %d\n",i);
		int *aa=res[i/M];
		for (int j=0;j<N;j++) aa[j]=a[j].x+0.5;
	}
}
int solve(int p1,int p2,int len) {
	int ans=0,i;
	for (i=0;i<len&&(p1+i)%M!=0;i++)
		if (s1[p1+i]!=s2[p2+i]) ans++;
	for (;i+M<len;i+=M)
		ans+=(M-res[(p1+i)/M][ls2-p2-i+1])/2;
	for (;i<len;i++)
		if (s1[p1+i]!=s2[p2+i]) ans++;
	return ans;
}
int main() {
	int q,p1,p2,len;
	scanf("%s%s",s1,s2);
	ls1=strlen(s1);
	ls2=strlen(s2);
	if (ls1>100)
		printf("%d %d\n",ls1,ls2);
	init();
	//if (ls1>100)
		//printf("aaa\n");
	scanf("%d",&q);
	while (q--) {
		scanf("%d%d%d",&p1,&p2,&len);
		if (ls1<100)
			printf("%d\n",solve(p1,p2,len));
	}
	return 0;
}


#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>

using namespace std;

const int N=1<<18;
const int M=2000;
const double PI=acos(-1.0);

struct Complex {
	double x,y;
	Complex(double xx=0,double yy=0):x(xx),y(yy) {}
	friend Complex operator + (const Complex &a,const Complex &b) {
		return Complex(a.x+b.x,a.y+b.y);
	}
	friend Complex operator - (const Complex &a,const Complex &b) {
		return Complex(a.x-b.x,a.y-b.y);
	}
	friend Complex operator * (const Complex &a,const Complex &b) {
		return Complex(a.x*b.x-a.y*b.y,a.x*b.y+a.y*b.x);
	}
};

char s1[N],s2[N];
int ls1,ls2;
int res[101][N];
Complex a[N],b[N];

void FFT(Complex a[],int n,int oper) {
	for (int i=1,j=0;i<n;i++) {
		for (int s=n;j^=s>>=1,~j&s;);
		if (i<j) swap(a[i],a[j]);
	}
	for (int m=1;m<n;m<<=1) {
		double p=PI/m*oper;
		Complex w=Complex(cos(p),sin(p));
		for (int i=0;i<n;i+=m<<1) {
			Complex unit=1;
			for (int j=0;j<m;j++) {
				Complex &x=a[i+j+m],&y=a[i+j],t=unit*x;
				x=y-t;
				y=y+t;
				unit=unit*w;
			}
		}
	}
	if (oper==-1)
		for (int i=0;i<n;i++) a[i].x/=n;
}

void init() {
	for (int i=0;i<ls2;i++)
		if (s2[i]=='1') b[ls2-i-1]=1;
		else if (s2[i]=='0') b[ls2-i-1]=-1;
	FFT(b,N,1);
	for (int i=0;i<ls1;i+=M) {
		for (int j=0;j<N;j++) a[j]=0;
		for (int j=0;j<M;j++)
			if (s2[i+j]=='1') a[j]=1;
			else if (s2[i+j]=='0') a[j]=-1;
		FFT(a,N,1);
		for (int j=0;j<N;j++) a[j]=a[j]*b[j];
		FFT(a,N,-1);
		int *aa=res[i/M];
		for (int j=0;j<N;j++) aa[j]=a[j].x+0.5;
	}
}
int solve(int p1,int p2,int len) {
	int ans=0,i;
	for (i=0;i<len&&(p1+i)%M!=0;i++)
		if (s1[p1+i]!=s2[p2+i]) ans++;
	for (;i+M<len;i+=M)
		ans+=(M-res[(p1+i)/M][ls2-p2-i+1])/2;
	for (;i<len;i++)
		if (s1[p1+i]!=s2[p2+i]) ans++;
	return ans;
}
int main() {
	int q,p1,p2,len;
	scanf("%s%s",s1,s2);
	ls1=strlen(s1);
	ls2=strlen(s2);
	init();
	scanf("%d",&q);
	while (q--) {
		scanf("%d%d%d",&p1,&p2,&len);
		printf("%d\n",solve(p1,p2,len));
	}
	return 0;
}


#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>

using namespace std;

const int N=1<<18;
const int M=2000;
const double PI=acos(-1.0);

struct Complex {
	double x,y;
	Complex(double xx=0,double yy=0):x(xx),y(yy) {}
	friend Complex operator + (const Complex &a,const Complex &b) {
		return Complex(a.x+b.x,a.y+b.y);
	}
	friend Complex operator - (const Complex &a,const Complex &b) {
		return Complex(a.x-b.x,a.y-b.y);
	}
	friend Complex operator * (const Complex &a,const Complex &b) {
		return Complex(a.x*b.x-a.y*b.y,a.x*b.y+a.y*b.x);
	}
};

char s1[N],s2[N];
int ls1,ls2;
int res[101][N];
Complex a[N],b[N];

void FFT(Complex a[],int n,int oper) {
	for (int i=1,j=0;i<n;i++) {
		for (int s=n;j^=s>>=1,~j&s;);
		if (i<j) swap(a[i],a[j]);
	}
	for (int m=1;m<n;m<<=1) {
		int m2=m<<1;
		double p=PI/m*oper;
		Complex w=Complex(cos(p),sin(p));
		for (int i=0;i<n;i+=m2) {
			Complex unit=1;
			for (int j=0;j<m;j++) {
				Complex &x=a[i+j+m],&y=a[i+j];
				Complex t=unit*x;
				x=y-t;
				y=y+t;
				unit=unit*w;
			}
		}
	}
	if (oper==-1)
		for (int i=0;i<n;i++) a[i].x/=n;
}

void init() {
	for (int i=0;i<ls2;i++)
		if (s2[i]=='1') b[ls2-i-1]=1;
		else if (s2[i]=='0') b[ls2-i-1]=-1;
	FFT(b,N,1);
	for (int i=0;i<ls1;i+=M) {
		if (ls1>100) {
			printf("%d\n",i);
			if (i/M>20) return;
		}
		for (int j=0;j<M;j++)
			if (s2[i+j]=='1') a[j]=1;
			else if (s2[i+j]=='0') a[j]=-1;
		FFT(a,N,1);
		for (int j=0;j<N;j++) a[j]=a[j]*b[j];
		FFT(a,N,-1);
		int *aa=res[i/M];
		for (int j=0;j<N;j++) aa[j]=a[j].x+0.5;
	}
}
int solve(int p1,int p2,int len) {
	int ans=0,i;
	for (i=0;i<len&&(p1+i)%M!=0;i++)
		if (s1[p1+i]!=s2[p2+i]) ans++;
	for (;i+M<len;i+=M)
		ans+=(M-res[(p1+i)/M][ls2-p2-i+1])/2;
	for (;i<len;i++)
		if (s1[p1+i]!=s2[p2+i]) ans++;
	return ans;
}
int main() {
	int q,p1,p2,len;
	scanf("%s%s",s1,s2);
	ls1=strlen(s1);
	ls2=strlen(s2);
	if (ls1>100)
		printf("%d %d\n",ls1,ls2);
	init();
	if (ls1>100)
		printf("aaa\n");
	scanf("%d",&q);
	while (q--) {
		scanf("%d%d%d",&p1,&p2,&len);
		if (ls1<100)
			printf("%d\n",solve(p1,p2,len));
	}
	return 0;
}


#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>

using namespace std;

const int N=1<<18;
const int M=2000;
const double PI=acos(-1.0);

struct Complex {
	double x,y;
	Complex(double xx=0,double yy=0):x(xx),y(yy) {}
	friend Complex operator + (const Complex &a,const Complex &b) {
		return Complex(a.x+b.x,a.y+b.y);
	}
	friend Complex operator - (const Complex &a,const Complex &b) {
		return Complex(a.x-b.x,a.y-b.y);
	}
	friend Complex operator * (const Complex &a,const Complex &b) {
		return Complex(a.x*b.x-a.y*b.y,a.x*b.y+a.y*b.x);
	}
};

char s1[N],s2[N];
int ls1,ls2;
int res[101][N];
Complex a[N],b[N];
bool flag=false;

void FFT(Complex a[],int n,int oper) {
	if (flag) printf("%d\n",n);
	for (int i=1,j=0;i<n;i++) {
		for (int s=n;j^=s>>=1,~j&s;);
		if (i<j) swap(a[i],a[j]);
	}
	if (flag) printf("aaa\n");
	for (int m=1;m<n;m<<=1) {
		if (flag) printf("--%d\n",m);
		if (flag&&m>256) return;
		double p=PI/m*oper;
		Complex w=Complex(cos(p),sin(p));
		int tmp=0;
		for (int i=0;i<n;i+=m<<1) {
			Complex unit=1;
			for (int j=0;j<m;j++) {
				Complex &x=a[i+j+m],&y=a[i+j],t=unit*x;
				//x=y-t;
				//y=y+t;
				//unit=unit*w;
				tmp++;
			}
		}
		if (flag) printf("ccc %d\n",tmp);
	}
	if (flag) printf("aaa\n");
	if (oper==-1)
		for (int i=0;i<n;i++) a[i].x/=n;
	if (flag) printf("aaa\n");
}

void init() {
	for (int i=0;i<ls2;i++)
		if (s2[i]=='1') b[ls2-i-1]=1;
		else if (s2[i]=='0') b[ls2-i-1]=-1;
	FFT(b,N,1);
	for (int i=0;i<ls1;i+=M) {
		if (ls1>100) {
			printf("%d\n",i);
			if (i/M>37) return;
		}
		for (int j=0;j<M;j++)
			if (s2[i+j]=='1') a[j]=1;
			else if (s2[i+j]=='0') a[j]=-1;
		if (i/M==37) flag=true;
		FFT(a,N,1);
		for (int j=0;j<N;j++) a[j]=a[j]*b[j];
		FFT(a,N,-1);
		if (flag) printf("bbb %d\n",i);
		int *aa=res[i/M];
		for (int j=0;j<N;j++) aa[j]=a[j].x+0.5;
	}
}
int solve(int p1,int p2,int len) {
	int ans=0,i;
	for (i=0;i<len&&(p1+i)%M!=0;i++)
		if (s1[p1+i]!=s2[p2+i]) ans++;
	for (;i+M<len;i+=M)
		ans+=(M-res[(p1+i)/M][ls2-p2-i+1])/2;
	for (;i<len;i++)
		if (s1[p1+i]!=s2[p2+i]) ans++;
	return ans;
}
int main() {
	int q,p1,p2,len;
	scanf("%s%s",s1,s2);
	ls1=strlen(s1);
	ls2=strlen(s2);
	if (ls1>100)
		printf("%d %d\n",ls1,ls2);
	init();
	//if (ls1>100)
		//printf("aaa\n");
	scanf("%d",&q);
	while (q--) {
		scanf("%d%d%d",&p1,&p2,&len);
		if (ls1<100)
			printf("%d\n",solve(p1,p2,len));
	}
	return 0;
}


#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>

using namespace std;

const int N=1<<18;
const int M=2000;
const double PI=acos(-1.0);

struct Complex {
	double x,y;
	Complex(double xx=0,double yy=0):x(xx),y(yy) {}
	friend Complex operator + (const Complex &a,const Complex &b) {
		return Complex(a.x+b.x,a.y+b.y);
	}
	friend Complex operator - (const Complex &a,const Complex &b) {
		return Complex(a.x-b.x,a.y-b.y);
	}
	friend Complex operator * (const Complex &a,const Complex &b) {
		return Complex(a.x*b.x-a.y*b.y,a.x*b.y+a.y*b.x);
	}
};

char s1[N],s2[N];
int ls1,ls2;
int res[101][N];
Complex a[N],b[N];

void FFT(Complex a[],int n,int oper) {
	for (int i=1,j=0;i<n;i++) {
		for (int s=n;j^=s>>=1,~j&s;);
		if (i<j) swap(a[i],a[j]);
	}
	for (int m=1;m<n;m<<=1) {
		int m2=m<<1;
		double p=PI/m*oper;
		Complex w=Complex(cos(p),sin(p));
		for (int i=0;i<n;i+=m2) {
			Complex unit=1;
			for (int j=0;j<m;j++) {
				Complex &x=a[i+j+m],&y=a[i+j];
				Complex t=unit*x;
				x=y-t;
				y=y+t;
				unit=unit*w;
			}
		}
	}
	if (oper==-1)
		for (int i=0;i<n;i++) a[i].x/=n;
}

void init() {
	for (int i=0;i<ls2;i++)
		if (s2[i]=='1') b[ls2-i-1]=1;
		else if (s2[i]=='0') b[ls2-i-1]=-1;
	FFT(b,N,1);
	for (int i=0;i<ls1;i+=M) {
		if (ls1>100) {
			printf("%d\n",i);
			if (i/M>30) return;
		}
		for (int j=0;j<M;j++)
			if (s2[i+j]=='1') a[j]=1;
			else if (s2[i+j]=='0') a[j]=-1;
		FFT(a,N,1);
		for (int j=0;j<N;j++) a[j]=a[j]*b[j];
		FFT(a,N,-1);
		int *aa=res[i/M];
		for (int j=0;j<N;j++) aa[j]=a[j].x+0.5;
	}
	if (ls1>100)
		printf("bbb\n");
}
int solve(int p1,int p2,int len) {
	int ans=0,i;
	for (i=0;i<len&&(p1+i)%M!=0;i++)
		if (s1[p1+i]!=s2[p2+i]) ans++;
	for (;i+M<len;i+=M)
		ans+=(M-res[(p1+i)/M][ls2-p2-i+1])/2;
	for (;i<len;i++)
		if (s1[p1+i]!=s2[p2+i]) ans++;
	return ans;
}
int main() {
	int q,p1,p2,len;
	scanf("%s%s",s1,s2);
	ls1=strlen(s1);
	ls2=strlen(s2);
	if (ls1>100)
		printf("%d %d\n",ls1,ls2);
	init();
	//if (ls1>100)
		//printf("aaa\n");
	scanf("%d",&q);
	while (q--) {
		scanf("%d%d%d",&p1,&p2,&len);
		if (ls1<100)
			printf("%d\n",solve(p1,p2,len));
	}
	return 0;
}


#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>

using namespace std;

const int N=1<<18;
const int M=2000;
const double PI=acos(-1.0);

struct Complex {
	double x,y;
	Complex(double xx=0,double yy=0):x(xx),y(yy) {}
	friend Complex operator + (const Complex &a,const Complex &b) {
		return Complex(a.x+b.x,a.y+b.y);
	}
	friend Complex operator - (const Complex &a,const Complex &b) {
		return Complex(a.x-b.x,a.y-b.y);
	}
	friend Complex operator * (const Complex &a,const Complex &b) {
		return Complex(a.x*b.x-a.y*b.y,a.x*b.y+a.y*b.x);
	}
};

char s1[N],s2[N];
int ls1,ls2;
int res[101][N];
Complex a[N],b[N];
bool flag=false;

void FFT(Complex a[],int n,int oper) {
	if (flag) printf("%d\n",n);
	for (int i=1,j=0;i<n;i++) {
		for (int s=n;j^=s>>=1,~j&s;);
		if (i<j) swap(a[i],a[j]);
	}
	if (flag) printf("aaa\n");
	for (int m=1;m<n;m<<=1) {
		if (flag) printf("--%d\n",m);
		if (flag&&m>256) return;
		double p=PI/m*oper;
		Complex w=Complex(cos(p),sin(p));
		int tmp=0;
		for (int i=0;i<n;i+=m<<1) {
			Complex unit=1;
			for (int j=0;j<m;j++) {
				Complex &x=a[i+j+m],&y=a[i+j],t=unit*x;
				x=y-t;
				y=y+t;
				unit=unit*w;
				tmp++;
			}
		}
		if (flag) printf("ccc %d\n",tmp);
	}
	if (flag) printf("aaa\n");
	if (oper==-1)
		for (int i=0;i<n;i++) a[i].x/=n;
	if (flag) printf("aaa\n");
}

void init() {
	for (int i=0;i<ls2;i++)
		if (s2[i]=='1') b[ls2-i-1]=1;
		else if (s2[i]=='0') b[ls2-i-1]=-1;
	FFT(b,N,1);
	for (int i=0;i<ls1;i+=M) {
		if (i<36) continue;
		if (ls1>100) {
			printf("%d\n",i);
			if (i/M>37) return;
		}
		for (int j=0;j<M;j++)
			if (s2[i+j]=='1') a[j]=1;
			else if (s2[i+j]=='0') a[j]=-1;
		if (i/M==37) flag=true;
		FFT(a,N,1);
		for (int j=0;j<N;j++) a[j]=a[j]*b[j];
		FFT(a,N,-1);
		if (flag) printf("bbb %d\n",i);
		int *aa=res[i/M];
		for (int j=0;j<N;j++) aa[j]=a[j].x+0.5;
	}
}
int solve(int p1,int p2,int len) {
	int ans=0,i;
	for (i=0;i<len&&(p1+i)%M!=0;i++)
		if (s1[p1+i]!=s2[p2+i]) ans++;
	for (;i+M<len;i+=M)
		ans+=(M-res[(p1+i)/M][ls2-p2-i+1])/2;
	for (;i<len;i++)
		if (s1[p1+i]!=s2[p2+i]) ans++;
	return ans;
}
int main() {
	int q,p1,p2,len;
	scanf("%s%s",s1,s2);
	ls1=strlen(s1);
	ls2=strlen(s2);
	if (ls1>100)
		printf("%d %d\n",ls1,ls2);
	init();
	//if (ls1>100)
		//printf("aaa\n");
	scanf("%d",&q);
	while (q--) {
		scanf("%d%d%d",&p1,&p2,&len);
		if (ls1<100)
			printf("%d\n",solve(p1,p2,len));
	}
	return 0;
}


#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>

using namespace std;

const int N=1<<18;
const int M=2000;
const double PI=acos(-1.0);

struct Complex {
	double x,y;
	Complex(double xx=0,double yy=0):x(xx),y(yy) {}
	friend Complex operator + (const Complex &a,const Complex &b) {
		return Complex(a.x+b.x,a.y+b.y);
	}
	friend Complex operator - (const Complex &a,const Complex &b) {
		return Complex(a.x-b.x,a.y-b.y);
	}
	friend Complex operator * (const Complex &a,const Complex &b) {
		return Complex(a.x*b.x-a.y*b.y,a.x*b.y+a.y*b.x);
	}
};

char s1[N],s2[N];
int ls1,ls2;
int res[101][N];
Complex a[N],b[N];
bool flag=false;

void FFT(Complex a[],int n,int oper) {
	if (flag) printf("%d\n",n);
	for (int i=1,j=0;i<n;i++) {
		for (int s=n;j^=s>>=1,~j&s;);
		if (i<j) swap(a[i],a[j]);
	}
	if (flag) printf("aaa\n");
	for (int m=1;m<n;m<<=1) {
		if (flag) printf("--%d\n",m);
		if (flag&&m>4096) return;
		double p=PI/m*oper;
		Complex w=Complex(cos(p),sin(p));
		for (int i=0;i<n;i+=m<<1) {
			Complex unit=1;
			for (int j=0;j<m;j++) {
				Complex &x=a[i+j+m],&y=a[i+j];
				Complex t=unit*x;
				x=y-t;
				y=y+t;
				unit=unit*w;
			}
		}
	}
	if (flag) printf("aaa\n");
	if (oper==-1)
		for (int i=0;i<n;i++) a[i].x/=n;
	if (flag) printf("aaa\n");
}

void init() {
	for (int i=0;i<ls2;i++)
		if (s2[i]=='1') b[ls2-i-1]=1;
		else if (s2[i]=='0') b[ls2-i-1]=-1;
	FFT(b,N,1);
	for (int i=0;i<ls1;i+=M) {
		if (ls1>100) {
			printf("%d\n",i);
			if (i/M>37) return;
		}
		for (int j=0;j<M;j++)
			if (s2[i+j]=='1') a[j]=1;
			else if (s2[i+j]=='0') a[j]=-1;
		FFT(a,N,1);
		for (int j=0;j<N;j++) a[j]=a[j]*b[j];
		if (i/M==37) flag=true;
		FFT(a,N,-1);
		if (flag) printf("bbb %d\n",i);
		int *aa=res[i/M];
		for (int j=0;j<N;j++) aa[j]=a[j].x+0.5;
	}
}
int solve(int p1,int p2,int len) {
	int ans=0,i;
	for (i=0;i<len&&(p1+i)%M!=0;i++)
		if (s1[p1+i]!=s2[p2+i]) ans++;
	for (;i+M<len;i+=M)
		ans+=(M-res[(p1+i)/M][ls2-p2-i+1])/2;
	for (;i<len;i++)
		if (s1[p1+i]!=s2[p2+i]) ans++;
	return ans;
}
int main() {
	int q,p1,p2,len;
	scanf("%s%s",s1,s2);
	ls1=strlen(s1);
	ls2=strlen(s2);
	if (ls1>100)
		printf("%d %d\n",ls1,ls2);
	init();
	//if (ls1>100)
		//printf("aaa\n");
	scanf("%d",&q);
	while (q--) {
		scanf("%d%d%d",&p1,&p2,&len);
		if (ls1<100)
			printf("%d\n",solve(p1,p2,len));
	}
	return 0;
}


#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>

using namespace std;

const int N=1<<18;
const int M=2000;
const double PI=acos(-1.0);

struct Complex {
	double x,y;
	Complex(double xx=0,double yy=0):x(xx),y(yy) {}
	friend Complex operator + (const Complex &a,const Complex &b) {
		return Complex(a.x+b.x,a.y+b.y);
	}
	friend Complex operator - (const Complex &a,const Complex &b) {
		return Complex(a.x-b.x,a.y-b.y);
	}
	friend Complex operator * (const Complex &a,const Complex &b) {
		return Complex(a.x*b.x-a.y*b.y,a.x*b.y+a.y*b.x);
	}
};

char s1[N],s2[N];
int ls1,ls2;
int res[101][N];
Complex a[N],b[N];
bool flag=false;

void FFT(Complex a[],int n,int oper) {
	if (flag) printf("%d\n",n);
	for (int i=1,j=0;i<n;i++) {
		for (int s=n;j^=s>>=1,~j&s;);
		if (i<j) swap(a[i],a[j]);
	}
	if (flag) printf("aaa\n");
	for (int m=1;m<n;m<<=1) {
		if (flag) printf("--%d\n",m);
		if (flag&&m>128) return;
		double p=PI/m*oper;
		Complex w=Complex(cos(p),sin(p));
		for (int i=0;i<n;i+=m<<1) {
			Complex unit=1;
			for (int j=0;j<m;j++) {
				Complex &x=a[i+j+m],&y=a[i+j];
				Complex t=unit*x;
				x=y-t;
				y=y+t;
				unit=unit*w;
			}
		}
	}
	if (flag) printf("aaa\n");
	if (oper==-1)
		for (int i=0;i<n;i++) a[i].x/=n;
	if (flag) printf("aaa\n");
}

void init() {
	for (int i=0;i<ls2;i++)
		if (s2[i]=='1') b[ls2-i-1]=1;
		else if (s2[i]=='0') b[ls2-i-1]=-1;
	FFT(b,N,1);
	for (int i=0;i<ls1;i+=M) {
		if (ls1>100) {
			printf("%d\n",i);
			if (i/M>37) return;
		}
		for (int j=0;j<M;j++)
			if (s2[i+j]=='1') a[j]=1;
			else if (s2[i+j]=='0') a[j]=-1;
		FFT(a,N,1);
		for (int j=0;j<N;j++) a[j]=a[j]*b[j];
		if (i/M==36) flag=true;
		FFT(a,N,-1);
		int *aa=res[i/M];
		for (int j=0;j<N;j++) aa[j]=a[j].x+0.5;
	}
}
int solve(int p1,int p2,int len) {
	int ans=0,i;
	for (i=0;i<len&&(p1+i)%M!=0;i++)
		if (s1[p1+i]!=s2[p2+i]) ans++;
	for (;i+M<len;i+=M)
		ans+=(M-res[(p1+i)/M][ls2-p2-i+1])/2;
	for (;i<len;i++)
		if (s1[p1+i]!=s2[p2+i]) ans++;
	return ans;
}
int main() {
	int q,p1,p2,len;
	scanf("%s%s",s1,s2);
	ls1=strlen(s1);
	ls2=strlen(s2);
	if (ls1>100)
		printf("%d %d\n",ls1,ls2);
	init();
	//if (ls1>100)
		//printf("aaa\n");
	scanf("%d",&q);
	while (q--) {
		scanf("%d%d%d",&p1,&p2,&len);
		if (ls1<100)
			printf("%d\n",solve(p1,p2,len));
	}
	return 0;
}


#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>

using namespace std;

const int N=1<<18;
const int M=2000;
const double PI=acos(-1.0);

struct Complex {
	double x,y;
	Complex(double xx=0,double yy=0):x(xx),y(yy) {}
	friend Complex operator + (const Complex &a,const Complex &b) {
		return Complex(a.x+b.x,a.y+b.y);
	}
	friend Complex operator - (const Complex &a,const Complex &b) {
		return Complex(a.x-b.x,a.y-b.y);
	}
	friend Complex operator * (const Complex &a,const Complex &b) {
		return Complex(a.x*b.x-a.y*b.y,a.x*b.y+a.y*b.x);
	}
};

char s1[N],s2[N];
int ls1,ls2;
int res[101][N];
Complex a[N],b[N];
bool flag=false;

void FFT(Complex a[],int n,int oper) {
	if (flag) printf("%d\n",n);
	for (int i=1,j=0;i<n;i++) {
		for (int s=n;j^=s>>=1,~j&s;);
		if (i<j) swap(a[i],a[j]);
	}
	if (flag) printf("aaa\n");
	for (int m=1;m<n;m<<=1) {
		if (flag) printf("--%d\n",m);
		if (flag&&m>512) return;
		double p=PI/m*oper;
		Complex w=Complex(cos(p),sin(p));
		for (int i=0;i<n;i+=m<<1) {
			Complex unit=1;
			for (int j=0;j<m;j++) {
				Complex &x=a[i+j+m],&y=a[i+j];
				Complex t=unit*x;
				x=y-t;
				y=y+t;
				unit=unit*w;
			}
		}
	}
	if (flag) printf("aaa\n");
	if (oper==-1)
		for (int i=0;i<n;i++) a[i].x/=n;
	if (flag) printf("aaa\n");
}

void init() {
	for (int i=0;i<ls2;i++)
		if (s2[i]=='1') b[ls2-i-1]=1;
		else if (s2[i]=='0') b[ls2-i-1]=-1;
	FFT(b,N,1);
	for (int i=0;i<ls1;i+=M) {
		if (ls1>100) {
			printf("%d\n",i);
			if (i/M>37) return;
		}
		for (int j=0;j<M;j++)
			if (s2[i+j]=='1') a[j]=1;
			else if (s2[i+j]=='0') a[j]=-1;
		FFT(a,N,1);
		for (int j=0;j<N;j++) a[j]=a[j]*b[j];
		if (i/M==37) flag=true;
		FFT(a,N,-1);
		if (flag) printf("bbb %d\n",i);
		int *aa=res[i/M];
		for (int j=0;j<N;j++) aa[j]=a[j].x+0.5;
	}
}
int solve(int p1,int p2,int len) {
	int ans=0,i;
	for (i=0;i<len&&(p1+i)%M!=0;i++)
		if (s1[p1+i]!=s2[p2+i]) ans++;
	for (;i+M<len;i+=M)
		ans+=(M-res[(p1+i)/M][ls2-p2-i+1])/2;
	for (;i<len;i++)
		if (s1[p1+i]!=s2[p2+i]) ans++;
	return ans;
}
int main() {
	int q,p1,p2,len;
	scanf("%s%s",s1,s2);
	ls1=strlen(s1);
	ls2=strlen(s2);
	if (ls1>100)
		printf("%d %d\n",ls1,ls2);
	init();
	//if (ls1>100)
		//printf("aaa\n");
	scanf("%d",&q);
	while (q--) {
		scanf("%d%d%d",&p1,&p2,&len);
		if (ls1<100)
			printf("%d\n",solve(p1,p2,len));
	}
	return 0;
}


#include <cstdio>
#include <cstring>
#include <cmath>
#include <complex>
#include <algorithm>

using namespace std;

const int N=1<<18;
const int M=1000;
const double PI=acos(-1.0);

char s1[N],s2[N];
int ls1,ls2;
int res[201][N];
complex<double> a[N],b[N];

void FFT(complex<double> a[],int n,int oper) {
	for (int i=1,j=0;i<n;i++) {
		for (int s=n;j^=s>>=1,~j&s;);
		if (i<j) swap(a[i],a[j]);
	}
	for (int m=1;m<n;m<<=1) {
		int m2=m<<1;
		double p=PI/m*oper;
		complex<double> w=complex<double>(cos(p),sin(p));
		for (int i=0;i<n;i+=m2) {
			complex<double> unit=1;
			for (int j=0;j<m;j++) {
				complex<double> &x=a[i+j+m],&y=a[i+j];
				complex<double> t=unit*x;
				x=y-t;
				y=y+t;
				unit*=w;
			}
		}
	}
	if (oper==-1)
		for (int i=0;i<n;i++) a[i]/=n;
}

void init() {
	for (int i=0;i<ls2;i++)
		if (s2[i]=='1') b[ls2-i-1]=1;
		else if (s2[i]=='0') b[ls2-i-1]=-1;
	FFT(b,N,1);
	for (int i=0;i<ls1;i+=M) {
		for (int j=0;j<M;j++)
			if (s2[i+j]=='1') a[j]=1;
			else if (s2[i+j]=='0') a[j]=-1;
		FFT(a,N,1);
		for (int j=0;j<N;j++) a[j]*=b[j];
		FFT(a,N,-1);
		int *aa=res[i/M];
		for (int j=0;j<N;j++) aa[j]=a[j].real()+0.5;
	}
}
int solve(int p1,int p2,int len) {
	int ans=0,i;
	//for (i=0;i<len&&(p1+i)%M!=0;i++)
		//if (s1[p1+i]!=s2[p2+i]) ans++;
	//for (;i+M<len;i+=M)
		//ans+=(M-res[(p1+i)/M][ls2-p2-i+1])/2;
	for (;i<len;i++)
		if (s1[p1+i]!=s2[p2+i]) ans++;
	return ans;
}
int main() {
	int q,p1,p2,len;
	scanf("%s%s",s1,s2);
	ls1=strlen(s1);
	ls2=strlen(s2);
	init();
	scanf("%d",&q);
	while (q--) {
		scanf("%d%d%d",&p1,&p2,&len);
		if (ls1<100)
			printf("%d\n",solve(p1,p2,len));
	}
	return 0;
}


#include <cstdio>
#include <cstring>

using namespace std;

const int N=200001;
const int M=6250;

char s1[N],s2[N];
unsigned int a[M],b[M];
int ls1,ls2;

void init() {
	ls1=strlen(s1);
	ls2=strlen(s2);
	for (int i=0;i<ls1;i++)
		if (s1[i]=='1') a[i>>5]|=1u<<(i&31);
	for (int i=0;i<ls2;i++)
		if (s2[i]=='1') b[i>>5]|=1u<<(i&31);
}
int solve(int p1,int p2,int len) {
	int ans=0,i;
	for (i=0;i<len&&(p1+i&31);i++)
		if (s1[p1+i]!=s2[p2+i]) ans++;
	for (;i+32<=len;i+=32) {
		unsigned int t=p2+i&31,x=a[p1+i>>5],y=b[p2+i>>5]>>t,z=b[(p2+i>>5)+1]<<32-t;
		ans+=__builtin_popcount((x^y)<<t);
		if (t) ans+=__builtin_popcount((x^z)>>32-t);
	}
	for (;i<len;i++)
		if (s1[p1+i]!=s2[p2+i]) ans++;
	return ans;
}
int main() {
	int q,p1,p2,len;
	scanf("%s%s",s1,s2);
	init();
	scanf("%d",&q);
	while (q--) {
		scanf("%d%d%d",&p1,&p2,&len);
		printf("%d\n",solve(p1,p2,len));
	}
	return 0;
}


#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>

using namespace std;

const int N=1<<18;
const int M=2000;
const double PI=acos(-1.0);

struct Complex {
	double x,y;
	Complex(double xx=0,double yy=0):x(xx),y(yy) {}
	friend Complex operator + (const Complex &a,const Complex &b) {
		return Complex(a.x+b.x,a.y+b.y);
	}
	friend Complex operator - (const Complex &a,const Complex &b) {
		return Complex(a.x-b.x,a.y-b.y);
	}
	friend Complex operator * (const Complex &a,const Complex &b) {
		return Complex(a.x+b.x,a.y+b.y);
		return Complex(a.x*b.x-a.y*b.y,a.x*b.y+a.y*b.x);
	}
};

char s1[N],s2[N];
int ls1,ls2;
int res[101][N];
Complex a[N],b[N];
bool flag=false;

void FFT(Complex a[],int n,int oper) {
	if (flag) printf("%d\n",n);
	for (int i=1,j=0;i<n;i++) {
		for (int s=n;j^=s>>=1,~j&s;);
		if (i<j) swap(a[i],a[j]);
	}
	if (flag) printf("aaa\n");
	for (int m=1;m<n;m<<=1) {
		if (flag) printf("--%d\n",m);
		if (flag&&m>256) return;
		double p=PI/m*oper;
		Complex w=Complex(cos(p),sin(p));
		int tmp=0;
		for (int i=0;i<n;i+=m<<1) {
			Complex unit=1;
			for (int j=0;j<m;j++) {
				Complex &x=a[i+j+m],&y=a[i+j],t=unit*x;
				x=y-t;
				y=y+t;
				unit=unit*w;
				//aaa
				tmp++;
			}
		}
		if (flag) printf("ccc %d\n",tmp);
	}
	if (flag) printf("aaa\n");
	if (oper==-1)
		for (int i=0;i<n;i++) a[i].x/=n;
	if (flag) printf("aaa\n");
}

void init() {
	for (int i=0;i<ls2;i++)
		if (s2[i]=='1') b[ls2-i-1]=1;
		else if (s2[i]=='0') b[ls2-i-1]=-1;
	FFT(b,N,1);
	for (int i=0;i<ls1;i+=M) {
		//if (ls1>100) {
			//printf("%d\n",i);
			//if (i/M>37) return;
		//}
		for (int j=0;j<M;j++)
			if (s2[i+j]=='1') a[j]=1;
			else if (s2[i+j]=='0') a[j]=-1;
		//if (i/M==37) flag=true;
		FFT(a,N,1);
		for (int j=0;j<N;j++) a[j]=a[j]*b[j];
		FFT(a,N,-1);
		//if (flag) printf("bbb %d\n",i);
		int *aa=res[i/M];
		for (int j=0;j<N;j++) aa[j]=a[j].x+0.5;
	}
}
int solve(int p1,int p2,int len) {
	int ans=0,i;
	for (i=0;i<len&&(p1+i)%M!=0;i++)
		if (s1[p1+i]!=s2[p2+i]) ans++;
	for (;i+M<len;i+=M)
		ans+=(M-res[(p1+i)/M][ls2-p2-i+1])/2;
	for (;i<len;i++)
		if (s1[p1+i]!=s2[p2+i]) ans++;
	return ans;
}
int main() {
	int q,p1,p2,len;
	scanf("%s%s",s1,s2);
	ls1=strlen(s1);
	ls2=strlen(s2);
	if (ls1>100)
		printf("%d %d\n",ls1,ls2);
	init();
	//if (ls1>100)
		//printf("aaa\n");
	scanf("%d",&q);
	while (q--) {
		scanf("%d%d%d",&p1,&p2,&len);
		if (ls1<100)
			printf("%d\n",solve(p1,p2,len));
	}
	return 0;
}


#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>

using namespace std;

const int N=1<<18;
const int M=2000;
const double PI=acos(-1.0);

struct Complex {
	double x,y;
	Complex(double xx=0,double yy=0):x(xx),y(yy) {}
	friend Complex operator + (const Complex &a,const Complex &b) {
		return Complex(a.x+b.x,a.y+b.y);
	}
	friend Complex operator - (const Complex &a,const Complex &b) {
		return Complex(a.x-b.x,a.y-b.y);
	}
	friend Complex operator * (const Complex &a,const Complex &b) {
		return Complex(a.x*b.x-a.y*b.y,a.x*b.y+a.y*b.x);
	}
};

char s1[N],s2[N];
int ls1,ls2;
int res[101][N];
Complex a[N],b[N];
bool flag=false;

void FFT(Complex a[],int n,int oper) {
	if (flag) printf("%d\n",n);
	for (int i=1,j=0;i<n;i++) {
		for (int s=n;j^=s>>=1,~j&s;);
		if (i<j) swap(a[i],a[j]);
	}
	if (flag) printf("aaa\n");
	for (int m=1;m<n;m<<=1) {
		if (flag) printf("--%d\n",m);
		if (flag&&m>256) return;
		double p=PI/m*oper;
		Complex w=Complex(cos(p),sin(p));
		int tmp=0;
		for (int i=0;i<n;i+=m<<1) {
			Complex unit=1;
			for (int j=0;j<m;j++) {
				//Complex &x=a[i+j+m],&y=a[i+j],t=unit*x;
				//x=y-t;
				//y=y+t;
				//unit=unit*w;
				tmp++;
			}
		}
		if (flag) printf("ccc %d\n",tmp);
	}
	if (flag) printf("aaa\n");
	if (oper==-1)
		for (int i=0;i<n;i++) a[i].x/=n;
	if (flag) printf("aaa\n");
}

void init() {
	for (int i=0;i<ls2;i++)
		if (s2[i]=='1') b[ls2-i-1]=1;
		else if (s2[i]=='0') b[ls2-i-1]=-1;
	FFT(b,N,1);
	for (int i=0;i<ls1;i+=M) {
		if (ls1>100) {
			printf("%d\n",i);
			if (i/M>37) return;
		}
		for (int j=0;j<M;j++)
			if (s2[i+j]=='1') a[j]=1;
			else if (s2[i+j]=='0') a[j]=-1;
		if (i/M==37) flag=true;
		FFT(a,N,1);
		for (int j=0;j<N;j++) a[j]=a[j]*b[j];
		FFT(a,N,-1);
		if (flag) printf("bbb %d\n",i);
		int *aa=res[i/M];
		for (int j=0;j<N;j++) aa[j]=a[j].x+0.5;
	}
}
int solve(int p1,int p2,int len) {
	int ans=0,i;
	for (i=0;i<len&&(p1+i)%M!=0;i++)
		if (s1[p1+i]!=s2[p2+i]) ans++;
	for (;i+M<len;i+=M)
		ans+=(M-res[(p1+i)/M][ls2-p2-i+1])/2;
	for (;i<len;i++)
		if (s1[p1+i]!=s2[p2+i]) ans++;
	return ans;
}
int main() {
	int q,p1,p2,len;
	scanf("%s%s",s1,s2);
	ls1=strlen(s1);
	ls2=strlen(s2);
	if (ls1>100)
		printf("%d %d\n",ls1,ls2);
	init();
	//if (ls1>100)
		//printf("aaa\n");
	scanf("%d",&q);
	while (q--) {
		scanf("%d%d%d",&p1,&p2,&len);
		if (ls1<100)
			printf("%d\n",solve(p1,p2,len));
	}
	return 0;
}


#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>

using namespace std;

const int N=1<<18;
const int M=2000;
const double PI=acos(-1.0);

struct Complex {
	double x,y;
	Complex(double xx=0,double yy=0):x(xx),y(yy) {}
	friend Complex operator + (const Complex &a,const Complex &b) {
		//return Complex(a.x+1,a.y+1);
		return Complex(a.x+b.x,a.y+b.y);
	}
	friend Complex operator - (const Complex &a,const Complex &b) {
		//return Complex(a.x+1,a.y+1);
		return Complex(a.x-b.x,a.y-b.y);
	}
	friend Complex operator * (const Complex &a,const Complex &b) {
		//return Complex(a.x+1,a.y+1);
		return Complex(a.x*b.x-a.y*b.y,a.x*b.y+a.y*b.x);
	}
};

char s1[N],s2[N];
int ls1,ls2;
int res[101][N];
Complex a[N],b[N];
bool flag=false;

void FFT(Complex a[],int n,int oper) {
	if (flag) printf("%d\n",n);
	for (int i=1,j=0;i<n;i++) {
		for (int s=n;j^=s>>=1,~j&s;);
		if (i<j) swap(a[i],a[j]);
	}
	if (flag) printf("aaa\n");
	for (int m=1;m<n;m<<=1) {
		if (flag) printf("--%d\n",m);
		if (flag&&m>256) return;
		double p=PI/m*oper;
		Complex w=Complex(cos(p),sin(p));
		int tmp=0;
		for (int i=0;i<n;i+=m<<1) {
			Complex unit=1;
			for (int j=0;j<m;j++) {
				Complex &x=a[i+j+m],&y=a[i+j],t=unit*x;
				x=y-t;
				y=y+t;
				unit=unit*w;
				//aaa
				tmp++;
			}
		}
		if (flag) printf("ccc %d\n",tmp);
	}
	if (flag) printf("aaa\n");
	if (oper==-1)
		for (int i=0;i<n;i++) a[i].x/=n;
	if (flag) printf("aaa\n");
}

void init() {
	for (int i=0;i<ls2;i++)
		if (s2[i]=='1') b[ls2-i-1]=1;
		else if (s2[i]=='0') b[ls2-i-1]=-1;
	FFT(b,N,1);
	for (int i=0;i<ls1;i+=M) {
		//if (ls1>100) {
			//printf("%d\n",i);
			//if (i/M>37) return;
		//}
		for (int j=0;j<M;j++)
			if (s2[i+j]=='1') a[j]=1;
			else if (s2[i+j]=='0') a[j]=-1;
		//if (i/M==37) flag=true;
		FFT(a,N,1);
		for (int j=0;j<N;j++) a[j]=a[j]*b[j];
		FFT(a,N,-1);
		//if (flag) printf("bbb %d\n",i);
		int *aa=res[i/M];
		for (int j=0;j<N;j++) aa[j]=a[j].x+0.5;
	}
}
int solve(int p1,int p2,int len) {
	int ans=0,i;
	for (i=0;i<len&&(p1+i)%M!=0;i++)
		if (s1[p1+i]!=s2[p2+i]) ans++;
	for (;i+M<len;i+=M)
		ans+=(M-res[(p1+i)/M][ls2-p2-i+1])/2;
	for (;i<len;i++)
		if (s1[p1+i]!=s2[p2+i]) ans++;
	return ans;
}
int main() {
	int q,p1,p2,len;
	scanf("%s%s",s1,s2);
	ls1=strlen(s1);
	ls2=strlen(s2);
	if (ls1>100)
		printf("%d %d\n",ls1,ls2);
	init();
	//if (ls1>100)
		//printf("aaa\n");
	scanf("%d",&q);
	while (q--) {
		scanf("%d%d%d",&p1,&p2,&len);
		if (ls1<100)
			printf("%d\n",solve(p1,p2,len));
	}
	return 0;
}


#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>

using namespace std;

const int N=1<<18;
const int M=2000;
const double PI=acos(-1.0);

struct Complex {
	double x,y;
	Complex(double xx=0,double yy=0):x(xx),y(yy) {}
	friend Complex operator + (const Complex &a,const Complex &b) {
		return Complex(a.x+b.x,a.y+b.y);
	}
	friend Complex operator - (const Complex &a,const Complex &b) {
		return Complex(a.x-b.x,a.y-b.y);
	}
	friend Complex operator * (const Complex &a,const Complex &b) {
		return Complex(a.x*b.x-a.y*b.y,a.x*b.y+a.y*b.x);
	}
};

char s1[N],s2[N];
int ls1,ls2;
int res[101][N];
Complex a[N],b[N];

void FFT(Complex a[],int n,int oper) {
	for (int i=1,j=0;i<n;i++) {
		for (int s=n;j^=s>>=1,~j&s;);
		if (i<j) swap(a[i],a[j]);
	}
	for (int m=1;m<n;m<<=1) {
		int m2=m<<1;
		double p=PI/m*oper;
		Complex w=Complex(cos(p),sin(p));
		for (int i=0;i<n;i+=m2) {
			Complex unit=1;
			for (int j=0;j<m;j++) {
				Complex &x=a[i+j+m],&y=a[i+j];
				Complex t=unit*x;
				x=y-t;
				y=y+t;
				unit=unit*w;
			}
		}
	}
	if (oper==-1)
		for (int i=0;i<n;i++) a[i].x/=n;
}

void init() {
	for (int i=0;i<ls2;i++)
		if (s2[i]=='1') b[ls2-i-1]=1;
		else if (s2[i]=='0') b[ls2-i-1]=-1;
	FFT(b,N,1);
	for (int i=0;i<ls1;i+=M) {
		if (ls1>100) {
			printf("%d\n",i);
			if (i/M>20) return;
		}
		for (int j=0;j<M;j++)
			if (s2[i+j]=='1') a[j]=1;
			else if (s2[i+j]=='0') a[j]=-1;
		FFT(a,N,1);
		for (int j=0;j<N;j++) a[j]=a[j]*b[j];
		FFT(a,N,-1);
		int *aa=res[i/M];
		for (int j=0;j<N;j++) aa[j]=a[j].x+0.5;
	}
}
int solve(int p1,int p2,int len) {
	int ans=0,i;
	for (i=0;i<len&&(p1+i)%M!=0;i++)
		if (s1[p1+i]!=s2[p2+i]) ans++;
	for (;i+M<len;i+=M)
		ans+=(M-res[(p1+i)/M][ls2-p2-i+1])/2;
	for (;i<len;i++)
		if (s1[p1+i]!=s2[p2+i]) ans++;
	return ans;
}
int main() {
	int q,p1,p2,len;
	scanf("%s%s",s1,s2);
	ls1=strlen(s1);
	ls2=strlen(s2);
	if (ls1>100)
		printf("%d %d\n",ls1,ls2);
	init();
	//if (ls1>100)
		//printf("aaa\n");
	scanf("%d",&q);
	while (q--) {
		scanf("%d%d%d",&p1,&p2,&len);
		if (ls1<100)
			printf("%d\n",solve(p1,p2,len));
	}
	return 0;
}