#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    string s;
    cin >> s;
    unsigned long long x;
    cin >> x;
    unsigned long long n = s.length();
    char w[n];
    for (unsigned long long i = 0; i < n; ++i) w[i] = '-';
    for (unsigned long long i = 0; i < n; ++i) {
      if (s[i] == '0') {
        if ((i + x) < n) w[i + x] = '0';
        if (i >= x) w[i - x] = '0';
      }
    }
    bool flag = true;
    unsigned long long k = 0;
    for (unsigned long long i = 0; i < n; ++i) {
      k = 0;
      if (s[i] == '1') {
        if ((i + x) < n) {
          if (w[i + x] == '-') k++;
        }
        if (i >= x) {
          if (w[i - x] == '-') k++;
        }
        if (k == 0) {
          flag = false;
          break;
        }
      }
    }
    if (!flag)
      cout << -1 << endl;
    else {
      for (unsigned long long i = 0; i < n; ++i) {
        if (w[i] == '-') w[i] = '1';
      }
      for (unsigned long long i = 0; i < n; ++i) cout << w[i];
      cout << endl;
    }
  }
}
