#include <bits/stdc++.h>
using namespace std;
inline int Isdigit(char c) {
  if (c < '0' || c > '9') return 0;
  return 1;
}
template <class T>
T read() {
  register T x = 0, flag = 1;
  register char ch;
  while (!Isdigit(ch = getchar()))
    if (ch == '-') flag = -1;
  while (Isdigit(ch)) x = x * 10 + (ch & 15), ch = getchar();
  return x * flag;
}
template <class T>
inline void write(T x) {
  if (x < 0) putchar('-'), x = -x;
  if (x >= 10) write(x / 10);
  putchar(x % 10 + '0');
}
template <class T>
inline bool Chkmax(T& x, const T& y) {
  return x < y ? x = y, true : false;
}
template <class T>
inline bool Chkmin(T& x, const T& y) {
  return x > y ? x = y, true : false;
}
const int maxn = 202020;
int T, x;
string s;
int tag[maxn];
int Id(int x) {
  if (x < 0 || x >= s.size()) return 0;
  if (tag[x] == 0) return 0;
  return 1;
}
int main() {
  T = read<int>();
  while (T--) {
    cin >> s;
    x = read<int>();
    for (int i = 0; i < s.size(); i++) tag[i] = -1;
    for (int i = 0; i < s.size(); i++) {
      if (s[i] == '0') {
        if (i - x >= 0) tag[i - x] = 0;
        if (i + x <= s.size() - 1) tag[i + x] = 0;
      }
    }
    int ok = 0;
    for (int i = 0; i < s.size(); i++) {
      if (s[i] == '1') {
        if ((Id(i - x) | Id(x + i)) == 0) ok = 1;
      }
    }
    if (ok == 1)
      puts("-1");
    else {
      for (int i = 0; i < s.size(); i++) {
        if (tag[i] == -1)
          printf("%d", 1);
        else
          printf("%d", 0);
      }
      puts("");
    }
  }
}
