#include <bits/stdc++.h>
using namespace std;
long long int t = 1, T;
long long int mod = 1e9 + 7;
void print(vector<long long int> a) {
  for (long long int x : a) {
    cout << x << ' ';
  }
  cout << '\n';
  return;
}
void swap(long long int &n, long long int &m) {
  n = n ^ m;
  m = n ^ m;
  n = n ^ m;
}
void solve() {
  string s;
  cin >> s;
  long long int x;
  cin >> x;
  long long int n = s.size();
  string ans(n, '2');
  for (long long int i = 0; i < n; i++) {
    if (s[i] == '1') {
      if (i + x >= n && i - x < 0) {
        cout << -1 << '\n';
        return;
      } else if (i + x >= n && i - x >= 0) {
        ans[i - x] = '1';
      } else if (i + x < n && i - x < 0) {
        ans[i + x] = '1';
      } else if (i + x < n && i - x >= 0) {
        ans[i + x] = '3';
        ans[i - x] = '3';
      }
    }
  }
  for (long long int i = 0; i < n; i++) {
    if (s[i] == '0') {
      if (i + x < n && i - x >= 0) {
        if (ans[i - x] == '1' || ans[i + x] == '1') {
          cout << -1 << '\n';
          return;
        }
        ans[i - x] = '0';
        ans[i + x] = '0';
      } else if (i + x >= n && i - x >= 0) {
        if (ans[i - x] == '1') {
          cout << -1 << '\n';
          return;
        }
        ans[i - x] = '0';
      } else if (i + x < n && i - x < 0) {
        if (ans[i + x] == '1') {
          cout << -1 << '\n';
          return;
        }
        ans[i + x] = '0';
      }
    }
  }
  for (long long int i = 0; i < n; i++) {
    if (s[i] == '1') {
      if (i + x < n && i - x >= 0) {
        if (ans[i + x] == '0' && ans[i - x] == '0') {
          cout << -1 << '\n';
          return;
        }
        if (ans[i + x] != '0') ans[i + x] = '1';
        if (ans[i - x] != '0') ans[i - x] = '1';
      }
    }
  }
  for (long long int i = 0; i < n; i++) {
    if (ans[i] == '2') ans[i] = '1';
  }
  cout << ans << '\n';
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> t;
  T = t;
  while (t--) {
    solve();
  }
  return 0;
}
